From 60a21e8fdba505f63e690ef2868659707a6b9533 Mon Sep 17 00:00:00 2001 From: libienz Date: Tue, 5 Mar 2024 15:36:02 +0900 Subject: [PATCH 001/124] =?UTF-8?q?docs:=20=ED=94=84=EB=A1=9C=EC=A0=9D?= =?UTF-8?q?=ED=8A=B8=20=EC=86=8C=EA=B0=9C=EA=B8=80=20=EB=B0=8F=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 556099c4de..80a74c8b5c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,47 @@ -# java-blackjack +# ♠️ java-blackjack ♠️ -블랙잭 미션 저장소 +- 블랙잭 게임을 변형한 프로그램을 구현한다. +- 블랙잭 게임은 딜러와 플레이어 중 카드의 합이 21 또는 21에 가장 가까운 숫자를 가지는 쪽이 이기는 게임이다. +- 카드의 숫자 계산은 카드 숫자를 기본으로 하며, 예외로 Ace는 1 또는 11로 계산할 수 있으며, King, Queen, Jack은 각각 10으로 계산한다. +- 게임을 시작하면 플레이어는 두 장의 카드를 지급 받으며, 두 장의 카드 숫자를 합쳐 21을 초과하지 않으면서 21에 가깝게 만들면 이긴다. +- 21을 넘지 않을 경우 원한다면 얼마든지 카드를 계속 뽑을 수 있다. +- 딜러는 처음에 받은 2장의 합계가 16이하이면 반드시 1장의 카드를 추가로 받아야 하고, 17점 이상이면 추가로 받을 수 없다. +- 게임을 완료한 후 각 플레이어별로 승패를 출력한다. -## 우아한테크코스 코드리뷰 +# 🛠️ 기능 구현 목록 -- [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md) +- [ ] 입력 + - [ ] 게임에 참여할 사람의 이름을 입력 받을 수 있다. + - [ ] 한 장 더 받을지 여부를 입력 받을 수 있다. +- [ ] 입력 검증 + - [ ] 카드 추가 여부를 올바른 형태 (y/n)으로 입력했는지 검증할 수 있다. +- [ ] 도메인 + - [ ] 이름 + - [ ] 이름은 빈 문자열일 수 없다. + - [ ] 플레이어 + - [ ] 배정 받은 카드를 관리한다. + - [ ] 보유하고 있는 카드합이 21을 넘는지 확인할 수 있다. + - [ ] 플레이어들 + - [ ] 이름이 중복되는 플레이어는 존재할 수 없다. + - [ ] 게임 참가자는 딜러 제외 3명 이하여야 한다. + - [ ] 특정 카드합보다 작은 카드합인 플레이어를 셀 수 있다. + - [ ] 딜러 + - [ ] 배정 받은 카드를 관리한다. + - [ ] 보유하고 있는 카드합이 17을 넘는지 확인할 수 있다. + - [ ] 정해진 룰대로 카드를 완성할 수 있다. + - [ ] 카드리스트 (게임 참여자 보유 카드) + - [ ] 에이스 카드가 사용되는 경우 카드의 값을 맥락에 맞게 정한다. + - [ ] 카드의 합을 계산할 수 있다. + - [ ] 카드를 추가할 수 있다. + - [ ] 카드덱 (전체 카드) + - [ ] 실제 트럼프 카드와 동일한 구성의 카드를 관리한다. (조커 제외) + - [ ] 카드를 뽑아서 버릴 수 있다. + - [ ] 카드를 섞을 수 있다. + - [ ] BlackJackGame + - [ ] 초기 카드를 분배할 수 있다. + - [ ] player마다 추가 드로우 여부를 유도할 수 있다. + - [ ] 승패 판정 역할을 수행할 수 있다. +- [ ] 출력 + - [ ] 각 참여자의 카드 정보를 출력할 수 있다. + - [ ] 각 참여자의 카드 합을 출력할 수 있다. + - [ ] 최종 승패를 출력할 수 있다. From 2f32bcaa7148dbb744e2ab84f3559b897304d5c5 Mon Sep 17 00:00:00 2001 From: libienz Date: Tue, 5 Mar 2024 16:26:29 +0900 Subject: [PATCH 002/124] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EC=9D=98=20?= =?UTF-8?q?=EB=AA=A8=EC=96=91=20=EB=B0=8F=20=EC=A0=90=EC=88=98=20=EC=9D=B4?= =?UTF-8?q?=EB=84=98=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/CardNumber.java | 28 +++++++++++++++++++ src/main/java/blackjack/domain/CardShape.java | 19 +++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/blackjack/domain/CardNumber.java create mode 100644 src/main/java/blackjack/domain/CardShape.java diff --git a/src/main/java/blackjack/domain/CardNumber.java b/src/main/java/blackjack/domain/CardNumber.java new file mode 100644 index 0000000000..9bd1f8d036 --- /dev/null +++ b/src/main/java/blackjack/domain/CardNumber.java @@ -0,0 +1,28 @@ +package blackjack.domain; + +public enum CardNumber { + + ACE(1), + TWO(2), + THREE(3), + FOUR(4), + FIVE(5), + SIX(6), + SEVEN(7), + EIGHT(8), + NINE(9), + TEN(10), + JACK(10), + QUEEN(10), + KING(10); + + private final int value; + + CardNumber(int value) { + this.value = value; + } + + public int getValue() { + return value; + } +} diff --git a/src/main/java/blackjack/domain/CardShape.java b/src/main/java/blackjack/domain/CardShape.java new file mode 100644 index 0000000000..999d581b19 --- /dev/null +++ b/src/main/java/blackjack/domain/CardShape.java @@ -0,0 +1,19 @@ +package blackjack.domain; + +public enum CardShape { + + HEART("하트"), + DIAMOND("다이아몬드"), + CLUB("클로버"), + SPADE("스페이드"); + + private final String name; + + CardShape(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} From 49be956e3bd0b4ed5892537fede220bf312d1f3e Mon Sep 17 00:00:00 2001 From: libienz Date: Tue, 5 Mar 2024 16:28:36 +0900 Subject: [PATCH 003/124] =?UTF-8?q?feat:=20=EC=86=8C=EC=9C=A0=ED=95=9C=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=ED=95=A9=20=EA=B3=84=EC=82=B0=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/blackjack/domain/Card.java | 16 ++++++++++++++ src/main/java/blackjack/domain/Hand.java | 18 +++++++++++++++ src/test/java/blackjack/domain/HandTest.java | 23 ++++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/domain/Card.java create mode 100644 src/main/java/blackjack/domain/Hand.java create mode 100644 src/test/java/blackjack/domain/HandTest.java diff --git a/README.md b/README.md index 80a74c8b5c..1c5fb7ea37 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ - [ ] 정해진 룰대로 카드를 완성할 수 있다. - [ ] 카드리스트 (게임 참여자 보유 카드) - [ ] 에이스 카드가 사용되는 경우 카드의 값을 맥락에 맞게 정한다. - - [ ] 카드의 합을 계산할 수 있다. + - [x] 카드의 합을 계산할 수 있다. - [ ] 카드를 추가할 수 있다. - [ ] 카드덱 (전체 카드) - [ ] 실제 트럼프 카드와 동일한 구성의 카드를 관리한다. (조커 제외) diff --git a/src/main/java/blackjack/domain/Card.java b/src/main/java/blackjack/domain/Card.java new file mode 100644 index 0000000000..c4401b41ef --- /dev/null +++ b/src/main/java/blackjack/domain/Card.java @@ -0,0 +1,16 @@ +package blackjack.domain; + +public class Card { + + private final CardShape cardShape; + private final CardNumber cardNumber; + + public Card(CardShape cardShape, CardNumber cardNumber) { + this.cardShape = cardShape; + this.cardNumber = cardNumber; + } + + public int getCardNumber() { + return cardNumber.getValue(); + } +} diff --git a/src/main/java/blackjack/domain/Hand.java b/src/main/java/blackjack/domain/Hand.java new file mode 100644 index 0000000000..ab4ef6e544 --- /dev/null +++ b/src/main/java/blackjack/domain/Hand.java @@ -0,0 +1,18 @@ +package blackjack.domain; + +import java.util.List; + +public class Hand { + + private final List cards; + + public Hand(List cards) { + this.cards = cards; + } + + public int sum() { + return cards.stream() + .mapToInt(Card::getCardNumber) + .sum(); + } +} diff --git a/src/test/java/blackjack/domain/HandTest.java b/src/test/java/blackjack/domain/HandTest.java new file mode 100644 index 0000000000..33095c26ee --- /dev/null +++ b/src/test/java/blackjack/domain/HandTest.java @@ -0,0 +1,23 @@ +package blackjack.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class HandTest { + + @DisplayName("카드의 합을 계산할 수 있다.") + @Test + void testCardSummation() { + Card card1 = new Card(CardShape.HEART, CardNumber.TWO); + Card card2 = new Card(CardShape.CLUB, CardNumber.TWO); + Card card3 = new Card(CardShape.DIAMOND, CardNumber.TWO); + + Hand hand = new Hand(List.of(card1, card2, card3)); + int expected = hand.sum(); + + assertThat(expected).isEqualTo(6); + } +} From 768674ff41495f466a92df78c8969db6a04f89aa Mon Sep 17 00:00:00 2001 From: libienz Date: Tue, 5 Mar 2024 16:47:20 +0900 Subject: [PATCH 004/124] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/blackjack/domain/Hand.java | 8 ++++++++ src/test/java/blackjack/domain/HandTest.java | 16 +++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1c5fb7ea37..8f3a4e0e14 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ - [ ] 카드리스트 (게임 참여자 보유 카드) - [ ] 에이스 카드가 사용되는 경우 카드의 값을 맥락에 맞게 정한다. - [x] 카드의 합을 계산할 수 있다. - - [ ] 카드를 추가할 수 있다. + - [x] 카드를 추가할 수 있다. - [ ] 카드덱 (전체 카드) - [ ] 실제 트럼프 카드와 동일한 구성의 카드를 관리한다. (조커 제외) - [ ] 카드를 뽑아서 버릴 수 있다. diff --git a/src/main/java/blackjack/domain/Hand.java b/src/main/java/blackjack/domain/Hand.java index ab4ef6e544..b8cd18f4cd 100644 --- a/src/main/java/blackjack/domain/Hand.java +++ b/src/main/java/blackjack/domain/Hand.java @@ -15,4 +15,12 @@ public int sum() { .mapToInt(Card::getCardNumber) .sum(); } + + public void append(Card card) { + cards.add(card); + } + + public List getCards() { + return List.copyOf(cards); + } } diff --git a/src/test/java/blackjack/domain/HandTest.java b/src/test/java/blackjack/domain/HandTest.java index 33095c26ee..5af988866a 100644 --- a/src/test/java/blackjack/domain/HandTest.java +++ b/src/test/java/blackjack/domain/HandTest.java @@ -2,15 +2,17 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +@DisplayName("참가자 소유 카드 테스트") class HandTest { @DisplayName("카드의 합을 계산할 수 있다.") @Test - void testCardSummation() { + void testHandSummation() { Card card1 = new Card(CardShape.HEART, CardNumber.TWO); Card card2 = new Card(CardShape.CLUB, CardNumber.TWO); Card card3 = new Card(CardShape.DIAMOND, CardNumber.TWO); @@ -20,4 +22,16 @@ void testCardSummation() { assertThat(expected).isEqualTo(6); } + + @DisplayName("특정 카드를 핸드에 추가할 수 있다") + @Test + void testAppendCard() { + Card card1 = new Card(CardShape.HEART, CardNumber.TWO); + List cards = new ArrayList<>(); + + Hand hand = new Hand(cards); + hand.append(card1); + + assertThat(hand.getCards()).hasSize(1); + } } From 6a9b3a84188f377887ba48caa78115f75f6f6a6f Mon Sep 17 00:00:00 2001 From: libienz Date: Tue, 5 Mar 2024 17:20:01 +0900 Subject: [PATCH 005/124] =?UTF-8?q?feat:=20Card=20=EB=8F=99=EB=93=B1?= =?UTF-8?q?=EC=84=B1=20=EB=B9=84=EA=B5=90=20=EB=B0=8F=20=ED=95=B4=EC=8B=B1?= =?UTF-8?q?=20=ED=95=A8=EC=88=98=20=EC=9E=AC=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Card.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/blackjack/domain/Card.java b/src/main/java/blackjack/domain/Card.java index c4401b41ef..ee1b084d9c 100644 --- a/src/main/java/blackjack/domain/Card.java +++ b/src/main/java/blackjack/domain/Card.java @@ -1,5 +1,7 @@ package blackjack.domain; +import java.util.Objects; + public class Card { private final CardShape cardShape; @@ -10,6 +12,23 @@ public Card(CardShape cardShape, CardNumber cardNumber) { this.cardNumber = cardNumber; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Card card = (Card) o; + return cardShape == card.cardShape && cardNumber == card.cardNumber; + } + + @Override + public int hashCode() { + return Objects.hash(cardShape, cardNumber); + } + public int getCardNumber() { return cardNumber.getValue(); } From e5ccb9ccb0b5632789503a8ca3fe73af76eb8f3c Mon Sep 17 00:00:00 2001 From: libienz Date: Tue, 5 Mar 2024 17:43:32 +0900 Subject: [PATCH 006/124] =?UTF-8?q?feat:=20CardDeck=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EB=BD=91=EC=9D=84=20=EC=88=98=20=EC=9E=88=EB=8A=94?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++- src/main/java/blackjack/domain/CardDeck.java | 23 +++++++++ .../java/blackjack/domain/CardDeckTest.java | 48 +++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/main/java/blackjack/domain/CardDeck.java create mode 100644 src/test/java/blackjack/domain/CardDeckTest.java diff --git a/README.md b/README.md index 8f3a4e0e14..e2dac67eb9 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,9 @@ - [ ] 에이스 카드가 사용되는 경우 카드의 값을 맥락에 맞게 정한다. - [x] 카드의 합을 계산할 수 있다. - [x] 카드를 추가할 수 있다. - - [ ] 카드덱 (전체 카드) - - [ ] 실제 트럼프 카드와 동일한 구성의 카드를 관리한다. (조커 제외) - - [ ] 카드를 뽑아서 버릴 수 있다. - - [ ] 카드를 섞을 수 있다. + - [x] 카드덱 (전체 카드) + - [x] 카드를 지정한 횟수만큼 건네줄 수 있다. + - [x] 카드를 한 장 뽑아서 건네줄 수 있다. - [ ] BlackJackGame - [ ] 초기 카드를 분배할 수 있다. - [ ] player마다 추가 드로우 여부를 유도할 수 있다. diff --git a/src/main/java/blackjack/domain/CardDeck.java b/src/main/java/blackjack/domain/CardDeck.java new file mode 100644 index 0000000000..3501b0fc7c --- /dev/null +++ b/src/main/java/blackjack/domain/CardDeck.java @@ -0,0 +1,23 @@ +package blackjack.domain; + +import java.util.List; +import java.util.stream.IntStream; + +public class CardDeck { + + private final List cards; + + public CardDeck(List cards) { + this.cards = cards; + } + + public Card popCard() { + return cards.remove(0); + } + + public List popCards(int count) { + return IntStream.range(0, count) + .mapToObj(i -> popCard()) + .toList(); + } +} diff --git a/src/test/java/blackjack/domain/CardDeckTest.java b/src/test/java/blackjack/domain/CardDeckTest.java new file mode 100644 index 0000000000..0dd873a9f6 --- /dev/null +++ b/src/test/java/blackjack/domain/CardDeckTest.java @@ -0,0 +1,48 @@ +package blackjack.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("총 카드 덱 테스트") +class CardDeckTest { + + @DisplayName("덱에서 첫번째 카드를 건네받을 수 있다") + @Test + void testPopCardFromDeck() { + List cards = new ArrayList<>(); + Card card1 = new Card(CardShape.HEART, CardNumber.TWO); + Card card2 = new Card(CardShape.CLUB, CardNumber.THREE); + Card card3 = new Card(CardShape.DIAMOND, CardNumber.FOUR); + + cards.add(card1); + cards.add(card2); + cards.add(card3); + + CardDeck cardDeck = new CardDeck(cards); + Card popped = cardDeck.popCard(); + assertThat(popped).isEqualTo(card1); + } + + @DisplayName("덱에서 횟수만큼 카드를 건네받을 수 있다") + @Test + void testPopCardsFromDeck() { + List cards = new ArrayList<>(); + Card card1 = new Card(CardShape.HEART, CardNumber.TWO); + Card card2 = new Card(CardShape.CLUB, CardNumber.THREE); + Card card3 = new Card(CardShape.DIAMOND, CardNumber.FOUR); + + cards.add(card1); + cards.add(card2); + cards.add(card3); + + CardDeck cardDeck = new CardDeck(cards); + List popped = cardDeck.popCards(3); + + assertThat(popped).hasSize(3); + } + // TODO : 카드가 남아있지 않은 경우 카드를 뽑으려고 할 때가 있을 지 생각해보기 +} From 2f8d112b91304e861aa041906bcdbb391599e392 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 6 Mar 2024 14:24:51 +0900 Subject: [PATCH 007/124] =?UTF-8?q?test:=20=ED=8A=B9=EC=A0=95=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9D=98=EB=AF=B8=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/blackjack/domain/HandTest.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/test/java/blackjack/domain/HandTest.java b/src/test/java/blackjack/domain/HandTest.java index 5af988866a..86cc67392c 100644 --- a/src/test/java/blackjack/domain/HandTest.java +++ b/src/test/java/blackjack/domain/HandTest.java @@ -1,11 +1,12 @@ package blackjack.domain; -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; @DisplayName("참가자 소유 카드 테스트") class HandTest { @@ -32,6 +33,6 @@ void testAppendCard() { Hand hand = new Hand(cards); hand.append(card1); - assertThat(hand.getCards()).hasSize(1); + assertThat(hand.getCards()).containsExactly(card1); } } From fa90a65d3822aea8217eb3b6a806333f7a911550 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 6 Mar 2024 14:45:38 +0900 Subject: [PATCH 008/124] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EC=9D=98=20?= =?UTF-8?q?=EC=88=98=EA=B0=80=20=EC=B6=A9=EB=B6=84=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EC=9D=80=20=EA=B2=BD=EC=9A=B0=20pop=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=EC=9D=98=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/CardDeck.java | 3 ++ .../java/blackjack/domain/CardDeckTest.java | 42 ++++++++++++++++--- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/main/java/blackjack/domain/CardDeck.java b/src/main/java/blackjack/domain/CardDeck.java index 3501b0fc7c..0076595ae4 100644 --- a/src/main/java/blackjack/domain/CardDeck.java +++ b/src/main/java/blackjack/domain/CardDeck.java @@ -12,6 +12,9 @@ public CardDeck(List cards) { } public Card popCard() { + if (cards.isEmpty()) { + throw new IllegalArgumentException("[ERROR] 남아있는 카드가 부족하여 카드를 뽑을 수 없습니다"); + } return cards.remove(0); } diff --git a/src/test/java/blackjack/domain/CardDeckTest.java b/src/test/java/blackjack/domain/CardDeckTest.java index 0dd873a9f6..068689d8db 100644 --- a/src/test/java/blackjack/domain/CardDeckTest.java +++ b/src/test/java/blackjack/domain/CardDeckTest.java @@ -1,16 +1,18 @@ package blackjack.domain; -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; @DisplayName("총 카드 덱 테스트") class CardDeckTest { - @DisplayName("덱에서 첫번째 카드를 건네받을 수 있다") + @DisplayName("덱에서 카드를 뽑을 수 있다") @Test void testPopCardFromDeck() { List cards = new ArrayList<>(); @@ -27,7 +29,7 @@ void testPopCardFromDeck() { assertThat(popped).isEqualTo(card1); } - @DisplayName("덱에서 횟수만큼 카드를 건네받을 수 있다") + @DisplayName("덱에서 횟수만큼 카드를 뽑을 수 있다") @Test void testPopCardsFromDeck() { List cards = new ArrayList<>(); @@ -44,5 +46,33 @@ void testPopCardsFromDeck() { assertThat(popped).hasSize(3); } - // TODO : 카드가 남아있지 않은 경우 카드를 뽑으려고 할 때가 있을 지 생각해보기 + + @DisplayName("덱에서 카드를 하나 뽑는 경우 카드가 부족하면 예외가 발생한다") + @Test + void testInvalidPopInsufficientDeckCount() { + List cards = new ArrayList<>(); + CardDeck cardDeck = new CardDeck(cards); + + assertThatThrownBy(() -> cardDeck.popCard()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 남아있는 카드가 부족하여 카드를 뽑을 수 없습니다"); + } + + @DisplayName("덱에서 카드를 여러개 뽑는 경우 카드가 부족하면 예외가 발생한다") + @Test + void testInvalidPopCardsInsufficientDeckCount() { + List cards = new ArrayList<>(); + Card card1 = new Card(CardShape.HEART, CardNumber.TWO); + Card card2 = new Card(CardShape.CLUB, CardNumber.THREE); + Card card3 = new Card(CardShape.DIAMOND, CardNumber.FOUR); + + cards.add(card1); + cards.add(card2); + cards.add(card3); + + CardDeck cardDeck = new CardDeck(cards); + assertThatThrownBy(() -> cardDeck.popCards(4)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 남아있는 카드가 부족하여 카드를 뽑을 수 없습니다"); + } } From 888c475a1f5a8457d79c4e6d041fe7487b9b2800 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 6 Mar 2024 15:04:43 +0900 Subject: [PATCH 009/124] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=20=EC=9D=B4=EB=A6=84=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/blackjack/domain/PlayerName.java | 21 +++++++++++++++++++ .../java/blackjack/domain/PlayerNameTest.java | 17 +++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 src/main/java/blackjack/domain/PlayerName.java create mode 100644 src/test/java/blackjack/domain/PlayerNameTest.java diff --git a/README.md b/README.md index e2dac67eb9..0bc0396620 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ - [x] 카드덱 (전체 카드) - [x] 카드를 지정한 횟수만큼 건네줄 수 있다. - [x] 카드를 한 장 뽑아서 건네줄 수 있다. + - [x] 보유한 카드 개수보다 많이 뽑으면 예외가 발생한다. - [ ] BlackJackGame - [ ] 초기 카드를 분배할 수 있다. - [ ] player마다 추가 드로우 여부를 유도할 수 있다. diff --git a/src/main/java/blackjack/domain/PlayerName.java b/src/main/java/blackjack/domain/PlayerName.java new file mode 100644 index 0000000000..69e8a92ac0 --- /dev/null +++ b/src/main/java/blackjack/domain/PlayerName.java @@ -0,0 +1,21 @@ +package blackjack.domain; + +public class PlayerName { + + private final String value; + + public PlayerName(String value) { + validate(value); + this.value = value; + } + + private void validate(String value) { + validateNotEmpty(value); + } + + private void validateNotEmpty(String value) { + if (value.isEmpty()) { + throw new IllegalArgumentException("[ERROR] 이름이 빈 문자열입니다."); + } + } +} diff --git a/src/test/java/blackjack/domain/PlayerNameTest.java b/src/test/java/blackjack/domain/PlayerNameTest.java new file mode 100644 index 0000000000..0985856157 --- /dev/null +++ b/src/test/java/blackjack/domain/PlayerNameTest.java @@ -0,0 +1,17 @@ +package blackjack.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class PlayerNameTest { + + @DisplayName("이름은 빈 문자열일 수 없다") + @Test + void testCreatePlayerNameWithEmpty() { + assertThatThrownBy(() -> new PlayerName("")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 이름이 빈 문자열입니다."); + } +} \ No newline at end of file From b28e233e38569c544ff24c974ddf999ebb8b291c Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 6 Mar 2024 15:53:20 +0900 Subject: [PATCH 010/124] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Player.java | 20 +++++++ .../java/blackjack/domain/PlayerTest.java | 53 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/main/java/blackjack/domain/Player.java create mode 100644 src/test/java/blackjack/domain/PlayerTest.java diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/Player.java new file mode 100644 index 0000000000..f1a2bea633 --- /dev/null +++ b/src/main/java/blackjack/domain/Player.java @@ -0,0 +1,20 @@ +package blackjack.domain; + +public class Player { + + private final PlayerName name; + private final Hand hand; + + public Player(PlayerName name, Hand hand) { + this.name = name; + this.hand = hand; + } + + public void appendCard(Card card) { + hand.append(card); + } + + public boolean handSummationExceed(int target) { + return hand.sum() > target; + } +} diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java new file mode 100644 index 0000000000..391005f69b --- /dev/null +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -0,0 +1,53 @@ +package blackjack.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class PlayerTest { + + @DisplayName("플레이어 핸드에 카드를 추가할 수 있다") + @Test + void testAppendCardToPlayer() { + PlayerName playerName = new PlayerName("pobi"); + + Card card1 = new Card(CardShape.HEART, CardNumber.TEN); + Card card2 = new Card(CardShape.CLUB, CardNumber.NINE); + + List hand = new ArrayList<>(); + hand.add(card1); + hand.add(card2); + + Player player = new Player(playerName, new Hand(hand)); + + Card card3 = new Card(CardShape.DIAMOND, CardNumber.TWO); + player.appendCard(card3); + + assertThat(hand).containsExactly(card1, card2, card3); + } + + @DisplayName("보유하고 있는 카드합이 특정 값을 넘는지 확인할 수 있다") + @Test + void testHandSumExceedTarget() { + PlayerName playerName = new PlayerName("pobi"); + + Card card1 = new Card(CardShape.HEART, CardNumber.TEN); + Card card2 = new Card(CardShape.CLUB, CardNumber.NINE); + Card card3 = new Card(CardShape.CLUB, CardNumber.THREE); + + List hand = new ArrayList<>(); + hand.add(card1); + hand.add(card2); + hand.add(card3); + + Player player = new Player(playerName, new Hand(hand)); + + boolean expected = player.handSummationExceed(21); + + assertThat(expected).isEqualTo(true); + } +} \ No newline at end of file From 29788391c7fbdf58d2c7f667dea9fcf2dc0c5ec0 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 6 Mar 2024 15:55:12 +0900 Subject: [PATCH 011/124] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20=EC=84=A4=EB=AA=85=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 +++++----- src/test/java/blackjack/domain/PlayerNameTest.java | 1 + src/test/java/blackjack/domain/PlayerTest.java | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0bc0396620..bac26f39b1 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,11 @@ - [ ] 입력 검증 - [ ] 카드 추가 여부를 올바른 형태 (y/n)으로 입력했는지 검증할 수 있다. - [ ] 도메인 - - [ ] 이름 - - [ ] 이름은 빈 문자열일 수 없다. - - [ ] 플레이어 - - [ ] 배정 받은 카드를 관리한다. - - [ ] 보유하고 있는 카드합이 21을 넘는지 확인할 수 있다. + - [x] 이름 + - [x] 이름은 빈 문자열일 수 없다. + - [x] 플레이어 + - [x] 배정 받은 카드를 관리한다. + - [x] 보유하고 있는 카드합이 21을 넘는지 확인할 수 있다. - [ ] 플레이어들 - [ ] 이름이 중복되는 플레이어는 존재할 수 없다. - [ ] 게임 참가자는 딜러 제외 3명 이하여야 한다. diff --git a/src/test/java/blackjack/domain/PlayerNameTest.java b/src/test/java/blackjack/domain/PlayerNameTest.java index 0985856157..d486790ace 100644 --- a/src/test/java/blackjack/domain/PlayerNameTest.java +++ b/src/test/java/blackjack/domain/PlayerNameTest.java @@ -5,6 +5,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; +@DisplayName("참가자 이름 테스트") class PlayerNameTest { @DisplayName("이름은 빈 문자열일 수 없다") diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java index 391005f69b..8db5767f41 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -8,6 +8,7 @@ import static org.assertj.core.api.Assertions.assertThat; +@DisplayName("참가자 테스트") class PlayerTest { @DisplayName("플레이어 핸드에 카드를 추가할 수 있다") From c100676153c802ce9df608d8fd3912b4dde8fd73 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 6 Mar 2024 18:29:56 +0900 Subject: [PATCH 012/124] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=ED=8E=B8=EC=9D=98=20=ED=94=BD=EC=8A=A4=EC=B2=98=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/fixture/CardDeckFixture.java | 14 ++++++++++++++ src/test/java/fixture/CardFixture.java | 19 +++++++++++++++++++ src/test/java/fixture/HandFixture.java | 14 ++++++++++++++ src/test/java/fixture/PlayerFixture.java | 11 +++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/test/java/fixture/CardDeckFixture.java create mode 100644 src/test/java/fixture/CardFixture.java create mode 100644 src/test/java/fixture/HandFixture.java create mode 100644 src/test/java/fixture/PlayerFixture.java diff --git a/src/test/java/fixture/CardDeckFixture.java b/src/test/java/fixture/CardDeckFixture.java new file mode 100644 index 0000000000..dde9f942ef --- /dev/null +++ b/src/test/java/fixture/CardDeckFixture.java @@ -0,0 +1,14 @@ +package fixture; + +import blackjack.domain.CardDeck; + +import java.util.Arrays; + +public class CardDeckFixture { + + public static CardDeck of(int... numbers) { + return new CardDeck(Arrays.stream(numbers) + .mapToObj(CardFixture::from) + .toList()); + } +} diff --git a/src/test/java/fixture/CardFixture.java b/src/test/java/fixture/CardFixture.java new file mode 100644 index 0000000000..edfbd71c55 --- /dev/null +++ b/src/test/java/fixture/CardFixture.java @@ -0,0 +1,19 @@ +package fixture; + +import blackjack.domain.Card; +import blackjack.domain.CardNumber; + +import java.util.Arrays; + +import static blackjack.domain.CardShape.HEART; + +public class CardFixture { + + public static Card from(int number) { + CardNumber cardNumber = Arrays.stream(CardNumber.values()) + .filter(cn -> cn.getValue() == number) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + return new Card(HEART, cardNumber); + } +} diff --git a/src/test/java/fixture/HandFixture.java b/src/test/java/fixture/HandFixture.java new file mode 100644 index 0000000000..fafbdb3103 --- /dev/null +++ b/src/test/java/fixture/HandFixture.java @@ -0,0 +1,14 @@ +package fixture; + +import blackjack.domain.Hand; + +import java.util.Arrays; + +public class HandFixture { + + public static Hand of(int... numbers) { + return new Hand(Arrays.stream(numbers) + .mapToObj(CardFixture::from) + .toList()); + } +} diff --git a/src/test/java/fixture/PlayerFixture.java b/src/test/java/fixture/PlayerFixture.java new file mode 100644 index 0000000000..f6033ca9bd --- /dev/null +++ b/src/test/java/fixture/PlayerFixture.java @@ -0,0 +1,11 @@ +package fixture; + +import blackjack.domain.Player; +import blackjack.domain.PlayerName; + +public class PlayerFixture { + + public static Player of(String name, int... hand) { + return new Player(new PlayerName(name), HandFixture.of(hand)); + } +} From 3e432f8760ddc090d1231e1dd1727e5be03db7c4 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 6 Mar 2024 18:46:54 +0900 Subject: [PATCH 013/124] =?UTF-8?q?feat:=20Player=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=20=EB=8F=99=EB=93=B1=EC=84=B1=20=EB=B9=84=EA=B5=90=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Player.java | 15 +++++++++++++++ src/main/java/blackjack/domain/PlayerName.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/Player.java index f1a2bea633..309f5d593f 100644 --- a/src/main/java/blackjack/domain/Player.java +++ b/src/main/java/blackjack/domain/Player.java @@ -1,5 +1,7 @@ package blackjack.domain; +import java.util.Objects; + public class Player { private final PlayerName name; @@ -17,4 +19,17 @@ public void appendCard(Card card) { public boolean handSummationExceed(int target) { return hand.sum() > target; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Player player = (Player) o; + return Objects.equals(name, player.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } } diff --git a/src/main/java/blackjack/domain/PlayerName.java b/src/main/java/blackjack/domain/PlayerName.java index 69e8a92ac0..db2707de4c 100644 --- a/src/main/java/blackjack/domain/PlayerName.java +++ b/src/main/java/blackjack/domain/PlayerName.java @@ -1,5 +1,7 @@ package blackjack.domain; +import java.util.Objects; + public class PlayerName { private final String value; @@ -18,4 +20,17 @@ private void validateNotEmpty(String value) { throw new IllegalArgumentException("[ERROR] 이름이 빈 문자열입니다."); } } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PlayerName that = (PlayerName) o; + return Objects.equals(value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } } From 9f6b0096d4f07d578fe6e19a2b8f757f5644b07e Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 6 Mar 2024 20:20:49 +0900 Subject: [PATCH 014/124] =?UTF-8?q?feat:=20Player=EC=9D=98=20=EC=9D=BC?= =?UTF-8?q?=EA=B8=89=20=EC=BB=AC=EB=A0=89=EC=85=98=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=20Players=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +-- src/main/java/blackjack/domain/Players.java | 51 ++++++++++++++ .../java/blackjack/domain/PlayersTest.java | 67 +++++++++++++++++++ 3 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 src/main/java/blackjack/domain/Players.java create mode 100644 src/test/java/blackjack/domain/PlayersTest.java diff --git a/README.md b/README.md index bac26f39b1..3ecabd0b9a 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,11 @@ - [x] 플레이어 - [x] 배정 받은 카드를 관리한다. - [x] 보유하고 있는 카드합이 21을 넘는지 확인할 수 있다. - - [ ] 플레이어들 - - [ ] 이름이 중복되는 플레이어는 존재할 수 없다. - - [ ] 게임 참가자는 딜러 제외 3명 이하여야 한다. - - [ ] 특정 카드합보다 작은 카드합인 플레이어를 셀 수 있다. + - [x] 플레이어들 + - [x] 이름이 중복되는 플레이어는 존재할 수 없다. + - [x] 플레이어가 없는 경우는 있을 수 없다. + - [x] 게임 참가자는 딜러 제외 3명 이하여야 한다. + - [x] 특정 카드합을 초과하는 카드합을 가진 플레이어를 셀 수 있다. - [ ] 딜러 - [ ] 배정 받은 카드를 관리한다. - [ ] 보유하고 있는 카드합이 17을 넘는지 확인할 수 있다. diff --git a/src/main/java/blackjack/domain/Players.java b/src/main/java/blackjack/domain/Players.java new file mode 100644 index 0000000000..6a27babd0d --- /dev/null +++ b/src/main/java/blackjack/domain/Players.java @@ -0,0 +1,51 @@ +package blackjack.domain; + +import java.util.List; + +public class Players { + + private static final int MAX_COUNT = 3; + + private final List players; + + public Players(List players) { + validate(players); + this.players = players; + } + + private void validate(List players) { + validateEachPlayerNameUnique(players); + validateEntryNotEmpty(players); + validatePlayerCountRange(players); + } + + private void validateEachPlayerNameUnique(List players) { + if (countUniquePlayer(players) != players.size()) { + throw new IllegalArgumentException("[ERROR] 중복되는 플레이어의 이름이 존재합니다"); + } + } + + private int countUniquePlayer(List players) { + return (int) players.stream() + .distinct() + .count(); + } + + private void validateEntryNotEmpty(List players) { + if (players.isEmpty()) { + throw new IllegalArgumentException("[ERROR] 플레이어가 없습니다"); + } + } + + private void validatePlayerCountRange(List players) { + if (players.size() > MAX_COUNT) { + throw new IllegalArgumentException("[ERROR] 플레이어의 수는 " + MAX_COUNT + "이하여야 합니다"); + } + } + + public int countPlayerWithSumExceed(int target) { + return (int) players.stream() + .filter(player -> !player.handSummationExceed(target)) + .count(); + } +} diff --git a/src/test/java/blackjack/domain/PlayersTest.java b/src/test/java/blackjack/domain/PlayersTest.java new file mode 100644 index 0000000000..d8271d816b --- /dev/null +++ b/src/test/java/blackjack/domain/PlayersTest.java @@ -0,0 +1,67 @@ +package blackjack.domain; + +import fixture.PlayerFixture; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@DisplayName("참가자들 테스트") +class PlayersTest { + + @DisplayName("참가자들 중 이름이 중복되는 경우는 생성 검증에 실패한다") + @Test + void testCreatePlayersWithDuplicateNames() { + Player player1 = PlayerFixture.of("리비", 1, 2); + Player player2 = PlayerFixture.of("리비", 3, 4); + + assertThatThrownBy(() -> new Players(List.of(player1, player2))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 중복되는 플레이어의 이름이 존재합니다"); + } + + @DisplayName("플레이어가 없으면 생성에 실패한다") + @Test + void testCreatePlayersWithEmptyEntry() { + assertThatThrownBy(() -> new Players(List.of())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 플레이어가 없습니다"); + } + + @DisplayName("딜러를 제외한 게임 참여자가 3명을 넘는 경우는 생성 검증에 실패한다") + @Test + void testCreatePlayersWithInvalidEntryCount() { + Player player1 = PlayerFixture.of("리비", 1, 2); + Player player2 = PlayerFixture.of("제리", 3, 4); + Player player3 = PlayerFixture.of("잉크", 1, 2); + Player player4 = PlayerFixture.of("트레", 3, 4); + + assertThatThrownBy(() -> new Players(List.of(player1, player2, player3, player4))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 플레이어의 수는 3이하여야 합니다"); + } + + @DisplayName("생성 검증을 모두 통과하면 생성에 성공한다") + @Test + void testCreateWithValidPlayers() { + Player player1 = PlayerFixture.of("리비", 1, 2); + Player player2 = PlayerFixture.of("제리", 3, 4); + + assertThatCode(() -> new Players(List.of(player1, player2))) + .doesNotThrowAnyException(); + } + + @DisplayName("특정 카드합보다 초과하는 카드합을 가진 플레이어 수를 셀 수 있다") + @Test + void testCountPlayerWithLowerSummation() { + Player player1 = PlayerFixture.of("리비", 4, 5); + Player player2 = PlayerFixture.of("제리", 2, 9); + Players players = new Players(List.of(player1, player2)); + + assertThat(players.countPlayerWithSumExceed(10)).isEqualTo(1); + } +} \ No newline at end of file From d4e3b0237b9fda3631cf5ad9cbd21e80f7a70167 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 11:11:04 +0900 Subject: [PATCH 015/124] =?UTF-8?q?feat:=20Player=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20Getter=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=ED=8D=BC=ED=8B=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Player.java | 4 ++++ src/main/java/blackjack/domain/PlayerName.java | 4 ++++ src/main/java/blackjack/domain/Players.java | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/Player.java index 309f5d593f..34413b31a3 100644 --- a/src/main/java/blackjack/domain/Player.java +++ b/src/main/java/blackjack/domain/Player.java @@ -32,4 +32,8 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(name); } + + public String getName() { + return name.getValue(); + } } diff --git a/src/main/java/blackjack/domain/PlayerName.java b/src/main/java/blackjack/domain/PlayerName.java index db2707de4c..49a5e66745 100644 --- a/src/main/java/blackjack/domain/PlayerName.java +++ b/src/main/java/blackjack/domain/PlayerName.java @@ -33,4 +33,8 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(value); } + + public String getValue() { + return value; + } } diff --git a/src/main/java/blackjack/domain/Players.java b/src/main/java/blackjack/domain/Players.java index 6a27babd0d..5fb4233d5a 100644 --- a/src/main/java/blackjack/domain/Players.java +++ b/src/main/java/blackjack/domain/Players.java @@ -48,4 +48,8 @@ public int countPlayerWithSumExceed(int target) { .filter(player -> !player.handSummationExceed(target)) .count(); } + + public List getPlayers() { + return players; + } } From 3628610d6a8fa206dae3e0447e151fb6d2f14cfa Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 11:23:15 +0900 Subject: [PATCH 016/124] =?UTF-8?q?feat:=20Player=EA=B0=80=20=EA=B0=80?= =?UTF-8?q?=EC=A7=80=EA=B3=A0=20=EC=9E=88=EB=8A=94=20=ED=95=B8=EB=93=9C=20?= =?UTF-8?q?Getter=20=ED=94=84=EB=A1=9C=ED=8D=BC=ED=8B=B0=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Player.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/Player.java index 34413b31a3..4a88e361de 100644 --- a/src/main/java/blackjack/domain/Player.java +++ b/src/main/java/blackjack/domain/Player.java @@ -36,4 +36,8 @@ public int hashCode() { public String getName() { return name.getValue(); } + + public Hand getHand() { + return hand; + } } From ccacf3d7b904c2fdadc1ecf722822aec398600fa Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 11:25:39 +0900 Subject: [PATCH 017/124] =?UTF-8?q?feat:=20Card=20=EA=B2=8C=ED=84=B0=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=8D=BC=ED=8B=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Card.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/blackjack/domain/Card.java b/src/main/java/blackjack/domain/Card.java index ee1b084d9c..f446191ab9 100644 --- a/src/main/java/blackjack/domain/Card.java +++ b/src/main/java/blackjack/domain/Card.java @@ -32,4 +32,8 @@ public int hashCode() { public int getCardNumber() { return cardNumber.getValue(); } + + public String getCardShape() { + return cardShape.getName(); + } } From 43b3f9f1ea8299797c6a0e43b35108df386b6289 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 11:42:50 +0900 Subject: [PATCH 018/124] =?UTF-8?q?feat:=20Player=EA=B0=80=20=EC=9E=90?= =?UTF-8?q?=EC=8B=A0=EC=9D=B4=20=EA=B0=80=EC=A7=80=EA=B3=A0=20=EC=9E=88?= =?UTF-8?q?=EB=8A=94=20=EC=B9=B4=EB=93=9C=EC=9D=98=20=ED=95=A9=EA=B3=84?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=98=ED=99=98=ED=95=A0=20=EC=88=98=20=EC=9E=88?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Player.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/Player.java index 4a88e361de..ad640992c7 100644 --- a/src/main/java/blackjack/domain/Player.java +++ b/src/main/java/blackjack/domain/Player.java @@ -20,6 +20,10 @@ public boolean handSummationExceed(int target) { return hand.sum() > target; } + public int calculateHandSum() { + return hand.sum(); + } + @Override public boolean equals(Object o) { if (this == o) return true; From 19e1ebbdbe7ef57ff3e0d2089c40068cde3d8b17 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 11:55:26 +0900 Subject: [PATCH 019/124] =?UTF-8?q?feat:=20InputView=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/view/InputView.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/blackjack/view/InputView.java diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java new file mode 100644 index 0000000000..9b9b82dd28 --- /dev/null +++ b/src/main/java/blackjack/view/InputView.java @@ -0,0 +1,20 @@ +package blackjack.view; + +import java.util.Scanner; + +public class InputView { + + private static final String LINE_SEPARATOR = System.lineSeparator(); + private final Scanner scanner = new Scanner(System.in); + + public String readNames() { + System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); + return scanner.nextLine(); + } + + public String readDrawPlan(String name) { + String message = String.format("%s는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)", name); + System.out.println(message); + return scanner.nextLine(); + } +} From 96f1d18be3cb408f9e7f416c362574c2b106d712 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 11:55:43 +0900 Subject: [PATCH 020/124] =?UTF-8?q?feat:=20=EC=B6=9C=EB=A0=A5=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EB=A6=AC=EC=A1=B8=EB=B2=84=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/view/MessageResolver.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/blackjack/view/MessageResolver.java diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java new file mode 100644 index 0000000000..1e4c37180e --- /dev/null +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -0,0 +1,55 @@ +package blackjack.view; + +import blackjack.domain.Card; +import blackjack.domain.Hand; +import blackjack.domain.Player; +import blackjack.domain.Players; + +import java.util.stream.Collectors; + +public class MessageResolver { + + private static final String LINE_SEPARATOR = System.lineSeparator(); + private static final String SEPARATOR = ", "; + + public String resolveHandOutEventMessage(Players players, int handOutCount) { + String namesMessage = resolveNamesMessage(players); + return String.format("딜러와 %s에게 %d장을 나누었습니다.", namesMessage, handOutCount); + } + + private String resolveNamesMessage(Players players) { + return players.getPlayers().stream() + .map(Player::getName) + .collect(Collectors.joining(SEPARATOR)); + } + + public String resolvePlayerHandMessage(Player player) { + return String.format("%s 카드: %s", player.getName(), resolveHandMessage(player.getHand())); + } + + private String resolveHandMessage(Hand hand) { + return hand.getCards().stream() + .map(this::resolveCardMessage) + .collect(Collectors.joining(SEPARATOR)); + } + + private String resolveCardMessage(Card card) { + return String.format("%d%s", card.getCardNumber(), card.getCardShape()); + } + + public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount) { + return String.format("딜러는 %d이하라 %d장의 카드를 더 받았습니다.", dealerDrawThreshold, popCount); + } + + public String resolvePlayersScoreMessage(Players players) { + return players.getPlayers().stream() + .map(this::resolvePlayerScoreMessage) + .collect(Collectors.joining(LINE_SEPARATOR)); + } + + private String resolvePlayerScoreMessage(Player player) { + String handMessage = resolveHandMessage(player.getHand()); + int sum = player.calculateHandSum(); + return String.format("%s - 결과: %d", handMessage, sum); + } +} From d324ce270e2d00c2371bc606760f0995fb44320c Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 11:55:57 +0900 Subject: [PATCH 021/124] =?UTF-8?q?feat:=20OutputView=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/view/OutputView.java | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/blackjack/view/OutputView.java diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java new file mode 100644 index 0000000000..db901318d1 --- /dev/null +++ b/src/main/java/blackjack/view/OutputView.java @@ -0,0 +1,29 @@ +package blackjack.view; + +import blackjack.domain.Player; +import blackjack.domain.Players; + +public class OutputView { + + private final MessageResolver messageResolver; + + public OutputView(MessageResolver messageResolver) { + this.messageResolver = messageResolver; + } + + public void printHandOutEvent(Players players, int handedCount) { + System.out.println(messageResolver.resolveHandOutEventMessage(players, handedCount)); + } + + public void printPlayerHand(Player player) { + System.out.println(messageResolver.resolvePlayerHandMessage(player)); + } + + public void printDealerPopCount(int dealerPopThreshold, int count) { + System.out.println(messageResolver.resolveDealerPopCountMessage(dealerPopThreshold, count)); + } + + public void printPlayersScore(Players players) { + System.out.println(messageResolver.resolvePlayersScoreMessage(players)); + } +} From d13d6edd1480f4e3b8330bc25e5d3bc5f3990dd6 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 13:27:44 +0900 Subject: [PATCH 022/124] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=A0=95=EB=A6=AC=20=EB=B0=8F?= =?UTF-8?q?=20=EC=B5=9C=EC=8B=A0=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 51 +++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 3ecabd0b9a..49207fe2f5 100644 --- a/README.md +++ b/README.md @@ -11,38 +11,29 @@ # 🛠️ 기능 구현 목록 - [ ] 입력 - - [ ] 게임에 참여할 사람의 이름을 입력 받을 수 있다. - - [ ] 한 장 더 받을지 여부를 입력 받을 수 있다. + - [x] 게임에 참여할 사람의 이름을 입력 받을 수 있다. + - [x] 한 장 더 받을지 여부를 입력 받을 수 있다. - [ ] 입력 검증 - [ ] 카드 추가 여부를 올바른 형태 (y/n)으로 입력했는지 검증할 수 있다. - [ ] 도메인 - - [x] 이름 - - [x] 이름은 빈 문자열일 수 없다. - - [x] 플레이어 - - [x] 배정 받은 카드를 관리한다. - - [x] 보유하고 있는 카드합이 21을 넘는지 확인할 수 있다. - - [x] 플레이어들 - - [x] 이름이 중복되는 플레이어는 존재할 수 없다. - - [x] 플레이어가 없는 경우는 있을 수 없다. - - [x] 게임 참가자는 딜러 제외 3명 이하여야 한다. - - [x] 특정 카드합을 초과하는 카드합을 가진 플레이어를 셀 수 있다. - - [ ] 딜러 - - [ ] 배정 받은 카드를 관리한다. - - [ ] 보유하고 있는 카드합이 17을 넘는지 확인할 수 있다. - - [ ] 정해진 룰대로 카드를 완성할 수 있다. - - [ ] 카드리스트 (게임 참여자 보유 카드) - - [ ] 에이스 카드가 사용되는 경우 카드의 값을 맥락에 맞게 정한다. - - [x] 카드의 합을 계산할 수 있다. - - [x] 카드를 추가할 수 있다. - - [x] 카드덱 (전체 카드) - - [x] 카드를 지정한 횟수만큼 건네줄 수 있다. - - [x] 카드를 한 장 뽑아서 건네줄 수 있다. - - [x] 보유한 카드 개수보다 많이 뽑으면 예외가 발생한다. - - [ ] BlackJackGame - - [ ] 초기 카드를 분배할 수 있다. - - [ ] player마다 추가 드로우 여부를 유도할 수 있다. - - [ ] 승패 판정 역할을 수행할 수 있다. + - [x] 이름은 빈 문자열일 수 없다. + - [x] 게임 참가자의 핸드에 새로운 카드를 추가할 수 있다. + - [x] 이름이 중복되는 플레이어는 존재할 수 없다. + - [x] 플레이어가 없는 경우는 게임을 시작할 수 없다. + - [x] 게임 참가자는 딜러 제외 3명 이하여야 한다. + - [ ] 카드합 비교를 통해서 승패를 결정할 수 있다. + - [ ] 딜러는 17점 미만이면 카드를 받아야 한다. + - [ ] 딜러는 17점 이상이면 카드를 그만 받아야 한다. + - [ ] 핸드의 최소합이 21을 초과하면 플레이어는 버스트한다. + - [ ] 핸드의 최소합이 21 이하면 플레이어는 카드를 뽑을지 여부를 선택할 수 있다. + - [x] 핸드에 카드를 추가할 수 있다. + - [ ] 핸드의 최소합을 계산할 수 있다. + - [ ] 핸드의 최대합을 계산할 수 있다. + - [x] 카드덱에서 카드를 지정한 개수만큼 건네줄 수 있다. + - [x] 카드덱에서 카드를 한 장 뽑아서 건네줄 수 있다. + - [x] 카드덱에서 보유한 카드 개수보다 많이 뽑으면 예외가 발생한다. + - [ ] 참여자의 핸드에 초기 카드를 분배할 수 있다. - [ ] 출력 - - [ ] 각 참여자의 카드 정보를 출력할 수 있다. - - [ ] 각 참여자의 카드 합을 출력할 수 있다. + - [x] 각 참여자의 카드 정보를 출력할 수 있다. + - [x] 각 참여자의 카드 합을 출력할 수 있다. - [ ] 최종 승패를 출력할 수 있다. From 12d5f01a011151ff76ad04e69382b0403bfe3eed Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 16:16:11 +0900 Subject: [PATCH 023/124] =?UTF-8?q?feat:=20DrawDecision=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/DrawDecision.java | 22 +++++++++++ .../blackjack/domain/DrawDecisionTest.java | 39 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/main/java/blackjack/domain/DrawDecision.java create mode 100644 src/test/java/blackjack/domain/DrawDecisionTest.java diff --git a/src/main/java/blackjack/domain/DrawDecision.java b/src/main/java/blackjack/domain/DrawDecision.java new file mode 100644 index 0000000000..2c00051e1b --- /dev/null +++ b/src/main/java/blackjack/domain/DrawDecision.java @@ -0,0 +1,22 @@ +package blackjack.domain; + +import java.util.Arrays; + +public enum DrawDecision { + + YES("y"), + NO("n"); + + private final String code; + + DrawDecision(String code) { + this.code = code; + } + + public static DrawDecision from(String code) { + return Arrays.stream(DrawDecision.values()) + .filter(drawDecision -> drawDecision.code.equals(code)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("[ERROR] 해당하는 값을 찾지 못하였습니다.")); + } +} diff --git a/src/test/java/blackjack/domain/DrawDecisionTest.java b/src/test/java/blackjack/domain/DrawDecisionTest.java new file mode 100644 index 0000000000..8ffeacd8ab --- /dev/null +++ b/src/test/java/blackjack/domain/DrawDecisionTest.java @@ -0,0 +1,39 @@ +package blackjack.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@DisplayName("드로우 결정 도메인 테스트") +class DrawDecisionTest { + + @DisplayName("존재하지 않는 코드명이면 예외가 발생한다") + @ParameterizedTest + @ValueSource(strings = {"1", "libi", "jerry"}) + void testEnumFromInvalidCode(String code) { + assertThatThrownBy(() -> DrawDecision.from(code)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 해당하는 값을 찾지 못하였습니다."); + } + + @DisplayName("존재하는 코드명이면 적절한 상수를 반환받는다") + @ParameterizedTest + @MethodSource("provideEnumFromCsv") + void testEnumFromValidCode(String code, DrawDecision drawDecision) { + assertThat(DrawDecision.from(code)).isEqualTo(drawDecision); + } + + private static Stream provideEnumFromCsv() { + return Stream.of( + Arguments.of("y", DrawDecision.YES), + Arguments.of("n", DrawDecision.NO) + ); + } +} \ No newline at end of file From e26f327176d5b3c7e1e6d482d5d3cdea4ce7d87b Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 17:25:43 +0900 Subject: [PATCH 024/124] =?UTF-8?q?feat:=20=EC=9D=B4=EB=A6=84=EB=A7=8C=20?= =?UTF-8?q?=EB=B0=9B=EC=95=84=EC=84=9C=20=EB=B9=88=ED=95=B8=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EA=B0=80=EC=A7=84=20=EC=B0=B8=EA=B0=80=EC=9E=90?= =?UTF-8?q?=EB=93=A4=EC=9D=84=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EC=A0=95=EC=A0=81=20=ED=8C=A9=ED=86=A0=EB=A6=AC=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Players.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/blackjack/domain/Players.java b/src/main/java/blackjack/domain/Players.java index 5fb4233d5a..316909e98d 100644 --- a/src/main/java/blackjack/domain/Players.java +++ b/src/main/java/blackjack/domain/Players.java @@ -1,5 +1,6 @@ package blackjack.domain; +import java.util.ArrayList; import java.util.List; public class Players { @@ -8,11 +9,17 @@ public class Players { private final List players; - public Players(List players) { + Players(List players) { validate(players); this.players = players; } + public static Players from(List playerNames) { + return new Players(playerNames.stream() + .map(playerName -> new Player(playerName, new Hand(new ArrayList<>()))) + .toList()); + } + private void validate(List players) { validateEachPlayerNameUnique(players); validateEntryNotEmpty(players); From e3683484129025f7da5dbdb677fe60de0a0de082 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 17:26:33 +0900 Subject: [PATCH 025/124] =?UTF-8?q?feat:=20InputMapper=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/InputMapper.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/blackjack/controller/InputMapper.java diff --git a/src/main/java/blackjack/controller/InputMapper.java b/src/main/java/blackjack/controller/InputMapper.java new file mode 100644 index 0000000000..d1260ba30c --- /dev/null +++ b/src/main/java/blackjack/controller/InputMapper.java @@ -0,0 +1,23 @@ +package blackjack.controller; + +import blackjack.domain.DrawDecision; +import blackjack.domain.PlayerName; + +import java.util.Arrays; +import java.util.List; + +public class InputMapper { + + private static final String DELIMITER = ","; + + public List mapToPlayers(String target) { + String[] split = target.split(DELIMITER); + return Arrays.stream(split) + .map(PlayerName::new) + .toList(); + } + + public DrawDecision mapToDrawDecision(String target) { + return DrawDecision.from(target); + } +} From 0e340ad6309192620d0aa6dc7e188b15ac12fc30 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 17:26:54 +0900 Subject: [PATCH 026/124] =?UTF-8?q?feat:=20=EC=A0=95=ED=95=B4=EC=A7=84=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=EA=B5=AC=EC=84=B1=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=8D=B1=EC=9D=84=20=EB=A7=8C=EB=93=9C=EB=8A=94=20Creator=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/domain/CardDeckCreator.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/blackjack/domain/CardDeckCreator.java diff --git a/src/main/java/blackjack/domain/CardDeckCreator.java b/src/main/java/blackjack/domain/CardDeckCreator.java new file mode 100644 index 0000000000..2729614c92 --- /dev/null +++ b/src/main/java/blackjack/domain/CardDeckCreator.java @@ -0,0 +1,22 @@ +package blackjack.domain; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class CardDeckCreator { + + public CardDeck create() { + List cards = createCards(CardShape.values(), CardNumber.values()); + Collections.shuffle(cards); + return new CardDeck(cards); + } + + private List createCards(CardShape[] cardShapes, CardNumber[] cardNumbers) { + return Arrays.stream(cardShapes) + .flatMap(shape -> Arrays.stream(cardNumbers) + .map(number -> new Card(shape, number))) + .collect(Collectors.toList()); + } +} From b706148ce5102d624c990e742fb94d0b6fdbbad7 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 19:33:04 +0900 Subject: [PATCH 027/124] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=EA=B0=80=20=EC=97=90=EC=9D=B4=EC=8A=A4=EC=97=90=20?= =?UTF-8?q?=ED=95=B4=EB=8B=B9=ED=95=98=EB=8A=94=20=EC=88=AB=EC=9E=90?= =?UTF-8?q?=EC=9D=B8=EC=A7=80=20=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/CardNumber.java | 4 +++ src/main/java/blackjack/domain/Dealer.java | 22 ++++++++++++++++ .../java/blackjack/domain/DealerTest.java | 26 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/main/java/blackjack/domain/Dealer.java create mode 100644 src/test/java/blackjack/domain/DealerTest.java diff --git a/src/main/java/blackjack/domain/CardNumber.java b/src/main/java/blackjack/domain/CardNumber.java index 9bd1f8d036..5bfa93c348 100644 --- a/src/main/java/blackjack/domain/CardNumber.java +++ b/src/main/java/blackjack/domain/CardNumber.java @@ -22,6 +22,10 @@ public enum CardNumber { this.value = value; } + public boolean isAce() { + return this == ACE; + } + public int getValue() { return value; } diff --git a/src/main/java/blackjack/domain/Dealer.java b/src/main/java/blackjack/domain/Dealer.java new file mode 100644 index 0000000000..d891eb2c2c --- /dev/null +++ b/src/main/java/blackjack/domain/Dealer.java @@ -0,0 +1,22 @@ +package blackjack.domain; + +public class Dealer { + + private final Hand hand; + + public Dealer(Hand hand) { + this.hand = hand; + } + + public void appendCard(Card card) { + hand.append(card); + } + + public int calculateLowestScore() { + return hand.calculateMinSum(); + } + + public int calculateHighestScore() { + return hand.calculateMaxSum(); + } +} diff --git a/src/test/java/blackjack/domain/DealerTest.java b/src/test/java/blackjack/domain/DealerTest.java new file mode 100644 index 0000000000..c24163d6ef --- /dev/null +++ b/src/test/java/blackjack/domain/DealerTest.java @@ -0,0 +1,26 @@ +package blackjack.domain; + +import fixture.CardFixture; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("딜러 도메인 테스트") +class DealerTest { + + @DisplayName("딜러 핸드에 카드를 추가할 수 있다") + @Test + void testAppendCardToPlayer() { + List hand = new ArrayList<>(); + Dealer dealer = new Dealer(new Hand(hand)); + + Card card = CardFixture.from(2); + dealer.appendCard(card); + + assertThat(hand).containsExactly(card); + } +} \ No newline at end of file From 0776cfd5ecb2e259cc800c88d59d841517b5deaa Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 19:33:38 +0900 Subject: [PATCH 028/124] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EA=B0=80=20?= =?UTF-8?q?=EC=97=90=EC=9D=B4=EC=8A=A4=EC=9D=B8=EC=A7=80=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Card.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/blackjack/domain/Card.java b/src/main/java/blackjack/domain/Card.java index f446191ab9..f8085e5c17 100644 --- a/src/main/java/blackjack/domain/Card.java +++ b/src/main/java/blackjack/domain/Card.java @@ -12,6 +12,10 @@ public Card(CardShape cardShape, CardNumber cardNumber) { this.cardNumber = cardNumber; } + public boolean isAce() { + return cardNumber.isAce(); + } + @Override public boolean equals(Object o) { if (this == o) { From 0f130c8c99837798d51e75cdfb1314b0aeb8d9b2 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 19:39:05 +0900 Subject: [PATCH 029/124] =?UTF-8?q?test:=20=EC=B9=B4=EB=93=9C=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=EA=B0=80=20=EC=97=90=EC=9D=B4=EC=8A=A4=EC=97=90=20?= =?UTF-8?q?=ED=95=B4=EB=8B=B9=ED=95=98=EB=8A=94=20=EC=88=AB=EC=9E=90?= =?UTF-8?q?=EC=9D=B8=EC=A7=80=20=EA=B2=80=EC=A6=9D=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/CardNumberTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/test/java/blackjack/domain/CardNumberTest.java diff --git a/src/test/java/blackjack/domain/CardNumberTest.java b/src/test/java/blackjack/domain/CardNumberTest.java new file mode 100644 index 0000000000..bbe2b52afc --- /dev/null +++ b/src/test/java/blackjack/domain/CardNumberTest.java @@ -0,0 +1,20 @@ +package blackjack.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +@DisplayName("카드 숫자 enum 테스트") +class CardNumberTest { + + @DisplayName("Ace에 해당하는 지 확인할 수 있다") + @Test + void testIsCardNumberAce() { + assertAll( + () -> assertThat(CardNumber.ACE.isAce()).isTrue(), + () -> assertThat(CardNumber.TWO.isAce()).isFalse() + ); + } +} \ No newline at end of file From 94c37a741f2f8c8e24a365b946968c86bae65948 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 19:41:35 +0900 Subject: [PATCH 030/124] =?UTF-8?q?fix:=20=EB=94=9C=EB=9F=AC=20=ED=95=B8?= =?UTF-8?q?=EB=93=9C=20=ED=95=A9=EA=B3=84=20=EA=B5=AC=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Dealer.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/blackjack/domain/Dealer.java b/src/main/java/blackjack/domain/Dealer.java index d891eb2c2c..1be6a0891a 100644 --- a/src/main/java/blackjack/domain/Dealer.java +++ b/src/main/java/blackjack/domain/Dealer.java @@ -12,11 +12,7 @@ public void appendCard(Card card) { hand.append(card); } - public int calculateLowestScore() { - return hand.calculateMinSum(); - } - - public int calculateHighestScore() { - return hand.calculateMaxSum(); + public int calculateHandSum() { + return hand.sum(); } } From 5b1551f545b1110b1bfaca08179877d19037467e Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 19:42:17 +0900 Subject: [PATCH 031/124] =?UTF-8?q?feat:=20=ED=95=B8=EB=93=9C=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=97=90=EC=9D=B4=EC=8A=A4=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EA=B0=80=20=EB=AA=87=EA=B0=9C=EC=9D=B8=EC=A7=80=20=EC=84=B8?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Hand.java | 6 ++++++ src/test/java/blackjack/domain/HandTest.java | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/blackjack/domain/Hand.java b/src/main/java/blackjack/domain/Hand.java index b8cd18f4cd..aab4764e56 100644 --- a/src/main/java/blackjack/domain/Hand.java +++ b/src/main/java/blackjack/domain/Hand.java @@ -20,6 +20,12 @@ public void append(Card card) { cards.add(card); } + public int countAce() { + return (int) cards.stream() + .filter(Card::isAce) + .count(); + } + public List getCards() { return List.copyOf(cards); } diff --git a/src/test/java/blackjack/domain/HandTest.java b/src/test/java/blackjack/domain/HandTest.java index 86cc67392c..a0415e3ff5 100644 --- a/src/test/java/blackjack/domain/HandTest.java +++ b/src/test/java/blackjack/domain/HandTest.java @@ -1,5 +1,6 @@ package blackjack.domain; +import fixture.HandFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -35,4 +36,11 @@ void testAppendCard() { assertThat(hand.getCards()).containsExactly(card1); } + + @DisplayName("핸드에 에이스 카드가 몇개 있는지 확인할 수 있다") + @Test + void testCountAceInHand() { + Hand hand = HandFixture.of(1, 1, 1, 3, 4, 5); + assertThat(hand.countAce()).isEqualTo(3); + } } From 65f58a3d99ec4ccd89a22158717c1f365678e968 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 19:57:44 +0900 Subject: [PATCH 032/124] =?UTF-8?q?feat:=20Judge=EA=B0=80=20=ED=95=B8?= =?UTF-8?q?=EB=93=9C=EB=A1=9C=EB=B6=80=ED=84=B0=2021=EC=97=90=20=EA=B0=80?= =?UTF-8?q?=EC=9E=A5=20=EA=B0=80=EA=B9=8C=EC=9A=B4=20=EA=B0=92=EC=9D=84=20?= =?UTF-8?q?=EA=B5=AC=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++----- src/main/java/blackjack/domain/Judge.java | 21 +++++++++++++++++++ src/test/java/blackjack/domain/JudgeTest.java | 19 +++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/main/java/blackjack/domain/Judge.java create mode 100644 src/test/java/blackjack/domain/JudgeTest.java diff --git a/README.md b/README.md index 49207fe2f5..c067a6c5aa 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,11 @@ # 🛠️ 기능 구현 목록 -- [ ] 입력 +- [x] 입력 - [x] 게임에 참여할 사람의 이름을 입력 받을 수 있다. - [x] 한 장 더 받을지 여부를 입력 받을 수 있다. -- [ ] 입력 검증 - - [ ] 카드 추가 여부를 올바른 형태 (y/n)으로 입력했는지 검증할 수 있다. +- [x] 입력 검증 + - [x] 카드 추가 여부를 올바른 형태 (y/n)으로 입력했는지 검증할 수 있다. - [ ] 도메인 - [x] 이름은 빈 문자열일 수 없다. - [x] 게임 참가자의 핸드에 새로운 카드를 추가할 수 있다. @@ -24,15 +24,17 @@ - [ ] 카드합 비교를 통해서 승패를 결정할 수 있다. - [ ] 딜러는 17점 미만이면 카드를 받아야 한다. - [ ] 딜러는 17점 이상이면 카드를 그만 받아야 한다. + - [x] 핸드에서 에이스가 몇개있는지 파악할 수 있다 + - [x] 핸드의 합을 계산할 수 있다. + - [x] 저지는 핸드에서 21에 가장 가까운 합을 구할 수 있다. - [ ] 핸드의 최소합이 21을 초과하면 플레이어는 버스트한다. - [ ] 핸드의 최소합이 21 이하면 플레이어는 카드를 뽑을지 여부를 선택할 수 있다. - [x] 핸드에 카드를 추가할 수 있다. - - [ ] 핸드의 최소합을 계산할 수 있다. - - [ ] 핸드의 최대합을 계산할 수 있다. - [x] 카드덱에서 카드를 지정한 개수만큼 건네줄 수 있다. - [x] 카드덱에서 카드를 한 장 뽑아서 건네줄 수 있다. - [x] 카드덱에서 보유한 카드 개수보다 많이 뽑으면 예외가 발생한다. - [ ] 참여자의 핸드에 초기 카드를 분배할 수 있다. + - [ ] - [ ] 출력 - [x] 각 참여자의 카드 정보를 출력할 수 있다. - [x] 각 참여자의 카드 합을 출력할 수 있다. diff --git a/src/main/java/blackjack/domain/Judge.java b/src/main/java/blackjack/domain/Judge.java new file mode 100644 index 0000000000..a94616db93 --- /dev/null +++ b/src/main/java/blackjack/domain/Judge.java @@ -0,0 +1,21 @@ +package blackjack.domain; + +public class Judge { + + private static final int BLACK_JACK = 21; + private static final int ACE_WEIGHT = 10; + + // private static final int DEA + public Judge() { + } + + public int calculateBestScore(Hand hand) { + int aceCount = hand.countAce(); + int sum = hand.sum(); + while (aceCount > 0 && (sum + ACE_WEIGHT) < BLACK_JACK) { + sum += ACE_WEIGHT; + aceCount--; + } + return sum; + } +} diff --git a/src/test/java/blackjack/domain/JudgeTest.java b/src/test/java/blackjack/domain/JudgeTest.java new file mode 100644 index 0000000000..4971922670 --- /dev/null +++ b/src/test/java/blackjack/domain/JudgeTest.java @@ -0,0 +1,19 @@ +package blackjack.domain; + +import fixture.HandFixture; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class JudgeTest { + + @DisplayName("핸드가 건네지면 가장 최선의 합계를 구할 수 있다") + @Test + void testCalculateBestScore() { + Judge judge = new Judge(); + + Hand hand = HandFixture.of(1, 1); + assertThat(judge.calculateBestScore(hand)).isEqualTo(12); + } +} \ No newline at end of file From 04003e54dd98e5937eb78df51651edc318d410ac Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 20:06:50 +0900 Subject: [PATCH 033/124] =?UTF-8?q?feat:=20Hand=EA=B0=80=20=EB=B2=84?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EB=90=98=EC=97=88=EB=8A=94=EC=A7=80=20?= =?UTF-8?q?=ED=8C=90=EB=B3=84=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/blackjack/domain/Judge.java | 8 ++++++++ src/test/java/blackjack/domain/JudgeTest.java | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c067a6c5aa..3ca849e654 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ - [x] 핸드에서 에이스가 몇개있는지 파악할 수 있다 - [x] 핸드의 합을 계산할 수 있다. - [x] 저지는 핸드에서 21에 가장 가까운 합을 구할 수 있다. - - [ ] 핸드의 최소합이 21을 초과하면 플레이어는 버스트한다. + - [x] 핸드의 최소합이 21을 초과하면 플레이어는 버스트한다. - [ ] 핸드의 최소합이 21 이하면 플레이어는 카드를 뽑을지 여부를 선택할 수 있다. - [x] 핸드에 카드를 추가할 수 있다. - [x] 카드덱에서 카드를 지정한 개수만큼 건네줄 수 있다. diff --git a/src/main/java/blackjack/domain/Judge.java b/src/main/java/blackjack/domain/Judge.java index a94616db93..88f8d8265d 100644 --- a/src/main/java/blackjack/domain/Judge.java +++ b/src/main/java/blackjack/domain/Judge.java @@ -18,4 +18,12 @@ public int calculateBestScore(Hand hand) { } return sum; } + + public boolean isBustedHand(Hand hand) { + return BLACK_JACK < calculateBestScore(hand); + } +// +// public void completeDealerHand(Hand hand) { +// a +// } } diff --git a/src/test/java/blackjack/domain/JudgeTest.java b/src/test/java/blackjack/domain/JudgeTest.java index 4971922670..8b2ed4d7e9 100644 --- a/src/test/java/blackjack/domain/JudgeTest.java +++ b/src/test/java/blackjack/domain/JudgeTest.java @@ -16,4 +16,20 @@ void testCalculateBestScore() { Hand hand = HandFixture.of(1, 1); assertThat(judge.calculateBestScore(hand)).isEqualTo(12); } + + @DisplayName("버스트 된 핸드를 판별할 수 있다") + @Test + void testDecideBusted() { + Judge judge = new Judge(); + Hand hand = HandFixture.of(10, 9, 3); + assertThat(judge.isBustedHand(hand)).isTrue(); + } + + @DisplayName("버스트 되지 않은 핸드를 판별할 수 있다") + @Test + void testDecideNotBusted() { + Judge judge = new Judge(); + Hand hand = HandFixture.of(10, 9, 2); + assertThat(judge.isBustedHand(hand)).isFalse(); + } } \ No newline at end of file From 45002da38f61d8a70dbfdf64ade03e7f73aa9045 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 20:19:17 +0900 Subject: [PATCH 034/124] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=20Hit=20?= =?UTF-8?q?=EB=8F=99=EC=9E=91=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Judge.java | 9 ++++---- src/test/java/blackjack/domain/JudgeTest.java | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/blackjack/domain/Judge.java b/src/main/java/blackjack/domain/Judge.java index 88f8d8265d..8f2af9f7d0 100644 --- a/src/main/java/blackjack/domain/Judge.java +++ b/src/main/java/blackjack/domain/Judge.java @@ -4,6 +4,7 @@ public class Judge { private static final int BLACK_JACK = 21; private static final int ACE_WEIGHT = 10; + private static final int DEALER_HIT_THRESHOLD = 17; // private static final int DEA public Judge() { @@ -22,8 +23,8 @@ public int calculateBestScore(Hand hand) { public boolean isBustedHand(Hand hand) { return BLACK_JACK < calculateBestScore(hand); } -// -// public void completeDealerHand(Hand hand) { -// a -// } + + public boolean canDealerHit(Hand hand) { + return calculateBestScore(hand) < DEALER_HIT_THRESHOLD; + } } diff --git a/src/test/java/blackjack/domain/JudgeTest.java b/src/test/java/blackjack/domain/JudgeTest.java index 8b2ed4d7e9..6f2e2dafba 100644 --- a/src/test/java/blackjack/domain/JudgeTest.java +++ b/src/test/java/blackjack/domain/JudgeTest.java @@ -32,4 +32,26 @@ void testDecideNotBusted() { Hand hand = HandFixture.of(10, 9, 2); assertThat(judge.isBustedHand(hand)).isFalse(); } + + @DisplayName("딜러는 17점 미만이면 카드를 받아야 한다") + @Test + void testDealerShouldHit() { + Judge judge = new Judge(); + + Hand hand = HandFixture.of(10, 6); + boolean moreCard = judge.canDealerHit(hand); + + assertThat(moreCard).isTrue(); + } + + @DisplayName("딜러는 17점 이상이면 카드를 받을 수 없다") + @Test + void testDealerShouldStay() { + Judge judge = new Judge(); + + Hand hand = HandFixture.of(10, 7); + boolean moreCard = judge.canDealerHit(hand); + + assertThat(moreCard).isFalse(); + } } \ No newline at end of file From 96f58beafab6f133d7ed489ea6e9f189c0de4e8b Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 20:21:07 +0900 Subject: [PATCH 035/124] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=20=ED=95=B8?= =?UTF-8?q?=EB=93=9C=20=EA=B2=8C=ED=84=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Dealer.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/blackjack/domain/Dealer.java b/src/main/java/blackjack/domain/Dealer.java index 1be6a0891a..29226fc218 100644 --- a/src/main/java/blackjack/domain/Dealer.java +++ b/src/main/java/blackjack/domain/Dealer.java @@ -15,4 +15,8 @@ public void appendCard(Card card) { public int calculateHandSum() { return hand.sum(); } + + public Hand getHand() { + return hand; + } } From f38f988e5da0426c8e9f9bbe1c0c4b2f42bc310d Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 20:23:48 +0900 Subject: [PATCH 036/124] =?UTF-8?q?refactor:=20=EB=94=9C=EB=9F=AC=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EB=A1=9C=EC=A7=81=20=ED=8C=8C=EB=9D=BC?= =?UTF-8?q?=EB=AF=B8=ED=84=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- src/main/java/blackjack/domain/Judge.java | 8 ++------ src/test/java/blackjack/domain/JudgeTest.java | 12 ++++++------ 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 3ca849e654..85fd816d50 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ - [x] 플레이어가 없는 경우는 게임을 시작할 수 없다. - [x] 게임 참가자는 딜러 제외 3명 이하여야 한다. - [ ] 카드합 비교를 통해서 승패를 결정할 수 있다. - - [ ] 딜러는 17점 미만이면 카드를 받아야 한다. - - [ ] 딜러는 17점 이상이면 카드를 그만 받아야 한다. + - [x] 딜러는 17점 미만이면 카드를 받아야 한다. + - [x] 딜러는 17점 이상이면 카드를 그만 받아야 한다. - [x] 핸드에서 에이스가 몇개있는지 파악할 수 있다 - [x] 핸드의 합을 계산할 수 있다. - [x] 저지는 핸드에서 21에 가장 가까운 합을 구할 수 있다. diff --git a/src/main/java/blackjack/domain/Judge.java b/src/main/java/blackjack/domain/Judge.java index 8f2af9f7d0..bcfd2d07a0 100644 --- a/src/main/java/blackjack/domain/Judge.java +++ b/src/main/java/blackjack/domain/Judge.java @@ -6,10 +6,6 @@ public class Judge { private static final int ACE_WEIGHT = 10; private static final int DEALER_HIT_THRESHOLD = 17; - // private static final int DEA - public Judge() { - } - public int calculateBestScore(Hand hand) { int aceCount = hand.countAce(); int sum = hand.sum(); @@ -24,7 +20,7 @@ public boolean isBustedHand(Hand hand) { return BLACK_JACK < calculateBestScore(hand); } - public boolean canDealerHit(Hand hand) { - return calculateBestScore(hand) < DEALER_HIT_THRESHOLD; + public boolean canDealerHit(Dealer dealer) { + return calculateBestScore(dealer.getHand()) < DEALER_HIT_THRESHOLD; } } diff --git a/src/test/java/blackjack/domain/JudgeTest.java b/src/test/java/blackjack/domain/JudgeTest.java index 6f2e2dafba..6119b802c5 100644 --- a/src/test/java/blackjack/domain/JudgeTest.java +++ b/src/test/java/blackjack/domain/JudgeTest.java @@ -37,21 +37,21 @@ void testDecideNotBusted() { @Test void testDealerShouldHit() { Judge judge = new Judge(); - Hand hand = HandFixture.of(10, 6); - boolean moreCard = judge.canDealerHit(hand); + Dealer dealer = new Dealer(hand); + boolean hit = judge.canDealerHit(dealer); - assertThat(moreCard).isTrue(); + assertThat(hit).isTrue(); } @DisplayName("딜러는 17점 이상이면 카드를 받을 수 없다") @Test void testDealerShouldStay() { Judge judge = new Judge(); - Hand hand = HandFixture.of(10, 7); - boolean moreCard = judge.canDealerHit(hand); + Dealer dealer = new Dealer(hand); + boolean hit = judge.canDealerHit(dealer); - assertThat(moreCard).isFalse(); + assertThat(hit).isFalse(); } } \ No newline at end of file From 9819609f088db67704aa58d9020338bd9790fa4e Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 20:37:43 +0900 Subject: [PATCH 037/124] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EC=9D=98=20=EC=8A=B9=ED=8C=A8=20=EA=B2=B0=EC=A0=95=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- src/main/java/blackjack/domain/Judge.java | 10 ++++++++ src/test/java/blackjack/domain/JudgeTest.java | 25 ++++++++++++++----- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 85fd816d50..c14867b6c6 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ - [x] 이름이 중복되는 플레이어는 존재할 수 없다. - [x] 플레이어가 없는 경우는 게임을 시작할 수 없다. - [x] 게임 참가자는 딜러 제외 3명 이하여야 한다. - - [ ] 카드합 비교를 통해서 승패를 결정할 수 있다. + - [x] 카드합 비교를 통해서 플레이어의 승패를 결정할 수 있다. + - [ ] 카드합 비교를 통해서 딜러의 승패를 결정할 수 있다. - [x] 딜러는 17점 미만이면 카드를 받아야 한다. - [x] 딜러는 17점 이상이면 카드를 그만 받아야 한다. - [x] 핸드에서 에이스가 몇개있는지 파악할 수 있다 diff --git a/src/main/java/blackjack/domain/Judge.java b/src/main/java/blackjack/domain/Judge.java index bcfd2d07a0..4a67ac0006 100644 --- a/src/main/java/blackjack/domain/Judge.java +++ b/src/main/java/blackjack/domain/Judge.java @@ -23,4 +23,14 @@ public boolean isBustedHand(Hand hand) { public boolean canDealerHit(Dealer dealer) { return calculateBestScore(dealer.getHand()) < DEALER_HIT_THRESHOLD; } + + public boolean isPlayerWin(Dealer dealer, Player player) { + if (isBustedHand(player.getHand())) { + return false; + } + if (isBustedHand(dealer.getHand())) { + return true; + } + return calculateBestScore(player.getHand()) > calculateBestScore(dealer.getHand()); + } } diff --git a/src/test/java/blackjack/domain/JudgeTest.java b/src/test/java/blackjack/domain/JudgeTest.java index 6119b802c5..e48240760d 100644 --- a/src/test/java/blackjack/domain/JudgeTest.java +++ b/src/test/java/blackjack/domain/JudgeTest.java @@ -1,6 +1,8 @@ package blackjack.domain; import fixture.HandFixture; +import fixture.PlayerFixture; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -8,11 +10,16 @@ class JudgeTest { + private Judge judge; + + @BeforeEach + void setUp() { + judge = new Judge(); + } + @DisplayName("핸드가 건네지면 가장 최선의 합계를 구할 수 있다") @Test void testCalculateBestScore() { - Judge judge = new Judge(); - Hand hand = HandFixture.of(1, 1); assertThat(judge.calculateBestScore(hand)).isEqualTo(12); } @@ -20,7 +27,6 @@ void testCalculateBestScore() { @DisplayName("버스트 된 핸드를 판별할 수 있다") @Test void testDecideBusted() { - Judge judge = new Judge(); Hand hand = HandFixture.of(10, 9, 3); assertThat(judge.isBustedHand(hand)).isTrue(); } @@ -28,7 +34,6 @@ void testDecideBusted() { @DisplayName("버스트 되지 않은 핸드를 판별할 수 있다") @Test void testDecideNotBusted() { - Judge judge = new Judge(); Hand hand = HandFixture.of(10, 9, 2); assertThat(judge.isBustedHand(hand)).isFalse(); } @@ -36,7 +41,6 @@ void testDecideNotBusted() { @DisplayName("딜러는 17점 미만이면 카드를 받아야 한다") @Test void testDealerShouldHit() { - Judge judge = new Judge(); Hand hand = HandFixture.of(10, 6); Dealer dealer = new Dealer(hand); boolean hit = judge.canDealerHit(dealer); @@ -47,11 +51,20 @@ void testDealerShouldHit() { @DisplayName("딜러는 17점 이상이면 카드를 받을 수 없다") @Test void testDealerShouldStay() { - Judge judge = new Judge(); Hand hand = HandFixture.of(10, 7); Dealer dealer = new Dealer(hand); boolean hit = judge.canDealerHit(dealer); assertThat(hit).isFalse(); } + + @DisplayName("딜러와 플레이어 중 누가 이겼는지 알 수 있다") + @Test + void testSelectWinner() { + Hand hand = HandFixture.of(10, 7); + Dealer dealer = new Dealer(hand); + Player player = PlayerFixture.of("pobi", 10, 6); + + assertThat(judge.isPlayerWin(dealer, player)).isFalse(); + } } \ No newline at end of file From 7b36a348d31e6d85dc2e24b02ec8778c3861578e Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 20:43:24 +0900 Subject: [PATCH 038/124] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EA=B2=B0=EA=B3=BC=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/domain/DealerGameResult.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/blackjack/domain/DealerGameResult.java diff --git a/src/main/java/blackjack/domain/DealerGameResult.java b/src/main/java/blackjack/domain/DealerGameResult.java new file mode 100644 index 0000000000..6e0eae9731 --- /dev/null +++ b/src/main/java/blackjack/domain/DealerGameResult.java @@ -0,0 +1,20 @@ +package blackjack.domain; + +public class DealerGameResult { + + private final int winCount; + private final int loseCount; + + public DealerGameResult(int winCount, int loseCount) { + this.winCount = winCount; + this.loseCount = loseCount; + } + + public int getWinCount() { + return winCount; + } + + public int getLoseCount() { + return loseCount; + } +} From 23364380fa0488de52831ffbe415d3bd95033be5 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 20:44:55 +0900 Subject: [PATCH 039/124] =?UTF-8?q?feat:=20Players=20=ED=94=8C=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=96=B4=20=EC=88=98=20=EB=B0=98=ED=99=98=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Players.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/blackjack/domain/Players.java b/src/main/java/blackjack/domain/Players.java index 316909e98d..34a77e5763 100644 --- a/src/main/java/blackjack/domain/Players.java +++ b/src/main/java/blackjack/domain/Players.java @@ -20,6 +20,10 @@ public static Players from(List playerNames) { .toList()); } + public int countPlayer() { + return players.size(); + } + private void validate(List players) { validateEachPlayerNameUnique(players); validateEntryNotEmpty(players); From 89a7c3672309b1a1962692238bcc4fb80cc7e143 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 20:52:42 +0900 Subject: [PATCH 040/124] =?UTF-8?q?feat:=20Judge=20=EB=94=9C=EB=9F=AC?= =?UTF-8?q?=EC=9D=98=20=EC=8A=B9=ED=8C=A8=EB=A5=BC=20=EA=B2=B0=EC=A0=95?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Judge.java | 9 ++++++- src/test/java/blackjack/domain/JudgeTest.java | 27 ++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/blackjack/domain/Judge.java b/src/main/java/blackjack/domain/Judge.java index 4a67ac0006..cc060f910e 100644 --- a/src/main/java/blackjack/domain/Judge.java +++ b/src/main/java/blackjack/domain/Judge.java @@ -9,7 +9,7 @@ public class Judge { public int calculateBestScore(Hand hand) { int aceCount = hand.countAce(); int sum = hand.sum(); - while (aceCount > 0 && (sum + ACE_WEIGHT) < BLACK_JACK) { + while (aceCount > 0 && (sum + ACE_WEIGHT) <= BLACK_JACK) { sum += ACE_WEIGHT; aceCount--; } @@ -33,4 +33,11 @@ public boolean isPlayerWin(Dealer dealer, Player player) { } return calculateBestScore(player.getHand()) > calculateBestScore(dealer.getHand()); } + + public DealerGameResult calculateDealerResult(Dealer dealer, Players players) { + int dealerLoseCount = (int) players.getPlayers().stream() + .filter(player -> isPlayerWin(dealer, player)) + .count(); + return new DealerGameResult(players.countPlayer() - dealerLoseCount, dealerLoseCount); + } } diff --git a/src/test/java/blackjack/domain/JudgeTest.java b/src/test/java/blackjack/domain/JudgeTest.java index e48240760d..b620105e9b 100644 --- a/src/test/java/blackjack/domain/JudgeTest.java +++ b/src/test/java/blackjack/domain/JudgeTest.java @@ -6,7 +6,10 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.List; + import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; class JudgeTest { @@ -20,8 +23,8 @@ void setUp() { @DisplayName("핸드가 건네지면 가장 최선의 합계를 구할 수 있다") @Test void testCalculateBestScore() { - Hand hand = HandFixture.of(1, 1); - assertThat(judge.calculateBestScore(hand)).isEqualTo(12); + Hand hand = HandFixture.of(2, 8, 1); + assertThat(judge.calculateBestScore(hand)).isEqualTo(21); } @DisplayName("버스트 된 핸드를 판별할 수 있다") @@ -67,4 +70,22 @@ void testSelectWinner() { assertThat(judge.isPlayerWin(dealer, player)).isFalse(); } -} \ No newline at end of file + + @DisplayName("딜러의 게임 결과를 계산할 수 있다") + @Test + void testDealerResult() { + Hand hand = HandFixture.of(3, 9, 8); + Dealer dealer = new Dealer(hand); + + Player player1 = PlayerFixture.of("pobi", 2, 8, 1); + Player player2 = PlayerFixture.of("jason", 7, 10); + Players players = new Players(List.of(player1, player2)); + + DealerGameResult dealerGameResult = judge.calculateDealerResult(dealer, players); + + assertAll( + () -> assertThat(dealerGameResult.getWinCount()).isEqualTo(1), + () -> assertThat(dealerGameResult.getLoseCount()).isEqualTo(1) + ); + } +} From 2f8a47b75662f7794d61123b63b7dd641fb423a3 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 7 Mar 2024 21:03:30 +0900 Subject: [PATCH 041/124] =?UTF-8?q?feat:=20=EC=B5=9C=EC=A2=85=20=EC=8A=B9?= =?UTF-8?q?=ED=8C=A8=20=EC=B6=9C=EB=A0=A5=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++---- .../java/blackjack/view/MessageResolver.java | 21 +++++++++++++++---- src/main/java/blackjack/view/OutputView.java | 9 ++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c14867b6c6..e371ff49df 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ - [x] 플레이어가 없는 경우는 게임을 시작할 수 없다. - [x] 게임 참가자는 딜러 제외 3명 이하여야 한다. - [x] 카드합 비교를 통해서 플레이어의 승패를 결정할 수 있다. - - [ ] 카드합 비교를 통해서 딜러의 승패를 결정할 수 있다. + - [x] 카드합 비교를 통해서 딜러의 승패를 계산할 수 있다. - [x] 딜러는 17점 미만이면 카드를 받아야 한다. - [x] 딜러는 17점 이상이면 카드를 그만 받아야 한다. - [x] 핸드에서 에이스가 몇개있는지 파악할 수 있다 @@ -35,8 +35,7 @@ - [x] 카드덱에서 카드를 한 장 뽑아서 건네줄 수 있다. - [x] 카드덱에서 보유한 카드 개수보다 많이 뽑으면 예외가 발생한다. - [ ] 참여자의 핸드에 초기 카드를 분배할 수 있다. - - [ ] -- [ ] 출력 +- [x] 출력 - [x] 각 참여자의 카드 정보를 출력할 수 있다. - [x] 각 참여자의 카드 합을 출력할 수 있다. - - [ ] 최종 승패를 출력할 수 있다. + - [x] 최종 승패를 출력할 수 있다. diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index 1e4c37180e..dd0061ffd9 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -1,9 +1,6 @@ package blackjack.view; -import blackjack.domain.Card; -import blackjack.domain.Hand; -import blackjack.domain.Player; -import blackjack.domain.Players; +import blackjack.domain.*; import java.util.stream.Collectors; @@ -52,4 +49,20 @@ private String resolvePlayerScoreMessage(Player player) { int sum = player.calculateHandSum(); return String.format("%s - 결과: %d", handMessage, sum); } + + public String resolvePlayerGameResult(Player player, boolean win) { + return String.format("%s: %s", player.getName(), resolveGameResultMessage(win)); + } + + private String resolveGameResultMessage(boolean win) { + if (win) { + return "승"; + } + return "패"; + } + + public String resolveDealerGameResult(DealerGameResult dealerGameResult) { + return String.format("딜러: %d승 %d패", dealerGameResult.getWinCount(), dealerGameResult.getLoseCount()); + } + } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index db901318d1..5e8f8614bc 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,5 +1,6 @@ package blackjack.view; +import blackjack.domain.DealerGameResult; import blackjack.domain.Player; import blackjack.domain.Players; @@ -26,4 +27,12 @@ public void printDealerPopCount(int dealerPopThreshold, int count) { public void printPlayersScore(Players players) { System.out.println(messageResolver.resolvePlayersScoreMessage(players)); } + + public void printPlayerGameResult(Player player, boolean win) { + System.out.println(messageResolver.resolvePlayerGameResult(player, win)); + } + + public void printDealerGameResult(DealerGameResult dealerGameResult) { + System.out.println(messageResolver.resolveDealerGameResult(dealerGameResult)); + } } From 32cc01a4fe1ad191c2165de8287a95e73b2062e5 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 13:09:55 +0900 Subject: [PATCH 042/124] =?UTF-8?q?feat:=20CardNumber=20=EC=97=B4=EA=B1=B0?= =?UTF-8?q?=ED=98=95=20=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/CardNumber.java | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/blackjack/domain/CardNumber.java b/src/main/java/blackjack/domain/CardNumber.java index 5bfa93c348..7f2b46c9fb 100644 --- a/src/main/java/blackjack/domain/CardNumber.java +++ b/src/main/java/blackjack/domain/CardNumber.java @@ -2,24 +2,26 @@ public enum CardNumber { - ACE(1), - TWO(2), - THREE(3), - FOUR(4), - FIVE(5), - SIX(6), - SEVEN(7), - EIGHT(8), - NINE(9), - TEN(10), - JACK(10), - QUEEN(10), - KING(10); + ACE(1, "A"), + TWO(2, "2"), + THREE(3, "3"), + FOUR(4, "4"), + FIVE(5, "5"), + SIX(6, "6"), + SEVEN(7, "7"), + EIGHT(8, "8"), + NINE(9, "9"), + TEN(10, "10"), + JACK(10, "J"), + QUEEN(10, "Q"), + KING(10, "K"); private final int value; + private final String name; - CardNumber(int value) { + CardNumber(int value, String name) { this.value = value; + this.name = name; } public boolean isAce() { @@ -29,4 +31,9 @@ public boolean isAce() { public int getValue() { return value; } + + public String getName() { + return name; + } } + From 287ab8c4d88c77424628b1ce0e98454d08288f5b Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 13:12:44 +0900 Subject: [PATCH 043/124] =?UTF-8?q?feat:=20Card=EC=9D=98=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Card.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/blackjack/domain/Card.java b/src/main/java/blackjack/domain/Card.java index f8085e5c17..dacee41a23 100644 --- a/src/main/java/blackjack/domain/Card.java +++ b/src/main/java/blackjack/domain/Card.java @@ -37,6 +37,10 @@ public int getCardNumber() { return cardNumber.getValue(); } + public String getCardNumberName() { + return cardNumber.getName(); + } + public String getCardShape() { return cardShape.getName(); } From 5579914c56c8efd671d0f1a720812d42ea9f9c65 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 13:15:01 +0900 Subject: [PATCH 044/124] =?UTF-8?q?fix:=20=EC=B9=B4=EB=93=9C=EC=9D=98=20?= =?UTF-8?q?=EC=88=AB=EC=9E=90=EA=B0=80=20=EC=95=84=EB=8B=8C=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B7=B0=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/view/MessageResolver.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index dd0061ffd9..75ddaa75c9 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -31,7 +31,7 @@ private String resolveHandMessage(Hand hand) { } private String resolveCardMessage(Card card) { - return String.format("%d%s", card.getCardNumber(), card.getCardShape()); + return String.format("%s%s", card.getCardNumberName(), card.getCardShape()); } public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount) { @@ -44,6 +44,7 @@ public String resolvePlayersScoreMessage(Players players) { .collect(Collectors.joining(LINE_SEPARATOR)); } + //TODO: 이름과 최고 점수를 파라미터로 받도록 개선 private String resolvePlayerScoreMessage(Player player) { String handMessage = resolveHandMessage(player.getHand()); int sum = player.calculateHandSum(); From 022b04a3a5e6dc86ac7caec1f223b26e9fe0a880 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 13:20:30 +0900 Subject: [PATCH 045/124] =?UTF-8?q?fix:=20=EC=99=84=EC=84=B1=EB=90=9C=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=EC=8B=9C=20=ED=94=8C=EB=A0=88=EC=9D=B4=EC=96=B4=EC=9D=98=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EA=B3=BC=20=ED=95=A8=EA=BB=98=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B7=B0=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/view/MessageResolver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index 75ddaa75c9..f13705a9de 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -46,7 +46,7 @@ public String resolvePlayersScoreMessage(Players players) { //TODO: 이름과 최고 점수를 파라미터로 받도록 개선 private String resolvePlayerScoreMessage(Player player) { - String handMessage = resolveHandMessage(player.getHand()); + String handMessage = resolvePlayerHandMessage(player); int sum = player.calculateHandSum(); return String.format("%s - 결과: %d", handMessage, sum); } From 02706a374b0e25938c6713a430f1e59f4bbec0a1 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 13:30:43 +0900 Subject: [PATCH 046/124] =?UTF-8?q?refactor:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EB=93=A4=EC=9D=98=20=EC=A0=90=EC=88=98=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=EC=8B=9C=20=EC=A0=90=EC=88=98=EB=A5=BC=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=ED=95=98=EC=A7=80=20=EC=95=8A=EA=B3=A0=20=EC=A0=84?= =?UTF-8?q?=EB=8B=AC=EB=B0=9B=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/view/MessageResolver.java | 12 ++---------- src/main/java/blackjack/view/OutputView.java | 4 ++-- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index f13705a9de..e242cbd830 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -38,17 +38,9 @@ public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount return String.format("딜러는 %d이하라 %d장의 카드를 더 받았습니다.", dealerDrawThreshold, popCount); } - public String resolvePlayersScoreMessage(Players players) { - return players.getPlayers().stream() - .map(this::resolvePlayerScoreMessage) - .collect(Collectors.joining(LINE_SEPARATOR)); - } - - //TODO: 이름과 최고 점수를 파라미터로 받도록 개선 - private String resolvePlayerScoreMessage(Player player) { + public String resolvePlayerScoreMessage(Player player, int score) { String handMessage = resolvePlayerHandMessage(player); - int sum = player.calculateHandSum(); - return String.format("%s - 결과: %d", handMessage, sum); + return String.format("%s - 결과: %d", handMessage, score); } public String resolvePlayerGameResult(Player player, boolean win) { diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 5e8f8614bc..3d3573ab9c 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -24,8 +24,8 @@ public void printDealerPopCount(int dealerPopThreshold, int count) { System.out.println(messageResolver.resolveDealerPopCountMessage(dealerPopThreshold, count)); } - public void printPlayersScore(Players players) { - System.out.println(messageResolver.resolvePlayersScoreMessage(players)); + public void printPlayerScore(Player player, int score) { + System.out.println(messageResolver.resolvePlayerScoreMessage(player, score)); } public void printPlayerGameResult(Player player, boolean win) { From e9620e6b33c455a9e6955c9828de1711a883648e Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 13:32:55 +0900 Subject: [PATCH 047/124] =?UTF-8?q?feat:=20=EC=BB=A8=ED=8A=B8=EB=A1=A4?= =?UTF-8?q?=EB=9F=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/main/java/blackjack/controller/BlackJackController.java diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java new file mode 100644 index 0000000000..c495ca5e02 --- /dev/null +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -0,0 +1,130 @@ +package blackjack.controller; + +import blackjack.domain.*; +import blackjack.view.InputView; +import blackjack.view.OutputView; + +import java.util.ArrayList; +import java.util.List; + +public class BlackJackController { + + private final InputView inputView; + private final OutputView outputView; + + public BlackJackController(InputView inputView, OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public void run() { + Players players = initPlayers(); + CardDeck cardDeck = initCardDeck(); + Dealer dealer = initDealer(); + + issueInitialCardsToPlayers(players, cardDeck); + issueInitialCardsToDealer(cardDeck, dealer); + outputView.printHandOutEvent(players, 2); + players.getPlayers().forEach(outputView::printPlayerHand); + + //딜러 카드 보여주는 메서드 만들어야 함 (카드 한장만) + Player tempDealer = new Player(new PlayerName("딜러"), dealer.getHand()); + outputView.printPlayerHand(tempDealer); + + completePlayersHand(players, cardDeck); + completeDealerHand(dealer, cardDeck); + + int dealerPopCount = dealer.getHand().getCards().size() - 2; + outputView.printDealerPopCount(16, dealerPopCount); + + printPlayersScore(players); + printDealerGameResult(dealer, players); + + printPlayersGameResult(players, dealer); + + } + + private void printPlayersScore(Players players) { + Judge judge = new Judge(); + players.getPlayers() + .forEach(player -> outputView.printPlayerScore(player, judge.calculateBestScore(player.getHand()))); + } + + private void printPlayersGameResult(Players players, Dealer dealer) { + Judge judge = new Judge(); + players.getPlayers() + .forEach(player -> outputView.printPlayerGameResult(player, judge.isPlayerWin(dealer, player))); + } + + private void printDealerGameResult(Dealer dealer, Players players) { + Judge judge = new Judge(); + DealerGameResult dealerGameResult = judge.calculateDealerResult(dealer, players); + outputView.printDealerGameResult(dealerGameResult); + } + + private void completeDealerHand(Dealer dealer, CardDeck cardDeck) { + Judge judge = new Judge(); + while (judge.canDealerHit(dealer)) { + Card card = cardDeck.popCard(); + dealer.appendCard(card); + } + } + + private void completePlayersHand(Players players, CardDeck cardDeck) { + for (Player player : players.getPlayers()) { + completePlayerHand(player, cardDeck); + } + } + + private void completePlayerHand(Player player, CardDeck cardDeck) { + Judge judge = new Judge(); + while (!judge.isBustedHand(player.getHand())) { + String name = player.getName(); + InputMapper inputMapper = new InputMapper(); + DrawDecision drawDecision = inputMapper.mapToDrawDecision(inputView.readDrawPlan(name)); + + if (drawDecision == DrawDecision.NO) { + break; + } + + Card card = cardDeck.popCard(); + player.appendCard(card); + outputView.printPlayerHand(player); + } + } + + private CardDeck initCardDeck() { + CardDeckCreator cardDeckCreator = new CardDeckCreator(); + return cardDeckCreator.create(); + } + + private Players initPlayers() { + InputMapper inputMapper = new InputMapper(); + List playerNames = inputMapper.mapToPlayers(inputView.readNames()); + return Players.from(playerNames); + } + + private Dealer initDealer() { + Hand hand = new Hand(new ArrayList<>()); + return new Dealer(hand); + } + + private List issueInitialHand(CardDeck cardDeck) { + return cardDeck.popCards(2); + } + + private void addIssuedCardToHand(Hand hand, List cards) { + cards.forEach(hand::append); + } + + private void issueInitialCardsToPlayers(Players players, CardDeck cardDeck) { + players.getPlayers().stream() + .map(Player::getHand) + .forEach(hand -> addIssuedCardToHand(hand, issueInitialHand(cardDeck))); + } + + private void issueInitialCardsToDealer(CardDeck cardDeck, Dealer dealer) { + List cards = issueInitialHand(cardDeck); + addIssuedCardToHand(dealer.getHand(), cards); + } +} From e7dec0cc7846b05108efc4d546ee82d8fb997938 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 13:33:12 +0900 Subject: [PATCH 048/124] =?UTF-8?q?feat:=20=EC=96=B4=ED=94=8C=EB=A6=AC?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=85=98=20=EC=A7=84=EC=9E=85=EC=A0=90?= =?UTF-8?q?=EA=B3=BC=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EC=97=B0?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/blackjack/Application.java diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java new file mode 100644 index 0000000000..a35be6e3f8 --- /dev/null +++ b/src/main/java/blackjack/Application.java @@ -0,0 +1,18 @@ +package blackjack; + +import blackjack.controller.BlackJackController; +import blackjack.view.InputView; +import blackjack.view.MessageResolver; +import blackjack.view.OutputView; + +public class Application { + + public static void main(String[] args) { + + InputView inputView = new InputView(); + OutputView outputView = new OutputView(new MessageResolver()); + + BlackJackController blackJackController = new BlackJackController(inputView, outputView); + blackJackController.run(); + } +} From 101e5502e7c1bdd0fab042eba0ac3f81064d6e7b Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 13:36:39 +0900 Subject: [PATCH 049/124] =?UTF-8?q?refactor:=20Player=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=EB=90=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=A0=9C=EA=B1=B0=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Player.java | 8 ------- src/main/java/blackjack/domain/Players.java | 6 ------ .../java/blackjack/domain/PlayerTest.java | 21 ------------------- .../java/blackjack/domain/PlayersTest.java | 11 ---------- 4 files changed, 46 deletions(-) diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/Player.java index ad640992c7..6cb864ce6b 100644 --- a/src/main/java/blackjack/domain/Player.java +++ b/src/main/java/blackjack/domain/Player.java @@ -16,14 +16,6 @@ public void appendCard(Card card) { hand.append(card); } - public boolean handSummationExceed(int target) { - return hand.sum() > target; - } - - public int calculateHandSum() { - return hand.sum(); - } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/blackjack/domain/Players.java b/src/main/java/blackjack/domain/Players.java index 34a77e5763..9a00691132 100644 --- a/src/main/java/blackjack/domain/Players.java +++ b/src/main/java/blackjack/domain/Players.java @@ -54,12 +54,6 @@ private void validatePlayerCountRange(List players) { } } - public int countPlayerWithSumExceed(int target) { - return (int) players.stream() - .filter(player -> !player.handSummationExceed(target)) - .count(); - } - public List getPlayers() { return players; } diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java index 8db5767f41..75a57c2abd 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -30,25 +30,4 @@ void testAppendCardToPlayer() { assertThat(hand).containsExactly(card1, card2, card3); } - - @DisplayName("보유하고 있는 카드합이 특정 값을 넘는지 확인할 수 있다") - @Test - void testHandSumExceedTarget() { - PlayerName playerName = new PlayerName("pobi"); - - Card card1 = new Card(CardShape.HEART, CardNumber.TEN); - Card card2 = new Card(CardShape.CLUB, CardNumber.NINE); - Card card3 = new Card(CardShape.CLUB, CardNumber.THREE); - - List hand = new ArrayList<>(); - hand.add(card1); - hand.add(card2); - hand.add(card3); - - Player player = new Player(playerName, new Hand(hand)); - - boolean expected = player.handSummationExceed(21); - - assertThat(expected).isEqualTo(true); - } } \ No newline at end of file diff --git a/src/test/java/blackjack/domain/PlayersTest.java b/src/test/java/blackjack/domain/PlayersTest.java index d8271d816b..4ac1040b92 100644 --- a/src/test/java/blackjack/domain/PlayersTest.java +++ b/src/test/java/blackjack/domain/PlayersTest.java @@ -8,7 +8,6 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @DisplayName("참가자들 테스트") class PlayersTest { @@ -54,14 +53,4 @@ void testCreateWithValidPlayers() { assertThatCode(() -> new Players(List.of(player1, player2))) .doesNotThrowAnyException(); } - - @DisplayName("특정 카드합보다 초과하는 카드합을 가진 플레이어 수를 셀 수 있다") - @Test - void testCountPlayerWithLowerSummation() { - Player player1 = PlayerFixture.of("리비", 4, 5); - Player player2 = PlayerFixture.of("제리", 2, 9); - Players players = new Players(List.of(player1, player2)); - - assertThat(players.countPlayerWithSumExceed(10)).isEqualTo(1); - } } \ No newline at end of file From 83be3627cdfbef9f5325b0363d480ffd82ebc186 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 13:53:05 +0900 Subject: [PATCH 050/124] =?UTF-8?q?refactor:=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=20=EA=B5=AC=EC=A1=B0=20=EC=84=B8=EB=B6=84=ED=99=94=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.java | 12 +++++++++++- src/main/java/blackjack/controller/InputMapper.java | 2 +- src/main/java/blackjack/domain/Dealer.java | 3 +++ src/main/java/blackjack/domain/{ => card}/Card.java | 2 +- .../java/blackjack/domain/{ => card}/CardDeck.java | 2 +- .../blackjack/domain/{ => card}/CardDeckCreator.java | 2 +- .../java/blackjack/domain/{ => card}/CardNumber.java | 2 +- .../java/blackjack/domain/{ => card}/CardShape.java | 2 +- .../java/blackjack/domain/{ => player}/Hand.java | 4 +++- .../java/blackjack/domain/{ => player}/Player.java | 4 +++- .../blackjack/domain/{ => player}/PlayerName.java | 2 +- .../java/blackjack/domain/{ => player}/Players.java | 4 ++-- src/main/java/blackjack/domain/{ => rule}/Judge.java | 8 +++++++- src/main/java/blackjack/view/MessageResolver.java | 6 +++++- src/main/java/blackjack/view/OutputView.java | 4 ++-- src/test/java/blackjack/domain/DealerTest.java | 2 ++ .../blackjack/domain/{ => card}/CardDeckTest.java | 2 +- .../blackjack/domain/{ => card}/CardNumberTest.java | 2 +- .../java/blackjack/domain/{ => player}/HandTest.java | 5 ++++- .../domain/{ => player}/PlayerNameTest.java | 2 +- .../blackjack/domain/{ => player}/PlayerTest.java | 5 ++++- .../blackjack/domain/{ => player}/PlayersTest.java | 2 +- .../java/blackjack/domain/{ => rule}/JudgeTest.java | 7 ++++++- src/test/java/fixture/CardDeckFixture.java | 2 +- src/test/java/fixture/CardFixture.java | 6 +++--- src/test/java/fixture/HandFixture.java | 2 +- src/test/java/fixture/PlayerFixture.java | 4 ++-- 27 files changed, 70 insertions(+), 30 deletions(-) rename src/main/java/blackjack/domain/{ => card}/Card.java (97%) rename src/main/java/blackjack/domain/{ => card}/CardDeck.java (95%) rename src/main/java/blackjack/domain/{ => card}/CardDeckCreator.java (95%) rename src/main/java/blackjack/domain/{ => card}/CardNumber.java (95%) rename src/main/java/blackjack/domain/{ => card}/CardShape.java (90%) rename src/main/java/blackjack/domain/{ => player}/Hand.java (89%) rename src/main/java/blackjack/domain/{ => player}/Player.java (91%) rename src/main/java/blackjack/domain/{ => player}/PlayerName.java (96%) rename src/main/java/blackjack/domain/{ => player}/Players.java (95%) rename src/main/java/blackjack/domain/{ => rule}/Judge.java (85%) rename src/test/java/blackjack/domain/{ => card}/CardDeckTest.java (98%) rename src/test/java/blackjack/domain/{ => card}/CardNumberTest.java (94%) rename src/test/java/blackjack/domain/{ => player}/HandTest.java (89%) rename src/test/java/blackjack/domain/{ => player}/PlayerNameTest.java (94%) rename src/test/java/blackjack/domain/{ => player}/PlayerTest.java (85%) rename src/test/java/blackjack/domain/{ => player}/PlayersTest.java (98%) rename src/test/java/blackjack/domain/{ => rule}/JudgeTest.java (92%) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index c495ca5e02..c3098c0099 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -1,6 +1,16 @@ package blackjack.controller; -import blackjack.domain.*; +import blackjack.domain.Dealer; +import blackjack.domain.DealerGameResult; +import blackjack.domain.DrawDecision; +import blackjack.domain.card.Card; +import blackjack.domain.card.CardDeck; +import blackjack.domain.card.CardDeckCreator; +import blackjack.domain.player.Hand; +import blackjack.domain.player.Player; +import blackjack.domain.player.PlayerName; +import blackjack.domain.player.Players; +import blackjack.domain.rule.Judge; import blackjack.view.InputView; import blackjack.view.OutputView; diff --git a/src/main/java/blackjack/controller/InputMapper.java b/src/main/java/blackjack/controller/InputMapper.java index d1260ba30c..3cb440af04 100644 --- a/src/main/java/blackjack/controller/InputMapper.java +++ b/src/main/java/blackjack/controller/InputMapper.java @@ -1,7 +1,7 @@ package blackjack.controller; import blackjack.domain.DrawDecision; -import blackjack.domain.PlayerName; +import blackjack.domain.player.PlayerName; import java.util.Arrays; import java.util.List; diff --git a/src/main/java/blackjack/domain/Dealer.java b/src/main/java/blackjack/domain/Dealer.java index 29226fc218..a38e4efc22 100644 --- a/src/main/java/blackjack/domain/Dealer.java +++ b/src/main/java/blackjack/domain/Dealer.java @@ -1,5 +1,8 @@ package blackjack.domain; +import blackjack.domain.card.Card; +import blackjack.domain.player.Hand; + public class Dealer { private final Hand hand; diff --git a/src/main/java/blackjack/domain/Card.java b/src/main/java/blackjack/domain/card/Card.java similarity index 97% rename from src/main/java/blackjack/domain/Card.java rename to src/main/java/blackjack/domain/card/Card.java index dacee41a23..1581d0eacd 100644 --- a/src/main/java/blackjack/domain/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; import java.util.Objects; diff --git a/src/main/java/blackjack/domain/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java similarity index 95% rename from src/main/java/blackjack/domain/CardDeck.java rename to src/main/java/blackjack/domain/card/CardDeck.java index 0076595ae4..beac6f7f39 100644 --- a/src/main/java/blackjack/domain/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; import java.util.List; import java.util.stream.IntStream; diff --git a/src/main/java/blackjack/domain/CardDeckCreator.java b/src/main/java/blackjack/domain/card/CardDeckCreator.java similarity index 95% rename from src/main/java/blackjack/domain/CardDeckCreator.java rename to src/main/java/blackjack/domain/card/CardDeckCreator.java index 2729614c92..62ee8a30af 100644 --- a/src/main/java/blackjack/domain/CardDeckCreator.java +++ b/src/main/java/blackjack/domain/card/CardDeckCreator.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; import java.util.Arrays; import java.util.Collections; diff --git a/src/main/java/blackjack/domain/CardNumber.java b/src/main/java/blackjack/domain/card/CardNumber.java similarity index 95% rename from src/main/java/blackjack/domain/CardNumber.java rename to src/main/java/blackjack/domain/card/CardNumber.java index 7f2b46c9fb..84c406d783 100644 --- a/src/main/java/blackjack/domain/CardNumber.java +++ b/src/main/java/blackjack/domain/card/CardNumber.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; public enum CardNumber { diff --git a/src/main/java/blackjack/domain/CardShape.java b/src/main/java/blackjack/domain/card/CardShape.java similarity index 90% rename from src/main/java/blackjack/domain/CardShape.java rename to src/main/java/blackjack/domain/card/CardShape.java index 999d581b19..fa799a8774 100644 --- a/src/main/java/blackjack/domain/CardShape.java +++ b/src/main/java/blackjack/domain/card/CardShape.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; public enum CardShape { diff --git a/src/main/java/blackjack/domain/Hand.java b/src/main/java/blackjack/domain/player/Hand.java similarity index 89% rename from src/main/java/blackjack/domain/Hand.java rename to src/main/java/blackjack/domain/player/Hand.java index aab4764e56..f3b76e8834 100644 --- a/src/main/java/blackjack/domain/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -1,4 +1,6 @@ -package blackjack.domain; +package blackjack.domain.player; + +import blackjack.domain.card.Card; import java.util.List; diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/player/Player.java similarity index 91% rename from src/main/java/blackjack/domain/Player.java rename to src/main/java/blackjack/domain/player/Player.java index 6cb864ce6b..151b3b7802 100644 --- a/src/main/java/blackjack/domain/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -1,4 +1,6 @@ -package blackjack.domain; +package blackjack.domain.player; + +import blackjack.domain.card.Card; import java.util.Objects; diff --git a/src/main/java/blackjack/domain/PlayerName.java b/src/main/java/blackjack/domain/player/PlayerName.java similarity index 96% rename from src/main/java/blackjack/domain/PlayerName.java rename to src/main/java/blackjack/domain/player/PlayerName.java index 49a5e66745..0dad86cfeb 100644 --- a/src/main/java/blackjack/domain/PlayerName.java +++ b/src/main/java/blackjack/domain/player/PlayerName.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.player; import java.util.Objects; diff --git a/src/main/java/blackjack/domain/Players.java b/src/main/java/blackjack/domain/player/Players.java similarity index 95% rename from src/main/java/blackjack/domain/Players.java rename to src/main/java/blackjack/domain/player/Players.java index 9a00691132..6942b4f974 100644 --- a/src/main/java/blackjack/domain/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.player; import java.util.ArrayList; import java.util.List; @@ -9,7 +9,7 @@ public class Players { private final List players; - Players(List players) { + public Players(List players) { validate(players); this.players = players; } diff --git a/src/main/java/blackjack/domain/Judge.java b/src/main/java/blackjack/domain/rule/Judge.java similarity index 85% rename from src/main/java/blackjack/domain/Judge.java rename to src/main/java/blackjack/domain/rule/Judge.java index cc060f910e..c30103698b 100644 --- a/src/main/java/blackjack/domain/Judge.java +++ b/src/main/java/blackjack/domain/rule/Judge.java @@ -1,4 +1,10 @@ -package blackjack.domain; +package blackjack.domain.rule; + +import blackjack.domain.Dealer; +import blackjack.domain.DealerGameResult; +import blackjack.domain.player.Hand; +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; public class Judge { diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index e242cbd830..6d244dfbe0 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -1,6 +1,10 @@ package blackjack.view; -import blackjack.domain.*; +import blackjack.domain.DealerGameResult; +import blackjack.domain.card.Card; +import blackjack.domain.player.Hand; +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; import java.util.stream.Collectors; diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 3d3573ab9c..dd88997c02 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,8 +1,8 @@ package blackjack.view; import blackjack.domain.DealerGameResult; -import blackjack.domain.Player; -import blackjack.domain.Players; +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; public class OutputView { diff --git a/src/test/java/blackjack/domain/DealerTest.java b/src/test/java/blackjack/domain/DealerTest.java index c24163d6ef..deb06b4eaf 100644 --- a/src/test/java/blackjack/domain/DealerTest.java +++ b/src/test/java/blackjack/domain/DealerTest.java @@ -1,5 +1,7 @@ package blackjack.domain; +import blackjack.domain.card.Card; +import blackjack.domain.player.Hand; import fixture.CardFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/blackjack/domain/CardDeckTest.java b/src/test/java/blackjack/domain/card/CardDeckTest.java similarity index 98% rename from src/test/java/blackjack/domain/CardDeckTest.java rename to src/test/java/blackjack/domain/card/CardDeckTest.java index 068689d8db..3636139d41 100644 --- a/src/test/java/blackjack/domain/CardDeckTest.java +++ b/src/test/java/blackjack/domain/card/CardDeckTest.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/blackjack/domain/CardNumberTest.java b/src/test/java/blackjack/domain/card/CardNumberTest.java similarity index 94% rename from src/test/java/blackjack/domain/CardNumberTest.java rename to src/test/java/blackjack/domain/card/CardNumberTest.java index bbe2b52afc..01b5e7497b 100644 --- a/src/test/java/blackjack/domain/CardNumberTest.java +++ b/src/test/java/blackjack/domain/card/CardNumberTest.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/blackjack/domain/HandTest.java b/src/test/java/blackjack/domain/player/HandTest.java similarity index 89% rename from src/test/java/blackjack/domain/HandTest.java rename to src/test/java/blackjack/domain/player/HandTest.java index a0415e3ff5..e5f9da78fb 100644 --- a/src/test/java/blackjack/domain/HandTest.java +++ b/src/test/java/blackjack/domain/player/HandTest.java @@ -1,5 +1,8 @@ -package blackjack.domain; +package blackjack.domain.player; +import blackjack.domain.card.Card; +import blackjack.domain.card.CardNumber; +import blackjack.domain.card.CardShape; import fixture.HandFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/blackjack/domain/PlayerNameTest.java b/src/test/java/blackjack/domain/player/PlayerNameTest.java similarity index 94% rename from src/test/java/blackjack/domain/PlayerNameTest.java rename to src/test/java/blackjack/domain/player/PlayerNameTest.java index d486790ace..562402ec14 100644 --- a/src/test/java/blackjack/domain/PlayerNameTest.java +++ b/src/test/java/blackjack/domain/player/PlayerNameTest.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.player; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java similarity index 85% rename from src/test/java/blackjack/domain/PlayerTest.java rename to src/test/java/blackjack/domain/player/PlayerTest.java index 75a57c2abd..01b862d23f 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -1,5 +1,8 @@ -package blackjack.domain; +package blackjack.domain.player; +import blackjack.domain.card.Card; +import blackjack.domain.card.CardNumber; +import blackjack.domain.card.CardShape; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/blackjack/domain/PlayersTest.java b/src/test/java/blackjack/domain/player/PlayersTest.java similarity index 98% rename from src/test/java/blackjack/domain/PlayersTest.java rename to src/test/java/blackjack/domain/player/PlayersTest.java index 4ac1040b92..55bf54dc9f 100644 --- a/src/test/java/blackjack/domain/PlayersTest.java +++ b/src/test/java/blackjack/domain/player/PlayersTest.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.player; import fixture.PlayerFixture; import org.junit.jupiter.api.DisplayName; diff --git a/src/test/java/blackjack/domain/JudgeTest.java b/src/test/java/blackjack/domain/rule/JudgeTest.java similarity index 92% rename from src/test/java/blackjack/domain/JudgeTest.java rename to src/test/java/blackjack/domain/rule/JudgeTest.java index b620105e9b..84c1cb2ebb 100644 --- a/src/test/java/blackjack/domain/JudgeTest.java +++ b/src/test/java/blackjack/domain/rule/JudgeTest.java @@ -1,5 +1,10 @@ -package blackjack.domain; +package blackjack.domain.rule; +import blackjack.domain.Dealer; +import blackjack.domain.DealerGameResult; +import blackjack.domain.player.Hand; +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; import fixture.HandFixture; import fixture.PlayerFixture; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/fixture/CardDeckFixture.java b/src/test/java/fixture/CardDeckFixture.java index dde9f942ef..856ee0c0cf 100644 --- a/src/test/java/fixture/CardDeckFixture.java +++ b/src/test/java/fixture/CardDeckFixture.java @@ -1,6 +1,6 @@ package fixture; -import blackjack.domain.CardDeck; +import blackjack.domain.card.CardDeck; import java.util.Arrays; diff --git a/src/test/java/fixture/CardFixture.java b/src/test/java/fixture/CardFixture.java index edfbd71c55..5711f6c9cf 100644 --- a/src/test/java/fixture/CardFixture.java +++ b/src/test/java/fixture/CardFixture.java @@ -1,11 +1,11 @@ package fixture; -import blackjack.domain.Card; -import blackjack.domain.CardNumber; +import blackjack.domain.card.Card; +import blackjack.domain.card.CardNumber; import java.util.Arrays; -import static blackjack.domain.CardShape.HEART; +import static blackjack.domain.card.CardShape.HEART; public class CardFixture { diff --git a/src/test/java/fixture/HandFixture.java b/src/test/java/fixture/HandFixture.java index fafbdb3103..40572bb714 100644 --- a/src/test/java/fixture/HandFixture.java +++ b/src/test/java/fixture/HandFixture.java @@ -1,6 +1,6 @@ package fixture; -import blackjack.domain.Hand; +import blackjack.domain.player.Hand; import java.util.Arrays; diff --git a/src/test/java/fixture/PlayerFixture.java b/src/test/java/fixture/PlayerFixture.java index f6033ca9bd..5d8ddbd0eb 100644 --- a/src/test/java/fixture/PlayerFixture.java +++ b/src/test/java/fixture/PlayerFixture.java @@ -1,7 +1,7 @@ package fixture; -import blackjack.domain.Player; -import blackjack.domain.PlayerName; +import blackjack.domain.player.Player; +import blackjack.domain.player.PlayerName; public class PlayerFixture { From 3e4259840d9e8a7de1957575ec1460df1632c532 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 14:16:09 +0900 Subject: [PATCH 051/124] =?UTF-8?q?feat:=20Score=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/rule/Score.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/main/java/blackjack/domain/rule/Score.java diff --git a/src/main/java/blackjack/domain/rule/Score.java b/src/main/java/blackjack/domain/rule/Score.java new file mode 100644 index 0000000000..a86db251ae --- /dev/null +++ b/src/main/java/blackjack/domain/rule/Score.java @@ -0,0 +1,14 @@ +package blackjack.domain.rule; + +public class Score { + + private final int value; + + public Score(int value) { + this.value = value; + } + + public int getValue() { + return value; + } +} From 9cdd4e4a2320d253f80b122f85bb349b0f2c953a Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 14:16:21 +0900 Subject: [PATCH 052/124] =?UTF-8?q?feat:=20ScoreCalculator=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/rule/ScoreCalculator.java | 19 ++++++++ .../domain/rule/ScoreCalculatorTest.java | 48 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/main/java/blackjack/domain/rule/ScoreCalculator.java create mode 100644 src/test/java/blackjack/domain/rule/ScoreCalculatorTest.java diff --git a/src/main/java/blackjack/domain/rule/ScoreCalculator.java b/src/main/java/blackjack/domain/rule/ScoreCalculator.java new file mode 100644 index 0000000000..d2df701d09 --- /dev/null +++ b/src/main/java/blackjack/domain/rule/ScoreCalculator.java @@ -0,0 +1,19 @@ +package blackjack.domain.rule; + +import blackjack.domain.player.Hand; + +public class ScoreCalculator { + + private static final int BLACK_JACK = 21; + private static final int ACE_WEIGHT = 10; + + public Score calculate(Hand hand) { + int aceCount = hand.countAce(); + int sum = hand.sum(); + while (aceCount > 0 && (sum + ACE_WEIGHT) <= BLACK_JACK) { + sum += ACE_WEIGHT; + aceCount--; + } + return new Score(sum); + } +} diff --git a/src/test/java/blackjack/domain/rule/ScoreCalculatorTest.java b/src/test/java/blackjack/domain/rule/ScoreCalculatorTest.java new file mode 100644 index 0000000000..bbdf5ec036 --- /dev/null +++ b/src/test/java/blackjack/domain/rule/ScoreCalculatorTest.java @@ -0,0 +1,48 @@ +package blackjack.domain.rule; + +import blackjack.domain.player.Hand; +import fixture.HandFixture; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("점수 계산 전략 테스트") +class ScoreCalculatorTest { + + private ScoreCalculator scoreCalculator; + + @BeforeEach + void setUp() { + scoreCalculator = new ScoreCalculator(); + } + + @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 없음") + @Test + void testCalculateScoreWithNoAce() { + Hand hand = HandFixture.of(2, 3, 4, 5); + assertThat(scoreCalculator.calculate(hand).getValue()).isEqualTo(14); + } + + @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 11로 이용됨") + @Test + void testCalculateScoreWithBigAce() { + Hand hand = HandFixture.of(1, 10); + assertThat(scoreCalculator.calculate(hand).getValue()).isEqualTo(21); + } + + @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 1로 이용됨") + @Test + void testCalculateScoreWithLowAce() { + Hand hand = HandFixture.of(1, 10, 2); + assertThat(scoreCalculator.calculate(hand).getValue()).isEqualTo(13); + } + + @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 두개 이상") + @Test + void testCalculateScoreWithMultipleAce() { + Hand hand = HandFixture.of(1, 1, 1); + assertThat(scoreCalculator.calculate(hand).getValue()).isEqualTo(13); + } +} \ No newline at end of file From 4127be150f0a9d9a7106b38161ea432fd13bb5cf Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 14:33:25 +0900 Subject: [PATCH 053/124] =?UTF-8?q?style:=20ScoreCalculator=20->=20ScoreCa?= =?UTF-8?q?lculateStrategy=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Calculator.java => ScoreCalculateStrategy.java} | 2 +- ...orTest.java => ScoreCalculateStrategyTest.java} | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) rename src/main/java/blackjack/domain/rule/{ScoreCalculator.java => ScoreCalculateStrategy.java} (92%) rename src/test/java/blackjack/domain/rule/{ScoreCalculatorTest.java => ScoreCalculateStrategyTest.java} (69%) diff --git a/src/main/java/blackjack/domain/rule/ScoreCalculator.java b/src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java similarity index 92% rename from src/main/java/blackjack/domain/rule/ScoreCalculator.java rename to src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java index d2df701d09..c83a917ead 100644 --- a/src/main/java/blackjack/domain/rule/ScoreCalculator.java +++ b/src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java @@ -2,7 +2,7 @@ import blackjack.domain.player.Hand; -public class ScoreCalculator { +public class ScoreCalculateStrategy { private static final int BLACK_JACK = 21; private static final int ACE_WEIGHT = 10; diff --git a/src/test/java/blackjack/domain/rule/ScoreCalculatorTest.java b/src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java similarity index 69% rename from src/test/java/blackjack/domain/rule/ScoreCalculatorTest.java rename to src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java index bbdf5ec036..68e1489f5c 100644 --- a/src/test/java/blackjack/domain/rule/ScoreCalculatorTest.java +++ b/src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java @@ -9,40 +9,40 @@ import static org.assertj.core.api.Assertions.assertThat; @DisplayName("점수 계산 전략 테스트") -class ScoreCalculatorTest { +class ScoreCalculateStrategyTest { - private ScoreCalculator scoreCalculator; + private ScoreCalculateStrategy scoreCalculateStrategy; @BeforeEach void setUp() { - scoreCalculator = new ScoreCalculator(); + scoreCalculateStrategy = new ScoreCalculateStrategy(); } @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 없음") @Test void testCalculateScoreWithNoAce() { Hand hand = HandFixture.of(2, 3, 4, 5); - assertThat(scoreCalculator.calculate(hand).getValue()).isEqualTo(14); + assertThat(scoreCalculateStrategy.calculate(hand).getValue()).isEqualTo(14); } @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 11로 이용됨") @Test void testCalculateScoreWithBigAce() { Hand hand = HandFixture.of(1, 10); - assertThat(scoreCalculator.calculate(hand).getValue()).isEqualTo(21); + assertThat(scoreCalculateStrategy.calculate(hand).getValue()).isEqualTo(21); } @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 1로 이용됨") @Test void testCalculateScoreWithLowAce() { Hand hand = HandFixture.of(1, 10, 2); - assertThat(scoreCalculator.calculate(hand).getValue()).isEqualTo(13); + assertThat(scoreCalculateStrategy.calculate(hand).getValue()).isEqualTo(13); } @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 두개 이상") @Test void testCalculateScoreWithMultipleAce() { Hand hand = HandFixture.of(1, 1, 1); - assertThat(scoreCalculator.calculate(hand).getValue()).isEqualTo(13); + assertThat(scoreCalculateStrategy.calculate(hand).getValue()).isEqualTo(13); } } \ No newline at end of file From f45b82e9fefee11bdc74310400c0468aea9bc136 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 14:40:40 +0900 Subject: [PATCH 054/124] =?UTF-8?q?refactor:=20Player=EA=B0=80=20=EC=A0=84?= =?UTF-8?q?=EB=9E=B5=EC=9D=84=20=EC=A0=84=EB=8B=AC=EB=B0=9B=EC=95=84=20?= =?UTF-8?q?=EC=A0=90=EC=88=98=EB=A5=BC=20=EA=B3=84=EC=82=B0=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Player.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index 151b3b7802..702403ccae 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -1,6 +1,8 @@ package blackjack.domain.player; import blackjack.domain.card.Card; +import blackjack.domain.rule.Score; +import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.Objects; @@ -18,6 +20,10 @@ public void appendCard(Card card) { hand.append(card); } + public Score calculateHandScore(ScoreCalculateStrategy scoreCalculateStrategy) { + return scoreCalculateStrategy.calculate(hand); + } + @Override public boolean equals(Object o) { if (this == o) return true; From 59f6a93bf154e4f37441e8480e9c789b47282cf2 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 14:43:15 +0900 Subject: [PATCH 055/124] =?UTF-8?q?refactor:=20=ED=8F=AC=EC=9E=A5=ED=95=9C?= =?UTF-8?q?=20Score=EB=A5=BC=20=EC=A0=84=EB=8B=AC=EB=B0=9B=EC=95=84=20?= =?UTF-8?q?=EB=B7=B0=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/view/MessageResolver.java | 5 +++-- src/main/java/blackjack/view/OutputView.java | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index 6d244dfbe0..b1a9bc383c 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -5,6 +5,7 @@ import blackjack.domain.player.Hand; import blackjack.domain.player.Player; import blackjack.domain.player.Players; +import blackjack.domain.rule.Score; import java.util.stream.Collectors; @@ -42,9 +43,9 @@ public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount return String.format("딜러는 %d이하라 %d장의 카드를 더 받았습니다.", dealerDrawThreshold, popCount); } - public String resolvePlayerScoreMessage(Player player, int score) { + public String resolvePlayerScoreMessage(Player player, Score score) { String handMessage = resolvePlayerHandMessage(player); - return String.format("%s - 결과: %d", handMessage, score); + return String.format("%s - 결과: %d", handMessage, score.getValue()); } public String resolvePlayerGameResult(Player player, boolean win) { diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index dd88997c02..a11b77510d 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -3,6 +3,7 @@ import blackjack.domain.DealerGameResult; import blackjack.domain.player.Player; import blackjack.domain.player.Players; +import blackjack.domain.rule.Score; public class OutputView { @@ -24,7 +25,7 @@ public void printDealerPopCount(int dealerPopThreshold, int count) { System.out.println(messageResolver.resolveDealerPopCountMessage(dealerPopThreshold, count)); } - public void printPlayerScore(Player player, int score) { + public void printPlayerScore(Player player, Score score) { System.out.println(messageResolver.resolvePlayerScoreMessage(player, score)); } From b0ecfde9b0dc0b50b98e1914a8cf69b81d241ea9 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 14:46:41 +0900 Subject: [PATCH 056/124] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC=20=EA=B0=9C=EC=84=A0=20=EB=B0=8F=20?= =?UTF-8?q?=EC=A0=90=EC=88=98=20=EA=B3=84=EC=82=B0=20=EC=B1=85=EC=9E=84?= =?UTF-8?q?=EC=9D=84=20Judge=EC=97=90=EC=84=9C=20=EC=9D=B4=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index c3098c0099..b3fa158d87 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -11,6 +11,8 @@ import blackjack.domain.player.PlayerName; import blackjack.domain.player.Players; import blackjack.domain.rule.Judge; +import blackjack.domain.rule.Score; +import blackjack.domain.rule.ScoreCalculateStrategy; import blackjack.view.InputView; import blackjack.view.OutputView; @@ -55,9 +57,14 @@ public void run() { } private void printPlayersScore(Players players) { - Judge judge = new Judge(); - players.getPlayers() - .forEach(player -> outputView.printPlayerScore(player, judge.calculateBestScore(player.getHand()))); + players.getPlayers().forEach(this::printPlayerScore); + + } + + private void printPlayerScore(Player player) { + ScoreCalculateStrategy scoreCalculateStrategy = new ScoreCalculateStrategy(); + Score score = player.calculateHandScore(scoreCalculateStrategy); + outputView.printPlayerScore(player, score); } private void printPlayersGameResult(Players players, Dealer dealer) { From e5eb87d6e1f2ba6dd82957975a416aba13657141 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 14:48:58 +0900 Subject: [PATCH 057/124] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9D=B4=EC=A0=84=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/blackjack/domain/rule/JudgeTest.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/test/java/blackjack/domain/rule/JudgeTest.java b/src/test/java/blackjack/domain/rule/JudgeTest.java index 84c1cb2ebb..06e9f21182 100644 --- a/src/test/java/blackjack/domain/rule/JudgeTest.java +++ b/src/test/java/blackjack/domain/rule/JudgeTest.java @@ -25,12 +25,6 @@ void setUp() { judge = new Judge(); } - @DisplayName("핸드가 건네지면 가장 최선의 합계를 구할 수 있다") - @Test - void testCalculateBestScore() { - Hand hand = HandFixture.of(2, 8, 1); - assertThat(judge.calculateBestScore(hand)).isEqualTo(21); - } @DisplayName("버스트 된 핸드를 판별할 수 있다") @Test From b8e2c049926e861526f1b40b1f983768cdb22d09 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 15:17:30 +0900 Subject: [PATCH 058/124] =?UTF-8?q?refactor:=20=ED=9E=88=ED=8A=B8=20?= =?UTF-8?q?=EA=B0=80=EB=8A=A5=20=EC=B1=85=EC=9E=84=EC=9D=84=20Judge?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EC=9D=B4=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/rule/DealerHitStrategy.java | 11 ++++++ .../blackjack/domain/rule/HitStrategy.java | 6 ++++ .../domain/rule/PlayerHitStrategy.java | 11 ++++++ .../java/blackjack/domain/rule/Score.java | 4 +++ .../domain/rule/DealerHitStrategyTest.java | 34 +++++++++++++++++++ .../domain/rule/PlayerHitStrategyTest.java | 34 +++++++++++++++++++ 6 files changed, 100 insertions(+) create mode 100644 src/main/java/blackjack/domain/rule/DealerHitStrategy.java create mode 100644 src/main/java/blackjack/domain/rule/HitStrategy.java create mode 100644 src/main/java/blackjack/domain/rule/PlayerHitStrategy.java create mode 100644 src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java create mode 100644 src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java diff --git a/src/main/java/blackjack/domain/rule/DealerHitStrategy.java b/src/main/java/blackjack/domain/rule/DealerHitStrategy.java new file mode 100644 index 0000000000..1132b5c510 --- /dev/null +++ b/src/main/java/blackjack/domain/rule/DealerHitStrategy.java @@ -0,0 +1,11 @@ +package blackjack.domain.rule; + +public class DealerHitStrategy implements HitStrategy { + + private static final int HIT_THRESHOLD = 17; + + @Override + public boolean canHit(Score score) { + return score.getValue() < HIT_THRESHOLD; + } +} diff --git a/src/main/java/blackjack/domain/rule/HitStrategy.java b/src/main/java/blackjack/domain/rule/HitStrategy.java new file mode 100644 index 0000000000..3316fab0b5 --- /dev/null +++ b/src/main/java/blackjack/domain/rule/HitStrategy.java @@ -0,0 +1,6 @@ +package blackjack.domain.rule; + +public interface HitStrategy { + + public boolean canHit(Score score); +} diff --git a/src/main/java/blackjack/domain/rule/PlayerHitStrategy.java b/src/main/java/blackjack/domain/rule/PlayerHitStrategy.java new file mode 100644 index 0000000000..bc334834aa --- /dev/null +++ b/src/main/java/blackjack/domain/rule/PlayerHitStrategy.java @@ -0,0 +1,11 @@ +package blackjack.domain.rule; + +public class PlayerHitStrategy implements HitStrategy { + + public static final int HIT_THRESHOLD = 21; + + @Override + public boolean canHit(Score score) { + return score.getValue() <= HIT_THRESHOLD; + } +} diff --git a/src/main/java/blackjack/domain/rule/Score.java b/src/main/java/blackjack/domain/rule/Score.java index a86db251ae..c8db32aa99 100644 --- a/src/main/java/blackjack/domain/rule/Score.java +++ b/src/main/java/blackjack/domain/rule/Score.java @@ -8,6 +8,10 @@ public Score(int value) { this.value = value; } + public boolean hitAllowed(HitStrategy hitStrategy) { + return hitStrategy.canHit(this); + } + public int getValue() { return value; } diff --git a/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java b/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java new file mode 100644 index 0000000000..8ac358c9b1 --- /dev/null +++ b/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java @@ -0,0 +1,34 @@ +package blackjack.domain.rule; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; + +class DealerHitStrategyTest { + + private DealerHitStrategy dealerHitStrategy; + + @BeforeEach + void setUp() { + dealerHitStrategy = new DealerHitStrategy(); + } + + @DisplayName("딜러는 17점 미만이면 카드를 받아야 한다") + @ParameterizedTest + @ValueSource(ints = {13, 14, 15, 16}) + void testDealerShouldHit(int score) { + boolean canHit = dealerHitStrategy.canHit(new Score(score)); + assertThat(canHit).isTrue(); + } + + @DisplayName("딜러는 17점 이상이면 카드를 받을 수 없다") + @ParameterizedTest + @ValueSource(ints = {17, 18, 19, 20}) + void testDealerShouldStay(int score) { + boolean canHit = dealerHitStrategy.canHit(new Score(score)); + assertThat(canHit).isFalse(); + } +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java b/src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java new file mode 100644 index 0000000000..16927ab222 --- /dev/null +++ b/src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java @@ -0,0 +1,34 @@ +package blackjack.domain.rule; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; + +class PlayerHitStrategyTest { + + private PlayerHitStrategy playerHitStrategy; + + @BeforeEach + void setUp() { + playerHitStrategy = new PlayerHitStrategy(); + } + + @DisplayName("플레이어는 21점 이하면 카드를 받을 수 있다") + @ParameterizedTest + @ValueSource(ints = {20, 21}) + void testDealerShouldHit(int score) { + boolean canHit = playerHitStrategy.canHit(new Score(score)); + assertThat(canHit).isTrue(); + } + + @DisplayName("플레이어는 21점을 초과하면 카드를 받을 수 없다") + @ParameterizedTest + @ValueSource(ints = {22, 23}) + void testDealerShouldStay(int score) { + boolean canHit = playerHitStrategy.canHit(new Score(score)); + assertThat(canHit).isFalse(); + } +} \ No newline at end of file From 3fbdbcc14aa7fc181e9b2b3925e509bef2b652fd Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 15:17:52 +0900 Subject: [PATCH 059/124] =?UTF-8?q?test:=20=EC=B1=85=EC=9E=84=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=EB=A1=9C=20=EC=9D=B8=ED=95=B4=20=EC=9C=A0=EC=9D=98?= =?UTF-8?q?=EB=AF=B8=ED=95=98=EC=A7=80=20=EC=95=8A=EC=9D=80=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/rule/JudgeTest.java | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/src/test/java/blackjack/domain/rule/JudgeTest.java b/src/test/java/blackjack/domain/rule/JudgeTest.java index 06e9f21182..ce0cbf4ceb 100644 --- a/src/test/java/blackjack/domain/rule/JudgeTest.java +++ b/src/test/java/blackjack/domain/rule/JudgeTest.java @@ -40,26 +40,6 @@ void testDecideNotBusted() { assertThat(judge.isBustedHand(hand)).isFalse(); } - @DisplayName("딜러는 17점 미만이면 카드를 받아야 한다") - @Test - void testDealerShouldHit() { - Hand hand = HandFixture.of(10, 6); - Dealer dealer = new Dealer(hand); - boolean hit = judge.canDealerHit(dealer); - - assertThat(hit).isTrue(); - } - - @DisplayName("딜러는 17점 이상이면 카드를 받을 수 없다") - @Test - void testDealerShouldStay() { - Hand hand = HandFixture.of(10, 7); - Dealer dealer = new Dealer(hand); - boolean hit = judge.canDealerHit(dealer); - - assertThat(hit).isFalse(); - } - @DisplayName("딜러와 플레이어 중 누가 이겼는지 알 수 있다") @Test void testSelectWinner() { From 7e748c6b0ec7557711728eb8c3760ddc176901ef Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 15:33:58 +0900 Subject: [PATCH 060/124] =?UTF-8?q?refactor:=20=ED=9E=88=ED=8A=B8=20?= =?UTF-8?q?=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8A=94=EC=A7=80=20=EC=97=AC?= =?UTF-8?q?=EB=B6=80=EB=A5=BC=20=EA=B2=B0=EC=A0=95=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EC=A0=84=EB=9E=B5=20Judge=EB=A1=9C=EB=B6=80=ED=84=B0=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 38 ++++++++++--------- .../java/blackjack/domain/rule/Judge.java | 9 +---- .../java/blackjack/domain/rule/JudgeTest.java | 24 +----------- 3 files changed, 24 insertions(+), 47 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index b3fa158d87..c8f1d29526 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -1,6 +1,5 @@ package blackjack.controller; -import blackjack.domain.Dealer; import blackjack.domain.DealerGameResult; import blackjack.domain.DrawDecision; import blackjack.domain.card.Card; @@ -10,9 +9,7 @@ import blackjack.domain.player.Player; import blackjack.domain.player.PlayerName; import blackjack.domain.player.Players; -import blackjack.domain.rule.Judge; -import blackjack.domain.rule.Score; -import blackjack.domain.rule.ScoreCalculateStrategy; +import blackjack.domain.rule.*; import blackjack.view.InputView; import blackjack.view.OutputView; @@ -32,16 +29,15 @@ public BlackJackController(InputView inputView, OutputView outputView) { public void run() { Players players = initPlayers(); CardDeck cardDeck = initCardDeck(); - Dealer dealer = initDealer(); + Player dealer = initDealer(); issueInitialCardsToPlayers(players, cardDeck); - issueInitialCardsToDealer(cardDeck, dealer); + issueInitialCardsToDealer(dealer, cardDeck); outputView.printHandOutEvent(players, 2); players.getPlayers().forEach(outputView::printPlayerHand); //딜러 카드 보여주는 메서드 만들어야 함 (카드 한장만) - Player tempDealer = new Player(new PlayerName("딜러"), dealer.getHand()); - outputView.printPlayerHand(tempDealer); + outputView.printPlayerHand(dealer); completePlayersHand(players, cardDeck); completeDealerHand(dealer, cardDeck); @@ -62,31 +58,37 @@ private void printPlayersScore(Players players) { } private void printPlayerScore(Player player) { - ScoreCalculateStrategy scoreCalculateStrategy = new ScoreCalculateStrategy(); - Score score = player.calculateHandScore(scoreCalculateStrategy); + Score score = calculateScore(player); outputView.printPlayerScore(player, score); } - private void printPlayersGameResult(Players players, Dealer dealer) { + private void printPlayersGameResult(Players players, Player dealer) { Judge judge = new Judge(); players.getPlayers() .forEach(player -> outputView.printPlayerGameResult(player, judge.isPlayerWin(dealer, player))); } - private void printDealerGameResult(Dealer dealer, Players players) { + private void printDealerGameResult(Player dealer, Players players) { Judge judge = new Judge(); DealerGameResult dealerGameResult = judge.calculateDealerResult(dealer, players); outputView.printDealerGameResult(dealerGameResult); } - private void completeDealerHand(Dealer dealer, CardDeck cardDeck) { - Judge judge = new Judge(); - while (judge.canDealerHit(dealer)) { + private void completeDealerHand(Player dealer, CardDeck cardDeck) { + Score dealerScore = calculateScore(dealer); + HitStrategy hitStrategy = new DealerHitStrategy(); + + while (dealerScore.hitAllowed(hitStrategy)) { Card card = cardDeck.popCard(); dealer.appendCard(card); } } + private Score calculateScore(Player player) { + ScoreCalculateStrategy scoreCalculateStrategy = new ScoreCalculateStrategy(); + return player.calculateHandScore(scoreCalculateStrategy); + } + private void completePlayersHand(Players players, CardDeck cardDeck) { for (Player player : players.getPlayers()) { completePlayerHand(player, cardDeck); @@ -121,9 +123,9 @@ private Players initPlayers() { return Players.from(playerNames); } - private Dealer initDealer() { + private Player initDealer() { Hand hand = new Hand(new ArrayList<>()); - return new Dealer(hand); + return new Player(new PlayerName("딜러"), hand); } private List issueInitialHand(CardDeck cardDeck) { @@ -140,7 +142,7 @@ private void issueInitialCardsToPlayers(Players players, CardDeck cardDeck) { .forEach(hand -> addIssuedCardToHand(hand, issueInitialHand(cardDeck))); } - private void issueInitialCardsToDealer(CardDeck cardDeck, Dealer dealer) { + private void issueInitialCardsToDealer(Player dealer, CardDeck cardDeck) { List cards = issueInitialHand(cardDeck); addIssuedCardToHand(dealer.getHand(), cards); } diff --git a/src/main/java/blackjack/domain/rule/Judge.java b/src/main/java/blackjack/domain/rule/Judge.java index c30103698b..e4e1268d5d 100644 --- a/src/main/java/blackjack/domain/rule/Judge.java +++ b/src/main/java/blackjack/domain/rule/Judge.java @@ -1,6 +1,5 @@ package blackjack.domain.rule; -import blackjack.domain.Dealer; import blackjack.domain.DealerGameResult; import blackjack.domain.player.Hand; import blackjack.domain.player.Player; @@ -26,11 +25,7 @@ public boolean isBustedHand(Hand hand) { return BLACK_JACK < calculateBestScore(hand); } - public boolean canDealerHit(Dealer dealer) { - return calculateBestScore(dealer.getHand()) < DEALER_HIT_THRESHOLD; - } - - public boolean isPlayerWin(Dealer dealer, Player player) { + public boolean isPlayerWin(Player dealer, Player player) { if (isBustedHand(player.getHand())) { return false; } @@ -40,7 +35,7 @@ public boolean isPlayerWin(Dealer dealer, Player player) { return calculateBestScore(player.getHand()) > calculateBestScore(dealer.getHand()); } - public DealerGameResult calculateDealerResult(Dealer dealer, Players players) { + public DealerGameResult calculateDealerResult(Player dealer, Players players) { int dealerLoseCount = (int) players.getPlayers().stream() .filter(player -> isPlayerWin(dealer, player)) .count(); diff --git a/src/test/java/blackjack/domain/rule/JudgeTest.java b/src/test/java/blackjack/domain/rule/JudgeTest.java index ce0cbf4ceb..caeb4c7830 100644 --- a/src/test/java/blackjack/domain/rule/JudgeTest.java +++ b/src/test/java/blackjack/domain/rule/JudgeTest.java @@ -1,11 +1,8 @@ package blackjack.domain.rule; -import blackjack.domain.Dealer; import blackjack.domain.DealerGameResult; -import blackjack.domain.player.Hand; import blackjack.domain.player.Player; import blackjack.domain.player.Players; -import fixture.HandFixture; import fixture.PlayerFixture; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -25,26 +22,10 @@ void setUp() { judge = new Judge(); } - - @DisplayName("버스트 된 핸드를 판별할 수 있다") - @Test - void testDecideBusted() { - Hand hand = HandFixture.of(10, 9, 3); - assertThat(judge.isBustedHand(hand)).isTrue(); - } - - @DisplayName("버스트 되지 않은 핸드를 판별할 수 있다") - @Test - void testDecideNotBusted() { - Hand hand = HandFixture.of(10, 9, 2); - assertThat(judge.isBustedHand(hand)).isFalse(); - } - @DisplayName("딜러와 플레이어 중 누가 이겼는지 알 수 있다") @Test void testSelectWinner() { - Hand hand = HandFixture.of(10, 7); - Dealer dealer = new Dealer(hand); + Player dealer = PlayerFixture.of("딜러", 10, 7); Player player = PlayerFixture.of("pobi", 10, 6); assertThat(judge.isPlayerWin(dealer, player)).isFalse(); @@ -53,8 +34,7 @@ void testSelectWinner() { @DisplayName("딜러의 게임 결과를 계산할 수 있다") @Test void testDealerResult() { - Hand hand = HandFixture.of(3, 9, 8); - Dealer dealer = new Dealer(hand); + Player dealer = PlayerFixture.of("딜러", 3, 9, 8); Player player1 = PlayerFixture.of("pobi", 2, 8, 1); Player player2 = PlayerFixture.of("jason", 7, 10); From db772d4708b1414506de5391050a569b41d159eb Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 15:58:04 +0900 Subject: [PATCH 061/124] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=EA=B0=80=20?= =?UTF-8?q?=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=EB=B3=B4=EB=8B=A4=20?= =?UTF-8?q?=EB=86=92=EC=9D=80=EC=A7=80=20=ED=99=95=EC=9D=B8=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/rule/Score.java | 4 +++ .../java/blackjack/domain/rule/ScoreTest.java | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/test/java/blackjack/domain/rule/ScoreTest.java diff --git a/src/main/java/blackjack/domain/rule/Score.java b/src/main/java/blackjack/domain/rule/Score.java index c8db32aa99..154062cb8a 100644 --- a/src/main/java/blackjack/domain/rule/Score.java +++ b/src/main/java/blackjack/domain/rule/Score.java @@ -12,6 +12,10 @@ public boolean hitAllowed(HitStrategy hitStrategy) { return hitStrategy.canHit(this); } + public boolean isAbove(int bustCondition) { + return bustCondition < value; + } + public int getValue() { return value; } diff --git a/src/test/java/blackjack/domain/rule/ScoreTest.java b/src/test/java/blackjack/domain/rule/ScoreTest.java new file mode 100644 index 0000000000..74acca14d6 --- /dev/null +++ b/src/test/java/blackjack/domain/rule/ScoreTest.java @@ -0,0 +1,28 @@ +package blackjack.domain.rule; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("점수 테스트") +class ScoreTest { + + + @DisplayName("점수가 파라미터를 넘는지 확인할 수 있다") + @ParameterizedTest + @ValueSource(ints = {1, 2, 3, 4, 5}) + void testScoreAbove(int value) { + Score score = new Score(6); + assertThat(score.isAbove(value)).isTrue(); + } + + @DisplayName("점수가 파라미터를 넘지 않는지 확인할 수 있다") + @ParameterizedTest + @ValueSource(ints = {1, 2, 3, 4, 5}) + void testScoreBelow(int value) { + Score score = new Score(0); + assertThat(score.isAbove(value)).isFalse(); + } +} \ No newline at end of file From 7ebe119df781b4ecadb6820f6d591e978a60fdf4 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 16:06:27 +0900 Subject: [PATCH 062/124] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=EA=B0=84=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/rule/Score.java | 4 ++++ .../java/blackjack/domain/rule/ScoreTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/blackjack/domain/rule/Score.java b/src/main/java/blackjack/domain/rule/Score.java index 154062cb8a..3f17f79d26 100644 --- a/src/main/java/blackjack/domain/rule/Score.java +++ b/src/main/java/blackjack/domain/rule/Score.java @@ -16,6 +16,10 @@ public boolean isAbove(int bustCondition) { return bustCondition < value; } + public boolean isBiggerThan(Score target) { + return this.value > target.value; + } + public int getValue() { return value; } diff --git a/src/test/java/blackjack/domain/rule/ScoreTest.java b/src/test/java/blackjack/domain/rule/ScoreTest.java index 74acca14d6..fbdfc86aa2 100644 --- a/src/test/java/blackjack/domain/rule/ScoreTest.java +++ b/src/test/java/blackjack/domain/rule/ScoreTest.java @@ -25,4 +25,22 @@ void testScoreBelow(int value) { Score score = new Score(0); assertThat(score.isAbove(value)).isFalse(); } + + @DisplayName("더 낮은 점수와 비교할 수 있다") + @ParameterizedTest + @ValueSource(ints = {1, 2, 3, 4, 5}) + void testScoreCompareWithBigger(int value) { + Score score = new Score(0); + Score target = new Score(value); + assertThat(score.isBiggerThan(target)).isFalse(); + } + + @DisplayName("점수가 파라미터를 넘지 않는지 확인할 수 있다") + @ParameterizedTest + @ValueSource(ints = {1, 2, 3, 4, 5}) + void testScoreCompareWithLower(int value) { + Score score = new Score(6); + Score target = new Score(value); + assertThat(score.isBiggerThan(target)).isTrue(); + } } \ No newline at end of file From 087b13378db8d972e379bd49c60b5a798cec8ce7 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 16:12:07 +0900 Subject: [PATCH 063/124] =?UTF-8?q?refactor:=20=EC=A0=90=EC=88=98=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90=20=EC=8A=B9=ED=8C=A8=20=ED=8C=90=EC=A0=95=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EB=8F=84=EB=A9=94=EC=9D=B8=EC=97=90?= =?UTF-8?q?=EA=B2=8C=20=EB=AC=BC=EC=96=B4=EB=B3=B4=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 24 +++++++++++++------ .../java/blackjack/domain/rule/Judge.java | 10 ++++++++ .../java/blackjack/domain/rule/JudgeTest.java | 7 +++--- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index c8f1d29526..00f3814d0b 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -64,8 +64,12 @@ private void printPlayerScore(Player player) { private void printPlayersGameResult(Players players, Player dealer) { Judge judge = new Judge(); + Score dealerScore = calculateScore(dealer); players.getPlayers() - .forEach(player -> outputView.printPlayerGameResult(player, judge.isPlayerWin(dealer, player))); + .forEach(player -> { + Score playerScore = calculateScore(player); + outputView.printPlayerGameResult(player, judge.isPlayerWin(dealerScore, playerScore)); + }); } private void printDealerGameResult(Player dealer, Players players) { @@ -96,22 +100,28 @@ private void completePlayersHand(Players players, CardDeck cardDeck) { } private void completePlayerHand(Player player, CardDeck cardDeck) { - Judge judge = new Judge(); - while (!judge.isBustedHand(player.getHand())) { - String name = player.getName(); - InputMapper inputMapper = new InputMapper(); - DrawDecision drawDecision = inputMapper.mapToDrawDecision(inputView.readDrawPlan(name)); + Score playerScore = calculateScore(player); + HitStrategy hitStrategy = new PlayerHitStrategy(); + while (playerScore.hitAllowed(hitStrategy)) { + DrawDecision drawDecision = readHitDecision(player); if (drawDecision == DrawDecision.NO) { break; } - Card card = cardDeck.popCard(); player.appendCard(card); outputView.printPlayerHand(player); + playerScore = calculateScore(player); } } + private DrawDecision readHitDecision(Player player) { + String name = player.getName(); + InputMapper inputMapper = new InputMapper(); + DrawDecision hitDecision = inputMapper.mapToDrawDecision(inputView.readDrawPlan(name)); + return hitDecision; + } + private CardDeck initCardDeck() { CardDeckCreator cardDeckCreator = new CardDeckCreator(); return cardDeckCreator.create(); diff --git a/src/main/java/blackjack/domain/rule/Judge.java b/src/main/java/blackjack/domain/rule/Judge.java index e4e1268d5d..a6ddc87896 100644 --- a/src/main/java/blackjack/domain/rule/Judge.java +++ b/src/main/java/blackjack/domain/rule/Judge.java @@ -35,6 +35,16 @@ public boolean isPlayerWin(Player dealer, Player player) { return calculateBestScore(player.getHand()) > calculateBestScore(dealer.getHand()); } + public boolean isPlayerWin(Score dealerScore, Score playerScore) { + if (dealerScore.isAbove(BLACK_JACK)) { + return false; + } + if (playerScore.isAbove(BLACK_JACK)) { + return true; + } + return playerScore.isBiggerThan(dealerScore); + } + public DealerGameResult calculateDealerResult(Player dealer, Players players) { int dealerLoseCount = (int) players.getPlayers().stream() .filter(player -> isPlayerWin(dealer, player)) diff --git a/src/test/java/blackjack/domain/rule/JudgeTest.java b/src/test/java/blackjack/domain/rule/JudgeTest.java index caeb4c7830..5e21ea5c05 100644 --- a/src/test/java/blackjack/domain/rule/JudgeTest.java +++ b/src/test/java/blackjack/domain/rule/JudgeTest.java @@ -25,10 +25,9 @@ void setUp() { @DisplayName("딜러와 플레이어 중 누가 이겼는지 알 수 있다") @Test void testSelectWinner() { - Player dealer = PlayerFixture.of("딜러", 10, 7); - Player player = PlayerFixture.of("pobi", 10, 6); - - assertThat(judge.isPlayerWin(dealer, player)).isFalse(); + Score dealerScore = new Score(10); + Score playerScore = new Score(5); + assertThat(judge.isPlayerWin(dealerScore, playerScore)).isFalse(); } @DisplayName("딜러의 게임 결과를 계산할 수 있다") From e68b09acd7998f415cc8491ebdd13603534432cc Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 16:16:22 +0900 Subject: [PATCH 064/124] =?UTF-8?q?fix:=20=EC=8A=B9=ED=8C=A8=20=ED=8C=90?= =?UTF-8?q?=EC=A0=95,=20=EC=B9=B4=EB=93=9C=20=EB=B6=84=EB=B0=B0=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/controller/BlackJackController.java | 1 + src/main/java/blackjack/domain/rule/Judge.java | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 00f3814d0b..9c5f229882 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -85,6 +85,7 @@ private void completeDealerHand(Player dealer, CardDeck cardDeck) { while (dealerScore.hitAllowed(hitStrategy)) { Card card = cardDeck.popCard(); dealer.appendCard(card); + dealerScore = calculateScore(dealer); } } diff --git a/src/main/java/blackjack/domain/rule/Judge.java b/src/main/java/blackjack/domain/rule/Judge.java index a6ddc87896..606a70bbfe 100644 --- a/src/main/java/blackjack/domain/rule/Judge.java +++ b/src/main/java/blackjack/domain/rule/Judge.java @@ -36,10 +36,10 @@ public boolean isPlayerWin(Player dealer, Player player) { } public boolean isPlayerWin(Score dealerScore, Score playerScore) { - if (dealerScore.isAbove(BLACK_JACK)) { + if (playerScore.isAbove(BLACK_JACK)) { return false; } - if (playerScore.isAbove(BLACK_JACK)) { + if (dealerScore.isAbove(BLACK_JACK)) { return true; } return playerScore.isBiggerThan(dealerScore); From fcf9aa7166d0c6b9f1097e4e72d655524b8d334f Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 16:26:43 +0900 Subject: [PATCH 065/124] =?UTF-8?q?refactor:=20Judge=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=EB=93=A4=20=EC=B1=85=EC=9E=84=20=EB=B6=84=EB=A6=AC=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 11 +++++- .../java/blackjack/domain/rule/Judge.java | 38 ------------------- .../java/blackjack/domain/rule/JudgeTest.java | 24 ------------ 3 files changed, 10 insertions(+), 63 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 9c5f229882..33e6784203 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -73,8 +73,17 @@ private void printPlayersGameResult(Players players, Player dealer) { } private void printDealerGameResult(Player dealer, Players players) { + Score dealerScore = calculateScore(dealer); Judge judge = new Judge(); - DealerGameResult dealerGameResult = judge.calculateDealerResult(dealer, players); + + int playerWinCount = (int) players.getPlayers().stream() + .map(this::calculateScore) + .filter(playerScore -> judge.isPlayerWin(dealerScore, playerScore)) + .count(); + + int dealerWinCount = players.countPlayer() - playerWinCount; + + DealerGameResult dealerGameResult = new DealerGameResult(dealerWinCount, playerWinCount); outputView.printDealerGameResult(dealerGameResult); } diff --git a/src/main/java/blackjack/domain/rule/Judge.java b/src/main/java/blackjack/domain/rule/Judge.java index 606a70bbfe..55d54d838a 100644 --- a/src/main/java/blackjack/domain/rule/Judge.java +++ b/src/main/java/blackjack/domain/rule/Judge.java @@ -1,39 +1,8 @@ package blackjack.domain.rule; -import blackjack.domain.DealerGameResult; -import blackjack.domain.player.Hand; -import blackjack.domain.player.Player; -import blackjack.domain.player.Players; - public class Judge { private static final int BLACK_JACK = 21; - private static final int ACE_WEIGHT = 10; - private static final int DEALER_HIT_THRESHOLD = 17; - - public int calculateBestScore(Hand hand) { - int aceCount = hand.countAce(); - int sum = hand.sum(); - while (aceCount > 0 && (sum + ACE_WEIGHT) <= BLACK_JACK) { - sum += ACE_WEIGHT; - aceCount--; - } - return sum; - } - - public boolean isBustedHand(Hand hand) { - return BLACK_JACK < calculateBestScore(hand); - } - - public boolean isPlayerWin(Player dealer, Player player) { - if (isBustedHand(player.getHand())) { - return false; - } - if (isBustedHand(dealer.getHand())) { - return true; - } - return calculateBestScore(player.getHand()) > calculateBestScore(dealer.getHand()); - } public boolean isPlayerWin(Score dealerScore, Score playerScore) { if (playerScore.isAbove(BLACK_JACK)) { @@ -44,11 +13,4 @@ public boolean isPlayerWin(Score dealerScore, Score playerScore) { } return playerScore.isBiggerThan(dealerScore); } - - public DealerGameResult calculateDealerResult(Player dealer, Players players) { - int dealerLoseCount = (int) players.getPlayers().stream() - .filter(player -> isPlayerWin(dealer, player)) - .count(); - return new DealerGameResult(players.countPlayer() - dealerLoseCount, dealerLoseCount); - } } diff --git a/src/test/java/blackjack/domain/rule/JudgeTest.java b/src/test/java/blackjack/domain/rule/JudgeTest.java index 5e21ea5c05..a0dd0442e2 100644 --- a/src/test/java/blackjack/domain/rule/JudgeTest.java +++ b/src/test/java/blackjack/domain/rule/JudgeTest.java @@ -1,17 +1,10 @@ package blackjack.domain.rule; -import blackjack.domain.DealerGameResult; -import blackjack.domain.player.Player; -import blackjack.domain.player.Players; -import fixture.PlayerFixture; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertAll; class JudgeTest { @@ -29,21 +22,4 @@ void testSelectWinner() { Score playerScore = new Score(5); assertThat(judge.isPlayerWin(dealerScore, playerScore)).isFalse(); } - - @DisplayName("딜러의 게임 결과를 계산할 수 있다") - @Test - void testDealerResult() { - Player dealer = PlayerFixture.of("딜러", 3, 9, 8); - - Player player1 = PlayerFixture.of("pobi", 2, 8, 1); - Player player2 = PlayerFixture.of("jason", 7, 10); - Players players = new Players(List.of(player1, player2)); - - DealerGameResult dealerGameResult = judge.calculateDealerResult(dealer, players); - - assertAll( - () -> assertThat(dealerGameResult.getWinCount()).isEqualTo(1), - () -> assertThat(dealerGameResult.getLoseCount()).isEqualTo(1) - ); - } } From a1b8d4bbbd27461100733b76a9756d77939a424c Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 16:35:30 +0900 Subject: [PATCH 066/124] =?UTF-8?q?fix:=20=EB=94=9C=EB=9F=AC=20=EC=B4=88?= =?UTF-8?q?=EA=B8=B0=20=ED=95=B8=EB=93=9C=EB=A5=BC=20=ED=95=9C=EC=9E=A5?= =?UTF-8?q?=EB=A7=8C=20=EB=85=B8=EC=B6=9C=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B7=B0=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/controller/BlackJackController.java | 4 +--- src/main/java/blackjack/view/MessageResolver.java | 5 +++++ src/main/java/blackjack/view/OutputView.java | 4 ++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 33e6784203..8d58d37742 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -34,11 +34,9 @@ public void run() { issueInitialCardsToPlayers(players, cardDeck); issueInitialCardsToDealer(dealer, cardDeck); outputView.printHandOutEvent(players, 2); + outputView.printDealerInitialHand(dealer); players.getPlayers().forEach(outputView::printPlayerHand); - //딜러 카드 보여주는 메서드 만들어야 함 (카드 한장만) - outputView.printPlayerHand(dealer); - completePlayersHand(players, cardDeck); completeDealerHand(dealer, cardDeck); diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index b1a9bc383c..f398b73fd2 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -39,6 +39,11 @@ private String resolveCardMessage(Card card) { return String.format("%s%s", card.getCardNumberName(), card.getCardShape()); } + public String resolveDealerHandMessage(Player dealer) { + Card card = dealer.getHand().getCards().get(0); + return String.format("딜러: %s", resolveCardMessage(card)); + } + public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount) { return String.format("딜러는 %d이하라 %d장의 카드를 더 받았습니다.", dealerDrawThreshold, popCount); } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index a11b77510d..808813ee84 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -36,4 +36,8 @@ public void printPlayerGameResult(Player player, boolean win) { public void printDealerGameResult(DealerGameResult dealerGameResult) { System.out.println(messageResolver.resolveDealerGameResult(dealerGameResult)); } + + public void printDealerInitialHand(Player dealer) { + System.out.println(messageResolver.resolveDealerHandMessage(dealer)); + } } From 16de5b7df9cf989483288a298c51bc4b3e0c1c7d Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 16:39:46 +0900 Subject: [PATCH 067/124] =?UTF-8?q?refactor:=20=EB=88=84=EB=9D=BD=EB=90=9C?= =?UTF-8?q?=20=EB=94=9C=EB=9F=AC=20=EC=A0=84=EC=B2=B4=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=20=EC=B6=9C=EB=A0=A5=20=EB=A9=94=EC=84=B8=EC=A7=80=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/controller/BlackJackController.java | 4 ++-- src/main/java/blackjack/view/MessageResolver.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 8d58d37742..0cb0d0bf95 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -43,16 +43,16 @@ public void run() { int dealerPopCount = dealer.getHand().getCards().size() - 2; outputView.printDealerPopCount(16, dealerPopCount); + printPlayerScore(dealer); printPlayersScore(players); + printDealerGameResult(dealer, players); - printPlayersGameResult(players, dealer); } private void printPlayersScore(Players players) { players.getPlayers().forEach(this::printPlayerScore); - } private void printPlayerScore(Player player) { diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index f398b73fd2..cc511be602 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -41,7 +41,7 @@ private String resolveCardMessage(Card card) { public String resolveDealerHandMessage(Player dealer) { Card card = dealer.getHand().getCards().get(0); - return String.format("딜러: %s", resolveCardMessage(card)); + return String.format("%s: %s", dealer.getName(), resolveCardMessage(card)); } public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount) { From eaa623f9398ea376179620350f87335ef477f6a0 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 16:40:46 +0900 Subject: [PATCH 068/124] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/Dealer.java | 25 ----------------- .../java/blackjack/domain/DealerTest.java | 28 ------------------- 2 files changed, 53 deletions(-) delete mode 100644 src/main/java/blackjack/domain/Dealer.java delete mode 100644 src/test/java/blackjack/domain/DealerTest.java diff --git a/src/main/java/blackjack/domain/Dealer.java b/src/main/java/blackjack/domain/Dealer.java deleted file mode 100644 index a38e4efc22..0000000000 --- a/src/main/java/blackjack/domain/Dealer.java +++ /dev/null @@ -1,25 +0,0 @@ -package blackjack.domain; - -import blackjack.domain.card.Card; -import blackjack.domain.player.Hand; - -public class Dealer { - - private final Hand hand; - - public Dealer(Hand hand) { - this.hand = hand; - } - - public void appendCard(Card card) { - hand.append(card); - } - - public int calculateHandSum() { - return hand.sum(); - } - - public Hand getHand() { - return hand; - } -} diff --git a/src/test/java/blackjack/domain/DealerTest.java b/src/test/java/blackjack/domain/DealerTest.java deleted file mode 100644 index deb06b4eaf..0000000000 --- a/src/test/java/blackjack/domain/DealerTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package blackjack.domain; - -import blackjack.domain.card.Card; -import blackjack.domain.player.Hand; -import fixture.CardFixture; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - -@DisplayName("딜러 도메인 테스트") -class DealerTest { - - @DisplayName("딜러 핸드에 카드를 추가할 수 있다") - @Test - void testAppendCardToPlayer() { - List hand = new ArrayList<>(); - Dealer dealer = new Dealer(new Hand(hand)); - - Card card = CardFixture.from(2); - dealer.appendCard(card); - - assertThat(hand).containsExactly(card); - } -} \ No newline at end of file From 1b9d5ba50e142e8a89fdc281008de8e98e5799af Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 17:00:32 +0900 Subject: [PATCH 069/124] =?UTF-8?q?fix:=20=EA=B3=B5=EB=B0=B1=20=EB=B7=B0?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.java | 7 +++++-- src/main/java/blackjack/view/InputView.java | 2 +- src/main/java/blackjack/view/MessageResolver.java | 11 +++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 0cb0d0bf95..d3764ffda4 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -41,11 +41,14 @@ public void run() { completeDealerHand(dealer, cardDeck); int dealerPopCount = dealer.getHand().getCards().size() - 2; - outputView.printDealerPopCount(16, dealerPopCount); + + if (dealerPopCount > 0) { + outputView.printDealerPopCount(16, dealerPopCount); + } printPlayerScore(dealer); printPlayersScore(players); - + printDealerGameResult(dealer, players); printPlayersGameResult(players, dealer); diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index 9b9b82dd28..9025acdc90 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -14,7 +14,7 @@ public String readNames() { public String readDrawPlan(String name) { String message = String.format("%s는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)", name); - System.out.println(message); + System.out.println(String.join("", LINE_SEPARATOR, message)); return scanner.nextLine(); } } diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index cc511be602..a1197a6d79 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -16,7 +16,8 @@ public class MessageResolver { public String resolveHandOutEventMessage(Players players, int handOutCount) { String namesMessage = resolveNamesMessage(players); - return String.format("딜러와 %s에게 %d장을 나누었습니다.", namesMessage, handOutCount); + String message = String.format("딜러와 %s에게 %d장을 나누었습니다.", namesMessage, handOutCount); + return String.join("", LINE_SEPARATOR, message); } private String resolveNamesMessage(Players players) { @@ -45,7 +46,8 @@ public String resolveDealerHandMessage(Player dealer) { } public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount) { - return String.format("딜러는 %d이하라 %d장의 카드를 더 받았습니다.", dealerDrawThreshold, popCount); + String message = String.format("딜러는 %d이하라 %d장의 카드를 더 받았습니다.", dealerDrawThreshold, popCount); + return String.join("", LINE_SEPARATOR, message, LINE_SEPARATOR); } public String resolvePlayerScoreMessage(Player player, Score score) { @@ -65,7 +67,8 @@ private String resolveGameResultMessage(boolean win) { } public String resolveDealerGameResult(DealerGameResult dealerGameResult) { - return String.format("딜러: %d승 %d패", dealerGameResult.getWinCount(), dealerGameResult.getLoseCount()); + String prefix = String.join("", LINE_SEPARATOR, "## 최종 승패"); + String message = String.format("딜러: %d승 %d패", dealerGameResult.getWinCount(), dealerGameResult.getLoseCount()); + return String.join("", prefix, LINE_SEPARATOR, message); } - } From 02ee5335d5a04c4b8db34b42d10de0f2bcc0fca2 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 17:01:50 +0900 Subject: [PATCH 070/124] =?UTF-8?q?docs:=20=EC=B2=B4=ED=81=AC=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=B5=9C=EC=8B=A0=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e371ff49df..704f5d5b15 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ - [x] 한 장 더 받을지 여부를 입력 받을 수 있다. - [x] 입력 검증 - [x] 카드 추가 여부를 올바른 형태 (y/n)으로 입력했는지 검증할 수 있다. -- [ ] 도메인 +- [x] 도메인 - [x] 이름은 빈 문자열일 수 없다. - [x] 게임 참가자의 핸드에 새로운 카드를 추가할 수 있다. - [x] 이름이 중복되는 플레이어는 존재할 수 없다. @@ -29,12 +29,12 @@ - [x] 핸드의 합을 계산할 수 있다. - [x] 저지는 핸드에서 21에 가장 가까운 합을 구할 수 있다. - [x] 핸드의 최소합이 21을 초과하면 플레이어는 버스트한다. - - [ ] 핸드의 최소합이 21 이하면 플레이어는 카드를 뽑을지 여부를 선택할 수 있다. + - [x] 핸드의 최소합이 21 이하면 플레이어는 카드를 뽑을지 여부를 선택할 수 있다. - [x] 핸드에 카드를 추가할 수 있다. - [x] 카드덱에서 카드를 지정한 개수만큼 건네줄 수 있다. - [x] 카드덱에서 카드를 한 장 뽑아서 건네줄 수 있다. - [x] 카드덱에서 보유한 카드 개수보다 많이 뽑으면 예외가 발생한다. - - [ ] 참여자의 핸드에 초기 카드를 분배할 수 있다. + - [x] 참여자의 핸드에 초기 카드를 분배할 수 있다. - [x] 출력 - [x] 각 참여자의 카드 정보를 출력할 수 있다. - [x] 각 참여자의 카드 합을 출력할 수 있다. From 663bc2803ed93384f32902552815f0330deb0cf8 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 17:07:25 +0900 Subject: [PATCH 071/124] =?UTF-8?q?test:=20=EB=B0=98=EB=B3=B5=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=EC=83=9D=EC=84=B1=20fixture=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/domain/player/HandTest.java | 23 ++++++------------ .../blackjack/domain/player/PlayerTest.java | 24 ++++++------------- src/test/java/fixture/HandFixture.java | 3 ++- 3 files changed, 16 insertions(+), 34 deletions(-) diff --git a/src/test/java/blackjack/domain/player/HandTest.java b/src/test/java/blackjack/domain/player/HandTest.java index e5f9da78fb..2886d83255 100644 --- a/src/test/java/blackjack/domain/player/HandTest.java +++ b/src/test/java/blackjack/domain/player/HandTest.java @@ -1,15 +1,11 @@ package blackjack.domain.player; import blackjack.domain.card.Card; -import blackjack.domain.card.CardNumber; -import blackjack.domain.card.CardShape; +import fixture.CardFixture; import fixture.HandFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.ArrayList; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; @DisplayName("참가자 소유 카드 테스트") @@ -18,11 +14,7 @@ class HandTest { @DisplayName("카드의 합을 계산할 수 있다.") @Test void testHandSummation() { - Card card1 = new Card(CardShape.HEART, CardNumber.TWO); - Card card2 = new Card(CardShape.CLUB, CardNumber.TWO); - Card card3 = new Card(CardShape.DIAMOND, CardNumber.TWO); - - Hand hand = new Hand(List.of(card1, card2, card3)); + Hand hand = HandFixture.of(2, 2, 2); int expected = hand.sum(); assertThat(expected).isEqualTo(6); @@ -31,19 +23,18 @@ void testHandSummation() { @DisplayName("특정 카드를 핸드에 추가할 수 있다") @Test void testAppendCard() { - Card card1 = new Card(CardShape.HEART, CardNumber.TWO); - List cards = new ArrayList<>(); + Card card = CardFixture.from(2); + Hand hand = HandFixture.of(); + hand.append(card); - Hand hand = new Hand(cards); - hand.append(card1); - - assertThat(hand.getCards()).containsExactly(card1); + assertThat(hand.getCards()).containsExactly(card); } @DisplayName("핸드에 에이스 카드가 몇개 있는지 확인할 수 있다") @Test void testCountAceInHand() { Hand hand = HandFixture.of(1, 1, 1, 3, 4, 5); + assertThat(hand.countAce()).isEqualTo(3); } } diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index 01b862d23f..572944bc8e 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -1,14 +1,11 @@ package blackjack.domain.player; import blackjack.domain.card.Card; -import blackjack.domain.card.CardNumber; -import blackjack.domain.card.CardShape; +import fixture.CardFixture; +import fixture.HandFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.ArrayList; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; @DisplayName("참가자 테스트") @@ -18,19 +15,12 @@ class PlayerTest { @Test void testAppendCardToPlayer() { PlayerName playerName = new PlayerName("pobi"); + Hand hand = HandFixture.of(10, 9); + Player player = new Player(playerName, hand); - Card card1 = new Card(CardShape.HEART, CardNumber.TEN); - Card card2 = new Card(CardShape.CLUB, CardNumber.NINE); - - List hand = new ArrayList<>(); - hand.add(card1); - hand.add(card2); - - Player player = new Player(playerName, new Hand(hand)); - - Card card3 = new Card(CardShape.DIAMOND, CardNumber.TWO); - player.appendCard(card3); + Card card = CardFixture.from(2); + player.appendCard(card); - assertThat(hand).containsExactly(card1, card2, card3); + assertThat(hand.sum()).isEqualTo(21); } } \ No newline at end of file diff --git a/src/test/java/fixture/HandFixture.java b/src/test/java/fixture/HandFixture.java index 40572bb714..7510a59c26 100644 --- a/src/test/java/fixture/HandFixture.java +++ b/src/test/java/fixture/HandFixture.java @@ -3,12 +3,13 @@ import blackjack.domain.player.Hand; import java.util.Arrays; +import java.util.stream.Collectors; public class HandFixture { public static Hand of(int... numbers) { return new Hand(Arrays.stream(numbers) .mapToObj(CardFixture::from) - .toList()); + .collect(Collectors.toList())); } } From 38e466f1ea97acced601629eeaf567c2d2ef7d41 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 17:23:08 +0900 Subject: [PATCH 072/124] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EC=88=9C=EC=84=9C=20=EB=B0=B0=EC=B9=98=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 142 +++++++++--------- .../java/blackjack/domain/player/Hand.java | 4 + .../java/blackjack/domain/player/Player.java | 4 + 3 files changed, 82 insertions(+), 68 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index d3764ffda4..78ee5ddacc 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -31,77 +31,61 @@ public void run() { CardDeck cardDeck = initCardDeck(); Player dealer = initDealer(); - issueInitialCardsToPlayers(players, cardDeck); - issueInitialCardsToDealer(dealer, cardDeck); - outputView.printHandOutEvent(players, 2); - outputView.printDealerInitialHand(dealer); - players.getPlayers().forEach(outputView::printPlayerHand); + prepareGame(players, cardDeck, dealer); completePlayersHand(players, cardDeck); completeDealerHand(dealer, cardDeck); - int dealerPopCount = dealer.getHand().getCards().size() - 2; - - if (dealerPopCount > 0) { - outputView.printDealerPopCount(16, dealerPopCount); - } + printDealerPopCount(dealer); printPlayerScore(dealer); printPlayersScore(players); - printDealerGameResult(dealer, players); printPlayersGameResult(players, dealer); - } - private void printPlayersScore(Players players) { - players.getPlayers().forEach(this::printPlayerScore); + private Players initPlayers() { + InputMapper inputMapper = new InputMapper(); + List playerNames = inputMapper.mapToPlayers(inputView.readNames()); + return Players.from(playerNames); } - private void printPlayerScore(Player player) { - Score score = calculateScore(player); - outputView.printPlayerScore(player, score); + private CardDeck initCardDeck() { + CardDeckCreator cardDeckCreator = new CardDeckCreator(); + return cardDeckCreator.create(); } - private void printPlayersGameResult(Players players, Player dealer) { - Judge judge = new Judge(); - Score dealerScore = calculateScore(dealer); - players.getPlayers() - .forEach(player -> { - Score playerScore = calculateScore(player); - outputView.printPlayerGameResult(player, judge.isPlayerWin(dealerScore, playerScore)); - }); + private Player initDealer() { + Hand hand = new Hand(new ArrayList<>()); + return new Player(new PlayerName("딜러"), hand); } - private void printDealerGameResult(Player dealer, Players players) { - Score dealerScore = calculateScore(dealer); - Judge judge = new Judge(); - - int playerWinCount = (int) players.getPlayers().stream() - .map(this::calculateScore) - .filter(playerScore -> judge.isPlayerWin(dealerScore, playerScore)) - .count(); + private void prepareGame(Players players, CardDeck cardDeck, Player dealer) { + issueInitialCardsToPlayers(players, cardDeck); + issueInitialCardsToDealer(dealer, cardDeck); - int dealerWinCount = players.countPlayer() - playerWinCount; + outputView.printHandOutEvent(players, 2); + outputView.printDealerInitialHand(dealer); + players.getPlayers().forEach(outputView::printPlayerHand); + } - DealerGameResult dealerGameResult = new DealerGameResult(dealerWinCount, playerWinCount); - outputView.printDealerGameResult(dealerGameResult); + private void issueInitialCardsToPlayers(Players players, CardDeck cardDeck) { + players.getPlayers().stream() + .map(Player::getHand) + .forEach(hand -> addIssuedCardToHand(hand, issueInitialHand(cardDeck))); } - private void completeDealerHand(Player dealer, CardDeck cardDeck) { - Score dealerScore = calculateScore(dealer); - HitStrategy hitStrategy = new DealerHitStrategy(); + private void issueInitialCardsToDealer(Player dealer, CardDeck cardDeck) { + List cards = issueInitialHand(cardDeck); + addIssuedCardToHand(dealer.getHand(), cards); + } - while (dealerScore.hitAllowed(hitStrategy)) { - Card card = cardDeck.popCard(); - dealer.appendCard(card); - dealerScore = calculateScore(dealer); - } + private List issueInitialHand(CardDeck cardDeck) { + return cardDeck.popCards(2); } - private Score calculateScore(Player player) { - ScoreCalculateStrategy scoreCalculateStrategy = new ScoreCalculateStrategy(); - return player.calculateHandScore(scoreCalculateStrategy); + private void addIssuedCardToHand(Hand hand, List cards) { + cards.forEach(hand::append); } private void completePlayersHand(Players players, CardDeck cardDeck) { @@ -133,38 +117,60 @@ private DrawDecision readHitDecision(Player player) { return hitDecision; } - private CardDeck initCardDeck() { - CardDeckCreator cardDeckCreator = new CardDeckCreator(); - return cardDeckCreator.create(); + private void completeDealerHand(Player dealer, CardDeck cardDeck) { + Score dealerScore = calculateScore(dealer); + HitStrategy hitStrategy = new DealerHitStrategy(); + + while (dealerScore.hitAllowed(hitStrategy)) { + Card card = cardDeck.popCard(); + dealer.appendCard(card); + dealerScore = calculateScore(dealer); + } } - private Players initPlayers() { - InputMapper inputMapper = new InputMapper(); - List playerNames = inputMapper.mapToPlayers(inputView.readNames()); - return Players.from(playerNames); + private void printDealerPopCount(Player dealer) { + int dealerPopCount = dealer.handSize() - 2; + if (dealerPopCount > 0) { + outputView.printDealerPopCount(16, dealerPopCount); + } } - private Player initDealer() { - Hand hand = new Hand(new ArrayList<>()); - return new Player(new PlayerName("딜러"), hand); + private void printPlayersScore(Players players) { + players.getPlayers().forEach(this::printPlayerScore); } - private List issueInitialHand(CardDeck cardDeck) { - return cardDeck.popCards(2); + private void printPlayerScore(Player player) { + Score score = calculateScore(player); + outputView.printPlayerScore(player, score); } - private void addIssuedCardToHand(Hand hand, List cards) { - cards.forEach(hand::append); + private void printDealerGameResult(Player dealer, Players players) { + Score dealerScore = calculateScore(dealer); + Judge judge = new Judge(); + + int playerWinCount = (int) players.getPlayers().stream() + .map(this::calculateScore) + .filter(playerScore -> judge.isPlayerWin(dealerScore, playerScore)) + .count(); + + int dealerWinCount = players.countPlayer() - playerWinCount; + + DealerGameResult dealerGameResult = new DealerGameResult(dealerWinCount, playerWinCount); + outputView.printDealerGameResult(dealerGameResult); } - private void issueInitialCardsToPlayers(Players players, CardDeck cardDeck) { - players.getPlayers().stream() - .map(Player::getHand) - .forEach(hand -> addIssuedCardToHand(hand, issueInitialHand(cardDeck))); + private void printPlayersGameResult(Players players, Player dealer) { + Judge judge = new Judge(); + Score dealerScore = calculateScore(dealer); + players.getPlayers() + .forEach(player -> { + Score playerScore = calculateScore(player); + outputView.printPlayerGameResult(player, judge.isPlayerWin(dealerScore, playerScore)); + }); } - private void issueInitialCardsToDealer(Player dealer, CardDeck cardDeck) { - List cards = issueInitialHand(cardDeck); - addIssuedCardToHand(dealer.getHand(), cards); + private Score calculateScore(Player player) { + ScoreCalculateStrategy scoreCalculateStrategy = new ScoreCalculateStrategy(); + return player.calculateHandScore(scoreCalculateStrategy); } } diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index f3b76e8834..854a35f0a6 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -28,6 +28,10 @@ public int countAce() { .count(); } + public int countCard() { + return cards.size(); + } + public List getCards() { return List.copyOf(cards); } diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index 702403ccae..317c80488d 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -24,6 +24,10 @@ public Score calculateHandScore(ScoreCalculateStrategy scoreCalculateStrategy) { return scoreCalculateStrategy.calculate(hand); } + public int handSize() { + return hand.countCard(); + } + @Override public boolean equals(Object o) { if (this == o) return true; From f86a043a57abc8872d41deaf6a8d87fa2f626690 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 20:32:24 +0900 Subject: [PATCH 073/124] =?UTF-8?q?feat:=20Deck=EC=97=90=EC=84=9C=20Hand?= =?UTF-8?q?=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8A=94=20HandCreator?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/player/HandCreator.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/java/blackjack/domain/player/HandCreator.java diff --git a/src/main/java/blackjack/domain/player/HandCreator.java b/src/main/java/blackjack/domain/player/HandCreator.java new file mode 100644 index 0000000000..565d2577b7 --- /dev/null +++ b/src/main/java/blackjack/domain/player/HandCreator.java @@ -0,0 +1,12 @@ +package blackjack.domain.player; + +import blackjack.domain.card.CardDeck; + +public class HandCreator { + + private static final int INITIAL_HAND_SIZE = 2; + + public Hand createFromDeck(CardDeck cardDeck) { + return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE)); + } +} From f7593b5097edfd85c190ef48b98ec173eb6fede1 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 20:47:25 +0900 Subject: [PATCH 074/124] =?UTF-8?q?fix:=20CardDeck=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EB=BD=91=EC=9D=84=20=EB=95=8C=20?= =?UTF-8?q?=EB=B6=88=EB=B3=80=EB=A6=AC=EC=8A=A4=ED=8A=B8=EB=A5=BC=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/card/CardDeck.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index beac6f7f39..7af1ac3555 100644 --- a/src/main/java/blackjack/domain/card/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -1,6 +1,7 @@ package blackjack.domain.card; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.IntStream; public class CardDeck { @@ -21,6 +22,6 @@ public Card popCard() { public List popCards(int count) { return IntStream.range(0, count) .mapToObj(i -> popCard()) - .toList(); + .collect(Collectors.toList()); } } From 563bedffd39b476af79056351d265010752dff90 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 20:48:08 +0900 Subject: [PATCH 075/124] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EA=B7=9C?= =?UTF-8?q?=EC=B9=99=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=ED=94=8C=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=96=B4=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/player/PlayerCreator.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/blackjack/domain/player/PlayerCreator.java diff --git a/src/main/java/blackjack/domain/player/PlayerCreator.java b/src/main/java/blackjack/domain/player/PlayerCreator.java new file mode 100644 index 0000000000..920ecc2ce6 --- /dev/null +++ b/src/main/java/blackjack/domain/player/PlayerCreator.java @@ -0,0 +1,22 @@ +package blackjack.domain.player; + +import blackjack.domain.card.CardDeck; + +public class PlayerCreator { + + private static final String DEALER_NAME = "딜러"; + + private final HandCreator handCreator; + + public PlayerCreator(HandCreator handCreator) { + this.handCreator = handCreator; + } + + public Player createPlayerFrom(PlayerName playerName, CardDeck cardDeck) { + return new Player(playerName, handCreator.createFromDeck(cardDeck)); + } + + public Player createDealerFrom(CardDeck cardDeck) { + return new Player(new PlayerName(DEALER_NAME), handCreator.createFromDeck(cardDeck)); + } +} From a8d1b14000367b4673765277c9ec0db619f4f679 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 20:48:33 +0900 Subject: [PATCH 076/124] =?UTF-8?q?refactor:=20PlayerCreator=EB=A1=9C=20?= =?UTF-8?q?=EB=B6=80=ED=84=B0=20=ED=94=8C=EB=A0=88=EC=9D=B4=EC=96=B4?= =?UTF-8?q?=EC=99=80=20=EB=94=9C=EB=9F=AC=EB=A5=BC=20=EC=B4=88=EA=B8=B0?= =?UTF-8?q?=ED=99=94=ED=95=98=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 56 ++++++------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 78ee5ddacc..e8c81f9bbe 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -5,15 +5,11 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; import blackjack.domain.card.CardDeckCreator; -import blackjack.domain.player.Hand; -import blackjack.domain.player.Player; -import blackjack.domain.player.PlayerName; -import blackjack.domain.player.Players; +import blackjack.domain.player.*; import blackjack.domain.rule.*; import blackjack.view.InputView; import blackjack.view.OutputView; -import java.util.ArrayList; import java.util.List; public class BlackJackController { @@ -27,11 +23,11 @@ public BlackJackController(InputView inputView, OutputView outputView) { } public void run() { - Players players = initPlayers(); CardDeck cardDeck = initCardDeck(); - Player dealer = initDealer(); + Players players = initPlayers(cardDeck); + Player dealer = initDealer(cardDeck); - prepareGame(players, cardDeck, dealer); + printPlayersInformation(players, cardDeck, dealer); completePlayersHand(players, cardDeck); completeDealerHand(dealer, cardDeck); @@ -44,50 +40,32 @@ public void run() { printPlayersGameResult(players, dealer); } - private Players initPlayers() { - InputMapper inputMapper = new InputMapper(); - List playerNames = inputMapper.mapToPlayers(inputView.readNames()); - return Players.from(playerNames); - } - private CardDeck initCardDeck() { CardDeckCreator cardDeckCreator = new CardDeckCreator(); return cardDeckCreator.create(); } - private Player initDealer() { - Hand hand = new Hand(new ArrayList<>()); - return new Player(new PlayerName("딜러"), hand); + private Players initPlayers(CardDeck cardDeck) { + InputMapper inputMapper = new InputMapper(); + PlayerCreator playerCreator = new PlayerCreator(new HandCreator()); + List playerNames = inputMapper.mapToPlayers(inputView.readNames()); + + return new Players(playerNames.stream() + .map(playerName -> playerCreator.createPlayerFrom(playerName, cardDeck)) + .toList()); } - private void prepareGame(Players players, CardDeck cardDeck, Player dealer) { - issueInitialCardsToPlayers(players, cardDeck); - issueInitialCardsToDealer(dealer, cardDeck); + private Player initDealer(CardDeck cardDeck) { + PlayerCreator playerCreator = new PlayerCreator(new HandCreator()); + return playerCreator.createDealerFrom(cardDeck); + } + private void printPlayersInformation(Players players, CardDeck cardDeck, Player dealer) { outputView.printHandOutEvent(players, 2); outputView.printDealerInitialHand(dealer); players.getPlayers().forEach(outputView::printPlayerHand); } - private void issueInitialCardsToPlayers(Players players, CardDeck cardDeck) { - players.getPlayers().stream() - .map(Player::getHand) - .forEach(hand -> addIssuedCardToHand(hand, issueInitialHand(cardDeck))); - } - - private void issueInitialCardsToDealer(Player dealer, CardDeck cardDeck) { - List cards = issueInitialHand(cardDeck); - addIssuedCardToHand(dealer.getHand(), cards); - } - - private List issueInitialHand(CardDeck cardDeck) { - return cardDeck.popCards(2); - } - - private void addIssuedCardToHand(Hand hand, List cards) { - cards.forEach(hand::append); - } - private void completePlayersHand(Players players, CardDeck cardDeck) { for (Player player : players.getPlayers()) { completePlayerHand(player, cardDeck); From 60d08c4a251c6981e8b67a81e577ebe8d5583458 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 21:31:10 +0900 Subject: [PATCH 077/124] =?UTF-8?q?feat:=20Players=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=EC=97=90=20=ED=8A=B9=EC=A0=95=20=EC=A0=90=EC=88=98=20?= =?UTF-8?q?=EC=9D=B4=ED=95=98=EC=9D=B8=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EB=A5=BC=20=EC=84=B8=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Players.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index 6942b4f974..d0f180f7a0 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -1,5 +1,8 @@ package blackjack.domain.player; +import blackjack.domain.rule.Score; +import blackjack.domain.rule.ScoreCalculateStrategy; + import java.util.ArrayList; import java.util.List; @@ -24,6 +27,13 @@ public int countPlayer() { return players.size(); } + public int countPlayerWithScoreAbove(Score target, ScoreCalculateStrategy scoreCalculateStrategy) { + return (int) players.stream() + .map(player -> player.calculateHandScore(scoreCalculateStrategy)) + .filter(playerScore -> playerScore.isBiggerThan(target)) + .count(); + } + private void validate(List players) { validateEachPlayerNameUnique(players); validateEntryNotEmpty(players); From 35898432acd4ab3dc3fc37ab6a855d40c0322464 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 21:35:30 +0900 Subject: [PATCH 078/124] =?UTF-8?q?refactor:=20=EA=B5=AC=ED=98=84=ED=95=9C?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=EC=9D=84=20=EC=9D=B4=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=B1=85=EC=9E=84=20=EC=9D=B4=EC=A0=84=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 30 +++++++------------ .../java/blackjack/domain/player/Players.java | 2 +- .../java/blackjack/domain/rule/Judge.java | 4 +-- .../java/blackjack/domain/rule/Score.java | 6 +--- .../java/blackjack/domain/rule/ScoreTest.java | 21 ++----------- 5 files changed, 16 insertions(+), 47 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index e8c81f9bbe..15c0576ac1 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -12,6 +12,8 @@ import java.util.List; +import static blackjack.domain.DrawDecision.YES; + public class BlackJackController { private final InputView inputView; @@ -26,7 +28,6 @@ public void run() { CardDeck cardDeck = initCardDeck(); Players players = initPlayers(cardDeck); Player dealer = initDealer(cardDeck); - printPlayersInformation(players, cardDeck, dealer); completePlayersHand(players, cardDeck); @@ -76,13 +77,8 @@ private void completePlayerHand(Player player, CardDeck cardDeck) { Score playerScore = calculateScore(player); HitStrategy hitStrategy = new PlayerHitStrategy(); - while (playerScore.hitAllowed(hitStrategy)) { - DrawDecision drawDecision = readHitDecision(player); - if (drawDecision == DrawDecision.NO) { - break; - } - Card card = cardDeck.popCard(); - player.appendCard(card); + while (playerScore.hitAllowed(hitStrategy) && readHitDecision(player) == YES) { + player.appendCard(cardDeck.popCard()); outputView.printPlayerHand(player); playerScore = calculateScore(player); } @@ -106,6 +102,11 @@ private void completeDealerHand(Player dealer, CardDeck cardDeck) { } } + private Score calculateScore(Player player) { + ScoreCalculateStrategy scoreCalculateStrategy = new ScoreCalculateStrategy(); + return player.calculateHandScore(scoreCalculateStrategy); + } + private void printDealerPopCount(Player dealer) { int dealerPopCount = dealer.handSize() - 2; if (dealerPopCount > 0) { @@ -124,13 +125,7 @@ private void printPlayerScore(Player player) { private void printDealerGameResult(Player dealer, Players players) { Score dealerScore = calculateScore(dealer); - Judge judge = new Judge(); - - int playerWinCount = (int) players.getPlayers().stream() - .map(this::calculateScore) - .filter(playerScore -> judge.isPlayerWin(dealerScore, playerScore)) - .count(); - + int playerWinCount = players.countPlayerWithScoreAbove(dealerScore, new ScoreCalculateStrategy()); int dealerWinCount = players.countPlayer() - playerWinCount; DealerGameResult dealerGameResult = new DealerGameResult(dealerWinCount, playerWinCount); @@ -146,9 +141,4 @@ private void printPlayersGameResult(Players players, Player dealer) { outputView.printPlayerGameResult(player, judge.isPlayerWin(dealerScore, playerScore)); }); } - - private Score calculateScore(Player player) { - ScoreCalculateStrategy scoreCalculateStrategy = new ScoreCalculateStrategy(); - return player.calculateHandScore(scoreCalculateStrategy); - } } diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index d0f180f7a0..96fd717a38 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -30,7 +30,7 @@ public int countPlayer() { public int countPlayerWithScoreAbove(Score target, ScoreCalculateStrategy scoreCalculateStrategy) { return (int) players.stream() .map(player -> player.calculateHandScore(scoreCalculateStrategy)) - .filter(playerScore -> playerScore.isBiggerThan(target)) + .filter(playerScore -> playerScore.isAbove(target)) .count(); } diff --git a/src/main/java/blackjack/domain/rule/Judge.java b/src/main/java/blackjack/domain/rule/Judge.java index 55d54d838a..f484085c7c 100644 --- a/src/main/java/blackjack/domain/rule/Judge.java +++ b/src/main/java/blackjack/domain/rule/Judge.java @@ -2,7 +2,7 @@ public class Judge { - private static final int BLACK_JACK = 21; + private static final Score BLACK_JACK = new Score(21); public boolean isPlayerWin(Score dealerScore, Score playerScore) { if (playerScore.isAbove(BLACK_JACK)) { @@ -11,6 +11,6 @@ public boolean isPlayerWin(Score dealerScore, Score playerScore) { if (dealerScore.isAbove(BLACK_JACK)) { return true; } - return playerScore.isBiggerThan(dealerScore); + return playerScore.isAbove(dealerScore); } } diff --git a/src/main/java/blackjack/domain/rule/Score.java b/src/main/java/blackjack/domain/rule/Score.java index 3f17f79d26..ef40ac5cf6 100644 --- a/src/main/java/blackjack/domain/rule/Score.java +++ b/src/main/java/blackjack/domain/rule/Score.java @@ -12,11 +12,7 @@ public boolean hitAllowed(HitStrategy hitStrategy) { return hitStrategy.canHit(this); } - public boolean isAbove(int bustCondition) { - return bustCondition < value; - } - - public boolean isBiggerThan(Score target) { + public boolean isAbove(Score target) { return this.value > target.value; } diff --git a/src/test/java/blackjack/domain/rule/ScoreTest.java b/src/test/java/blackjack/domain/rule/ScoreTest.java index fbdfc86aa2..e87e73749d 100644 --- a/src/test/java/blackjack/domain/rule/ScoreTest.java +++ b/src/test/java/blackjack/domain/rule/ScoreTest.java @@ -9,30 +9,13 @@ @DisplayName("점수 테스트") class ScoreTest { - - @DisplayName("점수가 파라미터를 넘는지 확인할 수 있다") - @ParameterizedTest - @ValueSource(ints = {1, 2, 3, 4, 5}) - void testScoreAbove(int value) { - Score score = new Score(6); - assertThat(score.isAbove(value)).isTrue(); - } - - @DisplayName("점수가 파라미터를 넘지 않는지 확인할 수 있다") - @ParameterizedTest - @ValueSource(ints = {1, 2, 3, 4, 5}) - void testScoreBelow(int value) { - Score score = new Score(0); - assertThat(score.isAbove(value)).isFalse(); - } - @DisplayName("더 낮은 점수와 비교할 수 있다") @ParameterizedTest @ValueSource(ints = {1, 2, 3, 4, 5}) void testScoreCompareWithBigger(int value) { Score score = new Score(0); Score target = new Score(value); - assertThat(score.isBiggerThan(target)).isFalse(); + assertThat(score.isAbove(target)).isFalse(); } @DisplayName("점수가 파라미터를 넘지 않는지 확인할 수 있다") @@ -41,6 +24,6 @@ void testScoreCompareWithBigger(int value) { void testScoreCompareWithLower(int value) { Score score = new Score(6); Score target = new Score(value); - assertThat(score.isBiggerThan(target)).isTrue(); + assertThat(score.isAbove(target)).isTrue(); } } \ No newline at end of file From 10e20b878239169da0600c63dce759268ca0d186 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 21:38:29 +0900 Subject: [PATCH 079/124] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=ED=8C=8C=EB=9D=BC=EB=AF=B8?= =?UTF-8?q?=ED=84=B0=20=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/controller/BlackJackController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 15c0576ac1..464030faf0 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -28,7 +28,7 @@ public void run() { CardDeck cardDeck = initCardDeck(); Players players = initPlayers(cardDeck); Player dealer = initDealer(cardDeck); - printPlayersInformation(players, cardDeck, dealer); + printPlayersInformation(players, dealer); completePlayersHand(players, cardDeck); completeDealerHand(dealer, cardDeck); @@ -61,7 +61,7 @@ private Player initDealer(CardDeck cardDeck) { return playerCreator.createDealerFrom(cardDeck); } - private void printPlayersInformation(Players players, CardDeck cardDeck, Player dealer) { + private void printPlayersInformation(Players players, Player dealer) { outputView.printHandOutEvent(players, 2); outputView.printDealerInitialHand(dealer); players.getPlayers().forEach(outputView::printPlayerHand); From 5728ff561e7d668201a5732882ef17c71d743cbf Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 21:46:58 +0900 Subject: [PATCH 080/124] =?UTF-8?q?refactor:=20=EC=B1=85=EC=9E=84=20?= =?UTF-8?q?=EC=9D=B4=EC=A0=84=EC=9C=BC=EB=A1=9C=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Players.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index 96fd717a38..43c39b3131 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -3,7 +3,6 @@ import blackjack.domain.rule.Score; import blackjack.domain.rule.ScoreCalculateStrategy; -import java.util.ArrayList; import java.util.List; public class Players { @@ -17,12 +16,6 @@ public Players(List players) { this.players = players; } - public static Players from(List playerNames) { - return new Players(playerNames.stream() - .map(playerName -> new Player(playerName, new Hand(new ArrayList<>()))) - .toList()); - } - public int countPlayer() { return players.size(); } From 867fcb4b7e1a96694b885228d6860dcf5ea17970 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 21:48:32 +0900 Subject: [PATCH 081/124] =?UTF-8?q?feat:=20Hand=EA=B0=80=20=EC=9E=90?= =?UTF-8?q?=EC=8B=A0=EC=9D=98=20=EC=B1=84=EC=A0=90=20=EA=B8=B0=EC=A4=80?= =?UTF-8?q?=EC=9D=84=20=EC=86=8D=EC=84=B1=EC=9C=BC=EB=A1=9C=20=EA=B0=80?= =?UTF-8?q?=EC=A7=80=EB=8F=84=EB=A1=9D=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Hand.java | 5 ++++- src/main/java/blackjack/domain/player/HandCreator.java | 3 ++- src/test/java/fixture/HandFixture.java | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index 854a35f0a6..f56601f003 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -1,15 +1,18 @@ package blackjack.domain.player; import blackjack.domain.card.Card; +import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.List; public class Hand { private final List cards; + private final ScoreCalculateStrategy scoreCalculateStrategy; - public Hand(List cards) { + public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy) { this.cards = cards; + this.scoreCalculateStrategy = scoreCalculateStrategy; } public int sum() { diff --git a/src/main/java/blackjack/domain/player/HandCreator.java b/src/main/java/blackjack/domain/player/HandCreator.java index 565d2577b7..e08e7b5945 100644 --- a/src/main/java/blackjack/domain/player/HandCreator.java +++ b/src/main/java/blackjack/domain/player/HandCreator.java @@ -1,12 +1,13 @@ package blackjack.domain.player; import blackjack.domain.card.CardDeck; +import blackjack.domain.rule.ScoreCalculateStrategy; public class HandCreator { private static final int INITIAL_HAND_SIZE = 2; public Hand createFromDeck(CardDeck cardDeck) { - return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE)); + return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy()); } } diff --git a/src/test/java/fixture/HandFixture.java b/src/test/java/fixture/HandFixture.java index 7510a59c26..2e8b1b72bc 100644 --- a/src/test/java/fixture/HandFixture.java +++ b/src/test/java/fixture/HandFixture.java @@ -1,6 +1,7 @@ package fixture; import blackjack.domain.player.Hand; +import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.Arrays; import java.util.stream.Collectors; @@ -10,6 +11,6 @@ public class HandFixture { public static Hand of(int... numbers) { return new Hand(Arrays.stream(numbers) .mapToObj(CardFixture::from) - .collect(Collectors.toList())); + .collect(Collectors.toList()), new ScoreCalculateStrategy()); } } From a6b3eb03cbf9238cf269ceb4713cece3bc833c50 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 21:49:18 +0900 Subject: [PATCH 082/124] =?UTF-8?q?feat:=20Hand=EA=B0=80=20=EC=9E=90?= =?UTF-8?q?=EC=8B=A0=EC=9D=84=20=EC=B1=84=EC=A0=90=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Hand.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index f56601f003..cdd38f9352 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -1,6 +1,7 @@ package blackjack.domain.player; import blackjack.domain.card.Card; +import blackjack.domain.rule.Score; import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.List; @@ -35,6 +36,10 @@ public int countCard() { return cards.size(); } + public Score calculateScore() { + return scoreCalculateStrategy.calculate(this); + } + public List getCards() { return List.copyOf(cards); } From b6a027a030ad74ee3488e138f2b10041a58dae3d Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 21:51:57 +0900 Subject: [PATCH 083/124] =?UTF-8?q?refactor:=20=EC=B1=85=EC=9E=84=20?= =?UTF-8?q?=EC=9D=B4=EC=A0=84=20=EA=B0=9C=EC=84=A0=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/controller/BlackJackController.java | 3 +-- src/main/java/blackjack/domain/player/Player.java | 5 ++--- src/main/java/blackjack/domain/player/Players.java | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 464030faf0..83d6693b60 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -103,8 +103,7 @@ private void completeDealerHand(Player dealer, CardDeck cardDeck) { } private Score calculateScore(Player player) { - ScoreCalculateStrategy scoreCalculateStrategy = new ScoreCalculateStrategy(); - return player.calculateHandScore(scoreCalculateStrategy); + return player.calculateHandScore(); } private void printDealerPopCount(Player dealer) { diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index 317c80488d..0edb249fe4 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -2,7 +2,6 @@ import blackjack.domain.card.Card; import blackjack.domain.rule.Score; -import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.Objects; @@ -20,8 +19,8 @@ public void appendCard(Card card) { hand.append(card); } - public Score calculateHandScore(ScoreCalculateStrategy scoreCalculateStrategy) { - return scoreCalculateStrategy.calculate(hand); + public Score calculateHandScore() { + return hand.calculateScore(); } public int handSize() { diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index 43c39b3131..2eefafc462 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -22,7 +22,7 @@ public int countPlayer() { public int countPlayerWithScoreAbove(Score target, ScoreCalculateStrategy scoreCalculateStrategy) { return (int) players.stream() - .map(player -> player.calculateHandScore(scoreCalculateStrategy)) + .map(Player::calculateHandScore) .filter(playerScore -> playerScore.isAbove(target)) .count(); } From 530c89c2d63a01741cfb32ab9f650f959e2d8c4a Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 21:55:23 +0900 Subject: [PATCH 084/124] =?UTF-8?q?refactor:=20=EC=A0=90=EC=88=98=EB=A5=BC?= =?UTF-8?q?=20=ED=94=8C=EB=A0=88=EC=9D=B4=EC=96=B4=EB=A5=BC=20=ED=86=B5?= =?UTF-8?q?=ED=95=B4=20=EA=B3=84=EC=82=B0=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 83d6693b60..295ab2ce1e 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -74,13 +74,10 @@ private void completePlayersHand(Players players, CardDeck cardDeck) { } private void completePlayerHand(Player player, CardDeck cardDeck) { - Score playerScore = calculateScore(player); HitStrategy hitStrategy = new PlayerHitStrategy(); - - while (playerScore.hitAllowed(hitStrategy) && readHitDecision(player) == YES) { + while (player.calculateHandScore().hitAllowed(hitStrategy) && readHitDecision(player) == YES) { player.appendCard(cardDeck.popCard()); outputView.printPlayerHand(player); - playerScore = calculateScore(player); } } @@ -92,20 +89,13 @@ private DrawDecision readHitDecision(Player player) { } private void completeDealerHand(Player dealer, CardDeck cardDeck) { - Score dealerScore = calculateScore(dealer); HitStrategy hitStrategy = new DealerHitStrategy(); - - while (dealerScore.hitAllowed(hitStrategy)) { + while (dealer.calculateHandScore().hitAllowed(hitStrategy)) { Card card = cardDeck.popCard(); dealer.appendCard(card); - dealerScore = calculateScore(dealer); } } - private Score calculateScore(Player player) { - return player.calculateHandScore(); - } - private void printDealerPopCount(Player dealer) { int dealerPopCount = dealer.handSize() - 2; if (dealerPopCount > 0) { @@ -118,12 +108,11 @@ private void printPlayersScore(Players players) { } private void printPlayerScore(Player player) { - Score score = calculateScore(player); - outputView.printPlayerScore(player, score); + outputView.printPlayerScore(player, player.calculateHandScore()); } private void printDealerGameResult(Player dealer, Players players) { - Score dealerScore = calculateScore(dealer); + Score dealerScore = dealer.calculateHandScore(); int playerWinCount = players.countPlayerWithScoreAbove(dealerScore, new ScoreCalculateStrategy()); int dealerWinCount = players.countPlayer() - playerWinCount; @@ -133,10 +122,10 @@ private void printDealerGameResult(Player dealer, Players players) { private void printPlayersGameResult(Players players, Player dealer) { Judge judge = new Judge(); - Score dealerScore = calculateScore(dealer); + Score dealerScore = dealer.calculateHandScore(); players.getPlayers() .forEach(player -> { - Score playerScore = calculateScore(player); + Score playerScore = player.calculateHandScore(); outputView.printPlayerGameResult(player, judge.isPlayerWin(dealerScore, playerScore)); }); } From 4abbc51e6d19622c90be9772d0dbfb5ce53c351b Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 22:10:28 +0900 Subject: [PATCH 085/124] =?UTF-8?q?refactor:=20Hand=EA=B0=80=20Hit?= =?UTF-8?q?=EC=A0=84=EB=9E=B5=EC=9D=84=20=EC=86=8D=EC=84=B1=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EA=B0=80=EC=A7=80=EB=8F=84=EB=A1=9D=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Hand.java | 5 ++++- src/main/java/blackjack/domain/player/HandCreator.java | 10 ++++++++-- .../java/blackjack/domain/player/PlayerCreator.java | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index cdd38f9352..fa9ee9c862 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -1,6 +1,7 @@ package blackjack.domain.player; import blackjack.domain.card.Card; +import blackjack.domain.rule.HitStrategy; import blackjack.domain.rule.Score; import blackjack.domain.rule.ScoreCalculateStrategy; @@ -10,10 +11,12 @@ public class Hand { private final List cards; private final ScoreCalculateStrategy scoreCalculateStrategy; + private final HitStrategy hitStrategy; - public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy) { + public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy, HitStrategy hitStrategy) { this.cards = cards; this.scoreCalculateStrategy = scoreCalculateStrategy; + this.hitStrategy = hitStrategy; } public int sum() { diff --git a/src/main/java/blackjack/domain/player/HandCreator.java b/src/main/java/blackjack/domain/player/HandCreator.java index e08e7b5945..80a1a8ddae 100644 --- a/src/main/java/blackjack/domain/player/HandCreator.java +++ b/src/main/java/blackjack/domain/player/HandCreator.java @@ -1,13 +1,19 @@ package blackjack.domain.player; import blackjack.domain.card.CardDeck; +import blackjack.domain.rule.DealerHitStrategy; +import blackjack.domain.rule.PlayerHitStrategy; import blackjack.domain.rule.ScoreCalculateStrategy; public class HandCreator { private static final int INITIAL_HAND_SIZE = 2; - public Hand createFromDeck(CardDeck cardDeck) { - return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy()); + public Hand createPlayerHandFrom(CardDeck cardDeck) { + return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy(), new PlayerHitStrategy()); + } + + public Hand createDealerHandFrom(CardDeck cardDeck) { + return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy(), new DealerHitStrategy()); } } diff --git a/src/main/java/blackjack/domain/player/PlayerCreator.java b/src/main/java/blackjack/domain/player/PlayerCreator.java index 920ecc2ce6..1910e14508 100644 --- a/src/main/java/blackjack/domain/player/PlayerCreator.java +++ b/src/main/java/blackjack/domain/player/PlayerCreator.java @@ -13,10 +13,10 @@ public PlayerCreator(HandCreator handCreator) { } public Player createPlayerFrom(PlayerName playerName, CardDeck cardDeck) { - return new Player(playerName, handCreator.createFromDeck(cardDeck)); + return new Player(playerName, handCreator.createPlayerHandFrom(cardDeck)); } public Player createDealerFrom(CardDeck cardDeck) { - return new Player(new PlayerName(DEALER_NAME), handCreator.createFromDeck(cardDeck)); + return new Player(new PlayerName(DEALER_NAME), handCreator.createPlayerHandFrom(cardDeck)); } } From ee6ca01a24803986141e22334386a06df43d0700 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 22:13:59 +0900 Subject: [PATCH 086/124] =?UTF-8?q?refactor:=20Player=EC=97=90=EA=B2=8C=20?= =?UTF-8?q?=ED=9E=88=ED=8A=B8=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8A=94?= =?UTF-8?q?=EC=A7=80=20=EC=A7=81=EC=A0=91=20=EB=AC=BC=EC=96=B4=EB=B3=B4?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/controller/BlackJackController.java | 10 +++++----- src/main/java/blackjack/domain/player/Hand.java | 4 ++++ src/main/java/blackjack/domain/player/Player.java | 4 ++++ .../java/blackjack/domain/player/PlayerCreator.java | 2 +- src/main/java/blackjack/domain/rule/Score.java | 4 ---- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 295ab2ce1e..885a8c06d4 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -6,7 +6,9 @@ import blackjack.domain.card.CardDeck; import blackjack.domain.card.CardDeckCreator; import blackjack.domain.player.*; -import blackjack.domain.rule.*; +import blackjack.domain.rule.Judge; +import blackjack.domain.rule.Score; +import blackjack.domain.rule.ScoreCalculateStrategy; import blackjack.view.InputView; import blackjack.view.OutputView; @@ -74,8 +76,7 @@ private void completePlayersHand(Players players, CardDeck cardDeck) { } private void completePlayerHand(Player player, CardDeck cardDeck) { - HitStrategy hitStrategy = new PlayerHitStrategy(); - while (player.calculateHandScore().hitAllowed(hitStrategy) && readHitDecision(player) == YES) { + while (player.canHit() && readHitDecision(player) == YES) { player.appendCard(cardDeck.popCard()); outputView.printPlayerHand(player); } @@ -89,8 +90,7 @@ private DrawDecision readHitDecision(Player player) { } private void completeDealerHand(Player dealer, CardDeck cardDeck) { - HitStrategy hitStrategy = new DealerHitStrategy(); - while (dealer.calculateHandScore().hitAllowed(hitStrategy)) { + while (dealer.canHit()) { Card card = cardDeck.popCard(); dealer.appendCard(card); } diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index fa9ee9c862..cc23eec94a 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -43,6 +43,10 @@ public Score calculateScore() { return scoreCalculateStrategy.calculate(this); } + public boolean canHit() { + return hitStrategy.canHit(calculateScore()); + } + public List getCards() { return List.copyOf(cards); } diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index 0edb249fe4..d8c6c754c1 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -23,6 +23,10 @@ public Score calculateHandScore() { return hand.calculateScore(); } + public boolean canHit() { + return hand.canHit(); + } + public int handSize() { return hand.countCard(); } diff --git a/src/main/java/blackjack/domain/player/PlayerCreator.java b/src/main/java/blackjack/domain/player/PlayerCreator.java index 1910e14508..f0b6100960 100644 --- a/src/main/java/blackjack/domain/player/PlayerCreator.java +++ b/src/main/java/blackjack/domain/player/PlayerCreator.java @@ -17,6 +17,6 @@ public Player createPlayerFrom(PlayerName playerName, CardDeck cardDeck) { } public Player createDealerFrom(CardDeck cardDeck) { - return new Player(new PlayerName(DEALER_NAME), handCreator.createPlayerHandFrom(cardDeck)); + return new Player(new PlayerName(DEALER_NAME), handCreator.createDealerHandFrom(cardDeck)); } } diff --git a/src/main/java/blackjack/domain/rule/Score.java b/src/main/java/blackjack/domain/rule/Score.java index ef40ac5cf6..53ec0d43f2 100644 --- a/src/main/java/blackjack/domain/rule/Score.java +++ b/src/main/java/blackjack/domain/rule/Score.java @@ -8,10 +8,6 @@ public Score(int value) { this.value = value; } - public boolean hitAllowed(HitStrategy hitStrategy) { - return hitStrategy.canHit(this); - } - public boolean isAbove(Score target) { return this.value > target.value; } From 86de5c1144060a53aa1dae67dfaa13e4a93ceeb1 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 22:20:09 +0900 Subject: [PATCH 087/124] =?UTF-8?q?refactor:=20=EB=9E=8C=EB=8B=A4=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/controller/BlackJackController.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 885a8c06d4..70bf16ddbe 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -70,9 +70,7 @@ private void printPlayersInformation(Players players, Player dealer) { } private void completePlayersHand(Players players, CardDeck cardDeck) { - for (Player player : players.getPlayers()) { - completePlayerHand(player, cardDeck); - } + players.getPlayers().forEach(player -> completePlayerHand(player, cardDeck)); } private void completePlayerHand(Player player, CardDeck cardDeck) { From 2fd301bd64456a1d00ff03b79e821d8bd6a0a952 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 22:21:35 +0900 Subject: [PATCH 088/124] =?UTF-8?q?refactor:=20inline=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/controller/BlackJackController.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 70bf16ddbe..5994ddbb14 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -81,10 +81,8 @@ private void completePlayerHand(Player player, CardDeck cardDeck) { } private DrawDecision readHitDecision(Player player) { - String name = player.getName(); InputMapper inputMapper = new InputMapper(); - DrawDecision hitDecision = inputMapper.mapToDrawDecision(inputView.readDrawPlan(name)); - return hitDecision; + return inputMapper.mapToDrawDecision(inputView.readDrawPlan(player.getName())); } private void completeDealerHand(Player dealer, CardDeck cardDeck) { From a794e127494b06d90d585196ff3feb29f4f9a75a Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 22:56:48 +0900 Subject: [PATCH 089/124] =?UTF-8?q?test:=20=EC=88=98=EC=A0=95=EB=90=9C=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=EC=97=90=20=EB=94=B0=EB=9D=BC=20fix?= =?UTF-8?q?ture=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/fixture/HandFixture.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/fixture/HandFixture.java b/src/test/java/fixture/HandFixture.java index 2e8b1b72bc..c9e3847e89 100644 --- a/src/test/java/fixture/HandFixture.java +++ b/src/test/java/fixture/HandFixture.java @@ -1,6 +1,7 @@ package fixture; import blackjack.domain.player.Hand; +import blackjack.domain.rule.PlayerHitStrategy; import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.Arrays; @@ -11,6 +12,6 @@ public class HandFixture { public static Hand of(int... numbers) { return new Hand(Arrays.stream(numbers) .mapToObj(CardFixture::from) - .collect(Collectors.toList()), new ScoreCalculateStrategy()); + .collect(Collectors.toList()), new ScoreCalculateStrategy(), new PlayerHitStrategy()); } } From b27538ded9ac6e63c4e3be37564552debe15d5fa Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 8 Mar 2024 23:55:55 +0900 Subject: [PATCH 090/124] =?UTF-8?q?style:=20=EC=9A=B0=ED=85=8C=EC=BD=94=20?= =?UTF-8?q?=EC=8A=A4=ED=83=80=EC=9D=BC=20=ED=8F=AC=EB=A7=A4=ED=8C=85=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.java | 11 +++++++---- src/main/java/blackjack/controller/InputMapper.java | 1 - src/main/java/blackjack/domain/card/CardDeck.java | 2 +- src/main/java/blackjack/domain/player/Hand.java | 1 - src/main/java/blackjack/domain/player/Player.java | 9 ++++++--- src/main/java/blackjack/domain/player/PlayerName.java | 8 ++++++-- src/main/java/blackjack/domain/player/Players.java | 1 - src/main/java/blackjack/view/MessageResolver.java | 1 - src/test/java/blackjack/domain/DrawDecisionTest.java | 11 +++++------ src/test/java/blackjack/domain/card/CardDeckTest.java | 9 ++++----- .../java/blackjack/domain/card/CardNumberTest.java | 8 ++++---- src/test/java/blackjack/domain/player/HandTest.java | 4 ++-- .../java/blackjack/domain/player/PlayerNameTest.java | 6 +++--- src/test/java/blackjack/domain/player/PlayerTest.java | 6 +++--- .../java/blackjack/domain/player/PlayersTest.java | 11 +++++------ .../blackjack/domain/rule/DealerHitStrategyTest.java | 6 +++--- src/test/java/blackjack/domain/rule/JudgeTest.java | 4 ++-- .../blackjack/domain/rule/PlayerHitStrategyTest.java | 6 +++--- .../domain/rule/ScoreCalculateStrategyTest.java | 6 +++--- src/test/java/blackjack/domain/rule/ScoreTest.java | 6 +++--- src/test/java/fixture/CardDeckFixture.java | 1 - src/test/java/fixture/CardFixture.java | 5 ++--- src/test/java/fixture/HandFixture.java | 1 - 23 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 5994ddbb14..4819f4eb80 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -1,21 +1,24 @@ package blackjack.controller; +import static blackjack.domain.DrawDecision.YES; + import blackjack.domain.DealerGameResult; import blackjack.domain.DrawDecision; import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; import blackjack.domain.card.CardDeckCreator; -import blackjack.domain.player.*; +import blackjack.domain.player.HandCreator; +import blackjack.domain.player.Player; +import blackjack.domain.player.PlayerCreator; +import blackjack.domain.player.PlayerName; +import blackjack.domain.player.Players; import blackjack.domain.rule.Judge; import blackjack.domain.rule.Score; import blackjack.domain.rule.ScoreCalculateStrategy; import blackjack.view.InputView; import blackjack.view.OutputView; - import java.util.List; -import static blackjack.domain.DrawDecision.YES; - public class BlackJackController { private final InputView inputView; diff --git a/src/main/java/blackjack/controller/InputMapper.java b/src/main/java/blackjack/controller/InputMapper.java index 3cb440af04..d8a04a1c75 100644 --- a/src/main/java/blackjack/controller/InputMapper.java +++ b/src/main/java/blackjack/controller/InputMapper.java @@ -2,7 +2,6 @@ import blackjack.domain.DrawDecision; import blackjack.domain.player.PlayerName; - import java.util.Arrays; import java.util.List; diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index 7af1ac3555..1a42c558ca 100644 --- a/src/main/java/blackjack/domain/card/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -7,7 +7,7 @@ public class CardDeck { private final List cards; - + public CardDeck(List cards) { this.cards = cards; } diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index cc23eec94a..2e7b2f716e 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -4,7 +4,6 @@ import blackjack.domain.rule.HitStrategy; import blackjack.domain.rule.Score; import blackjack.domain.rule.ScoreCalculateStrategy; - import java.util.List; public class Hand { diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index d8c6c754c1..9475a01d6a 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -2,7 +2,6 @@ import blackjack.domain.card.Card; import blackjack.domain.rule.Score; - import java.util.Objects; public class Player { @@ -33,8 +32,12 @@ public int handSize() { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } Player player = (Player) o; return Objects.equals(name, player.name); } diff --git a/src/main/java/blackjack/domain/player/PlayerName.java b/src/main/java/blackjack/domain/player/PlayerName.java index 0dad86cfeb..b3769c7e25 100644 --- a/src/main/java/blackjack/domain/player/PlayerName.java +++ b/src/main/java/blackjack/domain/player/PlayerName.java @@ -23,8 +23,12 @@ private void validateNotEmpty(String value) { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } PlayerName that = (PlayerName) o; return Objects.equals(value, that.value); } diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index 2eefafc462..7443cf715b 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -2,7 +2,6 @@ import blackjack.domain.rule.Score; import blackjack.domain.rule.ScoreCalculateStrategy; - import java.util.List; public class Players { diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index a1197a6d79..6852cd40c7 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -6,7 +6,6 @@ import blackjack.domain.player.Player; import blackjack.domain.player.Players; import blackjack.domain.rule.Score; - import java.util.stream.Collectors; public class MessageResolver { diff --git a/src/test/java/blackjack/domain/DrawDecisionTest.java b/src/test/java/blackjack/domain/DrawDecisionTest.java index 8ffeacd8ab..bb1aa6a14c 100644 --- a/src/test/java/blackjack/domain/DrawDecisionTest.java +++ b/src/test/java/blackjack/domain/DrawDecisionTest.java @@ -1,16 +1,15 @@ package blackjack.domain; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import java.util.stream.Stream; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - @DisplayName("드로우 결정 도메인 테스트") class DrawDecisionTest { @@ -36,4 +35,4 @@ private static Stream provideEnumFromCsv() { Arguments.of("n", DrawDecision.NO) ); } -} \ No newline at end of file +} diff --git a/src/test/java/blackjack/domain/card/CardDeckTest.java b/src/test/java/blackjack/domain/card/CardDeckTest.java index 3636139d41..f58037526b 100644 --- a/src/test/java/blackjack/domain/card/CardDeckTest.java +++ b/src/test/java/blackjack/domain/card/CardDeckTest.java @@ -1,13 +1,12 @@ package blackjack.domain.card; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.ArrayList; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; @DisplayName("총 카드 덱 테스트") class CardDeckTest { diff --git a/src/test/java/blackjack/domain/card/CardNumberTest.java b/src/test/java/blackjack/domain/card/CardNumberTest.java index 01b5e7497b..0a9493f1bd 100644 --- a/src/test/java/blackjack/domain/card/CardNumberTest.java +++ b/src/test/java/blackjack/domain/card/CardNumberTest.java @@ -1,11 +1,11 @@ package blackjack.domain.card; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + @DisplayName("카드 숫자 enum 테스트") class CardNumberTest { @@ -17,4 +17,4 @@ void testIsCardNumberAce() { () -> assertThat(CardNumber.TWO.isAce()).isFalse() ); } -} \ No newline at end of file +} diff --git a/src/test/java/blackjack/domain/player/HandTest.java b/src/test/java/blackjack/domain/player/HandTest.java index 2886d83255..debc83e309 100644 --- a/src/test/java/blackjack/domain/player/HandTest.java +++ b/src/test/java/blackjack/domain/player/HandTest.java @@ -1,13 +1,13 @@ package blackjack.domain.player; +import static org.assertj.core.api.Assertions.assertThat; + import blackjack.domain.card.Card; import fixture.CardFixture; import fixture.HandFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - @DisplayName("참가자 소유 카드 테스트") class HandTest { diff --git a/src/test/java/blackjack/domain/player/PlayerNameTest.java b/src/test/java/blackjack/domain/player/PlayerNameTest.java index 562402ec14..a5ae1d0229 100644 --- a/src/test/java/blackjack/domain/player/PlayerNameTest.java +++ b/src/test/java/blackjack/domain/player/PlayerNameTest.java @@ -1,10 +1,10 @@ package blackjack.domain.player; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - @DisplayName("참가자 이름 테스트") class PlayerNameTest { @@ -15,4 +15,4 @@ void testCreatePlayerNameWithEmpty() { .isInstanceOf(IllegalArgumentException.class) .hasMessage("[ERROR] 이름이 빈 문자열입니다."); } -} \ No newline at end of file +} diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index 572944bc8e..8d0340b07e 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -1,13 +1,13 @@ package blackjack.domain.player; +import static org.assertj.core.api.Assertions.assertThat; + import blackjack.domain.card.Card; import fixture.CardFixture; import fixture.HandFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - @DisplayName("참가자 테스트") class PlayerTest { @@ -23,4 +23,4 @@ void testAppendCardToPlayer() { assertThat(hand.sum()).isEqualTo(21); } -} \ No newline at end of file +} diff --git a/src/test/java/blackjack/domain/player/PlayersTest.java b/src/test/java/blackjack/domain/player/PlayersTest.java index 55bf54dc9f..8a4ea87a37 100644 --- a/src/test/java/blackjack/domain/player/PlayersTest.java +++ b/src/test/java/blackjack/domain/player/PlayersTest.java @@ -1,14 +1,13 @@ package blackjack.domain.player; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import fixture.PlayerFixture; +import java.util.List; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - @DisplayName("참가자들 테스트") class PlayersTest { @@ -53,4 +52,4 @@ void testCreateWithValidPlayers() { assertThatCode(() -> new Players(List.of(player1, player2))) .doesNotThrowAnyException(); } -} \ No newline at end of file +} diff --git a/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java b/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java index 8ac358c9b1..2dc5eff1d0 100644 --- a/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java +++ b/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java @@ -1,12 +1,12 @@ package blackjack.domain.rule; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import static org.assertj.core.api.Assertions.assertThat; - class DealerHitStrategyTest { private DealerHitStrategy dealerHitStrategy; @@ -31,4 +31,4 @@ void testDealerShouldStay(int score) { boolean canHit = dealerHitStrategy.canHit(new Score(score)); assertThat(canHit).isFalse(); } -} \ No newline at end of file +} diff --git a/src/test/java/blackjack/domain/rule/JudgeTest.java b/src/test/java/blackjack/domain/rule/JudgeTest.java index a0dd0442e2..6327051c3b 100644 --- a/src/test/java/blackjack/domain/rule/JudgeTest.java +++ b/src/test/java/blackjack/domain/rule/JudgeTest.java @@ -1,11 +1,11 @@ package blackjack.domain.rule; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - class JudgeTest { private Judge judge; diff --git a/src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java b/src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java index 16927ab222..e632de56f3 100644 --- a/src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java +++ b/src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java @@ -1,12 +1,12 @@ package blackjack.domain.rule; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import static org.assertj.core.api.Assertions.assertThat; - class PlayerHitStrategyTest { private PlayerHitStrategy playerHitStrategy; @@ -31,4 +31,4 @@ void testDealerShouldStay(int score) { boolean canHit = playerHitStrategy.canHit(new Score(score)); assertThat(canHit).isFalse(); } -} \ No newline at end of file +} diff --git a/src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java b/src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java index 68e1489f5c..6cca764215 100644 --- a/src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java +++ b/src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java @@ -1,13 +1,13 @@ package blackjack.domain.rule; +import static org.assertj.core.api.Assertions.assertThat; + import blackjack.domain.player.Hand; import fixture.HandFixture; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - @DisplayName("점수 계산 전략 테스트") class ScoreCalculateStrategyTest { @@ -45,4 +45,4 @@ void testCalculateScoreWithMultipleAce() { Hand hand = HandFixture.of(1, 1, 1); assertThat(scoreCalculateStrategy.calculate(hand).getValue()).isEqualTo(13); } -} \ No newline at end of file +} diff --git a/src/test/java/blackjack/domain/rule/ScoreTest.java b/src/test/java/blackjack/domain/rule/ScoreTest.java index e87e73749d..25adcc506f 100644 --- a/src/test/java/blackjack/domain/rule/ScoreTest.java +++ b/src/test/java/blackjack/domain/rule/ScoreTest.java @@ -1,11 +1,11 @@ package blackjack.domain.rule; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import static org.assertj.core.api.Assertions.assertThat; - @DisplayName("점수 테스트") class ScoreTest { @@ -26,4 +26,4 @@ void testScoreCompareWithLower(int value) { Score target = new Score(value); assertThat(score.isAbove(target)).isTrue(); } -} \ No newline at end of file +} diff --git a/src/test/java/fixture/CardDeckFixture.java b/src/test/java/fixture/CardDeckFixture.java index 856ee0c0cf..f270f6f9d9 100644 --- a/src/test/java/fixture/CardDeckFixture.java +++ b/src/test/java/fixture/CardDeckFixture.java @@ -1,7 +1,6 @@ package fixture; import blackjack.domain.card.CardDeck; - import java.util.Arrays; public class CardDeckFixture { diff --git a/src/test/java/fixture/CardFixture.java b/src/test/java/fixture/CardFixture.java index 5711f6c9cf..53b0aad843 100644 --- a/src/test/java/fixture/CardFixture.java +++ b/src/test/java/fixture/CardFixture.java @@ -1,12 +1,11 @@ package fixture; +import static blackjack.domain.card.CardShape.HEART; + import blackjack.domain.card.Card; import blackjack.domain.card.CardNumber; - import java.util.Arrays; -import static blackjack.domain.card.CardShape.HEART; - public class CardFixture { public static Card from(int number) { diff --git a/src/test/java/fixture/HandFixture.java b/src/test/java/fixture/HandFixture.java index c9e3847e89..22e5f99bb4 100644 --- a/src/test/java/fixture/HandFixture.java +++ b/src/test/java/fixture/HandFixture.java @@ -3,7 +3,6 @@ import blackjack.domain.player.Hand; import blackjack.domain.rule.PlayerHitStrategy; import blackjack.domain.rule.ScoreCalculateStrategy; - import java.util.Arrays; import java.util.stream.Collectors; From 7360053288708e5685939bfb1d631da5f924a777 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 07:26:53 +0900 Subject: [PATCH 091/124] =?UTF-8?q?docs:=20=EC=B9=B4=EB=93=9C=ED=95=A9=20?= =?UTF-8?q?=EB=B0=8F=20=EC=A0=90=EC=88=98=20=EA=B4=80=EB=A0=A8=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 704f5d5b15..5a33d0f5e3 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,9 @@ - [x] 이름이 중복되는 플레이어는 존재할 수 없다. - [x] 플레이어가 없는 경우는 게임을 시작할 수 없다. - [x] 게임 참가자는 딜러 제외 3명 이하여야 한다. + - [x] 카드합을 계산할 때 Ace 카드는 1 또는 11로 계산한다 + - [x] 카드합을 계산할 떄 J,Q,K카드는 각각 10으로 계산한다. + - [x] 점수를 계산할 때 Ace 카드가 있는 경우 21을 넘지 않으면서 가장 가깝도록 유리하게 계산한다 - [x] 카드합 비교를 통해서 플레이어의 승패를 결정할 수 있다. - [x] 카드합 비교를 통해서 딜러의 승패를 계산할 수 있다. - [x] 딜러는 17점 미만이면 카드를 받아야 한다. From 2318ce5cdcaf01f56d16f4d6d026de2092d17603 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 07:30:00 +0900 Subject: [PATCH 092/124] =?UTF-8?q?refactor:=20=EC=BB=A8=ED=8A=B8=EB=A1=A4?= =?UTF-8?q?=EB=9F=AC=20=EB=A0=88=EC=9D=B4=EC=96=B4=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EB=B0=8F=20=EA=B4=80=EB=A0=A8=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 5 ++--- .../BlackJackController.java => BlackJackGame.java} | 6 +++--- src/main/java/blackjack/{controller => }/InputMapper.java | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) rename src/main/java/blackjack/{controller/BlackJackController.java => BlackJackGame.java} (97%) rename src/main/java/blackjack/{controller => }/InputMapper.java (94%) diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index a35be6e3f8..859056e6da 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -1,6 +1,5 @@ package blackjack; -import blackjack.controller.BlackJackController; import blackjack.view.InputView; import blackjack.view.MessageResolver; import blackjack.view.OutputView; @@ -12,7 +11,7 @@ public static void main(String[] args) { InputView inputView = new InputView(); OutputView outputView = new OutputView(new MessageResolver()); - BlackJackController blackJackController = new BlackJackController(inputView, outputView); - blackJackController.run(); + BlackJackGame blackJackGame = new BlackJackGame(inputView, outputView); + blackJackGame.run(); } } diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/BlackJackGame.java similarity index 97% rename from src/main/java/blackjack/controller/BlackJackController.java rename to src/main/java/blackjack/BlackJackGame.java index 4819f4eb80..d07098d407 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/BlackJackGame.java @@ -1,4 +1,4 @@ -package blackjack.controller; +package blackjack; import static blackjack.domain.DrawDecision.YES; @@ -19,12 +19,12 @@ import blackjack.view.OutputView; import java.util.List; -public class BlackJackController { +public class BlackJackGame { private final InputView inputView; private final OutputView outputView; - public BlackJackController(InputView inputView, OutputView outputView) { + public BlackJackGame(InputView inputView, OutputView outputView) { this.inputView = inputView; this.outputView = outputView; } diff --git a/src/main/java/blackjack/controller/InputMapper.java b/src/main/java/blackjack/InputMapper.java similarity index 94% rename from src/main/java/blackjack/controller/InputMapper.java rename to src/main/java/blackjack/InputMapper.java index d8a04a1c75..e6e8feb734 100644 --- a/src/main/java/blackjack/controller/InputMapper.java +++ b/src/main/java/blackjack/InputMapper.java @@ -1,4 +1,4 @@ -package blackjack.controller; +package blackjack; import blackjack.domain.DrawDecision; import blackjack.domain.player.PlayerName; From 40f1c66f57260cac88ccb68ebee265ee14eb0764 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 07:31:10 +0900 Subject: [PATCH 093/124] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=B0=B8=EC=A1=B0=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/DrawDecision.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/blackjack/domain/DrawDecision.java b/src/main/java/blackjack/domain/DrawDecision.java index 2c00051e1b..28f5ca3c64 100644 --- a/src/main/java/blackjack/domain/DrawDecision.java +++ b/src/main/java/blackjack/domain/DrawDecision.java @@ -14,7 +14,7 @@ public enum DrawDecision { } public static DrawDecision from(String code) { - return Arrays.stream(DrawDecision.values()) + return Arrays.stream(values()) .filter(drawDecision -> drawDecision.code.equals(code)) .findFirst() .orElseThrow(() -> new IllegalArgumentException("[ERROR] 해당하는 값을 찾지 못하였습니다.")); From 1379611b28658f78bd62e40e6f4743a68d75a28c Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 07:45:04 +0900 Subject: [PATCH 094/124] =?UTF-8?q?refactor:=20=EC=97=90=EB=9F=AC=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EA=B5=AC=EC=B2=B4=ED=99=94=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/DrawDecision.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/blackjack/domain/DrawDecision.java b/src/main/java/blackjack/domain/DrawDecision.java index 28f5ca3c64..3d9850e0ee 100644 --- a/src/main/java/blackjack/domain/DrawDecision.java +++ b/src/main/java/blackjack/domain/DrawDecision.java @@ -17,6 +17,6 @@ public static DrawDecision from(String code) { return Arrays.stream(values()) .filter(drawDecision -> drawDecision.code.equals(code)) .findFirst() - .orElseThrow(() -> new IllegalArgumentException("[ERROR] 해당하는 값을 찾지 못하였습니다.")); + .orElseThrow(() -> new IllegalArgumentException("[ERROR] " + YES.code + "또는 " + NO.code + "로 입력해주세요")); } } From d87d04fa76919cb870f74f8989f766f8eaf794fd Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 08:08:08 +0900 Subject: [PATCH 095/124] =?UTF-8?q?refactor:=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=20=EB=82=B4=EC=97=90=20=EB=B7=B0=EB=A1=9C=EC=A7=81=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/card/Card.java | 12 ++--- .../java/blackjack/domain/player/Hand.java | 4 +- .../java/blackjack/view/CardDescription.java | 45 +++++++++++++++++++ .../java/blackjack/view/MessageResolver.java | 9 +++- 4 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 src/main/java/blackjack/view/CardDescription.java diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index 1581d0eacd..6b4f0b8cd9 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -33,15 +33,11 @@ public int hashCode() { return Objects.hash(cardShape, cardNumber); } - public int getCardNumber() { - return cardNumber.getValue(); + public CardNumber getCardNumber() { + return cardNumber; } - public String getCardNumberName() { - return cardNumber.getName(); - } - - public String getCardShape() { - return cardShape.getName(); + public CardShape getCardShape() { + return cardShape; } } diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index 2e7b2f716e..0f0e7eb342 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -1,6 +1,7 @@ package blackjack.domain.player; import blackjack.domain.card.Card; +import blackjack.domain.card.CardNumber; import blackjack.domain.rule.HitStrategy; import blackjack.domain.rule.Score; import blackjack.domain.rule.ScoreCalculateStrategy; @@ -20,7 +21,8 @@ public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy, Hit public int sum() { return cards.stream() - .mapToInt(Card::getCardNumber) + .map(Card::getCardNumber) + .mapToInt(CardNumber::getValue) .sum(); } diff --git a/src/main/java/blackjack/view/CardDescription.java b/src/main/java/blackjack/view/CardDescription.java new file mode 100644 index 0000000000..0527ff8ee6 --- /dev/null +++ b/src/main/java/blackjack/view/CardDescription.java @@ -0,0 +1,45 @@ +package blackjack.view; + +import static blackjack.domain.card.CardNumber.ACE; +import static blackjack.domain.card.CardNumber.EIGHT; +import static blackjack.domain.card.CardNumber.FIVE; +import static blackjack.domain.card.CardNumber.FOUR; +import static blackjack.domain.card.CardNumber.JACK; +import static blackjack.domain.card.CardNumber.KING; +import static blackjack.domain.card.CardNumber.NINE; +import static blackjack.domain.card.CardNumber.QUEEN; +import static blackjack.domain.card.CardNumber.SEVEN; +import static blackjack.domain.card.CardNumber.SIX; +import static blackjack.domain.card.CardNumber.TEN; +import static blackjack.domain.card.CardNumber.THREE; +import static blackjack.domain.card.CardNumber.TWO; +import static blackjack.domain.card.CardShape.CLUB; +import static blackjack.domain.card.CardShape.DIAMOND; +import static blackjack.domain.card.CardShape.HEART; +import static blackjack.domain.card.CardShape.SPADE; + +import blackjack.domain.card.CardNumber; +import blackjack.domain.card.CardShape; +import java.util.Map; + +public class CardDescription { + + private CardDescription() { + } + + public static final Map SHAPE_NAME = Map.of( + HEART, "하트", + SPADE, "스페이드", + DIAMOND, "다이아몬드", + CLUB, "클로버" + ); + public static final Map NUMBER_NAME = Map.ofEntries( + Map.entry(ACE, "A"), Map.entry(TWO, "2"), + Map.entry(THREE, "3"), Map.entry(FOUR, "4"), + Map.entry(FIVE, "5"), Map.entry(SIX, "6"), + Map.entry(SEVEN, "7"), Map.entry(EIGHT, "8"), + Map.entry(NINE, "9"), Map.entry(TEN, "10"), + Map.entry(JACK, "J"), Map.entry(QUEEN, "Q"), + Map.entry(KING, "K") + ); +} diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index 6852cd40c7..d1760f385c 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -1,7 +1,12 @@ package blackjack.view; +import static blackjack.view.CardDescription.NUMBER_NAME; +import static blackjack.view.CardDescription.SHAPE_NAME; + import blackjack.domain.DealerGameResult; import blackjack.domain.card.Card; +import blackjack.domain.card.CardNumber; +import blackjack.domain.card.CardShape; import blackjack.domain.player.Hand; import blackjack.domain.player.Player; import blackjack.domain.player.Players; @@ -36,7 +41,9 @@ private String resolveHandMessage(Hand hand) { } private String resolveCardMessage(Card card) { - return String.format("%s%s", card.getCardNumberName(), card.getCardShape()); + CardNumber cardNumber = card.getCardNumber(); + CardShape cardShape = card.getCardShape(); + return String.format("%s%s", NUMBER_NAME.get(cardNumber), SHAPE_NAME.get(cardShape)); } public String resolveDealerHandMessage(Player dealer) { From 1d4170b44f1a3f41795ff1c697d4729552b9707c Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 08:09:37 +0900 Subject: [PATCH 096/124] =?UTF-8?q?fix:=20=EC=97=90=EB=9F=AC=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EC=9D=98?= =?UTF-8?q?=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/blackjack/domain/DrawDecisionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/blackjack/domain/DrawDecisionTest.java b/src/test/java/blackjack/domain/DrawDecisionTest.java index bb1aa6a14c..6b8486ede4 100644 --- a/src/test/java/blackjack/domain/DrawDecisionTest.java +++ b/src/test/java/blackjack/domain/DrawDecisionTest.java @@ -19,7 +19,7 @@ class DrawDecisionTest { void testEnumFromInvalidCode(String code) { assertThatThrownBy(() -> DrawDecision.from(code)) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("[ERROR] 해당하는 값을 찾지 못하였습니다."); + .hasMessage("[ERROR] y또는 n로 입력해주세요"); } @DisplayName("존재하는 코드명이면 적절한 상수를 반환받는다") From 0eb36e82d3034d7c71636bf285475487a92148e8 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 08:14:08 +0900 Subject: [PATCH 097/124] =?UTF-8?q?refactor:=20=EB=8D=B1=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EB=BD=91=EC=9D=84=20=EB=95=8C?= =?UTF-8?q?=20=EB=A7=88=EC=A7=80=EB=A7=89=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EB=BD=91=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/card/CardDeck.java | 4 ++-- src/test/java/blackjack/domain/card/CardDeckTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index 1a42c558ca..81a7245796 100644 --- a/src/main/java/blackjack/domain/card/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -7,7 +7,7 @@ public class CardDeck { private final List cards; - + public CardDeck(List cards) { this.cards = cards; } @@ -16,7 +16,7 @@ public Card popCard() { if (cards.isEmpty()) { throw new IllegalArgumentException("[ERROR] 남아있는 카드가 부족하여 카드를 뽑을 수 없습니다"); } - return cards.remove(0); + return cards.remove(cards.size() - 1); } public List popCards(int count) { diff --git a/src/test/java/blackjack/domain/card/CardDeckTest.java b/src/test/java/blackjack/domain/card/CardDeckTest.java index f58037526b..1c6c858e7f 100644 --- a/src/test/java/blackjack/domain/card/CardDeckTest.java +++ b/src/test/java/blackjack/domain/card/CardDeckTest.java @@ -25,7 +25,7 @@ void testPopCardFromDeck() { CardDeck cardDeck = new CardDeck(cards); Card popped = cardDeck.popCard(); - assertThat(popped).isEqualTo(card1); + assertThat(popped).isEqualTo(card3); } @DisplayName("덱에서 횟수만큼 카드를 뽑을 수 있다") From bbaca861be24b4570ced9298c48157bca6851492 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 13:38:18 +0900 Subject: [PATCH 098/124] =?UTF-8?q?refactor:=20=ED=8C=A9=ED=86=A0=EB=A6=AC?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=EC=97=90=EC=84=9C=20=EC=A0=95?= =?UTF-8?q?=EC=A0=81=20=ED=8C=A9=ED=86=A0=EB=A6=AC=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=EB=A1=9C=20=EB=A1=9C=EC=A7=81=20=EC=9D=91=EC=A7=91=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/BlackJackGame.java | 8 +------ .../java/blackjack/domain/card/CardDeck.java | 11 ++++++++++ .../domain/card/CardDeckCreator.java | 22 ------------------- 3 files changed, 12 insertions(+), 29 deletions(-) delete mode 100644 src/main/java/blackjack/domain/card/CardDeckCreator.java diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java index d07098d407..103958b469 100644 --- a/src/main/java/blackjack/BlackJackGame.java +++ b/src/main/java/blackjack/BlackJackGame.java @@ -6,7 +6,6 @@ import blackjack.domain.DrawDecision; import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; -import blackjack.domain.card.CardDeckCreator; import blackjack.domain.player.HandCreator; import blackjack.domain.player.Player; import blackjack.domain.player.PlayerCreator; @@ -30,7 +29,7 @@ public BlackJackGame(InputView inputView, OutputView outputView) { } public void run() { - CardDeck cardDeck = initCardDeck(); + CardDeck cardDeck = CardDeck.createShuffledDeck(); Players players = initPlayers(cardDeck); Player dealer = initDealer(cardDeck); printPlayersInformation(players, dealer); @@ -46,11 +45,6 @@ public void run() { printPlayersGameResult(players, dealer); } - private CardDeck initCardDeck() { - CardDeckCreator cardDeckCreator = new CardDeckCreator(); - return cardDeckCreator.create(); - } - private Players initPlayers(CardDeck cardDeck) { InputMapper inputMapper = new InputMapper(); PlayerCreator playerCreator = new PlayerCreator(new HandCreator()); diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index 81a7245796..8b258ac885 100644 --- a/src/main/java/blackjack/domain/card/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -1,5 +1,7 @@ package blackjack.domain.card; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -12,6 +14,15 @@ public CardDeck(List cards) { this.cards = cards; } + public static CardDeck createShuffledDeck() { + List cards = Arrays.stream(CardShape.values()) + .flatMap(shape -> Arrays.stream(CardNumber.values()) + .map(number -> new Card(shape, number))) + .collect(Collectors.toList()); + Collections.shuffle(cards); + return new CardDeck(cards); + } + public Card popCard() { if (cards.isEmpty()) { throw new IllegalArgumentException("[ERROR] 남아있는 카드가 부족하여 카드를 뽑을 수 없습니다"); diff --git a/src/main/java/blackjack/domain/card/CardDeckCreator.java b/src/main/java/blackjack/domain/card/CardDeckCreator.java deleted file mode 100644 index 62ee8a30af..0000000000 --- a/src/main/java/blackjack/domain/card/CardDeckCreator.java +++ /dev/null @@ -1,22 +0,0 @@ -package blackjack.domain.card; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -public class CardDeckCreator { - - public CardDeck create() { - List cards = createCards(CardShape.values(), CardNumber.values()); - Collections.shuffle(cards); - return new CardDeck(cards); - } - - private List createCards(CardShape[] cardShapes, CardNumber[] cardNumbers) { - return Arrays.stream(cardShapes) - .flatMap(shape -> Arrays.stream(cardNumbers) - .map(number -> new Card(shape, number))) - .collect(Collectors.toList()); - } -} From 03c7006573968cde883d13338b9ba065d5187630 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 13:43:12 +0900 Subject: [PATCH 099/124] =?UTF-8?q?style:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=AA=85=20=EA=B5=AC=EC=B2=B4=ED=99=94=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Hand.java | 4 ++-- src/main/java/blackjack/domain/player/Player.java | 2 +- .../java/blackjack/domain/rule/ScoreCalculateStrategy.java | 2 +- src/test/java/blackjack/domain/player/HandTest.java | 4 ++-- src/test/java/blackjack/domain/player/PlayerTest.java | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index 0f0e7eb342..04ecf299c6 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -19,14 +19,14 @@ public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy, Hit this.hitStrategy = hitStrategy; } - public int sum() { + public int calculateCardSummation() { return cards.stream() .map(Card::getCardNumber) .mapToInt(CardNumber::getValue) .sum(); } - public void append(Card card) { + public void appendCard(Card card) { cards.add(card); } diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index 9475a01d6a..f363d3ecf4 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -15,7 +15,7 @@ public Player(PlayerName name, Hand hand) { } public void appendCard(Card card) { - hand.append(card); + hand.appendCard(card); } public Score calculateHandScore() { diff --git a/src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java b/src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java index c83a917ead..31dd4f4696 100644 --- a/src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java +++ b/src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java @@ -9,7 +9,7 @@ public class ScoreCalculateStrategy { public Score calculate(Hand hand) { int aceCount = hand.countAce(); - int sum = hand.sum(); + int sum = hand.calculateCardSummation(); while (aceCount > 0 && (sum + ACE_WEIGHT) <= BLACK_JACK) { sum += ACE_WEIGHT; aceCount--; diff --git a/src/test/java/blackjack/domain/player/HandTest.java b/src/test/java/blackjack/domain/player/HandTest.java index debc83e309..0f4a4e09b9 100644 --- a/src/test/java/blackjack/domain/player/HandTest.java +++ b/src/test/java/blackjack/domain/player/HandTest.java @@ -15,7 +15,7 @@ class HandTest { @Test void testHandSummation() { Hand hand = HandFixture.of(2, 2, 2); - int expected = hand.sum(); + int expected = hand.calculateCardSummation(); assertThat(expected).isEqualTo(6); } @@ -25,7 +25,7 @@ void testHandSummation() { void testAppendCard() { Card card = CardFixture.from(2); Hand hand = HandFixture.of(); - hand.append(card); + hand.appendCard(card); assertThat(hand.getCards()).containsExactly(card); } diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index 8d0340b07e..2c8ad5aee0 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -21,6 +21,6 @@ void testAppendCardToPlayer() { Card card = CardFixture.from(2); player.appendCard(card); - assertThat(hand.sum()).isEqualTo(21); + assertThat(hand.calculateCardSummation()).isEqualTo(21); } } From cd39577e0b9fc0b68850b5c983893371774e46d2 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 13:46:36 +0900 Subject: [PATCH 100/124] =?UTF-8?q?refactor:=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=20=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20?= =?UTF-8?q?=ED=86=B5=ED=95=A9=20=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/PlayerName.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/blackjack/domain/player/PlayerName.java b/src/main/java/blackjack/domain/player/PlayerName.java index b3769c7e25..675d9d6d1b 100644 --- a/src/main/java/blackjack/domain/player/PlayerName.java +++ b/src/main/java/blackjack/domain/player/PlayerName.java @@ -7,12 +7,8 @@ public class PlayerName { private final String value; public PlayerName(String value) { - validate(value); - this.value = value; - } - - private void validate(String value) { validateNotEmpty(value); + this.value = value; } private void validateNotEmpty(String value) { From 2d1e5681cefd432ad365dd7f4fba93d2dc6f0236 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 13:51:43 +0900 Subject: [PATCH 101/124] =?UTF-8?q?test:=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=86=8C=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20CsvSource?= =?UTF-8?q?=EB=A1=9C=20=EB=8B=A8=EC=88=9C=ED=99=94=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/DrawDecisionTest.java | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/test/java/blackjack/domain/DrawDecisionTest.java b/src/test/java/blackjack/domain/DrawDecisionTest.java index 6b8486ede4..c5d6dd4576 100644 --- a/src/test/java/blackjack/domain/DrawDecisionTest.java +++ b/src/test/java/blackjack/domain/DrawDecisionTest.java @@ -3,11 +3,9 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import java.util.stream.Stream; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; @DisplayName("드로우 결정 도메인 테스트") @@ -24,15 +22,8 @@ void testEnumFromInvalidCode(String code) { @DisplayName("존재하는 코드명이면 적절한 상수를 반환받는다") @ParameterizedTest - @MethodSource("provideEnumFromCsv") + @CsvSource(value = {"y, YES", "n, NO",}) void testEnumFromValidCode(String code, DrawDecision drawDecision) { assertThat(DrawDecision.from(code)).isEqualTo(drawDecision); } - - private static Stream provideEnumFromCsv() { - return Stream.of( - Arguments.of("y", DrawDecision.YES), - Arguments.of("n", DrawDecision.NO) - ); - } } From 2b52beeb3020b60e322d8061c12a015cb20213af Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 14:26:41 +0900 Subject: [PATCH 102/124] =?UTF-8?q?style:=20Player=20->=20Participant=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/BlackJackGame.java | 32 +++++++------- .../player/{Player.java => Participant.java} | 8 ++-- .../domain/player/PlayerCreator.java | 8 ++-- .../java/blackjack/domain/player/Players.java | 42 +++++++++---------- .../java/blackjack/view/MessageResolver.java | 18 ++++---- src/main/java/blackjack/view/OutputView.java | 16 +++---- ...NameTest.java => ParticipantNameTest.java} | 2 +- .../{PlayerTest.java => ParticipantTest.java} | 6 +-- .../blackjack/domain/player/PlayersTest.java | 22 +++++----- ...t.java => ParticipantHitStrategyTest.java} | 2 +- src/test/java/fixture/PlayerFixture.java | 6 +-- 11 files changed, 81 insertions(+), 81 deletions(-) rename src/main/java/blackjack/domain/player/{Player.java => Participant.java} (83%) rename src/test/java/blackjack/domain/player/{PlayerNameTest.java => ParticipantNameTest.java} (94%) rename src/test/java/blackjack/domain/player/{PlayerTest.java => ParticipantTest.java} (82%) rename src/test/java/blackjack/domain/rule/{PlayerHitStrategyTest.java => ParticipantHitStrategyTest.java} (96%) diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java index 103958b469..74072d8c54 100644 --- a/src/main/java/blackjack/BlackJackGame.java +++ b/src/main/java/blackjack/BlackJackGame.java @@ -7,7 +7,7 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; import blackjack.domain.player.HandCreator; -import blackjack.domain.player.Player; +import blackjack.domain.player.Participant; import blackjack.domain.player.PlayerCreator; import blackjack.domain.player.PlayerName; import blackjack.domain.player.Players; @@ -31,7 +31,7 @@ public BlackJackGame(InputView inputView, OutputView outputView) { public void run() { CardDeck cardDeck = CardDeck.createShuffledDeck(); Players players = initPlayers(cardDeck); - Player dealer = initDealer(cardDeck); + Participant dealer = initDealer(cardDeck); printPlayersInformation(players, dealer); completePlayersHand(players, cardDeck); @@ -55,12 +55,12 @@ private Players initPlayers(CardDeck cardDeck) { .toList()); } - private Player initDealer(CardDeck cardDeck) { + private Participant initDealer(CardDeck cardDeck) { PlayerCreator playerCreator = new PlayerCreator(new HandCreator()); return playerCreator.createDealerFrom(cardDeck); } - private void printPlayersInformation(Players players, Player dealer) { + private void printPlayersInformation(Players players, Participant dealer) { outputView.printHandOutEvent(players, 2); outputView.printDealerInitialHand(dealer); players.getPlayers().forEach(outputView::printPlayerHand); @@ -70,26 +70,26 @@ private void completePlayersHand(Players players, CardDeck cardDeck) { players.getPlayers().forEach(player -> completePlayerHand(player, cardDeck)); } - private void completePlayerHand(Player player, CardDeck cardDeck) { - while (player.canHit() && readHitDecision(player) == YES) { - player.appendCard(cardDeck.popCard()); - outputView.printPlayerHand(player); + private void completePlayerHand(Participant participant, CardDeck cardDeck) { + while (participant.canHit() && readHitDecision(participant) == YES) { + participant.appendCard(cardDeck.popCard()); + outputView.printPlayerHand(participant); } } - private DrawDecision readHitDecision(Player player) { + private DrawDecision readHitDecision(Participant participant) { InputMapper inputMapper = new InputMapper(); - return inputMapper.mapToDrawDecision(inputView.readDrawPlan(player.getName())); + return inputMapper.mapToDrawDecision(inputView.readDrawPlan(participant.getName())); } - private void completeDealerHand(Player dealer, CardDeck cardDeck) { + private void completeDealerHand(Participant dealer, CardDeck cardDeck) { while (dealer.canHit()) { Card card = cardDeck.popCard(); dealer.appendCard(card); } } - private void printDealerPopCount(Player dealer) { + private void printDealerPopCount(Participant dealer) { int dealerPopCount = dealer.handSize() - 2; if (dealerPopCount > 0) { outputView.printDealerPopCount(16, dealerPopCount); @@ -100,11 +100,11 @@ private void printPlayersScore(Players players) { players.getPlayers().forEach(this::printPlayerScore); } - private void printPlayerScore(Player player) { - outputView.printPlayerScore(player, player.calculateHandScore()); + private void printPlayerScore(Participant participant) { + outputView.printPlayerScore(participant, participant.calculateHandScore()); } - private void printDealerGameResult(Player dealer, Players players) { + private void printDealerGameResult(Participant dealer, Players players) { Score dealerScore = dealer.calculateHandScore(); int playerWinCount = players.countPlayerWithScoreAbove(dealerScore, new ScoreCalculateStrategy()); int dealerWinCount = players.countPlayer() - playerWinCount; @@ -113,7 +113,7 @@ private void printDealerGameResult(Player dealer, Players players) { outputView.printDealerGameResult(dealerGameResult); } - private void printPlayersGameResult(Players players, Player dealer) { + private void printPlayersGameResult(Players players, Participant dealer) { Judge judge = new Judge(); Score dealerScore = dealer.calculateHandScore(); players.getPlayers() diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Participant.java similarity index 83% rename from src/main/java/blackjack/domain/player/Player.java rename to src/main/java/blackjack/domain/player/Participant.java index f363d3ecf4..e1704faaad 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Participant.java @@ -4,12 +4,12 @@ import blackjack.domain.rule.Score; import java.util.Objects; -public class Player { +public class Participant { private final PlayerName name; private final Hand hand; - public Player(PlayerName name, Hand hand) { + public Participant(PlayerName name, Hand hand) { this.name = name; this.hand = hand; } @@ -38,8 +38,8 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) { return false; } - Player player = (Player) o; - return Objects.equals(name, player.name); + Participant participant = (Participant) o; + return Objects.equals(name, participant.name); } @Override diff --git a/src/main/java/blackjack/domain/player/PlayerCreator.java b/src/main/java/blackjack/domain/player/PlayerCreator.java index f0b6100960..815d53715d 100644 --- a/src/main/java/blackjack/domain/player/PlayerCreator.java +++ b/src/main/java/blackjack/domain/player/PlayerCreator.java @@ -12,11 +12,11 @@ public PlayerCreator(HandCreator handCreator) { this.handCreator = handCreator; } - public Player createPlayerFrom(PlayerName playerName, CardDeck cardDeck) { - return new Player(playerName, handCreator.createPlayerHandFrom(cardDeck)); + public Participant createPlayerFrom(PlayerName playerName, CardDeck cardDeck) { + return new Participant(playerName, handCreator.createPlayerHandFrom(cardDeck)); } - public Player createDealerFrom(CardDeck cardDeck) { - return new Player(new PlayerName(DEALER_NAME), handCreator.createDealerHandFrom(cardDeck)); + public Participant createDealerFrom(CardDeck cardDeck) { + return new Participant(new PlayerName(DEALER_NAME), handCreator.createDealerHandFrom(cardDeck)); } } diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index 7443cf715b..757d794f77 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -8,55 +8,55 @@ public class Players { private static final int MAX_COUNT = 3; - private final List players; + private final List participants; - public Players(List players) { - validate(players); - this.players = players; + public Players(List participants) { + validate(participants); + this.participants = participants; } public int countPlayer() { - return players.size(); + return participants.size(); } public int countPlayerWithScoreAbove(Score target, ScoreCalculateStrategy scoreCalculateStrategy) { - return (int) players.stream() - .map(Player::calculateHandScore) + return (int) participants.stream() + .map(Participant::calculateHandScore) .filter(playerScore -> playerScore.isAbove(target)) .count(); } - private void validate(List players) { - validateEachPlayerNameUnique(players); - validateEntryNotEmpty(players); - validatePlayerCountRange(players); + private void validate(List participants) { + validateEachPlayerNameUnique(participants); + validateEntryNotEmpty(participants); + validatePlayerCountRange(participants); } - private void validateEachPlayerNameUnique(List players) { - if (countUniquePlayer(players) != players.size()) { + private void validateEachPlayerNameUnique(List participants) { + if (countUniquePlayer(participants) != participants.size()) { throw new IllegalArgumentException("[ERROR] 중복되는 플레이어의 이름이 존재합니다"); } } - private int countUniquePlayer(List players) { - return (int) players.stream() + private int countUniquePlayer(List participants) { + return (int) participants.stream() .distinct() .count(); } - private void validateEntryNotEmpty(List players) { - if (players.isEmpty()) { + private void validateEntryNotEmpty(List participants) { + if (participants.isEmpty()) { throw new IllegalArgumentException("[ERROR] 플레이어가 없습니다"); } } - private void validatePlayerCountRange(List players) { - if (players.size() > MAX_COUNT) { + private void validatePlayerCountRange(List participants) { + if (participants.size() > MAX_COUNT) { throw new IllegalArgumentException("[ERROR] 플레이어의 수는 " + MAX_COUNT + "이하여야 합니다"); } } - public List getPlayers() { - return players; + public List getPlayers() { + return participants; } } diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index d1760f385c..1679b36d27 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -8,7 +8,7 @@ import blackjack.domain.card.CardNumber; import blackjack.domain.card.CardShape; import blackjack.domain.player.Hand; -import blackjack.domain.player.Player; +import blackjack.domain.player.Participant; import blackjack.domain.player.Players; import blackjack.domain.rule.Score; import java.util.stream.Collectors; @@ -26,12 +26,12 @@ public String resolveHandOutEventMessage(Players players, int handOutCount) { private String resolveNamesMessage(Players players) { return players.getPlayers().stream() - .map(Player::getName) + .map(Participant::getName) .collect(Collectors.joining(SEPARATOR)); } - public String resolvePlayerHandMessage(Player player) { - return String.format("%s 카드: %s", player.getName(), resolveHandMessage(player.getHand())); + public String resolvePlayerHandMessage(Participant participant) { + return String.format("%s 카드: %s", participant.getName(), resolveHandMessage(participant.getHand())); } private String resolveHandMessage(Hand hand) { @@ -46,7 +46,7 @@ private String resolveCardMessage(Card card) { return String.format("%s%s", NUMBER_NAME.get(cardNumber), SHAPE_NAME.get(cardShape)); } - public String resolveDealerHandMessage(Player dealer) { + public String resolveDealerHandMessage(Participant dealer) { Card card = dealer.getHand().getCards().get(0); return String.format("%s: %s", dealer.getName(), resolveCardMessage(card)); } @@ -56,13 +56,13 @@ public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount return String.join("", LINE_SEPARATOR, message, LINE_SEPARATOR); } - public String resolvePlayerScoreMessage(Player player, Score score) { - String handMessage = resolvePlayerHandMessage(player); + public String resolvePlayerScoreMessage(Participant participant, Score score) { + String handMessage = resolvePlayerHandMessage(participant); return String.format("%s - 결과: %d", handMessage, score.getValue()); } - public String resolvePlayerGameResult(Player player, boolean win) { - return String.format("%s: %s", player.getName(), resolveGameResultMessage(win)); + public String resolvePlayerGameResult(Participant participant, boolean win) { + return String.format("%s: %s", participant.getName(), resolveGameResultMessage(win)); } private String resolveGameResultMessage(boolean win) { diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 808813ee84..1e09941a90 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,7 +1,7 @@ package blackjack.view; import blackjack.domain.DealerGameResult; -import blackjack.domain.player.Player; +import blackjack.domain.player.Participant; import blackjack.domain.player.Players; import blackjack.domain.rule.Score; @@ -17,27 +17,27 @@ public void printHandOutEvent(Players players, int handedCount) { System.out.println(messageResolver.resolveHandOutEventMessage(players, handedCount)); } - public void printPlayerHand(Player player) { - System.out.println(messageResolver.resolvePlayerHandMessage(player)); + public void printPlayerHand(Participant participant) { + System.out.println(messageResolver.resolvePlayerHandMessage(participant)); } public void printDealerPopCount(int dealerPopThreshold, int count) { System.out.println(messageResolver.resolveDealerPopCountMessage(dealerPopThreshold, count)); } - public void printPlayerScore(Player player, Score score) { - System.out.println(messageResolver.resolvePlayerScoreMessage(player, score)); + public void printPlayerScore(Participant participant, Score score) { + System.out.println(messageResolver.resolvePlayerScoreMessage(participant, score)); } - public void printPlayerGameResult(Player player, boolean win) { - System.out.println(messageResolver.resolvePlayerGameResult(player, win)); + public void printPlayerGameResult(Participant participant, boolean win) { + System.out.println(messageResolver.resolvePlayerGameResult(participant, win)); } public void printDealerGameResult(DealerGameResult dealerGameResult) { System.out.println(messageResolver.resolveDealerGameResult(dealerGameResult)); } - public void printDealerInitialHand(Player dealer) { + public void printDealerInitialHand(Participant dealer) { System.out.println(messageResolver.resolveDealerHandMessage(dealer)); } } diff --git a/src/test/java/blackjack/domain/player/PlayerNameTest.java b/src/test/java/blackjack/domain/player/ParticipantNameTest.java similarity index 94% rename from src/test/java/blackjack/domain/player/PlayerNameTest.java rename to src/test/java/blackjack/domain/player/ParticipantNameTest.java index a5ae1d0229..e2f523e93f 100644 --- a/src/test/java/blackjack/domain/player/PlayerNameTest.java +++ b/src/test/java/blackjack/domain/player/ParticipantNameTest.java @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test; @DisplayName("참가자 이름 테스트") -class PlayerNameTest { +class ParticipantNameTest { @DisplayName("이름은 빈 문자열일 수 없다") @Test diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/ParticipantTest.java similarity index 82% rename from src/test/java/blackjack/domain/player/PlayerTest.java rename to src/test/java/blackjack/domain/player/ParticipantTest.java index 2c8ad5aee0..e4c9112a00 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/ParticipantTest.java @@ -9,17 +9,17 @@ import org.junit.jupiter.api.Test; @DisplayName("참가자 테스트") -class PlayerTest { +class ParticipantTest { @DisplayName("플레이어 핸드에 카드를 추가할 수 있다") @Test void testAppendCardToPlayer() { PlayerName playerName = new PlayerName("pobi"); Hand hand = HandFixture.of(10, 9); - Player player = new Player(playerName, hand); + Participant participant = new Participant(playerName, hand); Card card = CardFixture.from(2); - player.appendCard(card); + participant.appendCard(card); assertThat(hand.calculateCardSummation()).isEqualTo(21); } diff --git a/src/test/java/blackjack/domain/player/PlayersTest.java b/src/test/java/blackjack/domain/player/PlayersTest.java index 8a4ea87a37..e81c43535a 100644 --- a/src/test/java/blackjack/domain/player/PlayersTest.java +++ b/src/test/java/blackjack/domain/player/PlayersTest.java @@ -14,10 +14,10 @@ class PlayersTest { @DisplayName("참가자들 중 이름이 중복되는 경우는 생성 검증에 실패한다") @Test void testCreatePlayersWithDuplicateNames() { - Player player1 = PlayerFixture.of("리비", 1, 2); - Player player2 = PlayerFixture.of("리비", 3, 4); + Participant participant1 = PlayerFixture.of("리비", 1, 2); + Participant participant2 = PlayerFixture.of("리비", 3, 4); - assertThatThrownBy(() -> new Players(List.of(player1, player2))) + assertThatThrownBy(() -> new Players(List.of(participant1, participant2))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("[ERROR] 중복되는 플레이어의 이름이 존재합니다"); } @@ -33,12 +33,12 @@ void testCreatePlayersWithEmptyEntry() { @DisplayName("딜러를 제외한 게임 참여자가 3명을 넘는 경우는 생성 검증에 실패한다") @Test void testCreatePlayersWithInvalidEntryCount() { - Player player1 = PlayerFixture.of("리비", 1, 2); - Player player2 = PlayerFixture.of("제리", 3, 4); - Player player3 = PlayerFixture.of("잉크", 1, 2); - Player player4 = PlayerFixture.of("트레", 3, 4); + Participant participant1 = PlayerFixture.of("리비", 1, 2); + Participant participant2 = PlayerFixture.of("제리", 3, 4); + Participant participant3 = PlayerFixture.of("잉크", 1, 2); + Participant participant4 = PlayerFixture.of("트레", 3, 4); - assertThatThrownBy(() -> new Players(List.of(player1, player2, player3, player4))) + assertThatThrownBy(() -> new Players(List.of(participant1, participant2, participant3, participant4))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("[ERROR] 플레이어의 수는 3이하여야 합니다"); } @@ -46,10 +46,10 @@ void testCreatePlayersWithInvalidEntryCount() { @DisplayName("생성 검증을 모두 통과하면 생성에 성공한다") @Test void testCreateWithValidPlayers() { - Player player1 = PlayerFixture.of("리비", 1, 2); - Player player2 = PlayerFixture.of("제리", 3, 4); + Participant participant1 = PlayerFixture.of("리비", 1, 2); + Participant participant2 = PlayerFixture.of("제리", 3, 4); - assertThatCode(() -> new Players(List.of(player1, player2))) + assertThatCode(() -> new Players(List.of(participant1, participant2))) .doesNotThrowAnyException(); } } diff --git a/src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java b/src/test/java/blackjack/domain/rule/ParticipantHitStrategyTest.java similarity index 96% rename from src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java rename to src/test/java/blackjack/domain/rule/ParticipantHitStrategyTest.java index e632de56f3..d13cd1a893 100644 --- a/src/test/java/blackjack/domain/rule/PlayerHitStrategyTest.java +++ b/src/test/java/blackjack/domain/rule/ParticipantHitStrategyTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -class PlayerHitStrategyTest { +class ParticipantHitStrategyTest { private PlayerHitStrategy playerHitStrategy; diff --git a/src/test/java/fixture/PlayerFixture.java b/src/test/java/fixture/PlayerFixture.java index 5d8ddbd0eb..2e883cf302 100644 --- a/src/test/java/fixture/PlayerFixture.java +++ b/src/test/java/fixture/PlayerFixture.java @@ -1,11 +1,11 @@ package fixture; -import blackjack.domain.player.Player; +import blackjack.domain.player.Participant; import blackjack.domain.player.PlayerName; public class PlayerFixture { - public static Player of(String name, int... hand) { - return new Player(new PlayerName(name), HandFixture.of(hand)); + public static Participant of(String name, int... hand) { + return new Participant(new PlayerName(name), HandFixture.of(hand)); } } From 8c1c3c0097be2c62f625950b5fcd2e91cd995ec3 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 14:50:33 +0900 Subject: [PATCH 103/124] =?UTF-8?q?refactor:=20=EC=83=81=EC=86=8D=EC=9D=84?= =?UTF-8?q?=20=EC=9C=84=ED=95=9C=20=EC=86=8D=EC=84=B1=20=EC=A0=91=EA=B7=BC?= =?UTF-8?q?=EC=A0=9C=EC=96=B4=EC=9E=90=20=EC=88=98=EC=A0=95=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Participant.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Participant.java b/src/main/java/blackjack/domain/player/Participant.java index e1704faaad..eda26a9541 100644 --- a/src/main/java/blackjack/domain/player/Participant.java +++ b/src/main/java/blackjack/domain/player/Participant.java @@ -6,8 +6,8 @@ public class Participant { - private final PlayerName name; - private final Hand hand; + protected final PlayerName name; + protected final Hand hand; public Participant(PlayerName name, Hand hand) { this.name = name; From af26e10d837e24ed30d5c5d5e92c69023428805c Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 14:50:53 +0900 Subject: [PATCH 104/124] =?UTF-8?q?feat:=20Player=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/player/Player.java | 15 ++++++++++ .../blackjack/domain/player/PlayerTest.java | 28 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/main/java/blackjack/domain/player/Player.java create mode 100644 src/test/java/blackjack/domain/player/PlayerTest.java diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java new file mode 100644 index 0000000000..be9461db70 --- /dev/null +++ b/src/main/java/blackjack/domain/player/Player.java @@ -0,0 +1,15 @@ +package blackjack.domain.player; + +public class Player extends Participant { + + public static final int HIT_THRESHOLD = 21; + + public Player(PlayerName name, Hand hand) { + super(name, hand); + } + + @Override + public boolean canHit() { + return hand.calculateCardSummation() <= HIT_THRESHOLD; + } +} diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java new file mode 100644 index 0000000000..ac2a9a2e0f --- /dev/null +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -0,0 +1,28 @@ +package blackjack.domain.player; + +import static org.assertj.core.api.Assertions.assertThat; + +import fixture.HandFixture; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +@DisplayName("블랙잭 플레이어 테스트") +class PlayerTest { + + @DisplayName("플레이어는 21이 넘으면 히트할 수 없다") + @ParameterizedTest + @CsvSource(value = {"2, 10, 10", "3, 10, 10", "4, 10, 10"}) + void testCannotHit(int card1, int card2, int card3) { + Player player = new Player(new PlayerName("썬"), HandFixture.of(card1, card2, card3)); + assertThat(player.canHit()).isFalse(); + } + + @DisplayName("플레이어는 21이 넘으면 히트할 수 없다") + @ParameterizedTest + @CsvSource(value = {"1, 10, 10", "2, 8, 10", "2, 7, 10"}) + void testCanHit(int card1, int card2, int card3) { + Player player = new Player(new PlayerName("썬"), HandFixture.of(card1, card2, card3)); + assertThat(player.canHit()).isTrue(); + } +} From b4e10f832158925e7062c868cfc4c7696c9c0f59 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 14:58:29 +0900 Subject: [PATCH 105/124] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/player/Dealer.java | 16 +++++++++++ .../blackjack/domain/player/DealerTest.java | 28 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/blackjack/domain/player/Dealer.java create mode 100644 src/test/java/blackjack/domain/player/DealerTest.java diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java new file mode 100644 index 0000000000..1e4bcd6d13 --- /dev/null +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -0,0 +1,16 @@ +package blackjack.domain.player; + +public class Dealer extends Participant { + + private static final String DEALER_NAME = "딜러"; + private static final int HIT_THRESHOLD = 16; + + public Dealer(Hand hand) { + super(new PlayerName(DEALER_NAME), hand); + } + + @Override + public boolean canHit() { + return hand.calculateCardSummation() <= HIT_THRESHOLD; + } +} diff --git a/src/test/java/blackjack/domain/player/DealerTest.java b/src/test/java/blackjack/domain/player/DealerTest.java new file mode 100644 index 0000000000..6202fc84f6 --- /dev/null +++ b/src/test/java/blackjack/domain/player/DealerTest.java @@ -0,0 +1,28 @@ +package blackjack.domain.player; + +import static org.assertj.core.api.Assertions.assertThat; + +import fixture.HandFixture; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +@DisplayName("딜러 테스트") +class DealerTest { + + @DisplayName("딜러는 16점 이하이면 카드를 받을 수 있다") + @ParameterizedTest + @CsvSource(value = {"6, 10", "5, 10", "4, 10"}) + void testCannotHit(int card1, int card2) { + Dealer dealer = new Dealer(HandFixture.of(card1, card2)); + assertThat(dealer.canHit()).isTrue(); + } + + @DisplayName("딜러는 17점 이상이면 카드를 받을 수 없다") + @ParameterizedTest + @CsvSource(value = {"7, 10", "8, 10", "9, 10"}) + void testCanHit(int card1, int card2) { + Dealer dealer = new Dealer(HandFixture.of(card1, card2)); + assertThat(dealer.canHit()).isFalse(); + } +} From 9e56e1d91a7c4641417451b520b96ae44e4666bb Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 15:12:41 +0900 Subject: [PATCH 106/124] =?UTF-8?q?refactor:=20=EC=83=81=EC=86=8D=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EC=9D=BC?= =?UTF-8?q?=EA=B8=89=EC=BB=AC=EB=A0=89=EC=85=98=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/player/PlayerCreator.java | 8 +++--- .../java/blackjack/domain/player/Players.java | 26 +++++++++---------- .../blackjack/domain/player/PlayersTest.java | 22 ++++++++-------- src/test/java/fixture/PlayerFixture.java | 6 ++--- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/main/java/blackjack/domain/player/PlayerCreator.java b/src/main/java/blackjack/domain/player/PlayerCreator.java index 815d53715d..35ba8b3ce8 100644 --- a/src/main/java/blackjack/domain/player/PlayerCreator.java +++ b/src/main/java/blackjack/domain/player/PlayerCreator.java @@ -12,11 +12,11 @@ public PlayerCreator(HandCreator handCreator) { this.handCreator = handCreator; } - public Participant createPlayerFrom(PlayerName playerName, CardDeck cardDeck) { - return new Participant(playerName, handCreator.createPlayerHandFrom(cardDeck)); + public Player createPlayerFrom(PlayerName playerName, CardDeck cardDeck) { + return new Player(playerName, handCreator.createPlayerHandFrom(cardDeck)); } - public Participant createDealerFrom(CardDeck cardDeck) { - return new Participant(new PlayerName(DEALER_NAME), handCreator.createDealerHandFrom(cardDeck)); + public Dealer createDealerFrom(CardDeck cardDeck) { + return new Dealer(handCreator.createDealerHandFrom(cardDeck)); } } diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index 757d794f77..e5e5620b3e 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -8,55 +8,55 @@ public class Players { private static final int MAX_COUNT = 3; - private final List participants; + private final List players; - public Players(List participants) { - validate(participants); - this.participants = participants; + public Players(List players) { + validate(players); + this.players = players; } public int countPlayer() { - return participants.size(); + return players.size(); } public int countPlayerWithScoreAbove(Score target, ScoreCalculateStrategy scoreCalculateStrategy) { - return (int) participants.stream() + return (int) players.stream() .map(Participant::calculateHandScore) .filter(playerScore -> playerScore.isAbove(target)) .count(); } - private void validate(List participants) { + private void validate(List participants) { validateEachPlayerNameUnique(participants); validateEntryNotEmpty(participants); validatePlayerCountRange(participants); } - private void validateEachPlayerNameUnique(List participants) { + private void validateEachPlayerNameUnique(List participants) { if (countUniquePlayer(participants) != participants.size()) { throw new IllegalArgumentException("[ERROR] 중복되는 플레이어의 이름이 존재합니다"); } } - private int countUniquePlayer(List participants) { + private int countUniquePlayer(List participants) { return (int) participants.stream() .distinct() .count(); } - private void validateEntryNotEmpty(List participants) { + private void validateEntryNotEmpty(List participants) { if (participants.isEmpty()) { throw new IllegalArgumentException("[ERROR] 플레이어가 없습니다"); } } - private void validatePlayerCountRange(List participants) { + private void validatePlayerCountRange(List participants) { if (participants.size() > MAX_COUNT) { throw new IllegalArgumentException("[ERROR] 플레이어의 수는 " + MAX_COUNT + "이하여야 합니다"); } } - public List getPlayers() { - return participants; + public List getPlayers() { + return players; } } diff --git a/src/test/java/blackjack/domain/player/PlayersTest.java b/src/test/java/blackjack/domain/player/PlayersTest.java index e81c43535a..8a4ea87a37 100644 --- a/src/test/java/blackjack/domain/player/PlayersTest.java +++ b/src/test/java/blackjack/domain/player/PlayersTest.java @@ -14,10 +14,10 @@ class PlayersTest { @DisplayName("참가자들 중 이름이 중복되는 경우는 생성 검증에 실패한다") @Test void testCreatePlayersWithDuplicateNames() { - Participant participant1 = PlayerFixture.of("리비", 1, 2); - Participant participant2 = PlayerFixture.of("리비", 3, 4); + Player player1 = PlayerFixture.of("리비", 1, 2); + Player player2 = PlayerFixture.of("리비", 3, 4); - assertThatThrownBy(() -> new Players(List.of(participant1, participant2))) + assertThatThrownBy(() -> new Players(List.of(player1, player2))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("[ERROR] 중복되는 플레이어의 이름이 존재합니다"); } @@ -33,12 +33,12 @@ void testCreatePlayersWithEmptyEntry() { @DisplayName("딜러를 제외한 게임 참여자가 3명을 넘는 경우는 생성 검증에 실패한다") @Test void testCreatePlayersWithInvalidEntryCount() { - Participant participant1 = PlayerFixture.of("리비", 1, 2); - Participant participant2 = PlayerFixture.of("제리", 3, 4); - Participant participant3 = PlayerFixture.of("잉크", 1, 2); - Participant participant4 = PlayerFixture.of("트레", 3, 4); + Player player1 = PlayerFixture.of("리비", 1, 2); + Player player2 = PlayerFixture.of("제리", 3, 4); + Player player3 = PlayerFixture.of("잉크", 1, 2); + Player player4 = PlayerFixture.of("트레", 3, 4); - assertThatThrownBy(() -> new Players(List.of(participant1, participant2, participant3, participant4))) + assertThatThrownBy(() -> new Players(List.of(player1, player2, player3, player4))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("[ERROR] 플레이어의 수는 3이하여야 합니다"); } @@ -46,10 +46,10 @@ void testCreatePlayersWithInvalidEntryCount() { @DisplayName("생성 검증을 모두 통과하면 생성에 성공한다") @Test void testCreateWithValidPlayers() { - Participant participant1 = PlayerFixture.of("리비", 1, 2); - Participant participant2 = PlayerFixture.of("제리", 3, 4); + Player player1 = PlayerFixture.of("리비", 1, 2); + Player player2 = PlayerFixture.of("제리", 3, 4); - assertThatCode(() -> new Players(List.of(participant1, participant2))) + assertThatCode(() -> new Players(List.of(player1, player2))) .doesNotThrowAnyException(); } } diff --git a/src/test/java/fixture/PlayerFixture.java b/src/test/java/fixture/PlayerFixture.java index 2e883cf302..5d8ddbd0eb 100644 --- a/src/test/java/fixture/PlayerFixture.java +++ b/src/test/java/fixture/PlayerFixture.java @@ -1,11 +1,11 @@ package fixture; -import blackjack.domain.player.Participant; +import blackjack.domain.player.Player; import blackjack.domain.player.PlayerName; public class PlayerFixture { - public static Participant of(String name, int... hand) { - return new Participant(new PlayerName(name), HandFixture.of(hand)); + public static Player of(String name, int... hand) { + return new Player(new PlayerName(name), HandFixture.of(hand)); } } From 35dfddda13ae1579a0d339a7b7f5be65161dfe00 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 15:15:47 +0900 Subject: [PATCH 107/124] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EB=8D=B1?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=EB=B6=80=ED=84=B0=20=ED=95=B8=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Hand.java | 7 +++++++ src/main/java/blackjack/domain/player/HandCreator.java | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index 04ecf299c6..a4d2991938 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -3,6 +3,7 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardNumber; import blackjack.domain.rule.HitStrategy; +import blackjack.domain.rule.PlayerHitStrategy; import blackjack.domain.rule.Score; import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.List; @@ -19,6 +20,12 @@ public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy, Hit this.hitStrategy = hitStrategy; } + public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy) { + this.cards = cards; + this.scoreCalculateStrategy = scoreCalculateStrategy; + this.hitStrategy = new PlayerHitStrategy(); + } + public int calculateCardSummation() { return cards.stream() .map(Card::getCardNumber) diff --git a/src/main/java/blackjack/domain/player/HandCreator.java b/src/main/java/blackjack/domain/player/HandCreator.java index 80a1a8ddae..9390e3f7f6 100644 --- a/src/main/java/blackjack/domain/player/HandCreator.java +++ b/src/main/java/blackjack/domain/player/HandCreator.java @@ -16,4 +16,8 @@ public Hand createPlayerHandFrom(CardDeck cardDeck) { public Hand createDealerHandFrom(CardDeck cardDeck) { return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy(), new DealerHitStrategy()); } + + public Hand createHand(CardDeck cardDeck) { + return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy()); + } } From a8a49084f74e71c431b881e8b343649bb434c4c4 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 15:26:23 +0900 Subject: [PATCH 108/124] =?UTF-8?q?refactor:=20=EC=83=81=EC=86=8D=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EC=9A=B4=EC=9A=A9=20=EA=B2=B0=EC=A0=95?= =?UTF-8?q?=EC=9D=84=20=ED=86=B5=ED=95=9C=20PlayerCreator=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/BlackJackGame.java | 15 +++++++------ .../blackjack/domain/player/HandCreator.java | 2 +- .../domain/player/PlayerCreator.java | 22 ------------------- 3 files changed, 9 insertions(+), 30 deletions(-) delete mode 100644 src/main/java/blackjack/domain/player/PlayerCreator.java diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java index 74072d8c54..3eb4ed6fac 100644 --- a/src/main/java/blackjack/BlackJackGame.java +++ b/src/main/java/blackjack/BlackJackGame.java @@ -6,9 +6,10 @@ import blackjack.domain.DrawDecision; import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; +import blackjack.domain.player.Dealer; import blackjack.domain.player.HandCreator; import blackjack.domain.player.Participant; -import blackjack.domain.player.PlayerCreator; +import blackjack.domain.player.Player; import blackjack.domain.player.PlayerName; import blackjack.domain.player.Players; import blackjack.domain.rule.Judge; @@ -31,7 +32,7 @@ public BlackJackGame(InputView inputView, OutputView outputView) { public void run() { CardDeck cardDeck = CardDeck.createShuffledDeck(); Players players = initPlayers(cardDeck); - Participant dealer = initDealer(cardDeck); + Dealer dealer = initDealer(cardDeck); printPlayersInformation(players, dealer); completePlayersHand(players, cardDeck); @@ -47,17 +48,17 @@ public void run() { private Players initPlayers(CardDeck cardDeck) { InputMapper inputMapper = new InputMapper(); - PlayerCreator playerCreator = new PlayerCreator(new HandCreator()); + HandCreator handCreator = new HandCreator(); List playerNames = inputMapper.mapToPlayers(inputView.readNames()); return new Players(playerNames.stream() - .map(playerName -> playerCreator.createPlayerFrom(playerName, cardDeck)) + .map(playerName -> new Player(playerName, handCreator.createHandFrom(cardDeck))) .toList()); } - private Participant initDealer(CardDeck cardDeck) { - PlayerCreator playerCreator = new PlayerCreator(new HandCreator()); - return playerCreator.createDealerFrom(cardDeck); + private Dealer initDealer(CardDeck cardDeck) { + HandCreator handCreator = new HandCreator(); + return new Dealer(handCreator.createHandFrom(cardDeck)); } private void printPlayersInformation(Players players, Participant dealer) { diff --git a/src/main/java/blackjack/domain/player/HandCreator.java b/src/main/java/blackjack/domain/player/HandCreator.java index 9390e3f7f6..45a999a2f1 100644 --- a/src/main/java/blackjack/domain/player/HandCreator.java +++ b/src/main/java/blackjack/domain/player/HandCreator.java @@ -17,7 +17,7 @@ public Hand createDealerHandFrom(CardDeck cardDeck) { return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy(), new DealerHitStrategy()); } - public Hand createHand(CardDeck cardDeck) { + public Hand createHandFrom(CardDeck cardDeck) { return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy()); } } diff --git a/src/main/java/blackjack/domain/player/PlayerCreator.java b/src/main/java/blackjack/domain/player/PlayerCreator.java deleted file mode 100644 index 35ba8b3ce8..0000000000 --- a/src/main/java/blackjack/domain/player/PlayerCreator.java +++ /dev/null @@ -1,22 +0,0 @@ -package blackjack.domain.player; - -import blackjack.domain.card.CardDeck; - -public class PlayerCreator { - - private static final String DEALER_NAME = "딜러"; - - private final HandCreator handCreator; - - public PlayerCreator(HandCreator handCreator) { - this.handCreator = handCreator; - } - - public Player createPlayerFrom(PlayerName playerName, CardDeck cardDeck) { - return new Player(playerName, handCreator.createPlayerHandFrom(cardDeck)); - } - - public Dealer createDealerFrom(CardDeck cardDeck) { - return new Dealer(handCreator.createDealerHandFrom(cardDeck)); - } -} From 97d070b64c7be99a84903d17805f3f5d380d2e5f Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 15:29:12 +0900 Subject: [PATCH 109/124] =?UTF-8?q?refactor:=20HitStrategy=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=94=9C=EB=9F=AC=EC=99=80=20=ED=94=8C=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=96=B4=EC=97=90=EA=B2=8C=20=EC=9D=B4=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/player/Hand.java | 14 ---------- .../blackjack/domain/player/HandCreator.java | 10 ------- .../blackjack/domain/player/Participant.java | 6 ++--- .../domain/player/ParticipantTest.java | 26 ------------------- src/test/java/fixture/HandFixture.java | 3 +-- 5 files changed, 3 insertions(+), 56 deletions(-) delete mode 100644 src/test/java/blackjack/domain/player/ParticipantTest.java diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index a4d2991938..e4dc4e578e 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -2,8 +2,6 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardNumber; -import blackjack.domain.rule.HitStrategy; -import blackjack.domain.rule.PlayerHitStrategy; import blackjack.domain.rule.Score; import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.List; @@ -12,18 +10,10 @@ public class Hand { private final List cards; private final ScoreCalculateStrategy scoreCalculateStrategy; - private final HitStrategy hitStrategy; - - public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy, HitStrategy hitStrategy) { - this.cards = cards; - this.scoreCalculateStrategy = scoreCalculateStrategy; - this.hitStrategy = hitStrategy; - } public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy) { this.cards = cards; this.scoreCalculateStrategy = scoreCalculateStrategy; - this.hitStrategy = new PlayerHitStrategy(); } public int calculateCardSummation() { @@ -51,10 +41,6 @@ public Score calculateScore() { return scoreCalculateStrategy.calculate(this); } - public boolean canHit() { - return hitStrategy.canHit(calculateScore()); - } - public List getCards() { return List.copyOf(cards); } diff --git a/src/main/java/blackjack/domain/player/HandCreator.java b/src/main/java/blackjack/domain/player/HandCreator.java index 45a999a2f1..8e04f4d5f1 100644 --- a/src/main/java/blackjack/domain/player/HandCreator.java +++ b/src/main/java/blackjack/domain/player/HandCreator.java @@ -1,22 +1,12 @@ package blackjack.domain.player; import blackjack.domain.card.CardDeck; -import blackjack.domain.rule.DealerHitStrategy; -import blackjack.domain.rule.PlayerHitStrategy; import blackjack.domain.rule.ScoreCalculateStrategy; public class HandCreator { private static final int INITIAL_HAND_SIZE = 2; - public Hand createPlayerHandFrom(CardDeck cardDeck) { - return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy(), new PlayerHitStrategy()); - } - - public Hand createDealerHandFrom(CardDeck cardDeck) { - return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy(), new DealerHitStrategy()); - } - public Hand createHandFrom(CardDeck cardDeck) { return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy()); } diff --git a/src/main/java/blackjack/domain/player/Participant.java b/src/main/java/blackjack/domain/player/Participant.java index eda26a9541..5ec0def663 100644 --- a/src/main/java/blackjack/domain/player/Participant.java +++ b/src/main/java/blackjack/domain/player/Participant.java @@ -4,7 +4,7 @@ import blackjack.domain.rule.Score; import java.util.Objects; -public class Participant { +public abstract class Participant { protected final PlayerName name; protected final Hand hand; @@ -22,9 +22,7 @@ public Score calculateHandScore() { return hand.calculateScore(); } - public boolean canHit() { - return hand.canHit(); - } + public abstract boolean canHit(); public int handSize() { return hand.countCard(); diff --git a/src/test/java/blackjack/domain/player/ParticipantTest.java b/src/test/java/blackjack/domain/player/ParticipantTest.java deleted file mode 100644 index e4c9112a00..0000000000 --- a/src/test/java/blackjack/domain/player/ParticipantTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package blackjack.domain.player; - -import static org.assertj.core.api.Assertions.assertThat; - -import blackjack.domain.card.Card; -import fixture.CardFixture; -import fixture.HandFixture; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -@DisplayName("참가자 테스트") -class ParticipantTest { - - @DisplayName("플레이어 핸드에 카드를 추가할 수 있다") - @Test - void testAppendCardToPlayer() { - PlayerName playerName = new PlayerName("pobi"); - Hand hand = HandFixture.of(10, 9); - Participant participant = new Participant(playerName, hand); - - Card card = CardFixture.from(2); - participant.appendCard(card); - - assertThat(hand.calculateCardSummation()).isEqualTo(21); - } -} diff --git a/src/test/java/fixture/HandFixture.java b/src/test/java/fixture/HandFixture.java index 22e5f99bb4..b27efa7678 100644 --- a/src/test/java/fixture/HandFixture.java +++ b/src/test/java/fixture/HandFixture.java @@ -1,7 +1,6 @@ package fixture; import blackjack.domain.player.Hand; -import blackjack.domain.rule.PlayerHitStrategy; import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.Arrays; import java.util.stream.Collectors; @@ -11,6 +10,6 @@ public class HandFixture { public static Hand of(int... numbers) { return new Hand(Arrays.stream(numbers) .mapToObj(CardFixture::from) - .collect(Collectors.toList()), new ScoreCalculateStrategy(), new PlayerHitStrategy()); + .collect(Collectors.toList()), new ScoreCalculateStrategy()); } } From ae0fb23948e0c21bb3348e0a40e66069dd3165b8 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 15:36:56 +0900 Subject: [PATCH 110/124] =?UTF-8?q?refactor:=20=EC=B6=94=EC=83=81=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1=EC=9E=90=20?= =?UTF-8?q?=EC=A0=91=EA=B7=BC=EC=A0=9C=EC=96=B4=EC=9E=90=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Participant.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/blackjack/domain/player/Participant.java b/src/main/java/blackjack/domain/player/Participant.java index 5ec0def663..06c3956ce7 100644 --- a/src/main/java/blackjack/domain/player/Participant.java +++ b/src/main/java/blackjack/domain/player/Participant.java @@ -9,7 +9,7 @@ public abstract class Participant { protected final PlayerName name; protected final Hand hand; - public Participant(PlayerName name, Hand hand) { + protected Participant(PlayerName name, Hand hand) { this.name = name; this.hand = hand; } From a33e959e5af28c15bee258575cdb914ffc2ab6b8 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 17:18:04 +0900 Subject: [PATCH 111/124] =?UTF-8?q?refactor:=20=EC=A0=90=EC=88=98=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=20=EA=B8=B0=EB=8A=A5=20Hand=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=EC=9C=BC=EB=A1=9C=20=EC=B1=85=EC=9E=84=20?= =?UTF-8?q?=EC=9D=B4=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/player/Hand.java | 11 +++++++- .../blackjack/domain/player/HandTest.java | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index e4dc4e578e..b9d8ed0070 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -8,6 +8,9 @@ public class Hand { + private static final int BLACK_JACK = 21; + private static final int ACE_WEIGHT = 10; + private final List cards; private final ScoreCalculateStrategy scoreCalculateStrategy; @@ -38,7 +41,13 @@ public int countCard() { } public Score calculateScore() { - return scoreCalculateStrategy.calculate(this); + int aceCount = countAce(); + int sum = calculateCardSummation(); + while (aceCount > 0 && (sum + ACE_WEIGHT) <= BLACK_JACK) { + sum += ACE_WEIGHT; + aceCount--; + } + return new Score(sum); } public List getCards() { diff --git a/src/test/java/blackjack/domain/player/HandTest.java b/src/test/java/blackjack/domain/player/HandTest.java index 0f4a4e09b9..a4a3260f3d 100644 --- a/src/test/java/blackjack/domain/player/HandTest.java +++ b/src/test/java/blackjack/domain/player/HandTest.java @@ -37,4 +37,32 @@ void testCountAceInHand() { assertThat(hand.countAce()).isEqualTo(3); } + + @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 없음") + @Test + void testCalculateScoreWithNoAce() { + Hand hand = HandFixture.of(2, 3, 4, 5); + assertThat(hand.calculateScore().getValue()).isEqualTo(14); + } + + @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 11로 이용됨") + @Test + void testCalculateScoreWithBigAce() { + Hand hand = HandFixture.of(1, 10); + assertThat(hand.calculateScore().getValue()).isEqualTo(21); + } + + @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 1로 이용됨") + @Test + void testCalculateScoreWithLowAce() { + Hand hand = HandFixture.of(1, 10, 2); + assertThat(hand.calculateScore().getValue()).isEqualTo(13); + } + + @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 두개 이상") + @Test + void testCalculateScoreWithMultipleAce() { + Hand hand = HandFixture.of(1, 1, 1); + assertThat(hand.calculateScore().getValue()).isEqualTo(13); + } } From 1264068a70bad858ec8d0dccb32c2a202952d4ef Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 17:20:08 +0900 Subject: [PATCH 112/124] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/rule/DealerHitStrategy.java | 11 ----- .../blackjack/domain/rule/HitStrategy.java | 6 --- .../domain/rule/PlayerHitStrategy.java | 11 ----- .../domain/rule/DealerHitStrategyTest.java | 34 ------------- .../rule/ParticipantHitStrategyTest.java | 34 ------------- .../rule/ScoreCalculateStrategyTest.java | 48 ------------------- 6 files changed, 144 deletions(-) delete mode 100644 src/main/java/blackjack/domain/rule/DealerHitStrategy.java delete mode 100644 src/main/java/blackjack/domain/rule/HitStrategy.java delete mode 100644 src/main/java/blackjack/domain/rule/PlayerHitStrategy.java delete mode 100644 src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java delete mode 100644 src/test/java/blackjack/domain/rule/ParticipantHitStrategyTest.java delete mode 100644 src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java diff --git a/src/main/java/blackjack/domain/rule/DealerHitStrategy.java b/src/main/java/blackjack/domain/rule/DealerHitStrategy.java deleted file mode 100644 index 1132b5c510..0000000000 --- a/src/main/java/blackjack/domain/rule/DealerHitStrategy.java +++ /dev/null @@ -1,11 +0,0 @@ -package blackjack.domain.rule; - -public class DealerHitStrategy implements HitStrategy { - - private static final int HIT_THRESHOLD = 17; - - @Override - public boolean canHit(Score score) { - return score.getValue() < HIT_THRESHOLD; - } -} diff --git a/src/main/java/blackjack/domain/rule/HitStrategy.java b/src/main/java/blackjack/domain/rule/HitStrategy.java deleted file mode 100644 index 3316fab0b5..0000000000 --- a/src/main/java/blackjack/domain/rule/HitStrategy.java +++ /dev/null @@ -1,6 +0,0 @@ -package blackjack.domain.rule; - -public interface HitStrategy { - - public boolean canHit(Score score); -} diff --git a/src/main/java/blackjack/domain/rule/PlayerHitStrategy.java b/src/main/java/blackjack/domain/rule/PlayerHitStrategy.java deleted file mode 100644 index bc334834aa..0000000000 --- a/src/main/java/blackjack/domain/rule/PlayerHitStrategy.java +++ /dev/null @@ -1,11 +0,0 @@ -package blackjack.domain.rule; - -public class PlayerHitStrategy implements HitStrategy { - - public static final int HIT_THRESHOLD = 21; - - @Override - public boolean canHit(Score score) { - return score.getValue() <= HIT_THRESHOLD; - } -} diff --git a/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java b/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java deleted file mode 100644 index 2dc5eff1d0..0000000000 --- a/src/test/java/blackjack/domain/rule/DealerHitStrategyTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package blackjack.domain.rule; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -class DealerHitStrategyTest { - - private DealerHitStrategy dealerHitStrategy; - - @BeforeEach - void setUp() { - dealerHitStrategy = new DealerHitStrategy(); - } - - @DisplayName("딜러는 17점 미만이면 카드를 받아야 한다") - @ParameterizedTest - @ValueSource(ints = {13, 14, 15, 16}) - void testDealerShouldHit(int score) { - boolean canHit = dealerHitStrategy.canHit(new Score(score)); - assertThat(canHit).isTrue(); - } - - @DisplayName("딜러는 17점 이상이면 카드를 받을 수 없다") - @ParameterizedTest - @ValueSource(ints = {17, 18, 19, 20}) - void testDealerShouldStay(int score) { - boolean canHit = dealerHitStrategy.canHit(new Score(score)); - assertThat(canHit).isFalse(); - } -} diff --git a/src/test/java/blackjack/domain/rule/ParticipantHitStrategyTest.java b/src/test/java/blackjack/domain/rule/ParticipantHitStrategyTest.java deleted file mode 100644 index d13cd1a893..0000000000 --- a/src/test/java/blackjack/domain/rule/ParticipantHitStrategyTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package blackjack.domain.rule; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -class ParticipantHitStrategyTest { - - private PlayerHitStrategy playerHitStrategy; - - @BeforeEach - void setUp() { - playerHitStrategy = new PlayerHitStrategy(); - } - - @DisplayName("플레이어는 21점 이하면 카드를 받을 수 있다") - @ParameterizedTest - @ValueSource(ints = {20, 21}) - void testDealerShouldHit(int score) { - boolean canHit = playerHitStrategy.canHit(new Score(score)); - assertThat(canHit).isTrue(); - } - - @DisplayName("플레이어는 21점을 초과하면 카드를 받을 수 없다") - @ParameterizedTest - @ValueSource(ints = {22, 23}) - void testDealerShouldStay(int score) { - boolean canHit = playerHitStrategy.canHit(new Score(score)); - assertThat(canHit).isFalse(); - } -} diff --git a/src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java b/src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java deleted file mode 100644 index 6cca764215..0000000000 --- a/src/test/java/blackjack/domain/rule/ScoreCalculateStrategyTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package blackjack.domain.rule; - -import static org.assertj.core.api.Assertions.assertThat; - -import blackjack.domain.player.Hand; -import fixture.HandFixture; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -@DisplayName("점수 계산 전략 테스트") -class ScoreCalculateStrategyTest { - - private ScoreCalculateStrategy scoreCalculateStrategy; - - @BeforeEach - void setUp() { - scoreCalculateStrategy = new ScoreCalculateStrategy(); - } - - @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 없음") - @Test - void testCalculateScoreWithNoAce() { - Hand hand = HandFixture.of(2, 3, 4, 5); - assertThat(scoreCalculateStrategy.calculate(hand).getValue()).isEqualTo(14); - } - - @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 11로 이용됨") - @Test - void testCalculateScoreWithBigAce() { - Hand hand = HandFixture.of(1, 10); - assertThat(scoreCalculateStrategy.calculate(hand).getValue()).isEqualTo(21); - } - - @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 1로 이용됨") - @Test - void testCalculateScoreWithLowAce() { - Hand hand = HandFixture.of(1, 10, 2); - assertThat(scoreCalculateStrategy.calculate(hand).getValue()).isEqualTo(13); - } - - @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 두개 이상") - @Test - void testCalculateScoreWithMultipleAce() { - Hand hand = HandFixture.of(1, 1, 1); - assertThat(scoreCalculateStrategy.calculate(hand).getValue()).isEqualTo(13); - } -} From 9bc37a8ec2947875dc07358a7db2b33fc79e9ae2 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 17:22:49 +0900 Subject: [PATCH 113/124] =?UTF-8?q?refactor:=20=EC=A0=90=EC=88=98=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=20=EB=A1=9C=EC=A7=81=20Hand=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EC=9D=91=EC=A7=91=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B8=ED=95=9C=20=EC=97=B0=EA=B4=80=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/BlackJackGame.java | 3 +-- .../java/blackjack/domain/player/Hand.java | 5 +---- .../blackjack/domain/player/HandCreator.java | 3 +-- .../java/blackjack/domain/player/Players.java | 3 +-- .../domain/rule/ScoreCalculateStrategy.java | 19 ------------------- src/test/java/fixture/HandFixture.java | 3 +-- 6 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java index 3eb4ed6fac..33fbbd6702 100644 --- a/src/main/java/blackjack/BlackJackGame.java +++ b/src/main/java/blackjack/BlackJackGame.java @@ -14,7 +14,6 @@ import blackjack.domain.player.Players; import blackjack.domain.rule.Judge; import blackjack.domain.rule.Score; -import blackjack.domain.rule.ScoreCalculateStrategy; import blackjack.view.InputView; import blackjack.view.OutputView; import java.util.List; @@ -107,7 +106,7 @@ private void printPlayerScore(Participant participant) { private void printDealerGameResult(Participant dealer, Players players) { Score dealerScore = dealer.calculateHandScore(); - int playerWinCount = players.countPlayerWithScoreAbove(dealerScore, new ScoreCalculateStrategy()); + int playerWinCount = players.countPlayerWithScoreAbove(dealerScore); int dealerWinCount = players.countPlayer() - playerWinCount; DealerGameResult dealerGameResult = new DealerGameResult(dealerWinCount, playerWinCount); diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index b9d8ed0070..610e2283b0 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -3,7 +3,6 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardNumber; import blackjack.domain.rule.Score; -import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.List; public class Hand { @@ -12,11 +11,9 @@ public class Hand { private static final int ACE_WEIGHT = 10; private final List cards; - private final ScoreCalculateStrategy scoreCalculateStrategy; - public Hand(List cards, ScoreCalculateStrategy scoreCalculateStrategy) { + public Hand(List cards) { this.cards = cards; - this.scoreCalculateStrategy = scoreCalculateStrategy; } public int calculateCardSummation() { diff --git a/src/main/java/blackjack/domain/player/HandCreator.java b/src/main/java/blackjack/domain/player/HandCreator.java index 8e04f4d5f1..4e0d2deae4 100644 --- a/src/main/java/blackjack/domain/player/HandCreator.java +++ b/src/main/java/blackjack/domain/player/HandCreator.java @@ -1,13 +1,12 @@ package blackjack.domain.player; import blackjack.domain.card.CardDeck; -import blackjack.domain.rule.ScoreCalculateStrategy; public class HandCreator { private static final int INITIAL_HAND_SIZE = 2; public Hand createHandFrom(CardDeck cardDeck) { - return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE), new ScoreCalculateStrategy()); + return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE)); } } diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index e5e5620b3e..e0c17514b5 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -1,7 +1,6 @@ package blackjack.domain.player; import blackjack.domain.rule.Score; -import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.List; public class Players { @@ -19,7 +18,7 @@ public int countPlayer() { return players.size(); } - public int countPlayerWithScoreAbove(Score target, ScoreCalculateStrategy scoreCalculateStrategy) { + public int countPlayerWithScoreAbove(Score target) { return (int) players.stream() .map(Participant::calculateHandScore) .filter(playerScore -> playerScore.isAbove(target)) diff --git a/src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java b/src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java deleted file mode 100644 index 31dd4f4696..0000000000 --- a/src/main/java/blackjack/domain/rule/ScoreCalculateStrategy.java +++ /dev/null @@ -1,19 +0,0 @@ -package blackjack.domain.rule; - -import blackjack.domain.player.Hand; - -public class ScoreCalculateStrategy { - - private static final int BLACK_JACK = 21; - private static final int ACE_WEIGHT = 10; - - public Score calculate(Hand hand) { - int aceCount = hand.countAce(); - int sum = hand.calculateCardSummation(); - while (aceCount > 0 && (sum + ACE_WEIGHT) <= BLACK_JACK) { - sum += ACE_WEIGHT; - aceCount--; - } - return new Score(sum); - } -} diff --git a/src/test/java/fixture/HandFixture.java b/src/test/java/fixture/HandFixture.java index b27efa7678..d8e0e4a5ee 100644 --- a/src/test/java/fixture/HandFixture.java +++ b/src/test/java/fixture/HandFixture.java @@ -1,7 +1,6 @@ package fixture; import blackjack.domain.player.Hand; -import blackjack.domain.rule.ScoreCalculateStrategy; import java.util.Arrays; import java.util.stream.Collectors; @@ -10,6 +9,6 @@ public class HandFixture { public static Hand of(int... numbers) { return new Hand(Arrays.stream(numbers) .mapToObj(CardFixture::from) - .collect(Collectors.toList()), new ScoreCalculateStrategy()); + .collect(Collectors.toList())); } } From d2c0c4285dd5390558de327b21085d5211f7d755 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 17:28:47 +0900 Subject: [PATCH 114/124] =?UTF-8?q?refactor:=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=20=EB=82=B4=20=EB=B7=B0=EB=A1=9C=EC=A7=81=20=EC=A0=9C=EA=B1=B0?= =?UTF-8?q?=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/domain/card/CardNumber.java | 34 ++++++++----------- .../java/blackjack/domain/card/CardShape.java | 18 +++------- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/src/main/java/blackjack/domain/card/CardNumber.java b/src/main/java/blackjack/domain/card/CardNumber.java index 84c406d783..e93fae8cc7 100644 --- a/src/main/java/blackjack/domain/card/CardNumber.java +++ b/src/main/java/blackjack/domain/card/CardNumber.java @@ -2,26 +2,24 @@ public enum CardNumber { - ACE(1, "A"), - TWO(2, "2"), - THREE(3, "3"), - FOUR(4, "4"), - FIVE(5, "5"), - SIX(6, "6"), - SEVEN(7, "7"), - EIGHT(8, "8"), - NINE(9, "9"), - TEN(10, "10"), - JACK(10, "J"), - QUEEN(10, "Q"), - KING(10, "K"); + ACE(1), + TWO(2), + THREE(3), + FOUR(4), + FIVE(5), + SIX(6), + SEVEN(7), + EIGHT(8), + NINE(9), + TEN(10), + JACK(10), + QUEEN(10), + KING(10); private final int value; - private final String name; - CardNumber(int value, String name) { + CardNumber(int value) { this.value = value; - this.name = name; } public boolean isAce() { @@ -31,9 +29,5 @@ public boolean isAce() { public int getValue() { return value; } - - public String getName() { - return name; - } } diff --git a/src/main/java/blackjack/domain/card/CardShape.java b/src/main/java/blackjack/domain/card/CardShape.java index fa799a8774..24735fa598 100644 --- a/src/main/java/blackjack/domain/card/CardShape.java +++ b/src/main/java/blackjack/domain/card/CardShape.java @@ -2,18 +2,8 @@ public enum CardShape { - HEART("하트"), - DIAMOND("다이아몬드"), - CLUB("클로버"), - SPADE("스페이드"); - - private final String name; - - CardShape(String name) { - this.name = name; - } - - public String getName() { - return name; - } + HEART, + DIAMOND, + CLUB, + SPADE; } From ce24ab49bf38493816ab5953e28952734fd6ea3a Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 23:14:17 +0900 Subject: [PATCH 115/124] =?UTF-8?q?refactor:=20Hand=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=20=EB=A1=9C=EC=A7=81=20Hand=EB=8F=84=EB=A9=94=EC=9D=B8=20?= =?UTF-8?q?=EC=95=88=EC=9C=BC=EB=A1=9C=20=EC=9D=91=EC=A7=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Hand.java | 6 ++++++ .../java/blackjack/domain/player/HandTest.java | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index 610e2283b0..01322acd08 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -1,12 +1,14 @@ package blackjack.domain.player; import blackjack.domain.card.Card; +import blackjack.domain.card.CardDeck; import blackjack.domain.card.CardNumber; import blackjack.domain.rule.Score; import java.util.List; public class Hand { + private static final int INITIAL_HAND_SIZE = 2; private static final int BLACK_JACK = 21; private static final int ACE_WEIGHT = 10; @@ -16,6 +18,10 @@ public Hand(List cards) { this.cards = cards; } + public static Hand createHandFrom(CardDeck cardDeck) { + return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE)); + } + public int calculateCardSummation() { return cards.stream() .map(Card::getCardNumber) diff --git a/src/test/java/blackjack/domain/player/HandTest.java b/src/test/java/blackjack/domain/player/HandTest.java index a4a3260f3d..61504e550e 100644 --- a/src/test/java/blackjack/domain/player/HandTest.java +++ b/src/test/java/blackjack/domain/player/HandTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import blackjack.domain.card.Card; +import blackjack.domain.card.CardDeck; import fixture.CardFixture; import fixture.HandFixture; import org.junit.jupiter.api.DisplayName; @@ -11,13 +12,18 @@ @DisplayName("참가자 소유 카드 테스트") class HandTest { + @DisplayName("초기 핸드의 사이즈는 2이다") + @Test + void testCreateInitialHandSize() { + Hand hand = Hand.createHandFrom(CardDeck.createShuffledDeck()); + assertThat(hand.getCards().size()).isEqualTo(2); + } + @DisplayName("카드의 합을 계산할 수 있다.") @Test void testHandSummation() { Hand hand = HandFixture.of(2, 2, 2); - int expected = hand.calculateCardSummation(); - - assertThat(expected).isEqualTo(6); + assertThat(hand.calculateCardSummation()).isEqualTo(6); } @DisplayName("특정 카드를 핸드에 추가할 수 있다") @@ -26,7 +32,6 @@ void testAppendCard() { Card card = CardFixture.from(2); Hand hand = HandFixture.of(); hand.appendCard(card); - assertThat(hand.getCards()).containsExactly(card); } @@ -34,7 +39,6 @@ void testAppendCard() { @Test void testCountAceInHand() { Hand hand = HandFixture.of(1, 1, 1, 3, 4, 5); - assertThat(hand.countAce()).isEqualTo(3); } From a83dc83f6b2d06eed5cb4277b51aa65da4bf639b Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 23:16:44 +0900 Subject: [PATCH 116/124] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EA=B2=8C=20=EB=90=9C=20HandCreator=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=A0=9C=EA=B1=B0=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/BlackJackGame.java | 12 +++--------- .../java/blackjack/domain/player/HandCreator.java | 12 ------------ 2 files changed, 3 insertions(+), 21 deletions(-) delete mode 100644 src/main/java/blackjack/domain/player/HandCreator.java diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java index 33fbbd6702..a8472445b5 100644 --- a/src/main/java/blackjack/BlackJackGame.java +++ b/src/main/java/blackjack/BlackJackGame.java @@ -7,7 +7,7 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; import blackjack.domain.player.Dealer; -import blackjack.domain.player.HandCreator; +import blackjack.domain.player.Hand; import blackjack.domain.player.Participant; import blackjack.domain.player.Player; import blackjack.domain.player.PlayerName; @@ -31,7 +31,7 @@ public BlackJackGame(InputView inputView, OutputView outputView) { public void run() { CardDeck cardDeck = CardDeck.createShuffledDeck(); Players players = initPlayers(cardDeck); - Dealer dealer = initDealer(cardDeck); + Dealer dealer = new Dealer(Hand.createHandFrom(cardDeck)); printPlayersInformation(players, dealer); completePlayersHand(players, cardDeck); @@ -47,19 +47,13 @@ public void run() { private Players initPlayers(CardDeck cardDeck) { InputMapper inputMapper = new InputMapper(); - HandCreator handCreator = new HandCreator(); List playerNames = inputMapper.mapToPlayers(inputView.readNames()); return new Players(playerNames.stream() - .map(playerName -> new Player(playerName, handCreator.createHandFrom(cardDeck))) + .map(playerName -> new Player(playerName, Hand.createHandFrom(cardDeck))) .toList()); } - private Dealer initDealer(CardDeck cardDeck) { - HandCreator handCreator = new HandCreator(); - return new Dealer(handCreator.createHandFrom(cardDeck)); - } - private void printPlayersInformation(Players players, Participant dealer) { outputView.printHandOutEvent(players, 2); outputView.printDealerInitialHand(dealer); diff --git a/src/main/java/blackjack/domain/player/HandCreator.java b/src/main/java/blackjack/domain/player/HandCreator.java deleted file mode 100644 index 4e0d2deae4..0000000000 --- a/src/main/java/blackjack/domain/player/HandCreator.java +++ /dev/null @@ -1,12 +0,0 @@ -package blackjack.domain.player; - -import blackjack.domain.card.CardDeck; - -public class HandCreator { - - private static final int INITIAL_HAND_SIZE = 2; - - public Hand createHandFrom(CardDeck cardDeck) { - return new Hand(cardDeck.popCards(INITIAL_HAND_SIZE)); - } -} From 2a32365e665379dec306e9382967da6a697e71e3 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 23:26:55 +0900 Subject: [PATCH 117/124] =?UTF-8?q?feat:=20pop=ED=95=9C=20=EA=B0=9C?= =?UTF-8?q?=EC=88=98=EB=A5=BC=20=EA=B3=84=EC=82=B0=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Hand.java | 4 ++++ src/main/java/blackjack/domain/player/Participant.java | 4 ++++ src/test/java/blackjack/domain/player/HandTest.java | 9 +++++++++ 3 files changed, 17 insertions(+) diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index 01322acd08..53ff1f1af1 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -43,6 +43,10 @@ public int countCard() { return cards.size(); } + public int countPop() { + return cards.size() - INITIAL_HAND_SIZE; + } + public Score calculateScore() { int aceCount = countAce(); int sum = calculateCardSummation(); diff --git a/src/main/java/blackjack/domain/player/Participant.java b/src/main/java/blackjack/domain/player/Participant.java index 06c3956ce7..6e7ea45e93 100644 --- a/src/main/java/blackjack/domain/player/Participant.java +++ b/src/main/java/blackjack/domain/player/Participant.java @@ -22,6 +22,10 @@ public Score calculateHandScore() { return hand.calculateScore(); } + public int countPop() { + return hand.countPop(); + } + public abstract boolean canHit(); public int handSize() { diff --git a/src/test/java/blackjack/domain/player/HandTest.java b/src/test/java/blackjack/domain/player/HandTest.java index 61504e550e..d88b3d3f9a 100644 --- a/src/test/java/blackjack/domain/player/HandTest.java +++ b/src/test/java/blackjack/domain/player/HandTest.java @@ -35,6 +35,15 @@ void testAppendCard() { assertThat(hand.getCards()).containsExactly(card); } + @DisplayName("몇개의 카드를 더 뽑은 상태인지 확인할 수 있다") + @Test + void testCountPop() { + CardDeck cardDeck = CardDeck.createShuffledDeck(); + Hand hand = Hand.createHandFrom(cardDeck); + hand.appendCard(cardDeck.popCard()); + assertThat(hand.countPop()).isEqualTo(1); + } + @DisplayName("핸드에 에이스 카드가 몇개 있는지 확인할 수 있다") @Test void testCountAceInHand() { From 0472a19fd739612b7fc7c321d86dc55ffbdca526 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 23:30:11 +0900 Subject: [PATCH 118/124] =?UTF-8?q?refactor:=20=EB=AA=87=EC=9E=A5=20?= =?UTF-8?q?=EB=8D=94=20=EB=BD=91=EC=95=98=EB=8A=94=EC=A7=80=20=EB=94=9C?= =?UTF-8?q?=EB=9F=AC=EC=97=90=EA=B2=8C=20=EB=AC=BC=EC=96=B4=EB=B3=B4?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/BlackJackGame.java | 10 +--------- src/main/java/blackjack/domain/player/Dealer.java | 2 +- src/main/java/blackjack/view/MessageResolver.java | 7 +++++-- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java index a8472445b5..45c4f75fd4 100644 --- a/src/main/java/blackjack/BlackJackGame.java +++ b/src/main/java/blackjack/BlackJackGame.java @@ -36,8 +36,7 @@ public void run() { completePlayersHand(players, cardDeck); completeDealerHand(dealer, cardDeck); - - printDealerPopCount(dealer); + outputView.printDealerPopCount(Dealer.HIT_THRESHOLD, dealer.countPop()); printPlayerScore(dealer); printPlayersScore(players); @@ -83,13 +82,6 @@ private void completeDealerHand(Participant dealer, CardDeck cardDeck) { } } - private void printDealerPopCount(Participant dealer) { - int dealerPopCount = dealer.handSize() - 2; - if (dealerPopCount > 0) { - outputView.printDealerPopCount(16, dealerPopCount); - } - } - private void printPlayersScore(Players players) { players.getPlayers().forEach(this::printPlayerScore); } diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java index 1e4bcd6d13..56a536ead0 100644 --- a/src/main/java/blackjack/domain/player/Dealer.java +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -3,7 +3,7 @@ public class Dealer extends Participant { private static final String DEALER_NAME = "딜러"; - private static final int HIT_THRESHOLD = 16; + public static final int HIT_THRESHOLD = 16; public Dealer(Hand hand) { super(new PlayerName(DEALER_NAME), hand); diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index 1679b36d27..052b0b45a7 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -52,8 +52,11 @@ public String resolveDealerHandMessage(Participant dealer) { } public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount) { - String message = String.format("딜러는 %d이하라 %d장의 카드를 더 받았습니다.", dealerDrawThreshold, popCount); - return String.join("", LINE_SEPARATOR, message, LINE_SEPARATOR); + if (popCount > 0) { + String message = String.format("딜러는 %d이하라 %d장의 카드를 더 받았습니다.", dealerDrawThreshold, popCount); + return String.join("", LINE_SEPARATOR, message, LINE_SEPARATOR); + } + return ""; } public String resolvePlayerScoreMessage(Participant participant, Score score) { From 71a4e81b110fef9b6f4d18ce927fd97be8bac575 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 23:47:34 +0900 Subject: [PATCH 119/124] =?UTF-8?q?refactor:=20=EB=94=9C=EB=9F=AC=20?= =?UTF-8?q?=EC=A0=84=EC=A0=81=20=EA=B3=84=EC=82=B0=20Judge=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EC=B1=85=EC=9E=84=20=EC=9D=B4?= =?UTF-8?q?=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/BlackJackGame.java | 32 +++++++------------ .../java/blackjack/domain/rule/Judge.java | 17 +++++++++- .../java/blackjack/domain/rule/JudgeTest.java | 27 ++++++++++++++-- 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java index 45c4f75fd4..a72552b6a4 100644 --- a/src/main/java/blackjack/BlackJackGame.java +++ b/src/main/java/blackjack/BlackJackGame.java @@ -2,7 +2,6 @@ import static blackjack.domain.DrawDecision.YES; -import blackjack.domain.DealerGameResult; import blackjack.domain.DrawDecision; import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; @@ -13,7 +12,6 @@ import blackjack.domain.player.PlayerName; import blackjack.domain.player.Players; import blackjack.domain.rule.Judge; -import blackjack.domain.rule.Score; import blackjack.view.InputView; import blackjack.view.OutputView; import java.util.List; @@ -38,7 +36,7 @@ public void run() { completeDealerHand(dealer, cardDeck); outputView.printDealerPopCount(Dealer.HIT_THRESHOLD, dealer.countPop()); - printPlayerScore(dealer); + printParticipantScore(dealer); printPlayersScore(players); printDealerGameResult(dealer, players); printPlayersGameResult(players, dealer); @@ -63,19 +61,19 @@ private void completePlayersHand(Players players, CardDeck cardDeck) { players.getPlayers().forEach(player -> completePlayerHand(player, cardDeck)); } - private void completePlayerHand(Participant participant, CardDeck cardDeck) { + private void completePlayerHand(Player participant, CardDeck cardDeck) { while (participant.canHit() && readHitDecision(participant) == YES) { participant.appendCard(cardDeck.popCard()); outputView.printPlayerHand(participant); } } - private DrawDecision readHitDecision(Participant participant) { + private DrawDecision readHitDecision(Player participant) { InputMapper inputMapper = new InputMapper(); return inputMapper.mapToDrawDecision(inputView.readDrawPlan(participant.getName())); } - private void completeDealerHand(Participant dealer, CardDeck cardDeck) { + private void completeDealerHand(Dealer dealer, CardDeck cardDeck) { while (dealer.canHit()) { Card card = cardDeck.popCard(); dealer.appendCard(card); @@ -83,29 +81,21 @@ private void completeDealerHand(Participant dealer, CardDeck cardDeck) { } private void printPlayersScore(Players players) { - players.getPlayers().forEach(this::printPlayerScore); + players.getPlayers().forEach(this::printParticipantScore); } - private void printPlayerScore(Participant participant) { + private void printParticipantScore(Participant participant) { outputView.printPlayerScore(participant, participant.calculateHandScore()); } - private void printDealerGameResult(Participant dealer, Players players) { - Score dealerScore = dealer.calculateHandScore(); - int playerWinCount = players.countPlayerWithScoreAbove(dealerScore); - int dealerWinCount = players.countPlayer() - playerWinCount; - - DealerGameResult dealerGameResult = new DealerGameResult(dealerWinCount, playerWinCount); - outputView.printDealerGameResult(dealerGameResult); + private void printDealerGameResult(Dealer dealer, Players players) { + Judge judge = new Judge(); + outputView.printDealerGameResult(judge.calculateDealerGameResult(dealer, players)); } - private void printPlayersGameResult(Players players, Participant dealer) { + private void printPlayersGameResult(Players players, Dealer dealer) { Judge judge = new Judge(); - Score dealerScore = dealer.calculateHandScore(); players.getPlayers() - .forEach(player -> { - Score playerScore = player.calculateHandScore(); - outputView.printPlayerGameResult(player, judge.isPlayerWin(dealerScore, playerScore)); - }); + .forEach(player -> outputView.printPlayerGameResult(player, judge.isPlayerWin(dealer, player))); } } diff --git a/src/main/java/blackjack/domain/rule/Judge.java b/src/main/java/blackjack/domain/rule/Judge.java index f484085c7c..4bcf103ab1 100644 --- a/src/main/java/blackjack/domain/rule/Judge.java +++ b/src/main/java/blackjack/domain/rule/Judge.java @@ -1,10 +1,25 @@ package blackjack.domain.rule; +import blackjack.domain.DealerGameResult; +import blackjack.domain.player.Dealer; +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; + public class Judge { private static final Score BLACK_JACK = new Score(21); - public boolean isPlayerWin(Score dealerScore, Score playerScore) { + public DealerGameResult calculateDealerGameResult(Dealer dealer, Players players) { + int playerWinCount = (int) players.getPlayers().stream() + .filter(player -> isPlayerWin(dealer, player)) + .count(); + int dealerWinCount = players.countPlayer() - playerWinCount; + return new DealerGameResult(dealerWinCount, playerWinCount); + } + + public boolean isPlayerWin(Dealer dealer, Player player) { + Score dealerScore = dealer.calculateHandScore(); + Score playerScore = player.calculateHandScore(); if (playerScore.isAbove(BLACK_JACK)) { return false; } diff --git a/src/test/java/blackjack/domain/rule/JudgeTest.java b/src/test/java/blackjack/domain/rule/JudgeTest.java index 6327051c3b..cd279a457e 100644 --- a/src/test/java/blackjack/domain/rule/JudgeTest.java +++ b/src/test/java/blackjack/domain/rule/JudgeTest.java @@ -1,7 +1,15 @@ package blackjack.domain.rule; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; +import blackjack.domain.DealerGameResult; +import blackjack.domain.player.Dealer; +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; +import fixture.HandFixture; +import fixture.PlayerFixture; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -18,8 +26,21 @@ void setUp() { @DisplayName("딜러와 플레이어 중 누가 이겼는지 알 수 있다") @Test void testSelectWinner() { - Score dealerScore = new Score(10); - Score playerScore = new Score(5); - assertThat(judge.isPlayerWin(dealerScore, playerScore)).isFalse(); + Player player = PlayerFixture.of("리비", 2, 3); + Dealer dealer = new Dealer(HandFixture.of(3, 4)); + assertThat(judge.isPlayerWin(dealer, player)).isFalse(); + } + + @DisplayName("딜러의 전적을 계산할 수 있다") + @Test + void testCalculateDealerResult() { + Player player = PlayerFixture.of("리비", 2, 3); + Dealer dealer = new Dealer(HandFixture.of(3, 4)); + Players players = new Players(List.of(player)); + DealerGameResult dealerGameResult = judge.calculateDealerGameResult(dealer, players); + assertAll( + () -> assertThat(dealerGameResult.getWinCount()).isEqualTo(1), + () -> assertThat(dealerGameResult.getLoseCount()).isEqualTo(0) + ); } } From b3f67a89644780cf8339dce0d02a899839e6e7f3 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 23:51:28 +0900 Subject: [PATCH 120/124] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Participant.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Participant.java b/src/main/java/blackjack/domain/player/Participant.java index 6e7ea45e93..6a3d875fc1 100644 --- a/src/main/java/blackjack/domain/player/Participant.java +++ b/src/main/java/blackjack/domain/player/Participant.java @@ -28,10 +28,6 @@ public int countPop() { public abstract boolean canHit(); - public int handSize() { - return hand.countCard(); - } - @Override public boolean equals(Object o) { if (this == o) { From d5c52e8623c6a7cb76a68ba3acc3c02202f2b49a Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 10 Mar 2024 23:58:04 +0900 Subject: [PATCH 121/124] =?UTF-8?q?refactor:=20=EC=83=81=EC=86=8D=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=EB=A5=BC=20=ED=86=B5=ED=95=9C=20=ED=8C=8C?= =?UTF-8?q?=EB=9D=BC=EB=AF=B8=ED=84=B0=20=ED=83=80=EC=9E=85=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/BlackJackGame.java | 4 ++-- src/main/java/blackjack/view/MessageResolver.java | 10 ++++++---- src/main/java/blackjack/view/OutputView.java | 12 +++++++----- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java index a72552b6a4..4a8b5ece20 100644 --- a/src/main/java/blackjack/BlackJackGame.java +++ b/src/main/java/blackjack/BlackJackGame.java @@ -51,7 +51,7 @@ private Players initPlayers(CardDeck cardDeck) { .toList()); } - private void printPlayersInformation(Players players, Participant dealer) { + private void printPlayersInformation(Players players, Dealer dealer) { outputView.printHandOutEvent(players, 2); outputView.printDealerInitialHand(dealer); players.getPlayers().forEach(outputView::printPlayerHand); @@ -85,7 +85,7 @@ private void printPlayersScore(Players players) { } private void printParticipantScore(Participant participant) { - outputView.printPlayerScore(participant, participant.calculateHandScore()); + outputView.printParticipantScore(participant, participant.calculateHandScore()); } private void printDealerGameResult(Dealer dealer, Players players) { diff --git a/src/main/java/blackjack/view/MessageResolver.java b/src/main/java/blackjack/view/MessageResolver.java index 052b0b45a7..ed237db8a2 100644 --- a/src/main/java/blackjack/view/MessageResolver.java +++ b/src/main/java/blackjack/view/MessageResolver.java @@ -7,8 +7,10 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardNumber; import blackjack.domain.card.CardShape; +import blackjack.domain.player.Dealer; import blackjack.domain.player.Hand; import blackjack.domain.player.Participant; +import blackjack.domain.player.Player; import blackjack.domain.player.Players; import blackjack.domain.rule.Score; import java.util.stream.Collectors; @@ -26,7 +28,7 @@ public String resolveHandOutEventMessage(Players players, int handOutCount) { private String resolveNamesMessage(Players players) { return players.getPlayers().stream() - .map(Participant::getName) + .map(Player::getName) .collect(Collectors.joining(SEPARATOR)); } @@ -46,7 +48,7 @@ private String resolveCardMessage(Card card) { return String.format("%s%s", NUMBER_NAME.get(cardNumber), SHAPE_NAME.get(cardShape)); } - public String resolveDealerHandMessage(Participant dealer) { + public String resolveDealerHandMessage(Dealer dealer) { Card card = dealer.getHand().getCards().get(0); return String.format("%s: %s", dealer.getName(), resolveCardMessage(card)); } @@ -59,12 +61,12 @@ public String resolveDealerPopCountMessage(int dealerDrawThreshold, int popCount return ""; } - public String resolvePlayerScoreMessage(Participant participant, Score score) { + public String resolveParticipantScoreMessage(Participant participant, Score score) { String handMessage = resolvePlayerHandMessage(participant); return String.format("%s - 결과: %d", handMessage, score.getValue()); } - public String resolvePlayerGameResult(Participant participant, boolean win) { + public String resolvePlayerGameResult(Player participant, boolean win) { return String.format("%s: %s", participant.getName(), resolveGameResultMessage(win)); } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 1e09941a90..0048798f34 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,7 +1,9 @@ package blackjack.view; import blackjack.domain.DealerGameResult; +import blackjack.domain.player.Dealer; import blackjack.domain.player.Participant; +import blackjack.domain.player.Player; import blackjack.domain.player.Players; import blackjack.domain.rule.Score; @@ -17,7 +19,7 @@ public void printHandOutEvent(Players players, int handedCount) { System.out.println(messageResolver.resolveHandOutEventMessage(players, handedCount)); } - public void printPlayerHand(Participant participant) { + public void printPlayerHand(Player participant) { System.out.println(messageResolver.resolvePlayerHandMessage(participant)); } @@ -25,11 +27,11 @@ public void printDealerPopCount(int dealerPopThreshold, int count) { System.out.println(messageResolver.resolveDealerPopCountMessage(dealerPopThreshold, count)); } - public void printPlayerScore(Participant participant, Score score) { - System.out.println(messageResolver.resolvePlayerScoreMessage(participant, score)); + public void printParticipantScore(Participant participant, Score score) { + System.out.println(messageResolver.resolveParticipantScoreMessage(participant, score)); } - public void printPlayerGameResult(Participant participant, boolean win) { + public void printPlayerGameResult(Player participant, boolean win) { System.out.println(messageResolver.resolvePlayerGameResult(participant, win)); } @@ -37,7 +39,7 @@ public void printDealerGameResult(DealerGameResult dealerGameResult) { System.out.println(messageResolver.resolveDealerGameResult(dealerGameResult)); } - public void printDealerInitialHand(Participant dealer) { + public void printDealerInitialHand(Dealer dealer) { System.out.println(messageResolver.resolveDealerHandMessage(dealer)); } } From d756ac451327fd1c96c1788b608201b5cef4ffb4 Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 11 Mar 2024 00:04:57 +0900 Subject: [PATCH 122/124] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=B6=80=EB=B6=84=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Hand.java | 4 ---- src/test/java/fixture/CardDeckFixture.java | 13 ------------- 2 files changed, 17 deletions(-) delete mode 100644 src/test/java/fixture/CardDeckFixture.java diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index 53ff1f1af1..e522933c34 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -39,10 +39,6 @@ public int countAce() { .count(); } - public int countCard() { - return cards.size(); - } - public int countPop() { return cards.size() - INITIAL_HAND_SIZE; } diff --git a/src/test/java/fixture/CardDeckFixture.java b/src/test/java/fixture/CardDeckFixture.java deleted file mode 100644 index f270f6f9d9..0000000000 --- a/src/test/java/fixture/CardDeckFixture.java +++ /dev/null @@ -1,13 +0,0 @@ -package fixture; - -import blackjack.domain.card.CardDeck; -import java.util.Arrays; - -public class CardDeckFixture { - - public static CardDeck of(int... numbers) { - return new CardDeck(Arrays.stream(numbers) - .mapToObj(CardFixture::from) - .toList()); - } -} From 1464f133889154936485e8646451bed7f92686a8 Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 11 Mar 2024 00:06:29 +0900 Subject: [PATCH 123/124] =?UTF-8?q?test:=20TestFixture=20->=20TestDataCrea?= =?UTF-8?q?tor=EB=A1=9C=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD=EC=9D=84?= =?UTF-8?q?=20=ED=86=B5=ED=95=9C=20=EC=9D=98=EB=AF=B8=20=EA=B5=AC=EC=B2=B4?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/card/TestCardCreator.java} | 6 ++---- .../blackjack/domain/player/DealerTest.java | 5 ++--- .../blackjack/domain/player/HandTest.java | 19 +++++++++---------- .../blackjack/domain/player/PlayerTest.java | 5 ++--- .../blackjack/domain/player/PlayersTest.java | 17 ++++++++--------- .../domain/player/TestHandCreator.java} | 8 ++++---- .../domain/player/TestPlayerCreator.java | 8 ++++++++ .../java/blackjack/domain/rule/JudgeTest.java | 12 ++++++------ src/test/java/fixture/PlayerFixture.java | 11 ----------- 9 files changed, 41 insertions(+), 50 deletions(-) rename src/test/java/{fixture/CardFixture.java => blackjack/domain/card/TestCardCreator.java} (76%) rename src/test/java/{fixture/HandFixture.java => blackjack/domain/player/TestHandCreator.java} (57%) create mode 100644 src/test/java/blackjack/domain/player/TestPlayerCreator.java delete mode 100644 src/test/java/fixture/PlayerFixture.java diff --git a/src/test/java/fixture/CardFixture.java b/src/test/java/blackjack/domain/card/TestCardCreator.java similarity index 76% rename from src/test/java/fixture/CardFixture.java rename to src/test/java/blackjack/domain/card/TestCardCreator.java index 53b0aad843..50541eafff 100644 --- a/src/test/java/fixture/CardFixture.java +++ b/src/test/java/blackjack/domain/card/TestCardCreator.java @@ -1,12 +1,10 @@ -package fixture; +package blackjack.domain.card; import static blackjack.domain.card.CardShape.HEART; -import blackjack.domain.card.Card; -import blackjack.domain.card.CardNumber; import java.util.Arrays; -public class CardFixture { +public class TestCardCreator { public static Card from(int number) { CardNumber cardNumber = Arrays.stream(CardNumber.values()) diff --git a/src/test/java/blackjack/domain/player/DealerTest.java b/src/test/java/blackjack/domain/player/DealerTest.java index 6202fc84f6..c4003d07c0 100644 --- a/src/test/java/blackjack/domain/player/DealerTest.java +++ b/src/test/java/blackjack/domain/player/DealerTest.java @@ -2,7 +2,6 @@ import static org.assertj.core.api.Assertions.assertThat; -import fixture.HandFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -14,7 +13,7 @@ class DealerTest { @ParameterizedTest @CsvSource(value = {"6, 10", "5, 10", "4, 10"}) void testCannotHit(int card1, int card2) { - Dealer dealer = new Dealer(HandFixture.of(card1, card2)); + Dealer dealer = new Dealer(TestHandCreator.of(card1, card2)); assertThat(dealer.canHit()).isTrue(); } @@ -22,7 +21,7 @@ void testCannotHit(int card1, int card2) { @ParameterizedTest @CsvSource(value = {"7, 10", "8, 10", "9, 10"}) void testCanHit(int card1, int card2) { - Dealer dealer = new Dealer(HandFixture.of(card1, card2)); + Dealer dealer = new Dealer(TestHandCreator.of(card1, card2)); assertThat(dealer.canHit()).isFalse(); } } diff --git a/src/test/java/blackjack/domain/player/HandTest.java b/src/test/java/blackjack/domain/player/HandTest.java index d88b3d3f9a..6ec98853bb 100644 --- a/src/test/java/blackjack/domain/player/HandTest.java +++ b/src/test/java/blackjack/domain/player/HandTest.java @@ -4,8 +4,7 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; -import fixture.CardFixture; -import fixture.HandFixture; +import blackjack.domain.card.TestCardCreator; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -22,15 +21,15 @@ void testCreateInitialHandSize() { @DisplayName("카드의 합을 계산할 수 있다.") @Test void testHandSummation() { - Hand hand = HandFixture.of(2, 2, 2); + Hand hand = TestHandCreator.of(2, 2, 2); assertThat(hand.calculateCardSummation()).isEqualTo(6); } @DisplayName("특정 카드를 핸드에 추가할 수 있다") @Test void testAppendCard() { - Card card = CardFixture.from(2); - Hand hand = HandFixture.of(); + Card card = TestCardCreator.from(2); + Hand hand = TestHandCreator.of(); hand.appendCard(card); assertThat(hand.getCards()).containsExactly(card); } @@ -47,35 +46,35 @@ void testCountPop() { @DisplayName("핸드에 에이스 카드가 몇개 있는지 확인할 수 있다") @Test void testCountAceInHand() { - Hand hand = HandFixture.of(1, 1, 1, 3, 4, 5); + Hand hand = TestHandCreator.of(1, 1, 1, 3, 4, 5); assertThat(hand.countAce()).isEqualTo(3); } @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 없음") @Test void testCalculateScoreWithNoAce() { - Hand hand = HandFixture.of(2, 3, 4, 5); + Hand hand = TestHandCreator.of(2, 3, 4, 5); assertThat(hand.calculateScore().getValue()).isEqualTo(14); } @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 11로 이용됨") @Test void testCalculateScoreWithBigAce() { - Hand hand = HandFixture.of(1, 10); + Hand hand = TestHandCreator.of(1, 10); assertThat(hand.calculateScore().getValue()).isEqualTo(21); } @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드가 1로 이용됨") @Test void testCalculateScoreWithLowAce() { - Hand hand = HandFixture.of(1, 10, 2); + Hand hand = TestHandCreator.of(1, 10, 2); assertThat(hand.calculateScore().getValue()).isEqualTo(13); } @DisplayName("적절한 점수를 계산할 수 있다 - Ace 카드 두개 이상") @Test void testCalculateScoreWithMultipleAce() { - Hand hand = HandFixture.of(1, 1, 1); + Hand hand = TestHandCreator.of(1, 1, 1); assertThat(hand.calculateScore().getValue()).isEqualTo(13); } } diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index ac2a9a2e0f..ef15046bcd 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -2,7 +2,6 @@ import static org.assertj.core.api.Assertions.assertThat; -import fixture.HandFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -14,7 +13,7 @@ class PlayerTest { @ParameterizedTest @CsvSource(value = {"2, 10, 10", "3, 10, 10", "4, 10, 10"}) void testCannotHit(int card1, int card2, int card3) { - Player player = new Player(new PlayerName("썬"), HandFixture.of(card1, card2, card3)); + Player player = new Player(new PlayerName("썬"), TestHandCreator.of(card1, card2, card3)); assertThat(player.canHit()).isFalse(); } @@ -22,7 +21,7 @@ void testCannotHit(int card1, int card2, int card3) { @ParameterizedTest @CsvSource(value = {"1, 10, 10", "2, 8, 10", "2, 7, 10"}) void testCanHit(int card1, int card2, int card3) { - Player player = new Player(new PlayerName("썬"), HandFixture.of(card1, card2, card3)); + Player player = new Player(new PlayerName("썬"), TestHandCreator.of(card1, card2, card3)); assertThat(player.canHit()).isTrue(); } } diff --git a/src/test/java/blackjack/domain/player/PlayersTest.java b/src/test/java/blackjack/domain/player/PlayersTest.java index 8a4ea87a37..c4a6b9f6f0 100644 --- a/src/test/java/blackjack/domain/player/PlayersTest.java +++ b/src/test/java/blackjack/domain/player/PlayersTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import fixture.PlayerFixture; import java.util.List; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -14,8 +13,8 @@ class PlayersTest { @DisplayName("참가자들 중 이름이 중복되는 경우는 생성 검증에 실패한다") @Test void testCreatePlayersWithDuplicateNames() { - Player player1 = PlayerFixture.of("리비", 1, 2); - Player player2 = PlayerFixture.of("리비", 3, 4); + Player player1 = TestPlayerCreator.of("리비", 1, 2); + Player player2 = TestPlayerCreator.of("리비", 3, 4); assertThatThrownBy(() -> new Players(List.of(player1, player2))) .isInstanceOf(IllegalArgumentException.class) @@ -33,10 +32,10 @@ void testCreatePlayersWithEmptyEntry() { @DisplayName("딜러를 제외한 게임 참여자가 3명을 넘는 경우는 생성 검증에 실패한다") @Test void testCreatePlayersWithInvalidEntryCount() { - Player player1 = PlayerFixture.of("리비", 1, 2); - Player player2 = PlayerFixture.of("제리", 3, 4); - Player player3 = PlayerFixture.of("잉크", 1, 2); - Player player4 = PlayerFixture.of("트레", 3, 4); + Player player1 = TestPlayerCreator.of("리비", 1, 2); + Player player2 = TestPlayerCreator.of("제리", 3, 4); + Player player3 = TestPlayerCreator.of("잉크", 1, 2); + Player player4 = TestPlayerCreator.of("트레", 3, 4); assertThatThrownBy(() -> new Players(List.of(player1, player2, player3, player4))) .isInstanceOf(IllegalArgumentException.class) @@ -46,8 +45,8 @@ void testCreatePlayersWithInvalidEntryCount() { @DisplayName("생성 검증을 모두 통과하면 생성에 성공한다") @Test void testCreateWithValidPlayers() { - Player player1 = PlayerFixture.of("리비", 1, 2); - Player player2 = PlayerFixture.of("제리", 3, 4); + Player player1 = TestPlayerCreator.of("리비", 1, 2); + Player player2 = TestPlayerCreator.of("제리", 3, 4); assertThatCode(() -> new Players(List.of(player1, player2))) .doesNotThrowAnyException(); diff --git a/src/test/java/fixture/HandFixture.java b/src/test/java/blackjack/domain/player/TestHandCreator.java similarity index 57% rename from src/test/java/fixture/HandFixture.java rename to src/test/java/blackjack/domain/player/TestHandCreator.java index d8e0e4a5ee..8a4aeb229c 100644 --- a/src/test/java/fixture/HandFixture.java +++ b/src/test/java/blackjack/domain/player/TestHandCreator.java @@ -1,14 +1,14 @@ -package fixture; +package blackjack.domain.player; -import blackjack.domain.player.Hand; +import blackjack.domain.card.TestCardCreator; import java.util.Arrays; import java.util.stream.Collectors; -public class HandFixture { +public class TestHandCreator { public static Hand of(int... numbers) { return new Hand(Arrays.stream(numbers) - .mapToObj(CardFixture::from) + .mapToObj(TestCardCreator::from) .collect(Collectors.toList())); } } diff --git a/src/test/java/blackjack/domain/player/TestPlayerCreator.java b/src/test/java/blackjack/domain/player/TestPlayerCreator.java new file mode 100644 index 0000000000..003bf10f2a --- /dev/null +++ b/src/test/java/blackjack/domain/player/TestPlayerCreator.java @@ -0,0 +1,8 @@ +package blackjack.domain.player; + +public class TestPlayerCreator { + + public static Player of(String name, int... hand) { + return new Player(new PlayerName(name), TestHandCreator.of(hand)); + } +} diff --git a/src/test/java/blackjack/domain/rule/JudgeTest.java b/src/test/java/blackjack/domain/rule/JudgeTest.java index cd279a457e..fdf79a5e2d 100644 --- a/src/test/java/blackjack/domain/rule/JudgeTest.java +++ b/src/test/java/blackjack/domain/rule/JudgeTest.java @@ -7,8 +7,8 @@ import blackjack.domain.player.Dealer; import blackjack.domain.player.Player; import blackjack.domain.player.Players; -import fixture.HandFixture; -import fixture.PlayerFixture; +import blackjack.domain.player.TestHandCreator; +import blackjack.domain.player.TestPlayerCreator; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -26,16 +26,16 @@ void setUp() { @DisplayName("딜러와 플레이어 중 누가 이겼는지 알 수 있다") @Test void testSelectWinner() { - Player player = PlayerFixture.of("리비", 2, 3); - Dealer dealer = new Dealer(HandFixture.of(3, 4)); + Player player = TestPlayerCreator.of("리비", 2, 3); + Dealer dealer = new Dealer(TestHandCreator.of(3, 4)); assertThat(judge.isPlayerWin(dealer, player)).isFalse(); } @DisplayName("딜러의 전적을 계산할 수 있다") @Test void testCalculateDealerResult() { - Player player = PlayerFixture.of("리비", 2, 3); - Dealer dealer = new Dealer(HandFixture.of(3, 4)); + Player player = TestPlayerCreator.of("리비", 2, 3); + Dealer dealer = new Dealer(TestHandCreator.of(3, 4)); Players players = new Players(List.of(player)); DealerGameResult dealerGameResult = judge.calculateDealerGameResult(dealer, players); assertAll( diff --git a/src/test/java/fixture/PlayerFixture.java b/src/test/java/fixture/PlayerFixture.java deleted file mode 100644 index 5d8ddbd0eb..0000000000 --- a/src/test/java/fixture/PlayerFixture.java +++ /dev/null @@ -1,11 +0,0 @@ -package fixture; - -import blackjack.domain.player.Player; -import blackjack.domain.player.PlayerName; - -public class PlayerFixture { - - public static Player of(String name, int... hand) { - return new Player(new PlayerName(name), HandFixture.of(hand)); - } -} From 6bfeee68e11535101b6ebcef46318c2568843dc2 Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 11 Mar 2024 00:09:20 +0900 Subject: [PATCH 124/124] =?UTF-8?q?refactor:=20=EC=A0=95=EC=A0=81=20?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A6=AC=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EA=B0=80=EC=A7=80=EB=8A=94=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1=20=EB=A1=9C=EC=A7=81=20=ED=8D=BC?= =?UTF-8?q?=EC=A7=80=EC=A7=80=20=EC=95=8A=EB=8F=84=EB=A1=9D=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EC=9E=90=20=EC=A0=91=EA=B7=BC=20=EC=A0=9C=EC=96=B4?= =?UTF-8?q?=EC=9E=90=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/card/CardDeck.java | 2 +- src/main/java/blackjack/domain/player/Hand.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index 8b258ac885..8ad67fa1b0 100644 --- a/src/main/java/blackjack/domain/card/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -10,7 +10,7 @@ public class CardDeck { private final List cards; - public CardDeck(List cards) { + CardDeck(List cards) { this.cards = cards; } diff --git a/src/main/java/blackjack/domain/player/Hand.java b/src/main/java/blackjack/domain/player/Hand.java index e522933c34..6e99b759d2 100644 --- a/src/main/java/blackjack/domain/player/Hand.java +++ b/src/main/java/blackjack/domain/player/Hand.java @@ -14,7 +14,7 @@ public class Hand { private final List cards; - public Hand(List cards) { + Hand(List cards) { this.cards = cards; }