In: Computer Science
Need C++ code for following with a document describing the code as well:
Write a program to implement the game of Tic Tac Toe Four.
The game is played on a 4 × 4 chessboard, within 2 players. The player who is playing "X" always goes first. Players alternate placing Xs and Os on the board until either one player has four in a row, horizontally, vertically, or diagonally; or all 16 squares are filled(which is a draw).
The program should be able to:
1) keep receiving the input from two players in turn.
E.g. receive both row index and column index from keyboard input
2) print the current chessboard pattern for every turn. Below is an example of chessboard printing, with ‘-’ for empty squares. You can use any other pattern you like to visualize the chessboard.
- - - - - - - - - - - - - - - - game start |
- - - - - - x - - o - - - - - - during game |
- - - - - x x - - o - - - - o - during game |
o o o o - x x - - o - x x - o x “o” wins |
3) determine if any player wins the game, or the game comes to a draw. Print out the final game result.
E.g. “Player1 wins” or “Draw”
Please submit all your source codes together with a document describing your code and ideas
Code
#include<iostream>
using namespace std;
const int ROWS=4;
const int COLS=4;
bool checkForWinner(char board[][COLS], char player);
bool isBoardFull(char board[][COLS]);
void fillBoard(char [][4]);
bool gameOver(char board[][4]);
void getChoice(char board[][4],bool);
void showBoard(char board[][4]);
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[][4])
{
for(int i=0;i<ROWS;i++)
for(int j=0;j<COLS;j++)
board[i][j]='-';
}
void showBoard(char board[][4])
{
cout<<" 1 2 3 4"<<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[][4])
{
bool win=false;
for(int i=0;i<4;i++)
{
if(board[i][0]=='X' &&
board[i][1]=='X' && board[i][2]=='X' &&
board[i][3]=='X')
{
cout<<"\nPlayer X Wins!"<<endl;
win=true;
return
win;
}
if(board[0][i]=='X'
&&board[1][i]=='X' && board[2][i]=='X' &&
board[3][i]=='X')
{
cout<<"\nPlayer X Wins!"<<endl;
win=true;
return
win;
}
}
if(board[0][0]=='X' && board[1][1]=='X'
&& board[2][2]=='X' && board[3][3]=='X')
{
cout<<"\nPlayer X
Wins!"<<endl;
win=true;
return win;
}
if(board[0][3]=='X' && board[1][2]=='X'
&& board[3][0]=='X' && board[2][1]=='X')
{
cout<<"\nPlayer X
Wins!"<<endl;
win=true;
return win;
}
for(int i=0;i<4;i++)
{
if(board[i][0]=='O' &&
board[i][1]=='O' && board[i][2]=='O' &&
board[i][3]=='O')
{
cout<<"\nPlayer X Wins!"<<endl;
win=true;
return
win;
}
if(board[0][i]=='O'
&&board[1][i]=='O' && board[2][i]=='O' &&
board[3][i]=='O')
{
cout<<"\nPlayer O Wins!"<<endl;
win=true;
return
win;
}
}
if(board[0][0]=='O' && board[1][1]=='O'
&& board[2][2]=='O' && board[3][3]=='O')
{
cout<<"\nPlayer O
Wins!"<<endl;
win=true;
return win;
}
if(board[0][3]=='O' && board[1][2]=='O'
&& board[3][0]=='O' && board[2][1]=='O')
{
cout<<"\nPlayer 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[][4],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<=4)
break;
}
while(true)
{
cout<<"Player "<<symbol<<", Enter Column:
";
cin>>col;
if(col>=1
&& col<=4)
break;
}
if(board[row-1][col-1]=='-')
{
board[row-1][col-1]=symbol;
break;
}
else
cout<<"That postion is already taken! Try
Again."<<endl;
}
}
outputs
game 1
game 2
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.