im working on minimax algoritm now, i have a problem to compare each node and cant return the best node. once i can compare it, it still choose the bottom of the node, can someone help me to fix my codes ?
here is my codes:
public Node minimax(ArrayList newBoard, int depth, Player p, Node n)
{
int value = 0;
if (depth == 0 || StatePosition.PlayerWins(p, newBoard))
{
n.Score = StatePosition.getScore(newBoard); //the heuristic value of node
Debug.Log(n.Pos + " - " + n.Des + " " + n.MinMax + " " + n.Score);
return n; //out n
}
else
{
Children(p, newBoard, n); //generate possible moves
foreach (Node anak in n.Child)
{ //foreach moves
ArrayList Board = (ArrayList)newBoard.Clone(); //make a clone of board
Debug.Log(anak.Pos + " " + anak.Des + " move applied");
applyMove(Board, anak.Pos, anak.Des, p); //try current move
Node b = minimax((ArrayList)Board.Clone(), depth - 1, p == Player.H ? Player.C : Player.H, anak);
if (anak.MinMax == MiniMaxValue.MAX)
{
value = -1000;
if (value < anak.Score)
{
n = anak;
}
}
else
{
value = 1000;
if (value > anak.Score)
{
n = anak;
}
}
}
}
return n;
}