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 -...
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?
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT