TicTacToe: Two players take turns marking an available cell in a 3x3 grid with t
ID: 3544082 • Letter: T
Question
TicTacToe: Two players take turns marking an available cell in a 3x3 grid with their tokens ('X' or 'O'). When one player places three tokens in a horizontal, vertical or diagonal row, the player wins and the game is over. A draw occurs when all cells have been filled with tokens and neither player has achieved a win. Create a program that prompts a user to play against the computer. Use a 2D array to simulate the 3x3 grid. Prompt user to enter 'X' by specifying the row and column coordinates of an empty cell, and create strategy for the computer to enter 'O' (may be random choice). Display grid after a pair of moves by user and computer. Then determine status of game (win, draw, continue) and proceed accordingly. Your program should generate games until either the user or computer has two wins more than the other.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 0; /* Loop counter */
int player = 0; /* Player number - 1 or 2 */
int go = 0; /* Square selection number for turn */
int row = 0; /* Row index for a square */
int column = 0; /* Column index for a square */
int line = 0; /* Row or column index in checking loop */
int winner = 0; /* The winning player */
char board[3][3] = { /* The board */
{'1','2','3'}, /* Initial values are reference numbers */
{'4','5','6'}, /* used to select a vacant square for */
{'7','8','9'} /* a turn. */
};
clrscr();
/* The main game loop. The game continues for up to 9 turns */
/* As long as there is no winner */
for( i = 0; i<9 && winner==0; i++)
{
/* Display the board */
printf(" ");
printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);
printf("---+---+--- ");
printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);
printf("---+---+--- ");
printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);
player = i%2 + 1; /* Select player */
/* Get valid player square selection */
do
{
printf(" Player %d, please enter the number of the square "
"where you want to place your %c: ", player,(player==1)?'X':'O');
scanf("%d", &go);
row = --go/3; /* Get row index of square */
column = go%3; /* Get column index of square */
}while(go<0 || go>9 || board[row][column]>'9');
board[row][column] = (player == 1) ? 'X' : 'O'; /* Insert player symbol */
/* Check for a winning line - diagonals first */
if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
(board[0][2] == board[1][1] && board[0][2] == board[2][0]))
winner = player;
else
/* Check rows and columns for a winning line */
for(line = 0; line <= 2; line ++)
if((board[line][0] == board[line][1] && board[line][0] == board[line][2])||
(board[0][line] == board[1][line] && board[0][line] == board[2][line]))
winner = player;
}
/* Game is over so display the final board */
printf(" ");
printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);
printf("---+---+--- ");
printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);
printf("---+---+--- ");
printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);
/* Display result message */
if(winner == 0)
printf(" How boring, it is a draw ");
else
printf(" Congratulations, player %d, YOU ARE THE WINNER! ", winner);
getch():
}