コード例 #1
0
ファイル: mcw33Player.cs プロジェクト: mcwissink/mancala-bot
        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));
        }
コード例 #2
0
ファイル: BonzoPlayer.cs プロジェクト: mcwissink/mancala-bot
 public override int chooseMove(Board b)
 {
     if (b.whoseMove() == Position.Top)
     {
         for (int i = 12; i >= 7; i--)               // try first go-again
         {
             if (b.stonesAt(i) == 13 - i)
             {
                 return(i);
             }
         }
         for (int i = 12; i >= 7; i--)               // otherwise, first
         {
             if (b.stonesAt(i) > 0)
             {
                 return(i);                          // available move
             }
         }
     }
     else
     {
         for (int i = 5; i >= 0; i--)
         {
             if (b.stonesAt(i) == 6 - i)
             {
                 return(i);
             }
         }
         for (int i = 5; i >= 0; i--)
         {
             if (b.stonesAt(i) > 0)
             {
                 return(i);
             }
         }
     }
     return(-1);                 // an illegal move if there aren't any legal ones
 }                               // this can't happen unless game is over
コード例 #3
0
ファイル: MancalaMatch.cs プロジェクト: mcwissink/mancala-bot
        /*
         * 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());
        }