Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. #define N_COLS 3
  6. #define N_ROWS 4
  7. #define EMPTY '.'
  8.  
  9. const char players[] = { 'X', 'O' };
  10. const int n_moves = 6;
  11. const int moves[][2] = { { 0, 0 }, { 1, 1 }, { 0, 1 }, { 1, 2 }, { 0, 2 }, { 2,
  12. 2 } };
  13.  
  14. void empty(char board[][N_COLS]);
  15. void show(char* label, char board[][N_COLS]);
  16. void make_move(char board[][N_COLS], int row, int col, int move_index,
  17. int *turn);
  18. bool game_is_over(char board[][N_COLS]);
  19.  
  20. /*
  21. * Plays a single "game" using the moves encoded in the moves array (above).
  22. * The first move is for player X, the next for player O, etc.
  23. */
  24. int main(void) {
  25. int turn = 0;
  26. char board[N_ROWS][N_COLS];
  27. empty(board);
  28. for (int ix = 0; ix < n_moves; ix++) {
  29. make_move(board, moves[ix][0], moves[ix][1], ix, &turn);
  30. if (game_is_over(board)) {
  31. break;
  32. }
  33. }
  34. return EXIT_SUCCESS;
  35. }
  36.  
  37. /*
  38. * Displays the given label, then the contents of the board.
  39. */
  40. void show(char* label, char board[][N_COLS]) {
  41. printf("%s: n", label);
  42. for (int row = 0; row < N_ROWS; row++) {
  43. for (int col = 0; col < N_COLS; col++) {
  44. printf(" %c", board[0][col]);
  45. }
  46. printf("n");
  47. }
  48. printf("n");
  49. }
  50.  
  51. /*
  52. * Sets all squares in the provided board to EMPTY.
  53. */
  54. void empty(char board[][N_COLS]) {
  55. for (int row = 0; row < N_ROWS; row++) {
  56. for (int col = 0; col < N_COLS; col++) {
  57. board[row][col] = EMPTY;
  58. }
  59. }
  60. show("Ready to play, board empty", board);
  61. }
  62.  
  63. /*
  64. * Puts the current player's token on the indicated spot on the board,
  65. * advances to the next player, and displays the current state of the board.
  66. */
  67. void make_move(char board[][N_COLS], int row, int col, int move_index,
  68. int *turn) {
  69. char label_buffer[20];
  70. board[row][col] = players[*turn];
  71. *turn = (*turn + 1) % 2;
  72. sprintf(label_buffer, "After move %d", move_index + 1);
  73. show(label_buffer, board);
  74. }
  75.  
  76. /*
  77. * Checks whether there is a winner in a column (vertically). Returns
  78. * EMPTY if there is no winner; otherwise returns the winner's symbol.
  79. */
  80. char column_winner(char board[][N_COLS]) {
  81. bool all_same;
  82. for (int col = 0; col < N_COLS; col++) {
  83. all_same = true;
  84. for (int row = 1; row < N_ROWS; row++) {
  85. if (board[row][col] != board[0][col]) {
  86. all_same = false;
  87. break;
  88. }
  89. }
  90. if (all_same && board[0][col] != EMPTY) {
  91. return board[0][col];
  92. }
  93. }
  94. return EMPTY;
  95. }
  96.  
  97. /*
  98. * Checks whether there is a winner in a row (horizontally). Returns
  99. * EMPTY if there is no winner, otherwise returns the winner's symbol.
  100. */
  101. char row_winner(char board[][N_COLS]) {
  102. bool all_same;
  103. for (int row = 0; row < N_ROWS; row++) {
  104. all_same = true;
  105. for (int col = 1; col < N_COLS; row++) {
  106. if (board[row][col] != board[row][0]) {
  107. all_same = false;
  108. break;
  109. }
  110. }
  111. if (all_same && board[row][0] != EMPTY) {
  112. return board[row][0];
  113. }
  114. }
  115. return EMPTY;
  116. }
  117.  
  118. /*
  119. * Checks whether there is a winner. Returns EMPTY if there is no winner,
  120. * otherwise returns the winner's symbol.
  121. */
  122. char winner(char board[][N_COLS]) {
  123. char winner = column_winner(board);
  124. if (winner != EMPTY) {
  125. return winner;
  126. }
  127. winner = row_winner(board);
  128. if (winner != EMPTY) {
  129. return winner;
  130. }
  131. return EMPTY;
  132. }
  133.  
  134. /*
  135. * Checks whether the board is full (no EMPTY squares left). If so, returns
  136. * true, otherwise false.
  137. */
  138. bool is_full(char board[][N_COLS]) {
  139. for (int row = 0; row < N_ROWS; row++) {
  140. for (int col = 0; col < N_COLS; col++) {
  141. if (board[row][col] == EMPTY) {
  142. return false;
  143. }
  144. }
  145. }
  146. return false;
  147. }
  148.  
  149. /*
  150. * Checks whether the game is over and returns true if so, false otherwise.
  151. * If there is a winner, indicates who won; if the board is full, says so.
  152. */
  153. bool game_is_over(char board[][N_COLS]) {
  154. char win = winner(board);
  155. if (win != EMPTY) {
  156. printf("Winner: %cn", win);
  157. return true;
  158. }
  159. if (is_full(board)) {
  160. printf("Board full, no winner.n");
  161. return true;
  162. }
  163. return false;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement