In: Computer Science
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;
}
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;
}