mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-11 12:45:47 -08:00
Refactor to allow testing side effects
By externalizing the source of i/o and randomness for shuffling, we can inject non-interactive and deterministic behavior during unit tests.
This commit is contained in:
@@ -1,42 +1,50 @@
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Deck {
|
||||
|
||||
private LinkedList<Card> cards;
|
||||
private Function<LinkedList<Card>, LinkedList<Card>> shuffleAlgorithm;
|
||||
|
||||
/**
|
||||
* Initialize the game deck with the given number of standard decks.
|
||||
* e.g. if you want to play with 2 decks, then {@code new Decks(2)} will
|
||||
* initialize 'cards' with 2 copies of a standard 52 card deck.
|
||||
*
|
||||
* @param nDecks
|
||||
* @param shuffleAlgorithm A function that takes the initial sorted card
|
||||
* list and returns a shuffled list ready to deal.
|
||||
*
|
||||
*/
|
||||
public Deck() {
|
||||
cards = new LinkedList<>();
|
||||
for(Card.Suit suit : Card.Suit.values()) {
|
||||
for(int value = 1; value < 14; value++) {
|
||||
cards.add(new Card(value, suit));
|
||||
}
|
||||
}
|
||||
public Deck(Function<LinkedList<Card>, LinkedList<Card>> shuffleAlgorithm) {
|
||||
this.shuffleAlgorithm = shuffleAlgorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deals one card from the deck, removing it from this object's state.
|
||||
* Deals one card from the deck, removing it from this object's state. If
|
||||
* the deck is empty, it will be reshuffled before dealing a new card.
|
||||
*
|
||||
* @return The card that was dealt.
|
||||
*/
|
||||
public Card deal() {
|
||||
// TODO implement Deck.deal() - new Card(10, Card.Suit.CLUBS) added temporarily
|
||||
return new Card(10, Card.Suit.CLUBS);
|
||||
if(cards == null || cards.isEmpty()) {
|
||||
reshuffle();
|
||||
}
|
||||
return cards.pollFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuffle the cards in this deck.
|
||||
* Shuffle the cards in this deck using the shuffleAlgorithm.
|
||||
*/
|
||||
public void shuffle() {
|
||||
// TODO implement Deck.shuffle()
|
||||
// Probably just call Collections.shuffle(cards);
|
||||
public void reshuffle() {
|
||||
LinkedList<Card> newCards = new LinkedList<>();
|
||||
for(Card.Suit suit : Card.Suit.values()) {
|
||||
for(int value = 1; value < 14; value++) {
|
||||
newCards.add(new Card(value, suit));
|
||||
}
|
||||
}
|
||||
this.cards = this.shuffleAlgorithm.apply(newCards);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user