Question

In: Computer Science

Need to fix this code for tc -tac-toe game .. see the code below and fix...

Need to fix this code for tc -tac-toe game ..

see the code below and fix it

#include <iostream>
using namespace std;

void display_board();
void player_turn();
bool gameover ();

char turn ;
bool draw = false;
char board [3][3] = { {'1', '2', '3'}, { '4', '5', '6'}, { '7', '8', '9'}};


int main()
{
cout << " Lets play Tc- Tac- toe game " <<endl ;
cout << " Player 1 [X] ----- player 2 [0] " <<endl <<endl;

while (!gameover())
{
display_board();
player_turn();
gameover();
}
if (turn =='0' && !draw) {

display_board();
cout<<endl <<"Congratulations! player_1 [X], you won the game." ;

}
else if (turn == 'X' && !draw){
display_board();
cout <<endl<< "Congratulation ! player 2 [0], You won the game. ";
}
else{
display_board();
cout<<endl << "game over.It's draw." ;
}
}

void display_board()
{
cout << "-------------" <<endl <<endl;
cout << " " << board[0][0] << "|" << board [0][1] << "|" << board [0][2] << endl;
cout << "--|-|--" << endl;
cout << " " << board[1][0] << "|" << board [1][1] << "|" << board [1][2] << endl;
cout << "--|-|--" << endl;
cout << " " << board[2][0] << "|" << board [2][1] << "|" << board [2][2] << endl;

}

void player_turn (){
int choise;
int row =0, column =0;

if (turn == 'X'){
cout << "player 1 [X] turn: ";
}
else if (turn == 'O'){
cout << "player 2 [0] turn : ";
}
cin >> choise;


switch (choise)
{
case 1 : row = 0; column = 0; break;
case 2 : row = 0; column = 1; break;
case 3 : row = 0; column = 2; break;
case 4 : row = 1; column = 0; break;
case 5 : row = 1; column = 1; break;
case 6 : row = 1; column = 2; break;
case 7 : row = 2; column = 0; break;
case 8 : row = 2; column = 1; break;
case 9 : row = 2; column = 2; break;
default :
cout << "Invalid move ";
player_turn();

}
if (turn == 'X' && board[row][column] != 'X' && board[row][column] !='0' )
{
board[row][column] = 'X';
turn = '0';
}
else if (turn == '0' && board[row][column] != 'X' && board[row][column] !='0' )
{
board[row][column] = '0';
turn = 'X';
}
else{
cout << "The cell is already full" <<endl;
player_turn();
}
}


bool gameover()
{
for (int i=0; i<3; i++)
{
if (board [i][0] == board [i][1] && board [i][1]== board [i][2] || board [0][i] == board [1][i] && board [0][i] == board [2][i] )
{
return true;
}
}

for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
if (board [i][j] !='X' && board [i][j] != 'O')
{
return false;
}
}

}

draw = true;
return true;

}

Solutions

Expert Solution

Two fixes that I did to make it run:

1. Initialize turn to drive the game.

2. Check diagonal entries for checking winning condition.

#include <iostream>
using namespace std;

void display_board();
void player_turn();
bool gameover ();
int start;
char turn;
bool draw = false;
char board [3][3] = { {'1', '2', '3'}, { '4', '5', '6'}, { '7', '8', '9'}};

int main()
{
cout << " Lets play Tc- Tac- toe game " <<endl ;
cout << " Player 1 [X] ----- player 2 [0] " <<endl <<endl;
cout << "Who want to start first Player 1 or 2 " <<endl <<endl;
cin>>start;
if(start==1)
turn='X';
else
turn='0';
while (!gameover())
{
display_board();
player_turn();
gameover();
}

if (turn =='0' && !draw) {

display_board();
cout<<endl <<"Congratulations! player_1 [X], you won the game." ;

}
else if (turn == 'X' && !draw){
display_board();
cout <<endl<< "Congratulation ! player 2 [0], You won the game. ";
}
else{
display_board();
cout<<endl << "game over.It's draw." ;
}

}

void display_board()
{
cout << "-------------" <<endl <<endl;
cout << " " << board[0][0] << "|" << board [0][1] << "|" << board [0][2] << endl;
cout << "--|-|--" << endl;
cout << " " << board[1][0] << "|" << board [1][1] << "|" << board [1][2] << endl;
cout << "--|-|--" << endl;
cout << " " << board[2][0] << "|" << board [2][1] << "|" << board [2][2] << endl;

}

void player_turn (){

int choise;
int row =0, column =0;
if (turn == 'X'){
cout << "player 1 [X] turn: ";
}
else if (turn == '0'){
cout << "player 2 [0] turn : ";
}
cin >> choise;


switch (choise)
{
case 1 : row = 0; column = 0; break;
case 2 : row = 0; column = 1; break;
case 3 : row = 0; column = 2; break;
case 4 : row = 1; column = 0; break;
case 5 : row = 1; column = 1; break;
case 6 : row = 1; column = 2; break;
case 7 : row = 2; column = 0; break;
case 8 : row = 2; column = 1; break;
case 9 : row = 2; column = 2; break;
default :
cout << "Invalid move ";
player_turn();

}
if (turn == 'X' && board[row][column] != 'X' && board[row][column] !='0' )
{
board[row][column] = 'X';
turn = '0';
}
else if (turn == '0' && board[row][column] != 'X' && board[row][column] !='0' )
{
board[row][column] = '0';
turn = 'X';
}
else{
cout << "The cell is already full" <<endl;
player_turn();
}
}


bool gameover()
{
for (int i=0; i<3; i++)
{
if (board [i][0] == board [i][1] && board [i][1]== board [i][2] || board [0][i] == board [1][i] && board [0][i] == board [2][i] || board [0][0] == board [1][1] && 
        board [1][1] == board [2][2]|| board [0][2] == board [1][1] && 
        board [1][1] == board [2][0])
{
return true;
}
}

for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
if (board [i][j] !='X' && board [i][j] != '0')
{
return false;
}
}

}

draw = true;
return true;

}


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please...
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please provide code for checking diagonal win. function checkWinVertical(player, row, col) { var counter = 0; //each button var btn = document.getElementsByTagName("button"); for (counterC = 0; counterC < col; counterC++){ for (countR = 0; countR < row; countR++){ if (player != btn[counter].innerHTML){ counter += 1; break; } else if (countR + 1 == col) { for (countW = 0; countW < col; countW++){ btn[counter -...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
**MATLAB Code only Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it...
**MATLAB Code only Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it simple, but there is no other rules. Thank you.
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm =...
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new('andy', 'mike') => #<Game:0x2e91d78 @players=[Andy, Mike]> >> gm.play_game('mike') Mike, enter your next O O-- --- --- Andy, enter your next X O-X --- --- Mike, enter your next O O-X -O- --- Andy, enter your next X move Bad move dude! You go again. O-X -O- --- Andy, enter your next X move O-X -OX --- Mike, enter your next O move O-X -OX...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT