Java tictactoe problems with minmax algorithm
i want to implement the MinMax algorithm for tictactoe. I have two methods min() and max() and a evaluation method, but it doesn't works. For example when i call
max(9);
Field[bestCol][bestRow]='O';
min(8);
Field[bestCol][bestRow]='X';
in the main function the result is
OX-
---
---
But the best Move for Player 'X' is to put the 'X' in the middle.
Here is my Code without the evaluation Method:
static char[][] Field = { { '-', '-', '-' },
{ '-', '-', '-' },
{ '-', '-', '-' } };
static char Player = 'O';
static char Computer = 'X';
static int Depth =9; // searchdepth
static int bestRow=0, bestCol=0; // best Move
public static int max(int depth) {
if (depth == 0) {
return evaluateMove();
}
int maxValue = Integer.MIN_VALUE;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (Field[i][j] == '-') {
Field[i][j] = Computer;
int value = min(depth - 1);
Field[i][j] ='-';
if (value > maxValue) {
maxValue = value;
if (depth == Depth) {
bestCol=i;
bestRow=j;
}
}
}
}
}
return maxValue;
}
public static int min(int depth) {
int minValue = Integer.MAX_VALUE;
if (depth == 0) {
return evaluateMove();
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (Field[i][j] == '-') {
Field[i][j] = Player;
int value = max(depth - 1);
Field[i][j] = '-';
if (value < minValue) {
minValue = value;
bestCol=i;
bestRow=j;
}
}
}
}
return minValue;
}
Best Regards
EDIT:
Thanks for your answer. To the first point i have forgotten to change the '*' to '-' Here is my Evaluation Method:
public static int evaluateMove() {
for(int i=0; i<3; i++) {
int countX=0; int countY=0;
for(int j=0; j<3; j++) {
if(Feld[i][j]==Computer) countX++;
if(Feld[i][j]==Player) countY++;
}
if(countX==3) return 10;
if(countY==3) return -10;
}
for(int j=0; j<3; j++) { // Spalten
int countX=0; int countY=0;
for(int i=0; i<3; i++) {
if(Feld[i][j]==Computer) countX++;
if(Feld[i][j]==Player) countY++;
if(countX==3) return 10;
if(countY==3) return -10;
}
}
return 0; // Unentschieden
}
Some things that struck me:
Edit: and finally (drum roll...):
That is, what you actually do:
public static void ComputeAndExecuteBestMove()
{
// since Player begins, we minimize the gain value for the first move
if ((MaxDepth-Depth) % 2 == 0)
{
max(Depth);
Field[bestCol,bestRow] = Player;
}
else
{
min(Depth);
Field[bestCol,bestRow] = Computer;
}
// next move
Depth--;
}
but what you should do:
public static void ComputeAndExecuteBestMove()
{
// since Player begins, we minimize the gain value for the first move
if ((MaxDepth-Depth) % 2 == 0)
{
min(Depth);
Field[bestCol,bestRow] = Player;
}
else
{
max(Depth);
Field[bestCol,bestRow] = Computer;
}
// next move
Depth--;
}
链接地址: http://www.djcxy.com/p/56390.html