예제 #1
0
        public Tuple <int, double> MiniMax(Board board, int depth, double alpha, double beta)
        {
            double bestResult = 0;
            double result     = 0;
            int    bestMove   = 0;

            if (board.GameOver() || depth == 0)
            {
                return(new Tuple <int, double>(0, evaluate(board)));
            }

            int start = 0;

            if (board.WhoseMove() == Position.Top)
            {
                start = 7;
            }
            else
            {
                start = 0;
            }

            if (board.WhoseMove() == position)
            {
                bestResult = double.NegativeInfinity;
                for (int move = start; move <= start + 5; move++)
                {
                    if (board.LegalMove(move) && timer.ElapsedMilliseconds < getTimePerMove())
                    {
                        Board tempBoard = new Board(board);             //duplicate board
                        tempBoard.MakeMove(move, false);                //make the move
                        result = MiniMax(tempBoard, depth - 1, alpha, beta).Item2;
                        if (result > bestResult)                        //find its value
                        {
                            bestResult = result;                        //remember if best
                            bestMove   = move;
                        }
                        if (bestResult > alpha)
                        {
                            alpha = bestResult;
                        }
                        if (beta <= alpha)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                bestResult = double.PositiveInfinity;
                for (int move = start; move <= start + 5; move++)
                {
                    if (board.LegalMove(move) && timer.ElapsedMilliseconds < getTimePerMove())
                    {
                        Board tempBoard = new Board(board);             //duplicate board
                        tempBoard.MakeMove(move, false);                //make the move
                        result = MiniMax(tempBoard, depth - 1, alpha, beta).Item2;
                        if (result < bestResult)                        //find its value
                        {
                            bestResult = result;                        //remember if best
                            bestMove   = move;
                        }
                        if (beta < bestResult)
                        {
                            beta = bestResult;
                        }
                        if (beta <= alpha)
                        {
                            break;
                        }
                    }
                }
            }
            return(new Tuple <int, double>(bestMove, bestResult));
        }
예제 #2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Play a Mankalah game with the two given players, with the firstPlayer starting.
        /// Returns TOP's score.
        /// </summary>
        /// <param name="playerTop">the player on the top</param>
        /// <param name="playerBottom">the player on the bottom</param>
        /// <param name="firstPlayer">the player who is starting the game</param>
        /// <returns>the score of the TOP player</returns>
        public static int PlayGame(Player playerTop, Player playerBottom, Position firstPlayer)
        {
            // Create a new game board.
            _board = new Board(firstPlayer);

            // Determine the player who starts.
            if (firstPlayer == Position.Top)
            {
                Console.WriteLine("Player " + playerTop.GetName() + " starts.");
            }
            else
            {
                Console.WriteLine("Player " + playerBottom.GetName() + " starts.");
            }

            // Display the current state of the game board.
            _board.Display();

            // Continue rotating turns till the game is over.
            while (!_board.GameOver())
            {
                Console.WriteLine();

                // Get the player's move and output what move the player made.
                if (_board.WhoseMove() == Position.Top)
                {
                    _move = playerTop.ChooseMove(_board);
                    Console.WriteLine(playerTop.GetName() + " chooses move " + _move);
                }

                else
                {
                    _move = playerBottom.ChooseMove(_board);
                    Console.WriteLine(playerBottom.GetName() + " chooses move " + _move);
                }

                // Commit the move to the game state. (true = verbose, false = non-verbose)
                _board.MakeMove(_move, true);

                // Display the new state of the game board.
                _board.Display();

                // Game is over, determine final results.
                if (_board.GameOver())
                {
                    if (_board.Winner() == Position.Top)
                    {
                        Console.WriteLine("Player " + playerTop.GetName() +
                                          " (TOP) wins " + _board.ScoreTopPlayer() + " to " + _board.ScoreBottomPlayer());
                    }
                    else if (_board.Winner() == Position.Bottom)
                    {
                        Console.WriteLine("Player " + playerBottom.GetName() +
                                          " (BOTTOM) wins " + _board.ScoreBottomPlayer() + " to " + _board.ScoreTopPlayer());
                    }
                    else
                    {
                        Console.WriteLine("A tie!");
                    }
                }
                // Game is not over, ask player to make their move.
                else
                if (_board.WhoseMove() == Position.Top)
                {
                    Console.WriteLine(playerTop.GetName() + " to move.");
                }
                else
                {
                    Console.WriteLine(playerBottom.GetName() + " to move.");
                }
            }

            // Return the final score for the match.
            return(_board.ScoreTopPlayer());
        }
예제 #3
0
        /*
         * Play one Kalah game with the two given players, with firstPlayer
         * starting. This function returns TOP's score.
         */
        public static int playGame(Player pTop, Player pBot, Position firstPlayer)
        {
            b = new Board(firstPlayer);

            if (firstPlayer == Position.Top)
            {
                Console.WriteLine("Player " + pTop.getName() + " starts.");
            }
            else
            {
                Console.WriteLine("Player " + pBot.getName() + " starts.");
            }

            b.Display();

            while (!b.GameOver())
            {
                Console.WriteLine();
                if (b.WhoseMove() == Position.Top)
                {
                    move = pTop.chooseMove(b);
                    Console.WriteLine(pTop.getName() + " chooses move " + move);
                }
                else
                {
                    move = pBot.chooseMove(b);
                    Console.WriteLine(pBot.getName() + " chooses move " + move);
                }

                b.MakeMove(move, true);         // last parameter says to be chatty
                b.Display();

                if (b.GameOver())
                {
                    if (b.Winner() == Position.Top)
                    {
                        Console.WriteLine("Player " + pTop.getName() +
                                          " (TOP) wins " + b.ScoreTop() + " to " + b.ScoreBot());
                    }
                    else if (b.Winner() == Position.Bottom)
                    {
                        Console.WriteLine("Player " + pBot.getName() +
                                          " (BOTTOM) wins " + b.ScoreBot() + " to " + b.ScoreTop());
                    }
                    else
                    {
                        Console.WriteLine("A tie!");
                    }
                }
                else
                if (b.WhoseMove() == Position.Top)
                {
                    Console.WriteLine(pTop.getName() + " to move.");
                }
                else
                {
                    Console.WriteLine(pBot.getName() + " to move.");
                }
            }
            return(b.ScoreTop());
        }