Esempio n. 1
0
        private static MinimaxBestMove FindBestMove(IGameBoard b, long alpha, long beta, int depthLeft)
        {
            if (depthLeft == 0 || b.IsFinished == true)
            {
                return(new MinimaxBestMove()
                {
                    Weight = b.BoardWeight,
                    Move = null
                });
            }
            else
            {
                MinimaxBestMove move = new MinimaxBestMove();
                move.Move = null;
                if (b.CurrentPlayer == 1)
                {
                    move.Weight = long.MinValue;
                }
                else if (b.CurrentPlayer == 2)
                {
                    move.Weight = long.MaxValue;
                }

                foreach (var m in b.GetPossibleMoves())
                {
                    b.ApplyMove(m);
                    long w = FindBestMove(b, alpha, beta, depthLeft - 1).Weight;
                    b.UndoLastMove();

                    if ((b.CurrentPlayer == 1) && w > alpha)
                    {
                        move.Weight = w;
                        move.Move   = m;
                        alpha       = w;
                    }

                    else if ((b.CurrentPlayer == 2) && w < beta)
                    {
                        move.Weight = w;
                        move.Move   = m;
                        beta        = w;
                    }

                    if (!(alpha < beta))
                    {
                        return(move);
                    }
                }

                return(move);
            }
        }
Esempio n. 2
0
        //private static MinimaxBestMove FindBestMove(IGameBoard b, int depthLeft, bool maximize) {
        private static MinimaxBestMove FindBestMove(IGameBoard b, int depthLeft, bool maximize, int alpha, int beta)
        {
            // Implement the minimax algorithm.
            // Your first attempt will not use alpha-beta pruning. Once that works,
            // implement the pruning as discussed in the project notes.

            // maximize = player 1
            // !maximize = player 2

            if (depthLeft == 0 || b.IsFinished)
            {
                return(new MinimaxBestMove()
                {
                    Weight = b.Weight,
                    Move = null
                });
            }

            //int bestWeight = maximize ? int.MinValue : int.MaxValue;
            IGameMove bestMove = null;

            foreach (var m in b.GetPossibleMoves())
            {
                b.ApplyMove(m);
                MinimaxBestMove w = FindBestMove(b, depthLeft - 1, !maximize, alpha, beta);
                b.UndoLastMove();

                if (maximize && w.Weight > alpha)
                {
                    alpha    = w.Weight;
                    bestMove = m;
                }
                else if (!maximize && w.Weight < beta)
                {
                    beta     = w.Weight;
                    bestMove = m;
                }

                if (alpha >= beta)
                {
                    break;
                }
            }

            return(new MinimaxBestMove()
            {
                Weight = maximize ? alpha : beta,
                Move = bestMove
            });
        }
Esempio n. 3
0
        private static MinimaxBestMove FindBestMove(IGameBoard b, int depthLeft, bool isMaximizing, long alpha, long beta)
        {
            if (depthLeft == 0 || b.IsFinished)
            {
                return(new MinimaxBestMove()
                {
                    Weight = b.BoardWeight,
                    Move = null
                });
            }

            //long bestWeight = isMaximizing ? long.MinValue : long.MaxValue;
            //long local_alpha = int.MinValue;
            //long local_beta = int.MaxValue;
            //alpha = long.MinValue;
            //beta = long.MaxValue;
            IGameMove bestMove = null;

            foreach (IGameMove move in b.GetPossibleMoves())
            {
                b.ApplyMove(move);
                MinimaxBestMove w = FindBestMove(b, depthLeft - 1, !isMaximizing, alpha, beta);
                b.UndoLastMove();
                if (isMaximizing && w.Weight > alpha)
                {
                    //bestWeight = w.Weight;
                    bestMove = move;
                    alpha    = w.Weight;
                    //local_alpha = w.Weight;
                }
                else if (!isMaximizing && w.Weight < beta)
                {
                    //bestWeight = w.Weight;
                    bestMove = move;
                    beta     = w.Weight;
                    //local_beta = w.Weight;
                }

                if (!(alpha < beta))
                {
                    if (isMaximizing)
                    {
                        return(new MinimaxBestMove()
                        {
                            Weight = beta,
                            Move = bestMove
                        });
                    }
                    else
                    {
                        return(new MinimaxBestMove()
                        {
                            Weight = alpha,
                            Move = bestMove
                        });
                    }
                }
            }

            /*
             * return new MinimaxBestMove()
             * {
             *  Weight = bestWeight,
             *  Move = bestMove
             * };
             */
            if (isMaximizing)
            {
                return(new MinimaxBestMove()
                {
                    //Weight = local_alpha,
                    Weight = alpha,
                    Move = bestMove
                });
            }
            else
            {
                return(new MinimaxBestMove()
                {
                    //Weight = local_beta,
                    Weight = beta,
                    Move = bestMove
                });
            }
        }
Esempio n. 4
0
        private static MinimaxBestMove FindBestMove(IGameBoard b, int depthLeft, bool maximize, int alpha, int beta)
        {
            // Implement the minimax algorithm.
            // Your first attempt will not use alpha-beta pruning. Once that works,
            // implement the pruning as discussed in the project notes.
            MinimaxBestMove move = new MinimaxBestMove();

            //tree empty
            if (depthLeft == 0 || b.IsFinished)
            {
                move        = new MinimaxBestMove();
                move.Weight = b.Weight;
                move.Move   = null;
                return(move);
            }

            //initializaitons
            int       bestWeight = int.MaxValue;
            IGameMove bestMove   = null;

            if (maximize)
            {
                bestWeight *= -1;
            }

            var possMoves = b.GetPossibleMoves();

            foreach (var possMove in possMoves)
            {
                b.ApplyMove(possMove);
                //return best move
                MinimaxBestMove w = FindBestMove(b, depthLeft - 1, !maximize, alpha, beta);
                b.UndoLastMove();

                //update alpha
                if (maximize && w.Weight > alpha)
                {
                    alpha      = w.Weight;
                    bestWeight = w.Weight;
                    bestMove   = possMove;

                    //return if alpha not less than beta
                    if (alpha >= beta)
                    {
                        w.Weight = beta;
                        w.Move   = bestMove;
                        return(w);
                    }
                }

                //update beta
                else if (!maximize && w.Weight < beta)
                {
                    beta       = w.Weight;
                    bestWeight = w.Weight;
                    bestMove   = possMove;

                    //return if alpha not less than beta
                    if (alpha >= beta)
                    {
                        w.Weight = alpha;
                        w.Move   = bestMove;
                        return(w);
                    }
                }

                move.Weight = bestWeight;
                move.Move   = bestMove;
            }
            return(move);
        }