use of mage.player.ai.simulators.CombatSimulator in project mage by magefree.
the class ComputerPlayer method simulateAttack.
protected CombatSimulator simulateAttack(Attackers attackers, List<Permanent> blockers, UUID opponentId, Game game) {
log.debug("simulateAttack");
List<Permanent> attackersList = attackers.getAttackers();
CombatSimulator best = new CombatSimulator();
int bestResult = 0;
// use binary digits to calculate powerset of attackers
int powerElements = (int) Math.pow(2, attackersList.size());
for (int i = 1; i < powerElements; i++) {
String binary = Integer.toBinaryString(i);
while (binary.length() < attackersList.size()) {
binary = '0' + binary;
}
List<Permanent> trialAttackers = new ArrayList<>();
for (int j = 0; j < attackersList.size(); j++) {
if (binary.charAt(j) == '1') {
trialAttackers.add(attackersList.get(j));
}
}
CombatSimulator combat = new CombatSimulator();
for (Permanent permanent : trialAttackers) {
combat.groups.add(new CombatGroupSimulator(opponentId, Arrays.asList(permanent.getId()), new ArrayList<UUID>(), game));
}
CombatSimulator test = simulateBlock(combat, blockers, game);
if (test.evaluate() > bestResult) {
best = test;
bestResult = test.evaluate();
}
}
return best;
}
use of mage.player.ai.simulators.CombatSimulator in project mage by magefree.
the class ComputerPlayer method selectAttackers.
@Override
public void selectAttackers(Game game, UUID attackingPlayerId) {
log.debug("selectAttackers");
UUID opponentId = game.getCombat().getDefenders().iterator().next();
Attackers attackers = getPotentialAttackers(game);
List<Permanent> blockers = getOpponentBlockers(opponentId, game);
List<Permanent> actualAttackers = new ArrayList<>();
if (blockers.isEmpty()) {
actualAttackers = attackers.getAttackers();
} else if (attackers.size() - blockers.size() >= game.getPlayer(opponentId).getLife()) {
actualAttackers = attackers.getAttackers();
} else {
CombatSimulator combat = simulateAttack(attackers, blockers, opponentId, game);
if (combat.rating > 2) {
for (CombatGroupSimulator group : combat.groups) {
this.declareAttacker(group.attackers.get(0).id, group.defenderId, game, false);
}
}
}
for (Permanent attacker : actualAttackers) {
this.declareAttacker(attacker.getId(), opponentId, game, false);
}
}
use of mage.player.ai.simulators.CombatSimulator in project mage by magefree.
the class ComputerPlayer method selectBlockers.
@Override
public void selectBlockers(Ability source, Game game, UUID defendingPlayerId) {
log.debug("selectBlockers");
List<Permanent> blockers = getAvailableBlockers(game);
CombatSimulator sim = simulateBlock(CombatSimulator.load(game), blockers, game);
List<CombatGroup> groups = game.getCombat().getGroups();
for (int i = 0; i < groups.size(); i++) {
for (CreatureSimulator creature : sim.groups.get(i).blockers) {
groups.get(i).addBlocker(creature.id, playerId, game);
}
}
}
use of mage.player.ai.simulators.CombatSimulator in project mage by magefree.
the class ComputerPlayer method getWorstSimulation.
protected CombatSimulator getWorstSimulation(TreeNode<CombatSimulator> simulations) {
CombatSimulator worst = simulations.getData();
int worstResult = worst.evaluate();
for (TreeNode<CombatSimulator> node : simulations.getChildren()) {
CombatSimulator worstSub = getWorstSimulation(node);
if (worstSub.evaluate() < worstResult) {
worst = node.getData();
worstResult = worst.evaluate();
}
}
return worst;
}
use of mage.player.ai.simulators.CombatSimulator in project mage by magefree.
the class ComputerPlayer method getBestSimulation.
protected CombatSimulator getBestSimulation(TreeNode<CombatSimulator> simulations) {
CombatSimulator best = simulations.getData();
int bestResult = best.evaluate();
for (TreeNode<CombatSimulator> node : simulations.getChildren()) {
CombatSimulator bestSub = getBestSimulation(node);
if (bestSub.evaluate() > bestResult) {
best = node.getData();
bestResult = best.evaluate();
}
}
return best;
}
Aggregations