00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <stdio.h>
00042 #include <unistd.h>
00043
00044 #define NEWLINE 012
00045 #define EMPTY 0
00046 #define X_PLAYER 1
00047 #define O_PLAYER 2
00048 #define X_MARKER 1
00049 #define O_MARKER 2
00050 #define DIM 3
00051 #define DIMCHAR "2"
00052 #define MAXSTRING 100
00053
00054 typedef enum { FALSE, TRUE } bool;
00055
00056
00057 bool ask_yesno(const char *msg);
00058 bool do_move(int player);
00059 void initialize_board(void);
00060 bool is_win(int x, int y);
00061 int read_string(char *buf, int length);
00062 void print_board(void);
00063 void print_instructions(void);
00064 bool win_column(int y, int marker);
00065 bool win_diag_left(int x, int y, int marker);
00066 bool win_diag_right(int x, int y, int marker);
00067 bool win_row(int x, int marker);
00068 bool Strcmp(const char *a, const char *b);
00069
00070
00071
00072
00073
00074 int board[DIM][DIM];
00075
00076
00077
00078 int
00079 main()
00080 {
00081 bool win = FALSE;
00082 int move, max_moves;
00083 int player;
00084
00085 print_instructions();
00086 max_moves = DIM * DIM;
00087
00088 while (TRUE) {
00089 initialize_board();
00090 for (move = 1; move <= max_moves; move++) {
00091 player = move % 2 == 0 ? 2 : 1;
00092 win = do_move(player);
00093 print_board();
00094 if (win) {
00095 printf("Player %d, you WON!\n\n", player);
00096 break;
00097 }
00098 }
00099
00100
00101
00102
00103 if (!win)
00104 printf("Tie Game!\n\n");
00105 if (!ask_yesno("Do you wish to play again?"))
00106 break;
00107 }
00108 return 0;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 void
00122 print_instructions(void)
00123 {
00124 printf("Welcome to tic-tac-toe!\n");
00125 printf("Player 1 always plays X and player 2 always play O\n");
00126 printf("Good luck!\n\n\n");
00127 }
00128
00129 void
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 print_board(void)
00141 {
00142 int i, j;
00143
00144
00145 printf("\n 0 1 2\n");
00146
00147 for (i = 0; i < DIM; i++) {
00148
00149 printf(" %d ", i);
00150 for (j = 0; j < DIM; j++) {
00151 switch (board[i][j]) {
00152 case EMPTY: printf(" "); break;
00153 case X_MARKER: printf(" X "); break;
00154 case O_MARKER: printf(" O "); break;
00155 default: printf("???"); break;
00156 }
00157 }
00158 printf("\n");
00159 }
00160 printf("\n");
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 bool
00179 ask_yesno(const char *msg)
00180 {
00181 char answer[MAXSTRING];
00182
00183 while (TRUE) {
00184 printf("%s [yes/no] ", msg);
00185 if (read_string(answer, MAXSTRING) < 0)
00186 return(FALSE);
00187 if (Strcmp(answer, "yes"))
00188 return(TRUE);
00189 else if (Strcmp(answer, "no"))
00190 return(FALSE);
00191 else
00192 printf("Please answer either yes or no\n");
00193 }
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 bool
00213 do_move(int player)
00214 {
00215 int x, y;
00216 bool first;
00217 char answer[MAXSTRING];
00218 char cx;
00219
00220 first = TRUE;
00221 printf("Player %d (%c), your move\n", player,
00222 player == X_PLAYER ? 'X' : 'O');
00223
00224 while (TRUE) {
00225 printf("Which row [0-%d]: ", DIM-1);
00226 if (read_string(answer, MAXSTRING) < 0)
00227 return(FALSE);
00228 cx = answer[0];
00229 x = cx - '0';
00230 if (x < 0 || x >= DIM) {
00231 printf("Invalid row; must be >= 0 and < %d\n", DIM-1);
00232 continue;
00233 }
00234 printf("Which column [0-%d]: ", DIM-1);
00235 if (read_string(answer, MAXSTRING) < 0)
00236 return(FALSE);
00237 cx = answer[0];
00238 y = cx - '0';
00239 if (y < 0 || y >= DIM) {
00240 printf("Invalid column; must be >= 0 and < %d\n",
00241 DIM-1);
00242 continue;
00243 }
00244
00245 if (board[x][y] != EMPTY) {
00246 printf("That location is occupied; please try again\n");
00247 print_board();
00248 } else
00249 break;
00250 }
00251 board[x][y] = player == X_PLAYER ? X_MARKER : O_MARKER;
00252
00253 return(is_win(x, y));
00254
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 bool
00280 is_win(int x, int y)
00281 {
00282 int marker;
00283
00284 marker = board[x][y];
00285
00286
00287
00288
00289
00290
00291
00292 return(win_row(x, marker) || win_column(y, marker) ||
00293 win_diag_left(x, y, marker) || win_diag_right(x, y, marker));
00294 }
00295
00296
00297
00298
00299 bool
00300 win_column(int y, int marker)
00301 {
00302 int i;
00303 for (i = 0; i < DIM; i++)
00304 if (board[i][y] != marker)
00305 return(FALSE);
00306 return(TRUE);
00307 }
00308
00309 bool
00310 win_row(int x, int marker)
00311 {
00312 int i;
00313 for (i = 0; i < DIM; i++)
00314 if (board[x][i] != marker)
00315 return(FALSE);
00316 return(TRUE);
00317 }
00318
00319 bool
00320 win_diag_left(int x, int y, int marker)
00321 {
00322 int i;
00323
00324
00325 if (x != y)
00326 return(FALSE);
00327
00328 for (i = 0; i < DIM; i++)
00329 if (board[i][i] != marker)
00330 return(FALSE);
00331 return(TRUE);
00332 }
00333
00334 bool
00335 win_diag_right(int x, int y, int marker)
00336 {
00337 int i;
00338
00339
00340 if (x + y != DIM - 1)
00341 return(FALSE);
00342 for (i = 0; i < DIM; i++)
00343 if (board[i][DIM - 1 - i] != marker)
00344 return(FALSE);
00345 return(TRUE);
00346 }
00347
00348 void
00349 initialize_board(void)
00350 {
00351 int i, j;
00352
00353 for (i = 0; i < DIM; i++)
00354 for (j = 0; j < DIM; j++)
00355 board[i][j] = EMPTY;
00356 }
00357
00358 int
00359 read_string(char *buf, int length)
00360 {
00361 int char_read;
00362 int i;
00363
00364 i = 0;
00365 while ((char_read = getchar()) != EOF && char_read != NEWLINE &&
00366 i < length) {
00367 buf[i] = (char) char_read;
00368 i++;
00369 putchar(char_read);
00370 }
00371
00372 if (char_read == EOF)
00373 return(-1);
00374
00375
00376
00377
00378
00379 if (i >= length)
00380 i--;
00381 buf[i] = 0;
00382 return(i);
00383 }
00384
00385 bool
00386 Strcmp(const char *a, const char *b)
00387 {
00388 if (a == NULL)
00389 return(b == NULL);
00390 if (b == NULL)
00391 return(FALSE);
00392
00393 while (*a && *b)
00394 if (*a++ != *b++)
00395 return(FALSE);
00396
00397 return(*a == *b);
00398
00399 }