// examine every valid move, trying to maximize Player.One's score private int Max(GameBoard board, int depth, int alpha, int beta, ref int move) { // have we recursed far enough? if (depth <= 0) { return Evaluate(board); } for (int index = 0; index < board.ValidMoves.Count; index++) { // report status if we're at top-most recursion depth if (depth == Depth) { m_Status = (double)index / (double)board.ValidMoves.Count; } // copy current board GameBoard board2 = new GameBoard(board); // make move based on ValidMoves[index] board2.MakeMove(index); // get best move that opponent can make int val = Min(board2, depth - 1, alpha, beta, ref move); if (val >= beta) { // already found a better branch than this can possibly be return beta; } if (val > alpha) { // found a better score! alpha = val; if (depth == Depth) { // udpate move if this is top-level move = index; } } } // return best score found return alpha; }
// examine every valid move, trying to maximize Player.One's score private int Max(GameBoard board, int depth, ref int move) { // have we recursed far enough? if (depth <= 0) { return Evaluate(board); } // start with rediculously low score so that first // inspection will result in a match int score = int.MinValue; // for each ValidMove ... for (int index = 0; index < board.ValidMoves.Count; index++) { // report status if we're at top-most recursion depth if (depth == Depth) { m_Status = (double)index / (double)board.ValidMoves.Count; } // copy current board GameBoard board2 = new GameBoard(board); // make move based on ValidMoves[index] board2.MakeMove(index); // get best move that opponent can make int val = Min(board2, depth - 1, ref move); if (val > score) { // found a better score! score = val; if (depth == Depth) { // udpate move if this is top-level move = index; } } } // return best score found return score; }
protected int FindBestMove(GameBoard board, bool isPlayerOne, int score) { // assume first move is the best int move = 0; // scan valid moves, looking for best score for (int index = 0; index < board.ValidMoves.Count; index++) { // create copy of current board to play with GameBoard board2 = new GameBoard(board); // make the next valid move board2.MakeMove(index); // what's the score? int val = Evaluate(board2); // best score for player one is positive, // best score for player two is negative if (isPlayerOne) { if (val > score) { score = val; move = index; } } else { if (val < score) { score = val; move = index; } } } // report findings to the caller return move; }