From 48d014e4de2b14c662a7b4f2aab4d3ab7ddc6e6d Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Thu, 12 Mar 2020 22:25:01 +0100 Subject: [PATCH] Lose game after encountering animal with no actions left --- .../kit/informatik/cardgame/model/Card.java | 17 +++++++ .../cardgame/model/CardCategory.java | 7 +++ .../informatik/cardgame/model/CardGame.java | 44 ++++++++++++------- 3 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 src/edu/kit/informatik/cardgame/model/CardCategory.java diff --git a/src/edu/kit/informatik/cardgame/model/Card.java b/src/edu/kit/informatik/cardgame/model/Card.java index c36dbe0..bae7379 100644 --- a/src/edu/kit/informatik/cardgame/model/Card.java +++ b/src/edu/kit/informatik/cardgame/model/Card.java @@ -9,6 +9,23 @@ public enum Card { TIGER, THUNDERSTORM; + public CardCategory category() { + switch (this) { + case WOOD: + case METAL: + case PLASTIC: + return CardCategory.RESOURCE; + case SPIDER: + case SNAKE: + case TIGER: + return CardCategory.ANIMAL; + case THUNDERSTORM: + return CardCategory.CATASTROPHE; + default: + return null; + } + } + public boolean isResource() { return this.equals(WOOD) || this.equals(METAL) || this.equals(PLASTIC); } diff --git a/src/edu/kit/informatik/cardgame/model/CardCategory.java b/src/edu/kit/informatik/cardgame/model/CardCategory.java new file mode 100644 index 0000000..9605652 --- /dev/null +++ b/src/edu/kit/informatik/cardgame/model/CardCategory.java @@ -0,0 +1,7 @@ +package edu.kit.informatik.cardgame.model; + +public enum CardCategory { + RESOURCE, + ANIMAL, + CATASTROPHE +} diff --git a/src/edu/kit/informatik/cardgame/model/CardGame.java b/src/edu/kit/informatik/cardgame/model/CardGame.java index b50024d..dd810ff 100644 --- a/src/edu/kit/informatik/cardgame/model/CardGame.java +++ b/src/edu/kit/informatik/cardgame/model/CardGame.java @@ -45,20 +45,21 @@ public class CardGame { throw new LogicException("can only draw cards in scavenge phase"); } final Card card = currentCardStack.removeFirst(); - if (card.isResource()) { - resources.add(card); - } else if (card.needsDiceRoll()) { - awaitedDiceSize = card.diceSizeNeeded(); - minimumDiceRoll = card.minimumDiceRollNeeded(); - phase = Phase.ENCOUNTER; - } else if (card.isCatastrophe()) { - clearResources(); - items.remove(Item.FIREPLACE); - } - if (currentCardStack.isEmpty() && getBuildableItems().isEmpty()) { - endGame(); - this.phase = Phase.LOST; + switch (card.category()) { + case RESOURCE: + resources.add(card); + break; + case ANIMAL: + awaitedDiceSize = card.diceSizeNeeded(); + minimumDiceRoll = card.minimumDiceRollNeeded(); + phase = Phase.ENCOUNTER; + break; + case CATASTROPHE: + clearResources(); + items.remove(Item.FIREPLACE); + break; } + checkLost(); return card; } @@ -76,6 +77,7 @@ public class CardGame { if (phase == Phase.ENCOUNTER) { final int bonus = items.stream().mapToInt(Item::fightingBonus).max().orElse(0); phase = Phase.SCAVENGE; + checkLost(); if (roll + bonus >= minimumNeeded) { return "survived"; } else { @@ -90,6 +92,7 @@ public class CardGame { return "win"; } else { phase = Phase.SCAVENGE; + checkLost(); return "lose"; } } @@ -130,10 +133,7 @@ public class CardGame { } break; case DEFAULT: - if (currentCardStack != null && currentCardStack.isEmpty() && getBuildableItems().isEmpty()) { - endGame(); - phase = Phase.LOST; - } + checkLost(); break; } @@ -170,6 +170,16 @@ public class CardGame { return gameStarted() && phase != Phase.WON && phase != Phase.LOST; } + private void checkLost() { + if (phase != Phase.LOST + && currentCardStack != null && currentCardStack.isEmpty() + && phase != Phase.ENCOUNTER && phase != Phase.ENDEAVOR + && !Arrays.stream(Item.values()).anyMatch(this::canBuild)) { + endGame(); + phase = Phase.LOST; + } + } + public boolean gameLost() { return phase == Phase.LOST; }