public Deck() { for (Card.Suit suit : Card.Suit.values()) { for (Card.Rank rank : Card.Rank.values()) { cards.add(new Card(rank, suit)); } } }
/** * Clear deck and initialize it with 52 * NUM_DECKS new cards. */ private void initializeDeck() { deck.clear(); for(int i = 0; i < NUM_DECKS; i++) { for(Card.Rank rank: Card.Rank.values()) { for(Card.Suit suit: Card.Suit.values()) { deck.add(new Card(rank, suit)); } } } Collections.shuffle(deck, new Random()); }
@Override public void create(int numberOfSuits, int numberOfRanks) { this.numberOfRanks = numberOfRanks; this.numberOfSuits = numberOfSuits; logger.info( format("*** Creating Deck with %s suits and %s ranks ***", numberOfSuits, numberOfRanks)); List<Card.Suit> suits = Arrays.asList(Card.Suit.values()).subList(0, numberOfSuits); List<Card.Rank> ranks = Arrays.asList(Card.Rank.values()).subList(0, numberOfRanks); cards = suits .stream() .flatMap(suit -> ranks.stream().map(rank -> new Card(rank, suit))) .collect(toList()); }