private Object minimax(GameTree <Object> node, int depth, bool maximizingPlayer, IGame game) { if (depth == 0 || node.IsTerminal()) { return(node.getData()); } if (maximizingPlayer) { int bestValue = -10000; Object bestState = (Object) new Object(); foreach (GameTree <Object> item in node) { Object val = minimax(item, depth - 1, false, game); //bestValue = Math.Max(bestValue, game.Evaluate(val)); int tmp = game.Evaluate(val); if (bestValue < tmp) { bestValue = tmp; bestState = item.getData(); } } return(bestState); } else { int bestValue = 10000; Object bestState = (Object) new Object(); foreach (GameTree <Object> item in node) { Object val = minimax(item, depth - 1, true, game); //bestValue = Math.Min(bestValue, val); int tmp = game.Evaluate(val); if (bestValue > tmp) { bestValue = tmp; bestState = item.getData(); } } return(bestState); } }
private Object alphaBeta(GameTree <Object> node, int depth, int alpha, int beta, bool maximizingPlayer, IGame game) { if (depth == 0 || node.IsTerminal()) { return(node.getData()); } if (maximizingPlayer) { int v = -10000; Object bestState = (Object) new Object(); foreach (GameTree <Object> child in node) { Object tmp = alphaBeta(child, depth - 1, alpha, beta, false, game); //v = Math.Max(game.Evaluate(bestState), game.Evaluate(tmp)); int t = game.Evaluate(tmp); if (v < t) { v = t; bestState = child.getData(); } //alpha = Math.Max(alpha, v); if (alpha < v) { alpha = v; //bestState = child.getData(); } if (beta < alpha) { break; } } return(bestState); } else { int v = 1000; Object bestState = (Object) new Object(); foreach (GameTree <Object> child in node) { Object tmp = alphaBeta(child, depth - 1, alpha, beta, false, game); int t = game.Evaluate(tmp); //v = Math.Min(v, alphaBeta(child, depth - 1, alpha, beta, true, game)); if (v > t) { v = t; bestState = child.getData(); } //beta = Math.Min(beta, v); if (beta > v) { beta = v; //bestState = child.getData(); } if (beta <= alpha) { break; } } return(bestState); } }