diff --git a/src/edu/kit/informatik/cardgame/model/CardGame.java b/src/edu/kit/informatik/cardgame/model/CardGame.java index 698e5a2..2a89c0e 100644 --- a/src/edu/kit/informatik/cardgame/model/CardGame.java +++ b/src/edu/kit/informatik/cardgame/model/CardGame.java @@ -18,13 +18,9 @@ import java.util.stream.Collectors; */ public class CardGame { /** - * Copy of the card stack used to start the game. + * Card stack used. */ - private Deque cardStack; - /** - * Currently active card stack. - */ - private Deque currentCardStack; + private CardStack cardStack; /** * Resources collected by the player. Obviously only contains {@link CardCategory#RESOURCE resource} cards. */ @@ -57,7 +53,7 @@ public class CardGame { .allMatch(card -> Collections.frequency(cardStack, card) == card.requiredAmount())) { throw new LogicException("invalid deck: missing or surplus cards"); } - this.cardStack = new ArrayDeque<>(cardStack); + this.cardStack = new CardStack(cardStack); reset(); return true; } @@ -70,12 +66,12 @@ public class CardGame { * @throws LogicException if no card to draw exists or phase is not scavenge */ public Card draw() throws LogicException { - if (currentCardStack == null || currentCardStack.isEmpty()) { + if (cardStack == null) { throw new LogicException("no card to draw exists"); } else if (phase != Phase.SCAVENGE) { throw new LogicException("can only draw cards in scavenge phase"); } - final Card card = currentCardStack.removeFirst(); + final Card card = cardStack.draw().orElseThrow(() -> new LogicException("no card to draw exists")); switch (card.category()) { case RESOURCE: resources.add(card); @@ -245,7 +241,7 @@ public class CardGame { */ private void checkLost() { // can not draw new cards - if (currentCardStack != null && currentCardStack.isEmpty() + if (cardStack != null && cardStack.isEmpty() // can not roll dice && phase != Phase.ENCOUNTER && phase != Phase.ENDEAVOR // can not build item @@ -312,7 +308,7 @@ public class CardGame { * End the current game, deleting the active card stack. */ private void endGame() { - currentCardStack = null; + cardStack.clearActiveStack(); } /** @@ -324,7 +320,7 @@ public class CardGame { if (cardStack == null) { throw new LogicException("can not reset a game that is not started!"); } else { - this.currentCardStack = new ArrayDeque<>(cardStack); + this.cardStack.reset(); } this.resources.clear(); this.items.clear(); diff --git a/src/edu/kit/informatik/cardgame/model/CardStack.java b/src/edu/kit/informatik/cardgame/model/CardStack.java new file mode 100644 index 0000000..90fa38b --- /dev/null +++ b/src/edu/kit/informatik/cardgame/model/CardStack.java @@ -0,0 +1,38 @@ +package edu.kit.informatik.cardgame.model; + +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.Optional; + +public class CardStack { + /** + * Copy of the card stack used to initialize the stack. + */ + private final Deque stack; + /** + * Currently active card stack. + */ + private Deque activeStack; + + public CardStack(Deque cards) { + // copy stack to avoid modifications + this.stack = new ArrayDeque<>(cards); + reset(); + } + + public Optional draw() { + return Optional.ofNullable(activeStack.pollFirst()); + } + + public void clearActiveStack() { + activeStack.clear(); + } + + public void reset() { + activeStack = new ArrayDeque<>(stack); + } + + public boolean isEmpty() { + return activeStack.isEmpty(); + } +}