From e36116701b6c5daa96651b047460129296e47dbf Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Mon, 24 Feb 2020 19:23:53 +0100 Subject: [PATCH] Implement animal encounters --- src/edu/kit/informatik/model/Card.java | 34 ++++++++ src/edu/kit/informatik/model/CardGame.java | 94 +++++++++++++++++++--- 2 files changed, 116 insertions(+), 12 deletions(-) diff --git a/src/edu/kit/informatik/model/Card.java b/src/edu/kit/informatik/model/Card.java index fc27309..ad7bd00 100644 --- a/src/edu/kit/informatik/model/Card.java +++ b/src/edu/kit/informatik/model/Card.java @@ -13,6 +13,40 @@ public enum Card { return this.equals(WOOD) || this.equals(METAL) || this.equals(PLASTIC); } + public boolean needsDiceRoll() { + return this.equals(SPIDER) || this.equals(SNAKE) || this.equals(TIGER); + } + + public int diceSizeNeeded() { + switch (this) { + case SPIDER: + return 4; + case SNAKE: + return 6; + case TIGER: + return 8; + default: + return 0; + } + } + + public int minimumDiceRollNeeded() { + switch (this) { + case SPIDER: + return 3; + case SNAKE: + return 4; + case TIGER: + return 5; + default: + return 0; + } + } + + public boolean isCatastrophe() { + return this.equals(THUNDERSTORM); + } + public static Card parse(String input) { switch (input) { case "wood": diff --git a/src/edu/kit/informatik/model/CardGame.java b/src/edu/kit/informatik/model/CardGame.java index b7a248f..2c20812 100644 --- a/src/edu/kit/informatik/model/CardGame.java +++ b/src/edu/kit/informatik/model/CardGame.java @@ -2,14 +2,25 @@ package edu.kit.informatik.model; import edu.kit.informatik.ui.InvalidInputException; +import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Arrays; import java.util.Deque; import java.util.List; +import java.util.Objects; + +import static edu.kit.informatik.model.Item.AXE; +import static edu.kit.informatik.model.Item.CLUB; +import static edu.kit.informatik.model.Item.FIREPLACE; +import static edu.kit.informatik.model.Item.SHACK; public class CardGame { private Deque cardStack; - private List resources = new ArrayList<>(); + private Deque resources = new ArrayDeque<>(); private List buildings = new ArrayList<>(); + private boolean fightingAnimal = false; + private Integer awaitedDiceSize = null; + private Integer minimumDiceRoll = null; /** * @@ -29,25 +40,81 @@ public class CardGame { if (cardStack == null || cardStack.isEmpty()) { throw new InvalidInputException("no card to draw exists"); } - // TODO: awaiting dice roll + if (awaitedDiceSize != null) { + throw new InvalidInputException("roll dice, please"); + } Card card = cardStack.removeFirst(); if (card.isResource()) { resources.add(card); + } else if (card.needsDiceRoll()) { + fightingAnimal = true; + awaitedDiceSize = card.diceSizeNeeded(); + minimumDiceRoll = card.minimumDiceRollNeeded(); + } else if (card.isCatastrophe()) { + clearResources(); + buildings.remove(FIREPLACE); } - // TODO: make game await dice roll return card; } - public boolean build(Item item) throws InvalidInputException { - Card[] resourcesNeeded = item.resourcesNeeded(); - // TODO - buildings.add(item); - return true; + public String rollDice(int size, int roll) throws InvalidInputException { + if (awaitedDiceSize == null) { + throw new InvalidInputException("not expecting dice roll"); + } + if (awaitedDiceSize != size) { + throw new InvalidInputException("unexpected dice size"); + } + if (fightingAnimal) { + int bonus = buildings.contains(AXE) ? 2 : buildings.contains(CLUB) ? 1 : 0; + if (roll + bonus < minimumDiceRoll) { + clearResources(); + return "lose"; + } else { + return "survived"; + } + } else { + // TODO trying to flee + return "win"; + // return "lose"; + } } - public List getResources() { - // do not allow caller to modify internal list - return new ArrayList<>(resources); + public void clearResources() { + int keepLastResources = buildings.contains(SHACK) ? 5 : 0; + while (resources.size() > keepLastResources) { + resources.removeFirst(); + } + } + + public boolean build(Item item) throws InvalidInputException { + if (item == null) { + throw new InvalidInputException("can not build null item"); + } + Card[] resourcesNeeded = item.resourcesNeeded(); + for (Card resource : resources) { + for (int j = 0; j < resourcesNeeded.length; j++) { + if (resourcesNeeded[j] != null && resourcesNeeded[j] == resource) { + resourcesNeeded[j] = null; + break; + } + } + } + if (Arrays.stream(resourcesNeeded).allMatch(Objects::isNull)) { + // remove used resources + for (Card resource : item.resourcesNeeded()) { + assert(resources.remove(resource)); // TODO: remove assert + } + buildings.add(item); + // TODO: building to flee + return true; + } else { + return false; + } + } + + public Deque getResources() { + // do not allow caller to modify internal queue + return new ArrayDeque<>(resources); } public List getBuildings() { @@ -57,7 +124,10 @@ public class CardGame { public void reset() { this.cardStack = null; - this.resources = new ArrayList<>(); + this.resources = new ArrayDeque<>(); this.buildings = new ArrayList<>(); + this.fightingAnimal = false; + this.awaitedDiceSize = null; + this.minimumDiceRoll = null; } }