1

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;
}
Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
  • 1
    Don't use an `ArrayList` either use a `List` with the specific type or make your code generic. – juharr Feb 25 '16 at 12:41
  • Do not use "ArrayList" for any reason in Unity. Use `List<>` – Fattie Feb 25 '16 at 12:46
  • Why are you not using the `b` returned by your recursive call? I think we'll need more explanation on what your game is and possibly more of the code in order to help you. – juharr Feb 25 '16 at 12:52

0 Answers0