From 52ab63f578077cdfdba839d4a2b449beca7960fa Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sat, 29 Feb 2020 16:08:54 +0100 Subject: [PATCH] Correctly handle wrong start input --- commands1_input.txt | 1 + commands1_output.txt | 1 + game_over_no_actions_input.txt | 82 ++++++++++++++++++++++ game_over_no_actions_output.txt | 82 ++++++++++++++++++++++ src/edu/kit/informatik/MainTest.java | 5 ++ src/edu/kit/informatik/model/CardGame.java | 24 ++++++- 6 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 game_over_no_actions_input.txt create mode 100644 game_over_no_actions_output.txt diff --git a/commands1_input.txt b/commands1_input.txt index 3709e69..ae78f3d 100644 --- a/commands1_input.txt +++ b/commands1_input.txt @@ -9,3 +9,4 @@ reset the game rollD8 5 start creating,good,task,specifications,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a start with,less,cards +start wood,wood,wood,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,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/commands1_output.txt b/commands1_output.txt index c7cf735..2b999bb 100644 --- a/commands1_output.txt +++ b/commands1_output.txt @@ -9,3 +9,4 @@ Error, invalid reset argument: none expected Error, not expecting dice roll Error, invalid start argument value(s) Error, invalid start arguments +Error, invalid deck: missing or surplus cards diff --git a/game_over_no_actions_input.txt b/game_over_no_actions_input.txt new file mode 100644 index 0000000..7062f6d --- /dev/null +++ b/game_over_no_actions_input.txt @@ -0,0 +1,82 @@ +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 +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +draw +rollD4 1 +draw +rollD4 1 +draw +rollD4 2 +draw +rollD4 3 +draw +rollD4 4 +draw +rollD6 2 +draw +rollD6 3 +draw +rollD6 4 +draw +rollD6 5 +draw +rollD6 6 +draw +rollD8 4 +draw +rollD8 5 +draw +rollD8 6 +draw +rollD8 7 +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 new file mode 100644 index 0000000..0ab234e --- /dev/null +++ b/game_over_no_actions_output.txt @@ -0,0 +1,82 @@ +OK +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 +lose +spider +lose +spider +lose +spider +survived +spider +survived +snake +lose +snake +lose +snake +survived +snake +survived +snake +survived +tiger +lose +tiger +survived +tiger +survived +tiger +survived +tiger +survived +thunderstorm +EMPTY +OK diff --git a/src/edu/kit/informatik/MainTest.java b/src/edu/kit/informatik/MainTest.java index d1a9f09..7addddf 100644 --- a/src/edu/kit/informatik/MainTest.java +++ b/src/edu/kit/informatik/MainTest.java @@ -28,6 +28,11 @@ class MainTest { cmpInOut("shack_test_input.txt", "shack_test_output.txt"); } + @Test + void gameOverNoActions() throws IOException { + cmpInOut("game_over_no_actions_input.txt", "game_over_no_actions_output.txt"); + } + private void cmpInOut(String in, String out) throws IOException { System.setIn(new ByteArrayInputStream(readFile(in))); ByteArrayOutputStream output = new ByteArrayOutputStream(); diff --git a/src/edu/kit/informatik/model/CardGame.java b/src/edu/kit/informatik/model/CardGame.java index cc0faf5..691807a 100644 --- a/src/edu/kit/informatik/model/CardGame.java +++ b/src/edu/kit/informatik/model/CardGame.java @@ -1,17 +1,24 @@ package edu.kit.informatik.model; -import edu.kit.informatik.Terminal; import edu.kit.informatik.ui.InvalidInputException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Deque; import java.util.List; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; +import static edu.kit.informatik.model.Card.METAL; +import static edu.kit.informatik.model.Card.PLASTIC; +import static edu.kit.informatik.model.Card.SNAKE; +import static edu.kit.informatik.model.Card.SPIDER; +import static edu.kit.informatik.model.Card.THUNDERSTORM; +import static edu.kit.informatik.model.Card.TIGER; +import static edu.kit.informatik.model.Card.WOOD; import static edu.kit.informatik.model.Item.AXE; import static edu.kit.informatik.model.Item.BALLOON; import static edu.kit.informatik.model.Item.CLUB; @@ -32,10 +39,17 @@ public class CardGame { /** * * @param cardStack order: 1st element = 1st to take + * @throws InvalidInputException if card stack has wrong distribution of cards * @return */ - public boolean start(Deque cardStack) { + public boolean start(Deque cardStack) throws InvalidInputException { if (this.cardStack == 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 + || Collections.frequency(cardStack, THUNDERSTORM) != 1) { + throw new InvalidInputException("invalid deck: missing or surplus cards"); + } this.cardStack = cardStack; return true; } else { @@ -60,6 +74,9 @@ public class CardGame { clearResources(); items.remove(FIREPLACE); } + if (cardStack.isEmpty() && getBuildableItems().isEmpty()) { + cardStack = null; // game over + } return card; } @@ -120,6 +137,9 @@ public class CardGame { awaitedDiceSize = 6; minimumDiceRoll = 4; } + if (cardStack != null && cardStack.isEmpty() && getBuildableItems().isEmpty()) { + cardStack = null; // game over + } return true; } else { return false;