mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-21 23:00:43 -08:00
Implement unit tests for Blackjack Java
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Deck {
|
||||
|
||||
LinkedList<Card> cards;
|
||||
private LinkedList<Card> 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<Card> getCards() {
|
||||
// The returned list is immutable because we don't want other code messing with the deck.
|
||||
return Collections.unmodifiableList(cards);
|
||||
}
|
||||
}
|
||||
|
||||
40
10_Blackjack/java/test/DeckTest.java
Normal file
40
10_Blackjack/java/test/DeckTest.java
Normal file
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user