From 1a93c5d061b62103b054296e2cc0373858eabd7d Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sat, 29 Feb 2020 16:23:36 +0100 Subject: [PATCH] Correctly handle reset by reusing start cards and wait for start before allowing commands --- commands1_output.txt | 6 +-- game_over_no_actions_input.txt | 1 - game_over_no_actions_output.txt | 1 - src/edu/kit/informatik/model/CardGame.java | 41 +++++++++++++------ .../kit/informatik/ui/command/Buildable.java | 2 +- .../informatik/ui/command/ListBuildings.java | 2 +- .../informatik/ui/command/ListResources.java | 2 +- 7 files changed, 35 insertions(+), 20 deletions(-) diff --git a/commands1_output.txt b/commands1_output.txt index 2b999bb..e5ab76e 100644 --- a/commands1_output.txt +++ b/commands1_output.txt @@ -1,7 +1,7 @@ -EMPTY +Error, can not get buildable items: game not started Error, no card to draw exists -EMPTY -EMPTY +Error, can not get buildings: game not started +Error, can not get resources: game not started Error, invalid list-buildings argument: none expected Error, invalid list-resources argument: none expected OK diff --git a/game_over_no_actions_input.txt b/game_over_no_actions_input.txt index 7062f6d..dcb9cd9 100644 --- a/game_over_no_actions_input.txt +++ b/game_over_no_actions_input.txt @@ -79,4 +79,3 @@ draw rollD8 8 draw list-resources -start wood,wood,wood,wood,wood,wood,wood,wood,wood,wood,wood,wood,wood,wood,wood,wood,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,plastic,metal,metal,metal,metal,metal,metal,metal,metal,metal,metal,metal,metal,metal,metal,metal,metal,spider,spider,spider,spider,spider,snake,snake,snake,snake,snake,tiger,tiger,tiger,tiger,tiger,thunderstorm diff --git a/game_over_no_actions_output.txt b/game_over_no_actions_output.txt index 0ab234e..0712dda 100644 --- a/game_over_no_actions_output.txt +++ b/game_over_no_actions_output.txt @@ -79,4 +79,3 @@ tiger survived thunderstorm EMPTY -OK diff --git a/src/edu/kit/informatik/model/CardGame.java b/src/edu/kit/informatik/model/CardGame.java index 691807a..b584cb5 100644 --- a/src/edu/kit/informatik/model/CardGame.java +++ b/src/edu/kit/informatik/model/CardGame.java @@ -30,6 +30,7 @@ import static edu.kit.informatik.model.Item.STEAMBOAT; public class CardGame { private Deque cardStack; + private Deque currentCardStack; private Deque resources = new ArrayDeque<>(); private List items = new ArrayList<>(); private boolean fightingAnimal = false; @@ -50,20 +51,21 @@ public class CardGame { || Collections.frequency(cardStack, THUNDERSTORM) != 1) { throw new InvalidInputException("invalid deck: missing or surplus cards"); } - this.cardStack = cardStack; + this.cardStack = new ArrayDeque<>(cardStack); + this.currentCardStack = new ArrayDeque<>(cardStack); return true; } else { - return false; // still in game + return false; // game already started } } public Card draw() throws InvalidInputException { - if (cardStack == null || cardStack.isEmpty()) { + if (currentCardStack == null || currentCardStack.isEmpty()) { throw new InvalidInputException("no card to draw exists"); } else if (awaitedDiceSize != null) { throw new InvalidInputException("roll dice, please"); } - Card card = cardStack.removeFirst(); + Card card = currentCardStack.removeFirst(); if (card.isResource()) { resources.add(card); } else if (card.needsDiceRoll()) { @@ -74,8 +76,8 @@ public class CardGame { clearResources(); items.remove(FIREPLACE); } - if (cardStack.isEmpty() && getBuildableItems().isEmpty()) { - cardStack = null; // game over + if (currentCardStack.isEmpty() && getBuildableItems().isEmpty()) { + currentCardStack = null; // game over } return card; } @@ -137,8 +139,8 @@ public class CardGame { awaitedDiceSize = 6; minimumDiceRoll = 4; } - if (cardStack != null && cardStack.isEmpty() && getBuildableItems().isEmpty()) { - cardStack = null; // game over + if (currentCardStack != null && currentCardStack.isEmpty() && getBuildableItems().isEmpty()) { + currentCardStack = null; // game over } return true; } else { @@ -159,22 +161,37 @@ public class CardGame { return Arrays.stream(resourcesNeeded).allMatch(Objects::isNull); } - public Deque getResources() { + private boolean gameStarted() { + return cardStack != null; + } + + public Deque getResources() throws InvalidInputException { + if (!gameStarted()) { + throw new InvalidInputException("can not get resources: game not started"); + } // do not allow caller to modify internal queue return new ArrayDeque<>(resources); } - public List getItems() { + public List getItems() throws InvalidInputException { + if (!gameStarted()) { + throw new InvalidInputException("can not get buildings: game not started"); + } // do not allow caller to modify internal list return new ArrayList<>(items); } - public Set getBuildableItems() { + public Set getBuildableItems() throws InvalidInputException { + if (!gameStarted()) { + throw new InvalidInputException("can not get buildable items: game not started"); + } return Arrays.stream(Item.values()).filter(this::canBuild).collect(Collectors.toSet()); } public void reset() { - this.cardStack = null; + if (cardStack != null) { + this.currentCardStack = new ArrayDeque<>(cardStack); + } this.resources = new ArrayDeque<>(); this.items = new ArrayList<>(); this.fightingAnimal = false; diff --git a/src/edu/kit/informatik/ui/command/Buildable.java b/src/edu/kit/informatik/ui/command/Buildable.java index 91f7fd6..03878e1 100644 --- a/src/edu/kit/informatik/ui/command/Buildable.java +++ b/src/edu/kit/informatik/ui/command/Buildable.java @@ -12,7 +12,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE; public class Buildable extends Command { @Override - public void apply(CardGame game) { + public void apply(CardGame game) throws InvalidInputException { Set buildable = game.getBuildableItems(); if (buildable.isEmpty()) { Terminal.printLine("EMPTY"); diff --git a/src/edu/kit/informatik/ui/command/ListBuildings.java b/src/edu/kit/informatik/ui/command/ListBuildings.java index 5c63448..2d382d1 100644 --- a/src/edu/kit/informatik/ui/command/ListBuildings.java +++ b/src/edu/kit/informatik/ui/command/ListBuildings.java @@ -11,7 +11,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_BUILDINGS; public class ListBuildings extends Command { @Override - public void apply(CardGame game) { + public void apply(CardGame game) throws InvalidInputException { List items = game.getItems(); if (items.isEmpty()) { Terminal.printLine("EMPTY"); diff --git a/src/edu/kit/informatik/ui/command/ListResources.java b/src/edu/kit/informatik/ui/command/ListResources.java index 7d3eb4f..d5d4d0e 100644 --- a/src/edu/kit/informatik/ui/command/ListResources.java +++ b/src/edu/kit/informatik/ui/command/ListResources.java @@ -11,7 +11,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_RESOURCES; public class ListResources extends Command { @Override - public void apply(CardGame game) { + public void apply(CardGame game) throws InvalidInputException { Deque resources = game.getResources(); if (resources.isEmpty()) { Terminal.printLine("EMPTY");