diff --git a/10_Blackjack/java/src/Card.java b/10_Blackjack/java/src/Card.java index 8fd36ad0..3f88f9c5 100644 --- a/10_Blackjack/java/src/Card.java +++ b/10_Blackjack/java/src/Card.java @@ -23,6 +23,12 @@ public final class Card { private final Suit suit; public Card(int value, Suit suit) { + if(value < 1 || value > 13) { + throw new IllegalArgumentException("Invalid card value " + value); + } + if(suit == null) { + throw new IllegalArgumentException("Card suit must be non-null"); + } this.value = value; this.suit = suit; } @@ -51,4 +57,29 @@ public final class Card { // result.append(suit.name().charAt(0)); return result.toString(); } + + @Override + public boolean equals(Object obj) { + // Overriding 'equals' and 'hashCode' (below) make your class work correctly + // with all sorts of methods in the Java API that need to determine the uniqueness + // of an instance (like a Set). + if(obj.getClass() != Card.class) { + return false; + } + Card other = (Card) obj; + return this.getSuit() == other.getSuit() && this.getValue() == other.getValue(); + } + + @Override + public int hashCode() { + // This is a fairly standard hashCode implementation for a data object. + // The details are beyond the scope of this comment, but most IDEs can generate + // this for you. + + // Note that it's a best practice to implement hashCode whenever you implement equals and vice versa. + int hash = 7; + hash = 31 * hash + (int) value; + hash = 31 * hash + suit.hashCode(); + return hash; + } } \ No newline at end of file diff --git a/10_Blackjack/java/src/Deck.java b/10_Blackjack/java/src/Deck.java index 7b9a9094..7ed290bc 100644 --- a/10_Blackjack/java/src/Deck.java +++ b/10_Blackjack/java/src/Deck.java @@ -1,8 +1,10 @@ +import java.util.Collections; import java.util.LinkedList; +import java.util.List; public class Deck { - LinkedList cards; + private LinkedList cards; /** * Initialize the game deck with the given number of standard decks. @@ -12,12 +14,14 @@ public class Deck { * @param nDecks */ public Deck(int nDecks) { - // TODO implement Deck constructor - // See line 33 of Blackjack.java for the current version of this code - /* for each suit - * for each value 1-13 - * add new Card(value, suit) to cards - */ + cards = new LinkedList<>(); + for(int deckIndex = 0; deckIndex < nDecks; deckIndex++) { + for(Card.Suit suit : Card.Suit.values()) { + for(int value = 1; value < 14; value++) { + cards.add(new Card(value, suit)); + } + } + } } /** @@ -37,5 +41,20 @@ public class Deck { // Probably just call Collections.shuffle(cards); } + /** + * Get the number of cards in this deck. + * @return The number of cards in this deck. For example, 52 for a single deck. + */ + public int size() { + return cards.size(); + } + /** + * Returns the cards in this deck. + * @return An immutable view of the cards in this deck. + */ + public List getCards() { + // The returned list is immutable because we don't want other code messing with the deck. + return Collections.unmodifiableList(cards); + } } diff --git a/10_Blackjack/java/test/DeckTest.java b/10_Blackjack/java/test/DeckTest.java new file mode 100644 index 00000000..0ded8403 --- /dev/null +++ b/10_Blackjack/java/test/DeckTest.java @@ -0,0 +1,40 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertAll; +import org.junit.jupiter.api.Test; + +public class DeckTest { + + @Test + void testInitOne() { + // When + Deck deck = new Deck(1); + + // Then + long nCards = deck.size(); + long nSuits = deck.getCards().stream() + .map(card -> card.getSuit()) + .distinct() + .count(); + long nValues = deck.getCards().stream() + .map(card -> card.getValue()) + .distinct() + .count(); + + assertAll("deck", + () -> assertEquals(52, nCards, "Expected 52 cards in a deck, but got " + nCards), + () -> assertEquals(4, nSuits, "Expected 4 suits, but got " + nSuits), + () -> assertEquals(13, nValues, "Expected 13 values, but got " + nValues) + ); + + } + + @Test + void testInitTwo() { + // When + Deck deck = new Deck(2); + + // Then + assertEquals(104, deck.size()); + } + +}