diff --git a/game2_input.txt b/game2_input.txt index a53e5b8..6c0fd18 100644 --- a/game2_input.txt +++ b/game2_input.txt @@ -125,3 +125,30 @@ rollD6 3 build anything list-buildings build ballon +list-buildings +list-resources +build? +start metal,metal,metal,wood,wood,wood,wood,metal,metal,plastic,plastic,wood,wood,wood,wood,wood,wood,wood,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,wood,wood,metal,metal,wood,wood,wood,metal,metal,metal,metal,metal,metal,metal,metal,metal,spider,spider,spider,spider,spider,snake,snake,snake,snake,snake,tiger,tiger,tiger,tiger,thunderstorm,tiger +draw +draw +draw +build axe +list-buildings +draw +draw +draw +draw +draw +draw +draw +draw +build sailingraft +rollD6 6 +list-buildings +list-resources +reset +draw +draw +draw +build axe +list-buildings diff --git a/game2_output.txt b/game2_output.txt index 2c5b59b..b6acd5c 100644 --- a/game2_output.txt +++ b/game2_output.txt @@ -75,3 +75,30 @@ fireplace club axe OK +Error, can not get buildings: game not started +Error, can not get resources: game not started +Error, can not get buildable items: game not started +OK +metal +metal +metal +OK +axe +wood +wood +wood +wood +metal +metal +plastic +plastic +OK +win +Error, can not get buildings: game not started +Error, can not get resources: game not started +OK +metal +metal +metal +OK +axe diff --git a/game_over_no_actions_output.txt b/game_over_no_actions_output.txt index 0712dda..299a1c8 100644 --- a/game_over_no_actions_output.txt +++ b/game_over_no_actions_output.txt @@ -78,4 +78,4 @@ survived tiger survived thunderstorm -EMPTY +Error, can not get resources: game not started diff --git a/src/edu/kit/informatik/model/CardGame.java b/src/edu/kit/informatik/model/CardGame.java index 16dc033..0bfa414 100644 --- a/src/edu/kit/informatik/model/CardGame.java +++ b/src/edu/kit/informatik/model/CardGame.java @@ -44,7 +44,7 @@ public class CardGame { * @return whether the game could be successfully started */ public boolean start(Deque cardStack) throws InvalidInputException { - if (!gameStarted()) { + if (cardStack == null || currentCardStack == null) { if (Collections.frequency(cardStack, WOOD) != 16 || Collections.frequency(cardStack, PLASTIC) != 16 || Collections.frequency(cardStack, METAL) != 16 || Collections.frequency(cardStack, SPIDER) != 5 || Collections.frequency(cardStack, SNAKE) != 5 || Collections.frequency(cardStack, TIGER) != 5 @@ -52,7 +52,7 @@ public class CardGame { throw new InvalidInputException("invalid deck: missing or surplus cards"); } this.cardStack = new ArrayDeque<>(cardStack); - this.currentCardStack = new ArrayDeque<>(cardStack); + reset(); return true; } else { return false; // game already started @@ -105,6 +105,7 @@ public class CardGame { // remove item used to escape items.remove(items.size() - 1); if (roll >= minimumNeeded) { + win(); return "win"; } else { return "lose"; @@ -134,7 +135,7 @@ public class CardGame { items.add(item); if (item.equals(STEAMBOAT) || item.equals(BALLOON)) { // player won - reset(); + win(); } else if (item.equals(SAILING_RAFT) || item.equals(HANG_GLIDER)) { // need at least a 4/d6 fightingAnimal = false; @@ -170,7 +171,7 @@ public class CardGame { } private boolean gameStarted() { - return cardStack != null; + return currentCardStack != null; } public Deque getResources() throws InvalidInputException { @@ -196,6 +197,10 @@ public class CardGame { return Arrays.stream(Item.values()).filter(this::canBuild).collect(Collectors.toSet()); } + private void win() { + currentCardStack = null; + } + public void reset() { if (cardStack != null) { this.currentCardStack = new ArrayDeque<>(cardStack);