Reputation: 71
I am trying to solve a practice problem in this book I am reading, to self teach my self C++.
The problem asks to create a tic tac toe game that two humans could play, using enum
to represent the values on the board (the chapter talked about enumerators).
This is the code I have written so far and everything seems to work just fine, except that whenever I enter row 1 column 3, when it is player X's turn, this causes it to end the game and ask if to play again or not even though the game is not over.
I can't seem to figure out why, because its only this coordinate and only during X player's turn.
To use the program you first enter the row number and then the column number.
I am also aware that you can overwrite a place on the board, I am just assuming that you do not at the moment.
If anyone could help me point out this bug that would be great, my code may be confusing to read, sorry for that, but I am still practicing and am quite new to programming.
I am more than happy to explain what a line of code should be doing!.
Here is the code:
#include<iostream>
#include<array>
enum TicTacToeSquare
{
blankSquare=0,
X=1,
O=2
};
int turn=0;
using namespace std;
int winCondition(int a[3][3]);
void printBoard(int a[3][3]);
void resetBoard(int a[3][3]);
int testBoard(int a[3][3]);
int main()
{
int choice;
int board[3][3]=
{
{0,0,0},
{0,0,0},
{0,0,0}
};
int i,j;
while (true)
{
for (turn=0;turn<9;++turn)
{
if (turn%2==0)
{
std::cout<<"Player X: \n"<<"Enter board position: \n";
std::cin>>i>>j;
std::cout<<std::endl;
board[i-1][j-1]=X;
printBoard(board);
if (winCondition(board)==1)
{
int choice;
cout<<"\nDo you want to play again?"<<endl;
cout<<"1. Yes\n"<<"2. No"<<endl;
cin>>choice;
if (choice==1)
{
resetBoard(board);
std::cout<<std::endl;
printBoard(board);
turn=0;
}
else if (choice==2)
{
cout<<"\nThanks for playing!"<<endl;
return 0;
}
else
{
cout<<"\nInvalid option, game will terminate."<<endl;
return 0;
}
}
}
if (turn%2!=0)
{
std::cout<<"Player O: \n"<<"Enter board position: \n";
std::cin>>i>>j;
std::cout<<std::endl;
board[i-1][j-1]=O;
printBoard(board);
if (winCondition(board)==2)
{
int choice;
cout<<"\nDo you want to play again?"<<endl;
cout<<"1. Yes\n"<<"2. No"<<endl;
cin>>choice;
if (choice==1)
{
resetBoard(board);
std::cout<<std::endl;
printBoard(board);
turn=0;
}
else if (choice==2)
{
cout<<"\nThanks for playing!"<<endl;
return 0;
}
else
{
cout<<"\nInvalid option, game will now terminate."<<endl;
return 0;
}
}
}
}
std::cout<<"Game is a draw!"<<std::endl;
std::cout<<"Do you want to play again?"<<std::endl;
std::cin>>choice;
if (choice==1)
{
resetBoard(board);
std::cout<<std::endl;
printBoard(board);
}
else if (choice==2)
{
cout<<"\nThanks for playing!"<<std::endl;
return 0;
}
else
{
cout<<"Invalid option, game will now terminate."<<endl;
return 0;
}
}
}
void printBoard(int board[3][3])
{
for (int i=1;i<4;++i)
{
for (int j=1;j<4;++j)
{
cout<<board[i-1][j-1]<<" ";
if (j%3==0)
{
cout<<"\n";
}
}
}
}
int winCondition(int board[3][3])
{
if (board[0][0]==X&&board[0][1]==X&&board[0][2]==X)
{
cout<<"\nPlayer X wins!"<<std::endl;
return 1;
}
else if (board[0][0]==O&&board[0][1]==O&&board[0][2]==O)
{
cout<<"\nPlayer O wins!"<<std::endl;
return 2;
}
else if (board[0][0]==X&&board[1][0]==X&&board[2][0]==X)
{
cout<<"\nPlayer X wins!"<<std::endl;
return 1;
}
else if (board[0][0]==O&&board[1][0]==O&&board[2][0]==O)
{
cout<<"\nPlayer O wins!"<<std::endl;
return 2;
}
else if (board[0][1]==X&&board[1][1]==X&&board[2][1]==X)
{
cout<<"\nPlayer X wins!"<<std::endl;
return 1;
}
else if (board[0][1]==O&&board[1][1]==O&&board[2][1]==O)
{
cout<<"\Player O wins!"<<std::endl;
return 2;
}
else if (board[0][2]==X&&board[1][2]==X&&board[2][2]==X)
{
cout<<"Player X wins!"<<std::endl;
return 1;
}
else if (board[0][2]==O&&board[1][2]==O&&board[2][2]==O)
{
cout<<"\nPlayer O wins!"<<std::endl;
return 2;
}
else if (board[1][0]==X&&board[1][1]==X&&board[1][2]==X)
{
cout<<"\nPlayer X wins!"<<std::endl;
return 1;
}
else if (board[1][0]==O&&board[1][1]==O&&board[1][2]==O)
{
cout<<"\nPlayer O wins!"<<std::endl;
return 2;
}
else if (board[2][0]==X&&board[2][1]==X&&board[2][2]==X)
{
cout<<"\nPlayer X wins!"<<std::endl;
return 1;
}
else if (board[2][0]==O&&board[2][1]==O&&board[2][2]==O)
{
cout<<"\nPlayer O wins!"<<std::endl;
return 2;
}
else if (board[0][0]==X&&board[1][1]==X&&board[2][2]==X)
{
cout<<"\nPlayer X wins!"<<std::endl;
return 1;
}
else if (board[0][0]==O&&board[1][2]==O&&board[2][2]==O)
{
cout<<"\nPlayer O wins!"<<std::endl;
return 2;
}
else if (board[0][2]==X&&board[1][1]==X&&board[2][0]==X)
{
cout<<"\nPlayer X wins!"<<std::endl;
return 1;
}
else if (board[0][2]==O&&board[1][1]==O&&board[2][0]==O)
{
cout<<"\nPlayer O wins!"<<std::endl;
return 2;
}
}
void resetBoard(int board[3][3])
{
for (int i=0;i<3;++i)
{
for (int j=0;j<3;++j)
{
board[i][j]=blankSquare;
}
}
}
Upvotes: 1
Views: 80
Reputation: 32732
In winCondition
if nobody wins, you don't have an explicit return. This results in undefined behavior, which means any value could be returned. You need to add a return 0;
at the end of the function.
If you compile with all warnings enabled the compiler will tell you about problems like that.
Upvotes: 3