// Serial version of the minimax algorithm.
    public async static Task<Tuple<int, int>> Minimax(string board, char player, char opponent, char empty, char max, int iterCount, CallBackFn iterCallback)
    {
        ++iterCount;
        iterCallback(iterCount);
        var space = new Tuple<int, int> (-1, 0);
        var w = Winner(board, player, opponent, empty);
        if (w == empty)
        {
            space = new Tuple<int, int>(space.Item1, 0);
        }
        else if (w == max) 
        {
            space = new Tuple<int, int>(space.Item1, 1);
        }
        else
        {
            space = new Tuple<int, int>(space.Item1, -1);
        }
        if (Terminal(board, player, opponent, empty))
        {
            return space;
        }

        bool first = true;
        Tuple<int, int> best = new Tuple<int, int>(0, 0);
        var moves = AvailableMoves(board, empty);
        for (int i = 0; i < moves.Length; i++)
        {
            if (moves[i] != 0)
            {
                StringBuilder new_board = new StringBuilder(board);
                new_board[i] = player;
                Tuple<int, int> temp = await MiniMax.Minimax(new_board.ToString(), opponent, player, empty, max, iterCount, iterCallback);
                temp = new Tuple<int, int>(i, temp.Item2);

                // If the new move is better than our previous move, take it.
                if (first || (player == max && temp.Item2 > best.Item2) || (player != max && temp.Item2 < best.Item2))
                { 
                    first = false;
                    best = temp; 
                }
            }
        }
        return best;
        }
예제 #2
0
        // Serial version of the minimax algorithm.
        public async static Task <Tuple <int, int> > Minimax(string board, char player, char opponent, char empty, char max, int iterCount, CallBackFn iterCallback)
        {
            ++iterCount;
            iterCallback(iterCount);
            var space = new Tuple <int, int> (-1, 0);
            var w     = Winner(board, player, opponent, empty);

            if (w == empty)
            {
                space = new Tuple <int, int>(space.Item1, 0);
            }
            else if (w == max)
            {
                space = new Tuple <int, int>(space.Item1, 1);
            }
            else
            {
                space = new Tuple <int, int>(space.Item1, -1);
            }
            if (Terminal(board, player, opponent, empty))
            {
                return(space);
            }

            bool             first = true;
            Tuple <int, int> best  = new Tuple <int, int>(0, 0);
            var moves = AvailableMoves(board, empty);

            for (int i = 0; i < moves.Length; i++)
            {
                if (moves[i] != 0)
                {
                    StringBuilder new_board = new StringBuilder(board);
                    new_board[i] = player;
                    Tuple <int, int> temp = await MiniMax.Minimax(new_board.ToString(), opponent, player, empty, max, iterCount, iterCallback);

                    temp = new Tuple <int, int>(i, temp.Item2);

                    // If the new move is better than our previous move, take it.
                    if (first || (player == max && temp.Item2 > best.Item2) || (player != max && temp.Item2 < best.Item2))
                    {
                        first = false;
                        best  = temp;
                    }
                }
            }
            return(best);
        }