From 074adc61133af7572b0e833754a9aba7ec5f4ea5 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Tue, 28 Feb 2023 16:07:30 +0900 Subject: [PATCH 01/43] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000000..1991626932 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,45 @@ +## 블랙잭 게임 기능 목록 +### UI +- 입력 + - [ ] 참여할 사람의 이름 입력 + - [ ] 카드 추가 지급 의사 입력 + +- 출력 + - [ ] 카드 지급 완료 문구 출력 + - [ ] 개인 카드 목록 출력 + - [ ] 딜러 카드 추가 지급 여부 출력 + - [ ] 최종 결과 출력 + - [ ] 최종 승패 출력 + +### 기능 +- BlackJackGame : 게임 관리 시스템 + - [ ] 최종 승패를 계산한다 + +- Player : 게임 참여자 + - [ ] 초기 카드 2장을 받는다 + - [ ] 추가 카드를 뽑는다 + - Challenger : 일반 참여자 (Player를 상속받는다) + - [ ] 이름을 입력받는다 + - [ ] 카드를 뽑을지 말지 결정하다 + - Dealer : 딜러 (Player를 상속받는다) + - [ ] 쥐고 있는 카드의 합이 17이상인지 확인한다 + +- Players : Player 목록을 가지고 있는 일급 컬렉션 + +- Card : 카드 + - [ ] 카드 숫자를 보고 값을 반환한다 (ex. Ace -> 1 or 11 // King, Queen, Jack -> 10) + +- CardDeck : 덱에 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 + - [ ] 52장의 카드를 초기화한다 + - [ ] 게임 참여자에게 랜덤 카드를 전달한다 + +- HolingCards : 참여자가 소유하고 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 + - [ ] 가진 카드의 합이 21 초과인지 체크한다 + - [ ] 뽑은 카드를 저장한다 + - [ ] 가진 카드의 합을 반환한다 + +### 예외 사항 +- [ ] 플레이어 이름 중복 시 예외가 발생한다 +- [ ] 플레이어 이름이 `딜러`인 경우 예외가 발생한다 + +- [ ] 카드를 더 받겠느냐는 질문에 `y`, `n`을 입력하지 않으면 예외가 발생한다 From 2e463d4549f6232c17b4f70f8a341604961abe85 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Tue, 28 Feb 2023 16:42:28 +0900 Subject: [PATCH 02/43] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=EB=A5=BC=20=EB=B3=B4=EA=B3=A0=20=EC=A0=90=EC=88=98?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=98=ED=99=98=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=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 --- docs/README.md | 2 +- src/main/java/blackjack/domain/card/Card.java | 17 ++++++++++ .../java/blackjack/domain/card/Number.java | 33 +++++++++++++++++++ .../java/blackjack/domain/card/Shape.java | 18 ++++++++++ .../java/blackjack/domain/card/CardTest.java | 24 ++++++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/domain/card/Card.java create mode 100644 src/main/java/blackjack/domain/card/Number.java create mode 100644 src/main/java/blackjack/domain/card/Shape.java create mode 100644 src/test/java/blackjack/domain/card/CardTest.java diff --git a/docs/README.md b/docs/README.md index 1991626932..595ab472c6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -27,7 +27,7 @@ - Players : Player 목록을 가지고 있는 일급 컬렉션 - Card : 카드 - - [ ] 카드 숫자를 보고 값을 반환한다 (ex. Ace -> 1 or 11 // King, Queen, Jack -> 10) + - [x] 카드 숫자를 보고 점수를 반환한다 (ex. Ace -> 1 or 11 // King, Queen, Jack -> 10) - CardDeck : 덱에 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [ ] 52장의 카드를 초기화한다 diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java new file mode 100644 index 0000000000..238cb2c6fe --- /dev/null +++ b/src/main/java/blackjack/domain/card/Card.java @@ -0,0 +1,17 @@ +package blackjack.domain.card; + +public class Card { + + private final Shape shape; + private final Number number; + + public Card(Shape shape, Number number) { + this.shape = shape; + this.number = number; + } + + public int getPoint() { + // TODO : Ace의 경우 1 또는 11 반환 처리 + return number.getValue(); + } +} diff --git a/src/main/java/blackjack/domain/card/Number.java b/src/main/java/blackjack/domain/card/Number.java new file mode 100644 index 0000000000..98021ba87b --- /dev/null +++ b/src/main/java/blackjack/domain/card/Number.java @@ -0,0 +1,33 @@ +package blackjack.domain.card; + +public enum Number { + ACE("A", 1), + 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("J", 10), + QUEEN("Q", 10), + KING("K", 10); + + private final String number; + private final int value; + + Number(String number, int value) { + this.number = number; + this.value = value; + } + + public String getNumber() { + return number; + } + + public int getValue() { + return value; + } +} diff --git a/src/main/java/blackjack/domain/card/Shape.java b/src/main/java/blackjack/domain/card/Shape.java new file mode 100644 index 0000000000..9564da39cd --- /dev/null +++ b/src/main/java/blackjack/domain/card/Shape.java @@ -0,0 +1,18 @@ +package blackjack.domain.card; + +public enum Shape { + SPADE("스페이드"), + HEART("하트"), + CLOVER("클로버"), + DIAMOND("다이아몬드"); + + private final String name; + + Shape(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/src/test/java/blackjack/domain/card/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java new file mode 100644 index 0000000000..ab0be9c535 --- /dev/null +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -0,0 +1,24 @@ +package blackjack.domain.card; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class CardTest { + + @Test + @DisplayName("카드 생성 테스트") + void create_card() { + assertThatCode(() -> new Card(Shape.SPADE, Number.FOUR)) + .doesNotThrowAnyException(); + } + + @Test + @DisplayName("카드의 숫자를 보고 값을 반환한다") + void get_value() { + Card card = new Card(Shape.SPADE, Number.FOUR); + assertThat(card.getPoint()).isEqualTo(4); + } +} From 461bd275460ceca3824116dfc87a212d02ab2f7f Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 13:14:57 +0900 Subject: [PATCH 03/43] =?UTF-8?q?docs:=20=EC=B6=94=EA=B0=80=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=82=AC=ED=95=AD=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/README.md b/docs/README.md index 595ab472c6..922801374a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -43,3 +43,4 @@ - [ ] 플레이어 이름이 `딜러`인 경우 예외가 발생한다 - [ ] 카드를 더 받겠느냐는 질문에 `y`, `n`을 입력하지 않으면 예외가 발생한다 +- [ ] 더 이상 뽑을 카드가 없으면 카드 덱이 비어있다는 예외가 발생한다 From 44f7607f1313c3305d4bcfc9bcbd94b29f1b181c Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 13:24:59 +0900 Subject: [PATCH 04/43] =?UTF-8?q?feat:=2052=EC=9E=A5=EC=9D=98=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EC=83=9D=EC=84=B1=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 --- docs/README.md | 2 +- .../common/exception/CustomException.java | 10 +++++ .../java/blackjack/domain/card/CardDeck.java | 43 +++++++++++++++++++ .../card/exception/NoMoreCardException.java | 12 ++++++ .../blackjack/domain/card/CardDeckTest.java | 20 +++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/common/exception/CustomException.java create mode 100644 src/main/java/blackjack/domain/card/CardDeck.java create mode 100644 src/main/java/blackjack/domain/card/exception/NoMoreCardException.java create mode 100644 src/test/java/blackjack/domain/card/CardDeckTest.java diff --git a/docs/README.md b/docs/README.md index 922801374a..a0288e17ee 100644 --- a/docs/README.md +++ b/docs/README.md @@ -30,7 +30,7 @@ - [x] 카드 숫자를 보고 점수를 반환한다 (ex. Ace -> 1 or 11 // King, Queen, Jack -> 10) - CardDeck : 덱에 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - - [ ] 52장의 카드를 초기화한다 + - [x] 52장의 카드를 생성한다 - [ ] 게임 참여자에게 랜덤 카드를 전달한다 - HolingCards : 참여자가 소유하고 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 diff --git a/src/main/java/blackjack/common/exception/CustomException.java b/src/main/java/blackjack/common/exception/CustomException.java new file mode 100644 index 0000000000..72145fa46b --- /dev/null +++ b/src/main/java/blackjack/common/exception/CustomException.java @@ -0,0 +1,10 @@ +package blackjack.common.exception; + +public class CustomException extends RuntimeException { + + private static final String ERROR_PREFIX = "[ERROR] "; + + public CustomException(String message) { + super(ERROR_PREFIX + message); + } +} diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java new file mode 100644 index 0000000000..921633dde4 --- /dev/null +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -0,0 +1,43 @@ +package blackjack.domain.card; + +import blackjack.domain.card.exception.NoMoreCardException; +import java.util.ArrayList; +import java.util.List; + +public class CardDeck { + + private final List cards; + + private CardDeck(List cards) { + this.cards = cards; + } + + public static CardDeck create() { + List cards = new ArrayList<>(); + addShapeSets(cards); + return new CardDeck(cards); + } + + private static void addShapeSets(List cards) { + for (Shape shape : Shape.values()) { + addNumberSets(cards, shape); + } + } + + private static void addNumberSets(List cards, Shape shape) { + for (Number number : Number.values()) { + cards.add(new Card(shape, number)); + } + } + + public Card pick() { + validateCardExist(); + return cards.remove(0); + } + + private void validateCardExist() { + if (cards.isEmpty()) { + throw new NoMoreCardException(); + } + } +} diff --git a/src/main/java/blackjack/domain/card/exception/NoMoreCardException.java b/src/main/java/blackjack/domain/card/exception/NoMoreCardException.java new file mode 100644 index 0000000000..f47cdd05be --- /dev/null +++ b/src/main/java/blackjack/domain/card/exception/NoMoreCardException.java @@ -0,0 +1,12 @@ +package blackjack.domain.card.exception; + +import blackjack.common.exception.CustomException; + +public class NoMoreCardException extends CustomException { + + private static final String MESSAGE = "더 이상 뽑을 카드가 없습니다. 결과 창으로 넘어갑니다."; + + public NoMoreCardException() { + super(MESSAGE); + } +} diff --git a/src/test/java/blackjack/domain/card/CardDeckTest.java b/src/test/java/blackjack/domain/card/CardDeckTest.java new file mode 100644 index 0000000000..313f14da34 --- /dev/null +++ b/src/test/java/blackjack/domain/card/CardDeckTest.java @@ -0,0 +1,20 @@ +package blackjack.domain.card; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import blackjack.domain.card.exception.NoMoreCardException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class CardDeckTest { + + @Test + @DisplayName("카드 52장이 생성되는지 테스트") + void pick_random_card() { + CardDeck cardDeck = CardDeck.create(); + for (int i = 0; i < 52; i++) { + cardDeck.pick(); + } + assertThrows(NoMoreCardException.class, () -> cardDeck.pick()); + } +} From 464fed39e6576c86c2d8680b9d9017a9f66605ee Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 14:22:43 +0900 Subject: [PATCH 05/43] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=EB=BD=91?= =?UTF-8?q?=EA=B8=B0=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 --- docs/README.md | 3 ++- src/main/java/blackjack/domain/card/Card.java | 9 +++++++++ src/main/java/blackjack/domain/card/CardDeck.java | 7 ++++++- src/main/java/blackjack/domain/card/Number.java | 10 +++++----- .../java/blackjack/domain/card/CardDeckTest.java | 15 +++++++++++++++ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/docs/README.md b/docs/README.md index a0288e17ee..93e0910e98 100644 --- a/docs/README.md +++ b/docs/README.md @@ -31,7 +31,8 @@ - CardDeck : 덱에 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [x] 52장의 카드를 생성한다 - - [ ] 게임 참여자에게 랜덤 카드를 전달한다 + - [x] 카드 순서를 무작위로 섞는다 + - [x] 맨 위의 카드를 게임 참여자에게 전달한다 - HolingCards : 참여자가 소유하고 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [ ] 가진 카드의 합이 21 초과인지 체크한다 diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index 238cb2c6fe..80277e9939 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -10,6 +10,15 @@ public Card(Shape shape, Number number) { this.number = number; } + public Shape getShape() { + return shape; + } + + // TODO : string, number 어떤걸 get 해야할까? + public Number getNumber() { + return number; + } + public int getPoint() { // TODO : Ace의 경우 1 또는 11 반환 처리 return number.getValue(); diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index 921633dde4..541f1a586c 100644 --- a/src/main/java/blackjack/domain/card/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -2,11 +2,12 @@ import blackjack.domain.card.exception.NoMoreCardException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class CardDeck { - private final List cards; + private List cards; private CardDeck(List cards) { this.cards = cards; @@ -30,6 +31,10 @@ private static void addNumberSets(List cards, Shape shape) { } } + public void shuffle() { + Collections.shuffle(cards); + } + public Card pick() { validateCardExist(); return cards.remove(0); diff --git a/src/main/java/blackjack/domain/card/Number.java b/src/main/java/blackjack/domain/card/Number.java index 98021ba87b..a4d73ddc02 100644 --- a/src/main/java/blackjack/domain/card/Number.java +++ b/src/main/java/blackjack/domain/card/Number.java @@ -15,16 +15,16 @@ public enum Number { QUEEN("Q", 10), KING("K", 10); - private final String number; + private final String name; private final int value; - Number(String number, int value) { - this.number = number; + Number(String name, int value) { + this.name = name; this.value = value; } - public String getNumber() { - return number; + public String getName() { + return name; } public int getValue() { diff --git a/src/test/java/blackjack/domain/card/CardDeckTest.java b/src/test/java/blackjack/domain/card/CardDeckTest.java index 313f14da34..e9baff2db6 100644 --- a/src/test/java/blackjack/domain/card/CardDeckTest.java +++ b/src/test/java/blackjack/domain/card/CardDeckTest.java @@ -1,5 +1,6 @@ package blackjack.domain.card; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import blackjack.domain.card.exception.NoMoreCardException; @@ -17,4 +18,18 @@ void pick_random_card() { } assertThrows(NoMoreCardException.class, () -> cardDeck.pick()); } + + @Test + @DisplayName("카드 뽑기 테스트") + void pick_test() { + CardDeck cardDeck = CardDeck.create(); + Card spadeAce = cardDeck.pick(); + Card spadeTwo = cardDeck.pick(); + + assertThat(spadeAce.getShape()).isEqualTo(Shape.SPADE); + assertThat(spadeAce.getNumber()).isEqualTo(Number.ACE); + + assertThat(spadeTwo.getShape()).isEqualTo(Shape.SPADE); + assertThat(spadeTwo.getNumber()).isEqualTo(Number.TWO); + } } From cf351a53766151e7ab70710c1c56cd535ae673fd Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 14:36:44 +0900 Subject: [PATCH 06/43] =?UTF-8?q?feat:=20=EC=B4=88=EA=B8=B0=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=202=EC=9E=A5=EC=9D=84=20=EC=A7=80=EA=B8=89=EB=B0=9B?= =?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 --- docs/README.md | 4 +-- .../java/blackjack/domain/player/Player.java | 20 +++++++++++++++ .../blackjack/domain/player/PlayerTest.java | 25 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) 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/docs/README.md b/docs/README.md index 93e0910e98..148f08299e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,7 +16,7 @@ - [ ] 최종 승패를 계산한다 - Player : 게임 참여자 - - [ ] 초기 카드 2장을 받는다 + - [x] 초기 카드 2장을 받는다 - [ ] 추가 카드를 뽑는다 - Challenger : 일반 참여자 (Player를 상속받는다) - [ ] 이름을 입력받는다 @@ -32,7 +32,7 @@ - CardDeck : 덱에 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [x] 52장의 카드를 생성한다 - [x] 카드 순서를 무작위로 섞는다 - - [x] 맨 위의 카드를 게임 참여자에게 전달한다 + - [x] 맨 위의 카드를 게임 참여자에게 전달한ㅌ - HolingCards : 참여자가 소유하고 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [ ] 가진 카드의 합이 21 초과인지 체크한다 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..3d7f9e9d04 --- /dev/null +++ b/src/main/java/blackjack/domain/player/Player.java @@ -0,0 +1,20 @@ +package blackjack.domain.player; + +import blackjack.domain.card.Card; +import java.util.ArrayList; +import java.util.List; + +public class Player { + + private final List holdingCards = new ArrayList<>(); + + public void getInitialCards(List cards) { + for (Card card : cards) { + holdingCards.add(card); + } + } + + public List getHoldingCards() { + return List.copyOf(holdingCards); + } +} 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..9577e8d5cf --- /dev/null +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -0,0 +1,25 @@ +package blackjack.domain.player; + +import static org.assertj.core.api.Assertions.assertThat; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Number; +import blackjack.domain.card.Shape; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class PlayerTest { + + @Test + @DisplayName("플레이어는 초기 카드 2장을 받는다") + void get_two_cards() { + Player player = new Player(); + Card card1 = new Card(Shape.HEART, Number.FOUR); + Card card2 = new Card(Shape.CLOVER, Number.KING); + player.getInitialCards(List.of(card1, card2)); + + assertThat(player.getHoldingCards()) + .containsExactly(card1, card2); + } +} From 988d7a0295b77a73e8463a79700e9574219769c2 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 14:44:15 +0900 Subject: [PATCH 07/43] =?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=B9=B4=EB=93=9C=20=EB=BD=91=EA=B8=B0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/blackjack/domain/player/Player.java | 4 ++++ src/test/java/blackjack/domain/player/PlayerTest.java | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 148f08299e..73e6e2db20 100644 --- a/docs/README.md +++ b/docs/README.md @@ -17,7 +17,7 @@ - Player : 게임 참여자 - [x] 초기 카드 2장을 받는다 - - [ ] 추가 카드를 뽑는다 + - [x] 추가 카드를 뽑는다 - Challenger : 일반 참여자 (Player를 상속받는다) - [ ] 이름을 입력받는다 - [ ] 카드를 뽑을지 말지 결정하다 diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index 3d7f9e9d04..b08588cf57 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -17,4 +17,8 @@ public void getInitialCards(List cards) { public List getHoldingCards() { return List.copyOf(holdingCards); } + + public void pick(Card card) { + holdingCards.add(card); + } } diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index 9577e8d5cf..ffdbf43f6b 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -22,4 +22,15 @@ void get_two_cards() { assertThat(player.getHoldingCards()) .containsExactly(card1, card2); } + + @Test + @DisplayName("추가 카드를 뽑는다.") + void pick_card() { + Player player = new Player(); + Card card = new Card(Shape.DIAMOND, Number.JACK); + player.pick(card); + + assertThat(player.getHoldingCards()) + .contains(card); + } } From 5ecf658e0537dd7ac0f1c76e62e8d224a75c7c87 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 14:51:53 +0900 Subject: [PATCH 08/43] =?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=EC=9D=B4=20'=EB=94=9C=EB=9F=AC'?= =?UTF-8?q?=EC=9D=B4=EB=A9=B4=20=EC=98=88=EC=99=B8=EA=B0=80=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=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 --- docs/README.md | 4 ++-- .../blackjack/domain/player/Challenger.java | 21 +++++++++++++++++++ .../exception/InvalidPlayerNameException.java | 12 +++++++++++ .../domain/player/ChallengerTest.java | 17 +++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/main/java/blackjack/domain/player/Challenger.java create mode 100644 src/main/java/blackjack/domain/player/exception/InvalidPlayerNameException.java create mode 100644 src/test/java/blackjack/domain/player/ChallengerTest.java diff --git a/docs/README.md b/docs/README.md index 73e6e2db20..397f0c2d37 100644 --- a/docs/README.md +++ b/docs/README.md @@ -19,7 +19,7 @@ - [x] 초기 카드 2장을 받는다 - [x] 추가 카드를 뽑는다 - Challenger : 일반 참여자 (Player를 상속받는다) - - [ ] 이름을 입력받는다 + - [x] 이름을 입력받는다 - [ ] 카드를 뽑을지 말지 결정하다 - Dealer : 딜러 (Player를 상속받는다) - [ ] 쥐고 있는 카드의 합이 17이상인지 확인한다 @@ -41,7 +41,7 @@ ### 예외 사항 - [ ] 플레이어 이름 중복 시 예외가 발생한다 -- [ ] 플레이어 이름이 `딜러`인 경우 예외가 발생한다 +- [x] 플레이어 이름이 `딜러`인 경우 예외가 발생한다 - [ ] 카드를 더 받겠느냐는 질문에 `y`, `n`을 입력하지 않으면 예외가 발생한다 - [ ] 더 이상 뽑을 카드가 없으면 카드 덱이 비어있다는 예외가 발생한다 diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java new file mode 100644 index 0000000000..c03b884c5a --- /dev/null +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -0,0 +1,21 @@ +package blackjack.domain.player; + +import blackjack.domain.player.exception.InvalidPlayerNameException; + +public class Challenger extends Player { + + private static final String INVALID_NAME = "딜러"; + + private final String name; + + public Challenger(String name) { + validateName(name); + this.name = name; + } + + private void validateName(String name) { + if (name.equals(INVALID_NAME)) { + throw new InvalidPlayerNameException(); + } + } +} diff --git a/src/main/java/blackjack/domain/player/exception/InvalidPlayerNameException.java b/src/main/java/blackjack/domain/player/exception/InvalidPlayerNameException.java new file mode 100644 index 0000000000..ebdf173b1c --- /dev/null +++ b/src/main/java/blackjack/domain/player/exception/InvalidPlayerNameException.java @@ -0,0 +1,12 @@ +package blackjack.domain.player.exception; + +import blackjack.common.exception.CustomException; + +public class InvalidPlayerNameException extends CustomException { + + private static final String MESSAGE = "플레이어의 이름은 '딜러'이면 안됩니다."; + + public InvalidPlayerNameException() { + super(MESSAGE); + } +} diff --git a/src/test/java/blackjack/domain/player/ChallengerTest.java b/src/test/java/blackjack/domain/player/ChallengerTest.java new file mode 100644 index 0000000000..e8ed486ecf --- /dev/null +++ b/src/test/java/blackjack/domain/player/ChallengerTest.java @@ -0,0 +1,17 @@ +package blackjack.domain.player; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import blackjack.domain.player.exception.InvalidPlayerNameException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class ChallengerTest { + + @Test + @DisplayName("이름이 '딜러'인 경우 예외가 발생한다") + void validate_name() { + assertThrows(InvalidPlayerNameException.class, + () -> new Challenger("딜러")); + } +} From e1069e38f5e75d13ee731a673ccda0d54c736e2d Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 15:23:41 +0900 Subject: [PATCH 09/43] =?UTF-8?q?refactor:=20Player=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=20=EB=BD=91=EA=B8=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A5=BC=20?= =?UTF-8?q?Challenger=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A1=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/player/ChallengerTest.java | 28 +++++++++++++++ .../blackjack/domain/player/PlayerTest.java | 36 ------------------- 2 files changed, 28 insertions(+), 36 deletions(-) delete mode 100644 src/test/java/blackjack/domain/player/PlayerTest.java diff --git a/src/test/java/blackjack/domain/player/ChallengerTest.java b/src/test/java/blackjack/domain/player/ChallengerTest.java index e8ed486ecf..a428ed5c0a 100644 --- a/src/test/java/blackjack/domain/player/ChallengerTest.java +++ b/src/test/java/blackjack/domain/player/ChallengerTest.java @@ -1,13 +1,41 @@ package blackjack.domain.player; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import blackjack.domain.card.Card; +import blackjack.domain.card.Number; +import blackjack.domain.card.Shape; import blackjack.domain.player.exception.InvalidPlayerNameException; +import java.util.List; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; class ChallengerTest { + @Test + @DisplayName("플레이어는 초기 카드 2장을 받는다") + void get_two_cards() { + Player player = new Challenger("oing"); + Card card1 = new Card(Shape.HEART, Number.FOUR); + Card card2 = new Card(Shape.CLOVER, Number.KING); + player.getInitialCards(List.of(card1, card2)); + + assertThat(player.getHoldingCards()) + .containsExactly(card1, card2); + } + + @Test + @DisplayName("추가 카드를 뽑는다.") + void pick_card() { + Player player = new Challenger("ditoo"); + Card card = new Card(Shape.DIAMOND, Number.JACK); + player.pick(card); + + assertThat(player.getHoldingCards()) + .contains(card); + } + @Test @DisplayName("이름이 '딜러'인 경우 예외가 발생한다") void validate_name() { diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java deleted file mode 100644 index ffdbf43f6b..0000000000 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package blackjack.domain.player; - -import static org.assertj.core.api.Assertions.assertThat; - -import blackjack.domain.card.Card; -import blackjack.domain.card.Number; -import blackjack.domain.card.Shape; -import java.util.List; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -class PlayerTest { - - @Test - @DisplayName("플레이어는 초기 카드 2장을 받는다") - void get_two_cards() { - Player player = new Player(); - Card card1 = new Card(Shape.HEART, Number.FOUR); - Card card2 = new Card(Shape.CLOVER, Number.KING); - player.getInitialCards(List.of(card1, card2)); - - assertThat(player.getHoldingCards()) - .containsExactly(card1, card2); - } - - @Test - @DisplayName("추가 카드를 뽑는다.") - void pick_card() { - Player player = new Player(); - Card card = new Card(Shape.DIAMOND, Number.JACK); - player.pick(card); - - assertThat(player.getHoldingCards()) - .contains(card); - } -} From 0a03f1ec9cabfb3ebdc6e8636e8ef15d7321d240 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 15:37:06 +0900 Subject: [PATCH 10/43] =?UTF-8?q?refactor:=20HoldingCards=EB=A5=BC=20?= =?UTF-8?q?=EB=A7=8C=EB=93=A4=EC=96=B4=20=EC=9D=BC=EA=B8=89=20=EC=BB=AC?= =?UTF-8?q?=EB=A0=89=EC=85=98=EC=9C=BC=EB=A1=9C=20=EA=B0=9D=EC=B2=B4=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 --- docs/README.md | 2 +- .../blackjack/domain/player/Challenger.java | 6 +++++ .../blackjack/domain/player/HoldingCards.java | 22 +++++++++++++++++++ .../java/blackjack/domain/player/Player.java | 21 ++++++++++-------- .../domain/player/ChallengerTest.java | 6 ++--- 5 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 src/main/java/blackjack/domain/player/HoldingCards.java diff --git a/docs/README.md b/docs/README.md index 397f0c2d37..9015902471 100644 --- a/docs/README.md +++ b/docs/README.md @@ -36,7 +36,7 @@ - HolingCards : 참여자가 소유하고 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [ ] 가진 카드의 합이 21 초과인지 체크한다 - - [ ] 뽑은 카드를 저장한다 + - [x] 뽑은 카드를 저장한다 - [ ] 가진 카드의 합을 반환한다 ### 예외 사항 diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java index c03b884c5a..9e2bf0e6bb 100644 --- a/src/main/java/blackjack/domain/player/Challenger.java +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -18,4 +18,10 @@ private void validateName(String name) { throw new InvalidPlayerNameException(); } } + + @Override + public Boolean canPick() { + + return null; + } } diff --git a/src/main/java/blackjack/domain/player/HoldingCards.java b/src/main/java/blackjack/domain/player/HoldingCards.java new file mode 100644 index 0000000000..a24239253a --- /dev/null +++ b/src/main/java/blackjack/domain/player/HoldingCards.java @@ -0,0 +1,22 @@ +package blackjack.domain.player; + +import blackjack.domain.card.Card; +import java.util.ArrayList; +import java.util.List; + +public class HoldingCards { + + private final List cards = new ArrayList<>(); + + public void initialCard(List initialCards) { + cards.addAll(initialCards); + } + + public void add(Card card) { + cards.add(card); + } + + public List getCards() { + return cards; + } +} diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index b08588cf57..1fc1cea694 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -1,24 +1,27 @@ package blackjack.domain.player; import blackjack.domain.card.Card; -import java.util.ArrayList; import java.util.List; -public class Player { +public abstract class Player { - private final List holdingCards = new ArrayList<>(); + protected final HoldingCards holdingCards; - public void getInitialCards(List cards) { - for (Card card : cards) { - holdingCards.add(card); - } + protected Player() { + this.holdingCards = new HoldingCards(); } - public List getHoldingCards() { - return List.copyOf(holdingCards); + public void pickStartCards(List cards) { + holdingCards.initialCard(cards); + } + + public HoldingCards getHoldingCards() { + return holdingCards; } public void pick(Card card) { holdingCards.add(card); } + + public abstract Boolean canPick(); } diff --git a/src/test/java/blackjack/domain/player/ChallengerTest.java b/src/test/java/blackjack/domain/player/ChallengerTest.java index a428ed5c0a..aacbf010c6 100644 --- a/src/test/java/blackjack/domain/player/ChallengerTest.java +++ b/src/test/java/blackjack/domain/player/ChallengerTest.java @@ -19,9 +19,9 @@ void get_two_cards() { Player player = new Challenger("oing"); Card card1 = new Card(Shape.HEART, Number.FOUR); Card card2 = new Card(Shape.CLOVER, Number.KING); - player.getInitialCards(List.of(card1, card2)); + player.pickStartCards(List.of(card1, card2)); - assertThat(player.getHoldingCards()) + assertThat(player.getHoldingCards().getCards()) .containsExactly(card1, card2); } @@ -32,7 +32,7 @@ void pick_card() { Card card = new Card(Shape.DIAMOND, Number.JACK); player.pick(card); - assertThat(player.getHoldingCards()) + assertThat(player.getHoldingCards().getCards()) .contains(card); } From b0f2aea5110861e51743754eacd13401d0b0ac80 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 15:38:57 +0900 Subject: [PATCH 11/43] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 9015902471..4c458a6676 100644 --- a/docs/README.md +++ b/docs/README.md @@ -21,8 +21,9 @@ - Challenger : 일반 참여자 (Player를 상속받는다) - [x] 이름을 입력받는다 - [ ] 카드를 뽑을지 말지 결정하다 + - [ ] 카드의 합이 21이상인지 확인한다 - Dealer : 딜러 (Player를 상속받는다) - - [ ] 쥐고 있는 카드의 합이 17이상인지 확인한다 + - [ ] 카드의 합이 17이상인지 확인한다 - Players : Player 목록을 가지고 있는 일급 컬렉션 @@ -35,7 +36,6 @@ - [x] 맨 위의 카드를 게임 참여자에게 전달한ㅌ - HolingCards : 참여자가 소유하고 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - - [ ] 가진 카드의 합이 21 초과인지 체크한다 - [x] 뽑은 카드를 저장한다 - [ ] 가진 카드의 합을 반환한다 From 205100d0260362556af7f40f3a4cee267c9db182 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 15:45:44 +0900 Subject: [PATCH 12/43] =?UTF-8?q?feat:=20=EA=B0=80=EC=A7=84=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=EC=9D=98=20=ED=95=A9=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 --- docs/README.md | 2 +- .../blackjack/domain/player/HoldingCards.java | 7 ++++++ .../domain/player/HoldingCardsTest.java | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/test/java/blackjack/domain/player/HoldingCardsTest.java diff --git a/docs/README.md b/docs/README.md index 4c458a6676..ea2f250907 100644 --- a/docs/README.md +++ b/docs/README.md @@ -37,7 +37,7 @@ - HolingCards : 참여자가 소유하고 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [x] 뽑은 카드를 저장한다 - - [ ] 가진 카드의 합을 반환한다 + - [x] 가진 카드의 합을 반환한다 ### 예외 사항 - [ ] 플레이어 이름 중복 시 예외가 발생한다 diff --git a/src/main/java/blackjack/domain/player/HoldingCards.java b/src/main/java/blackjack/domain/player/HoldingCards.java index a24239253a..f0de082c5d 100644 --- a/src/main/java/blackjack/domain/player/HoldingCards.java +++ b/src/main/java/blackjack/domain/player/HoldingCards.java @@ -3,6 +3,7 @@ import blackjack.domain.card.Card; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class HoldingCards { @@ -19,4 +20,10 @@ public void add(Card card) { public List getCards() { return cards; } + + public int sum() { + return cards.stream() + .mapToInt(Card::getPoint) + .sum(); + } } diff --git a/src/test/java/blackjack/domain/player/HoldingCardsTest.java b/src/test/java/blackjack/domain/player/HoldingCardsTest.java new file mode 100644 index 0000000000..f6eff2bfce --- /dev/null +++ b/src/test/java/blackjack/domain/player/HoldingCardsTest.java @@ -0,0 +1,24 @@ +package blackjack.domain.player; + +import static org.assertj.core.api.Assertions.assertThat; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Number; +import blackjack.domain.card.Shape; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class HoldingCardsTest { + + @Test + @DisplayName("가진 카드의 합 테스트") + void sum_of_cards() { + HoldingCards holdingCards = new HoldingCards(); + Card card1 = new Card(Shape.DIAMOND, Number.KING); + Card card2 = new Card(Shape.SPADE, Number.FOUR); + holdingCards.initialCard(List.of(card1, card2)); + + assertThat(holdingCards.sum()).isEqualTo(14); + } +} From 8bc2ee0f51378c2416bd1cc8b38a80a8d257ca01 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 16:00:29 +0900 Subject: [PATCH 13/43] =?UTF-8?q?feat:=20challenger=EC=9D=98=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=EC=9D=98=20=ED=95=A9=EC=9D=B4=2021=EC=9D=B4=20?= =?UTF-8?q?=EB=84=98=EC=96=B4=EA=B0=80=EB=8A=94=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=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../blackjack/domain/player/Challenger.java | 4 +-- .../domain/player/ChallengerTest.java | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index ea2f250907..70748efdf4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -21,7 +21,7 @@ - Challenger : 일반 참여자 (Player를 상속받는다) - [x] 이름을 입력받는다 - [ ] 카드를 뽑을지 말지 결정하다 - - [ ] 카드의 합이 21이상인지 확인한다 + - [x] 카드의 합이 21 초과인지 확인한다 - Dealer : 딜러 (Player를 상속받는다) - [ ] 카드의 합이 17이상인지 확인한다 diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java index 9e2bf0e6bb..bf89dd0162 100644 --- a/src/main/java/blackjack/domain/player/Challenger.java +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -5,6 +5,7 @@ public class Challenger extends Player { private static final String INVALID_NAME = "딜러"; + private static final int MAXIMUM_POINT = 21; private final String name; @@ -21,7 +22,6 @@ private void validateName(String name) { @Override public Boolean canPick() { - - return null; + return holdingCards.sum() <= MAXIMUM_POINT; } } diff --git a/src/test/java/blackjack/domain/player/ChallengerTest.java b/src/test/java/blackjack/domain/player/ChallengerTest.java index aacbf010c6..0058fe3550 100644 --- a/src/test/java/blackjack/domain/player/ChallengerTest.java +++ b/src/test/java/blackjack/domain/player/ChallengerTest.java @@ -8,8 +8,12 @@ import blackjack.domain.card.Shape; import blackjack.domain.player.exception.InvalidPlayerNameException; import java.util.List; +import java.util.stream.Stream; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class ChallengerTest { @@ -42,4 +46,32 @@ void validate_name() { assertThrows(InvalidPlayerNameException.class, () -> new Challenger("딜러")); } + + @ParameterizedTest + @MethodSource("provideCards") + @DisplayName("가진 카드의 합이 21 초과인지 확인한다") + void checking_sum_is_over_21(List cards, boolean expected) { + Player player = new Challenger("neo"); + for (Card card : cards) { + player.pick(card); + } + + assertThat(player.canPick()).isEqualTo(expected); + } + + private static Stream provideCards() { + return Stream.of( + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Number.QUEEN), + new Card(Shape.CLOVER, Number.FIVE)), + true), + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Number.QUEEN), + new Card(Shape.CLOVER, Number.FIVE), + new Card(Shape.HEART, Number.EIGHT)), + false) + ); + } } From 7a6efb8f3eacee502fd95cbcb5a9b0c0555152f5 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 16:07:37 +0900 Subject: [PATCH 14/43] =?UTF-8?q?refactor:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EB=BD=91=EA=B8=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A5=BC=20Pla?= =?UTF-8?q?yer=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/player/ChallengerTest.java | 23 --------- .../blackjack/domain/player/PlayerTest.java | 48 +++++++++++++++++++ 2 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 src/test/java/blackjack/domain/player/PlayerTest.java diff --git a/src/test/java/blackjack/domain/player/ChallengerTest.java b/src/test/java/blackjack/domain/player/ChallengerTest.java index 0058fe3550..c4cc85cc4c 100644 --- a/src/test/java/blackjack/domain/player/ChallengerTest.java +++ b/src/test/java/blackjack/domain/player/ChallengerTest.java @@ -17,29 +17,6 @@ class ChallengerTest { - @Test - @DisplayName("플레이어는 초기 카드 2장을 받는다") - void get_two_cards() { - Player player = new Challenger("oing"); - Card card1 = new Card(Shape.HEART, Number.FOUR); - Card card2 = new Card(Shape.CLOVER, Number.KING); - player.pickStartCards(List.of(card1, card2)); - - assertThat(player.getHoldingCards().getCards()) - .containsExactly(card1, card2); - } - - @Test - @DisplayName("추가 카드를 뽑는다.") - void pick_card() { - Player player = new Challenger("ditoo"); - Card card = new Card(Shape.DIAMOND, Number.JACK); - player.pick(card); - - assertThat(player.getHoldingCards().getCards()) - .contains(card); - } - @Test @DisplayName("이름이 '딜러'인 경우 예외가 발생한다") void validate_name() { 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..62594fc0bf --- /dev/null +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -0,0 +1,48 @@ +package blackjack.domain.player; + +import static org.assertj.core.api.Assertions.assertThat; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Number; +import blackjack.domain.card.Shape; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class PlayerTest { + + private Player player; + + @BeforeEach + void setup() { + player = new Player() { + @Override + public Boolean canPick() { + return null; + } + }; + } + + + @Test + @DisplayName("플레이어는 초기 카드 2장을 받는다") + void get_two_cards() { + Card card1 = new Card(Shape.HEART, Number.FOUR); + Card card2 = new Card(Shape.CLOVER, Number.KING); + player.pickStartCards(List.of(card1, card2)); + + assertThat(player.getHoldingCards().getCards()) + .containsExactly(card1, card2); + } + + @Test + @DisplayName("추가 카드를 뽑는다.") + void pick_card() { + Card card = new Card(Shape.DIAMOND, Number.JACK); + player.pick(card); + + assertThat(player.getHoldingCards().getCards()) + .contains(card); + } +} From 86899c57c4d43e29666280755e05b846364c9d5f Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 16:13:25 +0900 Subject: [PATCH 15/43] =?UTF-8?q?feat:=20dealer=EC=9D=98=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=EC=9D=98=20=ED=95=A9=EC=9D=B4=2016=EC=9D=B4=20?= =?UTF-8?q?=EB=84=98=EC=96=B4=EA=B0=80=EB=8A=94=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=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../java/blackjack/domain/player/Dealer.java | 11 +++++ .../blackjack/domain/player/DealerTest.java | 43 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) 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/docs/README.md b/docs/README.md index 70748efdf4..68559b110b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -23,7 +23,7 @@ - [ ] 카드를 뽑을지 말지 결정하다 - [x] 카드의 합이 21 초과인지 확인한다 - Dealer : 딜러 (Player를 상속받는다) - - [ ] 카드의 합이 17이상인지 확인한다 + - [x] 카드의 합이 16 초과인지 확인한다 - Players : Player 목록을 가지고 있는 일급 컬렉션 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..a36518a97f --- /dev/null +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -0,0 +1,11 @@ +package blackjack.domain.player; + +public class Dealer extends Player { + + private static final int MAXIMUM_POINT_TO_PICK = 16; + + @Override + public Boolean canPick() { + return holdingCards.sum() <= MAXIMUM_POINT_TO_PICK; + } +} 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..0a5b5534a1 --- /dev/null +++ b/src/test/java/blackjack/domain/player/DealerTest.java @@ -0,0 +1,43 @@ +package blackjack.domain.player; + +import static org.assertj.core.api.Assertions.assertThat; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Number; +import blackjack.domain.card.Shape; +import java.util.List; +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; + +class DealerTest { + + @ParameterizedTest + @MethodSource("provideCards") + @DisplayName("가진 카드의 합이 16 초과인지 확인한다") + void checking_sum_is_over_16(List cards, boolean expected) { + Player player = new Dealer(); + for (Card card : cards) { + player.pick(card); + } + + assertThat(player.canPick()).isEqualTo(expected); + } + + private static Stream provideCards() { + return Stream.of( + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Number.QUEEN), + new Card(Shape.CLOVER, Number.FOUR)), + true), + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Number.QUEEN), + new Card(Shape.CLOVER, Number.SEVEN)), + false) + ); + } +} From 5a1d7879328bb11a3afe348856bef721a1ef4e07 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 16:26:52 +0900 Subject: [PATCH 16/43] =?UTF-8?q?feat:=20=EC=A4=91=EB=B3=B5=EB=90=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=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 --- docs/README.md | 2 +- .../java/blackjack/domain/player/Players.java | 32 +++++++++++++++++++ .../DuplicatedPlayerNameException.java | 12 +++++++ .../blackjack/domain/player/PlayersTest.java | 18 +++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/domain/player/Players.java create mode 100644 src/main/java/blackjack/domain/player/exception/DuplicatedPlayerNameException.java create mode 100644 src/test/java/blackjack/domain/player/PlayersTest.java diff --git a/docs/README.md b/docs/README.md index 68559b110b..209a295e3a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -40,7 +40,7 @@ - [x] 가진 카드의 합을 반환한다 ### 예외 사항 -- [ ] 플레이어 이름 중복 시 예외가 발생한다 +- [x] 플레이어 이름 중복 시 예외가 발생한다 - [x] 플레이어 이름이 `딜러`인 경우 예외가 발생한다 - [ ] 카드를 더 받겠느냐는 질문에 `y`, `n`을 입력하지 않으면 예외가 발생한다 diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java new file mode 100644 index 0000000000..736dcb7a08 --- /dev/null +++ b/src/main/java/blackjack/domain/player/Players.java @@ -0,0 +1,32 @@ +package blackjack.domain.player; + +import blackjack.domain.player.exception.DuplicatedPlayerNameException; +import java.util.List; +import java.util.stream.Collectors; + +public class Players { + + private final List players; + + private Players(List players) { + this.players = players; + } + + public static Players from(List names) { + validateDuplicatedNames(names); + List players = names.stream() + .map(Challenger::new) + .collect(Collectors.toUnmodifiableList()); + return new Players(players); + } + + private static void validateDuplicatedNames(List names) { + long distinctNameCount = names.stream() + .distinct() + .count(); + + if (names.size() != distinctNameCount) { + throw new DuplicatedPlayerNameException(); + } + } +} diff --git a/src/main/java/blackjack/domain/player/exception/DuplicatedPlayerNameException.java b/src/main/java/blackjack/domain/player/exception/DuplicatedPlayerNameException.java new file mode 100644 index 0000000000..11e6eb9cf3 --- /dev/null +++ b/src/main/java/blackjack/domain/player/exception/DuplicatedPlayerNameException.java @@ -0,0 +1,12 @@ +package blackjack.domain.player.exception; + +import blackjack.common.exception.CustomException; + +public class DuplicatedPlayerNameException extends CustomException { + + private static final String MESSAGE = "중복된 이름입니다."; + + public DuplicatedPlayerNameException() { + super(MESSAGE); + } +} diff --git a/src/test/java/blackjack/domain/player/PlayersTest.java b/src/test/java/blackjack/domain/player/PlayersTest.java new file mode 100644 index 0000000000..272df7d09e --- /dev/null +++ b/src/test/java/blackjack/domain/player/PlayersTest.java @@ -0,0 +1,18 @@ +package blackjack.domain.player; + +import static org.junit.jupiter.api.Assertions.*; + +import blackjack.domain.player.exception.DuplicatedPlayerNameException; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class PlayersTest { + + @Test + @DisplayName("플레이어의 이름이 중복되면 예외가 발생한다") + void checking_player_name_duplicated() { + assertThrows(DuplicatedPlayerNameException.class, + () -> Players.from(List.of("pobi", "pobi"))); + } +} From eaf15562b533064438f9e44436c02fe9b7b9a0a0 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 17:21:48 +0900 Subject: [PATCH 17/43] =?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=EC=9E=85=EB=A0=A5=20=EA=B8=B0?= =?UTF-8?q?=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 --- docs/README.md | 2 +- .../controller/BlackJackController.java | 27 ++++++++++++++++ .../java/blackjack/domain/BlackJackGame.java | 30 ++++++++++++++++++ .../java/blackjack/domain/card/CardDeck.java | 2 +- src/main/java/blackjack/view/InputView.java | 31 +++++++++++++++++++ src/main/java/blackjack/view/OutputView.java | 8 +++++ 6 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/main/java/blackjack/controller/BlackJackController.java create mode 100644 src/main/java/blackjack/domain/BlackJackGame.java create mode 100644 src/main/java/blackjack/view/InputView.java create mode 100644 src/main/java/blackjack/view/OutputView.java diff --git a/docs/README.md b/docs/README.md index 209a295e3a..c0c20a7c33 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,7 +1,7 @@ ## 블랙잭 게임 기능 목록 ### UI - 입력 - - [ ] 참여할 사람의 이름 입력 + - [x] 참여할 사람의 이름 입력 - [ ] 카드 추가 지급 의사 입력 - 출력 diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java new file mode 100644 index 0000000000..cbf4c6825a --- /dev/null +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -0,0 +1,27 @@ +package blackjack.controller; + +import blackjack.common.exception.CustomException; +import blackjack.domain.BlackJackGame; +import blackjack.view.InputView; +import blackjack.view.OutputView; +import java.util.List; + +public class BlackJackController { + + private BlackJackGame blackJackGame; + + public void run() { + init(); + InputView.terminate(); + } + + private void init() { + try { + List playerNames = InputView.inputPlayerNames(); + blackJackGame = BlackJackGame.from(playerNames); + } catch (CustomException e) { + OutputView.printErrorMessage(e); + init(); + } + } +} diff --git a/src/main/java/blackjack/domain/BlackJackGame.java b/src/main/java/blackjack/domain/BlackJackGame.java new file mode 100644 index 0000000000..ffbb27063c --- /dev/null +++ b/src/main/java/blackjack/domain/BlackJackGame.java @@ -0,0 +1,30 @@ +package blackjack.domain; + +import blackjack.domain.card.CardDeck; +import blackjack.domain.player.Players; +import java.util.List; + +public class BlackJackGame { + private final CardDeck cardDeck; + private final Players players; + + private BlackJackGame(CardDeck cardDeck, Players players) { + this.cardDeck = cardDeck; + this.players = players; + } + + public static BlackJackGame from(List names) { + CardDeck cardDeck = CardDeck.create(); + Players players = Players.from(names); + return new BlackJackGame(cardDeck, players); + } + + public void handOutStartCards() { + cardDeck.shuffle(); + players.pickStartCards(cardDeck); + } + + public Players getPlayers() { + return players; + } +} diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index 541f1a586c..a0b4732fd7 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 List cards; + private final List cards; private CardDeck(List cards) { this.cards = cards; diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java new file mode 100644 index 0000000000..fd2935ff0e --- /dev/null +++ b/src/main/java/blackjack/view/InputView.java @@ -0,0 +1,31 @@ +package blackjack.view; + +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; + +public class InputView { + + private static final Scanner scanner = new Scanner(System.in); + + private static final String DELIMITER = ","; + private static final String REQUEST_PLAYER_NAME = "게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"; + + public static List inputPlayerNames() { + System.out.println(REQUEST_PLAYER_NAME); + + String playerNames = scanner.nextLine(); + return splitInputByDelimiter(playerNames); + } + + private static List splitInputByDelimiter(String input) { + return Arrays.stream(input.split(DELIMITER)) + .map(String::trim) + .collect(Collectors.toUnmodifiableList()); + } + + public static void terminate() { + scanner.close(); + } +} diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java new file mode 100644 index 0000000000..8a088438ad --- /dev/null +++ b/src/main/java/blackjack/view/OutputView.java @@ -0,0 +1,8 @@ +package blackjack.view; + +public class OutputView { + + public static void printErrorMessage(Exception exception) { + System.out.println(exception.getMessage()); + } +} From 7f1ab245ea76141323b639fc65cdadfd3783a4e1 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 17:22:33 +0900 Subject: [PATCH 18/43] =?UTF-8?q?refactor:=20=EC=B4=88=EA=B8=B0=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=EC=84=B8=ED=8C=85=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=ED=98=95?= =?UTF-8?q?=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/player/HoldingCards.java | 4 ++-- src/main/java/blackjack/domain/player/Player.java | 5 ++--- src/main/java/blackjack/domain/player/Players.java | 13 ++++++++++++- .../blackjack/domain/player/HoldingCardsTest.java | 2 +- .../java/blackjack/domain/player/PlayerTest.java | 3 +-- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/java/blackjack/domain/player/HoldingCards.java b/src/main/java/blackjack/domain/player/HoldingCards.java index f0de082c5d..fd8dbcb168 100644 --- a/src/main/java/blackjack/domain/player/HoldingCards.java +++ b/src/main/java/blackjack/domain/player/HoldingCards.java @@ -9,8 +9,8 @@ public class HoldingCards { private final List cards = new ArrayList<>(); - public void initialCard(List initialCards) { - cards.addAll(initialCards); + public void initialCard(Card card1, Card card2) { + cards.addAll(List.of(card1, card2)); } public void add(Card card) { diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index 1fc1cea694..1fc35aa281 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -1,7 +1,6 @@ package blackjack.domain.player; import blackjack.domain.card.Card; -import java.util.List; public abstract class Player { @@ -11,8 +10,8 @@ protected Player() { this.holdingCards = new HoldingCards(); } - public void pickStartCards(List cards) { - holdingCards.initialCard(cards); + public void pickStartCards(Card card1, Card card2) { + holdingCards.initialCard(card1, card2); } public HoldingCards getHoldingCards() { diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index 736dcb7a08..d7f2da2a16 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -1,5 +1,7 @@ package blackjack.domain.player; +import blackjack.domain.card.Card; +import blackjack.domain.card.CardDeck; import blackjack.domain.player.exception.DuplicatedPlayerNameException; import java.util.List; import java.util.stream.Collectors; @@ -16,7 +18,8 @@ public static Players from(List names) { validateDuplicatedNames(names); List players = names.stream() .map(Challenger::new) - .collect(Collectors.toUnmodifiableList()); + .collect(Collectors.toList()); + players.add(new Dealer()); return new Players(players); } @@ -29,4 +32,12 @@ private static void validateDuplicatedNames(List names) { throw new DuplicatedPlayerNameException(); } } + + public void pickStartCards(CardDeck cardDeck) { + for (Player player : players) { + Card card1 = cardDeck.pick(); + Card card2 = cardDeck.pick(); + player.pickStartCards(card1, card2); + } + } } diff --git a/src/test/java/blackjack/domain/player/HoldingCardsTest.java b/src/test/java/blackjack/domain/player/HoldingCardsTest.java index f6eff2bfce..5eac908572 100644 --- a/src/test/java/blackjack/domain/player/HoldingCardsTest.java +++ b/src/test/java/blackjack/domain/player/HoldingCardsTest.java @@ -17,7 +17,7 @@ void sum_of_cards() { HoldingCards holdingCards = new HoldingCards(); Card card1 = new Card(Shape.DIAMOND, Number.KING); Card card2 = new Card(Shape.SPADE, Number.FOUR); - holdingCards.initialCard(List.of(card1, card2)); + holdingCards.initialCard(card1, card2); assertThat(holdingCards.sum()).isEqualTo(14); } diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index 62594fc0bf..b18530391c 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -5,7 +5,6 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Number; import blackjack.domain.card.Shape; -import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -30,7 +29,7 @@ public Boolean canPick() { void get_two_cards() { Card card1 = new Card(Shape.HEART, Number.FOUR); Card card2 = new Card(Shape.CLOVER, Number.KING); - player.pickStartCards(List.of(card1, card2)); + player.pickStartCards(card1, card2); assertThat(player.getHoldingCards().getCards()) .containsExactly(card1, card2); From 8a7797ef455814cedc81033811dc7ef63419e2ca Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 19:59:16 +0900 Subject: [PATCH 19/43] =?UTF-8?q?feat:=20=EB=8F=84=EC=A0=84=EC=9E=90,=20?= =?UTF-8?q?=EB=94=9C=EB=9F=AC=20=EA=B5=AC=EB=B6=84=ED=95=B4=EC=A3=BC?= =?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 --- .../blackjack/domain/player/Challenger.java | 5 +++++ .../java/blackjack/domain/player/Dealer.java | 7 ++++++ .../java/blackjack/domain/player/Player.java | 2 ++ .../java/blackjack/domain/player/Players.java | 21 +++++++++++++++--- .../blackjack/domain/player/PlayerTest.java | 5 +++++ .../blackjack/domain/player/PlayersTest.java | 22 ++++++++++++++++++- 6 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java index bf89dd0162..21b040ec49 100644 --- a/src/main/java/blackjack/domain/player/Challenger.java +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -24,4 +24,9 @@ private void validateName(String name) { public Boolean canPick() { return holdingCards.sum() <= MAXIMUM_POINT; } + + @Override + public String getName() { + return name; + } } diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java index a36518a97f..fdfbb3faf2 100644 --- a/src/main/java/blackjack/domain/player/Dealer.java +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -2,10 +2,17 @@ public class Dealer extends Player { + private static final String NAME = "딜러"; + private static final int MAXIMUM_POINT_TO_PICK = 16; @Override public Boolean canPick() { return holdingCards.sum() <= MAXIMUM_POINT_TO_PICK; } + + @Override + public String getName() { + return NAME; + } } diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index 1fc35aa281..e7afdbb5c6 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -23,4 +23,6 @@ public void pick(Card card) { } public abstract Boolean canPick(); + + public abstract String getName(); } diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index d7f2da2a16..07168242e0 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -3,6 +3,7 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; import blackjack.domain.player.exception.DuplicatedPlayerNameException; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -16,10 +17,11 @@ private Players(List players) { public static Players from(List names) { validateDuplicatedNames(names); - List players = names.stream() - .map(Challenger::new) - .collect(Collectors.toList()); + List players = new ArrayList<>(); players.add(new Dealer()); + names.stream() + .map(Challenger::new) + .forEach(players::add); return new Players(players); } @@ -40,4 +42,17 @@ public void pickStartCards(CardDeck cardDeck) { player.pickStartCards(card1, card2); } } + + public List getChallengers() { + return players.stream() + .filter(player -> player instanceof Challenger) + .collect(Collectors.toUnmodifiableList()); + } + + public Player getDealer() { + return players.stream() + .filter(player -> player instanceof Dealer) + .findFirst() + .orElse(null); + } } diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index b18530391c..9c43d88f08 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -20,6 +20,11 @@ void setup() { public Boolean canPick() { return null; } + + @Override + public String getName() { + return null; + } }; } diff --git a/src/test/java/blackjack/domain/player/PlayersTest.java b/src/test/java/blackjack/domain/player/PlayersTest.java index 272df7d09e..754f3d8ebf 100644 --- a/src/test/java/blackjack/domain/player/PlayersTest.java +++ b/src/test/java/blackjack/domain/player/PlayersTest.java @@ -1,6 +1,7 @@ package blackjack.domain.player; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; import blackjack.domain.player.exception.DuplicatedPlayerNameException; import java.util.List; @@ -15,4 +16,23 @@ void checking_player_name_duplicated() { assertThrows(DuplicatedPlayerNameException.class, () -> Players.from(List.of("pobi", "pobi"))); } + + @Test + @DisplayName("challenger만 반환하는지 테스트") + void return_challengers() { + Players players = Players.from(List.of("pobi", "oing")); + List challengers = players.getChallengers(); + + challengers.forEach(challenger -> + assertThat(challenger).isInstanceOf(Challenger.class)); + } + + @Test + @DisplayName("dealer만 반환하는지 테스트") + void return_dealer() { + Players players = Players.from(List.of("pobi", "oing")); + Player dealer = players.getDealer(); + + assertThat(dealer).isInstanceOf(Dealer.class); + } } From fd7fa5e9ea81f108aadb0ff370e98a9c70d01fe2 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 20:00:47 +0900 Subject: [PATCH 20/43] =?UTF-8?q?feat:=20=EC=B4=88=EA=B8=B0=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=20=EC=A7=80=EA=B8=89=20=EC=83=81=ED=83=9C=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=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 --- docs/README.md | 11 +++-- .../controller/BlackJackController.java | 25 +++++++++++ .../java/blackjack/domain/BlackJackGame.java | 9 ++++ .../blackjack/domain/player/HoldingCards.java | 3 +- .../java/blackjack/dto/PlayerStatusDto.java | 34 ++++++++++++++ src/main/java/blackjack/view/OutputView.java | 45 +++++++++++++++++++ 6 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 src/main/java/blackjack/dto/PlayerStatusDto.java diff --git a/docs/README.md b/docs/README.md index c0c20a7c33..6f1a1761d1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,8 +5,8 @@ - [ ] 카드 추가 지급 의사 입력 - 출력 - - [ ] 카드 지급 완료 문구 출력 - - [ ] 개인 카드 목록 출력 + - [x] 카드 지급 완료 문구 출력 + - [x] 개인 카드 목록 출력 - [ ] 딜러 카드 추가 지급 여부 출력 - [ ] 최종 결과 출력 - [ ] 최종 승패 출력 @@ -33,7 +33,7 @@ - CardDeck : 덱에 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [x] 52장의 카드를 생성한다 - [x] 카드 순서를 무작위로 섞는다 - - [x] 맨 위의 카드를 게임 참여자에게 전달한ㅌ + - [x] 맨 위의 카드를 게임 참여자에게 전달한다 - HolingCards : 참여자가 소유하고 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [x] 뽑은 카드를 저장한다 @@ -45,3 +45,8 @@ - [ ] 카드를 더 받겠느냐는 질문에 `y`, `n`을 입력하지 않으면 예외가 발생한다 - [ ] 더 이상 뽑을 카드가 없으면 카드 덱이 비어있다는 예외가 발생한다 + +--------- +## 고민 +- dto에도 정적 팩토리 메소드를 써야할까? +- diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index cbf4c6825a..61f587a555 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -2,8 +2,11 @@ import blackjack.common.exception.CustomException; import blackjack.domain.BlackJackGame; +import blackjack.domain.player.Player; +import blackjack.dto.PlayerStatusDto; import blackjack.view.InputView; import blackjack.view.OutputView; +import java.util.ArrayList; import java.util.List; public class BlackJackController { @@ -12,9 +15,31 @@ public class BlackJackController { public void run() { init(); + + blackJackGame.handOutStartCards(); + + PlayerStatusDto dealer = makeDealerStatus(); + List challengers = makeChallengersStatus(); + OutputView.printStartStatus(dealer, challengers); + InputView.terminate(); } + private List makeChallengersStatus() { + List players = blackJackGame.getChallengers(); + List gameStatus = new ArrayList<>(); + for (Player player : players) { + PlayerStatusDto playerStatusDto = new PlayerStatusDto(player); + gameStatus.add(playerStatusDto); + } + return gameStatus; + } + + private PlayerStatusDto makeDealerStatus() { + Player dealer = blackJackGame.getDealer(); + return new PlayerStatusDto(dealer); + } + private void init() { try { List playerNames = InputView.inputPlayerNames(); diff --git a/src/main/java/blackjack/domain/BlackJackGame.java b/src/main/java/blackjack/domain/BlackJackGame.java index ffbb27063c..6e45d686d0 100644 --- a/src/main/java/blackjack/domain/BlackJackGame.java +++ b/src/main/java/blackjack/domain/BlackJackGame.java @@ -1,6 +1,7 @@ package blackjack.domain; import blackjack.domain.card.CardDeck; +import blackjack.domain.player.Player; import blackjack.domain.player.Players; import java.util.List; @@ -27,4 +28,12 @@ public void handOutStartCards() { public Players getPlayers() { return players; } + + public Player getDealer() { + return players.getDealer(); + } + + public List getChallengers() { + return players.getChallengers(); + } } diff --git a/src/main/java/blackjack/domain/player/HoldingCards.java b/src/main/java/blackjack/domain/player/HoldingCards.java index fd8dbcb168..24820aa861 100644 --- a/src/main/java/blackjack/domain/player/HoldingCards.java +++ b/src/main/java/blackjack/domain/player/HoldingCards.java @@ -3,7 +3,6 @@ import blackjack.domain.card.Card; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; public class HoldingCards { @@ -18,7 +17,7 @@ public void add(Card card) { } public List getCards() { - return cards; + return List.copyOf(cards); } public int sum() { diff --git a/src/main/java/blackjack/dto/PlayerStatusDto.java b/src/main/java/blackjack/dto/PlayerStatusDto.java new file mode 100644 index 0000000000..2f76c2a686 --- /dev/null +++ b/src/main/java/blackjack/dto/PlayerStatusDto.java @@ -0,0 +1,34 @@ +package blackjack.dto; + +import blackjack.domain.card.Card; +import blackjack.domain.player.Player; +import java.util.ArrayList; +import java.util.List; + +public class PlayerStatusDto { + + private final String name; + private final List cards; + + public PlayerStatusDto(Player player) { + this.name = player.getName(); + this.cards = extractCardInfo(player); + } + + private List extractCardInfo(Player player) { + List cardInfo = new ArrayList<>(); + List inputCards = player.getHoldingCards().getCards(); + for (Card card : inputCards) { + cardInfo.add(card.getNumber().getName()+card.getShape().getName()); + } + return cardInfo; + } + + public String getName() { + return name; + } + + public List getCards() { + return cards; + } +} diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 8a088438ad..72584870be 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,8 +1,53 @@ package blackjack.view; +import blackjack.dto.PlayerStatusDto; +import java.util.List; +import java.util.stream.Collectors; + public class OutputView { + private static final String GIVE_START_CARD_COMPLETE_MESSAGE = "에게 2장을 나누었습니다."; + private static final String CARD = "카드"; + private static final String ITEM_DELIMITER = ", "; + private static final String PLAYER_NAME_AND_CARDS_PARTITION = ": "; + public static void printErrorMessage(Exception exception) { System.out.println(exception.getMessage()); } + + public static void printStartStatus(PlayerStatusDto dealerStatus, List challengersStatus) { + printGivenMessage(dealerStatus, challengersStatus); + printDealerStatus(dealerStatus); + printChallengersStatus(challengersStatus); + } + + private static void printGivenMessage(PlayerStatusDto dealerStatus, List challengersStatus) { + System.out.print(dealerStatus.getName() + "와 "); + String challengerNames = String.join(ITEM_DELIMITER, toChallengerNames(challengersStatus)); + System.out.print(challengerNames); + System.out.println(GIVE_START_CARD_COMPLETE_MESSAGE); + } + + private static void printDealerStatus(PlayerStatusDto dealerStatus) { + System.out.print(dealerStatus.getName()); + System.out.print(PLAYER_NAME_AND_CARDS_PARTITION); + String cards = String.join(ITEM_DELIMITER, dealerStatus.getCards()); + System.out.println(cards); + } + + private static void printChallengersStatus(List challengersStatus) { + for (PlayerStatusDto challenger : challengersStatus) { + System.out.print(challenger.getName() + CARD); + System.out.print(PLAYER_NAME_AND_CARDS_PARTITION); + String cards = String.join(ITEM_DELIMITER, challenger.getCards()); + System.out.println(cards); + } + System.out.println(); + } + + private static List toChallengerNames(List challengersStatus) { + return challengersStatus.stream() + .map(PlayerStatusDto::getName) + .collect(Collectors.toUnmodifiableList()); + } } From cdc1882a77802330121709c55ffede961e5cd4c4 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 20:57:47 +0900 Subject: [PATCH 21/43] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EB=BD=91=EA=B8=B0=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0?= =?UTF-8?q?=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 --- docs/README.md | 3 +- src/main/java/blackjack/Application.java | 10 +++++ .../controller/BlackJackController.java | 44 ++++++++++++++++--- .../java/blackjack/domain/BlackJackGame.java | 10 ++++- src/main/java/blackjack/view/InputView.java | 21 +++++++++ .../exception/InvalidChoiceException.java | 12 +++++ .../java/blackjack/domain/card/CardTest.java | 8 ---- 7 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 src/main/java/blackjack/Application.java create mode 100644 src/main/java/blackjack/view/exception/InvalidChoiceException.java diff --git a/docs/README.md b/docs/README.md index 6f1a1761d1..881330c87f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ ### UI - 입력 - [x] 참여할 사람의 이름 입력 - - [ ] 카드 추가 지급 의사 입력 + - [x] 카드 추가 지급 의사 입력 - 출력 - [x] 카드 지급 완료 문구 출력 @@ -20,7 +20,6 @@ - [x] 추가 카드를 뽑는다 - Challenger : 일반 참여자 (Player를 상속받는다) - [x] 이름을 입력받는다 - - [ ] 카드를 뽑을지 말지 결정하다 - [x] 카드의 합이 21 초과인지 확인한다 - Dealer : 딜러 (Player를 상속받는다) - [x] 카드의 합이 16 초과인지 확인한다 diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java new file mode 100644 index 0000000000..3f73ad1465 --- /dev/null +++ b/src/main/java/blackjack/Application.java @@ -0,0 +1,10 @@ +package blackjack; + +import blackjack.controller.BlackJackController; + +public class Application { + public static void main(String[] args) { + BlackJackController blackJackController = new BlackJackController(); + blackJackController.run(); + } +} diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 61f587a555..ed76f2af16 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -22,9 +22,26 @@ public void run() { List challengers = makeChallengersStatus(); OutputView.printStartStatus(dealer, challengers); + takeAllPlayersTurn(); + InputView.terminate(); } + private void init() { + try { + List playerNames = InputView.inputPlayerNames(); + blackJackGame = BlackJackGame.from(playerNames); + } catch (CustomException e) { + OutputView.printErrorMessage(e); + init(); + } + } + + private PlayerStatusDto makeDealerStatus() { + Player dealer = blackJackGame.getDealer(); + return new PlayerStatusDto(dealer); + } + private List makeChallengersStatus() { List players = blackJackGame.getChallengers(); List gameStatus = new ArrayList<>(); @@ -35,18 +52,31 @@ private List makeChallengersStatus() { return gameStatus; } - private PlayerStatusDto makeDealerStatus() { - Player dealer = blackJackGame.getDealer(); - return new PlayerStatusDto(dealer); + private void takeAllPlayersTurn() { + for (Player player : blackJackGame.getChallengers()) { + takeEachPlayerTurn(player); + } } - private void init() { + private void takeEachPlayerTurn(Player player) { + while (blackJackGame.canPick(player)) { + checkChoice(player); + } + } + + private void checkChoice(Player player) { try { - List playerNames = InputView.inputPlayerNames(); - blackJackGame = BlackJackGame.from(playerNames); + inputChoice(player); } catch (CustomException e) { OutputView.printErrorMessage(e); - init(); + checkChoice(player); + } + } + + private void inputChoice(Player player) { + boolean choice = InputView.inputPlayerChoice(player.getName()); + if (choice) { + blackJackGame.pick(player); } } } diff --git a/src/main/java/blackjack/domain/BlackJackGame.java b/src/main/java/blackjack/domain/BlackJackGame.java index 6e45d686d0..253b1fe132 100644 --- a/src/main/java/blackjack/domain/BlackJackGame.java +++ b/src/main/java/blackjack/domain/BlackJackGame.java @@ -1,5 +1,6 @@ package blackjack.domain; +import blackjack.domain.card.Card; import blackjack.domain.card.CardDeck; import blackjack.domain.player.Player; import blackjack.domain.player.Players; @@ -25,8 +26,13 @@ public void handOutStartCards() { players.pickStartCards(cardDeck); } - public Players getPlayers() { - return players; + public boolean canPick(Player player) { + return player.canPick(); + } + + public void pick(Player player) { + Card pickedCard = cardDeck.pick(); + player.pick(pickedCard); } public Player getDealer() { diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index fd2935ff0e..6bc2d64130 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -1,5 +1,6 @@ package blackjack.view; +import blackjack.view.exception.InvalidChoiceException; import java.util.Arrays; import java.util.List; import java.util.Scanner; @@ -10,7 +11,10 @@ public class InputView { private static final Scanner scanner = new Scanner(System.in); private static final String DELIMITER = ","; + private static final String YES = "y"; + private static final String NO = "n"; private static final String REQUEST_PLAYER_NAME = "게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"; + private static final String REQUEST_PLAYER_CHOICE = "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"; public static List inputPlayerNames() { System.out.println(REQUEST_PLAYER_NAME); @@ -25,6 +29,23 @@ private static List splitInputByDelimiter(String input) { .collect(Collectors.toUnmodifiableList()); } + public static boolean inputPlayerChoice(String name) { + System.out.println(name + REQUEST_PLAYER_CHOICE); + String choice = scanner.nextLine(); + return validateChoice(choice); + } + + private static boolean validateChoice(String choice) { + String lowerCase = choice.toLowerCase(); + if (lowerCase.equals(YES)) { + return true; + } + if (lowerCase.equals(NO)) { + return false; + } + throw new InvalidChoiceException(); + } + public static void terminate() { scanner.close(); } diff --git a/src/main/java/blackjack/view/exception/InvalidChoiceException.java b/src/main/java/blackjack/view/exception/InvalidChoiceException.java new file mode 100644 index 0000000000..3ba8baba84 --- /dev/null +++ b/src/main/java/blackjack/view/exception/InvalidChoiceException.java @@ -0,0 +1,12 @@ +package blackjack.view.exception; + +import blackjack.common.exception.CustomException; + +public class InvalidChoiceException extends CustomException { + + private static final String MESSAGE = "올바르지 않은 입력입니다. y 또는 n 중에 골라주세요."; + + public InvalidChoiceException() { + super(MESSAGE); + } +} diff --git a/src/test/java/blackjack/domain/card/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java index ab0be9c535..812d2b36d6 100644 --- a/src/test/java/blackjack/domain/card/CardTest.java +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -1,20 +1,12 @@ package blackjack.domain.card; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; class CardTest { - @Test - @DisplayName("카드 생성 테스트") - void create_card() { - assertThatCode(() -> new Card(Shape.SPADE, Number.FOUR)) - .doesNotThrowAnyException(); - } - @Test @DisplayName("카드의 숫자를 보고 값을 반환한다") void get_value() { From bc729683bd47c83c551a220224d4516dbb07e52c Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 21:22:32 +0900 Subject: [PATCH 22/43] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EB=8D=94=20=EB=B0=9B=EC=A7=80=20=EC=95=8A=EC=9C=BC=EB=A9=B4=20?= =?UTF-8?q?=ED=84=B4=EC=9D=B4=20=EB=81=9D=EB=82=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=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 --- docs/README.md | 2 +- .../controller/BlackJackController.java | 21 ++++++++++++++----- src/main/java/blackjack/view/OutputView.java | 16 ++++++++------ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/docs/README.md b/docs/README.md index 881330c87f..a120af5416 100644 --- a/docs/README.md +++ b/docs/README.md @@ -42,7 +42,7 @@ - [x] 플레이어 이름 중복 시 예외가 발생한다 - [x] 플레이어 이름이 `딜러`인 경우 예외가 발생한다 -- [ ] 카드를 더 받겠느냐는 질문에 `y`, `n`을 입력하지 않으면 예외가 발생한다 +- [x] 카드를 더 받겠느냐는 질문에 `y`, `n`을 입력하지 않으면 예외가 발생한다 - [ ] 더 이상 뽑을 카드가 없으면 카드 덱이 비어있다는 예외가 발생한다 --------- diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index ed76f2af16..74a2ff9959 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -22,11 +22,20 @@ public void run() { List challengers = makeChallengersStatus(); OutputView.printStartStatus(dealer, challengers); - takeAllPlayersTurn(); + takeAllChallengersTurn(); + + takeDealerTurn(); InputView.terminate(); } + private void takeDealerTurn() { + Player dealer = blackJackGame.getDealer(); + if (dealer.canPick()) { + blackJackGame.pick(dealer); + } + } + private void init() { try { List playerNames = InputView.inputPlayerNames(); @@ -52,14 +61,14 @@ private List makeChallengersStatus() { return gameStatus; } - private void takeAllPlayersTurn() { + private void takeAllChallengersTurn() { for (Player player : blackJackGame.getChallengers()) { - takeEachPlayerTurn(player); + takeEachChallengerTurn(player); } } - private void takeEachPlayerTurn(Player player) { - while (blackJackGame.canPick(player)) { + private void takeEachChallengerTurn(Player player) { + if (blackJackGame.canPick(player)) { checkChoice(player); } } @@ -77,6 +86,8 @@ private void inputChoice(Player player) { boolean choice = InputView.inputPlayerChoice(player.getName()); if (choice) { blackJackGame.pick(player); + OutputView.printChallengerStatus(new PlayerStatusDto(player)); + takeEachChallengerTurn(player); } } } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 72584870be..0488714c13 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -28,23 +28,27 @@ private static void printGivenMessage(PlayerStatusDto dealerStatus, List challengersStatus) { + public static void printChallengersStatus(List challengersStatus) { for (PlayerStatusDto challenger : challengersStatus) { - System.out.print(challenger.getName() + CARD); - System.out.print(PLAYER_NAME_AND_CARDS_PARTITION); - String cards = String.join(ITEM_DELIMITER, challenger.getCards()); - System.out.println(cards); + printChallengerStatus(challenger); } System.out.println(); } + public static void printChallengerStatus(PlayerStatusDto challenger) { + System.out.print(challenger.getName() + CARD); + System.out.print(PLAYER_NAME_AND_CARDS_PARTITION); + String cards = String.join(ITEM_DELIMITER, challenger.getCards()); + System.out.println(cards); + } + private static List toChallengerNames(List challengersStatus) { return challengersStatus.stream() .map(PlayerStatusDto::getName) From 5f7a7258a3d7036b612859d0bd06f7e09aa6ef87 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 21:32:01 +0900 Subject: [PATCH 23/43] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EB=8D=94=20=EB=B0=9B=EC=A7=80=20=EC=95=8A=EC=9C=BC=EB=A9=B4=20?= =?UTF-8?q?=ED=84=B4=EC=9D=B4=20=EB=81=9D=EB=82=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=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 --- docs/README.md | 2 +- .../java/blackjack/controller/BlackJackController.java | 4 +++- src/main/java/blackjack/view/OutputView.java | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index a120af5416..136a14e5e7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,7 +7,7 @@ - 출력 - [x] 카드 지급 완료 문구 출력 - [x] 개인 카드 목록 출력 - - [ ] 딜러 카드 추가 지급 여부 출력 + - [x] 딜러 카드 추가 지급 여부 출력 - [ ] 최종 결과 출력 - [ ] 최종 승패 출력 diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 74a2ff9959..8fb986f06a 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -31,9 +31,11 @@ public void run() { private void takeDealerTurn() { Player dealer = blackJackGame.getDealer(); - if (dealer.canPick()) { + boolean dealerCanPick = dealer.canPick(); + if (dealerCanPick) { blackJackGame.pick(dealer); } + OutputView.printDealerResult(dealerCanPick); } private void init() { diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 0488714c13..cc0404cb7f 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -7,6 +7,8 @@ public class OutputView { private static final String GIVE_START_CARD_COMPLETE_MESSAGE = "에게 2장을 나누었습니다."; + private static final String DEALER_CAN_PICK_MESSAGE = "딜러는 16이하라 한장의 카드를 더 받았습니다."; + private static final String DEALER_CAN_NOT_PICK_MESSAGE = "딜러는 17이상이라 한장의 카드를 더 받지 못했습니다."; private static final String CARD = "카드"; private static final String ITEM_DELIMITER = ", "; private static final String PLAYER_NAME_AND_CARDS_PARTITION = ": "; @@ -54,4 +56,12 @@ private static List toChallengerNames(List challengersS .map(PlayerStatusDto::getName) .collect(Collectors.toUnmodifiableList()); } + + public static void printDealerResult(boolean dealerCanPick) { + if (dealerCanPick) { + System.out.println(DEALER_CAN_PICK_MESSAGE); + return; + } + System.out.println(DEALER_CAN_NOT_PICK_MESSAGE); + } } From 7109bc49c8f3acd314def18fd3f2d1bc649341c2 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Thu, 2 Mar 2023 22:24:57 +0900 Subject: [PATCH 24/43] =?UTF-8?q?feat:=20=EC=B5=9C=EC=A2=85=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=20=EC=83=81=ED=83=9C=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0?= =?UTF-8?q?=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 --- docs/README.md | 4 +- .../controller/BlackJackController.java | 54 ++++++++++++------- .../java/blackjack/domain/player/Player.java | 4 ++ .../java/blackjack/dto/PlayerStatusDto.java | 6 +++ src/main/java/blackjack/view/InputView.java | 2 +- src/main/java/blackjack/view/OutputView.java | 24 +++++++-- 6 files changed, 68 insertions(+), 26 deletions(-) diff --git a/docs/README.md b/docs/README.md index 136a14e5e7..bbe04f8989 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,7 +8,7 @@ - [x] 카드 지급 완료 문구 출력 - [x] 개인 카드 목록 출력 - [x] 딜러 카드 추가 지급 여부 출력 - - [ ] 최종 결과 출력 + - [x] 최종 카드 상태 출력 - [ ] 최종 승패 출력 ### 기능 @@ -48,4 +48,4 @@ --------- ## 고민 - dto에도 정적 팩토리 메소드를 써야할까? -- +- playerStatusDto(필드2개) & playerResultDto(필드3개) -> 필드2개 겹치는데 두 dto를 따로 만들어야하나? 아니면 필드를 안쓰더라도 하나로 통합?(중복방지) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 8fb986f06a..520230bc6d 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -15,29 +15,12 @@ public class BlackJackController { public void run() { init(); - - blackJackGame.handOutStartCards(); - - PlayerStatusDto dealer = makeDealerStatus(); - List challengers = makeChallengersStatus(); - OutputView.printStartStatus(dealer, challengers); - - takeAllChallengersTurn(); - - takeDealerTurn(); - + start(); + takeTurn(); + showResult(); InputView.terminate(); } - private void takeDealerTurn() { - Player dealer = blackJackGame.getDealer(); - boolean dealerCanPick = dealer.canPick(); - if (dealerCanPick) { - blackJackGame.pick(dealer); - } - OutputView.printDealerResult(dealerCanPick); - } - private void init() { try { List playerNames = InputView.inputPlayerNames(); @@ -48,6 +31,17 @@ private void init() { } } + private void start() { + blackJackGame.handOutStartCards(); + showStartStatus(); + } + + private void showStartStatus() { + PlayerStatusDto dealerStatus = makeDealerStatus(); + List challengersStatus = makeChallengersStatus(); + OutputView.printStartStatus(dealerStatus, challengersStatus); + } + private PlayerStatusDto makeDealerStatus() { Player dealer = blackJackGame.getDealer(); return new PlayerStatusDto(dealer); @@ -63,6 +57,11 @@ private List makeChallengersStatus() { return gameStatus; } + private void takeTurn() { + takeAllChallengersTurn(); + takeDealerTurn(); + } + private void takeAllChallengersTurn() { for (Player player : blackJackGame.getChallengers()) { takeEachChallengerTurn(player); @@ -92,4 +91,19 @@ private void inputChoice(Player player) { takeEachChallengerTurn(player); } } + + private void takeDealerTurn() { + Player dealer = blackJackGame.getDealer(); + boolean dealerCanPick = dealer.canPick(); + if (dealerCanPick) { + blackJackGame.pick(dealer); + } + OutputView.printDealerResult(dealerCanPick); + } + + private void showResult() { + PlayerStatusDto dealerStatus = makeDealerStatus(); + List challengersStatus = makeChallengersStatus(); + OutputView.printEndStatus(dealerStatus, challengersStatus); + } } diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index e7afdbb5c6..d4ca110e1a 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -22,6 +22,10 @@ public void pick(Card card) { holdingCards.add(card); } + public int getTotalPoint() { + return holdingCards.sum(); + } + public abstract Boolean canPick(); public abstract String getName(); diff --git a/src/main/java/blackjack/dto/PlayerStatusDto.java b/src/main/java/blackjack/dto/PlayerStatusDto.java index 2f76c2a686..faa399c486 100644 --- a/src/main/java/blackjack/dto/PlayerStatusDto.java +++ b/src/main/java/blackjack/dto/PlayerStatusDto.java @@ -9,10 +9,12 @@ public class PlayerStatusDto { private final String name; private final List cards; + private final int point; public PlayerStatusDto(Player player) { this.name = player.getName(); this.cards = extractCardInfo(player); + this.point = player.getTotalPoint(); } private List extractCardInfo(Player player) { @@ -31,4 +33,8 @@ public String getName() { public List getCards() { return cards; } + + public int getPoint() { + return point; + } } diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index 6bc2d64130..db05253c7e 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -30,7 +30,7 @@ private static List splitInputByDelimiter(String input) { } public static boolean inputPlayerChoice(String name) { - System.out.println(name + REQUEST_PLAYER_CHOICE); + System.out.println(System.lineSeparator() + name + REQUEST_PLAYER_CHOICE); String choice = scanner.nextLine(); return validateChoice(choice); } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index cc0404cb7f..6d30274690 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -12,14 +12,17 @@ public class OutputView { private static final String CARD = "카드"; private static final String ITEM_DELIMITER = ", "; private static final String PLAYER_NAME_AND_CARDS_PARTITION = ": "; + private static final String RESULT_PREFIX = " - 결과: "; public static void printErrorMessage(Exception exception) { System.out.println(exception.getMessage()); } public static void printStartStatus(PlayerStatusDto dealerStatus, List challengersStatus) { + System.out.println(); printGivenMessage(dealerStatus, challengersStatus); printDealerStatus(dealerStatus); + System.out.println(); printChallengersStatus(challengersStatus); } @@ -34,21 +37,21 @@ public static void printDealerStatus(PlayerStatusDto dealerStatus) { System.out.print(dealerStatus.getName()); System.out.print(PLAYER_NAME_AND_CARDS_PARTITION); String cards = String.join(ITEM_DELIMITER, dealerStatus.getCards()); - System.out.println(cards); + System.out.print(cards); } public static void printChallengersStatus(List challengersStatus) { for (PlayerStatusDto challenger : challengersStatus) { printChallengerStatus(challenger); + System.out.println(); } - System.out.println(); } public static void printChallengerStatus(PlayerStatusDto challenger) { System.out.print(challenger.getName() + CARD); System.out.print(PLAYER_NAME_AND_CARDS_PARTITION); String cards = String.join(ITEM_DELIMITER, challenger.getCards()); - System.out.println(cards); + System.out.print(cards); } private static List toChallengerNames(List challengersStatus) { @@ -58,10 +61,25 @@ private static List toChallengerNames(List challengersS } public static void printDealerResult(boolean dealerCanPick) { + System.out.println(); if (dealerCanPick) { System.out.println(DEALER_CAN_PICK_MESSAGE); return; } System.out.println(DEALER_CAN_NOT_PICK_MESSAGE); + System.out.println(); + } + + public static void printEndStatus(PlayerStatusDto dealerStatus, List challengersStatus) { + printDealerStatus(dealerStatus); + printPoint(dealerStatus.getPoint()); + for (PlayerStatusDto challenger : challengersStatus) { + printChallengerStatus(challenger); + printPoint(challenger.getPoint()); + } + } + + private static void printPoint(int point) { + System.out.println(RESULT_PREFIX + point); } } From 5d1c183a65093cdce268f66c7b60dd90bbbbbc66 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Fri, 3 Mar 2023 13:39:13 +0900 Subject: [PATCH 25/43] =?UTF-8?q?feat:=20ace=EB=A5=BC=20=EA=B3=A0=EB=A0=A4?= =?UTF-8?q?=ED=95=9C=20=EC=B9=B4=EB=93=9C=20=ED=95=A9=EA=B3=84=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=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 --- docs/README.md | 5 ++- src/main/java/blackjack/domain/card/Card.java | 5 ++- .../blackjack/domain/player/Challenger.java | 9 ++++- .../java/blackjack/domain/player/Dealer.java | 10 ++++- .../blackjack/domain/player/HoldingCards.java | 34 +++++++++++++++- .../java/blackjack/domain/player/Player.java | 2 +- .../domain/player/HoldingCardsTest.java | 40 +++++++++++++++++-- 7 files changed, 95 insertions(+), 10 deletions(-) diff --git a/docs/README.md b/docs/README.md index bbe04f8989..c207f091f2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -36,7 +36,8 @@ - HolingCards : 참여자가 소유하고 있는 카드. Card 목록을 가지고 있는 일급 컬렉션 - [x] 뽑은 카드를 저장한다 - - [x] 가진 카드의 합을 반환한다 + - [x] 가진 카드로 가능한 합 목록을 반환한다 + - [x] 가진 카드들 중에 21보다 작은 가장 큰 값을 반환한다 ### 예외 사항 - [x] 플레이어 이름 중복 시 예외가 발생한다 @@ -48,4 +49,4 @@ --------- ## 고민 - dto에도 정적 팩토리 메소드를 써야할까? -- playerStatusDto(필드2개) & playerResultDto(필드3개) -> 필드2개 겹치는데 두 dto를 따로 만들어야하나? 아니면 필드를 안쓰더라도 하나로 통합?(중복방지) +- playerStatusDto(필드2개) & playerResultDto(필드3개) -> 필드2개 겹치는데 두 dto를 따로 만들어야하나? 아니면 필드를 안쓰더라도 하나로 통합?(중복방현) diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index 80277e9939..b5d0231fa2 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -10,6 +10,10 @@ public Card(Shape shape, Number number) { this.number = number; } + public boolean isAce() { + return number.equals(Number.ACE); + } + public Shape getShape() { return shape; } @@ -20,7 +24,6 @@ public Number getNumber() { } public int getPoint() { - // TODO : Ace의 경우 1 또는 11 반환 처리 return number.getValue(); } } diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java index 21b040ec49..369983290c 100644 --- a/src/main/java/blackjack/domain/player/Challenger.java +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -1,6 +1,7 @@ package blackjack.domain.player; import blackjack.domain.player.exception.InvalidPlayerNameException; +import java.util.List; public class Challenger extends Player { @@ -22,7 +23,13 @@ private void validateName(String name) { @Override public Boolean canPick() { - return holdingCards.sum() <= MAXIMUM_POINT; + List sumPossibility = holdingCards.getSums(); + for (Integer sum : sumPossibility) { + if (sum <= MAXIMUM_POINT) { + return true; + } + } + return false; } @Override diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java index fdfbb3faf2..217ae7cfc3 100644 --- a/src/main/java/blackjack/domain/player/Dealer.java +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -1,5 +1,7 @@ package blackjack.domain.player; +import java.util.List; + public class Dealer extends Player { private static final String NAME = "딜러"; @@ -8,7 +10,13 @@ public class Dealer extends Player { @Override public Boolean canPick() { - return holdingCards.sum() <= MAXIMUM_POINT_TO_PICK; + List sumPossibility = holdingCards.getSums(); + for (Integer sum : sumPossibility) { + if (sum <= MAXIMUM_POINT_TO_PICK) { + return true; + } + } + return false; } @Override diff --git a/src/main/java/blackjack/domain/player/HoldingCards.java b/src/main/java/blackjack/domain/player/HoldingCards.java index 24820aa861..51b7206b84 100644 --- a/src/main/java/blackjack/domain/player/HoldingCards.java +++ b/src/main/java/blackjack/domain/player/HoldingCards.java @@ -6,6 +6,9 @@ public class HoldingCards { + private static final int MAXIMUM_SUM = 21; + private static final int ACE_PLUS_POINT = 10; + private final List cards = new ArrayList<>(); public void initialCard(Card card1, Card card2) { @@ -20,9 +23,38 @@ public List getCards() { return List.copyOf(cards); } - public int sum() { + public List getSums() { + int defaultSum = getNotAceSum() + getAceCount(); + List possibility = new ArrayList<>(); + possibility.add(defaultSum); + for (int i = 1; i <= getAceCount(); i++) { + possibility.add(defaultSum + ACE_PLUS_POINT * i); + } + return possibility; + } + + private int getNotAceSum() { return cards.stream() + .filter(card -> !card.isAce()) .mapToInt(Card::getPoint) .sum(); } + + private int getAceCount() { + return (int) cards.stream() + .filter(Card::isAce) + .count(); + } + + public int getSum() { + return getSums().stream() + .filter(sum -> sum < MAXIMUM_SUM) + .mapToInt(sum -> sum) + .max() + .orElse(getMinimumSum()); + } + + private int getMinimumSum() { + return getSums().get(0); + } } diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index d4ca110e1a..8d5260edb0 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -23,7 +23,7 @@ public void pick(Card card) { } public int getTotalPoint() { - return holdingCards.sum(); + return holdingCards.getSum(); } public abstract Boolean canPick(); diff --git a/src/test/java/blackjack/domain/player/HoldingCardsTest.java b/src/test/java/blackjack/domain/player/HoldingCardsTest.java index 5eac908572..647ce2b654 100644 --- a/src/test/java/blackjack/domain/player/HoldingCardsTest.java +++ b/src/test/java/blackjack/domain/player/HoldingCardsTest.java @@ -5,20 +5,54 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Number; import blackjack.domain.card.Shape; -import java.util.List; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; class HoldingCardsTest { + private HoldingCards holdingCards; + + @BeforeEach + void setup() { + holdingCards = new HoldingCards(); + } + @Test @DisplayName("가진 카드의 합 테스트") void sum_of_cards() { - HoldingCards holdingCards = new HoldingCards(); Card card1 = new Card(Shape.DIAMOND, Number.KING); Card card2 = new Card(Shape.SPADE, Number.FOUR); holdingCards.initialCard(card1, card2); - assertThat(holdingCards.sum()).isEqualTo(14); + assertThat(holdingCards.getSum()).isEqualTo(14); + } + + @Test + @DisplayName("ACE가 존재하는 경우 가능한 모든 합 테스트") + void sums_of_ace_existing() { + Card cloverAce = new Card(Shape.CLOVER, Number.ACE); + Card cloverKing = new Card(Shape.CLOVER, Number.KING); + Card spadeAce = new Card(Shape.SPADE, Number.ACE); + Card heartAce = new Card(Shape.HEART, Number.ACE); + holdingCards.initialCard(cloverAce, cloverKing); + holdingCards.add(spadeAce); + holdingCards.add(heartAce); + + assertThat(holdingCards.getSums()) + .containsExactly(13, 23, 33, 43); + } + + @Test + @DisplayName("ACE가 존재하는 경우 21에 가장 가까운 합을 찾는지 확인하는 테스트") + void find_sum_of_nearly_21() { + Card cloverAce = new Card(Shape.CLOVER, Number.ACE); + Card cloverFive = new Card(Shape.CLOVER, Number.FIVE); + Card spadeAce = new Card(Shape.SPADE, Number.ACE); + holdingCards.initialCard(cloverAce, cloverFive); + holdingCards.add(spadeAce); + + assertThat(holdingCards.getSum()) + .isEqualTo(17); } } From 839265465ae6aa529f0844e037ec795439cf5ced Mon Sep 17 00:00:00 2001 From: hanueleee Date: Fri, 3 Mar 2023 14:55:55 +0900 Subject: [PATCH 26/43] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=EC=97=90=20Result=20=EA=B0=9D=EC=B2=B4=EC=9D=98=20?= =?UTF-8?q?=EC=97=AD=ED=95=A0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/README.md b/docs/README.md index c207f091f2..fed123562f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -39,6 +39,10 @@ - [x] 가진 카드로 가능한 합 목록을 반환한다 - [x] 가진 카드들 중에 21보다 작은 가장 큰 값을 반환한다 +- Result : 최종 결과를 계산하는 객체 + - [ ] 도전자들의 최종 승패를 계산한다. + - [ ] 딜러의 최종 승패를 계산한다. + ### 예외 사항 - [x] 플레이어 이름 중복 시 예외가 발생한다 - [x] 플레이어 이름이 `딜러`인 경우 예외가 발생한다 @@ -50,3 +54,4 @@ ## 고민 - dto에도 정적 팩토리 메소드를 써야할까? - playerStatusDto(필드2개) & playerResultDto(필드3개) -> 필드2개 겹치는데 두 dto를 따로 만들어야하나? 아니면 필드를 안쓰더라도 하나로 통합?(중복방현) +- else if 사용? From 8b22ed270e134590ecb4bac85f9b4ce5694849d7 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Fri, 3 Mar 2023 14:56:50 +0900 Subject: [PATCH 27/43] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EB=93=A4=EC=9D=98=20=EC=B5=9C=EC=A2=85=20=EC=8A=B9?= =?UTF-8?q?=ED=8C=A8=20=EC=97=AC=EB=B6=80=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 --- docs/README.md | 6 +- .../java/blackjack/domain/BlackJackGame.java | 6 ++ .../java/blackjack/domain/result/Rank.java | 28 +++++++ .../java/blackjack/domain/result/Result.java | 78 +++++++++++++++++++ .../blackjack/domain/result/ResultTest.java | 53 +++++++++++++ 5 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 src/main/java/blackjack/domain/result/Rank.java create mode 100644 src/main/java/blackjack/domain/result/Result.java create mode 100644 src/test/java/blackjack/domain/result/ResultTest.java diff --git a/docs/README.md b/docs/README.md index fed123562f..004a8bc90d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -13,7 +13,7 @@ ### 기능 - BlackJackGame : 게임 관리 시스템 - - [ ] 최종 승패를 계산한다 + - [x] 최종 승패를 계산한다 - Player : 게임 참여자 - [x] 초기 카드 2장을 받는다 @@ -40,8 +40,8 @@ - [x] 가진 카드들 중에 21보다 작은 가장 큰 값을 반환한다 - Result : 최종 결과를 계산하는 객체 - - [ ] 도전자들의 최종 승패를 계산한다. - - [ ] 딜러의 최종 승패를 계산한다. + - [x] 도전자들의 최종 승패를 계산한다. + - [x] 딜러의 최종 승패를 계산한다. ### 예외 사항 - [x] 플레이어 이름 중복 시 예외가 발생한다 diff --git a/src/main/java/blackjack/domain/BlackJackGame.java b/src/main/java/blackjack/domain/BlackJackGame.java index 253b1fe132..ca5f44707a 100644 --- a/src/main/java/blackjack/domain/BlackJackGame.java +++ b/src/main/java/blackjack/domain/BlackJackGame.java @@ -4,6 +4,7 @@ import blackjack.domain.card.CardDeck; import blackjack.domain.player.Player; import blackjack.domain.player.Players; +import blackjack.domain.result.Result; import java.util.List; public class BlackJackGame { @@ -35,6 +36,11 @@ public void pick(Player player) { player.pick(pickedCard); } + public Result makeResult() { + Result result = Result.from(players); + return result; + } + public Player getDealer() { return players.getDealer(); } diff --git a/src/main/java/blackjack/domain/result/Rank.java b/src/main/java/blackjack/domain/result/Rank.java new file mode 100644 index 0000000000..167c11ee4e --- /dev/null +++ b/src/main/java/blackjack/domain/result/Rank.java @@ -0,0 +1,28 @@ +package blackjack.domain.result; + +public enum Rank { + + WIN("승"), + DRAW("무"), + LOSE("패"); + + private final String label; + + Rank(String label) { + this.label = label; + } + + public Rank getOpposite() { + if (this.equals(Rank.WIN)) { + return LOSE; + } + if (this.equals(Rank.LOSE)) { + return WIN; + } + return DRAW; + } + + public String getLabel() { + return label; + } +} diff --git a/src/main/java/blackjack/domain/result/Result.java b/src/main/java/blackjack/domain/result/Result.java new file mode 100644 index 0000000000..425984d9c7 --- /dev/null +++ b/src/main/java/blackjack/domain/result/Result.java @@ -0,0 +1,78 @@ +package blackjack.domain.result; + +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class Result { + private static final int MAXIMUM_POINT = 21; + + private final Map results; + + private Result(Map results) { + this.results = results; + } + + public static Result from(Players players) { + Map results = new LinkedHashMap<>(); + int dealerPoint = players.getDealer().getTotalPoint(); + List challengers = players.getChallengers(); + for (Player challenger : challengers) { + Rank rank = makePlayerResult(dealerPoint, challenger); + results.put(challenger, rank); + } + return new Result(results); + } + + private static Rank makePlayerResult(int dealerPoint, Player challenger) { + int challengerPoint = challenger.getTotalPoint(); + if (isOverMaximumPoint(dealerPoint)) { + return getRankWhenDealerOverPoint(challengerPoint); + } + return getRankWhenDealerNotOverPoint(dealerPoint, challengerPoint); + } + + private static Rank getRankWhenDealerOverPoint(int challengerPoint) { + if (isOverMaximumPoint(challengerPoint)) { + return Rank.DRAW; + } + return Rank.WIN; + } + + private static Rank getRankWhenDealerNotOverPoint(int dealerPoint, int challengerPoint) { + if (isOverMaximumPoint(challengerPoint)) { + return Rank.LOSE; + } + return comparePoint(dealerPoint, challengerPoint); + } + + private static Rank comparePoint(int dealerPoint, int challengerPoint) { + if (challengerPoint < dealerPoint) { + return Rank.LOSE; + } + if (challengerPoint > dealerPoint) { + return Rank.WIN; + } + return Rank.DRAW; + } + + private static boolean isOverMaximumPoint(int sum) { + return sum > MAXIMUM_POINT; + } + + public Rank getChallengerResult(Player player) { + return results.get(player); + } + + public Map getDealerResult() { + Map dealerResult = new HashMap<>(); + for (Rank rank : results.values()) { + Rank dealerRank = rank.getOpposite(); + dealerResult.put(dealerRank, dealerResult.getOrDefault(dealerRank, 0) + 1); + } + return dealerResult; + } +} diff --git a/src/test/java/blackjack/domain/result/ResultTest.java b/src/test/java/blackjack/domain/result/ResultTest.java new file mode 100644 index 0000000000..60f042f199 --- /dev/null +++ b/src/test/java/blackjack/domain/result/ResultTest.java @@ -0,0 +1,53 @@ +package blackjack.domain.result; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Number; +import blackjack.domain.card.Shape; +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class ResultTest { + + private Result result; + private Players players; + + @BeforeEach + void setPlayers() { + players = Players.from(List.of("oing", "ditoo")); + + Card heartKing = new Card(Shape.HEART, Number.KING); + Card heartSeven = new Card(Shape.HEART, Number.SEVEN); + Card heartThree = new Card(Shape.HEART, Number.THREE); + + players.getDealer().pick(heartSeven); + players.getChallengers().get(0).pick(heartThree); + players.getChallengers().get(1).pick(heartKing); + result = Result.from(players); + } + + @Test + @DisplayName("도전자들의 승패가 올바르게 계산되는지 확인한다") + void check_challengers_result() { + Player oing = players.getChallengers().get(0); + Player ditoo = players.getChallengers().get(1); + + assertThat(result.getChallengerResult(oing)).isEqualTo(Rank.LOSE); + assertThat(result.getChallengerResult(ditoo)).isEqualTo(Rank.WIN); + } + + @Test + @DisplayName("딜러의 승패가 올바르게 계산되는지 확인한다") + void check_dealer_result() { + Map dealerResult = result.getDealerResult(); + assertThat(dealerResult) + .containsOnly(entry(Rank.WIN, 1), entry(Rank.LOSE, 1)); + } +} From 84a51549f3af5f38795a8267ed6071534771fc7f Mon Sep 17 00:00:00 2001 From: hanueleee Date: Fri, 3 Mar 2023 16:13:27 +0900 Subject: [PATCH 28/43] =?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=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 --- docs/README.md | 2 +- .../controller/BlackJackController.java | 15 +++++++ .../blackjack/dto/ChallengerResultDto.java | 26 +++++++++++ .../java/blackjack/dto/DealerResultDto.java | 38 ++++++++++++++++ .../java/blackjack/dto/PlayerStatusDto.java | 2 +- src/main/java/blackjack/view/OutputView.java | 44 +++++++++++++++++++ 6 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 src/main/java/blackjack/dto/ChallengerResultDto.java create mode 100644 src/main/java/blackjack/dto/DealerResultDto.java diff --git a/docs/README.md b/docs/README.md index 004a8bc90d..a83534a4c0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -9,7 +9,7 @@ - [x] 개인 카드 목록 출력 - [x] 딜러 카드 추가 지급 여부 출력 - [x] 최종 카드 상태 출력 - - [ ] 최종 승패 출력 + - [x] 최종 승패 출력 ### 기능 - BlackJackGame : 게임 관리 시스템 diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 520230bc6d..7fe06a32b7 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -3,6 +3,9 @@ import blackjack.common.exception.CustomException; import blackjack.domain.BlackJackGame; import blackjack.domain.player.Player; +import blackjack.domain.result.Result; +import blackjack.dto.ChallengerResultDto; +import blackjack.dto.DealerResultDto; import blackjack.dto.PlayerStatusDto; import blackjack.view.InputView; import blackjack.view.OutputView; @@ -102,8 +105,20 @@ private void takeDealerTurn() { } private void showResult() { + showPoint(); + showRank(); + } + + private void showPoint() { PlayerStatusDto dealerStatus = makeDealerStatus(); List challengersStatus = makeChallengersStatus(); OutputView.printEndStatus(dealerStatus, challengersStatus); } + + private void showRank() { + Result result = blackJackGame.makeResult(); + ChallengerResultDto challengerResultDto = new ChallengerResultDto(result, blackJackGame.getChallengers()); + DealerResultDto dealerResultDto = new DealerResultDto(result, blackJackGame.getDealer()); + OutputView.printFinalRank(challengerResultDto, dealerResultDto); + } } diff --git a/src/main/java/blackjack/dto/ChallengerResultDto.java b/src/main/java/blackjack/dto/ChallengerResultDto.java new file mode 100644 index 0000000000..147b6deb19 --- /dev/null +++ b/src/main/java/blackjack/dto/ChallengerResultDto.java @@ -0,0 +1,26 @@ +package blackjack.dto; + +import blackjack.domain.player.Player; +import blackjack.domain.result.Rank; +import blackjack.domain.result.Result; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class ChallengerResultDto { + + private final Map nameAndRanks; + + public ChallengerResultDto(Result result, List challengers) { + Map nameAndRanks = new LinkedHashMap<>(); + for (Player challenger : challengers) { + Rank challengerResult = result.getChallengerResult(challenger); + nameAndRanks.put(challenger.getName(), challengerResult.getLabel()); + } + this.nameAndRanks = nameAndRanks; + } + + public Map getNameAndRanks() { + return nameAndRanks; + } +} diff --git a/src/main/java/blackjack/dto/DealerResultDto.java b/src/main/java/blackjack/dto/DealerResultDto.java new file mode 100644 index 0000000000..eaba639291 --- /dev/null +++ b/src/main/java/blackjack/dto/DealerResultDto.java @@ -0,0 +1,38 @@ +package blackjack.dto; + +import blackjack.domain.player.Player; +import blackjack.domain.result.Rank; +import blackjack.domain.result.Result; +import java.util.Map; + +public class DealerResultDto { + + private final String name; + private final int winCount; + private final int drawCount; + private final int loseCount; + + public DealerResultDto(Result result, Player dealer) { + this.name = dealer.getName(); + Map dealerResult = result.getDealerResult(); + this.winCount = dealerResult.getOrDefault(Rank.WIN, 0); + this.drawCount = dealerResult.getOrDefault(Rank.DRAW, 0); + this.loseCount = dealerResult.getOrDefault(Rank.LOSE, 0); + } + + public String getName() { + return name; + } + + public int getWinCount() { + return winCount; + } + + public int getDrawCount() { + return drawCount; + } + + public int getLoseCount() { + return loseCount; + } +} diff --git a/src/main/java/blackjack/dto/PlayerStatusDto.java b/src/main/java/blackjack/dto/PlayerStatusDto.java index faa399c486..c4eeaf4bca 100644 --- a/src/main/java/blackjack/dto/PlayerStatusDto.java +++ b/src/main/java/blackjack/dto/PlayerStatusDto.java @@ -21,7 +21,7 @@ private List extractCardInfo(Player player) { List cardInfo = new ArrayList<>(); List inputCards = player.getHoldingCards().getCards(); for (Card card : inputCards) { - cardInfo.add(card.getNumber().getName()+card.getShape().getName()); + cardInfo.add(card.getNumber().getName() + card.getShape().getName()); } return cardInfo; } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 6d30274690..ba6f1445c4 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,7 +1,10 @@ package blackjack.view; +import blackjack.dto.ChallengerResultDto; +import blackjack.dto.DealerResultDto; import blackjack.dto.PlayerStatusDto; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class OutputView { @@ -9,6 +12,7 @@ public class OutputView { private static final String GIVE_START_CARD_COMPLETE_MESSAGE = "에게 2장을 나누었습니다."; private static final String DEALER_CAN_PICK_MESSAGE = "딜러는 16이하라 한장의 카드를 더 받았습니다."; private static final String DEALER_CAN_NOT_PICK_MESSAGE = "딜러는 17이상이라 한장의 카드를 더 받지 못했습니다."; + private static final String FINAL_RESULT_HEADER_MESSAGE = "## 최종 승패"; private static final String CARD = "카드"; private static final String ITEM_DELIMITER = ", "; private static final String PLAYER_NAME_AND_CARDS_PARTITION = ": "; @@ -82,4 +86,44 @@ public static void printEndStatus(PlayerStatusDto dealerStatus, List nameAndRanks = challengerResultDto.getNameAndRanks(); + for (String name : nameAndRanks.keySet()) { + System.out.println(name + PLAYER_NAME_AND_CARDS_PARTITION + nameAndRanks.get(name)); + } + } } From 89819cc26078d68ae649b50201725d7ef70d1e1f Mon Sep 17 00:00:00 2001 From: hanueleee Date: Fri, 3 Mar 2023 16:16:34 +0900 Subject: [PATCH 29/43] =?UTF-8?q?feat:=20=EB=8D=94=20=EC=9D=B4=EC=83=81=20?= =?UTF-8?q?=EB=BD=91=EC=9D=84=20=EC=B9=B4=EB=93=9C=EA=B0=80=20=EC=97=86?= =?UTF-8?q?=EC=9D=84=20=EA=B2=BD=EC=9A=B0=20=EC=B9=B4=EB=93=9C=20=EB=8D=B1?= =?UTF-8?q?=EC=9D=B4=20=EB=B9=84=EC=96=B4=EC=9E=88=EB=8B=A4=EB=8A=94=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D=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 --- docs/README.md | 2 +- .../java/blackjack/controller/BlackJackController.java | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index a83534a4c0..0fd5c78445 100644 --- a/docs/README.md +++ b/docs/README.md @@ -48,7 +48,7 @@ - [x] 플레이어 이름이 `딜러`인 경우 예외가 발생한다 - [x] 카드를 더 받겠느냐는 질문에 `y`, `n`을 입력하지 않으면 예외가 발생한다 -- [ ] 더 이상 뽑을 카드가 없으면 카드 덱이 비어있다는 예외가 발생한다 +- [x] 더 이상 뽑을 카드가 없으면 카드 덱이 비어있다는 예외가 발생한다 --------- ## 고민 diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 7fe06a32b7..4b7384fd83 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -61,8 +61,12 @@ private List makeChallengersStatus() { } private void takeTurn() { - takeAllChallengersTurn(); - takeDealerTurn(); + try { + takeAllChallengersTurn(); + takeDealerTurn(); + } catch (CustomException e) { + OutputView.printErrorMessage(e); + } } private void takeAllChallengersTurn() { From e09d6d98165090067d2f0373f0b99c3e13c8eb4d Mon Sep 17 00:00:00 2001 From: hanueleee Date: Fri, 3 Mar 2023 16:34:40 +0900 Subject: [PATCH 30/43] =?UTF-8?q?refactor:=20indent=202=EC=9D=B8=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20indent=201=EB=A1=9C=20=EC=A4=84?= =?UTF-8?q?=EC=9D=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Challenger.java | 8 ++------ src/main/java/blackjack/domain/player/Dealer.java | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java index 369983290c..c775b43566 100644 --- a/src/main/java/blackjack/domain/player/Challenger.java +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -24,12 +24,8 @@ private void validateName(String name) { @Override public Boolean canPick() { List sumPossibility = holdingCards.getSums(); - for (Integer sum : sumPossibility) { - if (sum <= MAXIMUM_POINT) { - return true; - } - } - return false; + return sumPossibility.stream() + .anyMatch(sum -> sum <= MAXIMUM_POINT); } @Override diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java index 217ae7cfc3..b8df8d12f8 100644 --- a/src/main/java/blackjack/domain/player/Dealer.java +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -11,12 +11,8 @@ public class Dealer extends Player { @Override public Boolean canPick() { List sumPossibility = holdingCards.getSums(); - for (Integer sum : sumPossibility) { - if (sum <= MAXIMUM_POINT_TO_PICK) { - return true; - } - } - return false; + return sumPossibility.stream() + .anyMatch(sum -> sum <= MAXIMUM_POINT_TO_PICK); } @Override From 744892b5670724698b385c52c723a7647896f018 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Fri, 3 Mar 2023 16:44:20 +0900 Subject: [PATCH 31/43] =?UTF-8?q?refactor:=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EC=A4=84=EB=B0=94=EA=BF=88=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/InputView.java | 3 ++- src/main/java/blackjack/view/OutputView.java | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index db05253c7e..677b67dfb2 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -30,7 +30,8 @@ private static List splitInputByDelimiter(String input) { } public static boolean inputPlayerChoice(String name) { - System.out.println(System.lineSeparator() + name + REQUEST_PLAYER_CHOICE); +// System.out.println(); + System.out.println(name + REQUEST_PLAYER_CHOICE); String choice = scanner.nextLine(); return validateChoice(choice); } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index ba6f1445c4..7fc0ce8806 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -28,6 +28,7 @@ public static void printStartStatus(PlayerStatusDto dealerStatus, List challengersStatus) { @@ -68,6 +69,7 @@ public static void printDealerResult(boolean dealerCanPick) { System.out.println(); if (dealerCanPick) { System.out.println(DEALER_CAN_PICK_MESSAGE); + System.out.println(); return; } System.out.println(DEALER_CAN_NOT_PICK_MESSAGE); From 20884aa6b3737e90369e2398fc424a79767bf2a2 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Fri, 3 Mar 2023 16:47:57 +0900 Subject: [PATCH 32/43] =?UTF-8?q?refactor:=20inline=EC=9D=B4=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=B4=EB=B3=B4=EC=9D=B4=EB=8A=94=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20inline=EC=9C=BC=EB=A1=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/BlackJackGame.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/blackjack/domain/BlackJackGame.java b/src/main/java/blackjack/domain/BlackJackGame.java index ca5f44707a..650031f60f 100644 --- a/src/main/java/blackjack/domain/BlackJackGame.java +++ b/src/main/java/blackjack/domain/BlackJackGame.java @@ -37,8 +37,7 @@ public void pick(Player player) { } public Result makeResult() { - Result result = Result.from(players); - return result; + return Result.from(players); } public Player getDealer() { From f7afb0e51e9821ee1979854bf10a20529de74da5 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Fri, 3 Mar 2023 17:14:57 +0900 Subject: [PATCH 33/43] =?UTF-8?q?refactor:=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EC=A4=84=EB=B0=94=EA=BF=88=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/controller/BlackJackController.java | 2 +- src/main/java/blackjack/view/InputView.java | 2 +- src/main/java/blackjack/view/OutputView.java | 9 ++++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 4b7384fd83..dc40d8717b 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -94,7 +94,7 @@ private void inputChoice(Player player) { boolean choice = InputView.inputPlayerChoice(player.getName()); if (choice) { blackJackGame.pick(player); - OutputView.printChallengerStatus(new PlayerStatusDto(player)); + OutputView.printChallengerStatusInGame(new PlayerStatusDto(player)); takeEachChallengerTurn(player); } } diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index 677b67dfb2..332a06813f 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -30,7 +30,7 @@ private static List splitInputByDelimiter(String input) { } public static boolean inputPlayerChoice(String name) { -// System.out.println(); + System.out.println(); System.out.println(name + REQUEST_PLAYER_CHOICE); String choice = scanner.nextLine(); return validateChoice(choice); diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 7fc0ce8806..0bdf47823b 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -28,7 +28,6 @@ public static void printStartStatus(PlayerStatusDto dealerStatus, List challengersStatus) { @@ -47,11 +46,15 @@ public static void printDealerStatus(PlayerStatusDto dealerStatus) { public static void printChallengersStatus(List challengersStatus) { for (PlayerStatusDto challenger : challengersStatus) { - printChallengerStatus(challenger); - System.out.println(); + printChallengerStatusInGame(challenger); } } + public static void printChallengerStatusInGame(PlayerStatusDto playerStatusDto) { + printChallengerStatus(playerStatusDto); + System.out.println(); + } + public static void printChallengerStatus(PlayerStatusDto challenger) { System.out.print(challenger.getName() + CARD); System.out.print(PLAYER_NAME_AND_CARDS_PARTITION); From 2f2d54cacc31d5484362380c304cc38f681c3f7e Mon Sep 17 00:00:00 2001 From: hanueleee Date: Mon, 6 Mar 2023 16:30:26 +0900 Subject: [PATCH 34/43] =?UTF-8?q?refactor:=20Dto=20=EB=82=B4=EB=B6=80=20?= =?UTF-8?q?=EB=B3=80=ED=99=98=20=EB=A1=9C=EC=A7=81=EC=9D=84=20controller?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=B4=EB=8F=99=20(Dto=20=EB=82=B4=EB=B6=80=20?= =?UTF-8?q?=EB=8B=A8=EC=88=9C=ED=99=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 60 +++++++++++++++---- .../java/blackjack/domain/player/Players.java | 6 +- .../blackjack/dto/ChallengerResultDto.java | 20 ++----- .../java/blackjack/dto/DealerResultDto.java | 16 ++--- .../java/blackjack/dto/PlayerStatusDto.java | 20 ++----- 5 files changed, 65 insertions(+), 57 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index dc40d8717b..e13f41faad 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -2,15 +2,20 @@ import blackjack.common.exception.CustomException; import blackjack.domain.BlackJackGame; +import blackjack.domain.card.Card; import blackjack.domain.player.Player; import blackjack.domain.result.Result; +import blackjack.domain.result.Results; import blackjack.dto.ChallengerResultDto; import blackjack.dto.DealerResultDto; import blackjack.dto.PlayerStatusDto; import blackjack.view.InputView; import blackjack.view.OutputView; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; public class BlackJackController { @@ -47,17 +52,14 @@ private void showStartStatus() { private PlayerStatusDto makeDealerStatus() { Player dealer = blackJackGame.getDealer(); - return new PlayerStatusDto(dealer); + return makePlayerStatusDto(dealer); } private List makeChallengersStatus() { - List players = blackJackGame.getChallengers(); - List gameStatus = new ArrayList<>(); - for (Player player : players) { - PlayerStatusDto playerStatusDto = new PlayerStatusDto(player); - gameStatus.add(playerStatusDto); - } - return gameStatus; + List challengers = blackJackGame.getChallengers(); + return challengers.stream() + .map(challenger -> makePlayerStatusDto(challenger)) + .collect(Collectors.toUnmodifiableList()); } private void takeTurn() { @@ -94,7 +96,7 @@ private void inputChoice(Player player) { boolean choice = InputView.inputPlayerChoice(player.getName()); if (choice) { blackJackGame.pick(player); - OutputView.printChallengerStatusInGame(new PlayerStatusDto(player)); + OutputView.printChallengerStatusInGame(makePlayerStatusDto(player)); takeEachChallengerTurn(player); } } @@ -120,9 +122,43 @@ private void showPoint() { } private void showRank() { - Result result = blackJackGame.makeResult(); - ChallengerResultDto challengerResultDto = new ChallengerResultDto(result, blackJackGame.getChallengers()); - DealerResultDto dealerResultDto = new DealerResultDto(result, blackJackGame.getDealer()); + Results results = blackJackGame.makeResult(); + + ChallengerResultDto challengerResultDto = makeChallengerResultDto(results, blackJackGame.getChallengers()); + DealerResultDto dealerResultDto = makeDealerResultDto(results, blackJackGame.getDealer()); OutputView.printFinalRank(challengerResultDto, dealerResultDto); } + + private PlayerStatusDto makePlayerStatusDto(Player player) { + String playerName = player.getName(); + List inputCards = player.getHoldingCards().getCards(); + int playerPoint = player.getTotalPoint(); + return new PlayerStatusDto(playerName, makeCardInfo(inputCards), playerPoint); + } + + private static List makeCardInfo(List inputCards) { + List cardInfo = new ArrayList<>(); + for (Card card : inputCards) { + cardInfo.add(card.getNumber().getName() + card.getShape().getName()); + } + return cardInfo; + } + + private ChallengerResultDto makeChallengerResultDto(Results results, List challengers) { + Map nameAndRanks = new LinkedHashMap<>(); + for (Player challenger : challengers) { + Result challengerResult = results.getChallengerResult(challenger); + nameAndRanks.put(challenger.getName(), challengerResult.getLabel()); + } + return new ChallengerResultDto(nameAndRanks); + } + + private DealerResultDto makeDealerResultDto(Results results, Player dealer) { + String dealerName = dealer.getName(); + Map dealerResult = results.getDealerResult(); + int winCount = dealerResult.getOrDefault(Result.WIN, 0); + int drawCount = dealerResult.getOrDefault(Result.DRAW, 0); + int loseCount = dealerResult.getOrDefault(Result.LOSE, 0); + return new DealerResultDto(dealerName, winCount, drawCount, loseCount); + } } diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index 07168242e0..705c456e14 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -37,9 +37,9 @@ private static void validateDuplicatedNames(List names) { public void pickStartCards(CardDeck cardDeck) { for (Player player : players) { - Card card1 = cardDeck.pick(); - Card card2 = cardDeck.pick(); - player.pickStartCards(card1, card2); + Card firstCard = cardDeck.pick(); + Card secondCard = cardDeck.pick(); + player.pickStartCards(firstCard, secondCard); } } diff --git a/src/main/java/blackjack/dto/ChallengerResultDto.java b/src/main/java/blackjack/dto/ChallengerResultDto.java index 147b6deb19..2a818ba58f 100644 --- a/src/main/java/blackjack/dto/ChallengerResultDto.java +++ b/src/main/java/blackjack/dto/ChallengerResultDto.java @@ -1,26 +1,16 @@ package blackjack.dto; -import blackjack.domain.player.Player; -import blackjack.domain.result.Rank; -import blackjack.domain.result.Result; -import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; public class ChallengerResultDto { - private final Map nameAndRanks; + private final Map nameAndResult; - public ChallengerResultDto(Result result, List challengers) { - Map nameAndRanks = new LinkedHashMap<>(); - for (Player challenger : challengers) { - Rank challengerResult = result.getChallengerResult(challenger); - nameAndRanks.put(challenger.getName(), challengerResult.getLabel()); - } - this.nameAndRanks = nameAndRanks; + public ChallengerResultDto(Map nameAndResult) { + this.nameAndResult = nameAndResult; } - public Map getNameAndRanks() { - return nameAndRanks; + public Map getNameAndResult() { + return nameAndResult; } } diff --git a/src/main/java/blackjack/dto/DealerResultDto.java b/src/main/java/blackjack/dto/DealerResultDto.java index eaba639291..6f877541ce 100644 --- a/src/main/java/blackjack/dto/DealerResultDto.java +++ b/src/main/java/blackjack/dto/DealerResultDto.java @@ -1,10 +1,5 @@ package blackjack.dto; -import blackjack.domain.player.Player; -import blackjack.domain.result.Rank; -import blackjack.domain.result.Result; -import java.util.Map; - public class DealerResultDto { private final String name; @@ -12,12 +7,11 @@ public class DealerResultDto { private final int drawCount; private final int loseCount; - public DealerResultDto(Result result, Player dealer) { - this.name = dealer.getName(); - Map dealerResult = result.getDealerResult(); - this.winCount = dealerResult.getOrDefault(Rank.WIN, 0); - this.drawCount = dealerResult.getOrDefault(Rank.DRAW, 0); - this.loseCount = dealerResult.getOrDefault(Rank.LOSE, 0); + public DealerResultDto(String name, int winCount, int drawCount, int loseCount) { + this.name = name; + this.winCount = winCount; + this.drawCount = drawCount; + this.loseCount = loseCount; } public String getName() { diff --git a/src/main/java/blackjack/dto/PlayerStatusDto.java b/src/main/java/blackjack/dto/PlayerStatusDto.java index c4eeaf4bca..54198aa818 100644 --- a/src/main/java/blackjack/dto/PlayerStatusDto.java +++ b/src/main/java/blackjack/dto/PlayerStatusDto.java @@ -1,8 +1,5 @@ package blackjack.dto; -import blackjack.domain.card.Card; -import blackjack.domain.player.Player; -import java.util.ArrayList; import java.util.List; public class PlayerStatusDto { @@ -11,19 +8,10 @@ public class PlayerStatusDto { private final List cards; private final int point; - public PlayerStatusDto(Player player) { - this.name = player.getName(); - this.cards = extractCardInfo(player); - this.point = player.getTotalPoint(); - } - - private List extractCardInfo(Player player) { - List cardInfo = new ArrayList<>(); - List inputCards = player.getHoldingCards().getCards(); - for (Card card : inputCards) { - cardInfo.add(card.getNumber().getName() + card.getShape().getName()); - } - return cardInfo; + public PlayerStatusDto(String name, List cards, int point) { + this.name = name; + this.cards = cards; + this.point = point; } public String getName() { From 1662ae6e5bb2eb41509acdb498154e094ecf3bab Mon Sep 17 00:00:00 2001 From: hanueleee Date: Mon, 6 Mar 2023 16:46:43 +0900 Subject: [PATCH 35/43] =?UTF-8?q?refactor:=20enum=EC=9D=B8=20Number?= =?UTF-8?q?=EC=9D=98=20=EB=AA=85=EC=B9=AD=EC=9D=84=20Symbol=EB=A1=9C=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 --- .../controller/BlackJackController.java | 2 +- src/main/java/blackjack/domain/card/Card.java | 15 +++++++------- .../domain/card/{Number.java => Symbol.java} | 4 ++-- .../blackjack/domain/card/CardDeckTest.java | 4 ++-- .../java/blackjack/domain/card/CardTest.java | 2 +- .../domain/player/ChallengerTest.java | 12 +++++------ .../blackjack/domain/player/DealerTest.java | 10 +++++----- .../domain/player/HoldingCardsTest.java | 20 +++++++++---------- .../blackjack/domain/player/PlayerTest.java | 8 ++++---- 9 files changed, 38 insertions(+), 39 deletions(-) rename src/main/java/blackjack/domain/card/{Number.java => Symbol.java} (89%) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index e13f41faad..e3fd0fba69 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -139,7 +139,7 @@ private PlayerStatusDto makePlayerStatusDto(Player player) { private static List makeCardInfo(List inputCards) { List cardInfo = new ArrayList<>(); for (Card card : inputCards) { - cardInfo.add(card.getNumber().getName() + card.getShape().getName()); + cardInfo.add(card.getSymbol().getName() + card.getShape().getName()); } return cardInfo; } diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index b5d0231fa2..1924a2ba19 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -3,27 +3,26 @@ public class Card { private final Shape shape; - private final Number number; + private final Symbol symbol; - public Card(Shape shape, Number number) { + public Card(Shape shape, Symbol symbol) { this.shape = shape; - this.number = number; + this.symbol = symbol; } public boolean isAce() { - return number.equals(Number.ACE); + return symbol.equals(Symbol.ACE); } public Shape getShape() { return shape; } - // TODO : string, number 어떤걸 get 해야할까? - public Number getNumber() { - return number; + public Symbol getSymbol() { + return symbol; } public int getPoint() { - return number.getValue(); + return symbol.getValue(); } } diff --git a/src/main/java/blackjack/domain/card/Number.java b/src/main/java/blackjack/domain/card/Symbol.java similarity index 89% rename from src/main/java/blackjack/domain/card/Number.java rename to src/main/java/blackjack/domain/card/Symbol.java index a4d73ddc02..e09ac7851c 100644 --- a/src/main/java/blackjack/domain/card/Number.java +++ b/src/main/java/blackjack/domain/card/Symbol.java @@ -1,6 +1,6 @@ package blackjack.domain.card; -public enum Number { +public enum Symbol { ACE("A", 1), TWO("2", 2), THREE("3", 3), @@ -18,7 +18,7 @@ public enum Number { private final String name; private final int value; - Number(String name, int value) { + Symbol(String name, int value) { this.name = name; this.value = value; } diff --git a/src/test/java/blackjack/domain/card/CardDeckTest.java b/src/test/java/blackjack/domain/card/CardDeckTest.java index e9baff2db6..3d53565727 100644 --- a/src/test/java/blackjack/domain/card/CardDeckTest.java +++ b/src/test/java/blackjack/domain/card/CardDeckTest.java @@ -27,9 +27,9 @@ void pick_test() { Card spadeTwo = cardDeck.pick(); assertThat(spadeAce.getShape()).isEqualTo(Shape.SPADE); - assertThat(spadeAce.getNumber()).isEqualTo(Number.ACE); + assertThat(spadeAce.getSymbol()).isEqualTo(Symbol.ACE); assertThat(spadeTwo.getShape()).isEqualTo(Shape.SPADE); - assertThat(spadeTwo.getNumber()).isEqualTo(Number.TWO); + assertThat(spadeTwo.getSymbol()).isEqualTo(Symbol.TWO); } } diff --git a/src/test/java/blackjack/domain/card/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java index 812d2b36d6..e6c83b7b90 100644 --- a/src/test/java/blackjack/domain/card/CardTest.java +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -10,7 +10,7 @@ class CardTest { @Test @DisplayName("카드의 숫자를 보고 값을 반환한다") void get_value() { - Card card = new Card(Shape.SPADE, Number.FOUR); + Card card = new Card(Shape.SPADE, Symbol.FOUR); assertThat(card.getPoint()).isEqualTo(4); } } diff --git a/src/test/java/blackjack/domain/player/ChallengerTest.java b/src/test/java/blackjack/domain/player/ChallengerTest.java index c4cc85cc4c..f9346abef5 100644 --- a/src/test/java/blackjack/domain/player/ChallengerTest.java +++ b/src/test/java/blackjack/domain/player/ChallengerTest.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import blackjack.domain.card.Card; -import blackjack.domain.card.Number; +import blackjack.domain.card.Symbol; import blackjack.domain.card.Shape; import blackjack.domain.player.exception.InvalidPlayerNameException; import java.util.List; @@ -40,14 +40,14 @@ private static Stream provideCards() { return Stream.of( Arguments.of( List.of( - new Card(Shape.DIAMOND, Number.QUEEN), - new Card(Shape.CLOVER, Number.FIVE)), + new Card(Shape.DIAMOND, Symbol.QUEEN), + new Card(Shape.CLOVER, Symbol.FIVE)), true), Arguments.of( List.of( - new Card(Shape.DIAMOND, Number.QUEEN), - new Card(Shape.CLOVER, Number.FIVE), - new Card(Shape.HEART, Number.EIGHT)), + new Card(Shape.DIAMOND, Symbol.QUEEN), + new Card(Shape.CLOVER, Symbol.FIVE), + new Card(Shape.HEART, Symbol.EIGHT)), false) ); } diff --git a/src/test/java/blackjack/domain/player/DealerTest.java b/src/test/java/blackjack/domain/player/DealerTest.java index 0a5b5534a1..19e42c8a16 100644 --- a/src/test/java/blackjack/domain/player/DealerTest.java +++ b/src/test/java/blackjack/domain/player/DealerTest.java @@ -3,7 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import blackjack.domain.card.Card; -import blackjack.domain.card.Number; +import blackjack.domain.card.Symbol; import blackjack.domain.card.Shape; import java.util.List; import java.util.stream.Stream; @@ -30,13 +30,13 @@ private static Stream provideCards() { return Stream.of( Arguments.of( List.of( - new Card(Shape.DIAMOND, Number.QUEEN), - new Card(Shape.CLOVER, Number.FOUR)), + new Card(Shape.DIAMOND, Symbol.QUEEN), + new Card(Shape.CLOVER, Symbol.FOUR)), true), Arguments.of( List.of( - new Card(Shape.DIAMOND, Number.QUEEN), - new Card(Shape.CLOVER, Number.SEVEN)), + new Card(Shape.DIAMOND, Symbol.QUEEN), + new Card(Shape.CLOVER, Symbol.SEVEN)), false) ); } diff --git a/src/test/java/blackjack/domain/player/HoldingCardsTest.java b/src/test/java/blackjack/domain/player/HoldingCardsTest.java index 647ce2b654..8db771de1a 100644 --- a/src/test/java/blackjack/domain/player/HoldingCardsTest.java +++ b/src/test/java/blackjack/domain/player/HoldingCardsTest.java @@ -3,7 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import blackjack.domain.card.Card; -import blackjack.domain.card.Number; +import blackjack.domain.card.Symbol; import blackjack.domain.card.Shape; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -21,8 +21,8 @@ void setup() { @Test @DisplayName("가진 카드의 합 테스트") void sum_of_cards() { - Card card1 = new Card(Shape.DIAMOND, Number.KING); - Card card2 = new Card(Shape.SPADE, Number.FOUR); + Card card1 = new Card(Shape.DIAMOND, Symbol.KING); + Card card2 = new Card(Shape.SPADE, Symbol.FOUR); holdingCards.initialCard(card1, card2); assertThat(holdingCards.getSum()).isEqualTo(14); @@ -31,10 +31,10 @@ void sum_of_cards() { @Test @DisplayName("ACE가 존재하는 경우 가능한 모든 합 테스트") void sums_of_ace_existing() { - Card cloverAce = new Card(Shape.CLOVER, Number.ACE); - Card cloverKing = new Card(Shape.CLOVER, Number.KING); - Card spadeAce = new Card(Shape.SPADE, Number.ACE); - Card heartAce = new Card(Shape.HEART, Number.ACE); + Card cloverAce = new Card(Shape.CLOVER, Symbol.ACE); + Card cloverKing = new Card(Shape.CLOVER, Symbol.KING); + Card spadeAce = new Card(Shape.SPADE, Symbol.ACE); + Card heartAce = new Card(Shape.HEART, Symbol.ACE); holdingCards.initialCard(cloverAce, cloverKing); holdingCards.add(spadeAce); holdingCards.add(heartAce); @@ -46,9 +46,9 @@ void sums_of_ace_existing() { @Test @DisplayName("ACE가 존재하는 경우 21에 가장 가까운 합을 찾는지 확인하는 테스트") void find_sum_of_nearly_21() { - Card cloverAce = new Card(Shape.CLOVER, Number.ACE); - Card cloverFive = new Card(Shape.CLOVER, Number.FIVE); - Card spadeAce = new Card(Shape.SPADE, Number.ACE); + Card cloverAce = new Card(Shape.CLOVER, Symbol.ACE); + Card cloverFive = new Card(Shape.CLOVER, Symbol.FIVE); + Card spadeAce = new Card(Shape.SPADE, Symbol.ACE); holdingCards.initialCard(cloverAce, cloverFive); holdingCards.add(spadeAce); diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index 9c43d88f08..f475861186 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -3,7 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import blackjack.domain.card.Card; -import blackjack.domain.card.Number; +import blackjack.domain.card.Symbol; import blackjack.domain.card.Shape; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -32,8 +32,8 @@ public String getName() { @Test @DisplayName("플레이어는 초기 카드 2장을 받는다") void get_two_cards() { - Card card1 = new Card(Shape.HEART, Number.FOUR); - Card card2 = new Card(Shape.CLOVER, Number.KING); + Card card1 = new Card(Shape.HEART, Symbol.FOUR); + Card card2 = new Card(Shape.CLOVER, Symbol.KING); player.pickStartCards(card1, card2); assertThat(player.getHoldingCards().getCards()) @@ -43,7 +43,7 @@ void get_two_cards() { @Test @DisplayName("추가 카드를 뽑는다.") void pick_card() { - Card card = new Card(Shape.DIAMOND, Number.JACK); + Card card = new Card(Shape.DIAMOND, Symbol.JACK); player.pick(card); assertThat(player.getHoldingCards().getCards()) From f8c5f0cd6edd36f5fbf67179fa09b082db255932 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Mon, 6 Mar 2023 16:50:16 +0900 Subject: [PATCH 36/43] =?UTF-8?q?refactor:=20CardDeck=EC=9D=98=20create?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=EC=97=90=20flatMap=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/card/CardDeck.java | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index a0b4732fd7..d07624ef46 100644 --- a/src/main/java/blackjack/domain/card/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -1,7 +1,10 @@ package blackjack.domain.card; +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.toList; + import blackjack.domain.card.exception.NoMoreCardException; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -12,23 +15,10 @@ public class CardDeck { private CardDeck(List cards) { this.cards = cards; } - public static CardDeck create() { - List cards = new ArrayList<>(); - addShapeSets(cards); - return new CardDeck(cards); - } - - private static void addShapeSets(List cards) { - for (Shape shape : Shape.values()) { - addNumberSets(cards, shape); - } - } - - private static void addNumberSets(List cards, Shape shape) { - for (Number number : Number.values()) { - cards.add(new Card(shape, number)); - } + return Arrays.stream(Shape.values()) + .flatMap(shape -> Arrays.stream(Symbol.values()).map(symbol -> new Card(shape, symbol))) + .collect(collectingAndThen(toList(), CardDeck::new)); } public void shuffle() { From 37ac7a56a8aad111c48a0a29a3b6a6c4c7a88685 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Tue, 7 Mar 2023 13:33:13 +0900 Subject: [PATCH 37/43] =?UTF-8?q?refactor:=20=EC=8A=B9=ED=8C=A8=20?= =?UTF-8?q?=ED=8C=90=EB=8B=A8=20=EB=A1=9C=EC=A7=81=EC=9D=84=20Dealer?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 30 +++---- .../java/blackjack/domain/BlackJackGame.java | 6 +- .../java/blackjack/domain/card/CardDeck.java | 1 + .../blackjack/domain/player/Challenger.java | 3 +- .../java/blackjack/domain/player/Dealer.java | 30 ++++++- .../java/blackjack/domain/player/Player.java | 12 ++- .../java/blackjack/domain/result/Result.java | 78 ------------------- .../blackjack/domain/result/ResultMap.java | 41 ++++++++++ .../result/{Rank.java => ResultType.java} | 10 +-- src/main/java/blackjack/view/OutputView.java | 6 +- .../domain/result/ResultMapTest.java | 53 +++++++++++++ 11 files changed, 160 insertions(+), 110 deletions(-) delete mode 100644 src/main/java/blackjack/domain/result/Result.java create mode 100644 src/main/java/blackjack/domain/result/ResultMap.java rename src/main/java/blackjack/domain/result/{Rank.java => ResultType.java} (63%) create mode 100644 src/test/java/blackjack/domain/result/ResultMapTest.java diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index e3fd0fba69..11056a4294 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -4,8 +4,8 @@ import blackjack.domain.BlackJackGame; import blackjack.domain.card.Card; import blackjack.domain.player.Player; -import blackjack.domain.result.Result; -import blackjack.domain.result.Results; +import blackjack.domain.result.ResultMap; +import blackjack.domain.result.ResultType; import blackjack.dto.ChallengerResultDto; import blackjack.dto.DealerResultDto; import blackjack.dto.PlayerStatusDto; @@ -122,10 +122,10 @@ private void showPoint() { } private void showRank() { - Results results = blackJackGame.makeResult(); + ResultMap resultMap = blackJackGame.makeResult(); - ChallengerResultDto challengerResultDto = makeChallengerResultDto(results, blackJackGame.getChallengers()); - DealerResultDto dealerResultDto = makeDealerResultDto(results, blackJackGame.getDealer()); + ChallengerResultDto challengerResultDto = makeChallengerResultDto(resultMap, blackJackGame.getChallengers()); + DealerResultDto dealerResultDto = makeDealerResultDto(resultMap, blackJackGame.getDealer()); OutputView.printFinalRank(challengerResultDto, dealerResultDto); } @@ -144,21 +144,21 @@ private static List makeCardInfo(List inputCards) { return cardInfo; } - private ChallengerResultDto makeChallengerResultDto(Results results, List challengers) { - Map nameAndRanks = new LinkedHashMap<>(); + private ChallengerResultDto makeChallengerResultDto(ResultMap resultMap, List challengers) { + Map nameAndResult = new LinkedHashMap<>(); for (Player challenger : challengers) { - Result challengerResult = results.getChallengerResult(challenger); - nameAndRanks.put(challenger.getName(), challengerResult.getLabel()); + ResultType challengerResultType = resultMap.getChallengerResult(challenger); + nameAndResult.put(challenger.getName(), challengerResultType.getLabel()); } - return new ChallengerResultDto(nameAndRanks); + return new ChallengerResultDto(nameAndResult); } - private DealerResultDto makeDealerResultDto(Results results, Player dealer) { + private DealerResultDto makeDealerResultDto(ResultMap resultMap, Player dealer) { String dealerName = dealer.getName(); - Map dealerResult = results.getDealerResult(); - int winCount = dealerResult.getOrDefault(Result.WIN, 0); - int drawCount = dealerResult.getOrDefault(Result.DRAW, 0); - int loseCount = dealerResult.getOrDefault(Result.LOSE, 0); + Map dealerResult = resultMap.getDealerResult(); + int winCount = dealerResult.getOrDefault(ResultType.WIN, 0); + int drawCount = dealerResult.getOrDefault(ResultType.DRAW, 0); + int loseCount = dealerResult.getOrDefault(ResultType.LOSE, 0); return new DealerResultDto(dealerName, winCount, drawCount, loseCount); } } diff --git a/src/main/java/blackjack/domain/BlackJackGame.java b/src/main/java/blackjack/domain/BlackJackGame.java index 650031f60f..9977bc7d03 100644 --- a/src/main/java/blackjack/domain/BlackJackGame.java +++ b/src/main/java/blackjack/domain/BlackJackGame.java @@ -4,7 +4,7 @@ import blackjack.domain.card.CardDeck; import blackjack.domain.player.Player; import blackjack.domain.player.Players; -import blackjack.domain.result.Result; +import blackjack.domain.result.ResultMap; import java.util.List; public class BlackJackGame { @@ -36,8 +36,8 @@ public void pick(Player player) { player.pick(pickedCard); } - public Result makeResult() { - return Result.from(players); + public ResultMap makeResult() { + return ResultMap.from(players); } public Player getDealer() { diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index d07624ef46..565c0df7b7 100644 --- a/src/main/java/blackjack/domain/card/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -15,6 +15,7 @@ public class CardDeck { private CardDeck(List cards) { this.cards = cards; } + public static CardDeck create() { return Arrays.stream(Shape.values()) .flatMap(shape -> Arrays.stream(Symbol.values()).map(symbol -> new Card(shape, symbol))) diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java index c775b43566..6fdb43352d 100644 --- a/src/main/java/blackjack/domain/player/Challenger.java +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -6,7 +6,6 @@ public class Challenger extends Player { private static final String INVALID_NAME = "딜러"; - private static final int MAXIMUM_POINT = 21; private final String name; @@ -25,7 +24,7 @@ private void validateName(String name) { public Boolean canPick() { List sumPossibility = holdingCards.getSums(); return sumPossibility.stream() - .anyMatch(sum -> sum <= MAXIMUM_POINT); + .anyMatch(sum -> sum <= BLACKJACK_POINT); } @Override diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java index b8df8d12f8..c1244d7572 100644 --- a/src/main/java/blackjack/domain/player/Dealer.java +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -1,18 +1,44 @@ package blackjack.domain.player; +import blackjack.domain.result.ResultType; import java.util.List; public class Dealer extends Player { private static final String NAME = "딜러"; - private static final int MAXIMUM_POINT_TO_PICK = 16; + private static final int MAXIMUM_POINT = 16; + + public ResultType judge(Player challenger) { + int dealerPoint = this.getTotalPoint(); + int challengerPoint = challenger.getTotalPoint(); + if (isBust() && challenger.isBust()) { + return ResultType.DRAW; + } + if (isBust() && !challenger.isBust()) { + return ResultType.WIN; + } + if (challenger.isBust()) { + return ResultType.LOSE; + } + return comparePoint(dealerPoint, challengerPoint); + } + + private static ResultType comparePoint(int dealerPoint, int challengerPoint) { + if (challengerPoint < dealerPoint) { + return ResultType.LOSE; + } + if (challengerPoint > dealerPoint) { + return ResultType.WIN; + } + return ResultType.DRAW; + } @Override public Boolean canPick() { List sumPossibility = holdingCards.getSums(); return sumPossibility.stream() - .anyMatch(sum -> sum <= MAXIMUM_POINT_TO_PICK); + .anyMatch(sum -> sum <= MAXIMUM_POINT); } @Override diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index 8d5260edb0..e5f0fa396b 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -3,6 +3,7 @@ import blackjack.domain.card.Card; public abstract class Player { + protected static final int BLACKJACK_POINT = 21; protected final HoldingCards holdingCards; @@ -10,8 +11,8 @@ protected Player() { this.holdingCards = new HoldingCards(); } - public void pickStartCards(Card card1, Card card2) { - holdingCards.initialCard(card1, card2); + public void pickStartCards(Card firstCard, Card secondCard) { + holdingCards.initialCard(firstCard, secondCard); } public HoldingCards getHoldingCards() { @@ -26,6 +27,13 @@ public int getTotalPoint() { return holdingCards.getSum(); } + public boolean isBust() { + if (holdingCards.getSum() > BLACKJACK_POINT) { + return true; + } + return false; + } + public abstract Boolean canPick(); public abstract String getName(); diff --git a/src/main/java/blackjack/domain/result/Result.java b/src/main/java/blackjack/domain/result/Result.java deleted file mode 100644 index 425984d9c7..0000000000 --- a/src/main/java/blackjack/domain/result/Result.java +++ /dev/null @@ -1,78 +0,0 @@ -package blackjack.domain.result; - -import blackjack.domain.player.Player; -import blackjack.domain.player.Players; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -public class Result { - private static final int MAXIMUM_POINT = 21; - - private final Map results; - - private Result(Map results) { - this.results = results; - } - - public static Result from(Players players) { - Map results = new LinkedHashMap<>(); - int dealerPoint = players.getDealer().getTotalPoint(); - List challengers = players.getChallengers(); - for (Player challenger : challengers) { - Rank rank = makePlayerResult(dealerPoint, challenger); - results.put(challenger, rank); - } - return new Result(results); - } - - private static Rank makePlayerResult(int dealerPoint, Player challenger) { - int challengerPoint = challenger.getTotalPoint(); - if (isOverMaximumPoint(dealerPoint)) { - return getRankWhenDealerOverPoint(challengerPoint); - } - return getRankWhenDealerNotOverPoint(dealerPoint, challengerPoint); - } - - private static Rank getRankWhenDealerOverPoint(int challengerPoint) { - if (isOverMaximumPoint(challengerPoint)) { - return Rank.DRAW; - } - return Rank.WIN; - } - - private static Rank getRankWhenDealerNotOverPoint(int dealerPoint, int challengerPoint) { - if (isOverMaximumPoint(challengerPoint)) { - return Rank.LOSE; - } - return comparePoint(dealerPoint, challengerPoint); - } - - private static Rank comparePoint(int dealerPoint, int challengerPoint) { - if (challengerPoint < dealerPoint) { - return Rank.LOSE; - } - if (challengerPoint > dealerPoint) { - return Rank.WIN; - } - return Rank.DRAW; - } - - private static boolean isOverMaximumPoint(int sum) { - return sum > MAXIMUM_POINT; - } - - public Rank getChallengerResult(Player player) { - return results.get(player); - } - - public Map getDealerResult() { - Map dealerResult = new HashMap<>(); - for (Rank rank : results.values()) { - Rank dealerRank = rank.getOpposite(); - dealerResult.put(dealerRank, dealerResult.getOrDefault(dealerRank, 0) + 1); - } - return dealerResult; - } -} diff --git a/src/main/java/blackjack/domain/result/ResultMap.java b/src/main/java/blackjack/domain/result/ResultMap.java new file mode 100644 index 0000000000..5b9af5d9b5 --- /dev/null +++ b/src/main/java/blackjack/domain/result/ResultMap.java @@ -0,0 +1,41 @@ +package blackjack.domain.result; + +import blackjack.domain.player.Dealer; +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class ResultMap { + private final Map results; + + private ResultMap(Map results) { + this.results = results; + } + + public static ResultMap from(Players players) { + Map results = new LinkedHashMap<>(); + Dealer dealer = (Dealer) players.getDealer(); + List challengers = players.getChallengers(); + for (Player challenger : challengers) { + ResultType resultType = dealer.judge(challenger); + results.put(challenger, resultType); + } + return new ResultMap(results); + } + + public ResultType getChallengerResult(Player player) { + return results.get(player); + } + + public Map getDealerResult() { + Map dealerResult = new HashMap<>(); + for (ResultType resultType : results.values()) { + ResultType dealerRank = resultType.getOpposite(); + dealerResult.put(dealerRank, dealerResult.getOrDefault(dealerRank, 0) + 1); + } + return dealerResult; + } +} diff --git a/src/main/java/blackjack/domain/result/Rank.java b/src/main/java/blackjack/domain/result/ResultType.java similarity index 63% rename from src/main/java/blackjack/domain/result/Rank.java rename to src/main/java/blackjack/domain/result/ResultType.java index 167c11ee4e..94c3b61ddf 100644 --- a/src/main/java/blackjack/domain/result/Rank.java +++ b/src/main/java/blackjack/domain/result/ResultType.java @@ -1,6 +1,6 @@ package blackjack.domain.result; -public enum Rank { +public enum ResultType { WIN("승"), DRAW("무"), @@ -8,15 +8,15 @@ public enum Rank { private final String label; - Rank(String label) { + ResultType(String label) { this.label = label; } - public Rank getOpposite() { - if (this.equals(Rank.WIN)) { + public ResultType getOpposite() { + if (this.equals(ResultType.WIN)) { return LOSE; } - if (this.equals(Rank.LOSE)) { + if (this.equals(ResultType.LOSE)) { return WIN; } return DRAW; diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 0bdf47823b..609ba47167 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -126,9 +126,9 @@ private static void printDealerLoseCount(DealerResultDto dealerResultDto) { } private static void printChallengersFinalRank(ChallengerResultDto challengerResultDto) { - Map nameAndRanks = challengerResultDto.getNameAndRanks(); - for (String name : nameAndRanks.keySet()) { - System.out.println(name + PLAYER_NAME_AND_CARDS_PARTITION + nameAndRanks.get(name)); + Map nameAndResult = challengerResultDto.getNameAndResult(); + for (String name : nameAndResult.keySet()) { + System.out.println(name + PLAYER_NAME_AND_CARDS_PARTITION + nameAndResult.get(name)); } } } diff --git a/src/test/java/blackjack/domain/result/ResultMapTest.java b/src/test/java/blackjack/domain/result/ResultMapTest.java new file mode 100644 index 0000000000..cd539f1c73 --- /dev/null +++ b/src/test/java/blackjack/domain/result/ResultMapTest.java @@ -0,0 +1,53 @@ +package blackjack.domain.result; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Symbol; +import blackjack.domain.card.Shape; +import blackjack.domain.player.Player; +import blackjack.domain.player.Players; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class ResultMapTest { + + private ResultMap resultMap; + private Players players; + + @BeforeEach + void setPlayers() { + players = Players.from(List.of("oing", "ditoo")); + + Card heartKing = new Card(Shape.HEART, Symbol.KING); + Card heartSeven = new Card(Shape.HEART, Symbol.SEVEN); + Card heartThree = new Card(Shape.HEART, Symbol.THREE); + + players.getDealer().pick(heartSeven); + players.getChallengers().get(0).pick(heartThree); + players.getChallengers().get(1).pick(heartKing); + resultMap = ResultMap.from(players); + } + + @Test + @DisplayName("도전자들의 승패가 올바르게 계산되는지 확인한다") + void check_challengers_result() { + Player oing = players.getChallengers().get(0); + Player ditoo = players.getChallengers().get(1); + + assertThat(resultMap.getChallengerResult(oing)).isEqualTo(ResultType.LOSE); + assertThat(resultMap.getChallengerResult(ditoo)).isEqualTo(ResultType.WIN); + } + + @Test + @DisplayName("딜러의 승패가 올바르게 계산되는지 확인한다") + void check_dealer_result() { + Map dealerResult = resultMap.getDealerResult(); + assertThat(dealerResult) + .containsOnly(entry(ResultType.WIN, 1), entry(ResultType.LOSE, 1)); + } +} From 69e781f1fc47f6b6c96a9b93d2a1b5bc24b7859c Mon Sep 17 00:00:00 2001 From: hanueleee Date: Tue, 7 Mar 2023 14:24:02 +0900 Subject: [PATCH 38/43] =?UTF-8?q?refactor:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=ED=95=A9=20=EA=B3=84=EC=82=B0=20=EB=A1=9C=EC=A7=81=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/BlackJackGame.java | 2 +- .../blackjack/domain/player/Challenger.java | 5 +- .../java/blackjack/domain/player/Dealer.java | 13 ++--- .../blackjack/domain/player/HoldingCards.java | 32 ++++------- .../java/blackjack/domain/player/Player.java | 20 +++---- .../domain/player/ChallengerTest.java | 4 +- .../blackjack/domain/player/DealerTest.java | 4 +- .../domain/player/HoldingCardsTest.java | 37 ++++++------- .../blackjack/domain/player/PlayerTest.java | 4 +- .../domain/result/ResultMapTest.java | 8 +-- .../blackjack/domain/result/ResultTest.java | 53 ------------------- 11 files changed, 56 insertions(+), 126 deletions(-) delete mode 100644 src/test/java/blackjack/domain/result/ResultTest.java diff --git a/src/main/java/blackjack/domain/BlackJackGame.java b/src/main/java/blackjack/domain/BlackJackGame.java index 9977bc7d03..a02dc3c298 100644 --- a/src/main/java/blackjack/domain/BlackJackGame.java +++ b/src/main/java/blackjack/domain/BlackJackGame.java @@ -33,7 +33,7 @@ public boolean canPick(Player player) { public void pick(Player player) { Card pickedCard = cardDeck.pick(); - player.pick(pickedCard); + player.pickCard(pickedCard); } public ResultMap makeResult() { diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java index 6fdb43352d..e9b83f6f70 100644 --- a/src/main/java/blackjack/domain/player/Challenger.java +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -1,7 +1,6 @@ package blackjack.domain.player; import blackjack.domain.player.exception.InvalidPlayerNameException; -import java.util.List; public class Challenger extends Player { @@ -22,9 +21,7 @@ private void validateName(String name) { @Override public Boolean canPick() { - List sumPossibility = holdingCards.getSums(); - return sumPossibility.stream() - .anyMatch(sum -> sum <= BLACKJACK_POINT); + return holdingCards.getSum() <= BLACKJACK_POINT; } @Override diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java index c1244d7572..3e69fd4051 100644 --- a/src/main/java/blackjack/domain/player/Dealer.java +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -1,7 +1,6 @@ package blackjack.domain.player; import blackjack.domain.result.ResultType; -import java.util.List; public class Dealer extends Player { @@ -10,8 +9,6 @@ public class Dealer extends Player { private static final int MAXIMUM_POINT = 16; public ResultType judge(Player challenger) { - int dealerPoint = this.getTotalPoint(); - int challengerPoint = challenger.getTotalPoint(); if (isBust() && challenger.isBust()) { return ResultType.DRAW; } @@ -21,10 +18,12 @@ public ResultType judge(Player challenger) { if (challenger.isBust()) { return ResultType.LOSE; } - return comparePoint(dealerPoint, challengerPoint); + return comparePoint(challenger); } - private static ResultType comparePoint(int dealerPoint, int challengerPoint) { + private ResultType comparePoint(Player challenger) { + int dealerPoint = this.getTotalPoint(); + int challengerPoint = challenger.getTotalPoint(); if (challengerPoint < dealerPoint) { return ResultType.LOSE; } @@ -36,9 +35,7 @@ private static ResultType comparePoint(int dealerPoint, int challengerPoint) { @Override public Boolean canPick() { - List sumPossibility = holdingCards.getSums(); - return sumPossibility.stream() - .anyMatch(sum -> sum <= MAXIMUM_POINT); + return holdingCards.getSum() <= MAXIMUM_POINT; } @Override diff --git a/src/main/java/blackjack/domain/player/HoldingCards.java b/src/main/java/blackjack/domain/player/HoldingCards.java index 51b7206b84..7520bc7f27 100644 --- a/src/main/java/blackjack/domain/player/HoldingCards.java +++ b/src/main/java/blackjack/domain/player/HoldingCards.java @@ -7,12 +7,12 @@ public class HoldingCards { private static final int MAXIMUM_SUM = 21; - private static final int ACE_PLUS_POINT = 10; + private static final int ACE_BONUS = 10; private final List cards = new ArrayList<>(); - public void initialCard(Card card1, Card card2) { - cards.addAll(List.of(card1, card2)); + public void initialize(Card firstCard, Card secondCard) { + cards.addAll(List.of(firstCard, secondCard)); } public void add(Card card) { @@ -23,14 +23,14 @@ public List getCards() { return List.copyOf(cards); } - public List getSums() { - int defaultSum = getNotAceSum() + getAceCount(); - List possibility = new ArrayList<>(); - possibility.add(defaultSum); - for (int i = 1; i <= getAceCount(); i++) { - possibility.add(defaultSum + ACE_PLUS_POINT * i); + public int getSum() { + int aceCount = getAceCount(); + int pointSum = getNotAceSum() + aceCount; + while (pointSum + ACE_BONUS <= MAXIMUM_SUM && aceCount > 0) { + pointSum += ACE_BONUS; + aceCount--; } - return possibility; + return pointSum; } private int getNotAceSum() { @@ -45,16 +45,4 @@ private int getAceCount() { .filter(Card::isAce) .count(); } - - public int getSum() { - return getSums().stream() - .filter(sum -> sum < MAXIMUM_SUM) - .mapToInt(sum -> sum) - .max() - .orElse(getMinimumSum()); - } - - private int getMinimumSum() { - return getSums().get(0); - } } diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index e5f0fa396b..a96d2be441 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -12,21 +12,13 @@ protected Player() { } public void pickStartCards(Card firstCard, Card secondCard) { - holdingCards.initialCard(firstCard, secondCard); + holdingCards.initialize(firstCard, secondCard); } - public HoldingCards getHoldingCards() { - return holdingCards; - } - - public void pick(Card card) { + public void pickCard(Card card) { holdingCards.add(card); } - public int getTotalPoint() { - return holdingCards.getSum(); - } - public boolean isBust() { if (holdingCards.getSum() > BLACKJACK_POINT) { return true; @@ -34,6 +26,14 @@ public boolean isBust() { return false; } + public int getTotalPoint() { + return holdingCards.getSum(); + } + + public HoldingCards getHoldingCards() { + return holdingCards; + } + public abstract Boolean canPick(); public abstract String getName(); diff --git a/src/test/java/blackjack/domain/player/ChallengerTest.java b/src/test/java/blackjack/domain/player/ChallengerTest.java index f9346abef5..300cd88fdd 100644 --- a/src/test/java/blackjack/domain/player/ChallengerTest.java +++ b/src/test/java/blackjack/domain/player/ChallengerTest.java @@ -4,8 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import blackjack.domain.card.Card; -import blackjack.domain.card.Symbol; import blackjack.domain.card.Shape; +import blackjack.domain.card.Symbol; import blackjack.domain.player.exception.InvalidPlayerNameException; import java.util.List; import java.util.stream.Stream; @@ -30,7 +30,7 @@ void validate_name() { void checking_sum_is_over_21(List cards, boolean expected) { Player player = new Challenger("neo"); for (Card card : cards) { - player.pick(card); + player.pickCard(card); } assertThat(player.canPick()).isEqualTo(expected); diff --git a/src/test/java/blackjack/domain/player/DealerTest.java b/src/test/java/blackjack/domain/player/DealerTest.java index 19e42c8a16..9757497db7 100644 --- a/src/test/java/blackjack/domain/player/DealerTest.java +++ b/src/test/java/blackjack/domain/player/DealerTest.java @@ -3,8 +3,8 @@ import static org.assertj.core.api.Assertions.assertThat; import blackjack.domain.card.Card; -import blackjack.domain.card.Symbol; import blackjack.domain.card.Shape; +import blackjack.domain.card.Symbol; import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.api.DisplayName; @@ -20,7 +20,7 @@ class DealerTest { void checking_sum_is_over_16(List cards, boolean expected) { Player player = new Dealer(); for (Card card : cards) { - player.pick(card); + player.pickCard(card); } assertThat(player.canPick()).isEqualTo(expected); diff --git a/src/test/java/blackjack/domain/player/HoldingCardsTest.java b/src/test/java/blackjack/domain/player/HoldingCardsTest.java index 8db771de1a..f447c5e4d7 100644 --- a/src/test/java/blackjack/domain/player/HoldingCardsTest.java +++ b/src/test/java/blackjack/domain/player/HoldingCardsTest.java @@ -3,8 +3,8 @@ import static org.assertj.core.api.Assertions.assertThat; import blackjack.domain.card.Card; -import blackjack.domain.card.Symbol; import blackjack.domain.card.Shape; +import blackjack.domain.card.Symbol; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -21,26 +21,25 @@ void setup() { @Test @DisplayName("가진 카드의 합 테스트") void sum_of_cards() { - Card card1 = new Card(Shape.DIAMOND, Symbol.KING); - Card card2 = new Card(Shape.SPADE, Symbol.FOUR); - holdingCards.initialCard(card1, card2); + Card DiamondKing = new Card(Shape.DIAMOND, Symbol.KING); + Card SpadeFour = new Card(Shape.SPADE, Symbol.FOUR); + + holdingCards.initialize(DiamondKing, SpadeFour); assertThat(holdingCards.getSum()).isEqualTo(14); } @Test - @DisplayName("ACE가 존재하는 경우 가능한 모든 합 테스트") - void sums_of_ace_existing() { - Card cloverAce = new Card(Shape.CLOVER, Symbol.ACE); - Card cloverKing = new Card(Shape.CLOVER, Symbol.KING); - Card spadeAce = new Card(Shape.SPADE, Symbol.ACE); - Card heartAce = new Card(Shape.HEART, Symbol.ACE); - holdingCards.initialCard(cloverAce, cloverKing); - holdingCards.add(spadeAce); - holdingCards.add(heartAce); + @DisplayName("합이 큰 경우 테스트") + void big_sum() { + Card diamondKing = new Card(Shape.DIAMOND, Symbol.KING); + Card heartKIng = new Card(Shape.HEART, Symbol.KING); + Card SpadeFour = new Card(Shape.SPADE, Symbol.FOUR); + + holdingCards.initialize(diamondKing, heartKIng); + holdingCards.add(SpadeFour); - assertThat(holdingCards.getSums()) - .containsExactly(13, 23, 33, 43); + assertThat(holdingCards.getSum()).isEqualTo(24); } @Test @@ -49,10 +48,12 @@ void find_sum_of_nearly_21() { Card cloverAce = new Card(Shape.CLOVER, Symbol.ACE); Card cloverFive = new Card(Shape.CLOVER, Symbol.FIVE); Card spadeAce = new Card(Shape.SPADE, Symbol.ACE); - holdingCards.initialCard(cloverAce, cloverFive); + Card heartAce = new Card(Shape.HEART, Symbol.ACE); + + holdingCards.initialize(cloverAce, cloverFive); holdingCards.add(spadeAce); + holdingCards.add(heartAce); - assertThat(holdingCards.getSum()) - .isEqualTo(17); + assertThat(holdingCards.getSum()).isEqualTo(18); } } diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index f475861186..fd18a5dd48 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -3,8 +3,8 @@ import static org.assertj.core.api.Assertions.assertThat; import blackjack.domain.card.Card; -import blackjack.domain.card.Symbol; import blackjack.domain.card.Shape; +import blackjack.domain.card.Symbol; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -44,7 +44,7 @@ void get_two_cards() { @DisplayName("추가 카드를 뽑는다.") void pick_card() { Card card = new Card(Shape.DIAMOND, Symbol.JACK); - player.pick(card); + player.pickCard(card); assertThat(player.getHoldingCards().getCards()) .contains(card); diff --git a/src/test/java/blackjack/domain/result/ResultMapTest.java b/src/test/java/blackjack/domain/result/ResultMapTest.java index cd539f1c73..b5a4a19086 100644 --- a/src/test/java/blackjack/domain/result/ResultMapTest.java +++ b/src/test/java/blackjack/domain/result/ResultMapTest.java @@ -4,8 +4,8 @@ import static org.assertj.core.api.Assertions.entry; import blackjack.domain.card.Card; -import blackjack.domain.card.Symbol; import blackjack.domain.card.Shape; +import blackjack.domain.card.Symbol; import blackjack.domain.player.Player; import blackjack.domain.player.Players; import java.util.List; @@ -27,9 +27,9 @@ void setPlayers() { Card heartSeven = new Card(Shape.HEART, Symbol.SEVEN); Card heartThree = new Card(Shape.HEART, Symbol.THREE); - players.getDealer().pick(heartSeven); - players.getChallengers().get(0).pick(heartThree); - players.getChallengers().get(1).pick(heartKing); + players.getDealer().pickCard(heartSeven); + players.getChallengers().get(0).pickCard(heartThree); + players.getChallengers().get(1).pickCard(heartKing); resultMap = ResultMap.from(players); } diff --git a/src/test/java/blackjack/domain/result/ResultTest.java b/src/test/java/blackjack/domain/result/ResultTest.java deleted file mode 100644 index 60f042f199..0000000000 --- a/src/test/java/blackjack/domain/result/ResultTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package blackjack.domain.result; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.entry; - -import blackjack.domain.card.Card; -import blackjack.domain.card.Number; -import blackjack.domain.card.Shape; -import blackjack.domain.player.Player; -import blackjack.domain.player.Players; -import java.util.List; -import java.util.Map; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -class ResultTest { - - private Result result; - private Players players; - - @BeforeEach - void setPlayers() { - players = Players.from(List.of("oing", "ditoo")); - - Card heartKing = new Card(Shape.HEART, Number.KING); - Card heartSeven = new Card(Shape.HEART, Number.SEVEN); - Card heartThree = new Card(Shape.HEART, Number.THREE); - - players.getDealer().pick(heartSeven); - players.getChallengers().get(0).pick(heartThree); - players.getChallengers().get(1).pick(heartKing); - result = Result.from(players); - } - - @Test - @DisplayName("도전자들의 승패가 올바르게 계산되는지 확인한다") - void check_challengers_result() { - Player oing = players.getChallengers().get(0); - Player ditoo = players.getChallengers().get(1); - - assertThat(result.getChallengerResult(oing)).isEqualTo(Rank.LOSE); - assertThat(result.getChallengerResult(ditoo)).isEqualTo(Rank.WIN); - } - - @Test - @DisplayName("딜러의 승패가 올바르게 계산되는지 확인한다") - void check_dealer_result() { - Map dealerResult = result.getDealerResult(); - assertThat(dealerResult) - .containsOnly(entry(Rank.WIN, 1), entry(Rank.LOSE, 1)); - } -} From 1431d3725d059d1009a324a4d6b91bfc2a15c994 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Tue, 7 Mar 2023 15:31:04 +0900 Subject: [PATCH 39/43] =?UTF-8?q?refactor:=20instanceof=20=EB=8C=80?= =?UTF-8?q?=EC=8B=A0=20abstract=20method=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Challenger.java | 5 +++++ src/main/java/blackjack/domain/player/Dealer.java | 5 +++++ src/main/java/blackjack/domain/player/Player.java | 2 ++ src/main/java/blackjack/domain/player/Players.java | 6 +++--- src/test/java/blackjack/domain/player/PlayerTest.java | 5 +++++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java index e9b83f6f70..167e446ffc 100644 --- a/src/main/java/blackjack/domain/player/Challenger.java +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -28,4 +28,9 @@ public Boolean canPick() { public String getName() { return name; } + + @Override + public boolean isDealer() { + return false; + } } diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java index 3e69fd4051..76c9107c99 100644 --- a/src/main/java/blackjack/domain/player/Dealer.java +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -42,4 +42,9 @@ public Boolean canPick() { public String getName() { return NAME; } + + @Override + public boolean isDealer() { + return true; + } } diff --git a/src/main/java/blackjack/domain/player/Player.java b/src/main/java/blackjack/domain/player/Player.java index a96d2be441..337da5e77a 100644 --- a/src/main/java/blackjack/domain/player/Player.java +++ b/src/main/java/blackjack/domain/player/Player.java @@ -37,4 +37,6 @@ public HoldingCards getHoldingCards() { public abstract Boolean canPick(); public abstract String getName(); + + public abstract boolean isDealer(); } diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index 705c456e14..cbc7e0ccb6 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -45,14 +45,14 @@ public void pickStartCards(CardDeck cardDeck) { public List getChallengers() { return players.stream() - .filter(player -> player instanceof Challenger) + .filter(player -> !player.isDealer()) .collect(Collectors.toUnmodifiableList()); } public Player getDealer() { return players.stream() - .filter(player -> player instanceof Dealer) + .filter(player -> player.isDealer()) .findFirst() - .orElse(null); + .orElseThrow(() -> new IllegalArgumentException("딜러가 존재하지 않습니다.")); } } diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index fd18a5dd48..684c6fab13 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -25,6 +25,11 @@ public Boolean canPick() { public String getName() { return null; } + + @Override + public boolean isDealer() { + return false; + } }; } From d29f513925994552fd0aef856fdea943fa03ea74 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Tue, 7 Mar 2023 16:14:00 +0900 Subject: [PATCH 40/43] =?UTF-8?q?refactor:=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=EC=97=90=20=EC=9D=98=EC=A1=B4=ED=95=98=EB=8A=94=20=EB=B7=B0=20?= =?UTF-8?q?=EC=86=8D=20=EB=A7=A4=EC=A7=81=EB=84=98=EB=B2=84=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.java | 12 ++++++------ src/main/java/blackjack/domain/BlackJackGame.java | 6 +----- src/main/java/blackjack/domain/player/Dealer.java | 2 +- src/main/java/blackjack/view/OutputView.java | 12 ++++++------ 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 11056a4294..45fb8a3e95 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -3,6 +3,7 @@ import blackjack.common.exception.CustomException; import blackjack.domain.BlackJackGame; import blackjack.domain.card.Card; +import blackjack.domain.player.Dealer; import blackjack.domain.player.Player; import blackjack.domain.result.ResultMap; import blackjack.domain.result.ResultType; @@ -26,7 +27,6 @@ public void run() { start(); takeTurn(); showResult(); - InputView.terminate(); } private void init() { @@ -78,7 +78,7 @@ private void takeAllChallengersTurn() { } private void takeEachChallengerTurn(Player player) { - if (blackJackGame.canPick(player)) { + if (player.canPick()) { checkChoice(player); } } @@ -95,7 +95,7 @@ private void checkChoice(Player player) { private void inputChoice(Player player) { boolean choice = InputView.inputPlayerChoice(player.getName()); if (choice) { - blackJackGame.pick(player); + blackJackGame.hit(player); OutputView.printChallengerStatusInGame(makePlayerStatusDto(player)); takeEachChallengerTurn(player); } @@ -105,9 +105,9 @@ private void takeDealerTurn() { Player dealer = blackJackGame.getDealer(); boolean dealerCanPick = dealer.canPick(); if (dealerCanPick) { - blackJackGame.pick(dealer); + blackJackGame.hit(dealer); } - OutputView.printDealerResult(dealerCanPick); + OutputView.printDealerTurnResult(dealerCanPick, Dealer.MAXIMUM_POINT); } private void showResult() { @@ -126,7 +126,7 @@ private void showRank() { ChallengerResultDto challengerResultDto = makeChallengerResultDto(resultMap, blackJackGame.getChallengers()); DealerResultDto dealerResultDto = makeDealerResultDto(resultMap, blackJackGame.getDealer()); - OutputView.printFinalRank(challengerResultDto, dealerResultDto); + OutputView.printEndRank(challengerResultDto, dealerResultDto); } private PlayerStatusDto makePlayerStatusDto(Player player) { diff --git a/src/main/java/blackjack/domain/BlackJackGame.java b/src/main/java/blackjack/domain/BlackJackGame.java index a02dc3c298..2ed1050461 100644 --- a/src/main/java/blackjack/domain/BlackJackGame.java +++ b/src/main/java/blackjack/domain/BlackJackGame.java @@ -27,11 +27,7 @@ public void handOutStartCards() { players.pickStartCards(cardDeck); } - public boolean canPick(Player player) { - return player.canPick(); - } - - public void pick(Player player) { + public void hit(Player player) { Card pickedCard = cardDeck.pick(); player.pickCard(pickedCard); } diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java index 76c9107c99..c2499195bc 100644 --- a/src/main/java/blackjack/domain/player/Dealer.java +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -6,7 +6,7 @@ public class Dealer extends Player { private static final String NAME = "딜러"; - private static final int MAXIMUM_POINT = 16; + public static final int MAXIMUM_POINT = 16; public ResultType judge(Player challenger) { if (isBust() && challenger.isBust()) { diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 609ba47167..8717fa5dd7 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -10,8 +10,8 @@ public class OutputView { private static final String GIVE_START_CARD_COMPLETE_MESSAGE = "에게 2장을 나누었습니다."; - private static final String DEALER_CAN_PICK_MESSAGE = "딜러는 16이하라 한장의 카드를 더 받았습니다."; - private static final String DEALER_CAN_NOT_PICK_MESSAGE = "딜러는 17이상이라 한장의 카드를 더 받지 못했습니다."; + private static final String DEALER_CAN_PICK_MESSAGE = "딜러는 %d이하라 한장의 카드를 더 받았습니다."; + private static final String DEALER_CAN_NOT_PICK_MESSAGE = "딜러는 %d이상이라 한장의 카드를 더 받지 못했습니다."; private static final String FINAL_RESULT_HEADER_MESSAGE = "## 최종 승패"; private static final String CARD = "카드"; private static final String ITEM_DELIMITER = ", "; @@ -68,14 +68,14 @@ private static List toChallengerNames(List challengersS .collect(Collectors.toUnmodifiableList()); } - public static void printDealerResult(boolean dealerCanPick) { + public static void printDealerTurnResult(boolean dealerCanPick, int dealerMaximumPoint) { System.out.println(); if (dealerCanPick) { - System.out.println(DEALER_CAN_PICK_MESSAGE); + System.out.println(String.format(DEALER_CAN_PICK_MESSAGE, dealerMaximumPoint)); System.out.println(); return; } - System.out.println(DEALER_CAN_NOT_PICK_MESSAGE); + System.out.println(String.format(DEALER_CAN_NOT_PICK_MESSAGE, dealerMaximumPoint + 1)); System.out.println(); } @@ -92,7 +92,7 @@ private static void printPoint(int point) { System.out.println(RESULT_PREFIX + point); } - public static void printFinalRank(ChallengerResultDto challengerResultDto, DealerResultDto dealerResultDto) { + public static void printEndRank(ChallengerResultDto challengerResultDto, DealerResultDto dealerResultDto) { System.out.println(); System.out.println(FINAL_RESULT_HEADER_MESSAGE); printDealerFinalRank(dealerResultDto); From 0f74a75b2372a2f601d9f7f41fac0e5b076142f1 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Tue, 7 Mar 2023 18:28:38 +0900 Subject: [PATCH 41/43] =?UTF-8?q?refactor:=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=EC=97=90=20=EC=9D=98=EC=A1=B4=ED=95=98=EB=8A=94=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EC=97=B4=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/player/Challenger.java | 4 +--- src/main/java/blackjack/domain/player/Dealer.java | 2 +- src/main/java/blackjack/domain/player/Players.java | 2 +- .../domain/player/exception/InvalidPlayerNameException.java | 3 ++- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/blackjack/domain/player/Challenger.java b/src/main/java/blackjack/domain/player/Challenger.java index 167e446ffc..e7065376f2 100644 --- a/src/main/java/blackjack/domain/player/Challenger.java +++ b/src/main/java/blackjack/domain/player/Challenger.java @@ -4,8 +4,6 @@ public class Challenger extends Player { - private static final String INVALID_NAME = "딜러"; - private final String name; public Challenger(String name) { @@ -14,7 +12,7 @@ public Challenger(String name) { } private void validateName(String name) { - if (name.equals(INVALID_NAME)) { + if (Dealer.NAME.equals(name)) { throw new InvalidPlayerNameException(); } } diff --git a/src/main/java/blackjack/domain/player/Dealer.java b/src/main/java/blackjack/domain/player/Dealer.java index c2499195bc..9c694c6d73 100644 --- a/src/main/java/blackjack/domain/player/Dealer.java +++ b/src/main/java/blackjack/domain/player/Dealer.java @@ -4,7 +4,7 @@ public class Dealer extends Player { - private static final String NAME = "딜러"; + public static final String NAME = "딜러"; public static final int MAXIMUM_POINT = 16; diff --git a/src/main/java/blackjack/domain/player/Players.java b/src/main/java/blackjack/domain/player/Players.java index cbc7e0ccb6..7700648716 100644 --- a/src/main/java/blackjack/domain/player/Players.java +++ b/src/main/java/blackjack/domain/player/Players.java @@ -53,6 +53,6 @@ public Player getDealer() { return players.stream() .filter(player -> player.isDealer()) .findFirst() - .orElseThrow(() -> new IllegalArgumentException("딜러가 존재하지 않습니다.")); + .orElseThrow(() -> new IllegalStateException("딜러가 존재하지 않습니다.")); } } diff --git a/src/main/java/blackjack/domain/player/exception/InvalidPlayerNameException.java b/src/main/java/blackjack/domain/player/exception/InvalidPlayerNameException.java index ebdf173b1c..926d7ea34f 100644 --- a/src/main/java/blackjack/domain/player/exception/InvalidPlayerNameException.java +++ b/src/main/java/blackjack/domain/player/exception/InvalidPlayerNameException.java @@ -1,10 +1,11 @@ package blackjack.domain.player.exception; import blackjack.common.exception.CustomException; +import blackjack.domain.player.Dealer; public class InvalidPlayerNameException extends CustomException { - private static final String MESSAGE = "플레이어의 이름은 '딜러'이면 안됩니다."; + private static final String MESSAGE = "플레이어의 이름은 " + Dealer.NAME + "이면 안됩니다."; public InvalidPlayerNameException() { super(MESSAGE); From 7bcf6031a64563e4d03e87dd82e9b3a48cffc4d3 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Wed, 8 Mar 2023 16:40:29 +0900 Subject: [PATCH 42/43] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/player/ChallengerTest.java | 2 +- .../blackjack/domain/player/DealerTest.java | 38 +++++++++- .../domain/player/HoldingCardsTest.java | 70 +++++++++---------- .../blackjack/domain/player/PlayerTest.java | 41 ++++++++++- 4 files changed, 111 insertions(+), 40 deletions(-) diff --git a/src/test/java/blackjack/domain/player/ChallengerTest.java b/src/test/java/blackjack/domain/player/ChallengerTest.java index 300cd88fdd..c4a58c2b06 100644 --- a/src/test/java/blackjack/domain/player/ChallengerTest.java +++ b/src/test/java/blackjack/domain/player/ChallengerTest.java @@ -26,7 +26,7 @@ void validate_name() { @ParameterizedTest @MethodSource("provideCards") - @DisplayName("가진 카드의 합이 21 초과인지 확인한다") + @DisplayName("카드를 뽑을 수 있는지 확인한다.") void checking_sum_is_over_21(List cards, boolean expected) { Player player = new Challenger("neo"); for (Card card : cards) { diff --git a/src/test/java/blackjack/domain/player/DealerTest.java b/src/test/java/blackjack/domain/player/DealerTest.java index 9757497db7..484766d274 100644 --- a/src/test/java/blackjack/domain/player/DealerTest.java +++ b/src/test/java/blackjack/domain/player/DealerTest.java @@ -5,6 +5,7 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Shape; import blackjack.domain.card.Symbol; +import blackjack.domain.result.ResultType; import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.api.DisplayName; @@ -13,10 +14,43 @@ import org.junit.jupiter.params.provider.MethodSource; class DealerTest { + @ParameterizedTest + @MethodSource("challengerCards") + @DisplayName("다른 Challenger의 게임 결과를 올바르게 판단하는지 확인한다") + void judge(List cards, ResultType expected) { + Challenger challenger = new Challenger("oing"); + challenger.pickStartCards(cards.get(0), cards.get(1)); + + Dealer dealer = new Dealer(); + dealer.pickStartCards(new Card(Shape.HEART, Symbol.KING), new Card(Shape.SPADE, Symbol.QUEEN)); + + assertThat(dealer.judge(challenger)).isEqualTo(expected); + } + + private static Stream challengerCards() { + return Stream.of( + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Symbol.QUEEN), + new Card(Shape.CLOVER, Symbol.SIX)), + ResultType.LOSE), + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Symbol.ACE), + new Card(Shape.CLOVER, Symbol.KING)), + ResultType.WIN), + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Symbol.JACK), + new Card(Shape.CLOVER, Symbol.KING)), + ResultType.DRAW) + ); + } + @ParameterizedTest @MethodSource("provideCards") - @DisplayName("가진 카드의 합이 16 초과인지 확인한다") + @DisplayName("카드를 뽑을 수 있는지 확인한다") void checking_sum_is_over_16(List cards, boolean expected) { Player player = new Dealer(); for (Card card : cards) { @@ -31,7 +65,7 @@ private static Stream provideCards() { Arguments.of( List.of( new Card(Shape.DIAMOND, Symbol.QUEEN), - new Card(Shape.CLOVER, Symbol.FOUR)), + new Card(Shape.CLOVER, Symbol.SIX)), true), Arguments.of( List.of( diff --git a/src/test/java/blackjack/domain/player/HoldingCardsTest.java b/src/test/java/blackjack/domain/player/HoldingCardsTest.java index f447c5e4d7..3e693d400b 100644 --- a/src/test/java/blackjack/domain/player/HoldingCardsTest.java +++ b/src/test/java/blackjack/domain/player/HoldingCardsTest.java @@ -5,9 +5,13 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Shape; import blackjack.domain.card.Symbol; +import java.util.List; +import java.util.stream.Stream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class HoldingCardsTest { @@ -18,42 +22,36 @@ void setup() { holdingCards = new HoldingCards(); } - @Test - @DisplayName("가진 카드의 합 테스트") - void sum_of_cards() { - Card DiamondKing = new Card(Shape.DIAMOND, Symbol.KING); - Card SpadeFour = new Card(Shape.SPADE, Symbol.FOUR); - - holdingCards.initialize(DiamondKing, SpadeFour); - - assertThat(holdingCards.getSum()).isEqualTo(14); - } - - @Test - @DisplayName("합이 큰 경우 테스트") - void big_sum() { - Card diamondKing = new Card(Shape.DIAMOND, Symbol.KING); - Card heartKIng = new Card(Shape.HEART, Symbol.KING); - Card SpadeFour = new Card(Shape.SPADE, Symbol.FOUR); - - holdingCards.initialize(diamondKing, heartKIng); - holdingCards.add(SpadeFour); - - assertThat(holdingCards.getSum()).isEqualTo(24); + @ParameterizedTest + @MethodSource("provideCards") + @DisplayName("카드의 합") + void sum(List cards, int expected) { + for (Card card : cards) { + holdingCards.add(card); + } + assertThat(holdingCards.getSum()).isEqualTo(expected); } - @Test - @DisplayName("ACE가 존재하는 경우 21에 가장 가까운 합을 찾는지 확인하는 테스트") - void find_sum_of_nearly_21() { - Card cloverAce = new Card(Shape.CLOVER, Symbol.ACE); - Card cloverFive = new Card(Shape.CLOVER, Symbol.FIVE); - Card spadeAce = new Card(Shape.SPADE, Symbol.ACE); - Card heartAce = new Card(Shape.HEART, Symbol.ACE); - - holdingCards.initialize(cloverAce, cloverFive); - holdingCards.add(spadeAce); - holdingCards.add(heartAce); - - assertThat(holdingCards.getSum()).isEqualTo(18); + private static Stream provideCards() { + return Stream.of( + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Symbol.QUEEN), + new Card(Shape.CLOVER, Symbol.FIVE)), + 15), + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Symbol.QUEEN), + new Card(Shape.CLOVER, Symbol.FIVE), + new Card(Shape.HEART, Symbol.EIGHT)), + 23), + Arguments.of( + List.of( + new Card(Shape.CLOVER, Symbol.ACE), + new Card(Shape.SPADE, Symbol.ACE), + new Card(Shape.HEART, Symbol.ACE), + new Card(Shape.CLOVER, Symbol.FIVE)), + 18) + ); } } diff --git a/src/test/java/blackjack/domain/player/PlayerTest.java b/src/test/java/blackjack/domain/player/PlayerTest.java index 684c6fab13..f1bf7c195e 100644 --- a/src/test/java/blackjack/domain/player/PlayerTest.java +++ b/src/test/java/blackjack/domain/player/PlayerTest.java @@ -5,9 +5,14 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Shape; import blackjack.domain.card.Symbol; +import java.util.List; +import java.util.stream.Stream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class PlayerTest { @@ -36,7 +41,7 @@ public boolean isDealer() { @Test @DisplayName("플레이어는 초기 카드 2장을 받는다") - void get_two_cards() { + void start_with_two_cards() { Card card1 = new Card(Shape.HEART, Symbol.FOUR); Card card2 = new Card(Shape.CLOVER, Symbol.KING); player.pickStartCards(card1, card2); @@ -54,4 +59,38 @@ void pick_card() { assertThat(player.getHoldingCards().getCards()) .contains(card); } + + @ParameterizedTest + @MethodSource("provideCards") + @DisplayName("버스트인지 확인") + void is_bust(List cards, boolean expected) { + for (Card card : cards) { + player.pickCard(card); + } + + assertThat(player.isBust()).isEqualTo(expected); + } + + private static Stream provideCards() { + return Stream.of( + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Symbol.QUEEN), + new Card(Shape.CLOVER, Symbol.FIVE)), + false), + Arguments.of( + List.of( + new Card(Shape.DIAMOND, Symbol.QUEEN), + new Card(Shape.CLOVER, Symbol.FIVE), + new Card(Shape.HEART, Symbol.EIGHT)), + true), + Arguments.of( + List.of( + new Card(Shape.CLOVER, Symbol.ACE), + new Card(Shape.SPADE, Symbol.ACE), + new Card(Shape.HEART, Symbol.ACE), + new Card(Shape.CLOVER, Symbol.FIVE)), + false) + ); + } } From 6800d7cdc43845c635f7b3748799e2dd09d0d0e5 Mon Sep 17 00:00:00 2001 From: hanueleee Date: Wed, 8 Mar 2023 17:01:28 +0900 Subject: [PATCH 43/43] =?UTF-8?q?refactor:=20CardDeck=EC=9D=84=20List?= =?UTF-8?q?=EC=97=90=EC=84=9C=20Deque=EC=9C=BC=EB=A1=9C=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 --- .../java/blackjack/domain/BlackJackGame.java | 1 - .../java/blackjack/domain/card/CardDeck.java | 17 ++++++++--------- .../blackjack/domain/card/CardDeckTest.java | 15 --------------- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/main/java/blackjack/domain/BlackJackGame.java b/src/main/java/blackjack/domain/BlackJackGame.java index 2ed1050461..7ea4fa8759 100644 --- a/src/main/java/blackjack/domain/BlackJackGame.java +++ b/src/main/java/blackjack/domain/BlackJackGame.java @@ -23,7 +23,6 @@ public static BlackJackGame from(List names) { } public void handOutStartCards() { - cardDeck.shuffle(); players.pickStartCards(cardDeck); } diff --git a/src/main/java/blackjack/domain/card/CardDeck.java b/src/main/java/blackjack/domain/card/CardDeck.java index 565c0df7b7..bfb6b9a987 100644 --- a/src/main/java/blackjack/domain/card/CardDeck.java +++ b/src/main/java/blackjack/domain/card/CardDeck.java @@ -1,34 +1,33 @@ package blackjack.domain.card; -import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; import blackjack.domain.card.exception.NoMoreCardException; +import java.util.ArrayDeque; import java.util.Arrays; import java.util.Collections; +import java.util.Deque; import java.util.List; public class CardDeck { - private final List cards; + private final Deque cards; - private CardDeck(List cards) { + private CardDeck(Deque cards) { this.cards = cards; } public static CardDeck create() { - return Arrays.stream(Shape.values()) + List cards = Arrays.stream(Shape.values()) .flatMap(shape -> Arrays.stream(Symbol.values()).map(symbol -> new Card(shape, symbol))) - .collect(collectingAndThen(toList(), CardDeck::new)); - } - - public void shuffle() { + .collect(toList()); Collections.shuffle(cards); + return new CardDeck(new ArrayDeque<>(cards)); } public Card pick() { validateCardExist(); - return cards.remove(0); + return cards.remove(); } private void validateCardExist() { diff --git a/src/test/java/blackjack/domain/card/CardDeckTest.java b/src/test/java/blackjack/domain/card/CardDeckTest.java index 3d53565727..313f14da34 100644 --- a/src/test/java/blackjack/domain/card/CardDeckTest.java +++ b/src/test/java/blackjack/domain/card/CardDeckTest.java @@ -1,6 +1,5 @@ package blackjack.domain.card; -import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import blackjack.domain.card.exception.NoMoreCardException; @@ -18,18 +17,4 @@ void pick_random_card() { } assertThrows(NoMoreCardException.class, () -> cardDeck.pick()); } - - @Test - @DisplayName("카드 뽑기 테스트") - void pick_test() { - CardDeck cardDeck = CardDeck.create(); - Card spadeAce = cardDeck.pick(); - Card spadeTwo = cardDeck.pick(); - - assertThat(spadeAce.getShape()).isEqualTo(Shape.SPADE); - assertThat(spadeAce.getSymbol()).isEqualTo(Symbol.ACE); - - assertThat(spadeTwo.getShape()).isEqualTo(Shape.SPADE); - assertThat(spadeTwo.getSymbol()).isEqualTo(Symbol.TWO); - } }