Lose game after encountering animal with no actions left

This commit is contained in:
Arne Keller 2020-03-12 22:25:01 +01:00
parent 22446ea8aa
commit 48d014e4de
3 changed files with 51 additions and 17 deletions

View File

@ -9,6 +9,23 @@ public enum Card {
TIGER, TIGER,
THUNDERSTORM; 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() { public boolean isResource() {
return this.equals(WOOD) || this.equals(METAL) || this.equals(PLASTIC); return this.equals(WOOD) || this.equals(METAL) || this.equals(PLASTIC);
} }

View File

@ -0,0 +1,7 @@
package edu.kit.informatik.cardgame.model;
public enum CardCategory {
RESOURCE,
ANIMAL,
CATASTROPHE
}

View File

@ -45,20 +45,21 @@ public class CardGame {
throw new LogicException("can only draw cards in scavenge phase"); throw new LogicException("can only draw cards in scavenge phase");
} }
final Card card = currentCardStack.removeFirst(); final Card card = currentCardStack.removeFirst();
if (card.isResource()) { switch (card.category()) {
resources.add(card); case RESOURCE:
} else if (card.needsDiceRoll()) { resources.add(card);
awaitedDiceSize = card.diceSizeNeeded(); break;
minimumDiceRoll = card.minimumDiceRollNeeded(); case ANIMAL:
phase = Phase.ENCOUNTER; awaitedDiceSize = card.diceSizeNeeded();
} else if (card.isCatastrophe()) { minimumDiceRoll = card.minimumDiceRollNeeded();
clearResources(); phase = Phase.ENCOUNTER;
items.remove(Item.FIREPLACE); break;
} case CATASTROPHE:
if (currentCardStack.isEmpty() && getBuildableItems().isEmpty()) { clearResources();
endGame(); items.remove(Item.FIREPLACE);
this.phase = Phase.LOST; break;
} }
checkLost();
return card; return card;
} }
@ -76,6 +77,7 @@ public class CardGame {
if (phase == Phase.ENCOUNTER) { if (phase == Phase.ENCOUNTER) {
final int bonus = items.stream().mapToInt(Item::fightingBonus).max().orElse(0); final int bonus = items.stream().mapToInt(Item::fightingBonus).max().orElse(0);
phase = Phase.SCAVENGE; phase = Phase.SCAVENGE;
checkLost();
if (roll + bonus >= minimumNeeded) { if (roll + bonus >= minimumNeeded) {
return "survived"; return "survived";
} else { } else {
@ -90,6 +92,7 @@ public class CardGame {
return "win"; return "win";
} else { } else {
phase = Phase.SCAVENGE; phase = Phase.SCAVENGE;
checkLost();
return "lose"; return "lose";
} }
} }
@ -130,10 +133,7 @@ public class CardGame {
} }
break; break;
case DEFAULT: case DEFAULT:
if (currentCardStack != null && currentCardStack.isEmpty() && getBuildableItems().isEmpty()) { checkLost();
endGame();
phase = Phase.LOST;
}
break; break;
} }
@ -170,6 +170,16 @@ public class CardGame {
return gameStarted() && phase != Phase.WON && phase != Phase.LOST; 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() { public boolean gameLost() {
return phase == Phase.LOST; return phase == Phase.LOST;
} }