示例#1
0
        private Result minimaxVal(Board b, int d, string color)
        {    // d is depth
            int bestVal = 0;

            if (d == 0)
            {
                return(new Result(0, b.evaluate("b")));
            }
            bool Max = true;

            if (color == "b")
            {     //TOP is MAX
                bestVal = -1000000;
            }
            else
            {  // similarly for BOTTOM’s move
                //BOTTOM is MIN
                bestVal = 1000000;
                Max     = false;
            }
            string opponent = "b";

            if (color == "b")
            {
                opponent = "w";
            }

            List <int> validMoves = b.ValidMovesList(color);
            // Make sure the default value is valid
            int bestMove = 0;

            if (validMoves.Count > 0)
            {
                bestMove = validMoves[0];
            }
            // Loop through all possible moves
            foreach (int move in validMoves)
            {
                Board b1  = b.move(move, color);                      // Make a copy of the move by moving
                int   val = minimaxVal(b1, d - 1, opponent).getVal(); //find its value
                if ((val > bestVal && Max) || (val < bestVal && !Max))
                {                                                     //remember if best
                    bestVal  = val;
                    bestMove = move;
                }
            }

            return(new Result(bestMove, bestVal));
        }
        private Result minimaxVal(Board b, int d, string color)
        {
            // d is depth
            int bestVal = 0;
            if (d == 0)
                return new Result(0, b.evaluate("b"));
            bool Max = true;
            if (color == "b")
            {     //TOP is MAX
                bestVal = -1000000;
            }
            else
            {  // similarly for BOTTOM’s move
                //BOTTOM is MIN
                bestVal = 1000000;
                Max = false;
                }
            string opponent = "b";
            if (color == "b")
            {
                opponent = "w";
            }

            List<int> validMoves = b.ValidMovesList(color);
            // Make sure the default value is valid
            int bestMove = 0;
            if (validMoves.Count > 0)
            {
                bestMove = validMoves[0];
            }
            // Loop through all possible moves
            foreach(int move in validMoves){
                        Board b1 = b.move(move, color); // Make a copy of the move by moving
                        int val = minimaxVal(b1, d - 1, opponent).getVal();   //find its value
                        if ((val > bestVal && Max) || (val < bestVal && !Max))
                        {        //remember if best
                            bestVal = val;
                            bestMove = move;
                        }
                    }

            return new Result(bestMove, bestVal);
        }