public override CheckersMove NextMove(CheckersGame game) { const int minValue = int.MaxValue; int maxValue = int.MinValue; CheckersMove bestMove = null; // Enumerate all moves foreach (CheckersMove move in game.EnumLegalMoves()) { if (!game.IsPlaying) { break; } CheckersGame nextGameState = game.Clone(); nextGameState.MovePiece(move.Clone(nextGameState)); int curValue = minMove(game, nextGameState, 1, maxValue, minValue); if ((curValue > maxValue) || (bestMove == null)) { maxValue = curValue; bestMove = move; } OnTick(game); } return(bestMove); }
public override CheckersMove NextMove(CheckersGame game) { const int minValue = int.MaxValue; int maxValue = int.MinValue; CheckersMove bestMove = null; // Enumerate all moves foreach(CheckersMove move in game.EnumLegalMoves()) { if(!game.IsPlaying) break; CheckersGame nextGameState = game.Clone(); nextGameState.MovePiece(move.Clone(nextGameState)); int curValue = minMove(game, nextGameState, 1, maxValue, minValue); if((curValue > maxValue) || (bestMove == null)) { maxValue = curValue; bestMove = move; } OnTick(game); } return bestMove; }
int maxMove(CheckersGame initGame, CheckersGame curGame, int depth, int alpha, int beta) { // Check algorithm limits..end prematurely, but with an educated approximation if (doCutOff(initGame, curGame, depth)) { return(doCalculateStrength(initGame, curGame)); } // Make move with all possibilities foreach (CheckersMove move in curGame.EnumLegalMoves()) { // Create next move CheckersGame nextGameState = move.Game.Clone(); CheckersMove nextMoveState = move.Clone(nextGameState); // Make next move and search move space if (!nextGameState.MovePiece(nextMoveState)) { continue; } int value = minMove(initGame, nextGameState, depth + 1, alpha, beta); if (value > alpha) { // Get new max value alpha = value; } if (alpha > beta) { // Return max value with pruning return(beta); } } // Return alpha (max value) return(alpha); }
int minMove(CheckersGame initGame, CheckersGame curGame, int depth, int alpha, int beta) { // Check algorithm limits..end prematurely, but with an educated approximation if(doCutOff(initGame, curGame, depth)) return -doCalculateStrength(initGame, curGame); // Make move with all possibilities foreach(CheckersMove move in curGame.EnumLegalMoves()) { // Create next move CheckersGame nextGameState = move.Game.Clone(); CheckersMove nextMoveState = move.Clone(nextGameState); // Make next move and search move space if(!nextGameState.MovePiece(nextMoveState)) continue; int value = maxMove(initGame, nextGameState, depth + 1, alpha, beta); if(value < beta) { // Get new min value beta = value; } if(beta < alpha) { // Return min value with pruning return alpha; } } // Return alpha (max value) return beta; }