a few more AI tweaks

This commit is contained in:
BetaSteward 2011-02-26 00:16:04 -05:00
parent bc3acccf2c
commit a81e6da37c
6 changed files with 29 additions and 11 deletions

View file

@ -218,6 +218,7 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
protected int minimaxAB(SimulationNode node, FilterAbility filter, int depth, int alpha, int beta) {
UUID currentPlayerId = node.getGame().getPlayerList().get();
SimulationNode bestChild = null;
boolean isSimulatedPlayer = currentPlayerId.equals(playerId);
for (SimulationNode child: node.getChildren()) {
if (alpha >= beta) {
logger.debug("alpha beta pruning");
@ -228,27 +229,33 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
break;
}
int val = addActions(child, filter, depth-1, alpha, beta);
if (!currentPlayerId.equals(playerId)) {
if (!isSimulatedPlayer) {
if (val < beta) {
beta = val;
bestChild = child;
// if (node.getCombat() == null)
node.setCombat(child.getCombat());
node.setCombat(child.getCombat());
}
if (val == GameStateEvaluator.LOSE_SCORE) {
logger.debug("simulating -- lose, can't do worse than this");
break;
}
}
else {
if (val > alpha) {
alpha = val;
bestChild = child;
// if (node.getCombat() == null)
node.setCombat(child.getCombat());
node.setCombat(child.getCombat());
}
if (val == GameStateEvaluator.WIN_SCORE) {
logger.debug("simulating -- win, can't do better than this");
break;
}
}
}
node.children.clear();
if (bestChild != null)
node.children.add(bestChild);
if (!currentPlayerId.equals(playerId)) {
if (!isSimulatedPlayer) {
logger.debug("returning minimax beta: " + beta);
return beta;
}
@ -380,6 +387,7 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
}
node.setGameValue(game.getState().getValue());
SimulatedPlayer currentPlayer = (SimulatedPlayer) game.getPlayer(game.getPlayerList().get());
boolean isSimulatedPlayer = currentPlayer.getId().equals(playerId);
logger.debug("simulating -- player " + currentPlayer.getName());
SimulationNode bestNode = null;
List<Ability> allActions = currentPlayer.simulatePriority(game, filter);
@ -401,12 +409,16 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
logger.debug("simulating -- node #:" + SimulationNode.getCount() + " actions:" + action);
sim.checkStateAndTriggered();
int val = addActions(newNode, filter, depth-1, alpha, beta);
if (!currentPlayer.getId().equals(playerId)) {
if (!isSimulatedPlayer) {
if (val < beta) {
beta = val;
bestNode = newNode;
node.setCombat(newNode.getCombat());
}
if (val == GameStateEvaluator.LOSE_SCORE) {
logger.debug("simulating -- lose, can't do worse than this");
break;
}
}
else {
if (val > alpha) {
@ -418,6 +430,10 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
if (node.getChoices().size() > 0)
choices = node.getChoices();
}
if (val == GameStateEvaluator.WIN_SCORE) {
logger.debug("simulating -- win, can't do better than this");
break;
}
}
if (alpha >= beta) {
logger.debug("simulating -- pruning");
@ -433,7 +449,7 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
node.children.clear();
node.children.add(bestNode);
}
if (!currentPlayer.getId().equals(playerId)) {
if (!isSimulatedPlayer) {
logger.debug("returning priority beta: " + beta);
return beta;
}

View file

@ -298,7 +298,6 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
}
else if (!counter) {
finishCombat(game);
// val = GameStateEvaluator.evaluate(playerId, game);
val = simulateCounterAttack(game, node, depth, alpha, beta);
}
}

View file

@ -59,14 +59,17 @@ public class GameStateEvaluator {
private static final int CREATURE_FACTOR = Config.evaluatorCreatureFactor;
private static final int HAND_FACTOR = Config.evaluatorHandFactor;
public static final int WIN_SCORE = Integer.MAX_VALUE - 1;
public static final int LOSE_SCORE = Integer.MIN_VALUE + 1;
public static int evaluate(UUID playerId, Game game) {
Player player = game.getPlayer(playerId);
Player opponent = game.getPlayer(game.getOpponents(playerId).iterator().next());
if (game.isGameOver()) {
if (player.hasLost() || opponent.hasWon())
return Integer.MIN_VALUE + 1;
return LOSE_SCORE;
if (opponent.hasLost() || player.hasWon())
return Integer.MAX_VALUE - 1;
return WIN_SCORE;
}
int lifeScore = (player.getLife() - opponent.getLife()) * LIFE_FACTOR;
int permanentScore = 0;