Trying to use less CardLists and/or hardcoded constants

This commit is contained in:
Maxmtg
2012-09-28 22:59:08 +00:00
parent 88030ed7fd
commit 15d3074d84
31 changed files with 274 additions and 406 deletions

View File

@@ -118,43 +118,12 @@ public class CardList extends ArrayList<Card> {
return this.filter(Predicates.not(CardPredicates.isProtectedFrom(source))); return this.filter(Predicates.not(CardPredicates.isProtectedFrom(source)));
} }
/**
* <p>
* getValidCards.
* </p>
*
* @param restrictions
* a {@link java.lang.String} object.
* @param sourceController
* a {@link forge.game.player.Player} object.
* @param source
* a {@link forge.Card} object.
* @return a {@link forge.CardList} object.
*/
public final CardList getValidCards(final String restrictions, final Player sourceController, final Card source) { public final CardList getValidCards(final String restrictions, final Player sourceController, final Card source) {
return this.getValidCards(restrictions.split(","), sourceController, source); return this.filter(CardPredicates.restriction(restrictions.split(","), sourceController, source));
} }
/**
* <p>
* getValidCards.
* </p>
*
* @param restrictions
* a {@link java.lang.String} object.
* @param sourceController
* a {@link forge.game.player.Player} object.
* @param source
* a {@link forge.Card} object.
* @return a {@link forge.CardList} object.
*/
public final CardList getValidCards(final String[] restrictions, final Player sourceController, final Card source) { public final CardList getValidCards(final String[] restrictions, final Player sourceController, final Card source) {
return this.filter(new Predicate<Card>() { return this.filter(CardPredicates.restriction(restrictions, sourceController, source));
@Override
public boolean apply(final Card c) {
return (c != null) && c.isValid(restrictions, sourceController, source);
}
});
} }

View File

@@ -21,6 +21,7 @@ import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import forge.card.cardfactory.CardFactoryUtil;
import forge.card.spellability.SpellAbility; import forge.card.spellability.SpellAbility;
import forge.game.phase.CombatUtil; import forge.game.phase.CombatUtil;
import forge.game.player.Player; import forge.game.player.Player;
@@ -37,7 +38,7 @@ import forge.util.PredicateString;
*/ */
public final class CardPredicates { public final class CardPredicates {
public static Predicate<Card> isController(final Player p) { public final static Predicate<Card> isController(final Player p) {
return new Predicate<Card>() { return new Predicate<Card>() {
@Override @Override
public boolean apply(final Card c) { public boolean apply(final Card c) {
@@ -45,7 +46,7 @@ public final class CardPredicates {
} }
}; };
} }
public static Predicate<Card> isOwner(final Player p) { public final static Predicate<Card> isOwner(final Player p) {
return new Predicate<Card>() { return new Predicate<Card>() {
@Override @Override
public boolean apply(final Card c) { public boolean apply(final Card c) {
@@ -54,7 +55,7 @@ public final class CardPredicates {
}; };
} }
public static Predicate<Card> isType(final String cardType) { public final static Predicate<Card> isType(final String cardType) {
return new Predicate<Card>() { return new Predicate<Card>() {
@Override @Override
public boolean apply(final Card c) { public boolean apply(final Card c) {
@@ -63,7 +64,7 @@ public final class CardPredicates {
}; };
} }
public static Predicate<Card> hasKeyword(final String keyword) { public final static Predicate<Card> hasKeyword(final String keyword) {
return new Predicate<Card>() { return new Predicate<Card>() {
@Override @Override
public boolean apply(final Card c) { public boolean apply(final Card c) {
@@ -72,7 +73,7 @@ public final class CardPredicates {
}; };
} }
public static Predicate<Card> containsKeyword(final String keyword) { public final static Predicate<Card> containsKeyword(final String keyword) {
return new Predicate<Card>() { return new Predicate<Card>() {
@Override @Override
public boolean apply(final Card c) { public boolean apply(final Card c) {
@@ -81,7 +82,7 @@ public final class CardPredicates {
}; };
} }
public static Predicate<Card> isTargetableBy(final SpellAbility source) { public final static Predicate<Card> isTargetableBy(final SpellAbility source) {
return new Predicate<Card>() { return new Predicate<Card>() {
@Override @Override
public boolean apply(final Card c) { public boolean apply(final Card c) {
@@ -90,7 +91,7 @@ public final class CardPredicates {
}; };
} }
public static Predicate<Card> nameEquals(final String name) { public final static Predicate<Card> nameEquals(final String name) {
return new Predicate<Card>() { return new Predicate<Card>() {
@Override @Override
public boolean apply(Card c) { public boolean apply(Card c) {
@@ -100,7 +101,7 @@ public final class CardPredicates {
} }
public static Predicate<Card> possibleBlockers(final Card attacker) { public final static Predicate<Card> possibleBlockers(final Card attacker) {
return new Predicate<Card>() { return new Predicate<Card>() {
@Override @Override
public boolean apply(final Card c) { public boolean apply(final Card c) {
@@ -116,7 +117,7 @@ public final class CardPredicates {
} }
}; };
public static Predicate<Card> isProtectedFrom(final Card source) { public final static Predicate<Card> isProtectedFrom(final Card source) {
return new Predicate<Card>() { return new Predicate<Card>() {
@Override @Override
public boolean apply(final Card c) { public boolean apply(final Card c) {
@@ -126,6 +127,15 @@ public final class CardPredicates {
} }
public final static Predicate<Card> restriction(final String[] restrictions, final Player sourceController, final Card source) {
return new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return (c != null) && c.isValid(restrictions, sourceController, source);
}
};
}
public static class Presets { public static class Presets {
/** /**
@@ -155,6 +165,15 @@ public final class CardPredicates {
return c.isCreature(); return c.isCreature();
} }
}; };
public static final Predicate<Card> CREATURES_CAN_ATTACK = new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return c.isCreature() && CombatUtil.canAttack(c);
}
};
/** /**
* a Predicate<Card> to get all enchantments. * a Predicate<Card> to get all enchantments.
*/ */
@@ -209,15 +228,6 @@ public final class CardPredicates {
return c.isToken(); return c.isToken();
} }
}; };
/**
* a Predicate<Card> to get all nonbasic lands.
*/
public static final Predicate<Card> NON_BASIC_LAND = new Predicate<Card>() {
@Override
public boolean apply(Card c) {
return !c.isBasicLand();
}
};
/** /**
* a Predicate<Card> to get all basicLands. * a Predicate<Card> to get all basicLands.
*/ */
@@ -255,15 +265,7 @@ public final class CardPredicates {
return c.isLand(); return c.isLand();
} }
}; };
/**
* a Predicate<Card> to get all nonlands.
*/
public static final Predicate<Card> NON_LANDS = new Predicate<Card>() {
@Override
public boolean apply(Card c) {
return !c.isLand();
}
};
/** /**
* a Predicate<Card> to get all cards that are black. * a Predicate<Card> to get all cards that are black.
*/ */
@@ -323,6 +325,18 @@ public final class CardPredicates {
return c.isCreature() && (!c.hasFirstStrike() || c.hasDoubleStrike()); return c.isCreature() && (!c.hasFirstStrike() || c.hasDoubleStrike());
} }
}; };
public static final Predicate<Card> SNOW_LANDS = new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return c.isLand() && c.isSnow();
}
};
public static final Predicate<Card> PLANEWALKERS = new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return c.isPlaneswalker();
}
};
} }
public static class Accessors { public static class Accessors {
@@ -333,6 +347,13 @@ public final class CardPredicates {
} }
}; };
public static final Function<Card, Integer> fnGetNetAttack = new Function<Card, Integer>() {
@Override
public Integer apply(Card a) {
return a.getNetAttack();
}
};
public static final Function<Card, Integer> fnGetAttack = new Function<Card, Integer>() { public static final Function<Card, Integer> fnGetAttack = new Function<Card, Integer>() {
@Override @Override
public Integer apply(Card a) { public Integer apply(Card a) {
@@ -346,5 +367,12 @@ public final class CardPredicates {
return a.getCMC(); return a.getCMC();
} }
}; };
public static final Function<Card, Integer> fnEvaluateCreature = new Function<Card, Integer>() {
@Override
public Integer apply(Card a) {
return CardFactoryUtil.evaluateCreature(a);
}
};
} }
} }

View File

@@ -1217,7 +1217,7 @@ public class GameAction {
*/ */
private void destroyPlaneswalkers() { private void destroyPlaneswalkers() {
// get all Planeswalkers // get all Planeswalkers
final CardList list = AllZoneUtil.getCardsIn(ZoneType.Battlefield).getType("Planeswalker"); final CardList list = AllZoneUtil.getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.PLANEWALKERS);
Card c; Card c;
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
@@ -1772,7 +1772,7 @@ public class GameAction {
manaCost.decreaseColorlessMana(numToExile); manaCost.decreaseColorlessMana(numToExile);
} }
} else if (spell.getSourceCard().hasKeyword("Convoke")) { } else if (spell.getSourceCard().hasKeyword("Convoke")) {
CardList untappedCreats = spell.getActivatingPlayer().getCardsIn(ZoneType.Battlefield).getType("Creature"); CardList untappedCreats = spell.getActivatingPlayer().getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.CREATURES);
untappedCreats = untappedCreats.filter(new Predicate<Card>() { untappedCreats = untappedCreats.filter(new Predicate<Card>() {
@Override @Override
public boolean apply(final Card c) { public boolean apply(final Card c) {

View File

@@ -19,6 +19,7 @@ package forge;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@@ -1229,7 +1230,7 @@ public final class GameActionUtil {
// add +1/+1 to cards // add +1/+1 to cards
list.clear(); list.clear();
final int num = AllZoneUtil.getCardsIn(ZoneType.Battlefield, "Coat of Arms").size(); final int num = AllZoneUtil.getCardsIn(ZoneType.Battlefield, "Coat of Arms").size();
final CardList creatures = AllZoneUtil.getCardsIn(ZoneType.Battlefield).getType("Creature"); final List<Card> creatures = AllZoneUtil.getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.CREATURES);
for (Card c : creatures) { for (Card c : creatures) {
for (Card c2 : creatures) { for (Card c2 : creatures) {

View File

@@ -1232,7 +1232,7 @@ public final class AbilityFactoryChangeZone {
System.out.println("Don't need a land or none available; trying for a creature."); System.out.println("Don't need a land or none available; trying for a creature.");
fetchList = fetchList.getNotType("Land"); fetchList = fetchList.getNotType("Land");
// Prefer to pull a creature, generally more useful for AI. // Prefer to pull a creature, generally more useful for AI.
c = chooseCreature(fetchList.getType("Creature")); c = chooseCreature(fetchList.filter(CardPredicates.Presets.CREATURES));
} }
if (c == null) { // Could not find a creature. if (c == null) { // Could not find a creature.
if (ai.getLife() <= 5) { // Desperate? if (ai.getLife() <= 5) { // Desperate?
@@ -1699,7 +1699,7 @@ public final class AbilityFactoryChangeZone {
} }
// Save combatants // Save combatants
else if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) { else if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) {
final CardList combatants = aiPermanents.getType("Creature"); final CardList combatants = aiPermanents.filter(CardPredicates.Presets.CREATURES);
CardListUtil.sortByEvaluateCreature(combatants); CardListUtil.sortByEvaluateCreature(combatants);
for (final Card c : combatants) { for (final Card c : combatants) {
@@ -1808,7 +1808,7 @@ public final class AbilityFactoryChangeZone {
} else if (destination.equals(ZoneType.Hand) || destination.equals(ZoneType.Library)) { } else if (destination.equals(ZoneType.Hand) || destination.equals(ZoneType.Library)) {
CardList nonLands = list.getNotType("Land"); CardList nonLands = list.getNotType("Land");
// Prefer to pull a creature, generally more useful for AI. // Prefer to pull a creature, generally more useful for AI.
choice = chooseCreature(nonLands.getType("Creature")); choice = chooseCreature(nonLands.filter(CardPredicates.Presets.CREATURES));
if (choice == null) { // Could not find a creature. if (choice == null) { // Could not find a creature.
if (AllZone.getComputerPlayer().getLife() <= 5) { // Desperate? if (AllZone.getComputerPlayer().getLife() <= 5) { // Desperate?
// Get something AI can cast soon. // Get something AI can cast soon.
@@ -1921,7 +1921,7 @@ public final class AbilityFactoryChangeZone {
} else if (destination.equals(ZoneType.Hand) || destination.equals(ZoneType.Library)) { } else if (destination.equals(ZoneType.Hand) || destination.equals(ZoneType.Library)) {
CardList nonLands = list.getNotType("Land"); CardList nonLands = list.getNotType("Land");
// Prefer to pull a creature, generally more useful for AI. // Prefer to pull a creature, generally more useful for AI.
choice = chooseCreature(nonLands.getType("Creature")); choice = chooseCreature(nonLands.filter(CardPredicates.Presets.CREATURES));
if (choice == null) { // Could not find a creature. if (choice == null) { // Could not find a creature.
if (AllZone.getComputerPlayer().getLife() <= 5) { // Desperate? if (AllZone.getComputerPlayer().getLife() <= 5) { // Desperate?
// Get something AI can cast soon. // Get something AI can cast soon.

View File

@@ -34,6 +34,7 @@ import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates;
import forge.CardPredicates.Presets; import forge.CardPredicates.Presets;
import forge.CardUtil; import forge.CardUtil;
import forge.Constant; import forge.Constant;
@@ -738,7 +739,7 @@ public final class AbilityFactoryChoose {
CardList list = AllZoneUtil.getCreaturesInPlay(AllZone.getHumanPlayer()); CardList list = AllZoneUtil.getCreaturesInPlay(AllZone.getHumanPlayer());
if (list.isEmpty()) { if (list.isEmpty()) {
list = AllZoneUtil.getCardsInGame().getController(AllZone.getHumanPlayer()) list = AllZoneUtil.getCardsInGame().getController(AllZone.getHumanPlayer())
.getType("Creature"); .filter(CardPredicates.Presets.CREATURES);
} }
chosen = CardFactoryUtil.getMostProminentColor(list); chosen = CardFactoryUtil.getMostProminentColor(list);
} }

View File

@@ -25,6 +25,7 @@ import com.google.common.base.Predicate;
import forge.AllZone; import forge.AllZone;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates;
import forge.Singletons; import forge.Singletons;
import forge.card.cardfactory.CardFactoryUtil; import forge.card.cardfactory.CardFactoryUtil;
import forge.card.spellability.AbilityActivated; import forge.card.spellability.AbilityActivated;
@@ -1059,7 +1060,7 @@ public final class AbilityFactoryCombat {
boolean chance = false; boolean chance = false;
if (abTgt != null) { if (abTgt != null) {
CardList list = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield).getType("Creature"); CardList list = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.CREATURES);
list = list.getTargetableCards(sa); list = list.getTargetableCards(sa);
list = list.getValidCards(abTgt.getValidTgts(), source.getController(), source); list = list.getValidCards(abTgt.getValidTgts(), source.getController(), source);
list = list.filter(new Predicate<Card>() { list = list.filter(new Predicate<Card>() {

View File

@@ -27,6 +27,7 @@ import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates;
import forge.Command; import forge.Command;
import forge.Singletons; import forge.Singletons;
import forge.card.cardfactory.CardFactoryUtil; import forge.card.cardfactory.CardFactoryUtil;
@@ -297,8 +298,8 @@ public class AbilityFactoryEffect {
} else if (logic.equals("Always")) { } else if (logic.equals("Always")) {
randomReturn = true; randomReturn = true;
} else if (logic.equals("Evasion")) { } else if (logic.equals("Evasion")) {
CardList comp = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield).getType("Creature"); CardList comp = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.CREATURES);
CardList human = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield).getType("Creature"); CardList human = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.CREATURES);
// only count creatures that can attack or block // only count creatures that can attack or block
comp = comp.filter(new Predicate<Card>() { comp = comp.filter(new Predicate<Card>() {

View File

@@ -23,11 +23,13 @@ import java.util.Iterator;
import java.util.Random; import java.util.Random;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import forge.AllZone; import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates;
import forge.CardPredicates.Presets; import forge.CardPredicates.Presets;
import forge.Singletons; import forge.Singletons;
import forge.card.cardfactory.CardFactoryUtil; import forge.card.cardfactory.CardFactoryUtil;
@@ -1065,16 +1067,8 @@ public class AbilityFactoryPermanentState {
} else if (phase.isPlayerTurn(AllZone.getHumanPlayer()) } else if (phase.isPlayerTurn(AllZone.getHumanPlayer())
&& phase.getPhase().isBefore(PhaseType.COMBAT_DECLARE_ATTACKERS)) { && phase.getPhase().isBefore(PhaseType.COMBAT_DECLARE_ATTACKERS)) {
// Tap creatures possible blockers before combat during AI's turn. // Tap creatures possible blockers before combat during AI's turn.
if (!tapList.getType("Creature").isEmpty()) { if (Iterables.any(tapList, CardPredicates.Presets.CREATURES)) {
CardList creatureList = tapList.filter(new Predicate<Card>() { CardList creatureList = tapList.filter(CardPredicates.Presets.CREATURES_CAN_ATTACK);
@Override
public boolean apply(final Card c) {
if (c.isCreature()) {
return CombatUtil.canAttack(c);
}
return false;
}
});
choice = CardFactoryUtil.getBestCreatureAI(creatureList); choice = CardFactoryUtil.getBestCreatureAI(creatureList);
} else { // no creatures available } else { // no creatures available
choice = CardFactoryUtil.getMostExpensivePermanentAI(tapList, sa, false); choice = CardFactoryUtil.getMostExpensivePermanentAI(tapList, sa, false);

View File

@@ -25,6 +25,7 @@ import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardListUtil; import forge.CardListUtil;
import forge.CardPredicates;
import forge.CardUtil; import forge.CardUtil;
import forge.Singletons; import forge.Singletons;
import forge.card.cardfactory.CardFactoryUtil; import forge.card.cardfactory.CardFactoryUtil;
@@ -379,7 +380,7 @@ public class AbilityFactoryPreventDamage {
if (targetables.size() == 0) { if (targetables.size() == 0) {
return false; return false;
} }
final CardList combatants = targetables.getType("Creature"); final CardList combatants = targetables.filter(CardPredicates.Presets.CREATURES);
CardListUtil.sortByEvaluateCreature(combatants); CardListUtil.sortByEvaluateCreature(combatants);
for (final Card c : combatants) { for (final Card c : combatants) {
@@ -469,7 +470,7 @@ public class AbilityFactoryPreventDamage {
} }
if (compTargetables.size() > 0) { if (compTargetables.size() > 0) {
final CardList combatants = compTargetables.getType("Creature"); final CardList combatants = compTargetables.filter(CardPredicates.Presets.CREATURES);
CardListUtil.sortByEvaluateCreature(combatants); CardListUtil.sortByEvaluateCreature(combatants);
if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) { if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) {
for (final Card c : combatants) { for (final Card c : combatants) {

View File

@@ -26,6 +26,7 @@ import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardListUtil; import forge.CardListUtil;
import forge.CardPredicates;
import forge.Command; import forge.Command;
import forge.Singletons; import forge.Singletons;
import forge.card.cardfactory.CardFactoryUtil; import forge.card.cardfactory.CardFactoryUtil;
@@ -340,7 +341,7 @@ public class AbilityFactoryRegenerate {
} }
} else { } else {
if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) { if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) {
final CardList combatants = targetables.getType("Creature"); final CardList combatants = targetables.filter(CardPredicates.Presets.CREATURES);
CardListUtil.sortByEvaluateCreature(combatants); CardListUtil.sortByEvaluateCreature(combatants);
for (final Card c : combatants) { for (final Card c : combatants) {
@@ -429,7 +430,7 @@ public class AbilityFactoryRegenerate {
} }
if (compTargetables.size() > 0) { if (compTargetables.size() > 0) {
final CardList combatants = compTargetables.getType("Creature"); final CardList combatants = compTargetables.filter(CardPredicates.Presets.CREATURES);
CardListUtil.sortByEvaluateCreature(combatants); CardListUtil.sortByEvaluateCreature(combatants);
if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) { if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) {
for (final Card c : combatants) { for (final Card c : combatants) {
@@ -739,7 +740,7 @@ public class AbilityFactoryRegenerate {
} else { } else {
if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) { if (Singletons.getModel().getGameState().getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY)) {
final CardList combatants = list.getType("Creature"); final CardList combatants = list.filter(CardPredicates.Presets.CREATURES);
for (final Card c : combatants) { for (final Card c : combatants) {
if ((c.getShield() == 0) && CombatUtil.combatantWouldBeDestroyed(c)) { if ((c.getShield() == 0) && CombatUtil.combatantWouldBeDestroyed(c)) {

View File

@@ -2,12 +2,11 @@ package forge.card.cardfactory;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import com.google.common.base.Predicate;
import forge.AllZone; import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates;
import forge.CardPredicates.Presets; import forge.CardPredicates.Presets;
import forge.Command; import forge.Command;
import forge.Counters; import forge.Counters;
@@ -185,12 +184,7 @@ class CardFactoryArtifacts {
} }
} else { } else {
CardList list = AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand); CardList list = AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand);
list = list.filter(new Predicate<Card>() { list = list.filter(CardPredicates.Presets.LANDS);
@Override
public boolean apply(final Card c) {
return (c.isLand());
}
});
AllZone.getComputerPlayer().discard(list.get(0), this); AllZone.getComputerPlayer().discard(list.get(0), this);
} // else } // else
} // resolve() } // resolve()
@@ -223,12 +217,7 @@ class CardFactoryArtifacts {
public boolean canPlay() { public boolean canPlay() {
CardList list = card.getController().getCardsIn(ZoneType.Hand); CardList list = card.getController().getCardsIn(ZoneType.Hand);
list.remove(card); list.remove(card);
list = list.filter(new Predicate<Card>() { list = list.filter(CardPredicates.Presets.LANDS);
@Override
public boolean apply(final Card c) {
return (c.isLand());
}
});
return (list.size() != 0) && super.canPlay(); return (list.size() != 0) && super.canPlay();
} // canPlay() } // canPlay()
}; };
@@ -399,7 +388,7 @@ class CardFactoryArtifacts {
private CardList getComputerLands() { private CardList getComputerLands() {
final CardList list = AllZone.getComputerPlayer().getCardsIn(ZoneType.Graveyard); final CardList list = AllZone.getComputerPlayer().getCardsIn(ZoneType.Graveyard);
return list.getType("Basic"); return list.filter(CardPredicates.Presets.BASIC_LANDS);
} }
} }
final Cost abCost = new Cost(card, "1 T Sac<1/CARDNAME>", true); final Cost abCost = new Cost(card, "1 T Sac<1/CARDNAME>", true);

View File

@@ -57,7 +57,7 @@ import forge.game.zone.PlayerZone;
import forge.game.zone.ZoneType; import forge.game.zone.ZoneType;
import forge.gui.GuiUtils; import forge.gui.GuiUtils;
import forge.gui.match.CMatchUI; import forge.gui.match.CMatchUI;
import forge.util.Aggregates;
import forge.view.ButtonUtil; import forge.view.ButtonUtil;
/** /**
@@ -215,7 +215,7 @@ public class CardFactoryCreatures {
if (card.getController().isHuman()) { if (card.getController().isHuman()) {
final CardList artifacts = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield) final CardList artifacts = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield)
.getType("Artifact"); .filter(CardPredicates.Presets.ARTIFACTS);
if (artifacts.size() != 0) { if (artifacts.size() != 0) {
final Card c = GuiUtils.chooseOne("Select an artifact put a phylactery counter on", artifacts); final Card c = GuiUtils.chooseOne("Select an artifact put a phylactery counter on", artifacts);
@@ -257,8 +257,8 @@ public class CardFactoryCreatures {
@Override @Override
public boolean canPlayAI() { public boolean canPlayAI() {
return (!AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield).getType("Artifact").isEmpty() && AllZone return Iterables.any(AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield), CardPredicates.Presets.ARTIFACTS)
.getZoneOf(this.getSourceCard()).is(ZoneType.Hand)); && AllZone.getZoneOf(this.getSourceCard()).is(ZoneType.Hand);
} }
}); });
card.addComesIntoPlayCommand(intoPlay); card.addComesIntoPlayCommand(intoPlay);
@@ -541,29 +541,18 @@ public class CardFactoryCreatures {
} }
private static final long serialVersionUID = 35050145102566898L; private static final long serialVersionUID = 35050145102566898L;
private final Predicate<Card> untappedCreature = Predicates.and(CardPredicates.Presets.UNTAPPED, CardPredicates.Presets.CREATURES);
@Override @Override
public boolean canPlayAI() { public boolean canPlayAI() {
CardList wolves = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield); List<Card> wolves = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield).getType("Wolf");
wolves = wolves.getType("Wolf"); Iterable<Card> untappedWolves = Iterables.filter(wolves, untappedCreature);
wolves = wolves.filter(new Predicate<Card>() { final int totalPower = Aggregates.sum(untappedWolves, CardPredicates.Accessors.fnGetNetAttack);
@Override if (totalPower == 0) {
public boolean apply(final Card c) {
return c.isUntapped() && c.isCreature();
}
});
int power = 0;
for (int i = 0; i < wolves.size(); i++) {
power += wolves.get(i).getNetAttack();
}
if (power == 0) {
return false; return false;
} }
final int totalPower = power;
CardList targetables = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield); CardList targetables = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield);
targetables = targetables.getTargetableCards(this).filter(new Predicate<Card>() { targetables = targetables.getTargetableCards(this).filter(new Predicate<Card>() {
@@ -585,15 +574,8 @@ public class CardFactoryCreatures {
@Override @Override
public void resolve() { public void resolve() {
CardList wolves = card.getController().getCardsIn(ZoneType.Battlefield); CardList wolves = card.getController().getCardsIn(ZoneType.Battlefield).getType("Wolf");
wolves = wolves.getType("Wolf"); wolves = wolves.filter(untappedCreature);
wolves = wolves.filter(new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return c.isUntapped() && c.isCreature();
}
});
final Card target = this.getTargetCard(); final Card target = this.getTargetCard();

View File

@@ -20,6 +20,7 @@ import forge.game.zone.ZoneType;
import forge.gui.GuiUtils; import forge.gui.GuiUtils;
import forge.gui.match.CMatchUI; import forge.gui.match.CMatchUI;
import forge.view.ButtonUtil; import forge.view.ButtonUtil;
import forge.CardPredicates;
/** /**
* TODO: Write javadoc for this type. * TODO: Write javadoc for this type.
@@ -81,8 +82,8 @@ class CardFactoryEnchantments {
public void showMessage() { public void showMessage() {
CardList grave = AllZone.getHumanPlayer().getCardsIn(ZoneType.Graveyard); CardList grave = AllZone.getHumanPlayer().getCardsIn(ZoneType.Graveyard);
CardList aiGrave = AllZone.getComputerPlayer().getCardsIn(ZoneType.Graveyard); CardList aiGrave = AllZone.getComputerPlayer().getCardsIn(ZoneType.Graveyard);
grave = grave.getType("Creature"); grave = grave.filter(CardPredicates.Presets.CREATURES);
aiGrave = aiGrave.getType("Creature"); aiGrave = aiGrave.filter(CardPredicates.Presets.CREATURES);
if (this.once || ((grave.size() < 2) && (aiGrave.size() < 2))) { if (this.once || ((grave.size() < 2) && (aiGrave.size() < 2))) {
this.once = false; this.once = false;

View File

@@ -17,12 +17,16 @@
*/ */
package forge.card.cardfactory; package forge.card.cardfactory;
import java.util.List;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import forge.AllZone; import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates;
import forge.CardPredicates.Presets; import forge.CardPredicates.Presets;
import forge.Command; import forge.Command;
import forge.Singletons; import forge.Singletons;
@@ -77,9 +81,8 @@ public class CardFactoryInstants {
@Override @Override
public boolean canPlayAI() { public boolean canPlayAI() {
CardList humanArts = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield); List<Card> humanCards = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield);
humanArts = humanArts.getType("Artifact"); return Iterables.any(humanCards, CardPredicates.Presets.ARTIFACTS);
return humanArts.size() > 0;
} //canPlayAI } //canPlayAI
@Override @Override
@@ -91,7 +94,7 @@ public class CardFactoryInstants {
public void resolve() { public void resolve() {
Player player = getTargetPlayer(); Player player = getTargetPlayer();
CardList artifacts = AllZoneUtil.getCardsIn(ZoneType.Battlefield); CardList artifacts = AllZoneUtil.getCardsIn(ZoneType.Battlefield);
artifacts = artifacts.getType("Artifact"); artifacts = artifacts.filter(CardPredicates.Presets.ARTIFACTS);
for (int i = 0; i < artifacts.size(); i++) { for (int i = 0; i < artifacts.size(); i++) {
Card thisArtifact = artifacts.get(i); Card thisArtifact = artifacts.get(i);
@@ -209,9 +212,8 @@ public class CardFactoryInstants {
@Override @Override
public boolean canPlayAI() { public boolean canPlayAI() {
CardList creature = AllZone.getComputerPlayer().getCardsIn(ZoneType.Library); Iterable<Card> creature = Iterables.filter(AllZone.getComputerPlayer().getCardsIn(ZoneType.Library), CardPredicates.Presets.CREATURES);
creature = creature.getType("Creature"); return Iterables.size(creature) >= 3;
return creature.size() >= 3;
} }
}; // SpellAbility }; // SpellAbility

View File

@@ -25,6 +25,7 @@ import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates;
import forge.CardPredicates.Presets; import forge.CardPredicates.Presets;
import forge.Command; import forge.Command;
import forge.Counters; import forge.Counters;
@@ -381,8 +382,7 @@ class CardFactoryLands {
if (this.player.isComputer()) { if (this.player.isComputer()) {
if (plains.size() > 1) { if (plains.size() > 1) {
CardList tappedPlains = new CardList(plains); CardList tappedPlains = plains.filter(CardPredicates.Presets.BASIC_LANDS);
tappedPlains = tappedPlains.getType("Basic");
for (final Card c : tappedPlains) { for (final Card c : tappedPlains) {
Singletons.getModel().getGameAction().sacrifice(c, null); Singletons.getModel().getGameAction().sacrifice(c, null);
} }

View File

@@ -21,11 +21,14 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Vector; import java.util.Vector;
import java.util.List;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import com.esotericsoftware.minlog.Log; import com.esotericsoftware.minlog.Log;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import forge.AllZone; import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
@@ -36,6 +39,7 @@ import forge.CardPredicates;
import forge.CardPredicates.Presets; import forge.CardPredicates.Presets;
import forge.CardUtil; import forge.CardUtil;
import forge.Command; import forge.Command;
import forge.Constant;
import forge.Singletons; import forge.Singletons;
import forge.card.cost.Cost; import forge.card.cost.Cost;
import forge.card.spellability.AbilitySub; import forge.card.spellability.AbilitySub;
@@ -372,9 +376,8 @@ public class CardFactorySorceries {
// figure out which basic land types the computer has // figure out which basic land types the computer has
CardList land = AllZoneUtil.getPlayerLandsInPlay(AllZone.getComputerPlayer()); CardList land = AllZoneUtil.getPlayerLandsInPlay(AllZone.getComputerPlayer());
final String[] basic = { "Forest", "Plains", "Mountain", "Island", "Swamp" };
for (final String element : basic) { for (final String element : Constant.Color.BASIC_LANDS) {
final CardList cl = land.getType(element); final CardList cl = land.getType(element);
if (!cl.isEmpty()) { if (!cl.isEmpty()) {
// remove one land of this basic type from this list // remove one land of this basic type from this list
@@ -505,9 +508,8 @@ public class CardFactorySorceries {
// figure out which basic land types the human has // figure out which basic land types the human has
// put those in an set to use later // put those in an set to use later
final CardList land = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield); final CardList land = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield);
final String[] basic = { "Forest", "Plains", "Mountain", "Island", "Swamp" };
for (final String element : basic) { for (final String element : Constant.Color.BASIC_LANDS) {
final CardList c = land.getType(element); final CardList c = land.getType(element);
if (!c.isEmpty()) { if (!c.isEmpty()) {
humanBasic.add(element); humanBasic.add(element);
@@ -531,6 +533,7 @@ public class CardFactorySorceries {
// *************** START *********** START ************************** // *************** START *********** START **************************
else if (cardName.equals("Haunting Echoes")) { else if (cardName.equals("Haunting Echoes")) {
final Predicate<Card> nonBasicLands = Predicates.not(CardPredicates.Presets.BASIC_LANDS);
final Cost cost = new Cost(card, "3 B B", false); final Cost cost = new Cost(card, "3 B B", false);
final Target tgt = new Target(card, "Select a Player", "Player"); final Target tgt = new Target(card, "Select a Player", "Player");
final SpellAbility spell = new Spell(card, cost, tgt) { final SpellAbility spell = new Spell(card, cost, tgt) {
@@ -540,33 +543,21 @@ public class CardFactorySorceries {
public boolean canPlayAI() { public boolean canPlayAI() {
// Haunting Echoes shouldn't be cast if only basic land in // Haunting Echoes shouldn't be cast if only basic land in
// graveyard or library is empty // graveyard or library is empty
CardList graveyard = AllZone.getHumanPlayer().getCardsIn(ZoneType.Graveyard); final List<Card> graveyard = AllZone.getHumanPlayer().getCardsIn(ZoneType.Graveyard);
final CardList library = AllZone.getHumanPlayer().getCardsIn(ZoneType.Library); final List<Card> library = AllZone.getHumanPlayer().getCardsIn(ZoneType.Library);
final int graveCount = graveyard.size();
graveyard = graveyard.filter(new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return c.isBasicLand();
}
});
this.setTargetPlayer(AllZone.getHumanPlayer()); this.setTargetPlayer(AllZone.getHumanPlayer());
return (((graveCount - graveyard.size()) > 0) && (library.size() > 0)); return Iterables.any(graveyard, nonBasicLands) && !library.isEmpty();
} }
@Override @Override
public void resolve() { public void resolve() {
final Player player = this.getTargetPlayer(); final Player player = this.getTargetPlayer();
CardList grave = player.getCardsIn(ZoneType.Graveyard);
grave = grave.getNotType("Basic");
final CardList lib = player.getCardsIn(ZoneType.Library); final CardList lib = player.getCardsIn(ZoneType.Library);
for (final Card c : grave) { for (final Card c : Iterables.filter(player.getCardsIn(ZoneType.Graveyard), nonBasicLands)) {
final CardList remLib = lib.filter(CardPredicates.nameEquals(c.getName())); for (final Card rem : Iterables.filter(lib, CardPredicates.nameEquals(c.getName()))) {
for (final Card rem : remLib) {
Singletons.getModel().getGameAction().exile(rem); Singletons.getModel().getGameAction().exile(rem);
lib.remove(rem); lib.remove(rem);
} }
@@ -697,7 +688,7 @@ public class CardFactorySorceries {
final CardList humCreats = AllZoneUtil.getCreaturesInPlay(AllZone.getHumanPlayer()); final CardList humCreats = AllZoneUtil.getCreaturesInPlay(AllZone.getHumanPlayer());
CardList compCreats = AllZoneUtil.getCreaturesInPlay(AllZone.getComputerPlayer()); CardList compCreats = AllZoneUtil.getCreaturesInPlay(AllZone.getComputerPlayer());
compCreats = compCreats.getType("Creature"); compCreats = compCreats.filter(CardPredicates.Presets.CREATURES);
diff += 1.5 * (humCreats.size() - compCreats.size()); diff += 1.5 * (humCreats.size() - compCreats.size());
final CardList humHand = AllZone.getHumanPlayer().getCardsIn(ZoneType.Hand); final CardList humHand = AllZone.getHumanPlayer().getCardsIn(ZoneType.Hand);
@@ -724,10 +715,7 @@ public class CardFactorySorceries {
// turn // turn
// is structured, it will already have played land to it's // is structured, it will already have played land to it's
// limit // limit
return Iterables.any(AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand), CardPredicates.Presets.LANDS);
CardList hand = AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand);
hand = hand.getType("Land");
return hand.size() > 0;
} }
@Override @Override
@@ -764,10 +752,7 @@ public class CardFactorySorceries {
// turn // turn
// is structured, it will already have played its first // is structured, it will already have played its first
// land. // land.
CardList list = AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand); return Iterables.any(AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand), CardPredicates.Presets.LANDS);
list = list.getType("Land");
return list.size() > 0;
} }
@Override @Override
@@ -1047,8 +1032,7 @@ public class CardFactorySorceries {
final HashMap<String, Integer> countInGraveyard = new HashMap<String, Integer>(); final HashMap<String, Integer> countInGraveyard = new HashMap<String, Integer>();
final CardList allGrave = AllZone.getComputerPlayer().getCardsIn(ZoneType.Graveyard); final CardList allGrave = AllZone.getComputerPlayer().getCardsIn(ZoneType.Graveyard);
allGrave.getType("Creature"); for (final Card c : Iterables.filter(allGrave, CardPredicates.Presets.CREATURES)) {
for (final Card c : allGrave) {
for (final String type : c.getType()) { for (final String type : c.getType()) {
if (CardUtil.isACreatureType(type)) { if (CardUtil.isACreatureType(type)) {
if (countInGraveyard.containsKey(type)) { if (countInGraveyard.containsKey(type)) {

View File

@@ -27,6 +27,7 @@ import java.util.TreeMap;
import com.esotericsoftware.minlog.Log; import com.esotericsoftware.minlog.Log;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import forge.AllZone; import forge.AllZone;
@@ -40,6 +41,7 @@ import forge.CardPredicates.Presets;
import forge.CardUtil; import forge.CardUtil;
import forge.Command; import forge.Command;
import forge.CommandArgs; import forge.CommandArgs;
import forge.Constant;
import forge.Counters; import forge.Counters;
import forge.GameActionUtil; import forge.GameActionUtil;
import forge.Singletons; import forge.Singletons;
@@ -225,40 +227,32 @@ public class CardFactoryUtil {
* @return a {@link forge.Card} object. * @return a {@link forge.Card} object.
*/ */
public static Card getBestLandAI(final CardList list) { public static Card getBestLandAI(final CardList list) {
final CardList land = list.getType("Land"); final CardList land = list.filter(CardPredicates.Presets.LANDS);
if (!(land.size() > 0)) { if (!(land.size() > 0)) {
return null; return null;
} }
// prefer to target non basic lands // prefer to target non basic lands
final CardList nbLand = land.filter(new Predicate<Card>() { final CardList nbLand = land.filter(Predicates.not(CardPredicates.Presets.BASIC_LANDS));
@Override
public boolean apply(final Card c) {
return (!c.isBasicLand());
}
});
if (nbLand.size() > 0) { if (nbLand.size() > 0) {
// TODO - Rank non basics? // TODO - Rank non basics?
return Aggregates.random(nbLand);
final Random r = MyRandom.getRandom();
return nbLand.get(r.nextInt(nbLand.size()));
} }
// if no non-basic lands, target the least represented basic land type // if no non-basic lands, target the least represented basic land type
final String[] names = { "Plains", "Island", "Swamp", "Mountain", "Forest" };
String sminBL = ""; String sminBL = "";
int iminBL = 20000; // hopefully no one will ever have more than 20000 int iminBL = 20000; // hopefully no one will ever have more than 20000
// lands of one type.... // lands of one type....
int n = 0; int n = 0;
for (int i = 0; i < 5; i++) { for (String name : Constant.Color.BASIC_LANDS) {
n = land.getType(names[i]).size(); n = land.getType(name).size();
if ((n < iminBL) && (n > 0)) { if ((n < iminBL) && (n > 0)) {
// if two or more are tied, only the // if two or more are tied, only the
// first // first
// one checked will be used // one checked will be used
iminBL = n; iminBL = n;
sminBL = names[i]; sminBL = name;
} }
} }
if (iminBL == 20000) { if (iminBL == 20000) {
@@ -266,16 +260,14 @@ public class CardFactoryUtil {
} }
final CardList bLand = land.getType(sminBL); final CardList bLand = land.getType(sminBL);
for (int i = 0; i < bLand.size(); i++) {
if (!bLand.get(i).isTapped()) { for( Card ut : Iterables.filter(bLand, CardPredicates.Presets.UNTAPPED) )
// prefer untapped lands {
return bLand.get(i); return ut;
}
} }
final Random r = MyRandom.getRandom();
return bLand.get(r.nextInt(bLand.size())); // random tapped land of return Aggregates.random(bLand); // random tapped land of least represented type
// least represented type
} }
// The AI doesn't really pick the best enchantment, just the most expensive. // The AI doesn't really pick the best enchantment, just the most expensive.
@@ -293,8 +285,7 @@ public class CardFactoryUtil {
* @return a {@link forge.Card} object. * @return a {@link forge.Card} object.
*/ */
public static Card getBestEnchantmentAI(final CardList list, final SpellAbility spell, final boolean targeted) { public static Card getBestEnchantmentAI(final CardList list, final SpellAbility spell, final boolean targeted) {
CardList all = list; CardList all = list.filter(CardPredicates.Presets.ENCHANTMENTS);
all = all.getType("Enchantment");
if (targeted) { if (targeted) {
all = all.filter(new Predicate<Card>() { all = all.filter(new Predicate<Card>() {
@@ -304,25 +295,9 @@ public class CardFactoryUtil {
} }
}); });
} }
if (all.size() == 0) {
return null;
}
// get biggest Enchantment // get biggest Enchantment
Card biggest = null; return Aggregates.itemWithMax(all, CardPredicates.Accessors.fnGetCmc);
biggest = all.get(0);
int bigCMC = 0;
for (int i = 0; i < all.size(); i++) {
final int curCMC = all.get(i).getManaCost().getCMC();
if (curCMC > bigCMC) {
bigCMC = curCMC;
biggest = all.get(i);
}
}
return biggest;
} }
// The AI doesn't really pick the best artifact, just the most expensive. // The AI doesn't really pick the best artifact, just the most expensive.
@@ -336,27 +311,12 @@ public class CardFactoryUtil {
* @return a {@link forge.Card} object. * @return a {@link forge.Card} object.
*/ */
public static Card getBestArtifactAI(final CardList list) { public static Card getBestArtifactAI(final CardList list) {
CardList all = list; CardList all = list.filter(CardPredicates.Presets.ARTIFACTS);
all = all.getType("Artifact");
if (all.size() == 0) { if (all.size() == 0) {
return null; return null;
} }
// get biggest Artifact // get biggest Artifact
Card biggest = null; return Aggregates.itemWithMax(all, CardPredicates.Accessors.fnGetCmc);
biggest = all.get(0);
int bigCMC = 0;
for (int i = 0; i < all.size(); i++) {
final int curCMC = all.get(i).getManaCost().getCMC();
if (curCMC > bigCMC) {
bigCMC = curCMC;
biggest = all.get(i);
}
}
return biggest;
} }
/** /**
@@ -650,20 +610,8 @@ public class CardFactoryUtil {
* @return the card * @return the card
*/ */
public static Card getBestCreatureAI(final CardList list) { public static Card getBestCreatureAI(final CardList list) {
CardList all = list; CardList all = list.filter(CardPredicates.Presets.CREATURES);
all = all.getType("Creature"); return Aggregates.itemWithMax(all, CardPredicates.Accessors.fnEvaluateCreature);
Card biggest = null;
if (all.size() != 0) {
biggest = all.get(0);
for (int i = 0; i < all.size(); i++) {
if (CardFactoryUtil.evaluateCreature(biggest) < CardFactoryUtil.evaluateCreature(all.get(i))) {
biggest = all.get(i);
}
}
}
return biggest;
} }
// This selection rates tokens higher // This selection rates tokens higher
@@ -678,8 +626,7 @@ public class CardFactoryUtil {
*/ */
public static Card getBestCreatureToBounceAI(final CardList list) { public static Card getBestCreatureToBounceAI(final CardList list) {
final int tokenBonus = 40; final int tokenBonus = 40;
CardList all = list; CardList all = list.filter(CardPredicates.Presets.CREATURES);
all = all.getType("Creature");
Card biggest = null; // returns null if list.size() == 0 Card biggest = null; // returns null if list.size() == 0
int biggestvalue = 0; int biggestvalue = 0;
int newvalue = 0; int newvalue = 0;
@@ -728,21 +675,9 @@ public class CardFactoryUtil {
* @return a {@link forge.Card} object. * @return a {@link forge.Card} object.
*/ */
public static Card getWorstCreatureAI(final CardList list) { public static Card getWorstCreatureAI(final CardList list) {
CardList all = list; CardList all = list.filter(CardPredicates.Presets.CREATURES);
all = all.getType("Creature");
// get smallest creature // get smallest creature
Card smallest = null; return Aggregates.itemWithMin(all, CardPredicates.Accessors.fnEvaluateCreature);
if (all.size() != 0) {
smallest = all.get(0);
for (int i = 0; i < all.size(); i++) {
if (CardFactoryUtil.evaluateCreature(smallest) > CardFactoryUtil.evaluateCreature(all.get(i))) {
smallest = all.get(i);
}
}
}
return smallest;
} }
/** /**
@@ -769,24 +704,25 @@ public class CardFactoryUtil {
} }
System.out.println("getWorstPermanentAI: " + list); System.out.println("getWorstPermanentAI: " + list);
if (biasEnch && (list.getType("Enchantment").size() > 0)) { if (biasEnch && Iterables.any(list, CardPredicates.Presets.ENCHANTMENTS)) {
return CardFactoryUtil.getCheapestPermanentAI(list.getType("Enchantment"), null, false); return CardFactoryUtil.getCheapestPermanentAI(list.filter(CardPredicates.Presets.ENCHANTMENTS), null, false);
} }
if (biasArt && (list.getType("Artifact").size() > 0)) { if (biasArt && Iterables.any(list, CardPredicates.Presets.ARTIFACTS)) {
return CardFactoryUtil.getCheapestPermanentAI(list.getType("Artifact"), null, false); return CardFactoryUtil.getCheapestPermanentAI(list.filter(CardPredicates.Presets.ARTIFACTS), null, false);
} }
if (biasLand && (list.getType("Land").size() > 0)) { if (biasLand && Iterables.any(list, CardPredicates.Presets.LANDS)) {
return CardFactoryUtil.getWorstLand(list.getType("Land")); return CardFactoryUtil.getWorstLand(list.filter(CardPredicates.Presets.LANDS));
} }
if (biasCreature && (list.getType("Creature").size() > 0)) { if (biasCreature && Iterables.any(list, CardPredicates.Presets.CREATURES)) {
return CardFactoryUtil.getWorstCreatureAI(list.getType("Creature")); return CardFactoryUtil.getWorstCreatureAI(list.filter(CardPredicates.Presets.CREATURES));
} }
if (list.getType("Land").size() > 6) { CardList lands = list.filter(CardPredicates.Presets.LANDS);
return CardFactoryUtil.getWorstLand(list.getType("Land")); if (lands.size() > 6) {
return CardFactoryUtil.getWorstLand(lands);
} }
if ((list.getType("Artifact").size() > 0) || (list.getType("Enchantment").size() > 0)) { if ((list.getType("Artifact").size() > 0) || (list.getType("Enchantment").size() > 0)) {
@@ -2540,10 +2476,8 @@ public class CardFactoryUtil {
// Count$Domain // Count$Domain
if (sq[0].contains("Domain")) { if (sq[0].contains("Domain")) {
someCards.addAll(cardController.getCardsIn(ZoneType.Battlefield)); someCards.addAll(cardController.getCardsIn(ZoneType.Battlefield));
final String[] basic = { "Forest", "Plains", "Mountain", "Island", "Swamp" }; for (String basic : Constant.Color.BASIC_LANDS) {
if (!someCards.getType(basic).isEmpty()) {
for (int i = 0; i < basic.length; i++) {
if (!someCards.getType(basic[i]).isEmpty()) {
n++; n++;
} }
} }
@@ -2553,10 +2487,8 @@ public class CardFactoryUtil {
// Count$OpponentDom // Count$OpponentDom
if (sq[0].contains("OpponentDom")) { if (sq[0].contains("OpponentDom")) {
someCards.addAll(cardController.getOpponent().getCardsIn(ZoneType.Battlefield)); someCards.addAll(cardController.getOpponent().getCardsIn(ZoneType.Battlefield));
final String[] basic = { "Forest", "Plains", "Mountain", "Island", "Swamp" }; for (String basic : Constant.Color.BASIC_LANDS) {
if (!someCards.getType(basic).isEmpty()) {
for (int i = 0; i < basic.length; i++) {
if (!someCards.getType(basic[i]).isEmpty()) {
n++; n++;
} }
} }
@@ -3702,38 +3634,14 @@ public class CardFactoryUtil {
*/ */
public static Card getWorstLand(final CardList lands) { public static Card getWorstLand(final CardList lands) {
Card worstLand = null; Card worstLand = null;
int maxScore = 0;
// first, check for tapped, basic lands // first, check for tapped, basic lands
for (int i = 0; i < lands.size(); i++) { for (Card tmp : lands) {
final Card tmp = lands.get(i); int score = tmp.isTapped() ? 2 : 0;
if (tmp.isTapped() && tmp.isBasicLand()) { score += tmp.isBasicLand() ? 1 : 0;
if( score >= maxScore ) {
worstLand = tmp; worstLand = tmp;
} maxScore = score;
}
// next, check for tapped, non-basic lands
if (worstLand == null) {
for (int i = 0; i < lands.size(); i++) {
final Card tmp = lands.get(i);
if (tmp.isTapped()) {
worstLand = tmp;
}
}
}
// next, untapped, basic lands
if (worstLand == null) {
for (int i = 0; i < lands.size(); i++) {
final Card tmp = lands.get(i);
if (tmp.isUntapped() && tmp.isBasicLand()) {
worstLand = tmp;
}
}
}
// next, untapped, non-basic lands
if (worstLand == null) {
for (int i = 0; i < lands.size(); i++) {
final Card tmp = lands.get(i);
if (tmp.isUntapped()) {
worstLand = tmp;
}
} }
} }
return worstLand; return worstLand;

View File

@@ -120,17 +120,6 @@ public class Mana {
return this.pumpCounterMagic; return this.pumpCounterMagic;
} }
/**
* <p>
* fromBasicLand.
* </p>
*
* @return a boolean.
*/
public final boolean fromBasicLand() {
return this.sourceCard.isBasicLand();
} // for Imperiosaur
/** /**
* <p> * <p>
* isColor. * isColor.

View File

@@ -18,6 +18,7 @@
package forge.card.spellability; package forge.card.spellability;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@@ -355,10 +356,11 @@ public class SpellPermanent extends Spell {
} }
if (card.isPlaneswalker()) { if (card.isPlaneswalker()) {
CardList list = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield); CardList list = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield);
list = list.getType("Planeswalker"); list = list.filter(CardPredicates.Presets.PLANEWALKERS);
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
final String subtype = card.getType().get(card.getType().size() - 1); List<String> type = card.getType();
final String subtype = type.get(type.size() - 1);
final CardList cl = list.getType(subtype); final CardList cl = list.getType(subtype);
if (cl.size() > 0) { if (cl.size() > 0) {

View File

@@ -17,9 +17,12 @@
*/ */
package forge.control.input; package forge.control.input;
import com.google.common.collect.Iterables;
import forge.AllZone; import forge.AllZone;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates;
import forge.Singletons; import forge.Singletons;
import forge.game.phase.CombatUtil; import forge.game.phase.CombatUtil;
import forge.game.zone.PlayerZone; import forge.game.zone.PlayerZone;
@@ -62,9 +65,7 @@ public class InputAttack extends Input {
if (AllZone.getCombat().getRemainingDefenders() == 0) { if (AllZone.getCombat().getRemainingDefenders() == 0) {
// Nothing left to attack, has to attack this defender // Nothing left to attack, has to attack this defender
CardList possibleAttackers = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield); CardList possibleAttackers = AllZone.getHumanPlayer().getCardsIn(ZoneType.Battlefield);
possibleAttackers = possibleAttackers.getType("Creature"); for (Card c : Iterables.filter(possibleAttackers, CardPredicates.Presets.CREATURES)) {
for (int i = 0; i < possibleAttackers.size(); i++) {
final Card c = possibleAttackers.get(i);
if (c.hasKeyword("CARDNAME attacks each turn if able.") && CombatUtil.canAttack(c, AllZone.getCombat()) if (c.hasKeyword("CARDNAME attacks each turn if able.") && CombatUtil.canAttack(c, AllZone.getCombat())
&& !c.isAttacking()) { && !c.isAttacking()) {
AllZone.getCombat().addAttacker(c); AllZone.getCombat().addAttacker(c);

View File

@@ -7,10 +7,15 @@ import java.util.Random;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import forge.AllZone; import forge.AllZone;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates.Presets; import forge.CardPredicates.Presets;
import forge.CardPredicates;
import forge.CardUtil; import forge.CardUtil;
import forge.Constant; import forge.Constant;
import forge.GameAction; import forge.GameAction;
@@ -31,6 +36,7 @@ import forge.item.CardPrinted;
import forge.properties.ForgePreferences.FPref; import forge.properties.ForgePreferences.FPref;
import forge.properties.ForgeProps; import forge.properties.ForgeProps;
import forge.properties.NewConstants.Lang.GameAction.GameActionText; import forge.properties.NewConstants.Lang.GameAction.GameActionText;
import forge.util.Aggregates;
import forge.util.MyRandom; import forge.util.MyRandom;
/** /**
@@ -268,16 +274,13 @@ public class GameNew {
final String nl = System.getProperty("line.separator"); final String nl = System.getProperty("line.separator");
final StringBuilder msg = new StringBuilder(); final StringBuilder msg = new StringBuilder();
for (final Player p : AllZone.getPlayersInGame()) { for (final Player p : AllZone.getPlayersInGame()) {
final CardList lib = p.getCardsIn(ZoneType.Library); final List<Card> lib = p.getCardsIn(ZoneType.Library);
Card ante; Predicate<Card> goodForAnte = Predicates.not(CardPredicates.Presets.BASIC_LANDS);
if ((lib.size() > 0) && (lib.getNotType("Basic").size() > 1)) { Card ante = Aggregates.random(Iterables.filter(lib, goodForAnte));
ante = CardUtil.getRandom(lib); if (ante == null ) {
while (ante.isBasicLand()) { if (!lib.isEmpty())
ante = CardUtil.getRandom(lib);
}
} else if (lib.size() > 1) {
ante = lib.get(0); ante = lib.get(0);
} else { else
throw new RuntimeException(p + " library is empty."); throw new RuntimeException(p + " library is empty.");
} }
AllZone.getGameLog().add("Ante", p + " anted " + ante, 0); AllZone.getGameLog().add("Ante", p + " anted " + ante, 0);
@@ -375,10 +378,10 @@ public class GameNew {
library.shuffle(); library.shuffle();
// remove all land, keep non-basicland in there, shuffled // remove all land, keep non-basicland in there, shuffled
CardList land = library.getType("Land"); CardList land = library.filter(CardPredicates.Presets.LANDS);
for (int i = 0; i < land.size(); i++) { for (Card c : land) {
if (land.get(i).isLand()) { if (c.isLand()) {
library.remove(land.get(i)); library.remove(c);
} }
} }
@@ -444,10 +447,10 @@ public class GameNew {
*/ */
private static void seeWhoPlaysFirst() { private static void seeWhoPlaysFirst() {
final GameAction ga = Singletons.getModel().getGameAction(); final GameAction ga = Singletons.getModel().getGameAction();
CardList hLibrary = AllZone.getHumanPlayer().getCardsIn(ZoneType.Library); Predicate<Card> nonLand = Predicates.not(Presets.LANDS);
hLibrary = hLibrary.filter(Presets.NON_LANDS); Iterable<Card> hLibrary = Iterables.filter(AllZone.getHumanPlayer().getCardsIn(ZoneType.Library), nonLand);
CardList cLibrary = AllZone.getComputerPlayer().getCardsIn(ZoneType.Library); Iterable<Card> cLibrary = Iterables.filter(AllZone.getComputerPlayer().getCardsIn(ZoneType.Library), nonLand);
cLibrary = cLibrary.filter(Presets.NON_LANDS);
final boolean starterDetermined = false; final boolean starterDetermined = false;
int cutCount = 0; int cutCount = 0;
@@ -457,8 +460,9 @@ public class GameNew {
break; break;
} }
if (hLibrary.size() > 0) { Card hRandom = Aggregates.random(hLibrary);
ga.setHumanCut(hLibrary.get(MyRandom.getRandom().nextInt(hLibrary.size()))); if ( null != hRandom ) {
ga.setHumanCut(hRandom);
} else { } else {
GameNew.computerStartsGame(); GameNew.computerStartsGame();
JOptionPane.showMessageDialog(null, ForgeProps.getLocalized(GameActionText.HUMAN_MANA_COST) + "\r\n" JOptionPane.showMessageDialog(null, ForgeProps.getLocalized(GameActionText.HUMAN_MANA_COST) + "\r\n"
@@ -466,8 +470,9 @@ public class GameNew {
return; return;
} }
if (cLibrary.size() > 0) { Card cRandom = Aggregates.random(cLibrary);
ga.setComputerCut(cLibrary.get(MyRandom.getRandom().nextInt(cLibrary.size()))); if (cRandom != null) {
ga.setComputerCut(cRandom);
} else { } else {
JOptionPane.showMessageDialog(null, ForgeProps.getLocalized(GameActionText.COMPUTER_MANA_COST) + "\r\n" JOptionPane.showMessageDialog(null, ForgeProps.getLocalized(GameActionText.COMPUTER_MANA_COST) + "\r\n"
+ ForgeProps.getLocalized(GameActionText.HUMAN_STARTS), "", JOptionPane.INFORMATION_MESSAGE); + ForgeProps.getLocalized(GameActionText.HUMAN_STARTS), "", JOptionPane.INFORMATION_MESSAGE);

View File

@@ -30,6 +30,7 @@ import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardPredicates;
import forge.GameActionUtil; import forge.GameActionUtil;
import forge.GameEntity; import forge.GameEntity;
import forge.Singletons; import forge.Singletons;
@@ -111,7 +112,7 @@ public class Combat {
this.defenders.add((GameEntity) defender); this.defenders.add((GameEntity) defender);
this.defenderMap.put((GameEntity) defender, new CardList()); this.defenderMap.put((GameEntity) defender, new CardList());
CardList planeswalkers = defender.getCardsIn(ZoneType.Battlefield); CardList planeswalkers = defender.getCardsIn(ZoneType.Battlefield);
planeswalkers = planeswalkers.getType("Planeswalker"); planeswalkers = planeswalkers.filter(CardPredicates.Presets.PLANEWALKERS);
for (final Card pw : planeswalkers) { for (final Card pw : planeswalkers) {
this.defenders.add((GameEntity) pw); this.defenders.add((GameEntity) pw);
this.defenderMap.put((GameEntity) pw, new CardList()); this.defenderMap.put((GameEntity) pw, new CardList());

View File

@@ -33,6 +33,7 @@ import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardListUtil; import forge.CardListUtil;
import forge.CardPredicates;
import forge.Command; import forge.Command;
import forge.Constant; import forge.Constant;
import forge.Counters; import forge.Counters;
@@ -400,7 +401,7 @@ public class CombatUtil {
boolean must = true; boolean must = true;
if (attacker.hasKeyword("CARDNAME can't be blocked except by two or more creatures.")) { if (attacker.hasKeyword("CARDNAME can't be blocked except by two or more creatures.")) {
final CardList possibleBlockers = combat.getDefendingPlayer().getCardsIn(ZoneType.Battlefield) final CardList possibleBlockers = combat.getDefendingPlayer().getCardsIn(ZoneType.Battlefield)
.getType("Creature"); .filter(CardPredicates.Presets.CREATURES);
possibleBlockers.remove(blocker); possibleBlockers.remove(blocker);
if (!CombatUtil.canBeBlocked(attacker, possibleBlockers)) { if (!CombatUtil.canBeBlocked(attacker, possibleBlockers)) {
must = false; must = false;
@@ -524,7 +525,7 @@ public class CombatUtil {
boolean canBe = true; boolean canBe = true;
if (attacker.hasKeyword("CARDNAME can't be blocked except by two or more creatures.")) { if (attacker.hasKeyword("CARDNAME can't be blocked except by two or more creatures.")) {
final CardList blockers = combat.getDefendingPlayer().getCardsIn(ZoneType.Battlefield) final CardList blockers = combat.getDefendingPlayer().getCardsIn(ZoneType.Battlefield)
.getType("Creature"); .filter(CardPredicates.Presets.CREATURES);
blockers.remove(blocker); blockers.remove(blocker);
if (!CombatUtil.canBeBlocked(attacker, blockers)) { if (!CombatUtil.canBeBlocked(attacker, blockers)) {
canBe = false; canBe = false;
@@ -542,7 +543,7 @@ public class CombatUtil {
boolean canBe = true; boolean canBe = true;
if (attacker.hasKeyword("CARDNAME can't be blocked except by two or more creatures.")) { if (attacker.hasKeyword("CARDNAME can't be blocked except by two or more creatures.")) {
final CardList blockers = combat.getDefendingPlayer().getCardsIn(ZoneType.Battlefield) final CardList blockers = combat.getDefendingPlayer().getCardsIn(ZoneType.Battlefield)
.getType("Creature"); .filter(CardPredicates.Presets.CREATURES);
blockers.remove(blocker); blockers.remove(blocker);
if (!CombatUtil.canBeBlocked(attacker, blockers)) { if (!CombatUtil.canBeBlocked(attacker, blockers)) {
canBe = false; canBe = false;
@@ -929,12 +930,7 @@ public class CombatUtil {
return false; return false;
} }
} else if (keyword.equals("CARDNAME can't attack unless defending player controls a snow land.")) { } else if (keyword.equals("CARDNAME can't attack unless defending player controls a snow land.")) {
temp = list.filter(new Predicate<Card>() { temp = list.filter(CardPredicates.Presets.SNOW_LANDS);
@Override
public boolean apply(final Card c) {
return c.isLand() && c.isSnow();
}
});
if (temp.isEmpty()) { if (temp.isEmpty()) {
return false; return false;
} }

View File

@@ -831,7 +831,7 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable {
// resets the status of attacked/blocked this phase // resets the status of attacked/blocked this phase
CardList list = player.getCardsIn(ZoneType.Battlefield); CardList list = player.getCardsIn(ZoneType.Battlefield);
list = list.getType("Creature"); list = list.filter(CardPredicates.Presets.CREATURES);
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
final Card c = list.get(i); final Card c = list.get(i);

View File

@@ -1959,9 +1959,8 @@ public class Upkeep extends Phase implements java.io.Serializable {
oathFlag = false; oathFlag = false;
} }
} else { // if player == Computer } else { // if player == Computer
final CardList creaturesInLibrary = player.getCardsIn(ZoneType.Library).getType("Creature"); final CardList creaturesInLibrary = player.getCardsIn(ZoneType.Library).filter(CardPredicates.Presets.CREATURES);
final CardList creaturesInBattlefield = player.getCardsIn(ZoneType.Battlefield).getType( final CardList creaturesInBattlefield = player.getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.CREATURES);
"Creature");
// if there are at least 3 creatures in library, // if there are at least 3 creatures in library,
// or none in play with one in library, oath // or none in play with one in library, oath

View File

@@ -19,6 +19,8 @@ package forge.game.player;
import java.util.Random; import java.util.Random;
import com.google.common.collect.Iterables;
import forge.AllZone; import forge.AllZone;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
@@ -201,9 +203,9 @@ public class AIPlayer extends Player {
boolean bottom = false; boolean bottom = false;
if (topN.get(i).isBasicLand()) { if (topN.get(i).isBasicLand()) {
CardList bl = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield); CardList bl = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield);
bl = bl.filter(CardPredicates.Presets.BASIC_LANDS); int nBasicLands = Iterables.size(Iterables.filter(bl, CardPredicates.Presets.BASIC_LANDS));
bottom = bl.size() > 5; // if control more than 5 Basic land, bottom = nBasicLands > 5; // if control more than 5 Basic land,
// probably don't need more // probably don't need more
} else if (topN.get(i).isCreature()) { } else if (topN.get(i).isCreature()) {
CardList cl = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield); CardList cl = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield);

View File

@@ -21,8 +21,10 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import forge.AllZone; import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
@@ -32,6 +34,7 @@ import forge.CardListUtil;
import forge.CardPredicates; import forge.CardPredicates;
import forge.CardPredicates.Presets; import forge.CardPredicates.Presets;
import forge.CardUtil; import forge.CardUtil;
import forge.Constant;
import forge.GameActionUtil; import forge.GameActionUtil;
import forge.Singletons; import forge.Singletons;
import forge.card.abilityfactory.AbilityFactory; import forge.card.abilityfactory.AbilityFactory;
@@ -1386,15 +1389,14 @@ public class ComputerUtil {
Card land = landList.get(ix); Card land = landList.get(ix);
//play basic lands that are needed the most //play basic lands that are needed the most
if (landList.getNotType("Basic").isEmpty()) { if (!Iterables.any(landList, CardPredicates.Presets.BASIC_LANDS)) {
final CardList combined = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield); final CardList combined = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield);
final String[] names = "Plains,Island,Swamp,Mountain,Forest".split(",");
final ArrayList<String> basics = new ArrayList<String>(); final ArrayList<String> basics = new ArrayList<String>();
// what types can I go get? // what types can I go get?
for (final String name : names) { for (final String name : Constant.CardTypes.BASIC_TYPES) {
if (landList.getType(name).size() != 0) { if (!landList.getType(name).isEmpty()) {
basics.add(name); basics.add(name);
} }
} }
@@ -1611,8 +1613,9 @@ public class ComputerUtil {
if (hand.size() <= 0) { if (hand.size() <= 0) {
continue; continue;
} }
final int numLandsInPlay = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield).getType("Land").size(); List<Card> aiCards = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield);
final CardList landsInHand = hand.getType("Land"); final int numLandsInPlay = Iterables.size(Iterables.filter(aiCards, CardPredicates.Presets.LANDS));
final CardList landsInHand = hand.filter(CardPredicates.Presets.LANDS);
final int numLandsInHand = landsInHand.size(); final int numLandsInHand = landsInHand.size();
// Discard a land // Discard a land
@@ -2257,8 +2260,8 @@ public class ComputerUtil {
if (!discard.getSVar("DiscardMe").equals("")) { if (!discard.getSVar("DiscardMe").equals("")) {
return true; return true;
} }
final CardList landsInPlay = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield).getType("Land"); final CardList landsInPlay = AllZone.getComputerPlayer().getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.LANDS);
final CardList landsInHand = AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand).getType("Land"); final CardList landsInHand = AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand).filter(CardPredicates.Presets.LANDS);
final CardList nonLandsInHand = AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand).getNotType("Land"); final CardList nonLandsInHand = AllZone.getComputerPlayer().getCardsIn(ZoneType.Hand).getNotType("Land");
final int highestCMC = Math.max(6, Aggregates.max(nonLandsInHand, CardPredicates.Accessors.fnGetCmc)); final int highestCMC = Math.max(6, Aggregates.max(nonLandsInHand, CardPredicates.Accessors.fnGetCmc));
final int discardCMC = discard.getCMC(); final int discardCMC = discard.getCMC();

View File

@@ -27,6 +27,7 @@ import forge.AllZoneUtil;
import forge.Card; import forge.Card;
import forge.CardList; import forge.CardList;
import forge.CardListUtil; import forge.CardListUtil;
import forge.CardPredicates;
import forge.Counters; import forge.Counters;
import forge.GameEntity; import forge.GameEntity;
import forge.Singletons; import forge.Singletons;
@@ -75,10 +76,10 @@ public class ComputerUtilAttack {
*/ */
public ComputerUtilAttack(final CardList possibleAttackers, final CardList possibleBlockers) { public ComputerUtilAttack(final CardList possibleAttackers, final CardList possibleBlockers) {
this.humanList = new CardList(possibleBlockers); this.humanList = new CardList(possibleBlockers);
this.humanList = this.humanList.getType("Creature"); this.humanList = this.humanList.filter(CardPredicates.Presets.CREATURES);
this.computerList = new CardList(possibleAttackers); this.computerList = new CardList(possibleAttackers);
this.computerList = this.computerList.getType("Creature"); this.computerList = this.computerList.filter(CardPredicates.Presets.CREATURES);
this.attackers = this.getPossibleAttackers(possibleAttackers); this.attackers = this.getPossibleAttackers(possibleAttackers);
this.blockers = this.getPossibleBlockers(possibleBlockers, this.attackers); this.blockers = this.getPossibleBlockers(possibleBlockers, this.attackers);

View File

@@ -28,6 +28,8 @@ import java.util.Random;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import com.google.common.collect.Iterables;
import forge.AllZone; import forge.AllZone;
import forge.AllZoneUtil; import forge.AllZoneUtil;
import forge.Card; import forge.Card;
@@ -1886,7 +1888,7 @@ public abstract class Player extends GameEntity implements Comparable<Player> {
* @return a {@link forge.Card} object. * @return a {@link forge.Card} object.
*/ */
public final Card getPlaneswalker() { public final Card getPlaneswalker() {
final CardList c = this.getCardsIn(ZoneType.Battlefield).getType("Planeswalker"); final CardList c = this.getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.PLANEWALKERS);
if ((null != c) && (c.size() > 0)) { if ((null != c) && (c.size() > 0)) {
return c.get(0); return c.get(0);
} else { } else {
@@ -2284,7 +2286,7 @@ public abstract class Player extends GameEntity implements Comparable<Player> {
* @return a boolean. * @return a boolean.
*/ */
public final boolean hasMetalcraft() { public final boolean hasMetalcraft() {
final CardList list = this.getCardsIn(ZoneType.Battlefield).getType("Artifact"); final CardList list = this.getCardsIn(ZoneType.Battlefield).filter(CardPredicates.Presets.ARTIFACTS);
return list.size() >= 3; return list.size() >= 3;
} }
@@ -2318,9 +2320,8 @@ public abstract class Player extends GameEntity implements Comparable<Player> {
* @return a boolean. * @return a boolean.
*/ */
public final boolean hasLandfall() { public final boolean hasLandfall() {
final CardList list = ((DefaultPlayerZone) this.getZone(ZoneType.Battlefield)).getCardsAddedThisTurn(null).getType( final List<Card> list = ((DefaultPlayerZone) this.getZone(ZoneType.Battlefield)).getCardsAddedThisTurn(null);
"Land"); return Iterables.any(list, CardPredicates.Presets.LANDS);
return !list.isEmpty();
} }
/** /**

View File

@@ -26,6 +26,20 @@ public class Aggregates {
return max; return max;
} }
public final static <T> T itemWithMax(final Iterable<T> source, final Function<T, Integer> valueAccessor) {
if (source == null) { return null; }
int max = Integer.MIN_VALUE;
T result = null;
for (final T c : source) {
int value = valueAccessor.apply(c);
if ( value > max ) {
max = value;
result = c;
}
}
return result;
}
public final static <T> int sum(final Iterable<T> source, final Function<T, Integer> valueAccessor) { public final static <T> int sum(final Iterable<T> source, final Function<T, Integer> valueAccessor) {
int result = 0; int result = 0;
if (source != null) { if (source != null) {
@@ -58,15 +72,6 @@ public class Aggregates {
// Get several random values // Get several random values
// should improve to make 1 pass over source and track N candidates at once // should improve to make 1 pass over source and track N candidates at once
/**
* Random.
*
* @param source
* the source
* @param count
* the count
* @return the list
*/
public final static <T> List<T> random(final Iterable<T> source, final int count) { public final static <T> List<T> random(final Iterable<T> source, final int count) {
final List<T> result = new ArrayList<T>(); final List<T> result = new ArrayList<T>();
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
@@ -79,21 +84,6 @@ public class Aggregates {
return result; return result;
} }
/**
* Unique by last.
*
* @param <K>
* the key type
* @param <U>
* the generic type
* @param source
* the source
* @param fnUniqueKey
* the fn unique key
* @param accessor
* the accessor
* @return the iterable
*/
public static final <K, U> Iterable<U> uniqueByLast(final Iterable<U> source, final Function<U, K> fnUniqueKey) { // this might be exotic public static final <K, U> Iterable<U> uniqueByLast(final Iterable<U> source, final Function<U, K> fnUniqueKey) { // this might be exotic
final Map<K, U> uniques = new Hashtable<K, U>(); final Map<K, U> uniques = new Hashtable<K, U>();
for (final U c : source) { for (final U c : source) {
@@ -102,4 +92,19 @@ public class Aggregates {
return uniques.values(); return uniques.values();
} }
public static <T> T itemWithMin(final Iterable<T> source, final Function<T, Integer> valueAccessor) {
if (source == null) { return null; }
int max = Integer.MAX_VALUE;
T result = null;
for (final T c : source) {
int value = valueAccessor.apply(c);
if ( value < max ) {
max = value;
result = c;
}
}
return result;
}
} }