Correctly handle reset by reusing start cards and wait for start before allowing commands

This commit is contained in:
Arne Keller 2020-02-29 16:23:36 +01:00
parent 52ab63f578
commit 1a93c5d061
7 changed files with 35 additions and 20 deletions

View File

@ -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

View File

@ -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

View File

@ -79,4 +79,3 @@ tiger
survived
thunderstorm
EMPTY
OK

View File

@ -30,6 +30,7 @@ import static edu.kit.informatik.model.Item.STEAMBOAT;
public class CardGame {
private Deque<Card> cardStack;
private Deque<Card> currentCardStack;
private Deque<Card> resources = new ArrayDeque<>();
private List<Item> 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<Card> getResources() {
private boolean gameStarted() {
return cardStack != null;
}
public Deque<Card> 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<Item> getItems() {
public List<Item> 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<Item> getBuildableItems() {
public Set<Item> 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;

View File

@ -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<Item> buildable = game.getBuildableItems();
if (buildable.isEmpty()) {
Terminal.printLine("EMPTY");

View File

@ -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<Item> items = game.getItems();
if (items.isEmpty()) {
Terminal.printLine("EMPTY");

View File

@ -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<Card> resources = game.getResources();
if (resources.isEmpty()) {
Terminal.printLine("EMPTY");