please turn this c++ to psuedocode. #include <iostream> #include <cstring> using
ID: 3720056 • Letter: P
Question
please turn this c++ to psuedocode.
#include <iostream>
#include <cstring>
using namespace std;
int board[3][3]; // our tic tac toe board
string player1; // player1's name
string player2; // player2's name
// this function initiates the game
void init()
{
memset(board, 0, sizeof board); // sets all elements of board to zero
cout<<"Enter player1's name"<<endl;
cin>> player1;
cout<< "Enter player2's name"<<endl;
cin>>player2;
}
// check_error return 0 if coordinates are out of bound
// return 1 if player attempts to play in already played cell
// return 2 if move is valid
int check_error(int row,int col)
{
if(row>3||row<1||col>3||col<1)
return 0;
else if(board[row-1][col-1]!=0)
return 1;
else
return 2;
}
// displays board
void display_board(void)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<board[i][j]<<" ";
cout<<endl;
}
}
// updates the board
void update_board(int row,int col,int turn)
{
board[row-1][col-1]=turn;
display_board();
}
void input(string player,int turn)
{
// keep taking input will exit the loop once player enter's the correct coordinates
while(true)
{
cout<<player<<", Enter row and column to make your move"<<endl;
int row,col;
cin>>row>>col;
int flag= check_error(row,col);
if(flag==0)
cout<<"Invalid coordinates, enter again"<<endl;
else if(flag==1)
cout<<"Cannot make move in this position, it is already used, enter again"<<endl;
else if(flag==2)
{
update_board(row,col,turn);
return;
}
}
}
int check_state(int turn)
{
for(int i=0;i<3;i++)
{
//check for rows
if(turn==board[i][0] && board[i][0]==board[i][1] && board[i][1]==board[i][2])
return 1;
//check for columns
if(turn==board[0][i] && board[0][i]==board[1][i] && board[1][i]==board[2][i])
return 1;
}
//check for diagnols
if(turn==board[0][0]&&board[0][0]==board[1][1] && board[1][1]==board[2][2])
return 1;
if(turn==board[0][2] && board[0][2]==board[1][1] && board[1][1]==board[2][0])
return 1;
// check if game is a draw by checking if there is a 0 or not
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
return 2;
}
}
return 0;
}
int main(void)
{
int continue_playing=1;
while(continue_playing)
{
init();
int turn=1; // tells us about which player has to move
bool winner=false;
//status =0 represents game is drawn
//status =1 represents game is won by somebody
// status=2 game is not won and play can still go on
int status=2;
int name_winner; // stores who is the winner(0 or 1)
while(status==2)
{
if(turn==1)
{
input(player1,turn);
status=check_state(turn);
if(status==1)
name_winner=turn;
turn=2;
}
else if(turn==2)
{
input(player2, turn);
status=check_state(turn);
if(status==1)
name_winner=turn;
turn=1;
}
}
if(status==0)
cout<<"Game Drawn!!"<<endl;
else
{
winner=true;
if(name_winner==1)
cout<<"Winner is 1 i.e. "<<player1<<endl;
else
cout<<"Winner is 2 i.e. "<< player2<<endl;
}
cout<<"Another Game?, enter any number for yes,0 for no";
cin >> continue_playing;
}
return 0;
}
Explanation / Answer
TicTacToe pseducode:-
step1:- declare boaed[3][3] and equate its elements to 0;
step2:- ask player 1 to enter row and column number
step3:- check the validity of entered row and column values (check_error())
step4:- check whether the entered location is empty or not (input(string player,int turn))
a) if not ask for another location
b) if yes then mark 'X' on that location
step5:- check win condition (check_state(int turn))
a)if entered move make player 1 win show and end the game
b)if not follow next step
step6:- display board
step7:- ask player 2 to enter row and column number
step8:- check the validity of entered row and column values (check_error())
step9:- check whether the entered location is empty or not (input(string player,int turn))
a) if not ask for another location
b) if yes then mark 'O' on that location
step10:- check win condition (check_state(int turn))
a)if entered move make player 2 win show and end the game
b)if not display board step 2 to 10 till either one win the game or game tie while(continue_playing)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.