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);
        }
 public int chooseMove(Board board)
 {
     return minimaxVal(board, 4, Color).getMove();
 }
Esempio n. 3
0
 public Board move(int move, String player)
 {
     Board newBoard = new Board() { height = this.height, squares = this.squares, width = this.width };
     int x = move % 8;
     int y = move / 8;
     newBoard.flip(x, y);
     for (int i = -1; i < 2; i++)
     {
         for (int j = -1; j < 2; j++)
         {
             if (!(i == j && i == 0))
             {
                 newBoard.flipRecursive(x, y, i, j, player);
             }
         }
     }
         return newBoard;
 }