123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- #include <stdio.h>
- #include <unistd.h>
- #define NEWLINE 012
- #define EMPTY 0
- #define X_PLAYER 1
- #define O_PLAYER 2
- #define X_MARKER 1
- #define O_MARKER 2
- #define DIM 3
- #define DIMCHAR "2"
- #define MAXSTRING 100
- typedef enum { FALSE, TRUE } bool;
- bool ask_yesno(const char *msg);
- bool do_move(int player);
- void initialize_board(void);
- bool is_win(int x, int y);
- int read_string(char *buf, int length);
- void print_board(void);
- void print_instructions(void);
- bool win_column(int y, int marker);
- bool win_diag_left(int x, int y, int marker);
- bool win_diag_right(int x, int y, int marker);
- bool win_row(int x, int marker);
- bool Strcmp(const char *a, const char *b);
- int board[DIM][DIM];
- int
- main()
- {
- bool win = FALSE;
- int move, max_moves;
- int player;
- print_instructions();
- max_moves = DIM * DIM;
- while (TRUE) {
- initialize_board();
- for (move = 1; move <= max_moves; move++) {
- player = move % 2 == 0 ? 2 : 1;
- win = do_move(player);
- print_board();
- if (win) {
- printf("Player %d, you WON!\n\n", player);
- break;
- }
- }
-
- if (!win)
- printf("Tie Game!\n\n");
- if (!ask_yesno("Do you wish to play again?"))
- break;
- }
- return 0;
- }
- void
- print_instructions(void)
- {
- printf("Welcome to tic-tac-toe!\n");
- printf("Player 1 always plays X and player 2 always play O\n");
- printf("Good luck!\n\n\n");
- }
- void
- print_board(void)
- {
- int i, j;
-
- printf("\n 0 1 2\n");
- for (i = 0; i < DIM; i++) {
-
- printf(" %d ", i);
- for (j = 0; j < DIM; j++) {
- switch (board[i][j]) {
- case EMPTY: printf(" "); break;
- case X_MARKER: printf(" X "); break;
- case O_MARKER: printf(" O "); break;
- default: printf("???"); break;
- }
- }
- printf("\n");
- }
- printf("\n");
- }
- bool
- ask_yesno(const char *msg)
- {
- char answer[MAXSTRING];
- while (TRUE) {
- printf("%s [yes/no] ", msg);
- if (read_string(answer, MAXSTRING) < 0)
- return(FALSE);
- if (Strcmp(answer, "yes"))
- return(TRUE);
- else if (Strcmp(answer, "no"))
- return(FALSE);
- else
- printf("Please answer either yes or no\n");
- }
- }
- bool
- do_move(int player)
- {
- int x, y;
- bool first;
- char answer[MAXSTRING];
- char cx;
- first = TRUE;
- printf("Player %d (%c), your move\n", player,
- player == X_PLAYER ? 'X' : 'O');
-
- while (TRUE) {
- printf("Which row [0-%d]: ", DIM-1);
- if (read_string(answer, MAXSTRING) < 0)
- return(FALSE);
- cx = answer[0];
- x = cx - '0';
- if (x < 0 || x >= DIM) {
- printf("Invalid row; must be >= 0 and < %d\n", DIM-1);
- continue;
- }
- printf("Which column [0-%d]: ", DIM-1);
- if (read_string(answer, MAXSTRING) < 0)
- return(FALSE);
- cx = answer[0];
- y = cx - '0';
- if (y < 0 || y >= DIM) {
- printf("Invalid column; must be >= 0 and < %d\n",
- DIM-1);
- continue;
- }
- if (board[x][y] != EMPTY) {
- printf("That location is occupied; please try again\n");
- print_board();
- } else
- break;
- }
- board[x][y] = player == X_PLAYER ? X_MARKER : O_MARKER;
- return(is_win(x, y));
- }
- bool
- is_win(int x, int y)
- {
- int marker;
- marker = board[x][y];
-
- return(win_row(x, marker) || win_column(y, marker) ||
- win_diag_left(x, y, marker) || win_diag_right(x, y, marker));
- }
- bool
- win_column(int y, int marker)
- {
- int i;
- for (i = 0; i < DIM; i++)
- if (board[i][y] != marker)
- return(FALSE);
- return(TRUE);
- }
- bool
- win_row(int x, int marker)
- {
- int i;
- for (i = 0; i < DIM; i++)
- if (board[x][i] != marker)
- return(FALSE);
- return(TRUE);
- }
- bool
- win_diag_left(int x, int y, int marker)
- {
- int i;
-
- if (x != y)
- return(FALSE);
- for (i = 0; i < DIM; i++)
- if (board[i][i] != marker)
- return(FALSE);
- return(TRUE);
- }
- bool
- win_diag_right(int x, int y, int marker)
- {
- int i;
-
- if (x + y != DIM - 1)
- return(FALSE);
- for (i = 0; i < DIM; i++)
- if (board[i][DIM - 1 - i] != marker)
- return(FALSE);
- return(TRUE);
- }
- void
- initialize_board(void)
- {
- int i, j;
- for (i = 0; i < DIM; i++)
- for (j = 0; j < DIM; j++)
- board[i][j] = EMPTY;
- }
- int
- read_string(char *buf, int length)
- {
- int char_read;
- int i;
- i = 0;
- while ((char_read = getchar()) != EOF && char_read != NEWLINE &&
- i < length) {
- buf[i] = (char) char_read;
- i++;
- putchar(char_read);
- }
- if (char_read == EOF)
- return(-1);
-
- if (i >= length)
- i--;
- buf[i] = 0;
- return(i);
- }
- bool
- Strcmp(const char *a, const char *b)
- {
- if (a == NULL)
- return(b == NULL);
- if (b == NULL)
- return(FALSE);
- while (*a && *b)
- if (*a++ != *b++)
- return(FALSE);
- return(*a == *b);
- }
|