Correctly handle game end

This commit is contained in:
Arne Keller 2020-03-03 18:32:00 +01:00
parent 6c8f172d3f
commit 72c9922c44
4 changed files with 64 additions and 5 deletions

View File

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

View File

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

View File

@ -78,4 +78,4 @@ survived
tiger
survived
thunderstorm
EMPTY
Error, can not get resources: game not started

View File

@ -44,7 +44,7 @@ public class CardGame {
* @return whether the game could be successfully started
*/
public boolean start(Deque<Card> 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<Card> 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);