put comparing logic in counttype, remove all switches

This commit is contained in:
ingmargoudt 2017-03-06 11:46:22 +01:00
parent 0adc715499
commit 3d029d6ad5
8 changed files with 66 additions and 145 deletions

View file

@ -53,13 +53,12 @@ import mage.players.Player;
import mage.target.common.TargetOpponent; import mage.target.common.TargetOpponent;
/** /**
*
* @author LevelX2 * @author LevelX2
*/ */
public class NezumiShortfang extends CardImpl { public class NezumiShortfang extends CardImpl {
public NezumiShortfang(UUID ownerId, CardSetInfo setInfo) { public NezumiShortfang(UUID ownerId, CardSetInfo setInfo) {
super(ownerId,setInfo,new CardType[]{CardType.CREATURE},"{1}{B}"); super(ownerId, setInfo, new CardType[]{CardType.CREATURE}, "{1}{B}");
this.subtype.add("Rat"); this.subtype.add("Rat");
this.subtype.add("Rogue"); this.subtype.add("Rogue");
@ -73,9 +72,9 @@ public class NezumiShortfang extends CardImpl {
ability.addCost(new TapSourceCost()); ability.addCost(new TapSourceCost());
ability.addTarget(new TargetOpponent()); ability.addTarget(new TargetOpponent());
ability.addEffect(new ConditionalOneShotEffect( ability.addEffect(new ConditionalOneShotEffect(
new FlipSourceEffect(new StabwhiskerTheOdious()), new FlipSourceEffect(new StabwhiskerTheOdious()),
new CardsInTargetOpponentHandCondition(CountType.FEWER_THAN, 1), new CardsInTargetOpponentHandCondition(CountType.FEWER_THAN, 1),
"Then if that player has no cards in hand, flip {this}")); "Then if that player has no cards in hand, flip {this}"));
this.addAbility(ability); this.addAbility(ability);
} }
@ -128,7 +127,7 @@ class StabwhiskerLoseLifeEffect extends OneShotEffect {
Player opponent = game.getPlayer(getTargetPointer().getFirst(game, source)); Player opponent = game.getPlayer(getTargetPointer().getFirst(game, source));
if (opponent != null) { if (opponent != null) {
int lifeLose = 3 - opponent.getHand().size(); int lifeLose = 3 - opponent.getHand().size();
if (lifeLose > 0 ) { if (lifeLose > 0) {
opponent.loseLife(lifeLose, game, false); opponent.loseLife(lifeLose, game, false);
} }
return true; return true;
@ -143,16 +142,16 @@ class CardsInTargetOpponentHandCondition implements Condition {
private CountType type; private CountType type;
private int count; private int count;
public CardsInTargetOpponentHandCondition() { public CardsInTargetOpponentHandCondition() {
this(CountType.EQUAL_TO, 0); this(CountType.EQUAL_TO, 0);
} }
public CardsInTargetOpponentHandCondition (CountType type, int count ) { public CardsInTargetOpponentHandCondition(CountType type, int count) {
this.type = type; this.type = type;
this.count = count; this.count = count;
} }
public CardsInTargetOpponentHandCondition (CountType type, int count, Condition conditionToDecorate ) { public CardsInTargetOpponentHandCondition(CountType type, int count, Condition conditionToDecorate) {
this(type, count); this(type, count);
this.condition = conditionToDecorate; this.condition = conditionToDecorate;
} }
@ -164,20 +163,10 @@ class CardsInTargetOpponentHandCondition implements Condition {
if (opponent == null) { if (opponent == null) {
return false; return false;
} }
switch ( this.type ) { conditionApplies = CountType.compare(opponent.getHand().size(), type, count);
case FEWER_THAN:
conditionApplies = opponent.getHand().size() < this.count;
break;
case MORE_THAN:
conditionApplies = opponent.getHand().size() > this.count;
break;
case EQUAL_TO:
conditionApplies = opponent.getHand().size() == this.count;
break;
}
//If a decorated condition exists, check it as well and apply them together. //If a decorated condition exists, check it as well and apply them together.
if ( this.condition != null ) { if (this.condition != null) {
conditionApplies = conditionApplies && this.condition.apply(game, source); conditionApplies = conditionApplies && this.condition.apply(game, source);
} }

View file

@ -5,4 +5,18 @@ package mage.abilities;
*/ */
public enum CountType { public enum CountType {
MORE_THAN, FEWER_THAN, EQUAL_TO; MORE_THAN, FEWER_THAN, EQUAL_TO;
public static boolean compare(int source, CountType comparison, int target){
switch (comparison){
case MORE_THAN:
return source > target;
case FEWER_THAN:
return source < target;
case EQUAL_TO:
return source == target;
default:
throw new IllegalArgumentException("comparison rules for "+comparison + " missing");
}
}
} }

View file

@ -13,7 +13,7 @@ import mage.game.Game;
@FunctionalInterface @FunctionalInterface
public interface Condition extends Serializable { public interface Condition extends Serializable {
public enum ComparisonType { enum ComparisonType {
GreaterThan(">"), GreaterThan(">"),
Equal("=="), Equal("=="),

View file

@ -28,6 +28,7 @@
package mage.abilities.condition.common; package mage.abilities.condition.common;
import java.util.UUID; import java.util.UUID;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.CountType; import mage.abilities.CountType;
import mage.abilities.condition.Condition; import mage.abilities.condition.Condition;
@ -40,7 +41,6 @@ import mage.util.CardUtil;
* Cards in controller hand condition. This condition can decorate other * Cards in controller hand condition. This condition can decorate other
* conditions as well as be used standalone. * conditions as well as be used standalone.
* *
*
* @author LevelX * @author LevelX
*/ */
public class CardsInHandCondition implements Condition { public class CardsInHandCondition implements Condition {
@ -77,70 +77,24 @@ public class CardsInHandCondition implements Condition {
if (controller != null) { if (controller != null) {
switch (targetController) { switch (targetController) {
case YOU: case YOU:
switch (this.type) { conditionApplies = CountType.compare(game.getPlayer(source.getControllerId()).getHand().size(), type, count);
case FEWER_THAN:
conditionApplies = game.getPlayer(source.getControllerId()).getHand().size() < this.count;
break;
case MORE_THAN:
conditionApplies = game.getPlayer(source.getControllerId()).getHand().size() > this.count;
break;
case EQUAL_TO:
conditionApplies = game.getPlayer(source.getControllerId()).getHand().size() == this.count;
break;
}
break; break;
case ACTIVE: case ACTIVE:
Player player = game.getPlayer(game.getActivePlayerId()); Player player = game.getPlayer(game.getActivePlayerId());
if (player != null) { if (player != null) {
switch (this.type) { conditionApplies = CountType.compare(player.getHand().size(), type, count);
case FEWER_THAN:
conditionApplies = player.getHand().size() < this.count;
break;
case MORE_THAN:
conditionApplies = player.getHand().size() > this.count;
break;
case EQUAL_TO:
conditionApplies = player.getHand().size() == this.count;
break;
}
} }
break; break;
case ANY: case ANY:
boolean conflict = false; boolean conflict = false;
switch (this.type) { for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
case FEWER_THAN: player = game.getPlayer(playerId);
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) { if (player != null) {
player = game.getPlayer(playerId); if (!CountType.compare(player.getHand().size(), type, this.count)) {
if (player != null) { conflict = true;
if (player.getHand().size() >= this.count) { break;
conflict = true;
break;
}
}
} }
break; }
case MORE_THAN:
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
player = game.getPlayer(playerId);
if (player != null) {
if (player.getHand().size() <= this.count) {
conflict = true;
break;
}
}
}
break;
case EQUAL_TO:
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
player = game.getPlayer(playerId);
if (player != null) {
if (player.getHand().size() != this.count) {
conflict = true;
break;
}
}
}
break;
} }
conditionApplies = !conflict; conditionApplies = !conflict;
break; break;

View file

@ -29,6 +29,7 @@
package mage.abilities.condition.common; package mage.abilities.condition.common;
import java.util.UUID; import java.util.UUID;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.CountType; import mage.abilities.CountType;
import mage.abilities.condition.Condition; import mage.abilities.condition.Condition;
@ -39,7 +40,7 @@ import mage.game.Game;
/** /**
* Checks if one opponent (each opponent is checked on its own) fulfills * Checks if one opponent (each opponent is checked on its own) fulfills
* the defined condition of controlling the defined number of permanents. * the defined condition of controlling the defined number of permanents.
* *
* @author LevelX2 * @author LevelX2
*/ */
@ -66,7 +67,7 @@ public class OpponentControlsPermanentCondition implements Condition {
* @param type * @param type
* @param count * @param count
*/ */
public OpponentControlsPermanentCondition ( FilterPermanent filter, CountType type, int count ) { public OpponentControlsPermanentCondition(FilterPermanent filter, CountType type, int count) {
this.filter = filter; this.filter = filter;
this.type = type; this.type = type;
this.count = count; this.count = count;
@ -74,30 +75,16 @@ public class OpponentControlsPermanentCondition implements Condition {
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
boolean conditionApplies = false; boolean conditionApplies = false;
for(UUID opponentId :game.getOpponents(source.getControllerId())) { for (UUID opponentId : game.getOpponents(source.getControllerId())) {
FilterPermanent localFilter = filter.copy(); FilterPermanent localFilter = filter.copy();
localFilter.add(new ControllerIdPredicate(opponentId)); localFilter.add(new ControllerIdPredicate(opponentId));
switch ( this.type ) { if (CountType.compare(game.getBattlefield().count(localFilter, source.getSourceId(), source.getControllerId(), game), type, this.count)) {
case FEWER_THAN: conditionApplies = true;
if (game.getBattlefield().count(localFilter, source.getSourceId(), source.getControllerId(), game) < this.count) { break;
conditionApplies = true;
break;
}
case MORE_THAN:
if (game.getBattlefield().count(localFilter, source.getSourceId(), source.getControllerId(), game) > this.count) {
conditionApplies = true;
break;
}
break;
case EQUAL_TO:
if (game.getBattlefield().count(localFilter, source.getSourceId(), source.getControllerId(), game) == this.count) {
conditionApplies = true;
break;
}
break;
} }
} }
return conditionApplies; return conditionApplies;
} }

View file

@ -47,7 +47,7 @@ public class PermanentHasCounterCondition implements Condition {
private CounterType counterType; private CounterType counterType;
private int amount; private int amount;
private FilterPermanent filter; private FilterPermanent filter;
private CountType type; private CountType counttype;
private boolean anyPlayer; private boolean anyPlayer;
public PermanentHasCounterCondition(CounterType counterType, int amount, FilterPermanent filter) { public PermanentHasCounterCondition(CounterType counterType, int amount, FilterPermanent filter) {
@ -59,7 +59,7 @@ public class PermanentHasCounterCondition implements Condition {
this.counterType = counterType; this.counterType = counterType;
this.amount = amount; this.amount = amount;
this.filter = filter; this.filter = filter;
this.type = type; this.counttype = type;
this.anyPlayer = false; this.anyPlayer = false;
} }
@ -67,32 +67,19 @@ public class PermanentHasCounterCondition implements Condition {
this.counterType = counterType; this.counterType = counterType;
this.amount = amount; this.amount = amount;
this.filter = filter; this.filter = filter;
this.type = type; this.counttype = type;
this.anyPlayer = any; this.anyPlayer = any;
} }
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
List<Permanent> permanents = game.getBattlefield().getActivePermanents(this.filter, source.getControllerId(), game); List<Permanent> permanents = game.getBattlefield().getActivePermanents(this.filter, source.getControllerId(), game);
if(this.anyPlayer == true) { if(this.anyPlayer) {
permanents = game.getBattlefield().getAllActivePermanents(this.filter, game); permanents = game.getBattlefield().getAllActivePermanents(this.filter, game);
} }
for (Permanent permanent : permanents) { for (Permanent permanent : permanents) {
switch (this.type) { if(CountType.compare(permanent.getCounters(game).getCount(this.counterType), counttype, this.amount))
case FEWER_THAN: {
if (permanent.getCounters(game).getCount(this.counterType) < this.amount) { return true;
return true;
}
break;
case MORE_THAN:
if (permanent.getCounters(game).getCount(this.counterType) > this.amount) {
return true;
}
break;
case EQUAL_TO:
if (permanent.getCounters(game).getCount(this.counterType) == this.amount) {
return true;
}
break;
} }
} }
return false; return false;

View file

@ -38,11 +38,10 @@ import mage.game.Game;
* Battlefield checking condition. This condition can decorate other conditions * Battlefield checking condition. This condition can decorate other conditions
* as well as be used standalone. * as well as be used standalone.
* *
* @see #Controls(mage.filter.Filter)
* @see #Controls(mage.filter.Filter, mage.abilities.condition.Condition)
*
* @author nantuko * @author nantuko
* @author maurer.it_at_gmail.com * @author maurer.it_at_gmail.com
* @see #Controls(mage.filter.Filter)
* @see #Controls(mage.filter.Filter, mage.abilities.condition.Condition)
*/ */
public class PermanentsOnTheBattlefieldCondition implements Condition { public class PermanentsOnTheBattlefieldCondition implements Condition {
@ -56,7 +55,7 @@ public class PermanentsOnTheBattlefieldCondition implements Condition {
* Applies a filter and delegates creation to * Applies a filter and delegates creation to
* {@link #ControlsPermanent(mage.filter.FilterPermanent, mage.abilities.condition.common.ControlsPermanent.CountType, int)} * {@link #ControlsPermanent(mage.filter.FilterPermanent, mage.abilities.condition.common.ControlsPermanent.CountType, int)}
* with {@link CountType#MORE_THAN}, and 0. * with {@link CountType#MORE_THAN}, and 0.
* *
* @param filter * @param filter
*/ */
public PermanentsOnTheBattlefieldCondition(FilterPermanent filter) { public PermanentsOnTheBattlefieldCondition(FilterPermanent filter) {
@ -72,14 +71,14 @@ public class PermanentsOnTheBattlefieldCondition implements Condition {
* @param type * @param type
* @param count * @param count
*/ */
public PermanentsOnTheBattlefieldCondition ( FilterPermanent filter, CountType type, int count ) { public PermanentsOnTheBattlefieldCondition(FilterPermanent filter, CountType type, int count) {
this(filter, type, count, true); this(filter, type, count, true);
} }
public PermanentsOnTheBattlefieldCondition ( FilterPermanent filter, CountType type, int count, boolean onlyControlled ) { public PermanentsOnTheBattlefieldCondition(FilterPermanent filter, CountType type, int count, boolean onlyControlled) {
this.filter = filter; this.filter = filter;
this.type = type; this.type = type;
this.count = count; this.count = count;
this.onlyControlled = onlyControlled; this.onlyControlled = onlyControlled;
} }
@ -94,7 +93,7 @@ public class PermanentsOnTheBattlefieldCondition implements Condition {
* @param count * @param count
* @param conditionToDecorate * @param conditionToDecorate
*/ */
public PermanentsOnTheBattlefieldCondition ( FilterPermanent filter, CountType type, int count, Condition conditionToDecorate ) { public PermanentsOnTheBattlefieldCondition(FilterPermanent filter, CountType type, int count, Condition conditionToDecorate) {
this(filter, type, count); this(filter, type, count);
this.condition = conditionToDecorate; this.condition = conditionToDecorate;
} }
@ -108,20 +107,11 @@ public class PermanentsOnTheBattlefieldCondition implements Condition {
localFilter.add(new ControllerIdPredicate(source.getControllerId())); localFilter.add(new ControllerIdPredicate(source.getControllerId()));
} }
switch ( this.type ) { int permanentsOnBattlefield = game.getBattlefield().count(localFilter, source.getSourceId(), source.getControllerId(), game);
case FEWER_THAN: conditionApplies = CountType.compare(permanentsOnBattlefield, type, count);
conditionApplies = game.getBattlefield().count(localFilter, source.getSourceId(), source.getControllerId(), game) < this.count;
break;
case MORE_THAN:
conditionApplies = game.getBattlefield().count(localFilter, source.getSourceId(), source.getControllerId(), game) > this.count;
break;
case EQUAL_TO:
conditionApplies = game.getBattlefield().count(localFilter, source.getSourceId(), source.getControllerId(), game) == this.count;
break;
}
//If a decorated condition exists, check it as well and apply them together. //If a decorated condition exists, check it as well and apply them together.
if ( this.condition != null ) { if (this.condition != null) {
conditionApplies = conditionApplies && this.condition.apply(game, source); conditionApplies = conditionApplies && this.condition.apply(game, source);
} }

View file

@ -40,7 +40,7 @@ import mage.players.PlayerList;
*/ */
public class TenOrLessLifeCondition implements Condition { public class TenOrLessLifeCondition implements Condition {
public static enum CheckType { AN_OPPONENT, CONTROLLER, TARGET_OPPONENT, EACH_PLAYER } public enum CheckType { AN_OPPONENT, CONTROLLER, TARGET_OPPONENT, EACH_PLAYER }
private final CheckType type; private final CheckType type;