I'm working on this Tic-Tac-Toe game for my programming class, and I've gotten it all but completed, but I'm now trying to implement a mechanic to handle tie games. The computer has at least one condition for every possible move on the board, but for some reason, even if none of these execute, the 'else' statement I have after them doesn't execute either.
[code]
#include<iostream>
using namespace std;
int cointoss();
int gameover(int);
class board
{
public:
void newgame();
void printboard();
void makemove(int);
void cpumove();
void turnswitch(int);
void firstturn(int);
void reservenum(int);
void cat();
void gameover(int);
void wincondition();
int taken(int);
int isitover();
private:
char grid[3][3];
int turn;
int gamecheck;
int reserved[16];
};
void board::newgame()
{
gamecheck=1;
grid[0][0]='1';
grid[0][1]='2';
grid[0][2]='3';
grid[1][0]='4';
grid[1][1]='5';
grid[1][2]='6';
grid[2][0]='7';
grid[2][1]='8';
grid[2][2]='9';
}
int board::taken(int takecheck)
{
if(reserved[takecheck]==1) return 1;
else return 0;
}
void board::makemove(int move)
{
int row0count,row1count,row2count;
int goodmove;
do{
char movenum;
movenum='0'+move;
if(taken(move)==0)
{
for(row0count=0;row0count<=2;row0count++)
{
if(grid[0][row0count]==movenum)
{
grid[0][row0count]='X';
}
}
for(row1count=0;row1count<=2;row1count++)
{
if(grid[1][row1count]==movenum)
{
grid[1][row1count]='X';
}
}
for(row2count=0;row2count<=2;row2count++)
{
if(grid[2][row2count]==movenum)
{
grid[2][row2count]='X';
}
}
goodmove=1;
}
else
{
cout <<"Invalid move. Please try another." <<endl;
cin >>move;
goodmove=0;
}
}while(goodmove==0);
turnswitch(1);
printboard();
reservenum(move);
}
void board::reservenum(int num)
{
reserved[num]=1;
}
void board::firstturn(int firstturn)
{
if(firstturn==1) turn=1;
else if(firstturn==2) turn=2;
}
void board::turnswitch(int turnswitch)
{
if(turnswitch==1) turn=2;
else if(turnswitch==2) turn=1;
}
void board::cpumove()
{
if((turn==2)&&(gamecheck==1))
{
if(grid[0][0]=='1'){grid[0][0]='O'; reservenum(1);}
else if ((grid[0][2]=='O')&&(grid[1][1]=='O')&&(grid[2][0]=='7')){grid[2][0]='O'; reservenum(7);}
else if (((grid[0][2]=='3')&&(grid[0][0]=='X')&&(grid[0][1]=='2'))||((grid[0][1]!='2')&&(grid[1][0]!='4')&&(grid[2][2]!='9'))){grid[0][2]='O'; reservenum(3);}
else if ((grid[1][0]=='4')&&(grid[0][0]=='O')&&(grid[2][0]=='7')){grid[1][0]='O'; reservenum(4);}
else if (((grid[0][0]=='O')&&(grid[1][0]=='O')&&(grid[2][0]=='7'))||((grid[0][2]=='O')&&(grid[1][1]=='O')&&(grid[2][0]=='7'))){grid[2][0]='O'; reservenum(7);}
else if ((grid[1][1]=='5')&&(((grid[0][0]=='O')||(grid[0][2]=='O'))&&((grid[2][2]=='9')||(grid[2][0]=='7'))||((grid[0][1]!='2')&&(grid[1][0]!='4')))){grid[1][1]='O'; reservenum(5);}
else if ((grid[0][0]=='O')&&(grid[1][1]=='O')&&(grid[2][2]=='9')){grid[2][2]='O'; reservenum(9);}
else if ((grid[0][2]=='O')&&(grid[1][2]=='6')&&(grid[2][2]=='9')){grid[1][2]='O'; reservenum(6);}
else if ((grid[1][1]=='O')&&((grid[2][2]!='9')||(grid[0][0]!='O'||'1'))&&(grid[0][1]=='2')&&(grid[2][1]=='8')){grid[2][1]='O'; reservenum(8);}
else if ((grid[0][1]=='2')&&(grid[2][1]=='O')&&(grid[1][1]=='O')){grid[0][1]='O'; reservenum(2);}
else if ((grid[0][2]=='O')&&(grid[1][2]=='O')&&(grid[2][2]=='9')){grid[2][2]='O'; reservenum(9);}
else if ((grid[0][0]=='O')&&(grid[1][0]=='O')&&(grid[0][2]=='3')){grid[0][2]='O'; reservenum(3);}
else gameover(3);
}
wincondition();
cout <<"Computer's Move: " <<endl;
printboard();
turnswitch(2);
}
void board::printboard()
{
int row0,row1,row2;
for(row0=0;row0<=2;row0++)
{
cout <<"|"<<grid[0][row0];
}
cout <<"|" <<endl;
for(row1=0;row1<=2;row1++)
{
cout <<"|"<<grid[1][row1];
}
cout <<"|" <<endl;
for(row2=0;row2<=2;row2++)
{
cout <<"|"<<grid[2][row2];
}
cout <<"|" <<endl;
}
void board::wincondition()
{
if((grid[0][0]=='X')&&(grid[0][1]=='X')&&(grid[0][2]=='X')) gameover(1);
else if((grid[1][0]=='X')&&(grid[1][1]=='X')&&(grid[1][2]=='X')) gameover(1);
else if((grid[2][0]=='X')&&(grid[2][1]=='X')&&(grid[2][2]=='X')) gameover(1);
else if((grid[0][0]=='X')&&(grid[1][0]=='X')&&(grid[2][0]=='X')) gameover(1);
else if((grid[0][1]=='X')&&(grid[1][1]=='X')&&(grid[2][1]=='X')) gameover(1);
else if((grid[0][2]=='X')&&(grid[1][2]=='X')&&(grid[2][2]=='X')) gameover(1);
else if((grid[0][0]=='X')&&(grid[1][1]=='X')&&(grid[2][2]=='X')) gameover(1);
else if((grid[0][2]=='X')&&(grid[1][1]=='X')&&(grid[2][0]=='X')) gameover(1);
else if((grid[0][0]=='O')&&(grid[0][1]=='O')&&(grid[0][2]=='O')) gameover(2);
else if((grid[1][0]=='O')&&(grid[1][1]=='O')&&(grid[1][2]=='O')) gameover(2);
else if((grid[2][0]=='O')&&(grid[2][1]=='O')&&(grid[2][2]=='O')) gameover(2);
else if((grid[0][0]=='O')&&(grid[1][0]=='O')&&(grid[2][0]=='O')) gameover(2);
else if((grid[0][1]=='O')&&(grid[1][1]=='O')&&(grid[2][1]=='O')) gameover(2);
else if((grid[0][2]=='O')&&(grid[1][2]=='O')&&(grid[2][2]=='O')) gameover(2);
else if((grid[0][0]=='O')&&(grid[1][1]=='O')&&(grid[2][2]=='O')) gameover(2);
else if((grid[0][2]=='O')&&(grid[1][1]=='O')&&(grid[2][0]=='O')) gameover(2);
}
void board::gameover(int wincheck)
{
if(wincheck==1)
{
cout <<"You are victorious." <<endl <<endl;
printboard();
gamecheck=0;
}
else if(wincheck==2)
{
cout <<"The computer is victorious." <<endl <<endl;
printboard();
gamecheck=0;
}
else if(wincheck==3)
{
cout <<"Victory is impossible." <<endl <<endl;
printboard();
gamecheck=0;
}
gamecheck=0;
}
int board::isitover()
{
if(gamecheck==1)
{
return 1;
}
else if(gamecheck==0)
{
return 0;
}
}
int main()
{
srand(time(NULL));
char humancpu[16];
int coin;
int option;
int menu=1;
int move;
int rungame;
board Field;
cout <<"Welcome to Tic-Tac-Toe.\n\n";
do{
cout <<"Menu\n"
<<"----\n"
<<"1. New Game\n"
<<"2. Exit\n"
<<"\nEnter your selection:";
cin >>option;
switch(option)
{
case 1:
Field.newgame();
coin=cointoss();
//coin=1;
if(coin==1) strcpy(humancpu,"you");
else if(coin==2) strcpy(humancpu,"the computer");
cout <<"The coin toss has determined that " <<humancpu <<" will have the first turn.\n";
switch(coin)
{
case 1:
Field.firstturn(coin);
Field.printboard();
do{
Field.wincondition();
rungame=Field.isitover();
if(rungame==1)
{
cout <<"You are playing as Xs. Make your move: ";
cin >>move;
Field.makemove(move);
}
Field.wincondition();
rungame=Field.isitover();
if(rungame==1)
{
Field.cpumove();
}
}while(rungame==1);
break;
case 2:
Field.firstturn(coin);
do{
Field.wincondition();
rungame=Field.isitover();
if(rungame==1)
{
Field.cpumove();
}
Field.wincondition();
rungame=Field.isitover();
if(rungame==1)
{
cout <<"You are playing as Xs. Make your move: ";
cin >>move;
Field.makemove(move);
}
}while(rungame==1);
break;
}
case 2:
menu=0;
break;
default:
cout <<"Invalid selection." <<endl <<endl;
break;
}
}while(menu==1);
system("Pause");
return 0;
}
int cointoss()
{
int result;
result=(rand()%2)+1;
return result;
}
[/code]
So, if I make the moves 2, 4, 9, and 7, making victory impossible for either player, it just prints "Computer's Move: " and the board exactly as it was after my last move. By the fact the computer doesn't make a move, I know none of the move conditions are executed, but if that's true, then [B]else gameover(3);[/B] should terminate the program.
What's going on here?
Well my first thought is that maybe one of your conditions is firing when you aren't expecting it to. What you have there is quite a messy set of else's, I think it would be wise to have everything in it's own separate if/else and that should help traces.
Another option would be to have debug messages everywhere, in the code executed after the conditional statement add some code to print to the console showing which one fired, it should help determine what exactly is going on.
Ah, I had an or condition for making move #3 that it could keep executing because it didn't check if 3 was already taken. Thanks.
Better code formatting would help. I actually made a game like this in flash a while back.
Oh, here is my old topic about it.
[url]http://www.facepunch.com/showthread.php?t=445583[/url]
As far as where the game is, I forget. Just make sure each situation works out before moving onto the next one. Otherwise you'll find that certain things don't work out and it'll take forever to find the tiny error in your code.
Sorry, you need to Log In to post a reply to this thread.