Example #1
0
    static public Board makeMove(int value, Treenode <int> gameTree, Treenode <Board> boardTree)
    {
        int index = -1;

        for (int i = 0; i < gameTree.ChildrenCount; i++)
        {
            Treenode <int> choice = gameTree.GetChild(i);
            for (int j = 0; j < choice.ChildrenCount; j++)
            {
                int result = choice.GetChild(j).Value;
                if (result == value)
                {
                    index = i;
                    break;
                }
            }
            if (index == i)
            {
                break;
            }
        }
        Board move = boardTree.GetChild(index).Value;

        return(move);
    }
Example #2
0
    static public int Minimax(int depth, Treenode <int> root, bool isMaxPlayer, int alpha, int beta)
    {
        if (depth == 2)
        {
            return(root.Value);
        }

        if (isMaxPlayer)
        {
            int best = MIN;
            for (int i = 0; i < root.ChildrenCount; i++)
            {
                int val = Minimax(depth + 1, root.GetChild(i), false, alpha, beta);
                best  = Math.Max(best, val);
                alpha = Math.Max(best, alpha);
                if (alpha >= beta)
                {
                    break;
                }
            }
            return(best);
        }
        else
        {
            int best = MAX;
            for (int i = 0; i < root.ChildrenCount; i++)
            {
                int val = Minimax(depth + 1, root.GetChild(i), true, alpha, beta);
                best = Math.Min(best, val);
                beta = Math.Min(best, beta);
                if (alpha >= beta)
                {
                    break;
                }
            }
            return(best);
        }
    }