Exemplo n.º 1
0
        public Tuple <int, int> minimaxVal(Board b, int d, int alpha, int beta, ParallelOptions po)
        {
            // Base case
            if (b.gameOver() || d == 0)
            {
                return(Tuple.Create(0, evaluate(b)));
            }

            // Cancel process if time is up
            po.CancellationToken.ThrowIfCancellationRequested();

            // Initalize variables to make minimax code shorter
            bool myTurn   = b.whoseMove() == position;
            int  bestMove = -1;
            int  offset   = myTurn ? offsets.Item1 : offsets.Item2;
            int  bestVal  = myTurn ? int.MinValue : int.MaxValue;

            // Minimaxing
            for (int move = 0 + offset; move <= 5 + offset; move++)
            {
                if (b.legalMove(move))
                {
                    Board b1 = new Board(b);
                    b1.makeMove(move, false);
                    Tuple <int, int> res = minimaxVal(b1, d - 1, alpha, beta, po);
                    if (miniMaxCompare(res.Item2, bestVal, myTurn))
                    {
                        bestMove = move;
                        bestVal  = res.Item2;
                    }
                    // Update alpha or beta
                    if (myTurn)
                    {
                        alpha = Math.Max(alpha, bestVal);
                    }
                    else
                    {
                        beta = Math.Min(beta, bestVal);
                    }
                    // Prune the tree
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
            }

            return(Tuple.Create(bestMove, bestVal));
        }
Exemplo n.º 2
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());
        }