(C++) Tic-Tac-Toe (Extend the feature of your Tic-Tac-Toe program to have the co
ID: 3842053 • Letter: #
Question
(C++)
Tic-Tac-Toe
(Extend the feature of your Tic-Tac-Toe program to have the computer play against the user.
The computer will always start as the first move. The computer should never lose the game, no matter what the user enters. So the computer can win or draw the game.)
Write a program that allows two players (player X and player O) to play a game of tic-tac-toe. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the arrayshould be initialized with an asterisk (*). The players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that:
Displays the contents of the board array (see prompts and output , below).
Prompts and allows the player whose turn it is to select a location on the board for an X in the case of player X or an O in the case of player O. The program should ask the player to enter the row and column number. Valid row or column numbers are 1, 2, or 3.
The loop terminates when a player has won, or a tie has occurred. If a player has won, the programshould declare that player the winner and end. If a tie has occurred, the program should say so and end.
Player X (O) wins when there are three Xs (three Os) in a row on the game board. The Xs (Os) can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.
Input Validation: The program should check the validity of the row and column numbers entered by the players. To be valid, the row and column number must refer to an unoccupied position in the game board. When an invalid entry is made, the program simply redisplays its most recent prompt and attempts to reread the value .
Prompts And Output Labels: The board is displayed as a sequence of three rows, each with threecharacters . The characters are "X" or "O" or "." depending on whether player X or player O has chosen that position or whether the position is still unselected. Thus the following:
. . X
O . X
. . .
represents the board when it's O's turn and when X has moved twice (to row1/column3 and row2/column3) and O has moved once (to row2/column1).
Players are prompted as "Player X, row and column:" or "Player O, row and column:". The game's end is announced either by "Player X wins", or "Player O wins" or "The cat wins" (in the case of no winner).
Tic-Tac-Toe
Write a program that allows two players (player X and player O) to play a game of tic-tac-toe. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the arrayshould be initialized with an asterisk (*). The players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that:
Displays the contents of the board array (see prompts and output , below).
Prompts and allows the player whose turn it is to select a location on the board for an X in the case of player X or an O in the case of player O. The program should ask the player to enter the row and column number. Valid row or column numbers are 1, 2, or 3.
The loop terminates when a player has won, or a tie has occurred. If a player has won, the programshould declare that player the winner and end. If a tie has occurred, the program should say so and end.
Player X (O) wins when there are three Xs (three Os) in a row on the game board. The Xs (Os) can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.
Input Validation: The program should check the validity of the row and column numbers entered by the players. To be valid, the row and column number must refer to an unoccupied position in the game board. When an invalid entry is made, the program simply redisplays its most recent prompt and attempts to reread the value .
Prompts And Output Labels: The board is displayed as a sequence of three rows, each with threecharacters . The characters are "X" or "O" or "." depending on whether player X or player O has chosen that position or whether the position is still unselected. Thus the following:
. . X
O . X
. . .
represents the board when it's O's turn and when X has moved twice (to row1/column3 and row2/column3) and O has moved once (to row2/column1).
Players are prompted as "Player X, row and column:" or "Player O, row and column:". The game's end is announced either by "Player X wins", or "Player O wins" or "The cat wins" (in the case of no winner).
Tic-Tac-Toe
Write a program that allows two players (player X and player O) to play a game of tic-tac-toe. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the arrayshould be initialized with an asterisk (*). The players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that:
Displays the contents of the board array (see prompts and output , below).
Prompts and allows the player whose turn it is to select a location on the board for an X in the case of player X or an O in the case of player O. The program should ask the player to enter the row and column number. Valid row or column numbers are 1, 2, or 3.
The loop terminates when a player has won, or a tie has occurred. If a player has won, the programshould declare that player the winner and end. If a tie has occurred, the program should say so and end.
Player X (O) wins when there are three Xs (three Os) in a row on the game board. The Xs (Os) can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.
Input Validation: The program should check the validity of the row and column numbers entered by the players. To be valid, the row and column number must refer to an unoccupied position in the game board. When an invalid entry is made, the program simply redisplays its most recent prompt and attempts to reread the value .
Prompts And Output Labels: The board is displayed as a sequence of three rows, each with threecharacters . The characters are "X" or "O" or "." depending on whether player X or player O has chosen that position or whether the position is still unselected. Thus the following:
. . X
O . X
. . .
represents the board when it's O's turn and when X has moved twice (to row1/column3 and row2/column3) and O has moved once (to row2/column1).
Players are prompted as "Player X, row and column:" or "Player O, row and column:". The game's end is announced either by "Player X wins", or "Player O wins" or "The cat wins" (in the case of no winner).
Extend the feature of your Tic-Tac-Toe program to have the computer play against the user.
The computer will always start as the first move. The computer should never lose the game, no matter what the user enters. So the computer can win or draw the game.
Explanation / Answer
/* Below is the c++ program for tic toe*/
#include<bits/stdc++.h>
using namespace std;
#define COMPUTER 1 // defining computer move
#define HUMAN 2 // defining human move
#define SIDE 3 // Length of the board
// Computer will move with 'O'
// and human with 'X'
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'
// A function to show the current board status
void showBoard(char board[][SIDE])
{
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]);
return;
}
// A function to show the instructions
void showInstructions()
{
printf(" Tic-Tac-Toe ");
printf("Choose a block numbered from 1 to 9 as below"
" and play ");
printf(" 1 | 2 | 3 ");
printf(" -------------- ");
printf(" 4 | 5 | 6 ");
printf(" -------------- ");
printf(" 7 | 8 | 9 ");
printf("- ");
return;
}
// A function to initialise the game
void initialise(char board[][SIDE], int moves[])
{
// Initiate the random number generator so that
// the same configuration doesn't arises
srand(time(NULL));
// Initially the board is empty
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
board[i][j] = ' ';
}
// Fill the moves with numbers
for (int i=0; i<SIDE*SIDE; i++)
moves[i] = i;
// randomise the moves
random_shuffle(moves, moves + SIDE*SIDE);
return;
}
// A function to declare the winner of the Tic Tac Toe game
void declareWinner(int whoseTurn)
{
if (whoseTurn == COMPUTER)
printf("COMPUTER has won ");
else
printf("HUMAN has won ");
return;
}
// A function that returns true if any of the row
// is crossed with the same player's move
bool rowCrossed(char board[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != ' ')
return (true);
}
return(false);
}
// A function that returns true if any of the column
// is crossed with the same player's move
bool columnCrossed(char board[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] != ' ')
return (true);
}
return(false);
}
// A function that returns true if any of the diagonal
// is crossed with the same player's move
bool diagonalCrossed(char board[][SIDE])
{
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != ' ')
return(true);
if (board[0][2] == board[1][1] &&
board[1][1] == board[2][0] &&
board[0][2] != ' ')
return(true);
return(false);
}
// A function that returns true if the game is over
// else it returns a false
bool gameOver(char board[][SIDE])
{
return(rowCrossed(board) || columnCrossed(board)
|| diagonalCrossed(board) );
}
// A function to play Tic-Tac-Toe
void playTicTacToe(int whoseTurn)
{
// A 3*3 Tic-Tac-Toe board for playing
char board[SIDE][SIDE];
int moves[SIDE*SIDE];
// Initialise the game
initialise(board, moves);
// Show the instructions before playing
showInstructions();
int moveIndex = 0, x, y;
// Keep playing till the game is over or it is a draw
while (gameOver(board) == false &&
moveIndex != SIDE*SIDE)
{
if (whoseTurn == COMPUTER)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = COMPUTERMOVE;
printf("COMPUTER has put a %c in cell %d ",
COMPUTERMOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = HUMAN;
}
else if (whoseTurn == HUMAN)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = HUMANMOVE;
printf ("HUMAN has put a %c in cell %d ",
HUMANMOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = COMPUTER;
}
}
// If the game has drawn
if (gameOver(board) == false &&
moveIndex == SIDE * SIDE)
printf("It's a draw ");
else
{
// Toggling the user to declare the actual
// winner
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
// Declare the winner
declareWinner(whoseTurn);
}
return;
}
// Driver program for Tic-Tac-Toe
int main()
{
// Let us play the game with COMPUTER starting first
playTicTacToe(COMPUTER);
return (0);
}
sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.