mirror of
https://github.com/correl/mage.git
synced 2024-12-24 11:50:45 +00:00
more AI fixes
This commit is contained in:
parent
0bde24648b
commit
6982608043
8 changed files with 198 additions and 135 deletions
|
@ -29,6 +29,7 @@
|
||||||
package mage.player.ai;
|
package mage.player.ai;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
@ -39,6 +40,7 @@ import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.FutureTask;
|
import java.util.concurrent.FutureTask;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.logging.Level;
|
||||||
import mage.Constants.Outcome;
|
import mage.Constants.Outcome;
|
||||||
import mage.Constants.PhaseStep;
|
import mage.Constants.PhaseStep;
|
||||||
import mage.Constants.RangeOfInfluence;
|
import mage.Constants.RangeOfInfluence;
|
||||||
|
@ -182,14 +184,18 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
if (!getNextAction(game)) {
|
if (!getNextAction(game)) {
|
||||||
Game sim = createSimulation(game);
|
Game sim = createSimulation(game);
|
||||||
SimulationNode.resetCount();
|
SimulationNode.resetCount();
|
||||||
root = new SimulationNode(null, sim, maxDepth, playerId);
|
root = new SimulationNode(null, sim, playerId);
|
||||||
logger.debug("simulating actions");
|
logger.debug("simulating actions");
|
||||||
addActionsTimed(new FilterAbility());
|
addActionsTimed(new FilterAbility());
|
||||||
if (root.children.size() > 0) {
|
if (root.children.size() > 0) {
|
||||||
root = root.children.get(0);
|
root = root.children.get(0);
|
||||||
actions = new LinkedList<Ability>(root.abilities);
|
actions = new LinkedList<Ability>(root.abilities);
|
||||||
combat = root.combat;
|
combat = root.combat;
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("adding actions:" + actions);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
logger.debug("no actions added");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,20 +221,20 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int minimaxAB(SimulationNode node, FilterAbility filter, int depth, int alpha, int beta) {
|
protected int minimaxAB(SimulationNode node, FilterAbility filter, int alpha, int beta) {
|
||||||
UUID currentPlayerId = node.getGame().getPlayerList().get();
|
UUID currentPlayerId = node.getGame().getPlayerList().get();
|
||||||
SimulationNode bestChild = null;
|
SimulationNode bestChild = null;
|
||||||
boolean isSimulatedPlayer = currentPlayerId.equals(playerId);
|
boolean isSimulatedPlayer = currentPlayerId.equals(playerId);
|
||||||
for (SimulationNode child: node.getChildren()) {
|
for (SimulationNode child: node.getChildren()) {
|
||||||
if (alpha >= beta) {
|
if (alpha >= beta) {
|
||||||
logger.debug("alpha beta pruning");
|
logger.debug(indent(node.depth) + "alpha beta pruning");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (SimulationNode.nodeCount > maxNodes) {
|
// if (SimulationNode.nodeCount > maxNodes) {
|
||||||
logger.debug("simulating -- reached end-state");
|
// logger.debug(indent(node.depth) + "simulating -- reached end-state");
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
int val = addActions(child, filter, depth-1, alpha, beta);
|
int val = addActions(child, filter, alpha, beta);
|
||||||
if (!isSimulatedPlayer) {
|
if (!isSimulatedPlayer) {
|
||||||
if (val < beta) {
|
if (val < beta) {
|
||||||
beta = val;
|
beta = val;
|
||||||
|
@ -236,7 +242,7 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
node.setCombat(child.getCombat());
|
node.setCombat(child.getCombat());
|
||||||
}
|
}
|
||||||
if (val == GameStateEvaluator.LOSE_SCORE) {
|
if (val == GameStateEvaluator.LOSE_SCORE) {
|
||||||
logger.debug("simulating -- lose, can't do worse than this");
|
logger.debug(indent(node.depth) + "simulating -- lose, can't do worse than this");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -247,7 +253,7 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
node.setCombat(child.getCombat());
|
node.setCombat(child.getCombat());
|
||||||
}
|
}
|
||||||
if (val == GameStateEvaluator.WIN_SCORE) {
|
if (val == GameStateEvaluator.WIN_SCORE) {
|
||||||
logger.debug("simulating -- win, can't do better than this");
|
logger.debug(indent(node.depth) + "simulating -- win, can't do better than this");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -256,11 +262,11 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
if (bestChild != null)
|
if (bestChild != null)
|
||||||
node.children.add(bestChild);
|
node.children.add(bestChild);
|
||||||
if (!isSimulatedPlayer) {
|
if (!isSimulatedPlayer) {
|
||||||
logger.debug("returning minimax beta: " + beta);
|
logger.debug(indent(node.depth) + "returning minimax beta: " + beta);
|
||||||
return beta;
|
return beta;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
logger.debug("returning minimax alpha: " + alpha);
|
logger.debug(indent(node.depth) + "returning minimax alpha: " + alpha);
|
||||||
return alpha;
|
return alpha;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -274,7 +280,7 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void resolve(SimulationNode node, int depth, Game game) {
|
protected void resolve(SimulationNode node, Game game) {
|
||||||
StackObject ability = game.getStack().pop();
|
StackObject ability = game.getStack().pop();
|
||||||
if (ability instanceof StackAbility) {
|
if (ability instanceof StackAbility) {
|
||||||
SearchEffect effect = getSearchEffect((StackAbility) ability);
|
SearchEffect effect = getSearchEffect((StackAbility) ability);
|
||||||
|
@ -287,16 +293,16 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
SearchEffect newEffect = getSearchEffect((StackAbility) newAbility);
|
SearchEffect newEffect = getSearchEffect((StackAbility) newAbility);
|
||||||
newEffect.getTarget().addTarget(targetId, newAbility, sim);
|
newEffect.getTarget().addTarget(targetId, newAbility, sim);
|
||||||
sim.getStack().push(newAbility);
|
sim.getStack().push(newAbility);
|
||||||
SimulationNode newNode = new SimulationNode(node, sim, depth, ability.getControllerId());
|
SimulationNode newNode = new SimulationNode(node, sim, ability.getControllerId());
|
||||||
node.children.add(newNode);
|
node.children.add(newNode);
|
||||||
newNode.getTargets().add(targetId);
|
newNode.getTargets().add(targetId);
|
||||||
logger.debug("simulating search -- node#: " + SimulationNode.getCount() + "for player: " + sim.getPlayer(ability.getControllerId()).getName());
|
logger.debug(indent(node.depth) + "simulating search -- node#: " + SimulationNode.getCount() + "for player: " + sim.getPlayer(ability.getControllerId()).getName());
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.debug("simulating resolve ");
|
logger.debug(indent(node.depth) + "simulating resolve ");
|
||||||
ability.resolve(game);
|
ability.resolve(game);
|
||||||
game.applyEffects();
|
game.applyEffects();
|
||||||
game.getPlayers().resetPassed();
|
game.getPlayers().resetPassed();
|
||||||
|
@ -307,7 +313,7 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
|
FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
|
||||||
public Integer call() throws Exception
|
public Integer call() throws Exception
|
||||||
{
|
{
|
||||||
return addActions(root, filter, maxDepth, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
return addActions(root, filter, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
pool.execute(task);
|
pool.execute(task);
|
||||||
|
@ -316,6 +322,12 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
logger.debug("simulating - timed out");
|
logger.debug("simulating - timed out");
|
||||||
task.cancel(true);
|
task.cancel(true);
|
||||||
|
// sleep for 1 second to allow cleanup to finish
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
logger.fatal("can't sleep");
|
||||||
|
}
|
||||||
} catch (ExecutionException e) {
|
} catch (ExecutionException e) {
|
||||||
logger.fatal("Simulation error", e);
|
logger.fatal("Simulation error", e);
|
||||||
task.cancel(true);
|
task.cancel(true);
|
||||||
|
@ -325,28 +337,28 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int addActions(SimulationNode node, FilterAbility filter, int depth, int alpha, int beta) {
|
protected int addActions(SimulationNode node, FilterAbility filter, int alpha, int beta) {
|
||||||
Game game = node.getGame();
|
Game game = node.getGame();
|
||||||
int val;
|
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
logger.debug("interrupted");
|
logger.debug(indent(node.depth) + "interrupted");
|
||||||
return GameStateEvaluator.evaluate(playerId, game);
|
return GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
if (depth <= 0 || SimulationNode.nodeCount > maxNodes || game.isGameOver()) {
|
int val;
|
||||||
logger.debug("simulating -- reached end state");
|
if (node.depth > maxDepth || game.isGameOver()) {
|
||||||
|
logger.debug(indent(node.depth) + "simulating -- reached end state");
|
||||||
val = GameStateEvaluator.evaluate(playerId, game);
|
val = GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
else if (node.getChildren().size() > 0) {
|
else if (node.getChildren().size() > 0) {
|
||||||
logger.debug("simulating -- somthing added children:" + node.getChildren().size());
|
logger.debug(indent(node.depth) + "simulating -- somthing added children:" + node.getChildren().size());
|
||||||
val = minimaxAB(node, filter, depth-1, alpha, beta);
|
val = minimaxAB(node, filter, alpha, beta);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("simulating -- alpha: " + alpha + " beta: " + beta + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + (node.getPlayerId().equals(playerId)?"yes":"no"));
|
logger.debug(indent(node.depth) + "simulating -- alpha: " + alpha + " beta: " + beta + " depth:" + node.depth + " step:" + game.getTurn().getStepType() + " for player:" + (node.getPlayerId().equals(playerId)?"yes":"no"));
|
||||||
if (allPassed(game)) {
|
if (allPassed(game)) {
|
||||||
if (!game.getStack().isEmpty()) {
|
if (!game.getStack().isEmpty()) {
|
||||||
resolve(node, depth, game);
|
resolve(node, game);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// int testScore = GameStateEvaluator.evaluate(playerId, game);
|
// int testScore = GameStateEvaluator.evaluate(playerId, game);
|
||||||
|
@ -365,55 +377,57 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
}
|
}
|
||||||
else if (node.getChildren().size() > 0) {
|
else if (node.getChildren().size() > 0) {
|
||||||
//declared attackers or blockers or triggered abilities
|
//declared attackers or blockers or triggered abilities
|
||||||
logger.debug("simulating -- attack/block/trigger added children:" + node.getChildren().size());
|
logger.debug(indent(node.depth) + "simulating -- attack/block/trigger added children:" + node.getChildren().size());
|
||||||
val = minimaxAB(node, filter, depth-1, alpha, beta);
|
val = minimaxAB(node, filter, alpha, beta);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
val = simulatePriority(node, game, filter, depth, alpha, beta);
|
val = simulatePriority(node, game, filter, alpha, beta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("returning -- score: " + val + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
logger.debug(indent(node.depth) + "returning -- score: " + val + " depth:" + node.depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||||
return val;
|
return val;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int simulatePriority(SimulationNode node, Game game, FilterAbility filter, int depth, int alpha, int beta) {
|
protected int simulatePriority(SimulationNode node, Game game, FilterAbility filter, int alpha, int beta) {
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
logger.debug("interrupted");
|
logger.debug(indent(node.depth) + "interrupted");
|
||||||
return GameStateEvaluator.evaluate(playerId, game);
|
return GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
node.setGameValue(game.getState().getValue());
|
node.setGameValue(game.getState().getValue());
|
||||||
SimulatedPlayer currentPlayer = (SimulatedPlayer) game.getPlayer(game.getPlayerList().get());
|
SimulatedPlayer currentPlayer = (SimulatedPlayer) game.getPlayer(game.getPlayerList().get());
|
||||||
boolean isSimulatedPlayer = currentPlayer.getId().equals(playerId);
|
boolean isSimulatedPlayer = currentPlayer.getId().equals(playerId);
|
||||||
logger.debug("simulating -- player " + currentPlayer.getName());
|
logger.debug(indent(node.depth) + "simulating -- player " + currentPlayer.getName());
|
||||||
SimulationNode bestNode = null;
|
SimulationNode bestNode = null;
|
||||||
List<Ability> allActions = currentPlayer.simulatePriority(game, filter);
|
List<Ability> allActions = currentPlayer.simulatePriority(game, filter);
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("simulating -- adding " + allActions.size() + " children:" + allActions);
|
logger.debug(indent(node.depth) + "simulating -- adding " + allActions.size() + " children:" + allActions);
|
||||||
for (Ability action: allActions) {
|
for (Ability action: allActions) {
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
logger.debug("interrupted");
|
logger.debug(indent(node.depth) + "interrupted");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Game sim = game.copy();
|
Game sim = game.copy();
|
||||||
if (sim.getPlayer(currentPlayer.getId()).activateAbility((ActivatedAbility) action.copy(), sim)) {
|
if (sim.getPlayer(currentPlayer.getId()).activateAbility((ActivatedAbility) action.copy(), sim)) {
|
||||||
sim.applyEffects();
|
sim.applyEffects();
|
||||||
if (checkForRepeatedAction(sim, node, action, currentPlayer.getId()))
|
if (checkForUselessAction(sim, node, action, currentPlayer.getId())) {
|
||||||
|
logger.debug(indent(node.depth) + "found useless action: " + action);
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
if (!sim.isGameOver() && action.isUsesStack()) {
|
if (!sim.isGameOver() && action.isUsesStack()) {
|
||||||
// only pass if the last action uses the stack
|
// only pass if the last action uses the stack
|
||||||
sim.getPlayer(currentPlayer.getId()).pass();
|
sim.getPlayer(currentPlayer.getId()).pass();
|
||||||
sim.getPlayerList().getNext();
|
sim.getPlayerList().getNext();
|
||||||
}
|
}
|
||||||
SimulationNode newNode = new SimulationNode(node, sim, action, depth, currentPlayer.getId());
|
SimulationNode newNode = new SimulationNode(node, sim, action, currentPlayer.getId());
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("simulating -- node #:" + SimulationNode.getCount() + " actions:" + action);
|
logger.debug(indent(node.depth) + "simulating -- node #:" + SimulationNode.getCount() + " actions:" + action);
|
||||||
sim.checkStateAndTriggered();
|
sim.checkStateAndTriggered();
|
||||||
int val = addActions(newNode, filter, depth-1, alpha, beta);
|
int val = addActions(newNode, filter, alpha, beta);
|
||||||
if (!isSimulatedPlayer) {
|
if (!isSimulatedPlayer) {
|
||||||
if (val < beta) {
|
if (val < beta) {
|
||||||
beta = val;
|
beta = val;
|
||||||
|
@ -421,7 +435,7 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
node.setCombat(newNode.getCombat());
|
node.setCombat(newNode.getCombat());
|
||||||
}
|
}
|
||||||
if (val == GameStateEvaluator.LOSE_SCORE) {
|
if (val == GameStateEvaluator.LOSE_SCORE) {
|
||||||
logger.debug("simulating -- lose, can't do worse than this");
|
logger.debug(indent(node.depth) + "simulating -- lose, can't do worse than this");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -436,18 +450,18 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
choices = node.getChoices();
|
choices = node.getChoices();
|
||||||
}
|
}
|
||||||
if (val == GameStateEvaluator.WIN_SCORE) {
|
if (val == GameStateEvaluator.WIN_SCORE) {
|
||||||
logger.debug("simulating -- win, can't do better than this");
|
logger.debug(indent(node.depth) + "simulating -- win, can't do better than this");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (alpha >= beta) {
|
if (alpha >= beta) {
|
||||||
logger.debug("simulating -- pruning");
|
logger.debug(indent(node.depth) + "simulating -- pruning");
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (SimulationNode.nodeCount > maxNodes) {
|
|
||||||
logger.debug("simulating -- reached end-state");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// if (SimulationNode.nodeCount > maxNodes) {
|
||||||
|
// logger.debug(indent(node.depth) + "simulating -- reached end-state");
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (bestNode != null) {
|
if (bestNode != null) {
|
||||||
|
@ -455,11 +469,11 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
node.children.add(bestNode);
|
node.children.add(bestNode);
|
||||||
}
|
}
|
||||||
if (!isSimulatedPlayer) {
|
if (!isSimulatedPlayer) {
|
||||||
logger.debug("returning priority beta: " + beta);
|
logger.debug(indent(node.depth) + "returning priority beta: " + beta);
|
||||||
return beta;
|
return beta;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
logger.debug("returning priority alpha: " + alpha);
|
logger.debug(indent(node.depth) + "returning priority alpha: " + alpha);
|
||||||
return alpha;
|
return alpha;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -606,8 +620,8 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, playerId, playerId));
|
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, playerId, playerId));
|
||||||
SimulationNode newNode = new SimulationNode(node, sim, node.getDepth()-1, activePlayerId);
|
SimulationNode newNode = new SimulationNode(node, sim, activePlayerId);
|
||||||
logger.debug("simulating -- node #:" + SimulationNode.getCount() + " declare attakers");
|
logger.debug(indent(node.depth) + "simulating -- node #:" + SimulationNode.getCount() + " declare attakers");
|
||||||
newNode.setCombat(sim.getCombat());
|
newNode.setCombat(sim.getCombat());
|
||||||
node.children.add(newNode);
|
node.children.add(newNode);
|
||||||
}
|
}
|
||||||
|
@ -627,8 +641,8 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
|
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
|
||||||
SimulationNode newNode = new SimulationNode(node, sim, node.getDepth()-1, defenderId);
|
SimulationNode newNode = new SimulationNode(node, sim, defenderId);
|
||||||
logger.debug("simulating -- node #:" + SimulationNode.getCount() + " declare blockers");
|
logger.debug(indent(node.depth) + "simulating -- node #:" + SimulationNode.getCount() + " declare blockers");
|
||||||
newNode.setCombat(sim.getCombat());
|
newNode.setCombat(sim.getCombat());
|
||||||
node.children.add(newNode);
|
node.children.add(newNode);
|
||||||
}
|
}
|
||||||
|
@ -698,22 +712,43 @@ public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements
|
||||||
return sim;
|
return sim;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkForRepeatedAction(Game sim, SimulationNode node, Ability action, UUID playerId) {
|
/**
|
||||||
|
* resolve current ability on the stack if there is one, then
|
||||||
|
* check if current game state is the same as the previous, if so then
|
||||||
|
* action has no effect and is not useful
|
||||||
|
*
|
||||||
|
* @param sim
|
||||||
|
* @param node
|
||||||
|
* @param action
|
||||||
|
* @param playerId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean checkForUselessAction(Game sim, SimulationNode node, Ability action, UUID playerId) {
|
||||||
|
int currentVal = 0;
|
||||||
|
int prevVal = 0;
|
||||||
if (action instanceof PassAbility)
|
if (action instanceof PassAbility)
|
||||||
return false;
|
return false;
|
||||||
int val = GameStateEvaluator.evaluate(playerId, sim);
|
|
||||||
SimulationNode test = node.getParent();
|
SimulationNode test = node.getParent();
|
||||||
while (test != null && !test.getPlayerId().equals(playerId)) {
|
if (test == null)
|
||||||
test = test.getParent();
|
return false;
|
||||||
|
if (action.isUsesStack()) {
|
||||||
|
Game testSim = sim.copy();
|
||||||
|
StackObject ability = testSim.getStack().pop();
|
||||||
|
ability.resolve(testSim);
|
||||||
|
testSim.applyEffects();
|
||||||
|
currentVal = GameStateEvaluator.evaluate(playerId, testSim, true);
|
||||||
}
|
}
|
||||||
if (test != null && test.getAbilities() != null && test.getAbilities().size() == 1) {
|
else {
|
||||||
if (action.toString().equals(test.getAbilities().get(0).toString()) && GameStateEvaluator.evaluate(playerId, sim) == val) {
|
currentVal = GameStateEvaluator.evaluate(playerId, sim, true);
|
||||||
if (logger.isDebugEnabled())
|
|
||||||
logger.debug("found repeated action " + action);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
prevVal = GameStateEvaluator.evaluate(playerId, test.getGame(), true);
|
||||||
|
return currentVal == prevVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String indent(int num) {
|
||||||
|
char[] fill = new char[num];
|
||||||
|
Arrays.fill(fill, ' ');
|
||||||
|
return Integer.toString(num) + new String(fill);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,7 +156,7 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
currentScore = GameStateEvaluator.evaluate(playerId, game);
|
currentScore = GameStateEvaluator.evaluate(playerId, game);
|
||||||
Game sim = createSimulation(game);
|
Game sim = createSimulation(game);
|
||||||
SimulationNode.resetCount();
|
SimulationNode.resetCount();
|
||||||
root = new SimulationNode(null, sim, maxDepth, playerId);
|
root = new SimulationNode(null, sim, playerId);
|
||||||
logger.debug("simulating pre combat actions -----------------------------------------------------------------------------------------");
|
logger.debug("simulating pre combat actions -----------------------------------------------------------------------------------------");
|
||||||
|
|
||||||
addActionsTimed(new FilterAbility());
|
addActionsTimed(new FilterAbility());
|
||||||
|
@ -165,7 +165,11 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
root = root.children.get(0);
|
root = root.children.get(0);
|
||||||
actions = new LinkedList<Ability>(root.abilities);
|
actions = new LinkedList<Ability>(root.abilities);
|
||||||
combat = root.combat;
|
combat = root.combat;
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("adding pre-combat actions:" + actions);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
logger.debug("no pre-combat actions added");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +178,7 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
currentScore = GameStateEvaluator.evaluate(playerId, game);
|
currentScore = GameStateEvaluator.evaluate(playerId, game);
|
||||||
Game sim = createSimulation(game);
|
Game sim = createSimulation(game);
|
||||||
SimulationNode.resetCount();
|
SimulationNode.resetCount();
|
||||||
root = new SimulationNode(null, sim, maxDepth, playerId);
|
root = new SimulationNode(null, sim, playerId);
|
||||||
logger.debug("simulating post combat actions ----------------------------------------------------------------------------------------");
|
logger.debug("simulating post combat actions ----------------------------------------------------------------------------------------");
|
||||||
addActionsTimed(new FilterAbility());
|
addActionsTimed(new FilterAbility());
|
||||||
// addActions(root, new FilterAbility(), maxDepth, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
// addActions(root, new FilterAbility(), maxDepth, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||||
|
@ -182,34 +186,38 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
root = root.children.get(0);
|
root = root.children.get(0);
|
||||||
actions = new LinkedList<Ability>(root.abilities);
|
actions = new LinkedList<Ability>(root.abilities);
|
||||||
combat = root.combat;
|
combat = root.combat;
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("adding post-combat actions:" + actions);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
logger.debug("no post-combat actions added");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int addActions(SimulationNode node, FilterAbility filter, int depth, int alpha, int beta) {
|
protected int addActions(SimulationNode node, FilterAbility filter, int alpha, int beta) {
|
||||||
boolean stepFinished = false;
|
boolean stepFinished = false;
|
||||||
int val;
|
int val;
|
||||||
Game game = node.getGame();
|
Game game = node.getGame();
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
logger.debug("interrupted");
|
logger.debug(indent(node.depth) + "interrupted");
|
||||||
return GameStateEvaluator.evaluate(playerId, game);
|
return GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
if (depth <= 0 || SimulationNode.nodeCount > maxNodes || game.isGameOver()) {
|
if (node.depth > maxDepth || game.isGameOver()) {
|
||||||
logger.debug("simulating -- reached end state");
|
logger.debug(indent(node.depth) + "simulating -- reached end state");
|
||||||
val = GameStateEvaluator.evaluate(playerId, game);
|
val = GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
else if (node.getChildren().size() > 0) {
|
else if (node.getChildren().size() > 0) {
|
||||||
logger.debug("simulating -- somthing added children:" + node.getChildren().size());
|
logger.debug(indent(node.depth) + "simulating -- somthing added children:" + node.getChildren().size());
|
||||||
val = minimaxAB(node, filter, depth-1, alpha, beta);
|
val = minimaxAB(node, filter, alpha, beta);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("simulating -- alpha: " + alpha + " beta: " + beta + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(game.getPlayerList().get()).getName());
|
logger.debug(indent(node.depth) + "simulating -- alpha: " + alpha + " beta: " + beta + " depth:" + node.depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(game.getPlayerList().get()).getName());
|
||||||
if (allPassed(game)) {
|
if (allPassed(game)) {
|
||||||
if (!game.getStack().isEmpty()) {
|
if (!game.getStack().isEmpty()) {
|
||||||
resolve(node, depth, game);
|
resolve(node, game);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
stepFinished = true;
|
stepFinished = true;
|
||||||
|
@ -220,21 +228,21 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
val = GameStateEvaluator.evaluate(playerId, game);
|
val = GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
else if (stepFinished) {
|
else if (stepFinished) {
|
||||||
logger.debug("step finished");
|
logger.debug(indent(node.depth) + "step finished");
|
||||||
int testScore = GameStateEvaluator.evaluate(playerId, game);
|
int testScore = GameStateEvaluator.evaluate(playerId, game);
|
||||||
if (game.getActivePlayerId().equals(playerId)) {
|
if (game.getActivePlayerId().equals(playerId)) {
|
||||||
if (testScore < currentScore) {
|
if (testScore < currentScore) {
|
||||||
// if score at end of step is worse than original score don't check further
|
// if score at end of step is worse than original score don't check further
|
||||||
logger.debug("simulating -- abandoning check, no immediate benefit");
|
logger.debug(indent(node.depth) + "simulating -- abandoning check, no immediate benefit");
|
||||||
val = testScore;
|
val = testScore;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
switch (game.getTurn().getStepType()) {
|
switch (game.getTurn().getStepType()) {
|
||||||
case PRECOMBAT_MAIN:
|
case PRECOMBAT_MAIN:
|
||||||
val = simulateCombat(game, node, depth-1, alpha, beta, false);
|
val = simulateCombat(game, node, alpha, beta, false);
|
||||||
break;
|
break;
|
||||||
case POSTCOMBAT_MAIN:
|
case POSTCOMBAT_MAIN:
|
||||||
val = simulateCounterAttack(game, node, depth-1, alpha, beta);
|
val = simulateCounterAttack(game, node, alpha, beta);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
val = GameStateEvaluator.evaluate(playerId, game);
|
val = GameStateEvaluator.evaluate(playerId, game);
|
||||||
|
@ -244,31 +252,31 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (game.getTurn().getStepType() == PhaseStep.DECLARE_ATTACKERS)
|
if (game.getTurn().getStepType() == PhaseStep.DECLARE_ATTACKERS)
|
||||||
val = simulateBlockers(game, node, playerId, depth-1, alpha, beta, true);
|
val = simulateBlockers(game, node, playerId, alpha, beta, true);
|
||||||
else
|
else
|
||||||
val = GameStateEvaluator.evaluate(playerId, game);
|
val = GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (node.getChildren().size() > 0) {
|
else if (node.getChildren().size() > 0) {
|
||||||
logger.debug("simulating -- trigger added children:" + node.getChildren().size());
|
logger.debug(indent(node.depth) + "simulating -- trigger added children:" + node.getChildren().size());
|
||||||
val = minimaxAB(node, filter, depth, alpha, beta);
|
val = minimaxAB(node, filter, alpha, beta);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
val = simulatePriority(node, game, filter, depth, alpha, beta);
|
val = simulatePriority(node, game, filter, alpha, beta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("returning -- score: " + val + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
logger.debug(indent(node.depth) + "returning -- score: " + val + " depth:" + node.depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||||
return val;
|
return val;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int simulateCombat(Game game, SimulationNode node, int depth, int alpha, int beta, boolean counter) {
|
protected int simulateCombat(Game game, SimulationNode node, int alpha, int beta, boolean counter) {
|
||||||
Integer val = null;
|
Integer val = null;
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
logger.debug("interrupted");
|
logger.debug(indent(node.depth) + "interrupted");
|
||||||
return GameStateEvaluator.evaluate(playerId, game);
|
return GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
if (game.getTurn().getStepType() != PhaseStep.DECLARE_BLOCKERS) {
|
if (game.getTurn().getStepType() != PhaseStep.DECLARE_BLOCKERS) {
|
||||||
|
@ -279,12 +287,12 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
if (!game.getStep().skipStep(game, game.getActivePlayerId())) {
|
if (!game.getStep().skipStep(game, game.getActivePlayerId())) {
|
||||||
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_PRE, null, null, game.getActivePlayerId()));
|
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_PRE, null, null, game.getActivePlayerId()));
|
||||||
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, game.getActivePlayerId(), game.getActivePlayerId()))) {
|
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, game.getActivePlayerId(), game.getActivePlayerId()))) {
|
||||||
val = simulateAttackers(game, node, game.getActivePlayerId(), depth, alpha, beta, counter);
|
val = simulateAttackers(game, node, game.getActivePlayerId(), alpha, beta, counter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!counter) {
|
else if (!counter) {
|
||||||
simulateToEnd(game);
|
simulateToEnd(game);
|
||||||
val = simulatePostCombatMain(game, node, depth, alpha, beta);
|
val = simulatePostCombatMain(game, node, alpha, beta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -293,26 +301,26 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_PRE, null, null, game.getActivePlayerId()));
|
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_PRE, null, null, game.getActivePlayerId()));
|
||||||
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_BLOCKERS, game.getActivePlayerId(), game.getActivePlayerId()))) {
|
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_BLOCKERS, game.getActivePlayerId(), game.getActivePlayerId()))) {
|
||||||
//only suitable for two player games - only simulates blocks for 1st defender
|
//only suitable for two player games - only simulates blocks for 1st defender
|
||||||
val = simulateBlockers(game, node, game.getCombat().getDefenders().iterator().next(), depth, alpha, beta, counter);
|
val = simulateBlockers(game, node, game.getCombat().getDefenders().iterator().next(), alpha, beta, counter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!counter) {
|
else if (!counter) {
|
||||||
finishCombat(game);
|
finishCombat(game);
|
||||||
val = simulateCounterAttack(game, node, depth, alpha, beta);
|
val = simulateCounterAttack(game, node, alpha, beta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (val == null)
|
if (val == null)
|
||||||
val = GameStateEvaluator.evaluate(playerId, game);
|
val = GameStateEvaluator.evaluate(playerId, game);
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("returning -- combat score: " + val + " depth:" + depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
logger.debug(indent(node.depth) + "returning -- combat score: " + val + " depth:" + node.depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected int simulateAttackers(Game game, SimulationNode node, UUID attackerId, int depth, int alpha, int beta, boolean counter) {
|
protected int simulateAttackers(Game game, SimulationNode node, UUID attackerId, int alpha, int beta, boolean counter) {
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
logger.debug("interrupted");
|
logger.debug(indent(node.depth) + "interrupted");
|
||||||
return GameStateEvaluator.evaluate(playerId, game);
|
return GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
Integer val = null;
|
Integer val = null;
|
||||||
|
@ -320,10 +328,10 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
SimulatedPlayer attacker = (SimulatedPlayer) game.getPlayer(attackerId);
|
SimulatedPlayer attacker = (SimulatedPlayer) game.getPlayer(attackerId);
|
||||||
|
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug(attacker.getName() + "'s possible attackers: " + attacker.getAvailableAttackers(game));
|
logger.debug(indent(node.depth) + attacker.getName() + "'s possible attackers: " + attacker.getAvailableAttackers(game));
|
||||||
for (Combat engagement: attacker.addAttackers(game)) {
|
for (Combat engagement: attacker.addAttackers(game)) {
|
||||||
if (alpha >= beta) {
|
if (alpha >= beta) {
|
||||||
logger.debug("simulating -- pruning attackers");
|
logger.debug(indent(node.depth) + "simulating -- pruning attackers");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Game sim = game.copy();
|
Game sim = game.copy();
|
||||||
|
@ -334,19 +342,19 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, attackerId, attackerId));
|
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, attackerId, attackerId));
|
||||||
SimulationNode newNode = new SimulationNode(node, sim, depth, attackerId);
|
SimulationNode newNode = new SimulationNode(node, sim, attackerId);
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("simulating attack for player:" + game.getPlayer(attackerId).getName());
|
logger.debug(indent(node.depth) + "simulating attack for player:" + game.getPlayer(attackerId).getName());
|
||||||
sim.checkStateAndTriggered();
|
sim.checkStateAndTriggered();
|
||||||
while (!sim.getStack().isEmpty()) {
|
while (!sim.getStack().isEmpty()) {
|
||||||
sim.getStack().resolve(sim);
|
sim.getStack().resolve(sim);
|
||||||
logger.debug("resolving triggered abilities");
|
logger.debug(indent(node.depth) + "resolving triggered abilities");
|
||||||
sim.applyEffects();
|
sim.applyEffects();
|
||||||
}
|
}
|
||||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_POST, sim.getActivePlayerId(), sim.getActivePlayerId()));
|
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_POST, sim.getActivePlayerId(), sim.getActivePlayerId()));
|
||||||
Combat simCombat = sim.getCombat().copy();
|
Combat simCombat = sim.getCombat().copy();
|
||||||
sim.getPhase().setStep(new DeclareBlockersStep());
|
sim.getPhase().setStep(new DeclareBlockersStep());
|
||||||
val = simulateCombat(sim, newNode, depth-1, alpha, beta, counter);
|
val = simulateCombat(sim, newNode, alpha, beta, counter);
|
||||||
if (!attackerId.equals(playerId)) {
|
if (!attackerId.equals(playerId)) {
|
||||||
if (val < beta) {
|
if (val < beta) {
|
||||||
beta = val;
|
beta = val;
|
||||||
|
@ -369,14 +377,14 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
node.children.add(bestNode);
|
node.children.add(bestNode);
|
||||||
}
|
}
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("returning -- combat attacker score: " + val + " depth:" + depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
logger.debug(indent(node.depth) + "returning -- combat attacker score: " + val + " depth:" + node.depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int simulateBlockers(Game game, SimulationNode node, UUID defenderId, int depth, int alpha, int beta, boolean counter) {
|
protected int simulateBlockers(Game game, SimulationNode node, UUID defenderId, int alpha, int beta, boolean counter) {
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
logger.debug("interrupted");
|
logger.debug(indent(node.depth) + "interrupted");
|
||||||
return GameStateEvaluator.evaluate(playerId, game);
|
return GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
Integer val = null;
|
Integer val = null;
|
||||||
|
@ -385,10 +393,10 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
if (game.getCombat().isAttacked(defenderId, game)) {
|
if (game.getCombat().isAttacked(defenderId, game)) {
|
||||||
SimulatedPlayer defender = (SimulatedPlayer) game.getPlayer(defenderId);
|
SimulatedPlayer defender = (SimulatedPlayer) game.getPlayer(defenderId);
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug(defender.getName() + "'s possible blockers: " + defender.getAvailableBlockers(game));
|
logger.debug(indent(node.depth) + defender.getName() + "'s possible blockers: " + defender.getAvailableBlockers(game));
|
||||||
for (Combat engagement: defender.addBlockers(game)) {
|
for (Combat engagement: defender.addBlockers(game)) {
|
||||||
if (alpha >= beta) {
|
if (alpha >= beta) {
|
||||||
logger.debug("simulating -- pruning blockers");
|
logger.debug(indent(node.depth) + "simulating -- pruning blockers");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Game sim = game.copy();
|
Game sim = game.copy();
|
||||||
|
@ -401,13 +409,13 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, defenderId, defenderId));
|
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, defenderId, defenderId));
|
||||||
SimulationNode newNode = new SimulationNode(node, sim, depth, defenderId);
|
SimulationNode newNode = new SimulationNode(node, sim, defenderId);
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("simulating block for player:" + game.getPlayer(defenderId).getName());
|
logger.debug(indent(node.depth) + "simulating block for player:" + game.getPlayer(defenderId).getName());
|
||||||
sim.checkStateAndTriggered();
|
sim.checkStateAndTriggered();
|
||||||
while (!sim.getStack().isEmpty()) {
|
while (!sim.getStack().isEmpty()) {
|
||||||
sim.getStack().resolve(sim);
|
sim.getStack().resolve(sim);
|
||||||
logger.debug("resolving triggered abilities");
|
logger.debug(indent(node.depth) + "resolving triggered abilities");
|
||||||
sim.applyEffects();
|
sim.applyEffects();
|
||||||
}
|
}
|
||||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_POST, sim.getActivePlayerId(), sim.getActivePlayerId()));
|
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_POST, sim.getActivePlayerId(), sim.getActivePlayerId()));
|
||||||
|
@ -417,7 +425,7 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
val = GameStateEvaluator.evaluate(playerId, sim);
|
val = GameStateEvaluator.evaluate(playerId, sim);
|
||||||
}
|
}
|
||||||
else if (!counter) {
|
else if (!counter) {
|
||||||
val = simulatePostCombatMain(sim, newNode, depth-1, alpha, beta);
|
val = simulatePostCombatMain(sim, newNode, alpha, beta);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
val = GameStateEvaluator.evaluate(playerId, sim);
|
val = GameStateEvaluator.evaluate(playerId, sim);
|
||||||
|
@ -444,21 +452,21 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
node.children.add(bestNode);
|
node.children.add(bestNode);
|
||||||
}
|
}
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("returning -- combat blocker score: " + val + " depth:" + depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
logger.debug(indent(node.depth) + "returning -- combat blocker score: " + val + " depth:" + node.depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int simulateCounterAttack(Game game, SimulationNode node, int depth, int alpha, int beta) {
|
protected int simulateCounterAttack(Game game, SimulationNode node, int alpha, int beta) {
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
logger.debug("interrupted");
|
logger.debug(indent(node.depth) + "interrupted");
|
||||||
return GameStateEvaluator.evaluate(playerId, game);
|
return GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
Integer val = null;
|
Integer val = null;
|
||||||
if (!game.isGameOver()) {
|
if (!game.isGameOver()) {
|
||||||
simulateToEnd(game);
|
simulateToEnd(game);
|
||||||
game.getState().setActivePlayerId(game.getState().getPlayerList(game.getActivePlayerId()).getNext());
|
game.getState().setActivePlayerId(game.getState().getPlayerList(game.getActivePlayerId()).getNext());
|
||||||
logger.debug("simulating -- counter attack for player " + game.getPlayer(game.getActivePlayerId()).getName());
|
logger.debug(indent(node.depth) + "simulating -- counter attack for player " + game.getPlayer(game.getActivePlayerId()).getName());
|
||||||
game.getTurn().setPhase(new BeginningPhase());
|
game.getTurn().setPhase(new BeginningPhase());
|
||||||
if (game.getPhase().beginPhase(game, game.getActivePlayerId())) {
|
if (game.getPhase().beginPhase(game, game.getActivePlayerId())) {
|
||||||
simulateStep(game, new UntapStep());
|
simulateStep(game, new UntapStep());
|
||||||
|
@ -466,9 +474,9 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
simulateStep(game, new DrawStep());
|
simulateStep(game, new DrawStep());
|
||||||
game.getPhase().endPhase(game, game.getActivePlayerId());
|
game.getPhase().endPhase(game, game.getActivePlayerId());
|
||||||
}
|
}
|
||||||
val = simulateCombat(game, node, depth-1, alpha, beta, true);
|
val = simulateCombat(game, node, alpha, beta, true);
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("returning -- counter attack score: " + val + " depth:" + depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
logger.debug(indent(node.depth) + "returning -- counter attack score: " + val + " depth:" + node.depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||||
}
|
}
|
||||||
if (val == null)
|
if (val == null)
|
||||||
val = GameStateEvaluator.evaluate(playerId, game);
|
val = GameStateEvaluator.evaluate(playerId, game);
|
||||||
|
@ -506,21 +514,21 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||||
simulateStep(game, new EndOfCombatStep());
|
simulateStep(game, new EndOfCombatStep());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int simulatePostCombatMain(Game game, SimulationNode node, int depth, int alpha, int beta) {
|
protected int simulatePostCombatMain(Game game, SimulationNode node, int alpha, int beta) {
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
logger.debug("interrupted");
|
logger.debug(indent(node.depth) + "interrupted");
|
||||||
return GameStateEvaluator.evaluate(playerId, game);
|
return GameStateEvaluator.evaluate(playerId, game);
|
||||||
}
|
}
|
||||||
logger.debug("simulating -- post combat main");
|
logger.debug(indent(node.depth) + "simulating -- post combat main");
|
||||||
game.getTurn().setPhase(new PostCombatMainPhase());
|
game.getTurn().setPhase(new PostCombatMainPhase());
|
||||||
if (game.getPhase().beginPhase(game, game.getActivePlayerId())) {
|
if (game.getPhase().beginPhase(game, game.getActivePlayerId())) {
|
||||||
game.getPhase().setStep(new PostCombatMainStep());
|
game.getPhase().setStep(new PostCombatMainStep());
|
||||||
game.getStep().beginStep(game, playerId);
|
game.getStep().beginStep(game, playerId);
|
||||||
game.getPlayers().resetPassed();
|
game.getPlayers().resetPassed();
|
||||||
return addActions(node, new FilterAbility(), depth, alpha, beta);
|
return addActions(node, new FilterAbility(), alpha, beta);
|
||||||
}
|
}
|
||||||
return simulateCounterAttack(game, node, depth, alpha, beta);
|
return simulateCounterAttack(game, node, alpha, beta);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void simulateToEnd(Game game) {
|
protected void simulateToEnd(Game game) {
|
||||||
|
|
|
@ -63,6 +63,10 @@ public class GameStateEvaluator {
|
||||||
public static final int LOSE_SCORE = Integer.MIN_VALUE + 1;
|
public static final int LOSE_SCORE = Integer.MIN_VALUE + 1;
|
||||||
|
|
||||||
public static int evaluate(UUID playerId, Game game) {
|
public static int evaluate(UUID playerId, Game game) {
|
||||||
|
return evaluate(playerId, game, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int evaluate(UUID playerId, Game game, boolean ignoreTapped) {
|
||||||
Player player = game.getPlayer(playerId);
|
Player player = game.getPlayer(playerId);
|
||||||
Player opponent = game.getPlayer(game.getOpponents(playerId).iterator().next());
|
Player opponent = game.getPlayer(game.getOpponents(playerId).iterator().next());
|
||||||
if (game.isGameOver()) {
|
if (game.isGameOver()) {
|
||||||
|
@ -74,10 +78,10 @@ public class GameStateEvaluator {
|
||||||
int lifeScore = (player.getLife() - opponent.getLife()) * LIFE_FACTOR;
|
int lifeScore = (player.getLife() - opponent.getLife()) * LIFE_FACTOR;
|
||||||
int permanentScore = 0;
|
int permanentScore = 0;
|
||||||
for (Permanent permanent: game.getBattlefield().getAllActivePermanents(playerId)) {
|
for (Permanent permanent: game.getBattlefield().getAllActivePermanents(playerId)) {
|
||||||
permanentScore += evaluatePermanent(permanent, game);
|
permanentScore += evaluatePermanent(permanent, game, ignoreTapped);
|
||||||
}
|
}
|
||||||
for (Permanent permanent: game.getBattlefield().getAllActivePermanents(opponent.getId())) {
|
for (Permanent permanent: game.getBattlefield().getAllActivePermanents(opponent.getId())) {
|
||||||
permanentScore -= evaluatePermanent(permanent, game);
|
permanentScore -= evaluatePermanent(permanent, game, ignoreTapped);
|
||||||
}
|
}
|
||||||
permanentScore *= PERMANENT_FACTOR;
|
permanentScore *= PERMANENT_FACTOR;
|
||||||
|
|
||||||
|
@ -91,8 +95,12 @@ public class GameStateEvaluator {
|
||||||
return score;
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int evaluatePermanent(Permanent permanent, Game game) {
|
public static int evaluatePermanent(Permanent permanent, Game game, boolean ignoreTapped) {
|
||||||
int value = permanent.isTapped()?4:5;
|
int value = 0;
|
||||||
|
if (ignoreTapped)
|
||||||
|
value = 5;
|
||||||
|
else
|
||||||
|
value = permanent.isTapped()?4:5;
|
||||||
if (permanent.getCardType().contains(CardType.CREATURE)) {
|
if (permanent.getCardType().contains(CardType.CREATURE)) {
|
||||||
value += evaluateCreature(permanent, game) * CREATURE_FACTOR;
|
value += evaluateCreature(permanent, game) * CREATURE_FACTOR;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
package mage.player.ai;
|
package mage.player.ai;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -64,9 +65,11 @@ public class SimulatedPlayer extends ComputerPlayer<SimulatedPlayer> {
|
||||||
private FilterAbility filter;
|
private FilterAbility filter;
|
||||||
private transient ConcurrentLinkedQueue<Ability> allActions;
|
private transient ConcurrentLinkedQueue<Ability> allActions;
|
||||||
private static PassAbility pass = new PassAbility();
|
private static PassAbility pass = new PassAbility();
|
||||||
|
protected int maxDepth;
|
||||||
|
|
||||||
public SimulatedPlayer(UUID id, boolean isSimulatedPlayer) {
|
public SimulatedPlayer(UUID id, boolean isSimulatedPlayer) {
|
||||||
super(id);
|
super(id);
|
||||||
|
maxDepth = Config.maxDepth;
|
||||||
pass.setControllerId(playerId);
|
pass.setControllerId(playerId);
|
||||||
this.isSimulatedPlayer = isSimulatedPlayer;
|
this.isSimulatedPlayer = isSimulatedPlayer;
|
||||||
}
|
}
|
||||||
|
@ -91,6 +94,7 @@ public class SimulatedPlayer extends ComputerPlayer<SimulatedPlayer> {
|
||||||
simulateOptions(sim, pass);
|
simulateOptions(sim, pass);
|
||||||
|
|
||||||
ArrayList<Ability> list = new ArrayList<Ability>(allActions);
|
ArrayList<Ability> list = new ArrayList<Ability>(allActions);
|
||||||
|
//Collections.shuffle(list);
|
||||||
Collections.reverse(list);
|
Collections.reverse(list);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -244,23 +248,22 @@ public class SimulatedPlayer extends ComputerPlayer<SimulatedPlayer> {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
SimulationNode parent = (SimulationNode) game.getCustomData();
|
SimulationNode parent = (SimulationNode) game.getCustomData();
|
||||||
int depth = parent.getDepth() - 1;
|
if (parent.getDepth() == maxDepth) return true;
|
||||||
if (depth == 0) return true;
|
logger.debug(indent(parent.getDepth()) + "simulating -- triggered ability - adding children:" + options.size());
|
||||||
logger.debug("simulating -- triggered ability - adding children:" + options.size());
|
|
||||||
for (Ability option: options) {
|
for (Ability option: options) {
|
||||||
addAbilityNode(parent, option, depth, game);
|
addAbilityNode(parent, option, game);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addAbilityNode(SimulationNode parent, Ability ability, int depth, Game game) {
|
protected void addAbilityNode(SimulationNode parent, Ability ability, Game game) {
|
||||||
Game sim = game.copy();
|
Game sim = game.copy();
|
||||||
sim.getStack().push(new StackAbility(ability, playerId));
|
sim.getStack().push(new StackAbility(ability, playerId));
|
||||||
ability.activate(sim, false);
|
ability.activate(sim, false);
|
||||||
sim.applyEffects();
|
sim.applyEffects();
|
||||||
SimulationNode newNode = new SimulationNode(parent, sim, depth, playerId);
|
SimulationNode newNode = new SimulationNode(parent, sim, playerId);
|
||||||
logger.debug("simulating -- node #:" + SimulationNode.getCount() + " triggered ability option");
|
logger.debug(indent(newNode.getDepth()) + "simulating -- node #:" + SimulationNode.getCount() + " triggered ability option");
|
||||||
for (Target target: ability.getTargets()) {
|
for (Target target: ability.getTargets()) {
|
||||||
for (UUID targetId: target.getTargets()) {
|
for (UUID targetId: target.getTargets()) {
|
||||||
newNode.getTargets().add(targetId);
|
newNode.getTargets().add(targetId);
|
||||||
|
@ -277,4 +280,10 @@ public class SimulatedPlayer extends ComputerPlayer<SimulatedPlayer> {
|
||||||
//should never get here
|
//should never get here
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String indent(int num) {
|
||||||
|
char[] fill = new char[num];
|
||||||
|
Arrays.fill(fill, ' ');
|
||||||
|
return new String(fill);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,22 +55,25 @@ public class SimulationNode implements Serializable {
|
||||||
protected UUID playerId;
|
protected UUID playerId;
|
||||||
protected Combat combat;
|
protected Combat combat;
|
||||||
|
|
||||||
public SimulationNode(SimulationNode parent, Game game, int depth, UUID playerId) {
|
public SimulationNode(SimulationNode parent, Game game, UUID playerId) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.game = game;
|
this.game = game;
|
||||||
this.depth = depth;
|
if (parent == null)
|
||||||
|
this.depth = 1;
|
||||||
|
else
|
||||||
|
this.depth = parent.getDepth() + 1;
|
||||||
this.playerId = playerId;
|
this.playerId = playerId;
|
||||||
game.setCustomData(this);
|
game.setCustomData(this);
|
||||||
nodeCount++;
|
nodeCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimulationNode(SimulationNode parent, Game game, List<Ability> abilities, int depth, UUID playerId) {
|
public SimulationNode(SimulationNode parent, Game game, List<Ability> abilities, UUID playerId) {
|
||||||
this(parent, game, depth, playerId);
|
this(parent, game, playerId);
|
||||||
this.abilities = abilities;
|
this.abilities = abilities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimulationNode(SimulationNode parent, Game game, Ability ability, int depth, UUID playerId) {
|
public SimulationNode(SimulationNode parent, Game game, Ability ability, UUID playerId) {
|
||||||
this(parent, game, depth, playerId);
|
this(parent, game, playerId);
|
||||||
this.abilities = new ArrayList<Ability>();
|
this.abilities = new ArrayList<Ability>();
|
||||||
abilities.add(ability);
|
abilities.add(ability);
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -53,7 +53,7 @@ public class PayLifeCost extends CostImpl<PayLifeCost> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canPay(UUID sourceId, UUID controllerId, Game game) {
|
public boolean canPay(UUID sourceId, UUID controllerId, Game game) {
|
||||||
return game.getPlayer(controllerId).getLife() >= amount;
|
return game.getPlayer(controllerId).getLife() > amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue