mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final2.git
synced 2024-11-27 18:55:54 +00:00
Refactor card stack into own class
This commit is contained in:
parent
44412a6d09
commit
7a09111164
@ -18,13 +18,9 @@ import java.util.stream.Collectors;
|
|||||||
*/
|
*/
|
||||||
public class CardGame {
|
public class CardGame {
|
||||||
/**
|
/**
|
||||||
* Copy of the card stack used to start the game.
|
* Card stack used.
|
||||||
*/
|
*/
|
||||||
private Deque<Card> cardStack;
|
private CardStack cardStack;
|
||||||
/**
|
|
||||||
* Currently active card stack.
|
|
||||||
*/
|
|
||||||
private Deque<Card> currentCardStack;
|
|
||||||
/**
|
/**
|
||||||
* Resources collected by the player. Obviously only contains {@link CardCategory#RESOURCE resource} cards.
|
* 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())) {
|
.allMatch(card -> Collections.frequency(cardStack, card) == card.requiredAmount())) {
|
||||||
throw new LogicException("invalid deck: missing or surplus cards");
|
throw new LogicException("invalid deck: missing or surplus cards");
|
||||||
}
|
}
|
||||||
this.cardStack = new ArrayDeque<>(cardStack);
|
this.cardStack = new CardStack(cardStack);
|
||||||
reset();
|
reset();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -70,12 +66,12 @@ public class CardGame {
|
|||||||
* @throws LogicException if no card to draw exists or phase is not scavenge
|
* @throws LogicException if no card to draw exists or phase is not scavenge
|
||||||
*/
|
*/
|
||||||
public Card draw() throws LogicException {
|
public Card draw() throws LogicException {
|
||||||
if (currentCardStack == null || currentCardStack.isEmpty()) {
|
if (cardStack == null) {
|
||||||
throw new LogicException("no card to draw exists");
|
throw new LogicException("no card to draw exists");
|
||||||
} else if (phase != Phase.SCAVENGE) {
|
} else if (phase != Phase.SCAVENGE) {
|
||||||
throw new LogicException("can only draw cards in scavenge phase");
|
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()) {
|
switch (card.category()) {
|
||||||
case RESOURCE:
|
case RESOURCE:
|
||||||
resources.add(card);
|
resources.add(card);
|
||||||
@ -245,7 +241,7 @@ public class CardGame {
|
|||||||
*/
|
*/
|
||||||
private void checkLost() {
|
private void checkLost() {
|
||||||
// can not draw new cards
|
// can not draw new cards
|
||||||
if (currentCardStack != null && currentCardStack.isEmpty()
|
if (cardStack != null && cardStack.isEmpty()
|
||||||
// can not roll dice
|
// can not roll dice
|
||||||
&& phase != Phase.ENCOUNTER && phase != Phase.ENDEAVOR
|
&& phase != Phase.ENCOUNTER && phase != Phase.ENDEAVOR
|
||||||
// can not build item
|
// can not build item
|
||||||
@ -312,7 +308,7 @@ public class CardGame {
|
|||||||
* End the current game, deleting the active card stack.
|
* End the current game, deleting the active card stack.
|
||||||
*/
|
*/
|
||||||
private void endGame() {
|
private void endGame() {
|
||||||
currentCardStack = null;
|
cardStack.clearActiveStack();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -324,7 +320,7 @@ public class CardGame {
|
|||||||
if (cardStack == null) {
|
if (cardStack == null) {
|
||||||
throw new LogicException("can not reset a game that is not started!");
|
throw new LogicException("can not reset a game that is not started!");
|
||||||
} else {
|
} else {
|
||||||
this.currentCardStack = new ArrayDeque<>(cardStack);
|
this.cardStack.reset();
|
||||||
}
|
}
|
||||||
this.resources.clear();
|
this.resources.clear();
|
||||||
this.items.clear();
|
this.items.clear();
|
||||||
|
38
src/edu/kit/informatik/cardgame/model/CardStack.java
Normal file
38
src/edu/kit/informatik/cardgame/model/CardStack.java
Normal file
@ -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<Card> stack;
|
||||||
|
/**
|
||||||
|
* Currently active card stack.
|
||||||
|
*/
|
||||||
|
private Deque<Card> activeStack;
|
||||||
|
|
||||||
|
public CardStack(Deque<Card> cards) {
|
||||||
|
// copy stack to avoid modifications
|
||||||
|
this.stack = new ArrayDeque<>(cards);
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Card> draw() {
|
||||||
|
return Optional.ofNullable(activeStack.pollFirst());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearActiveStack() {
|
||||||
|
activeStack.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
activeStack = new ArrayDeque<>(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return activeStack.isEmpty();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user