// We assume Winner field is updated and correct => No need to recalculate winner int Evaluate(GameBoardState boardState, PlayerTurn maxTurn, int currentDepth, int maxDepth) { bool maximizing = boardState.CurrentTurn == maxTurn; if (currentDepth == maxDepth || boardState.Winner != null) { return(EvaluateHeuristic(boardState, maxTurn)); } List <GameAction> possibleActions = GameLogicUtils.GetAllAvailableActions(boardState); int evaluation; if (maximizing) { int maxEvaluation = int.MinValue; foreach (GameAction action in possibleActions) { GameBoardState afterMove = gameLogic.ApplyAction(boardState, action); int afterMoveEvaluation = Evaluate(afterMove, maxTurn, currentDepth + 1, maxDepth); if (afterMoveEvaluation > maxEvaluation) { maxEvaluation = afterMoveEvaluation; } } evaluation = maxEvaluation; } else { int minEvaluation = int.MaxValue; foreach (GameAction action in possibleActions) { GameBoardState afterMove = gameLogic.ApplyAction(boardState, action); int afterMoveEvaluation = Evaluate(afterMove, maxTurn, currentDepth + 1, maxDepth); if (afterMoveEvaluation < minEvaluation) { minEvaluation = afterMoveEvaluation; } } evaluation = minEvaluation; } return(evaluation); }
public GameAction CalculateComputerMove(GameBoardState gameState, GameDifficulty difficulty) { int maxDepth = ToRecursiveDepth(difficulty); PlayerTurn maxTurn = gameState.CurrentTurn; List <GameAction> possibleActions = GameLogicUtils.GetAllAvailableActions(gameState); int maxEvaluation = int.MinValue; GameAction bestMove = null; foreach (GameAction action in possibleActions) { GameBoardState afterMove = gameLogic.ApplyAction(gameState, action); int afterMoveEvaluation = Evaluate(afterMove, maxTurn, 0, maxDepth); if (afterMoveEvaluation > maxEvaluation || (afterMoveEvaluation >= maxEvaluation && bestMove == null)) { maxEvaluation = afterMoveEvaluation; bestMove = action; } } return(bestMove); }
public GameAction CalculateComputerMove(GameBoardState gameState, GameDifficulty difficulty) { List <GameAction> actions = GameLogicUtils.GetAllAvailableActions(gameState); return(actions[rand.Next(0, actions.Count)]); }