This question already has an answer here:
I am currently working on a Checkers game using Unity ( human vs computer ) I am done with the base architecture of the game ie defining the rules, moves, jumps etc.
I am currently stuck in implementation of MinMax for computer AI. I know how it works but I am confused on two things. Firstly, the heuristics ( I am using simple red checker count - black checker count ) but I am not sure if its correct. Secondly, outputting the final path of nodes for my AI to make its next turn.
Below is my MinMax implementation so far.
int MiniMax(TreeNode node)
{
int depth = 2;
List<TreeNode> pathNodes = new List<TreeNode>();
int final = MaxMove(node, depth, pathNodes);
return final;
}
int MaxMove(TreeNode currNode, int depth, List<TreeNode> pathNodes)
{
if (depth == 0)
{
return currNode.getPriority();
}
else
{
depth--;
int v = -10000;
List<TreeNode> allPossRedMoveNodes = gameBoardScript.createNewRedBoardsFromCurrent(currNode.getCurrentBoard());
foreach (TreeNode RedNode in allPossRedMoveNodes)
{
int v1 = MinMove(RedNode, depth,pathNodes);
if(v1 > v)
{
v = v1;
}
}
return v;
}
}
int MinMove(TreeNode currNode, int depth, List<TreeNode> pathNodes)
{
if (depth == 0)
{
return currNode.getPriority();
}
else
{
depth--;
int v = 10000;
List<TreeNode> allPossBlackMoveNodes = gameBoardScript.createNewBlackBoardsFromCurrent(currNode.getCurrentBoard());
foreach (TreeNode BlackNode in allPossBlackMoveNodes)
{
int v1 = MaxMove(BlackNode, depth,pathNodes);
if (v1 < v)
{
v = v1;
}
}
return v;
}
}
Max in this case would be Black Checkers ( Computer AI ) and Min is Red Checker, the human player
Let me briefly explain what I am trying to do with my code above. I have a TreeNode structure that stores the GameBoard, next possible move and also the Heuristic value
In My Max Move, I get all the possible Red Move Nodes from the current board. Logic for finding all possible moves is working absolutely fine. And for my Min Move which would be the human player, I get all its possible black nodes.
I am currently testing the code with depths 2 to 3 for my game tree. Please correct me if my current implementation is wrong. And finally how or where do I trace the final path of all my final Tree Nodes based on Min and Max values generated and get the next move for the Black Checker?