示例#1
0
        public MinimaxOutcome minimax(Int64[,] board, Int64 depth, Boolean minOrMax)
        {
            if (depth == 0)
            {
                return(new MinimaxOutcome
                {
                    Evaluation = Evaluate(board)
                });
            }

            if (minOrMax)
            {
                Int64                    maxEval            = -1000;
                PotentialNextMove        bestMove           = null;
                List <PotentialNextMove> player2MovesBoards = GetAvailableBoards(board, 2);
                foreach (PotentialNextMove player2MovesBoard in player2MovesBoards)
                {
                    MinimaxOutcome evaluation = minimax(player2MovesBoard.Board, depth - 1, false);
                    maxEval = Math.Max(maxEval, evaluation.Evaluation);
                    if (maxEval == evaluation.Evaluation)
                    {
                        bestMove = player2MovesBoard;
                    }
                }

                return(new MinimaxOutcome
                {
                    PotentialNextMove = bestMove,
                    Evaluation = maxEval
                });
            }
            else
            {
                Int64                    minEval            = 1000;
                PotentialNextMove        bestMove           = null;
                List <PotentialNextMove> player1MovesBoards = GetAvailableBoards(board, 1);
                foreach (PotentialNextMove player1MovesBoard in player1MovesBoards)
                {
                    MinimaxOutcome evaluation = minimax(player1MovesBoard.Board, depth - 1, true);
                    minEval = Math.Min(minEval, evaluation.Evaluation);
                    if (minEval == evaluation.Evaluation)
                    {
                        bestMove = player1MovesBoard;
                    }
                }

                return(new MinimaxOutcome
                {
                    PotentialNextMove = bestMove,
                    Evaluation = minEval
                });
            }
        }
        public MinimaxOutcome minimax(int[,] board, int depth, bool minOrMax, List <NextMove> moves)
        {
            if (depth == 0)
            {
                return(new MinimaxOutcome
                {
                    Evaluation = evaluate(board, moves)
                });
            }

            if (minOrMax)
            {
                int                   maxEval            = -1000;
                AvailableBoard        bestMove           = null;
                List <AvailableBoard> player2MovesBoards = GetAvailableBoards(board, 2);
                foreach (AvailableBoard player2MovesBoard in player2MovesBoards)
                {
                    MinimaxOutcome evaluation = minimax(player2MovesBoard.Board, depth - 1, false, player2MovesBoard.moves);
                    maxEval = Math.Max(maxEval, evaluation.Evaluation);
                    if (maxEval == evaluation.Evaluation)
                    {
                        bestMove = player2MovesBoard;
                    }
                }

                return(new MinimaxOutcome
                {
                    Board = bestMove,
                    Evaluation = maxEval
                });
            }
            else
            {
                int                   minEval            = 1000;
                AvailableBoard        bestMove           = null;
                List <AvailableBoard> player1MovesBoards = GetAvailableBoards(board, 1);
                foreach (AvailableBoard player1MovesBoard in player1MovesBoards)
                {
                    MinimaxOutcome evaluation = minimax(player1MovesBoard.Board, depth - 1, true, player1MovesBoard.moves);
                    minEval = Math.Min(minEval, evaluation.Evaluation);
                    if (minEval == evaluation.Evaluation)
                    {
                        bestMove = player1MovesBoard;
                    }
                }

                return(new MinimaxOutcome
                {
                    Board = bestMove,
                    Evaluation = minEval
                });
            }
        }
示例#3
0
        public NextMove GetNextMoveForAutomatedPlayer(GetNextMove getNextMove)
        {
            MinimaxOutcome result = minimax(getNextMove.Board, getNextMove.Depth, true);

            return(new NextMove
            {
                CurrentHeight = result.PotentialNextMove.CurrentHeight,
                CurrentWidth = result.PotentialNextMove.CurrentWidth,
                NextHeight = result.PotentialNextMove.NextHeight,
                NextWidth = result.PotentialNextMove.NextWidth,
                Takes = result.PotentialNextMove.Takes
            });
        }