diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-22 02:30:51 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-22 02:59:55 +0100 |
commit | 097c08df4f17ccff079677611fddee1a990a7ab8 (patch) | |
tree | c173f98679a9b9cfbeb738dc093a78bee48f5155 /src/main | |
parent | Move Decks to the cards package (diff) | |
download | seven-wonders-097c08df4f17ccff079677611fddee1a990a7ab8.tar.gz seven-wonders-097c08df4f17ccff079677611fddee1a990a7ab8.tar.bz2 seven-wonders-097c08df4f17ccff079677611fddee1a990a7ab8.zip |
Introduce Hands (pun intended)
Diffstat (limited to 'src/main')
3 files changed, 63 insertions, 26 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java index 389ef844..9023a80f 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Game.java +++ b/src/main/java/org/luxons/sevenwonders/game/Game.java @@ -5,13 +5,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.IntStream; -import org.luxons.sevenwonders.game.api.HandCard; import org.luxons.sevenwonders.game.api.PlayerTurnInfo; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.cards.Card; import org.luxons.sevenwonders.game.cards.Decks; +import org.luxons.sevenwonders.game.cards.Hands; public class Game { @@ -29,7 +30,7 @@ public class Game { private final Map<Integer, Move> preparedMoves; - private Map<Integer, List<Card>> hands; + private Hands hands; private int currentAge = 0; @@ -40,7 +41,6 @@ public class Game { this.table = new Table(boards); this.decks = decks; this.discardedCards = new ArrayList<>(); - this.hands = new HashMap<>(); this.preparedMoves = new HashMap<>(); startNewAge(); } @@ -59,21 +59,16 @@ public class Game { } public List<PlayerTurnInfo> getTurnInfo() { - return hands.entrySet().stream().map(e -> createPlayerTurnInfo(e.getKey(), e.getValue())) - .collect(Collectors.toList()); + return IntStream.range(0, players.size()).mapToObj(this::createPlayerTurnInfo).collect(Collectors.toList()); } - private PlayerTurnInfo createPlayerTurnInfo(int playerIndex, List<Card> cards) { + private PlayerTurnInfo createPlayerTurnInfo(int playerIndex) { PlayerTurnInfo pti = new PlayerTurnInfo(playerIndex, table); - pti.setHand(createHand(playerIndex, cards)); + pti.setHand(hands.createHand(table, playerIndex)); pti.setCurrentAge(currentAge); return pti; } - private List<HandCard> createHand(int playerIndex, List<Card> cards) { - return cards.stream().map(c -> new HandCard(c, table, playerIndex)).collect(Collectors.toList()); - } - public void prepareCard(Move move) throws InvalidMoveException { validate(move); preparedMoves.put(move.getPlayerIndex(), move); @@ -110,7 +105,7 @@ public class Game { if (endOfAgeReached()) { startNewAge(); } else { - rotateHands(); + hands.rotate(getHandRotationOffset()); } } @@ -170,16 +165,6 @@ public class Game { return hands.get(0).size() == 1; } - private void rotateHands() { - int offset = getHandRotationOffset(); - Map<Integer, List<Card>> newHands = new HashMap<>(hands.size()); - for (int i = 0; i < players.size(); i++) { - int newIndex = Math.floorMod(i + offset, players.size()); - newHands.put(newIndex, hands.get(i)); - } - hands = newHands; - } - private int getHandRotationOffset() { // clockwise at age 1, and alternating return currentAge % 2 == 0 ? -1 : 1; diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Decks.java b/src/main/java/org/luxons/sevenwonders/game/cards/Decks.java index b798b035..aa2b00bf 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Decks.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Decks.java @@ -22,7 +22,7 @@ public class Decks { .orElseThrow(() -> new CardNotFoundException(cardName)); } - public Map<Integer, List<Card>> deal(int age, int nbPlayers) { + public Hands deal(int age, int nbPlayers) { List<Card> deck = getDeck(age); validateNbCards(deck, nbPlayers); return deal(deck, nbPlayers); @@ -46,13 +46,15 @@ public class Decks { } } - private Map<Integer, List<Card>> deal(List<Card> deck, int nbPlayers) { + private Hands deal(List<Card> deck, int nbPlayers) { Map<Integer, List<Card>> hands = new HashMap<>(nbPlayers); + for (int i = 0; i < nbPlayers; i++) { + hands.put(i, new ArrayList<>()); + } for (int i = 0; i < deck.size(); i++) { - hands.putIfAbsent(i % nbPlayers, new ArrayList<>()); hands.get(i % nbPlayers).add(deck.get(i)); } - return hands; + return new Hands(hands, nbPlayers); } class CardNotFoundException extends RuntimeException { diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java b/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java new file mode 100644 index 00000000..4a5588c9 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java @@ -0,0 +1,50 @@ +package org.luxons.sevenwonders.game.cards; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.luxons.sevenwonders.game.api.HandCard; +import org.luxons.sevenwonders.game.api.Table; + +public class Hands { + + private final int nbPlayers; + + private Map<Integer, List<Card>> hands; + + Hands(Map<Integer, List<Card>> hands, int nbPlayers) { + this.hands = hands; + this.nbPlayers = nbPlayers; + } + + public List<Card> get(int playerIndex) { + if (!hands.containsKey(playerIndex)) { + throw new PlayerIndexOutOfBoundsException(playerIndex); + } + return hands.get(playerIndex); + } + + public List<HandCard> createHand(Table table, int playerIndex) { + return hands.get(playerIndex) + .stream() + .map(c -> new HandCard(c, table, playerIndex)) + .collect(Collectors.toList()); + } + + public Hands rotate(int offset) { + Map<Integer, List<Card>> newHands = new HashMap<>(hands.size()); + for (int i = 0; i < nbPlayers; i++) { + int newIndex = Math.floorMod(i + offset, nbPlayers); + newHands.put(newIndex, hands.get(i)); + } + return new Hands(newHands, nbPlayers); + } + + class PlayerIndexOutOfBoundsException extends ArrayIndexOutOfBoundsException { + PlayerIndexOutOfBoundsException(int index) { + super(index); + } + } +} |