Esempio n. 1
0
        public override int chooseMove(Board b)
        {
            int    move = -1;
            string moveString;

            while (!b.legalMove(move))
            {
                Console.Write("Your move: ");
                moveString = Console.ReadLine();
                if (!int.TryParse(moveString, out move) || !b.legalMove(move))
                {
                    Console.WriteLine("Illegal move. Try again.");
                }
            }
            return(move);
        }
Esempio n. 2
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));
        }