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,7 +53,6 @@ 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 {
@ -164,17 +163,7 @@ 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) {

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,71 +77,25 @@ 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) {
case FEWER_THAN:
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) { for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
player = game.getPlayer(playerId); player = game.getPlayer(playerId);
if (player != null) { if (player != null) {
if (player.getHand().size() >= this.count) { if (!CountType.compare(player.getHand().size(), type, this.count)) {
conflict = true; conflict = true;
break; 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;
default: default:

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;
@ -78,25 +79,11 @@ public class OpponentControlsPermanentCondition implements Condition {
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:
if (game.getBattlefield().count(localFilter, source.getSourceId(), source.getControllerId(), game) < this.count) {
conditionApplies = true; conditionApplies = true;
break; 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,33 +67,20 @@ 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 {
@ -108,17 +107,8 @@ 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) {

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;