From b33228829b5bfa1de53ac4497104f98c778a6f16 Mon Sep 17 00:00:00 2001 From: jbion Date: Thu, 22 Dec 2016 16:03:10 +0100 Subject: Handle discarded cards at end of age --- .../java/org/luxons/sevenwonders/game/Game.java | 44 +++++++++++++++++----- .../org/luxons/sevenwonders/game/cards/Hands.java | 14 +++++++ 2 files changed, 49 insertions(+), 9 deletions(-) (limited to 'src/main/java/org/luxons/sevenwonders') 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 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 mapToList(Map movesPerPlayer) { @@ -125,11 +138,14 @@ public class Game { private void placePreparedCards(List 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 hand = hands.get(playerIndex); + hand.remove(card); + } + private void activatePlayedCards(List 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 gatherAndClear() { + List remainingCards = hands.values().stream().flatMap(List::stream).collect(Collectors.toList()); + hands.clear(); + return remainingCards; + } + class PlayerIndexOutOfBoundsException extends ArrayIndexOutOfBoundsException { PlayerIndexOutOfBoundsException(int index) { super(index); -- cgit