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 Error, no card to draw exists
EMPTY Error, can not get buildings: game not started
EMPTY Error, can not get resources: game not started
Error, invalid list-buildings argument: none expected Error, invalid list-buildings argument: none expected
Error, invalid list-resources argument: none expected Error, invalid list-resources argument: none expected
OK OK

View File

@ -79,4 +79,3 @@ draw
rollD8 8 rollD8 8
draw draw
list-resources 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 survived
thunderstorm thunderstorm
EMPTY EMPTY
OK

View File

@ -30,6 +30,7 @@ import static edu.kit.informatik.model.Item.STEAMBOAT;
public class CardGame { public class CardGame {
private Deque<Card> cardStack; private Deque<Card> cardStack;
private Deque<Card> currentCardStack;
private Deque<Card> resources = new ArrayDeque<>(); private Deque<Card> resources = new ArrayDeque<>();
private List<Item> items = new ArrayList<>(); private List<Item> items = new ArrayList<>();
private boolean fightingAnimal = false; private boolean fightingAnimal = false;
@ -50,20 +51,21 @@ public class CardGame {
|| Collections.frequency(cardStack, THUNDERSTORM) != 1) { || Collections.frequency(cardStack, THUNDERSTORM) != 1) {
throw new InvalidInputException("invalid deck: missing or surplus cards"); throw new InvalidInputException("invalid deck: missing or surplus cards");
} }
this.cardStack = cardStack; this.cardStack = new ArrayDeque<>(cardStack);
this.currentCardStack = new ArrayDeque<>(cardStack);
return true; return true;
} else { } else {
return false; // still in game return false; // game already started
} }
} }
public Card draw() throws InvalidInputException { public Card draw() throws InvalidInputException {
if (cardStack == null || cardStack.isEmpty()) { if (currentCardStack == null || currentCardStack.isEmpty()) {
throw new InvalidInputException("no card to draw exists"); throw new InvalidInputException("no card to draw exists");
} else if (awaitedDiceSize != null) { } else if (awaitedDiceSize != null) {
throw new InvalidInputException("roll dice, please"); throw new InvalidInputException("roll dice, please");
} }
Card card = cardStack.removeFirst(); Card card = currentCardStack.removeFirst();
if (card.isResource()) { if (card.isResource()) {
resources.add(card); resources.add(card);
} else if (card.needsDiceRoll()) { } else if (card.needsDiceRoll()) {
@ -74,8 +76,8 @@ public class CardGame {
clearResources(); clearResources();
items.remove(FIREPLACE); items.remove(FIREPLACE);
} }
if (cardStack.isEmpty() && getBuildableItems().isEmpty()) { if (currentCardStack.isEmpty() && getBuildableItems().isEmpty()) {
cardStack = null; // game over currentCardStack = null; // game over
} }
return card; return card;
} }
@ -137,8 +139,8 @@ public class CardGame {
awaitedDiceSize = 6; awaitedDiceSize = 6;
minimumDiceRoll = 4; minimumDiceRoll = 4;
} }
if (cardStack != null && cardStack.isEmpty() && getBuildableItems().isEmpty()) { if (currentCardStack != null && currentCardStack.isEmpty() && getBuildableItems().isEmpty()) {
cardStack = null; // game over currentCardStack = null; // game over
} }
return true; return true;
} else { } else {
@ -159,22 +161,37 @@ public class CardGame {
return Arrays.stream(resourcesNeeded).allMatch(Objects::isNull); 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 // do not allow caller to modify internal queue
return new ArrayDeque<>(resources); 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 // do not allow caller to modify internal list
return new ArrayList<>(items); 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()); return Arrays.stream(Item.values()).filter(this::canBuild).collect(Collectors.toSet());
} }
public void reset() { public void reset() {
this.cardStack = null; if (cardStack != null) {
this.currentCardStack = new ArrayDeque<>(cardStack);
}
this.resources = new ArrayDeque<>(); this.resources = new ArrayDeque<>();
this.items = new ArrayList<>(); this.items = new ArrayList<>();
this.fightingAnimal = false; this.fightingAnimal = false;

View File

@ -12,7 +12,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE;
public class Buildable extends Command { public class Buildable extends Command {
@Override @Override
public void apply(CardGame game) { public void apply(CardGame game) throws InvalidInputException {
Set<Item> buildable = game.getBuildableItems(); Set<Item> buildable = game.getBuildableItems();
if (buildable.isEmpty()) { if (buildable.isEmpty()) {
Terminal.printLine("EMPTY"); Terminal.printLine("EMPTY");

View File

@ -11,7 +11,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_BUILDINGS;
public class ListBuildings extends Command { public class ListBuildings extends Command {
@Override @Override
public void apply(CardGame game) { public void apply(CardGame game) throws InvalidInputException {
List<Item> items = game.getItems(); List<Item> items = game.getItems();
if (items.isEmpty()) { if (items.isEmpty()) {
Terminal.printLine("EMPTY"); Terminal.printLine("EMPTY");

View File

@ -11,7 +11,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_RESOURCES;
public class ListResources extends Command { public class ListResources extends Command {
@Override @Override
public void apply(CardGame game) { public void apply(CardGame game) throws InvalidInputException {
Deque<Card> resources = game.getResources(); Deque<Card> resources = game.getResources();
if (resources.isEmpty()) { if (resources.isEmpty()) {
Terminal.printLine("EMPTY"); Terminal.printLine("EMPTY");