public static double alphabetarate(Game root, int depth, double alpha, double beta, bool player) { if (depth == 0) { return(root.calcRating()); } //Max phase if (player) { List <StateTransition> moves = root.getAllMoveStates(); if (moves.Count == 0) { return(-100); } StateTransition selectedMove = moves[0]; foreach (StateTransition move in moves) { alpha = Math.Max(alpha, Game.alphabetarate(move.nextState, depth - 1, alpha, beta, !player)); if (beta <= alpha) { break; } } return(alpha); } //Min phase else { List <Game> rnd = root.getAllRandom(); foreach (Game g in rnd) { beta = Math.Min(beta, Game.alphabetarate(g, depth - 1, alpha, beta, !player)); if (beta <= alpha) { break; } } return(beta); } }
public static double expectimax(Game root, int depth, bool player) { double curRating; if (depth == 0) { return(root.calcRating()); } //Max phase if (player) { double maxRating = 0; List <StateTransition> moves = root.getAllMoveStates(); if (moves.Count == 0) { return(-100); } StateTransition selectedMove = moves[0]; foreach (StateTransition move in moves) { move.rating = Game.expectimax(move.nextState, depth - 1, !player); maxRating = Math.Max(maxRating, move.rating); } return(maxRating); } //Min phase else { double minRating = 10000; double probability; List <Game> rnd = root.getAllRandom(); foreach (Game g in rnd) { probability = (rnd.IndexOf(g) % 2 == 0) ? 0.9F : 0.1F; curRating = probability * Game.expectimax(g, depth - 1, !player); minRating = Math.Min(minRating, curRating); } return(minRating); } }
void AI_Step() { List <StateTransition> moves = oGame.getAllMoveStates(); if (moves.Count == 0) { oGame.gameOver = true; return; } StateTransition selectedMove = moves[0]; //int depth = cboDepth.SelectedIndex; foreach (StateTransition move in moves) { switch (currentAlg) { case Algorithm.MiniMax: move.rating = Game.minimax(move.nextState, depth, false); break; case Algorithm.AlphaBeta: move.rating = Game.alphabetarate(move.nextState, depth, float.MinValue, float.MaxValue, false); break; case Algorithm.ExpectiMax: move.rating = Game.expectimax(move.nextState, depth, false); break; } if (selectedMove.rating < move.rating) { selectedMove = move; } } oGame.moveBoard(selectedMove.dir); UpdateGame(); }