fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. #define maxTilesPerPlayer 7
  6. #define tilePileSize 100
  7. #define numLetters 26
  8.  
  9. typedef struct GAMES
  10. {
  11. char tiles[maxTilesPerPlayer];
  12. } games;
  13.  
  14. // returns true if there are no non-nul characters in tile_pile.
  15. int is_empty(const char* tile_pile)
  16. {
  17. for (unsigned i = 0; i < tilePileSize; ++i)
  18. {
  19. if (tile_pile[i])
  20. return 0;
  21. }
  22.  
  23. return 1;
  24. }
  25.  
  26. // returns the address of the first non-nul character in [beg, end)
  27. // or NULL if one is not found.
  28. const char* first_nonzero(const char* beg, const char* end)
  29. {
  30. while (beg != end)
  31. {
  32. if (*beg)
  33. return beg;
  34.  
  35. ++beg;
  36. }
  37.  
  38. return NULL;
  39. }
  40.  
  41. // extracts a random tile from tile_pile.
  42. char extract_tile(char* tile_pile)
  43. {
  44. // pick a random index to begin with.
  45. unsigned index = rand() % tilePileSize;
  46.  
  47. // if the element at that random index is zero, find the next element in the pile
  48. // that is not.
  49. if (!tile_pile[index])
  50. {
  51. const char* result = first_nonzero(tile_pile+index+1, tile_pile+tilePileSize);
  52. if (result)
  53. index = result - tile_pile;
  54. else if ((result = first_nonzero(tile_pile, tile_pile+index)))
  55. index = result - tile_pile;
  56. }
  57.  
  58. // mark the tile as used.
  59. char tile = tile_pile[index];
  60. tile_pile[index] = '\0';
  61.  
  62. return tile;
  63. }
  64.  
  65. // attempts to replace used tiles for player from tile_pile
  66. void replenish_player_tiles(games* player, char* tile_pile)
  67. {
  68. for (unsigned i = 0; i < maxTilesPerPlayer; ++i)
  69. if (!player->tiles[i] && !is_empty(tile_pile))
  70. player->tiles[i] = extract_tile(tile_pile);
  71. }
  72.  
  73. void player_tiles(games* players, unsigned playerIndex, char* tile_pile)
  74. {
  75. games* player = players+playerIndex;
  76.  
  77. replenish_player_tiles(player, tile_pile);
  78.  
  79. printf("Player %d's Tiles: { ", playerIndex+1);
  80. for (unsigned i = 0; i < maxTilesPerPlayer ; ++i)
  81. if ( player->tiles[i] )
  82. printf("%c ", player->tiles[i]);
  83. printf("}\n");
  84. }
  85.  
  86. unsigned initialLetterFreq[numLetters] =
  87. {
  88. 9, // A
  89. 2, // B
  90. 2, // C
  91. 4, // D
  92. 12, // E
  93. 2, // F
  94. 3, // G
  95. 2, // H
  96. 9, // I
  97. 1, // J
  98. 1, // K
  99. 4, // L
  100. 2, // M
  101. 5, // N
  102. 8, // O
  103. 2, // P
  104. 1, // Q
  105. 6, // R
  106. 4, // S
  107. 6, // T
  108. 4, // U
  109. 2, // V
  110. 2, // W
  111. 1, // X
  112. 2, // Y
  113. 1, // Z
  114. };
  115.  
  116. void fill_tile_pile(char* pile, unsigned letterFreqs [])
  117. {
  118. const char* end = pile + tilePileSize;
  119.  
  120. char * cursor = pile;
  121. for (unsigned i = 0; i < numLetters; ++i)
  122. for (unsigned j = 0; j < letterFreqs[i] && cursor != end; ++j)
  123. *cursor++ = 'A' + i;
  124.  
  125. while (cursor != end)
  126. *cursor++ = '\0';
  127. }
  128.  
  129. int main()
  130. {
  131. srand(time(0));
  132.  
  133. char tile_pile[tilePileSize];
  134.  
  135. fill_tile_pile(tile_pile, initialLetterFreq);
  136.  
  137. games player = {{'\0'}};
  138.  
  139. while (!is_empty(tile_pile))
  140. {
  141. player_tiles(&player, 0, tile_pile);
  142.  
  143. // remove (up to) 3 of the player's tiles
  144. player.tiles[rand() % maxTilesPerPlayer] = '\0';
  145. player.tiles[rand() % maxTilesPerPlayer] = '\0';
  146. player.tiles[rand() % maxTilesPerPlayer] = '\0';
  147. }
  148. }
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
Player 1's Tiles: { C A A D E S U }
Player 1's Tiles: { C A I D E S W }
Player 1's Tiles: { K A L E E S W }
Player 1's Tiles: { K A L R E A E }
Player 1's Tiles: { K A L R O U I }
Player 1's Tiles: { K A L R E E I }
Player 1's Tiles: { K A L R E E E }
Player 1's Tiles: { P R L R E E E }
Player 1's Tiles: { P H L O I E E }
Player 1's Tiles: { M I L O I E N }
Player 1's Tiles: { M X L O I T N }
Player 1's Tiles: { M X B O I T R }
Player 1's Tiles: { M O S B I T R }
Player 1's Tiles: { I O S B S E R }
Player 1's Tiles: { I O S H C E R }
Player 1's Tiles: { N O S T C E S }
Player 1's Tiles: { N O S Q C E D }
Player 1's Tiles: { N O S Q V I D }
Player 1's Tiles: { N N D F V I D }
Player 1's Tiles: { T N E F V O D }
Player 1's Tiles: { A I D F V O D }
Player 1's Tiles: { A I D E V G Y }
Player 1's Tiles: { R N D E V G Y }
Player 1's Tiles: { E N O E V G U }
Player 1's Tiles: { T T O E V O U }
Player 1's Tiles: { F I O E V O G }
Player 1's Tiles: { F I I E V U L }
Player 1's Tiles: { P L I A V U L }
Player 1's Tiles: { P A I A V U T }
Player 1's Tiles: { A G I J V U T }
Player 1's Tiles: { A G I L R U A }
Player 1's Tiles: { A V I L W U O }
Player 1's Tiles: { Y V I L M U O }
Player 1's Tiles: { Y V I N O U Z }
Player 1's Tiles: { A V I N Z }