CardLists: removed duplicate sortByMostExpensive, gave propper names to methods of CardLists

This commit is contained in:
Maxmtg
2013-02-27 10:03:25 +00:00
parent f6769cf62b
commit 1d5f1a49f0
13 changed files with 51 additions and 101 deletions

View File

@@ -62,26 +62,26 @@ public class CardLists {
});
}
public static final Comparator<Card> DefenseComparator = new Comparator<Card>() {
public static final Comparator<Card> ToughnessComparator = new Comparator<Card>() {
@Override
public int compare(final Card a, final Card b) {
return b.getNetDefense() - a.getNetDefense();
return a.getNetDefense() - b.getNetDefense();
}
};
public static final Comparator<Card> AttackComparator = new Comparator<Card>() {
public static final Comparator<Card> PowerComparator = new Comparator<Card>() {
@Override
public int compare(final Card a, final Card b) {
return b.getNetCombatDamage() - a.getNetCombatDamage();
return a.getNetCombatDamage() - b.getNetCombatDamage();
}
};
public static final Comparator<Card> CmcComparator = new Comparator<Card>() {
public static final Comparator<Card> CmcComparatorInv = new Comparator<Card>() {
@Override
public int compare(final Card a, final Card b) {
return b.getCMC() - a.getCMC();
}
};
public static final Comparator<Card> TextLenReverseComparator = new Comparator<Card>() {
public static final Comparator<Card> TextLenComparator = new Comparator<Card>() {
@Override
public int compare(final Card a, final Card b) {
final int aLen = a.getText().length();
@@ -90,30 +90,6 @@ public class CardLists {
}
};
public static final Comparator<Card> getKeywordComparator(final String kw) {
return new Comparator<Card>() {
@Override
public int compare(final Card a, final Card b) {
int aV = a.hasKeyword(kw) ? 1 : 0;
int bV = b.hasKeyword(kw) ? 1 : 0;
return bV - aV;
}
};
}
// the higher the attack the better
/**
* <p>
* sortAttack.
* </p>
*
* @param list
* a {@link forge.CardList} object.
*/
public static void sortAttack(final List<Card> list) {
Collections.sort(list, AttackComparator);
} // sortAttack()
/**
* <p>
* Sorts a List<Card> by "best" using the EvaluateCreature function.
@@ -129,17 +105,16 @@ public class CardLists {
/**
* <p>
* Sorts a List<Card> by converted mana cost, putting highest first.
* Sorts a List<Card> from highest converted mana cost to lowest.
* </p>
*
* @param list
* a {@link forge.CardList} object.
*/
public static void sortByMostExpensive(final List<Card> list) {
Collections.sort(list, CmcComparator);
} // sortByMostExpensive()
public static void sortByCmcDesc(final List<Card> list) {
Collections.sort(list, CmcComparatorInv);
} // sortCMC
// the lower the attack the better
/**
* <p>
* sortAttackLowFirst.
@@ -148,46 +123,24 @@ public class CardLists {
* @param list
* a {@link forge.CardList} object.
*/
public static void sortAttackLowFirst(final List<Card> list) {
Collections.sort(list, Collections.reverseOrder(AttackComparator));
public static void sortByPowerAsc(final List<Card> list) {
Collections.sort(list, PowerComparator);
} // sortAttackLowFirst()
// the higher the attack the better
/**
* <p>
* Sorts a CardList, putting creatures without Flying first.
* sortAttack.
* </p>
*
* @param list
* a {@link forge.CardList} object.
*/
public static void sortNonFlyingFirst(final List<Card> list) {
CardLists.sortFlying(list);
Collections.reverse(list);
} // sortNonFlyingFirst
public static void sortByPowerDesc(final List<Card> list) {
Collections.sort(list, Collections.reverseOrder(PowerComparator));
} // sortAttack()
/**
* <p>
* Sorts a CardList, putting creatures with Flying first.
* </p>
*
* @param list
* a {@link forge.CardList} object.
*/
public static void sortFlying(final List<Card> list) {
Collections.sort(list, getKeywordComparator("Flying"));
} // sortFlying()
/**
* <p>
* Sorts a List<Card> from highest converted mana cost to lowest.
* </p>
*
* @param list
* a {@link forge.CardList} object.
*/
public static void sortCMC(final List<Card> list) {
Collections.sort(list, CmcComparator);
} // sortCMC
/**
* <p>

View File

@@ -860,7 +860,7 @@ public class ChangeZoneAi extends SpellAbilityAi {
if (ai.getLife() <= 5) { // Desperate?
// Get something AI can cast soon.
System.out.println("5 Life or less, trying to find something castable.");
CardLists.sortByMostExpensive(nonLands);
CardLists.sortByCmcDesc(nonLands);
for (Card potentialCard : nonLands) {
if (ComputerUtilMana.payManaCost(potentialCard.getFirstSpellAbility(), ai, true, 0, false)) {
choice = potentialCard;
@@ -971,7 +971,7 @@ public class ChangeZoneAi extends SpellAbilityAi {
if (ai.getLife() <= 5) { // Desperate?
// Get something AI can cast soon.
System.out.println("5 Life or less, trying to find something castable.");
CardLists.sortByMostExpensive(nonLands);
CardLists.sortByCmcDesc(nonLands);
for (Card potentialCard : nonLands) {
if (ComputerUtilMana.payManaCost(potentialCard.getFirstSpellAbility(), ai, true, 0, false)) {
choice = potentialCard;
@@ -1212,7 +1212,7 @@ public class ChangeZoneAi extends SpellAbilityAi {
if (ai.getLife() <= 5) { // Desperate?
// Get something AI can cast soon.
System.out.println("5 Life or less, trying to find something castable.");
CardLists.sortByMostExpensive(fetchList);
CardLists.sortByCmcDesc(fetchList);
for (Card potentialCard : fetchList) {
if (ComputerUtilMana.payManaCost(potentialCard.getFirstSpellAbility(), ai, true, 0, false)) {
c = potentialCard;

View File

@@ -184,7 +184,7 @@ public class PumpAi extends PumpAiBase {
if (sa.getParam("AILogic").equals("HighestPower")) {
list = CardLists.getValidCards(CardLists.filter(Singletons.getModel().getGame().getCardsIn(ZoneType.Battlefield), Presets.CREATURES), tgt.getValidTgts(), sa.getActivatingPlayer(), sa.getSourceCard());
list = CardLists.getTargetableCards(list, sa);
CardLists.sortAttack(list);
CardLists.sortByPowerDesc(list);
if (!list.isEmpty()) {
tgt.addTarget(list.get(0));
return true;

View File

@@ -213,7 +213,7 @@ public class RegenerateAi extends SpellAbilityAi {
tgt.addTarget(combatants.get(0));
return true;
} else {
CardLists.sortByMostExpensive(compTargetables);
CardLists.sortByCmcDesc(compTargetables);
for (final Card c : compTargetables) {
if (c.getShield() == 0) {
tgt.addTarget(c);

View File

@@ -117,9 +117,9 @@ public class DiscardEffect extends RevealEffectBase {
}
}
Collections.sort(goodChoices, CardLists.TextLenReverseComparator);
Collections.sort(goodChoices, CardLists.TextLenComparator);
CardLists.sortCMC(goodChoices);
CardLists.sortByCmcDesc(goodChoices);
dChoices.add(goodChoices.get(0));
return Aggregates.random(goodChoices);

View File

@@ -505,8 +505,8 @@ public class CardFactorySorceries {
continue;
}
if (p.isComputer()) {
CardLists.sortAttackLowFirst(c);
CardLists.sortCMC(c);
CardLists.sortByPowerAsc(c);
CardLists.sortByCmcDesc(c);
Collections.reverse(c);
for (int i = 0; i < sac; i++) {
Singletons.getModel().getGame().getAction().sacrifice(c.get(i), card);

View File

@@ -1237,7 +1237,7 @@ public class GameAction {
return crd.hasKeyword("Totem armor");
}
});
CardLists.sortCMC(list);
CardLists.sortByCmcDesc(list);
if (list.size() != 0) {
final Card crd;

View File

@@ -253,7 +253,7 @@ public class AiAttackController {
}
}
}
CardLists.sortAttackLowFirst(attackers);
CardLists.sortByPowerAsc(attackers);
int blockersNeeded = this.oppList.size();
// don't hold back creatures that can't block any of the human creatures
@@ -382,7 +382,7 @@ public class AiAttackController {
}
}
CardLists.sortAttack(this.attackers);
CardLists.sortByPowerDesc(this.attackers);
final List<Card> unblockedAttackers = new ArrayList<Card>();
final List<Card> remainingAttackers = new ArrayList<Card>(this.attackers);
@@ -531,7 +531,7 @@ public class AiAttackController {
}
if (bAssault) {
System.out.println("Assault");
CardLists.sortAttack(attackersLeft);
CardLists.sortByPowerDesc(attackersLeft);
for (Card attacker : attackersLeft) {
if (CombatUtil.canAttack(attacker, combat) && this.isEffectiveAttacker(ai, attacker, combat)) {
combat.addAttacker(attacker);
@@ -563,7 +563,7 @@ public class AiAttackController {
}
}
if (exalted) {
CardLists.sortAttack(this.attackers);
CardLists.sortByPowerDesc(this.attackers);
System.out.println("Exalted");
this.aiAggression = 6;
for (Card attacker : this.attackers) {
@@ -663,7 +663,7 @@ public class AiAttackController {
// *********************
boolean doAttritionalAttack = false;
// get list of attackers ordered from low power to high
CardLists.sortAttackLowFirst(this.attackers);
CardLists.sortByPowerAsc(this.attackers);
// get player life total
int humanLife = opp.getLife();
// get the list of attackers up to the first blocked one
@@ -799,7 +799,7 @@ public class AiAttackController {
int attackNum = 0;
int damage = 0;
List<Card> attacking = combat.getAttackersByDefenderSlot(combat.getCurrentDefenderNumber());
CardLists.sortAttackLowFirst(attacking);
CardLists.sortByPowerAsc(attacking);
for (Card atta : attacking) {
if (attackNum >= blockNum || !CombatUtil.canBeBlocked(attacker, this.blockers)) {
damage += ComputerUtilCombat.damageIfUnblocked(atta, opp, null);

View File

@@ -586,7 +586,7 @@ public class AiController {
discardList.add(landsInHand.get(0));
hand.remove(landsInHand.get(0));
} else { // Discard other stuff
CardLists.sortCMC(hand);
CardLists.sortByCmcDesc(hand);
int numLandsAvailable = numLandsInPlay;
if (numLandsInHand > 0) {
numLandsAvailable++;

View File

@@ -399,7 +399,7 @@ public class ComputerUtil {
*
* @param type
* a {@link java.lang.String} object.
* @param activate
* @param source
* a {@link forge.Card} object.
* @param target
* a {@link forge.Card} object.
@@ -407,10 +407,8 @@ public class ComputerUtil {
* a int.
* @return a {@link forge.CardList} object.
*/
public static List<Card> chooseSacrificeType(final Player ai, final String type, final Card activate, final Card target,
final int amount) {
List<Card> typeList =
CardLists.getValidCards(ai.getCardsIn(ZoneType.Battlefield), type.split(";"), activate.getController(), activate);
public static List<Card> chooseSacrificeType(final Player ai, final String type, final Card source, final Card target, final int amount) {
List<Card> typeList = CardLists.getValidCards(ai.getCardsIn(ZoneType.Battlefield), type.split(";"), source.getController(), source);
if (ai.hasKeyword("You can't sacrifice creatures to cast spells or activate abilities.")) {
typeList = CardLists.getNotType(typeList, "Creature");
}
@@ -427,7 +425,7 @@ public class ComputerUtil {
int count = 0;
while (count < amount) {
final Card prefCard = ComputerUtil.getCardPreference(ai, activate, "SacCost", typeList);
final Card prefCard = ComputerUtil.getCardPreference(ai, source, "SacCost", typeList);
if (prefCard != null) {
sacList.add(prefCard);
typeList.remove(prefCard);
@@ -437,7 +435,7 @@ public class ComputerUtil {
}
}
CardLists.sortAttackLowFirst(typeList);
CardLists.sortByPowerAsc(typeList);
for (int i = count; i < amount; i++) {
sacList.add(typeList.get(i));
@@ -481,7 +479,7 @@ public class ComputerUtil {
return null;
}
CardLists.sortAttackLowFirst(typeList);
CardLists.sortByPowerAsc(typeList);
final List<Card> exileList = new ArrayList<Card>();
for (int i = 0; i < amount; i++) {
@@ -520,7 +518,7 @@ public class ComputerUtil {
return null;
}
CardLists.sortAttackLowFirst(typeList);
CardLists.sortByPowerAsc(typeList);
final List<Card> tapList = new ArrayList<Card>();
@@ -560,7 +558,7 @@ public class ComputerUtil {
return null;
}
CardLists.sortAttack(typeList);
CardLists.sortByPowerDesc(typeList);
final List<Card> untapList = new ArrayList<Card>();
@@ -597,7 +595,7 @@ public class ComputerUtil {
return null;
}
CardLists.sortAttackLowFirst(typeList);
CardLists.sortByPowerAsc(typeList);
final List<Card> returnList = new ArrayList<Card>();
for (int i = 0; i < amount; i++) {
@@ -645,7 +643,7 @@ public class ComputerUtil {
return sacrificed; // sacrifice none
}
CardLists.sortCMC(remaining);
CardLists.sortByCmcDesc(remaining);
Collections.reverse(remaining);
final int max = Math.min(remaining.size(), amount);

View File

@@ -285,7 +285,7 @@ public class ComputerUtilBlock {
// Begin with the attackers that pose the biggest threat
CardLists.sortByEvaluateCreature(firstAttacker);
CardLists.sortAttack(firstAttacker);
CardLists.sortByPowerDesc(firstAttacker);
// If I don't have any planeswalkers than sorting doesn't really matter
if (defenders.size() == 1) {
@@ -301,7 +301,7 @@ public class ComputerUtilBlock {
// if planeswalker will be too difficult to defend don't even bother
for (List<Card> attacker : attackerLists) {
// Begin with the attackers that pose the biggest threat
CardLists.sortAttack(attacker);
CardLists.sortByPowerDesc(attacker);
for (final Card c : attacker) {
sortedAttackers.add(c);
}
@@ -423,7 +423,7 @@ public class ComputerUtilBlock {
}
if (firstStrikeBlockers.size() > 1) {
CardLists.sortAttack(firstStrikeBlockers);
CardLists.sortByPowerDesc(firstStrikeBlockers);
for (final Card blocker : firstStrikeBlockers) {
final int damageNeeded = ComputerUtilCombat.getDamageToKill(attacker)
+ ComputerUtilCombat.predictToughnessBonusOfAttacker(attacker, blocker, combat, false);
@@ -803,7 +803,7 @@ public class ComputerUtilBlock {
}
// Begin with the weakest blockers
CardLists.sortAttackLowFirst(ComputerUtilBlock.getBlockersLeft());
CardLists.sortByPowerAsc(ComputerUtilBlock.getBlockersLeft());
// == 1. choose best blocks first ==
combat = ComputerUtilBlock.makeGoodBlocks(ai, combat);
@@ -919,7 +919,7 @@ public class ComputerUtilBlock {
// very very simple ordering of blockers, sort by evaluate, then sort by attack
//final int damage = attacker.getNetCombatDamage();
CardLists.sortByEvaluateCreature(blockers);
CardLists.sortAttack(blockers);
CardLists.sortByPowerDesc(blockers);
// TODO: Take total damage, and attempt to maximize killing the greatest evaluation of creatures
// It's probably generally better to kill the largest creature, but sometimes its better to kill a few smaller ones
@@ -932,7 +932,7 @@ public class ComputerUtilBlock {
// very very simple ordering of attackers, sort by evaluate, then sort by attack
//final int damage = attacker.getNetCombatDamage();
CardLists.sortByEvaluateCreature(blockers);
CardLists.sortAttack(blockers);
CardLists.sortByPowerDesc(blockers);
// TODO: Take total damage, and attempt to maximize killing the greatest evaluation of creatures
// It's probably generally better to kill the largest creature, but sometimes its better to kill a few smaller ones

View File

@@ -544,7 +544,7 @@ public class Upkeep extends Phase {
public void resolve() {
final List<Card> creatures = CardLists.filter(game.getCardsIn(ZoneType.Battlefield), Presets.CREATURES);
if (creatures.size() > 0) {
CardLists.sortAttackLowFirst(creatures);
CardLists.sortByPowerAsc(creatures);
final int power = creatures.get(0).getNetAttack();
if (player.isHuman()) {
Singletons.getModel().getMatch().getInput().setInput(

View File

@@ -419,7 +419,6 @@ public class DeckLister extends JPanel implements ILocalRepaint {
this.cmdRowSelect.execute();
}
}
@SuppressWarnings("unchecked")
private <T extends DeckBase> void editDeck(final Deck d0) {
switch (this.gametype) {
case Quest: