Hi everyone,
I noticed an earlier tic tac toe program, but my particular problem is slightly different. I am using bloodshed to compile this bad boy and I am getting weird results. I have narrowed the problem down to my already suspect if statements that compare the board array elements to see if we have a winner. Eventually I will add a return 0 statement to finish off the program when a winner is declared, but for now there is a problem.
I have taken out the if statements and everything enters in correctly and prints correctly including input validation.
The weird problem is when I use the if statements to check for a winner, it automatically declares player one the winner and places an X in elements (0,0), (0,1), and (0,2). When I changed the top line in the if statement to (1,0), (1,1), and (1, 2) it places Xs in those respective elements. Somehow it is still checking for a winner and declaring a winner.
Basically, can I even write my if statement like this or is something wrong with the way it is laid out? Although I imagine this comes with the territory, but is there a cleaner way that I could put this last piece of the puzzle together or "should" it work?
Many thanks everyone.
Sorry about the formatting on those statements, it is showing up properly on my compiler. Also, the wincheck function was being used earlier as an attempt to do the same thing, but I decided to try and directly input the if/else winner statements and just commented it out for now.
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
//Tic Tac Toe by Steve P.
using namespace std;
bool check (int, int);
bool legal = false;
void active (void);
char board [3][3];
int main()
{
int row,col;
char game = 'y';
for (row=0;row<3;row++)
{
for (col=0;col<3;col++)
board[row][col]='*';
}
cout<<"GREETINGS GAMERS!! TIME TO TIC TAC TOE!!"<<endl<<endl;
cout<<"You will need two players for this game."<<endl<<endl;
active();
cout<<endl;
cout<<"^^^This is your board.^^^"<<endl;
for (int count=0;count<9;count++)
{
cout<<"Player One, enter in the row"<<endl;
cout<<"that you want an X in."<<endl;
cout<<"Enter row here-->";
cin>>row;
cout<<"Player One, enter the column-->";
cin>>col;
legal = false;
while (legal == false)
{
legal= check(row, col);
if (!legal)
{
cout<<"Spot already taken."<<endl;
cout<<"Enter in row again.-->";
cin>>row;
cout<<"Enter in column again."<<endl;
cin>>col;
}
}
board [row][col] = 'X';
active();
count++;
if (count == 9)
{
cout<<"We have a tie! Thanks for playing!"<<endl;
system ("PAUSE");
return 0;
}
if (((board[0][0]='X')&&(board[0][1]='X')&&(board[0][2]='X')) ||
((board[1][0]='X')&&(board[1][1]='X')&&(board[1][2]='X')) ||
((board[2][0]='X')&&(board[2][1]='X')&&(board[2][2]='X')) ||
((board[0][0]='X')&&(board[1][0]='X')&&(board[2][0]='X')) ||
((board[0][1]='X')&&(board[1][1]='X')&&(board[2][1]='X')) ||
((board[0][2]='X')&&(board[1][2]='X')&&(board[2][2]='X')) ||
((board[0][0]='X')&&(board[1][1]='X')&&(board[2][2]='X')) ||
((board[2][0]='X')&&(board[1][1]='X')&&(board[0][2]='X')))
{
cout<<"Congratulations player One! You Win!!"<<endl;
}
else if(((board[0][0]='O')&&(board[0][1]='O')&&(board[0][2]='O')) ||
((board[1][0]='O')&&(board[1][1]='O')&&(board[1][2]='O')) ||
((board[2][0]='O')&&(board[2][1]='O')&&(board[2][2]='O')) ||
((board[0][0]='O')&&(board[1][0]='O')&&(board[2][0]='O')) ||
((board[0][1]='O')&&(board[1][1]='O')&&(board[2][1]='O')) ||
((board[0][2]='O')&&(board[1][2]='O')&&(board[2][2]='O')) ||
((board[0][0]='O')&&(board[1][1]='O')&&(board[2][2]='O')) ||
((board[2][0]='O')&&(board[1][1]='O')&&(board[0][2]='O')))
{
cout<<"Congratulations player Two! You Win!!"<<endl;
}
cout<<"Player Two, enter in the row"<<endl;
cout<<"that you want an O in"<<endl;
cout<<"Enter here-->";
cin>>row;
cout<<"Player Two, enter the column-->";
cin>>col;
legal = false;
while (legal == false)
{
legal= check(row, col);
if (!legal)
{
cout<<"Spot already taken."<<endl;
cout<<"Enter in row again.-->";
cin>>row;
cout<<"Enter in column again."<<endl;
cin>>col;
}
}
board[row][col] = 'O';
active();
if (((board[0][0]='X')&&(board[0][1]='X')&&(board[0][2]='X')) ||
((board[1][0]='X')&&(board[1][1]='X')&&(board[1][2]='X')) ||
((board[2][0]='X')&&(board[2][1]='X')&&(board[2][2]='X')) ||
((board[0][0]='X')&&(board[1][0]='X')&&(board[2][0]='X')) ||
((board[0][1]='X')&&(board[1][1]='X')&&(board[2][1]='X')) ||
((board[0][2]='X')&&(board[1][2]='X')&&(board[2][2]='X')) ||
((board[0][0]='X')&&(board[1][1]='X')&&(board[2][2]='X')) ||
((board[2][0]='X')&&(board[1][1]='X')&&(board[0][2]='X')))
cout<<"Congratulations player One! You Win!!"<<endl;
else if(((board[0][0]='O')&&(board[0][1]='O')&&(board[0][2]='O')) ||
((board[1][0]='O')&&(board[1][1]='O')&&(board[1][2]='O')) ||
((board[2][0]='O')&&(board[2][1]='O')&&(board[2][2]='O')) ||
((board[0][0]='O')&&(board[1][0]='O')&&(board[2][0]='O')) ||
((board[0][1]='O')&&(board[1][1]='O')&&(board[2][1]='O')) ||
((board[0][2]='O')&&(board[1][2]='O')&&(board[2][2]='O')) ||
((board[0][0]='O')&&(board[1][1]='O')&&(board[2][2]='O')) ||
((board[2][0]='O')&&(board[1][1]='O')&&(board[0][2]='O')))
cout<<"Congratulations player Two! You Win!!"<<endl;
}
cout<<"Looks like we got ourselves a tie!"<<endl;
system ("PAUSE");
return 0;
}
bool check(int r, int c)
{
if (board [r][c] != '*')
return false;
else
return true;
}
void active (void)
{
cout<<" TIC TAC TOE "<<endl;
cout<<"________________________________"<<endl;
cout<<" C 0 C 1 C 2 "<<endl<<endl;
cout<<" R 0 | "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" "<<endl;
cout<<" -----------------------"<<endl;
cout<<" R 1 | "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" "<<endl;
cout<<" -----------------------"<<endl;
cout<<" R 2 | "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" "<<endl;
}
/*void wincheck (void)
{
if (((board[0][0]='X')&&(board[0][1]='X')&&(board[0][2]='X')) ||
((board[1][0]='X')&&(board[1][1]='X')&&(board[1][2]='X')) ||
((board[2][0]='X')&&(board[2][1]='X')&&(board[2][2]='X')) ||
((board[0][0]='X')&&(board[1][0]='X')&&(board[2][0]='X')) ||
((board[0][1]='X')&&(board[1][1]='X')&&(board[2][1]='X')) ||
((board[0][2]='X')&&(board[1][2]='X')&&(board[2][2]='X')) ||
((board[0][0]='X')&&(board[1][1]='X')&&(board[2][2]='X')) ||
((board[2][0]='X')&&(board[1][1]='X')&&(board[0][2]='X')))
cout<<"Congratulations player One! You Win!!"<<endl;
else if (((board[0][0]='O')&&(board[0][1]='O')&&(board[0][2]='O')) ||
((board[1][0]='O')&&(board[1][1]='O')&&(board[1][2]='O')) ||
((board[2][0]='O')&&(board[2][1]='O')&&(board[2][2]='O')) ||
((board[0][0]='O')&&(board[1][0]='O')&&(board[2][0]='O')) ||
((board[0][1]='O')&&(board[1][1]='O')&&(board[2][1]='O')) ||
((board[0][2]='O')&&(board[1][2]='O')&&(board[2][2]='O')) ||
((board[0][0]='O')&&(board[1][1]='O')&&(board[2][2]='O')) ||
((board[2][0]='O')&&(board[1][1]='O')&&(board[0][2]='O')))
cout<<"Congratulations player Two! You Win!!"<<endl;
}
*/