I am trying to write AI algorithm using MinMax to checkers game. Everything was fine before final test... The computer chooses any piece and after that he is coming to my guys. He really wants to die fast. Here is my code:
public MoveAndPoints MinMax(Dictionary<int, Field> gameBoard, string myColor, Boolean maximizingPlayer, int depth)
{
MoveAndPoints bestValue = new MoveAndPoints();
if (0 == depth)
{
return new MoveAndPoints(((myColor == enemyColor) ? 1 : -1) * evaluateGameBoard(gameBoard, myColor), bestValue.move);
}
MoveAndPoints val = new MoveAndPoints();
if (maximizingPlayer)
{
bestValue.points = int.MinValue;
foreach (Move move in GetPossibleMoves(gameBoard, myColor))
{
gameBoard = ApplyMove(gameBoard, move);
bestValue.move = move;
val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), false, depth - 1);
bestValue.points = Math.Max(bestValue.points, val.points);
gameBoard = RevertMove(gameBoard, move);
}
return bestValue;
}
else
{
bestValue.points = int.MaxValue;
foreach (Move move in GetPossibleMoves(gameBoard, myColor))
{
gameBoard = ApplyMove(gameBoard, move);
val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), true, depth - 1);
bestValue.points = Math.Min(bestValue.points, val.points);
gameBoard = RevertMove(gameBoard, move);
}
return bestValue;
}
}
This is my heuretic:
public int evaluateGameBoard(Dictionary<int, Field> gameBoard, string color)
{
return Extend.GetNumberOfPieces(gameBoard, color) - Extend.GetNumberOfPieces(gameBoard, Extend.GetEnemyPlayerColor(color));
}
For example, I have that scenario:
Instead of move any other place he is choosing to die:
If i choose death myself he only kills me because of fact that he must because this is his only move. Can someone help me fix this? I think problem is inside MinMax. I am almost sure this is not very problematic but i really can not find it :(