Hi everyone!
I just finished writing up the main program logic for a tictactoe game and it's all working fine but half way though a game it will just exit, it will do so at a diffrent point in the game each time i play it...
i think it may be a bug in the checkwin function, but i cant see anything wrong in there...
or have i made a stupid logic error somewhere? please help
Code:#include <cstdlib> #include <iostream> using namespace std; char sqr[3][3]; struct player { string name; char syb; }; struct player play[2]; /*---------------------------------*/ void ai(void); void cou(void); void turn(void); void checkwin(void); void humanVcpu(void); /*---------------------------------*/ int main() { system("cls"); humanVcpu(); system("PAUSE"); return EXIT_SUCCESS; } void humanVcpu(void) { play[1].name = "CPU"; cout << "Please enter your name >>"; cin >> play[0].name; play[0].syb = 'X'; play[1].syb = 'O'; turn(); } void turn(void) { cou();int x,y; cout << "Row: "; cin >> x; cout << "Column: "; cin >> y; sqr[x-1][y-1] = play[0].syb; checkwin(); ai(); } void cou(void) { system("cls"); cout<<"\n\t\t 1 2 3\n"<<endl; cout<<"\t\t 1 "<<sqr[0][0]<<" | "<<sqr[0][1]<<" | "<<sqr[0][2]<<endl; cout<<"\t\t ---|---|---\n"; cout<<"\t\t 2 "<<sqr[1][0]<<" | "<<sqr[1][1]<<" | "<<sqr[1][2]<<endl; cout<<"\t\t ---|---|---\n"; cout<<"\t\t 3 "<<sqr[2][0]<<" | "<<sqr[2][1]<<" | "<<sqr[2][2]<<"\n\n\n"; } void checkwin(void) { // check if human win // - wins for (int i=0;i<3;i++) if ((sqr[i][0]==play[0].syb)&&(sqr[i][1]==play[0].syb)&&(sqr[i][2]==play[0].syb)) { cout<<play[0].name<<" wins!";system("pause"); } // | wins for (int i=0;i<3;i++) if ((sqr[0][i]==play[0].syb)&&(sqr[1][i]==play[0].syb)&&(sqr[2][i]==play[0].syb)) { cout<<play[0].name<<" wins!";system("pause"); } // / wins if ((sqr[0][0]==play[0].syb)&&(sqr[1][1]==play[0].syb)&&(sqr[2][2]==play[0].syb)) { cout<<play[0].name<<" wins!";system("pause"); } // \ wins if ((sqr[2][0]==play[0].syb)&&(sqr[1][1]==play[0].syb)&&(sqr[0][2]==play[0].syb)) { cout<<play[0].name<<" wins!";system("pause"); } // check if cpu win for (int i=0;i<3;i++) if ((sqr[i][0]==play[1].syb)&&(sqr[i][1]==play[1].syb)&&(sqr[i][2]==play[1].syb)) { cout<<play[1].name<<" wins!";system("pause"); } // | wins for (int i=0;i<3;i++) if ((sqr[0][i]==play[1].syb)&&(sqr[1][i]==play[1].syb)&&(sqr[2][i]==play[1].syb)) { cout<<play[1].name<<" wins!";system("pause"); } // / wins if ((sqr[0][0]==play[1].syb)&&(sqr[1][1]==play[1].syb)&&(sqr[2][2]==play[1].syb)) { cout<<play[1].name<<" wins!";system("pause"); } // \ wins if ((sqr[2][0]==play[1].syb)&&(sqr[1][1]==play[1].syb)&&(sqr[0][2]==play[1].syb)) { cout<<play[1].name<<" wins!";system("pause"); } } void ai(void) { // - wins for (int i=0;i<3;i++) { if ((sqr[i][0]!=play[0].syb)&&(sqr[i][0]!=play[1].syb)) if ((sqr[i][1]==play[0].syb)&&(sqr[i][2]==play[0].syb)){sqr[i][0]=play[1].syb;return;} if ((sqr[i][1]!=play[0].syb)&&(sqr[i][1]!=play[1].syb)) if ((sqr[i][0]==play[0].syb)&&(sqr[i][2]==play[0].syb)){sqr[i][1]=play[1].syb;return;} if ((sqr[i][2]!=play[0].syb)&&(sqr[i][2]!=play[1].syb)) if ((sqr[i][0]==play[0].syb)&&(sqr[i][1]==play[0].syb)){sqr[i][2]=play[1].syb;return;}} // | wins for (int i=0;i<3;i++) { if ((sqr[0][i]!=play[1].syb)&&(sqr[0][i]!=play[1].syb)) if ((sqr[1][i]==play[0].syb)&&(sqr[2][i]==play[0].syb)){sqr[0][i]=play[1].syb;return;}} // \ wins if ((sqr[0][0]!=play[0].syb)&&(sqr[0][0]!=play[1].syb)) if ((sqr[1][1]==play[0].syb)&&(sqr[2][2]==play[0].syb)){sqr[0][0]=play[1].syb;return;} if ((sqr[1][1]!=play[0].syb)&&(sqr[1][1]!=play[1].syb)) if ((sqr[0][0]==play[0].syb)&&(sqr[2][2]==play[0].syb)){sqr[1][1]=play[1].syb;return;} if ((sqr[2][2]!=play[0].syb)&&(sqr[2][2]!=play[1].syb)) if ((sqr[0][0]==play[0].syb)&&(sqr[1][1]==play[0].syb)){sqr[2][2]=play[1].syb;return;} // / wins if ((sqr[0][2]!=play[0].syb)&&(sqr[0][2]!=play[1].syb)) if ((sqr[1][1]==play[0].syb)&&(sqr[2][0]==play[0].syb)){sqr[0][2]=play[1].syb;return;} if ((sqr[1][1]!=play[0].syb)&&(sqr[1][1]!=play[1].syb)) if ((sqr[2][0]==play[0].syb)&&(sqr[0][2]==play[0].syb)){sqr[1][1]=play[1].syb;return;} if ((sqr[2][0]!=play[0].syb)&&(sqr[2][0]!=play[1].syb)) if ((sqr[0][2]==play[0].syb)&&(sqr[1][1]==play[0].syb)){sqr[2][0]=play[1].syb;return;} // if no winning sqr, pick random. srand ( time(NULL) ); int rndA, rndB; for (;;){ rndA = rand()%3; rndB = rand()%3; if ((sqr[rndA][rndB]!=play[0].syb)&&(sqr[rndA][rndB]!=play[1].syb)){sqr[rndA][rndB]=play[1].syb;break;}} turn(); }