boolean stepFinished = false;
int val;
if (logger.isTraceEnabled() && node !=null && node.getAbilities() != null && !node.getAbilities().toString().equals("[Pass]")){
logger.trace("Add Action [" + depth + "] " + node.getAbilities().toString() + " a: " + alpha + " b: " + beta);
}
Game game = node.getGame();
if (Thread.interrupted()) {
Thread.currentThread().interrupt();
logger.debug("interrupted");
return GameStateEvaluator2.evaluate(playerId, game);
}
// Condition to stop deeper simulation
if (depth <= 0 || SimulationNode2.nodeCount > maxNodes || game.gameOver(null)) {
val = GameStateEvaluator2.evaluate(playerId, game);
if (logger.isTraceEnabled()) {
StringBuilder sb = new StringBuilder("Add Actions -- reached end state <").append(val).append(">");
SimulationNode2 logNode = node;
do {
sb.append(new StringBuilder(" <- ["+logNode.getDepth()+"]" + (logNode.getAbilities() != null ? logNode.getAbilities().toString():"[empty]")));
logNode = logNode.getParent();
} while((logNode.getParent() != null));
logger.trace(sb);
}
} else if (node.getChildren().size() > 0) {
if (logger.isDebugEnabled()) {
StringBuilder sb = new StringBuilder("Add Action [").append(depth)
.append("] -- something added children ")
.append(node.getAbilities() != null ? node.getAbilities().toString():"null")
.append(" added children: ").append(node.getChildren().size()).append(" (");
for (SimulationNode2 logNode: node.getChildren()) {
sb.append(logNode.getAbilities() != null ? logNode.getAbilities().toString():"null").append(", ");
}
sb.append(")");
logger.debug(sb);
}
val = minimaxAB(node, depth-1, alpha, beta);
}
else {
logger.trace("Add Action -- alpha: " + alpha + " beta: " + beta + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(game.getPlayerList().get()).getName());
if (allPassed(game)) {
if (!game.getStack().isEmpty()) {
resolve(node, depth, game);
}
else {
stepFinished = true;
}
}
if (game.gameOver(null)) {
val = GameStateEvaluator2.evaluate(playerId, game);
}
else if (stepFinished) {
logger.debug("Step finished");
int testScore = GameStateEvaluator2.evaluate(playerId, game);
if (game.getActivePlayerId().equals(playerId)) {
if (testScore < currentScore) {
// if score at end of step is worse than original score don't check further
//logger.debug("Add Action -- abandoning check, no immediate benefit");
val = testScore;
}
else {
/*switch (game.getTurn().getStepType()) {
case PRECOMBAT_MAIN:
val = simulateCombat(game, node, depth-1, alpha, beta, false);
break;
case POSTCOMBAT_MAIN:
val = simulateCounterAttack(game, node, depth-1, alpha, beta);
break;
default:
val = GameStateEvaluator2.evaluate(playerId, game);
break;
}*/
val = GameStateEvaluator2.evaluate(playerId, game);
}
}
else {
val = GameStateEvaluator2.evaluate(playerId, game);
/*if (game.getTurn().getStepType() == PhaseStep.DECLARE_ATTACKERS)
val = simulateBlockers(game, node, playerId, depth-1, alpha, beta, true);
else
val = GameStateEvaluator2.evaluate(playerId, game);
*/
}
}
else if (node.getChildren().size() > 0) {
if (logger.isDebugEnabled()) {
StringBuilder sb = new StringBuilder("Add Action [").append(depth)
.append("] -- trigger ")
.append(node.getAbilities() != null ? node.getAbilities().toString():"null")
.append(" added children: ").append(node.getChildren().size()).append(" (");
for (SimulationNode2 logNode: node.getChildren()) {
sb.append(logNode.getAbilities() != null ? logNode.getAbilities().toString():"null").append(", ");
}
sb.append(")");
logger.debug(sb);
}
val = minimaxAB(node, depth, alpha, beta);
}
else {
val = simulatePriority(node, game, depth, alpha, beta);
}
}
node.setScore(val); // test
logger.trace("returning -- score: " + val + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(node.getPlayerId()).getName());
return val;
}