From 33bf28485e310ea9a9c3a5acaf74c56970899fab Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Tue, 17 Mar 2020 11:41:54 +0100 Subject: [PATCH] Create interface for objects that require dice roll --- .../kit/informatik/cardgame/model/Card.java | 26 +++++++----- .../informatik/cardgame/model/CardGame.java | 27 ++++++------ .../kit/informatik/cardgame/model/Item.java | 42 +++++-------------- .../cardgame/model/RequireDice.java | 15 +++++++ 4 files changed, 52 insertions(+), 58 deletions(-) create mode 100644 src/edu/kit/informatik/cardgame/model/RequireDice.java diff --git a/src/edu/kit/informatik/cardgame/model/Card.java b/src/edu/kit/informatik/cardgame/model/Card.java index b54e26b..234bf68 100644 --- a/src/edu/kit/informatik/cardgame/model/Card.java +++ b/src/edu/kit/informatik/cardgame/model/Card.java @@ -1,5 +1,7 @@ package edu.kit.informatik.cardgame.model; +import java.util.Optional; + /** * Game card. * @@ -7,7 +9,7 @@ package edu.kit.informatik.cardgame.model; * @author Arne Keller * @version 1.0 */ -public enum Card { +public enum Card implements RequireDice { /** * Wood. Basic resource. */ @@ -54,29 +56,31 @@ public enum Card { } } - public int diceSizeNeeded() { + @Override + public Optional diceSizeNeeded() { switch (this) { case SPIDER: - return 4; + return Optional.of(4); case SNAKE: - return 6; + return Optional.of(6); case TIGER: - return 8; + return Optional.of(8); default: - return 0; + return Optional.empty(); } } - public int minimumDiceRollNeeded() { + @Override + public Optional minimumDiceRollNeeded() { switch (this) { case SPIDER: - return 3; + return Optional.of(3); case SNAKE: - return 4; + return Optional.of(4); case TIGER: - return 5; + return Optional.of(5); default: - return 0; + return Optional.empty(); } } diff --git a/src/edu/kit/informatik/cardgame/model/CardGame.java b/src/edu/kit/informatik/cardgame/model/CardGame.java index 2a89a2d..8b0cd9d 100644 --- a/src/edu/kit/informatik/cardgame/model/CardGame.java +++ b/src/edu/kit/informatik/cardgame/model/CardGame.java @@ -33,8 +33,7 @@ public class CardGame { * Items built by the player. */ private final List items = new ArrayList<>(); - private Integer awaitedDiceSize = null; - private Integer minimumDiceRoll = null; + private RequireDice requireDice = null; /** * Current game phase. */ @@ -79,8 +78,7 @@ public class CardGame { resources.add(card); break; case ANIMAL: - awaitedDiceSize = card.diceSizeNeeded(); - minimumDiceRoll = card.minimumDiceRollNeeded(); + requireDice = card; phase = Phase.ENCOUNTER; break; case CATASTROPHE: @@ -103,16 +101,17 @@ public class CardGame { * @throws LogicException if not expecting dice roll or dice roll is invalid */ public String rollDice(int size, int roll) throws LogicException { - if (phase != Phase.ENDEAVOR && phase != Phase.ENCOUNTER) { + if (requireDice == null) { throw new LogicException("not expecting dice roll"); - } else if (awaitedDiceSize != size) { + } + int sizeNeeded = requireDice.diceSizeNeeded().get(); + int minimumNeeded = requireDice.minimumDiceRollNeeded().get(); + if (sizeNeeded != size) { throw new LogicException("unexpected dice size"); - } else if (roll > awaitedDiceSize || roll < 1) { + } else if (roll > sizeNeeded || roll < 1) { throw new LogicException("impossible roll"); } - awaitedDiceSize = null; - final int minimumNeeded = minimumDiceRoll; - minimumDiceRoll = null; + requireDice = null; if (phase == Phase.ENCOUNTER) { final int bonus = items.stream().mapToInt(Item::fightingBonus).max().orElse(0); phase = Phase.SCAVENGE; @@ -170,10 +169,9 @@ public class CardGame { items.add(item); switch (item.category()) { case ESCAPE: - if (item.requiresDice()) { + if (item.diceSizeNeeded().isPresent()) { // need at least a 4/d6 - awaitedDiceSize = item.diceSizeNeeded(); - minimumDiceRoll = item.minimumDiceRollNeeded(); + requireDice = item; phase = Phase.ENDEAVOR; } else { // player won @@ -321,8 +319,7 @@ public class CardGame { } this.resources.clear(); this.items.clear(); - this.awaitedDiceSize = null; - this.minimumDiceRoll = null; + this.requireDice = null; this.phase = Phase.SCAVENGE; } diff --git a/src/edu/kit/informatik/cardgame/model/Item.java b/src/edu/kit/informatik/cardgame/model/Item.java index fc0054a..56b603d 100644 --- a/src/edu/kit/informatik/cardgame/model/Item.java +++ b/src/edu/kit/informatik/cardgame/model/Item.java @@ -2,6 +2,7 @@ package edu.kit.informatik.cardgame.model; import java.util.Collection; import java.util.Collections; +import java.util.Optional; import static edu.kit.informatik.cardgame.model.Card.METAL; import static edu.kit.informatik.cardgame.model.Card.PLASTIC; @@ -14,7 +15,7 @@ import static edu.kit.informatik.cardgame.model.Card.WOOD; * @author Arne Keller * @version 1.0 */ -public enum Item { +public enum Item implements RequireDice { /** * Axe. Provides an attack bonus of two. */ @@ -117,48 +118,25 @@ public enum Item { return this == Item.SHACK ? 5 : 0; } - /** - * Check whether this item requires rolling a dice to take effect. - * - * @return whether the player has to roll a dice after building this item - */ - public boolean requiresDice() { + @Override + public Optional diceSizeNeeded() { switch (this) { case HANG_GLIDER: case SAILING_RAFT: - return true; + return Optional.of(6); default: - return false; + return Optional.empty(); } } - /** - * Get the size of the dice to roll after building this item. - * - * @return size of the dice to roll, or 0 if not necessary - */ - public int diceSizeNeeded() { + @Override + public Optional minimumDiceRollNeeded() { switch (this) { case HANG_GLIDER: case SAILING_RAFT: - return 6; + return Optional.of(4); default: - return 0; - } - } - - /** - * Get the minimum dice roll needed to activate this item if it needs a dice roll. - * - * @return minimum dice roll needed to use this item - */ - public int minimumDiceRollNeeded() { - switch (this) { - case HANG_GLIDER: - case SAILING_RAFT: - return 4; - default: - return 0; + return Optional.empty(); } } diff --git a/src/edu/kit/informatik/cardgame/model/RequireDice.java b/src/edu/kit/informatik/cardgame/model/RequireDice.java new file mode 100644 index 0000000..dc2bdc8 --- /dev/null +++ b/src/edu/kit/informatik/cardgame/model/RequireDice.java @@ -0,0 +1,15 @@ +package edu.kit.informatik.cardgame.model; + +import java.util.Optional; + +public interface RequireDice { + Optional diceSizeNeeded(); + + + /** + * Get the minimum dice roll needed to activate this object if it needs a dice roll. + * + * @return minimum dice roll needed to use this item + */ + Optional minimumDiceRollNeeded(); +}