Implement animal encounters

This commit is contained in:
Arne Keller 2020-02-24 19:23:53 +01:00
parent 59a2f86a32
commit e36116701b
2 changed files with 116 additions and 12 deletions

View File

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

View File

@ -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<Card> cardStack;
private List<Card> resources = new ArrayList<>();
private Deque<Card> resources = new ArrayDeque<>();
private List<Item> 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<Card> 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<Card> getResources() {
// do not allow caller to modify internal queue
return new ArrayDeque<>(resources);
}
public List<Item> 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;
}
}