summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2016-12-22 16:03:10 +0100
committerjbion <joffrey.bion@amadeus.com>2016-12-22 17:47:58 +0100
commitb33228829b5bfa1de53ac4497104f98c778a6f16 (patch)
tree15ebc5c07bc67d185503955682f0ddf5f2a2a37b /src/main/java
parentAdd BoughtResources representation (diff)
downloadseven-wonders-b33228829b5bfa1de53ac4497104f98c778a6f16.tar.gz
seven-wonders-b33228829b5bfa1de53ac4497104f98c778a6f16.tar.bz2
seven-wonders-b33228829b5bfa1de53ac4497104f98c778a6f16.zip
Handle discarded cards at end of age
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Game.java44
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/cards/Hands.java14
2 files changed, 49 insertions, 9 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java
index dd98f421..1fab129c 100644
--- a/src/main/java/org/luxons/sevenwonders/game/Game.java
+++ b/src/main/java/org/luxons/sevenwonders/game/Game.java
@@ -96,18 +96,31 @@ public class Game {
}
public void playTurn() {
+ makeMoves();
+ if (endOfAgeReached()) {
+ executeEndOfAgeEvents();
+ startNewAge();
+ } else {
+ hands.rotate(getHandRotationOffset());
+ }
+ }
+
+ private void makeMoves() {
List<Move> playedMoves = mapToList(preparedMoves);
- // cards need to be all placed first as some effects depend on just-played cards
+ // all cards from this turn need to be placed before executing any effect
+ // because effects depending on played cards need to take the ones from the current turn into account too
placePreparedCards(playedMoves);
+
+ // same goes for the discarded cards during the last turn, which should be available for special actions
+ if (lastTurnOfAge()) {
+ discardedCards.addAll(hands.gatherAndClear());
+ }
+
activatePlayedCards(playedMoves);
+
table.setLastPlayedMoves(playedMoves);
preparedMoves.clear();
- if (endOfAgeReached()) {
- startNewAge();
- } else {
- hands.rotate(getHandRotationOffset());
- }
}
private static List<Move> mapToList(Map<Integer, Move> movesPerPlayer) {
@@ -125,11 +138,14 @@ public class Game {
private void placePreparedCards(List<Move> playedMoves) {
playedMoves.forEach(move -> {
placeCard(move);
- Card card = decks.getCard(move.getCardName());
- hands.get(move.getPlayerIndex()).remove(card);
+ removeFromHand(move.getPlayerIndex(), move.getCardName());
});
}
+ private boolean lastTurnOfAge() {
+ return hands.maxOneCardRemains();
+ }
+
private void placeCard(Move move) {
switch (move.getType()) {
case PLAY:
@@ -144,6 +160,12 @@ public class Game {
}
}
+ private void removeFromHand(int playerIndex, String cardName) {
+ Card card = decks.getCard(cardName);
+ List<Card> hand = hands.get(playerIndex);
+ hand.remove(card);
+ }
+
private void activatePlayedCards(List<Move> playedMoves) {
playedMoves.forEach(this::activateCard);
}
@@ -163,7 +185,11 @@ public class Game {
}
private boolean endOfAgeReached() {
- return hands.get(0).size() == 1;
+ return hands.isEmpty();
+ }
+
+ private void executeEndOfAgeEvents() {
+ // TODO resolve military conflicts
}
private int getHandRotationOffset() {
diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java b/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java
index 4a5588c9..4d09c53b 100644
--- a/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java
+++ b/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java
@@ -42,6 +42,20 @@ public class Hands {
return new Hands(newHands, nbPlayers);
}
+ public boolean isEmpty() {
+ return hands.values().stream().allMatch(List::isEmpty);
+ }
+
+ public boolean maxOneCardRemains() {
+ return hands.values().stream().mapToInt(List::size).max().orElse(0) <= 1;
+ }
+
+ public List<Card> gatherAndClear() {
+ List<Card> remainingCards = hands.values().stream().flatMap(List::stream).collect(Collectors.toList());
+ hands.clear();
+ return remainingCards;
+ }
+
class PlayerIndexOutOfBoundsException extends ArrayIndexOutOfBoundsException {
PlayerIndexOutOfBoundsException(int index) {
super(index);
bgstack15