Home > Back-end >  Tic-tac-toe chess man-machine against
Tic-tac-toe chess man-machine against

Time:09-27

Intend to use minimax algorithm for a tic-tac-toe chess man-machine against, the program can run normally, but can not reach the computer lose effect, trouble can debug the great god and see what place? Thank you

#include
using namespace std;
# define BOARDNUM 10

Char square [BOARDNUM]={' 0 ', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
Char player='X', the CPU='O';

Bool isMovesLeft (char square [BOARDNUM])
{
For (int I=1; iIf (square [I]==I + '0')
return true;
}

return false;
}

Int the evaluate (char square [BOARDNUM])
{
If (square [1]==square [2] & amp; & Square [2]==square [3]) {
If (square [1]==CPU)
Return 10;
Else if (square [1]==player), return to 10;
}

If (square [4]==square [5] & amp; & Square [5]==square [6]) {
If (square [4]==CPU)
Return 10;
Else if (square [4]==player), return to 10;
}

If (square [7]==square [8] & amp; & Square [8]==square [9]) {
If (square [7]==CPU)
Return 10;
Else if (square [7]==player), return to 10;
}

If (square [1]==square [4] & amp; & Square [4]==square [7]) {
If (square [1]==CPU)
Return 10;
Else if (square [1]==player), return to 10;
}

If (square [2]==square [5] & amp; & Square [5]==square [8]) {
If (square [2]==CPU)
Return 10;
Else if (square [2]==player), return to 10;
}

If (square [3]==square [6] & amp; & Square [6]==square [9]) {
If (square [3]==CPU)
Return 10;
Else if (square [3]==player), return to 10;
}

If (square [1]==square [5] & amp; & Square [5]==square [9]) {
If (square [1]==CPU)
Return 10;
Else if (square [1]==player), return to 10;
}

If (square [3]==square [5] & amp; & Square [5]==square [7]) {
If (square [3]==CPU)
Return 10;
Else if (square [3]==player), return to 10;
}

return 0;

}


Int minimax (char square [BOARDNUM], bool isCpu)
{
Int score=the evaluate (square);

If (score==10) {
Return 10;
}

If (score==- 10)
The return - 10;

If (isMovesLeft (square)==false)
return 0;


If (isCpu)
{
Int best=- 1000;
For (int I=1; iIf (square [I]==I + '0') {
Square [I]=CPU;
If (bestBest=minimax (square, false);
}
Square [I]=I + '0';

}
}
Return the best;
}

The else
{
Int best=1000;
For (int I=1; iIf (square [I]==I + '0') {
Square [I]=player;
If (best> Minimax (square, true)) {
Best=minimax (square, true);
}
Square [I]=I + '0';
}
}
Return the best;
}
}


Int bestMove (char square [BOARDNUM])
{
Int bestScore=- 1000;
Int move;
Int score;
For (int I=1; iIf (square [I]==I + '0') {
Square [I]=CPU;
Score=minimax (square, false);
Square [I]=I + '0';
}
If (score> BestScore) {
BestScore=score;
Move=I;
}
}
Return a move;
}



Void printboard ()
{
system("cls");
Cout & lt; <"\ n \ n \ tTic Tac Toe \ n \ n";

Cout & lt; <"The Player (X) - Computer (O)" & lt; Cout & lt;
Cout & lt; <"| |" & lt; Cout & lt; <"" & lt;
Cout & lt; <"| | _____ _____ _____" & lt; Cout & lt; <"| |" & lt;
Cout & lt; <"" & lt;
Cout & lt; <"| | _____ _____ _____" & lt; Cout & lt; <"| |" & lt;
Cout & lt; <"" & lt;
Cout & lt; <"| |" & lt; }


Int main ()
{

Do {
Printboard ();
Int choice;
Cout & lt; <"The Player enter a number:";
Cin> Choice;

If (choice==1 & amp; & Square [1]=='1')
Square [1]=player;
Else if (choice==2 & amp; & Square [2]=='2')
Square [2]=player;
Else if (choice==3 & amp; & Square [3]=='3')
Square [3]=player;
Else if (choice==4 & amp; & Square [4]=='4')
Square [4]=player;
Else if (choice==5 & amp; & Square [5]=='5')
Square [5]=player;
Else if (choice==6 & amp; & Square [6]=='6')
Square [6]=player;
Else if (choice==7 & amp; & Square [7]=='7')
Square [7]=player;
Else if (choice==8 & amp; & Square [8]=='8')
Square [8]=player;
Else if (choice==9 & amp; & Square [9]=='9')
Square [9]=player;
The else {
Char blank;
cout<" Invalid move and input anything "& lt; Cin> nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related