Handle item requirements when building better

This commit is contained in:
Arne Keller 2020-03-12 08:39:02 +01:00
parent ed5170ebfc
commit 172e085d28
3 changed files with 17 additions and 8 deletions

View File

@ -103,7 +103,7 @@ fireplace
hangglider
sailingraft
shack
Error, need fireplace to build
Error, could not build item
OK
Error, already built
OK

View File

@ -111,8 +111,6 @@ public class CardGame {
throw new LogicException("can not build item");
} else if (awaitingDiceRoll()) {
throw new LogicException("awaiting dice roll, can not build");
} else if (item.requiresFireplace() && !items.contains(FIREPLACE)) {
throw new LogicException("need fireplace to build");
} else if (items.contains(item)) {
throw new LogicException("already built");
} else if (canBuild(item)) {
@ -141,7 +139,7 @@ public class CardGame {
}
private boolean canBuild(Item item) {
if (item.requiresFireplace() && !items.contains(FIREPLACE)) {
if (!items.containsAll(item.itemsNeededToBuild())) {
return false;
}
if (items.contains(item)) {

View File

@ -1,5 +1,10 @@
package edu.kit.informatik.cardgame.model;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static edu.kit.informatik.cardgame.model.Card.METAL;
import static edu.kit.informatik.cardgame.model.Card.PLASTIC;
import static edu.kit.informatik.cardgame.model.Card.WOOD;
@ -32,13 +37,19 @@ public enum Item {
return new Card[] {METAL, METAL, METAL, METAL, METAL, METAL, PLASTIC};
case BALLON:
return new Card[] {WOOD, PLASTIC, PLASTIC, PLASTIC, PLASTIC, PLASTIC, PLASTIC};
default: // we don't have any such items, but could add some eventually
throw new IllegalArgumentException("item does not need resources");
default:
return new Card[0];
}
}
public boolean requiresFireplace() {
return this.equals(STEAMBOAT) || this.equals(BALLON);
public Collection<Item> itemsNeededToBuild() {
switch (this) {
case BALLON:
case STEAMBOAT:
return Arrays.asList(FIREPLACE);
default:
return Collections.emptyList();
}
}
public int fightingBonus() {