From 172e085d2814287a953e4306de7f2171136b4b2f Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Thu, 12 Mar 2020 08:39:02 +0100 Subject: [PATCH] Handle item requirements when building better --- game1_output.txt | 2 +- .../informatik/cardgame/model/CardGame.java | 4 +--- .../kit/informatik/cardgame/model/Item.java | 19 +++++++++++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/game1_output.txt b/game1_output.txt index e2599cc..8b848c6 100644 --- a/game1_output.txt +++ b/game1_output.txt @@ -103,7 +103,7 @@ fireplace hangglider sailingraft shack -Error, need fireplace to build +Error, could not build item OK Error, already built OK diff --git a/src/edu/kit/informatik/cardgame/model/CardGame.java b/src/edu/kit/informatik/cardgame/model/CardGame.java index 813d732..917222f 100644 --- a/src/edu/kit/informatik/cardgame/model/CardGame.java +++ b/src/edu/kit/informatik/cardgame/model/CardGame.java @@ -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)) { diff --git a/src/edu/kit/informatik/cardgame/model/Item.java b/src/edu/kit/informatik/cardgame/model/Item.java index 30722d4..33dabb5 100644 --- a/src/edu/kit/informatik/cardgame/model/Item.java +++ b/src/edu/kit/informatik/cardgame/model/Item.java @@ -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 itemsNeededToBuild() { + switch (this) { + case BALLON: + case STEAMBOAT: + return Arrays.asList(FIREPLACE); + default: + return Collections.emptyList(); + } } public int fightingBonus() {