tictac.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * These are some constants we use in our program.
  31. * EMPTY is used to indicate empty spaces in the board.
  32. * X_MARKER and O_MARKER are used to indicate where each
  33. * players has moved
  34. * DIM indicates the size of the board. For now
  35. * we assume a conventional 3x3 playing board.
  36. *
  37. * This should work once the basic system calls are complete.
  38. */
  39. #include <stdio.h>
  40. #include <unistd.h>
  41. #define NEWLINE 012
  42. #define EMPTY 0
  43. #define X_PLAYER 1
  44. #define O_PLAYER 2
  45. #define X_MARKER 1
  46. #define O_MARKER 2
  47. #define DIM 3
  48. #define DIMCHAR "2"
  49. #define MAXSTRING 100
  50. typedef enum { FALSE, TRUE } bool;
  51. /* Function Declarations */
  52. bool ask_yesno(const char *msg);
  53. bool do_move(int player);
  54. void initialize_board(void);
  55. bool is_win(int x, int y);
  56. int read_string(char *buf, int length);
  57. void print_board(void);
  58. void print_instructions(void);
  59. bool win_column(int y, int marker);
  60. bool win_diag_left(int x, int y, int marker);
  61. bool win_diag_right(int x, int y, int marker);
  62. bool win_row(int x, int marker);
  63. bool Strcmp(const char *a, const char *b);
  64. /*
  65. * The board is gloabally defined.
  66. */
  67. int board[DIM][DIM];
  68. /* Console I/O routines */
  69. int
  70. main()
  71. {
  72. bool win = FALSE;
  73. int move, max_moves;
  74. int player;
  75. print_instructions();
  76. max_moves = DIM * DIM; /* Maximum number of moves in a game */
  77. while (TRUE) {
  78. initialize_board();
  79. for (move = 1; move <= max_moves; move++) {
  80. player = move % 2 == 0 ? 2 : 1;
  81. win = do_move(player);
  82. print_board();
  83. if (win) {
  84. printf("Player %d, you WON!\n\n", player);
  85. break; /* out of for loop */
  86. }
  87. }
  88. /*
  89. * If we got here by falling through the loop, then it is a
  90. * tie game.
  91. */
  92. if (!win)
  93. printf("Tie Game!\n\n");
  94. if (!ask_yesno("Do you wish to play again?"))
  95. break; /* out of while loop */
  96. }
  97. return 0;
  98. }
  99. /*
  100. * print_instructions
  101. * Displays the instructions for the game.
  102. * Input
  103. * None
  104. * Output
  105. * None
  106. * Error
  107. * None
  108. */
  109. void
  110. print_instructions(void)
  111. {
  112. printf("Welcome to tic-tac-toe!\n");
  113. printf("Player 1 always plays X and player 2 always play O\n");
  114. printf("Good luck!\n\n\n");
  115. }
  116. void
  117. /*
  118. * print_board
  119. * Display the DIM by DIM board.
  120. * Input
  121. * None.
  122. * Output
  123. * None.
  124. * Errors
  125. * None.
  126. */
  127. print_board(void)
  128. {
  129. int i, j;
  130. /* Print labels across the top */
  131. printf("\n 0 1 2\n");
  132. for (i = 0; i < DIM; i++) {
  133. /* Print row labels */
  134. printf(" %d ", i);
  135. for (j = 0; j < DIM; j++) {
  136. switch (board[i][j]) {
  137. case EMPTY: printf(" "); break;
  138. case X_MARKER: printf(" X "); break;
  139. case O_MARKER: printf(" O "); break;
  140. default: printf("???"); break;
  141. }
  142. }
  143. printf("\n");
  144. }
  145. printf("\n");
  146. }
  147. /*
  148. * ask_yesno (taken from histo.c)
  149. * This function prints out the message and asks the user to respond
  150. * with either a yes or a no. It continues asking the questions until
  151. * a valid reply is encountered and returns TRUE/FALSE indicating
  152. * the answer (True for yes, false for no).
  153. *
  154. * Input
  155. * Question to be asked.
  156. * Output
  157. * TRUE if response is yes
  158. * FALSE if response is no
  159. * Error
  160. * None
  161. */
  162. bool
  163. ask_yesno(const char *msg)
  164. {
  165. char answer[MAXSTRING];
  166. while (TRUE) {
  167. printf("%s [yes/no] ", msg);
  168. if (read_string(answer, MAXSTRING) < 0)
  169. return(FALSE);
  170. if (Strcmp(answer, "yes"))
  171. return(TRUE);
  172. else if (Strcmp(answer, "no"))
  173. return(FALSE);
  174. else
  175. printf("Please answer either yes or no\n");
  176. }
  177. }
  178. /*
  179. * do_move
  180. * Processes one player move. The player enters a row and column
  181. * and we have to determine if the square is valid (on the board)
  182. * and that there is no mark already there. We continue to ask
  183. * for row/column pairs until we receive a valid combination.
  184. * Then we mark the board, check for a win, and return.
  185. *
  186. * Input
  187. * player Indicates which player (1 or 2) is moving
  188. * Output
  189. * TRUE if this move won the game.
  190. * FALSE if this move did not win the game.
  191. * Error
  192. * None
  193. */
  194. bool
  195. do_move(int player)
  196. {
  197. int x, y;
  198. bool first;
  199. char answer[MAXSTRING];
  200. char cx;
  201. first = TRUE;
  202. printf("Player %d (%c), your move\n", player,
  203. player == X_PLAYER ? 'X' : 'O');
  204. while (TRUE) {
  205. printf("Which row [0-%d]: ", DIM-1);
  206. if (read_string(answer, MAXSTRING) < 0)
  207. return(FALSE);
  208. cx = answer[0];
  209. x = cx - '0';
  210. if (x < 0 || x >= DIM) {
  211. printf("Invalid row; must be >= 0 and < %d\n", DIM-1);
  212. continue;
  213. }
  214. printf("Which column [0-%d]: ", DIM-1);
  215. if (read_string(answer, MAXSTRING) < 0)
  216. return(FALSE);
  217. cx = answer[0];
  218. y = cx - '0';
  219. if (y < 0 || y >= DIM) {
  220. printf("Invalid column; must be >= 0 and < %d\n",
  221. DIM-1);
  222. continue;
  223. }
  224. if (board[x][y] != EMPTY) {
  225. printf("That location is occupied; please try again\n");
  226. print_board();
  227. } else
  228. break;
  229. }
  230. board[x][y] = player == X_PLAYER ? X_MARKER : O_MARKER;
  231. return(is_win(x, y));
  232. }
  233. /*
  234. * is_win
  235. * Checks if the move into position x, y created a tic-tac-toe.
  236. * There are four possible ways to win -- horizontal, vertical,
  237. * and the two diagonals; check each one using a separate routine.
  238. *
  239. * Four routines for checking the wins are:
  240. * win_row The horizontal spots are all the same as this current mark.
  241. * win_column The vertical spots are all the same as this current mark.
  242. * win_diag_left The diagonal spots from left to right are all the
  243. * same as the current mark.
  244. * win_diag_right The diagonal spots from right to left are all the
  245. * same as the current mark.
  246. *
  247. * Input (for all routines)
  248. * x x coordinate
  249. * y y coordinate
  250. * marker the value just placed on the board.
  251. * Output
  252. * TRUE if player won
  253. * FALSE otherwise
  254. */
  255. bool
  256. is_win(int x, int y)
  257. {
  258. int marker;
  259. marker = board[x][y];
  260. /*
  261. * Note that C "short circuit evaluation". As soon as any one
  262. * of these functions returns TRUE, we know that the expression
  263. * is true. Therefore, we can return TRUE without executing
  264. * any of the other routines.
  265. */
  266. return(win_row(x, marker) || win_column(y, marker) ||
  267. win_diag_left(x, y, marker) || win_diag_right(x, y, marker));
  268. }
  269. /*
  270. * Four helper functions for determining a win.
  271. */
  272. bool
  273. win_column(int y, int marker)
  274. {
  275. int i;
  276. for (i = 0; i < DIM; i++)
  277. if (board[i][y] != marker)
  278. return(FALSE);
  279. return(TRUE);
  280. }
  281. bool
  282. win_row(int x, int marker)
  283. {
  284. int i;
  285. for (i = 0; i < DIM; i++)
  286. if (board[x][i] != marker)
  287. return(FALSE);
  288. return(TRUE);
  289. }
  290. bool
  291. win_diag_left(int x, int y, int marker)
  292. {
  293. int i;
  294. /* Check that move is on the diagonal */
  295. if (x != y)
  296. return(FALSE);
  297. for (i = 0; i < DIM; i++)
  298. if (board[i][i] != marker)
  299. return(FALSE);
  300. return(TRUE);
  301. }
  302. bool
  303. win_diag_right(int x, int y, int marker)
  304. {
  305. int i;
  306. /* Check that move is on the diagonal */
  307. if (x + y != DIM - 1)
  308. return(FALSE);
  309. for (i = 0; i < DIM; i++)
  310. if (board[i][DIM - 1 - i] != marker)
  311. return(FALSE);
  312. return(TRUE);
  313. }
  314. void
  315. initialize_board(void)
  316. {
  317. int i, j;
  318. for (i = 0; i < DIM; i++)
  319. for (j = 0; j < DIM; j++)
  320. board[i][j] = EMPTY;
  321. }
  322. int
  323. read_string(char *buf, int length)
  324. {
  325. int char_read;
  326. int i;
  327. i = 0;
  328. while ((char_read = getchar()) != EOF && char_read != NEWLINE &&
  329. i < length) {
  330. buf[i] = (char) char_read;
  331. i++;
  332. putchar(char_read);
  333. }
  334. if (char_read == EOF)
  335. return(-1);
  336. /*
  337. * If the input overflows the buffer, just cut it short
  338. * at length - 1 characters.
  339. */
  340. if (i >= length)
  341. i--;
  342. buf[i] = 0;
  343. return(i);
  344. }
  345. bool
  346. Strcmp(const char *a, const char *b)
  347. {
  348. if (a == NULL)
  349. return(b == NULL);
  350. if (b == NULL)
  351. return(FALSE);
  352. while (*a && *b)
  353. if (*a++ != *b++)
  354. return(FALSE);
  355. return(*a == *b);
  356. }