mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final2.git
synced 2024-11-24 01:15:03 +00:00
Use logic exception in model classes
This commit is contained in:
parent
4fc13f159c
commit
8e3bd2bd25
@ -40,16 +40,16 @@ public class CardGame {
|
||||
/**
|
||||
* Start a new game with the specified stack of cards.
|
||||
* @param cardStack stack of cards to use, where the first element is the first to take
|
||||
* @throws InvalidInputException if card stack has wrong distribution of cards
|
||||
* @throws LogicException if card stack has wrong distribution of cards
|
||||
* @return whether the game could be successfully started
|
||||
*/
|
||||
public boolean start(Deque<Card> cardStack) throws InvalidInputException {
|
||||
public boolean start(Deque<Card> cardStack) throws LogicException {
|
||||
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
|
||||
|| Collections.frequency(cardStack, THUNDERSTORM) != 1) {
|
||||
throw new InvalidInputException("invalid deck: missing or surplus cards");
|
||||
throw new LogicException("invalid deck: missing or surplus cards");
|
||||
}
|
||||
this.cardStack = new ArrayDeque<>(cardStack);
|
||||
reset();
|
||||
@ -59,11 +59,11 @@ public class CardGame {
|
||||
}
|
||||
}
|
||||
|
||||
public Card draw() throws InvalidInputException {
|
||||
public Card draw() throws LogicException {
|
||||
if (currentCardStack == null || currentCardStack.isEmpty()) {
|
||||
throw new InvalidInputException("no card to draw exists");
|
||||
throw new LogicException("no card to draw exists");
|
||||
} else if (awaitedDiceSize != null) {
|
||||
throw new InvalidInputException("roll dice, please");
|
||||
throw new LogicException("roll dice, please");
|
||||
}
|
||||
final Card card = currentCardStack.removeFirst();
|
||||
if (card.isResource()) {
|
||||
@ -82,13 +82,13 @@ public class CardGame {
|
||||
return card;
|
||||
}
|
||||
|
||||
public String rollDice(int size, int roll) throws InvalidInputException {
|
||||
public String rollDice(int size, int roll) throws LogicException {
|
||||
if (awaitedDiceSize == null) {
|
||||
throw new InvalidInputException("not expecting dice roll");
|
||||
throw new LogicException("not expecting dice roll");
|
||||
} else if (awaitedDiceSize != size) {
|
||||
throw new InvalidInputException("unexpected dice size");
|
||||
throw new LogicException("unexpected dice size");
|
||||
} else if (roll > awaitedDiceSize || roll < 1) {
|
||||
throw new InvalidInputException("impossible roll");
|
||||
throw new LogicException("impossible roll");
|
||||
}
|
||||
awaitedDiceSize = null;
|
||||
final int minimumNeeded = minimumDiceRoll;
|
||||
@ -120,13 +120,13 @@ public class CardGame {
|
||||
}
|
||||
}
|
||||
|
||||
public String build(Item item) throws InvalidInputException {
|
||||
public String build(Item item) throws LogicException {
|
||||
if (item == null) {
|
||||
throw new InvalidInputException("can not build item");
|
||||
throw new LogicException("can not build item");
|
||||
} else if (item.requiresFireplace() && !items.contains(FIREPLACE)) {
|
||||
throw new InvalidInputException("need fireplace to build");
|
||||
throw new LogicException("need fireplace to build");
|
||||
} else if (items.contains(item) && !item.canHaveMoreThanOne()) {
|
||||
throw new InvalidInputException("already built");
|
||||
throw new LogicException("already built");
|
||||
} else if (canBuild(item)) {
|
||||
// remove used resources
|
||||
for (final Card resource : item.resourcesNeeded()) {
|
||||
@ -175,25 +175,25 @@ public class CardGame {
|
||||
return currentCardStack != null;
|
||||
}
|
||||
|
||||
public Deque<Card> getResources() throws InvalidInputException {
|
||||
public Deque<Card> getResources() throws LogicException {
|
||||
if (!gameStarted()) {
|
||||
throw new InvalidInputException("can not get resources: game not started");
|
||||
throw new LogicException("can not get resources: game not started");
|
||||
}
|
||||
// do not allow caller to modify internal queue
|
||||
return new ArrayDeque<>(resources);
|
||||
}
|
||||
|
||||
public List<Item> getItems() throws InvalidInputException {
|
||||
public List<Item> getItems() throws LogicException {
|
||||
if (!gameStarted()) {
|
||||
throw new InvalidInputException("can not get buildings: game not started");
|
||||
throw new LogicException("can not get buildings: game not started");
|
||||
}
|
||||
// do not allow caller to modify internal list
|
||||
return new ArrayList<>(items);
|
||||
}
|
||||
|
||||
public Set<Item> getBuildableItems() throws InvalidInputException {
|
||||
public Set<Item> getBuildableItems() throws LogicException {
|
||||
if (!gameStarted()) {
|
||||
throw new InvalidInputException("can not get buildable items: game not started");
|
||||
throw new LogicException("can not get buildable items: game not started");
|
||||
}
|
||||
return Arrays.stream(Item.values()).filter(this::canBuild).collect(Collectors.toSet());
|
||||
}
|
||||
@ -202,9 +202,9 @@ public class CardGame {
|
||||
currentCardStack = null;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
public void reset() throws LogicException {
|
||||
if (cardStack == null) {
|
||||
throw new InvalidInputException("can not reset a game that is not started!");
|
||||
throw new LogicException("can not reset a game that is not started!");
|
||||
} else {
|
||||
this.currentCardStack = new ArrayDeque<>(cardStack);
|
||||
}
|
||||
|
7
src/edu/kit/informatik/model/LogicException.java
Normal file
7
src/edu/kit/informatik/model/LogicException.java
Normal file
@ -0,0 +1,7 @@
|
||||
package edu.kit.informatik.model;
|
||||
|
||||
public class LogicException extends Exception {
|
||||
public LogicException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package edu.kit.informatik.ui;
|
||||
|
||||
public class InvalidInputException extends RuntimeException {
|
||||
public class InvalidInputException extends Exception {
|
||||
public InvalidInputException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user