Create a tic-tac-toe game.
1.First display a welcome message to the game. A player enters her or his name, selects to play first or not, and then start to plays the game.
2.Display a 3 by 3 tic-tac-toe board.
3. The player selects the cell to mark. The computer randomly selects the cell to mark.
4. The game is continuing until there is a winner or the game is tie.
5.The player can choose to continue a new game.
Hint: Refer to the following code.
welcome (char name[]) {
printf("===========================================\n");
printf("Welcome to the tic-tac-toe game! \n");
printf("Your name: ");
scanf("%s", name);
printf("%s, welcome to the game!\n", name);
printf("===========================================\n");
}
draw_box() {
char box[3][3] = {{'+', '-', '+'}, {'|', ' ', '|'}, {'+', '-', '+'}};
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) printf("%c", box[i][j]);
printf("\n");
}
}
main() {
char name[40];
welcome(name);
draw_box();
}
A possible run may look like as follows:
===========================================
Welcome to the tic-tac-toe game!
Your name: Rita
Rita, welcome to the game!
===========================================
a b c
1 | |
---+---+---
2 | |
---+---+---
3 | |
Rita, do you want to play first (Y/N)? Y
Rita's step => 2b /* input by the user */
a b c
1 | |
---+---+---
2 | o |
---+---+---
3 | |
Computer's step => 1a /* generated by the computer */
a b c
1 x | |
---+---+---
2 | o |
---+---+---
3 | |
Rita's step => 3c
a b c
1 x | |
---+---+---
2 | o |
---+---+---
3 | | o
Computer's step => 3a
a b c
1 x | |
---+---+---
2 | o |
---+---+---
3 x | | o
Rita's step => 2a
a b c
1 x | |
---+---+---
2 o | o |
---+---+---
3 x | | o
Computer's step => 1c
a b c
1 x | | x
---+---+---
2 o | o |
---+---+---
3 x | | o
Rita's step => 2c
a b c
1 x | | x
---+---+---
2 o | o | o
---+---+---
3 x | | o
===========================================
Congratulations! Rita is the winner.
Play more (Y/N)? Y
===========================================