Skip to content

Commit

Permalink
블랙잭 게임에서 Dealer 및 Player 클래스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
gunkim committed May 2, 2024
1 parent 0c5f198 commit e042cfa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.gunkim.blackjack.domain;

import java.util.*;
import java.util.stream.IntStream;

public class CardDeck {
private static final int DEFAULT_CARD_SIZE = 52;
Expand All @@ -26,6 +27,12 @@ public Card draw() {
return cards.remove();
}

public List<Card> draw(int quantity) {
return IntStream.range(0, quantity)
.mapToObj(__ -> draw())
.toList();
}

private void validateCards(Collection<Card> cards) {
Objects.requireNonNull(cards, "cards must not be null");
checkForDuplicateCards(cards);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.gunkim.blackjack.domain;

public record Dealer(CardDeck deck) {
//TODO: 최초 블랙잭 시작 시 딜러는 플레이어에게 2장의 카드를 나눠줘야 함을 변수로 나타냄. 더 적절한 네이밍이 필요함.
public static final int CARD_DEAL = 2;

public Player deal() {
return new Player(deck.draw(CARD_DEAL));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.gunkim.blackjack.domain;

import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class Player {
//TODO: 일급 컬렉션 및 별도의 객체로 분리될 수 있을 것 같음. 혹은 CardDeck이 이 역할을 어느정도 할 수도 있음.
private final Set<Card> cards;

public Player(Collection<Card> cards) {
Objects.requireNonNull(cards, "cards must not be null");

if (cards.size() != Dealer.CARD_DEAL) {
throw new IllegalArgumentException("Player must have exactly %d cards".formatted(Dealer.CARD_DEAL));
}
this.cards = new HashSet<>(cards);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.gunkim.blackjack.domain;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@DisplayName("딜러는")
class DealerTest {
private Dealer sut;

@BeforeEach
void setup() {
var dummyCardDeck = new CardDeck(Card.distinctCards());
this.sut = new Dealer(dummyCardDeck);
}

@Test
@DisplayName("플레이어에게 카드를 나눠준다")
void shouldDeal() {
var player = sut.deal();
assertNotNull(player);
}
}

0 comments on commit e042cfa

Please sign in to comment.