diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2016-12-14 22:46:21 +0100 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2016-12-14 22:49:36 +0100 |
commit | 1c3ce82037001e18c44c86a1041758747ccb6e3e (patch) | |
tree | bbc131a5f8e19e0472fdf7255e18c35881e46436 /src/main | |
parent | Fix default nbPlayers in the settings (diff) | |
download | seven-wonders-1c3ce82037001e18c44c86a1041758747ccb6e3e.tar.gz seven-wonders-1c3ce82037001e18c44c86a1041758747ccb6e3e.tar.bz2 seven-wonders-1c3ce82037001e18c44c86a1041758747ccb6e3e.zip |
Add deal() method to the Decks
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/game/Decks.java | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Decks.java b/src/main/java/org/luxons/sevenwonders/game/Decks.java index 72dff4e9..be811b6f 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Decks.java +++ b/src/main/java/org/luxons/sevenwonders/game/Decks.java @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.game; -import java.util.Collections; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -9,13 +9,40 @@ import org.luxons.sevenwonders.game.cards.Card; public class Decks { + private static final int HAND_SIZE = 7; + private Map<Integer, List<Card>> cardsPerAge = new HashMap<>(); public Decks(Map<Integer, List<Card>> cardsPerAge) { this.cardsPerAge = cardsPerAge; } - public List<Card> getCards(int age) { - return cardsPerAge.getOrDefault(age, Collections.emptyList()); + Map<Integer, List<Card>> deal(int age, int nbPlayers) { + List<Card> deck = getDeck(age); + validateNbCards(deck, nbPlayers); + return deal(deck, nbPlayers); + } + + private List<Card> getDeck(int age) { + List<Card> deck = cardsPerAge.get(age); + if (deck == null) { + throw new IllegalArgumentException("No deck found for age " + age); + } + return deck; + } + + private void validateNbCards(List<Card> deck, int nbPlayers) { + if (nbPlayers * HAND_SIZE != deck.size()) { + throw new IllegalArgumentException( + String.format("%d cards is not the expected number for %d players", deck.size(), nbPlayers)); + } + } + + private Map<Integer, List<Card>> deal(List<Card> deck, int nbPlayers) { + Map<Integer, List<Card>> hands = new HashMap<>(nbPlayers); + for (int i = 0; i < deck.size(); i++) { + hands.putIfAbsent(i % nbPlayers, new ArrayList<>()).add(deck.get(i)); + } + return hands; } } |