1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
// Check for end of game conditions
if (cSquare1 != '1') {
if (cSquare2 == cSquare1 && cSquare3 == cSquare1) {
bGameOver = true;
}
if (cSquare4 == cSquare1 && cSquare7 == cSquare1) {
bGameOver = true;
}
}
if (cSquare5 != '5') {
if (cSquare1 == cSquare5 && cSquare9 == cSquare5) {
bGameOver = true;
}
if (cSquare2 == cSquare5 && cSquare8 == cSquare5) {
bGameOver = true;
}
if (cSquare4 == cSquare5 && cSquare6 == cSquare5) {
bGameOver = true;
}
if (cSquare3 == cSquare5 && cSquare7 == cSquare5) {
bGameOver = true;
}
}
if (cSquare9 != '9') {
if (cSquare3 == cSquare9 && cSquare6 == cSquare9) {
bGameOver = true;
}
if (cSquare7 == cSquare9 && cSquare8 == cSquare9) {
bGameOver = true;
}
}
// Need to check the board full (no-win condition)
if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' && !bGameOver) // If board full then this executed
{
bGameOver = true; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // Game Over with no one win
bWinGame = false; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Game OVER !!
}
if (bGameOver) { // Game is over and tell who wins.
if (bWinGame == 1 && Turn == 1) { // if the game is over in turn 1, then player wins.
cout << "\nYou" << " wins!" << endl << endl;
} else if (bWinGame == 1&& Turn == 2) { // if the game is over in turn != 1 or Turn 2, AI wins.
cout << "\nAI wins! " << endl << endl;
}
// Print ending board
cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
cout << "-+-+-"<< endl;
cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
cout << "-+-+-"<< endl;
cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl;
cout << "Game Over!" << endl;
cout << "Play again (y/n)?: "; // Ask player to play again or not.
char cPlayAgain;
cin >> cPlayAgain;
if (cPlayAgain == 'y') {
bGameOver = false;
// Clear the board
cSquare1 = '1';
cSquare2 = '2';
cSquare3 = '3';
cSquare4 = '4';
cSquare5 = '5';
cSquare6 = '6';
cSquare7 = '7';
cSquare8 = '8';
cSquare9 = '9';
}
Turn = 1;
} else {
// Alternate player turns also inform that if Turn != 1 so it means Turn = 2 which is responsible in tell the player who wins
if (Turn == 1) {
Turn = 2;
} else {
Turn = 1;
}
}
} while (!bGameOver);
cout << "\nThank you for playing with stupid AI."; getch(); // I believe the stupid here is me.
return 0;
}
|
First I know this code is very big, But I have give very detailed comment so a very beginner will easy to understand this code, secondly you will notice that I don't use any array. So please read it first slowly(if you still don not want to read it, then don't and jump to my last comment in this topic). Or you can just go to line 131st I believe that's the main problem(or not).
Now the problem is if AI input invalid command and do the loop, the computer will start at line 75th, thus input that invalid command again, and then loop again and do the same thing forever, for example, I input 3 and then the AI input 6, now I input 5, The AI block me and input 7, next I input 2 so I have a 2 X row which is 3 and 2(If I input 1 in the my next turn I will win), but for the AI, instead defending and block square 1, she insist to input 7 although she have already block it before, this make her input invalid, hence the program will loop and order the AI to input her move again, but she do the bad ass input which is 7 again, this become an infinite loop of invalid(For the easy understand, just copy paste this code into your IDE and run it, you will understand very quick).
One thing that makes me very confused is, take a look at line 77th, there is srand(time(NULL)), If I move this thing to the line 131th, exactly just before rand() % 9+1; the whole AI defensive system code will be skipped(or not recognized) by the program, thus the AI will input perfectly random number, it just become the stupid AI like the level 2 stars modified.
I have search everywhere for this problem but cannot find the answer, of course there are many guide in making Tic Tac Toe versus AI, but they all using array at least one, in my case I want to fix this without any array,
The question is, is there any one willing to help me solve/fix my algorithm ?
you can try to play with it for a while, maybe you know something that I missed(perhaps very basic like I must add another if else i.e if the AI have blocked square 7 she should not pick number 7 again ? But I believe this is not efficient).
Anyway, I appreciate it very much, thank you.
P.S : If you suggest me to use array then go for it, so I know that it's actually impossible to make Tic Tac Toe AI defensive version without array(But I finished making Tic Tac Toe with stupid AI that input random number without any array, and work perfect)