From 1c3ce82037001e18c44c86a1041758747ccb6e3e Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Wed, 14 Dec 2016 22:46:21 +0100 Subject: Add deal() method to the Decks --- .../java/org/luxons/sevenwonders/game/Decks.java | 33 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'src/main') 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> cardsPerAge = new HashMap<>(); public Decks(Map> cardsPerAge) { this.cardsPerAge = cardsPerAge; } - public List getCards(int age) { - return cardsPerAge.getOrDefault(age, Collections.emptyList()); + Map> deal(int age, int nbPlayers) { + List deck = getDeck(age); + validateNbCards(deck, nbPlayers); + return deal(deck, nbPlayers); + } + + private List getDeck(int age) { + List deck = cardsPerAge.get(age); + if (deck == null) { + throw new IllegalArgumentException("No deck found for age " + age); + } + return deck; + } + + private void validateNbCards(List 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> deal(List deck, int nbPlayers) { + Map> hands = new HashMap<>(nbPlayers); + for (int i = 0; i < deck.size(); i++) { + hands.putIfAbsent(i % nbPlayers, new ArrayList<>()).add(deck.get(i)); + } + return hands; } } -- cgit