Question
3.1.6 Tic-tac-toe Tic-tac-toe is a game for two players, X and O, who take turns marking the spaces in a 3x3 grid. The player who
3.1.6 Tic-tac-toe
Tic-tac-toe is a game for two players, X and O, who take turns marking the spaces in a 3x3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical or diagonal row wins the game. If the grid gets full without any player winning, it is a tie.
Usually this game is a paper-and-pencil but it can also be implemented as a computer game.
Write program tic_tac_toe.c that enables two players to play tic-tac-toe interactively, as illustrated in the following example.
$ ./tic_tac_toe ------- | | | | ------- | | | | ------- | | | | ------- Enter cell for Player 1 0 ------- |X| | | ------- | | | | ------- | | | | ------- Enter cell for Player 2 1 ------- |X|O| | ------- | | | | ------- | | | | ------- Enter cell for Player 1 3 ------- |X|O| | ------- |X| | | ------- | | | | ------- Enter cell for Player 2 4 ------- |X|O| | ------- |X|O| | ------- | | | | ------- Enter cell for Player 1 9 Invalid cell Enter cell for Player 1 6 ------- |X|O| | ------- |X|O| | ------- |X| | | ------- Player 1 wins! $
As you can notice, cells are not describe by their x-y coordinates but by their sequential order:
0 | 1 | 2 |
3 | 4 | 5 |
6 | 7 | 8 |
The playing grid can easily be represented by a 2-D array of characters. A space represents an empty cell, X is the first players mark while O is the second players mark.
You must implement the following functions:
void init_grid(void): empty the grid (ie, fills it out with spaces).
void display_grid(void): display the grid as shown in the example.
int play(char player, int pos): tries playing the player represented by the specified character at the given position. If it fails (cell is already occupied), return 0; if it succeeds, place the mark and return 1.
int is_win(char player): checks if the player represented by the specified character has won. This function depends on three sub-functions:
int is_horizontal_winner(char player): checks if the player represented by the specified character has filled out a horizontal line.
int is_vertical_winner(char player): checks if the player represented by the specified character has filled out a vertical line.
int is_diag_winner(char player): checks if the player represented by the specified character has filled out one of the two diagonal lines. Note that this function can be left for the end.
int is_tie(void): return 1 if the grid is full, 0 otherwise.
In the main function, after initializing the grid and displaying it for the first time, you will have the main game loop.
The first task is to ask the current player in which cell they want to play and try to execute the specified move; if such move cant be played, the player should be asked again. We assume that the player only enters a number.
After the move was successfully played, the grid can be displayed.
Then we can determine if the current player has won, in which case the game stops;
Otherwise, the current player changes to the other player and the loop starts again, but only if the game is not a tie.
Include comments if possible showing how it works
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started