In: Computer Science
Tony Gaddis C++
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 array should 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 program should 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.
Code
#include<iostream>
using namespace std;
const int ROWS=3;
const int COLS=3;
void displayBoard(char board[][COLS]);
//void playerTurn(char board[][COLS], char player);
bool checkForWinner(char board[][COLS], char player);
bool isBoardFull(char board[][COLS]);
void fillBoard(char [][3]);
bool gameOver(char board[][3]);
void getChoice(char board[][3],bool);
void showBoard(char board[][3]);
int main()
{
char board[ROWS][COLS];
bool playerToggle=false;
fillBoard(board);
showBoard(board);
while(!gameOver(board))
{
getChoice(board,playerToggle);
showBoard(board);
playerToggle=!playerToggle;
}
return 1;
}
void fillBoard(char board[][3])
{
for(int i=0;i<ROWS;i++)
for(int j=0;j<COLS;j++)
board[i][j]='*';
}
void showBoard(char board[][3])
{
cout<<" 1 2 3"<<endl;
for(int i=0;i<ROWS;i++)
{
cout<<(i+1)<<" ";
for(int j=0;j<COLS;j++)
{
cout<<board[i][j]<<" ";
}
cout<<endl;
}
}
bool gameOver(char board[][3])
{
bool win=false;
for(int i=0;i<3;i++)
{
if(board[i][0]=='X'
&&board[i][1]=='X' && board[i][2]=='X')
{
cout<<"\nPlayer X Wins!"<<endl;
win=true;
return
win;
}
if(board[0][i]=='X'
&&board[1][i]=='X' && board[2][i]=='X')
{
cout<<"\nPlayer X Wins!"<<endl;
win=true;
return
win;
}
}
if(board[0][0]=='X' && board[1][1]=='X'
&& board[2][2]=='X')
{
cout<<"\nPlayer X
Wins!"<<endl;
win=true;
return win;
}
if(board[0][2]=='X' && board[1][1]=='X'
&& board[2][0]=='X')
{
cout<<"\nPlayer X
Wins!"<<endl;
win=true;
return win;
}
for(int i=0;i<3;i++)
{
if(board[i][0]=='O'
&&board[i][1]=='O' && board[i][2]=='O')
{
cout<<"Player O Wins!"<<endl;
win=true;
return
win;
}
if(board[0][i]=='O'
&&board[1][i]=='O' && board[2][i]=='O')
{
cout<<"Player O Wins!"<<endl;
win=true;
return
win;
}
}
if(board[0][0]=='O' && board[1][1]=='O'
&& board[2][2]=='O')
{
cout<<"Player O
Wins!"<<endl;
win=true;
return win;
}
if(board[0][2]=='O' && board[1][1]=='O'
&& board[2][0]=='O')
{
cout<<"Player O
Wins!"<<endl;
win=true;
return win;
}
for(int i=0;i<ROWS;i++)
{
for(int j=0;j<COLS;j++)
{
if(board[i][j]=='*')
return win;
}
}
cout<<"Tie"<<endl;
return true;
}
void getChoice(char board[][3],bool playerToggle)
{
char symbol;
int player;
int row,col;
if(playerToggle)
{
symbol='O';
player=2;
}
else
{
symbol='X';
player=1;
}
while(true)
{
while(true)
{
cout<<"Player "<<symbol<<", Enter Row: ";
cin>>row;
if(row>=1
&& row<=3)
break;
}
while(true)
{
cout<<"Player "<<symbol<<", Enter Column:
";
cin>>col;
if(col>=1
&& col<=3)
break;
}
if(board[row-1][col-1]=='*')
{
board[row-1][col-1]=symbol;
break;
}
else
cout<<"That postion is already taken! Try
Again."<<endl;
}
}
output
If you have any query regarding the code
please ask me in the comment i am here for help you. Please do not
direct thumbs down just ask if you have any query. And if you like
my work then please appreciates with up vote. Thank You.