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 hangglider
sailingraft sailingraft
shack shack
Error, need fireplace to build Error, could not build item
OK OK
Error, already built Error, already built
OK OK

View File

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

View File

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