|
This is my homework
It is a tic-tac-toe game, this game is played by two players user1 and 2 and I have to use the 2-D arrys, I tried to solve it but there is some mistakes, I think there is a mistake in the loop or some thig else.
you can check the attachment NOTE: the explination of the functions is writen as comments in the program
#include <stdio.h> void init_board(void); void draw_board(void);
void play_game(void); int play_again(void);
void user1_move(void); void user2_move(void); int find_win(char); int middle_open(void); int symbol_won(char); int square_valid(int);
char board[3][3]; char user1, user2;
/* Initialize the board, ask who goes first, play a game, ask if user wants to play again. */ int main(void) { while(1) { init_board(); user1 = 'X'; user2 = 'O'; play_game(); if (!play_again()) break; }
return (0); }
/* Make sure board starts off empty. */ void init_board(void) { int row, col;
for (row = 0; row < 3; row++) for (col = 0; col < 3; col++) board[row][col] = ' ';
}
/* Display the board to standard output. */ void draw_board(void) { int row, col;
printf("\n"); for (row = 0; row < 3; row++) { printf(" * * \n"); printf(" %c * %c * %c \n", board[row][0], board[row][1], board[row][2]); printf(" * * \n"); if (row != 2) printf("***********\n"); } printf("\n");
return; }
void play_game(void) { int turn;
for (turn = 1; turn <= 9; turn++) { /* Check if turn is even or odd to determine which player should move. */ if (turn % 2 == 1) { if (user1 == 'X') user1_move(); else user2_move(); } else { if (user2 == 'O') user2_move(); else user1_move(); }
draw_board();
if (symbol_won(user2)) { printf("\n PLAYER 2 WIN!!!\n\n"); return; } else if (symbol_won(user1)) { printf("\n PLAYER 1 WIN!\n\n"); return; } }
printf("\nThe game is a draw.\n\n"); return; }
/* Ask if user wants to play again. Returns 1 if yes, 0 if no. */ int play_again(void) { char response;
printf("Do you want to play again? (y/n) "); do { response = getchar(); } while ((response != 'y') && (response != 'Y') && (response != 'n') && (response != 'N'));
if ((response == 'y') || (response == 'Y')) return 1; else return 0; } void user1_move(void) { int square; int row, col;
/* Use first strategy rule that returns valid square */ square = find_win(user1); if (!square) square = find_win(user2); if (!square) square = middle_open();
row = (square - 1) / 3; col = (square - 1) % 3;
board[row][col] = user1;
return; }
/* Choose a move for the computer. */ void user2_move(void) { int square; int row, col;
/* Use first strategy rule that returns valid square */ square = find_win(user2); if (!square) square = find_win(user1); if (!square) square = middle_open();
row = (square - 1) / 3; col = (square - 1) % 3;
board[row][col] = user2;
return; }
/* * Find a win, if any, for the given symbol. * If a winning square exists, return the square; * Otherwise, return 0; */ int find_win(char symbol) { int square, row, col; int result = 0;
/* * Loop through the 9 squares. * For each, if it is empty, fill it in with the given symbol and check * if this has resulted in a win. If so, keep track of this square in result. * Either way, reset the square to empty afterwards. After the loop, if one or * more wins have been found, the last will be recorded in result. * Otherwise, result will still be 0. */ for (square = 1; square <= 9; square++) { row = (square - 1) / 3; col = (square - 1) % 3;
if (board[row][col] == ' ') { board[row][col] = symbol; if (symbol_won(symbol)) result = square; board[row][col] = ' '; } }
return result; }
/* If middle square is empty, return 5; Otherwise return 0. */ int middle_open(void) { if (board[1][1] == ' ') return 5; else return 0; }
/* Check if the given symbol has already one the game. */ int symbol_won(char symbol) { int row, col;
for (row = 0; row < 3; row++) { if ((board[row][0] == symbol) && (board[row][1] == symbol) && (board[row][2] == symbol)) return 1; }
for (col = 0; col < 3; col++) { if ((board[0][col] == symbol) && (board[1][col] == symbol) && (board[2][col] == symbol)) return 1; }
if ((board[0][0] == symbol) && (board[1][1] == symbol) && (board[2][2] == symbol)) return 1;
if ((board[0][2] == symbol) && (board[1][1] == symbol) && (board[2][0] == symbol)) return 1;
return 0; }
/* Check if the given square is valid and empty. */ int square_valid(int square) { int row, col;
row = (square - 1) / 3; col = (square - 1) % 3;
if ((square >= 1) && (square <= 9)) { if (board[row][col] == ' ') return 1; }
return 0;
}
This post has been edited by bader: 21 May, 2008 - 06:15 AM
|