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

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 :(
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...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
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...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to...
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to Functional Based component in React. //CODE import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; function calculateWinner(squares) { const lines = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; for (let i = 0; i < lines.length; i++) { const [a, b, c] = lines[i];...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT