From 93adb74721ad74d28cdf94fc7f3ca17a382a3490 Mon Sep 17 00:00:00 2001 From: kokodak Date: Tue, 28 Feb 2023 17:07:01 +0900 Subject: [PATCH 01/80] =?UTF-8?q?docs:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EB=AA=85=EC=84=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/README.md b/README.md index 556099c4de..926969b849 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,85 @@ ## 우아한테크코스 코드리뷰 - [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md) + +## 블랙잭 개요 + +- 블랙잭 게임은 딜러와 플레이어가 진행한다. +- 카드의 합이 21에 가장 가까운 숫자를 가지는 쪽이 이기는 게임이다. +- 숫자 계산은 카드의 숫자를 기본으로 하되, + - Ace: 1 or 11 + - King, Queen, Jack: 10 +- 게임을 시작하면, 딜러와 플레이어는 두 장의 카드를 지급받는다. +- 플레이어는 카드 숫자의 합이 21 미만이라면 카드를 계속 뽑을 수 있다. +- 딜러는 카드 숫자의 합이 16 이하인 경우에만 1장의 카드를 추가로 받아야 한다. + - 딜러는 최대 3장의 카드만 받을 수 있다. +- 게임이 완료되면, 각 플레이어의 승패를 출력한다. + +## 도메인 다이어그램 + +```mermaid +graph TD + BlackJackController --> InputView + BlackJackController --> OutputView + BlackJackController --> BlackJackGame + BlackJackController --> DeckFactory + DeckFactory --> Deck + BlackJackGame --> Players + BlackJackGame --> Deck + BlackJackGame --> Result + Deck --> Cards + Cards --> Card + Card --> Symbol + Card --> Rank + Players --> Player + Player --> Name +``` + +## 기능 구현 목록 + +### 플레이어 + +- [ ] 이름을 가진다. + - [ ] 최소 1자, 최대 10자까지 가능하다. + - [ ] 앞, 뒤 공백은 제거한다. + - [ ] 중간 공백은 허용한다. + - [ ] 중복되는 이름은 가질 수 없다. + - [ ] `딜러`라는 이름은 가질 수 없다. +- [ ] 여러 명일 수 있다. + - [ ] 최대 5명이다. + +### 카드 + +- [ ] 문양을 가진다. +- [ ] 숫자를 가진다. + +### 덱 + +- [ ] 카드 목록을 가진다. +- [ ] 카드를 섞는다. +- [ ] 카드를 뽑는다. + - [ ] 카드가 없으면 뽑을 수 없다. + +### 블랙잭 게임 + +- [ ] 플레이어가 카드를 더 뽑을 수 있는지 판단한다. +- [ ] 플레이어의 카드를 추가한다. +- [ ] 점수를 계산한다. + +### 결과 + +- [ ] 딜러와 플레이어의 점수를 확인한다. +- [ ] 딜러가 몇 승 몇 패인지 확인한다. +- [ ] 플레이어의 승패 여부를 확인한다. + +### 입력 + +- [ ] 플레이어의 이름을 입력한다. +- [ ] 카드를 받을 여부를 입력한다. + +### 출력 + +- [ ] 딜러와 플레이어의 카드 현황을 출력한다. +- [ ] 딜러 카드 추가 여부를 출력한다. +- [ ] 최종 결과를 출력한다. +- [ ] 최종 승패를 출력한다. From 4d669bd73788d3473710fe9d5a7abc0045e225b9 Mon Sep 17 00:00:00 2001 From: kokodak Date: Tue, 28 Feb 2023 17:39:55 +0900 Subject: [PATCH 02/80] =?UTF-8?q?feat:=20=EC=9D=B4=EB=A6=84=EC=9D=98=20?= =?UTF-8?q?=EA=B8=B8=EC=9D=B4=20=EA=B2=80=EC=A6=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 --- README.md | 2 +- src/test/java/blackjack/domain/NameTest.java | 50 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/java/blackjack/domain/NameTest.java diff --git a/README.md b/README.md index 926969b849..76cfe106b0 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ graph TD ### 플레이어 - [ ] 이름을 가진다. - - [ ] 최소 1자, 최대 10자까지 가능하다. + - [x] 최소 1자, 최대 10자까지 가능하다. - [ ] 앞, 뒤 공백은 제거한다. - [ ] 중간 공백은 허용한다. - [ ] 중복되는 이름은 가질 수 없다. diff --git a/src/test/java/blackjack/domain/NameTest.java b/src/test/java/blackjack/domain/NameTest.java new file mode 100644 index 0000000000..f7e79e670b --- /dev/null +++ b/src/test/java/blackjack/domain/NameTest.java @@ -0,0 +1,50 @@ +package blackjack.domain; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class NameTest { + + @Test + void 이름이_존재하지_않으면_예외를_던진다() { + final String input = " "; + + assertThatThrownBy(() -> new Name(input)) + .isInstanceOf(IllegalArgumentException.class); + } + + @ParameterizedTest + @ValueSource(strings = {"", "abcdeabcdea"}) + void 이름은_1자_이상_10자_이하가_아니라면_예외를_던진다(final String input) { + assertThatThrownBy(() -> new Name(input)) + .isInstanceOf(IllegalArgumentException.class); + } +} + +class Name { + + private static final int UPPER_BOUND = 10; + + private final String value; + + public Name(final String value) { + validate(value); + this.value = value; + } + + private void validate(final String value) { + if (value.isBlank()) { + throw new IllegalArgumentException("이름은 존재해야 합니다. 현재 이름: " + value); + } + if (value.length() > UPPER_BOUND) { + throw new IllegalArgumentException("이름은 " + UPPER_BOUND + "글자 이하여야 합니다. 현재 이름: " + value); + } + } +} From ab04caa503178de7baf4e245da686d2f9130527e Mon Sep 17 00:00:00 2001 From: kokodak Date: Tue, 28 Feb 2023 17:50:30 +0900 Subject: [PATCH 03/80] =?UTF-8?q?feat:=20=EA=B8=88=EC=A7=80=EB=90=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EA=B2=80=EC=A6=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 --- README.md | 2 +- src/test/java/blackjack/domain/NameTest.java | 24 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 76cfe106b0..54b41305fa 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ graph TD - [ ] 앞, 뒤 공백은 제거한다. - [ ] 중간 공백은 허용한다. - [ ] 중복되는 이름은 가질 수 없다. - - [ ] `딜러`라는 이름은 가질 수 없다. + - [x] `딜러`라는 이름은 가질 수 없다. - [ ] 여러 명일 수 있다. - [ ] 최대 5명이다. diff --git a/src/test/java/blackjack/domain/NameTest.java b/src/test/java/blackjack/domain/NameTest.java index f7e79e670b..571a74fb9e 100644 --- a/src/test/java/blackjack/domain/NameTest.java +++ b/src/test/java/blackjack/domain/NameTest.java @@ -26,11 +26,20 @@ class NameTest { assertThatThrownBy(() -> new Name(input)) .isInstanceOf(IllegalArgumentException.class); } + + @Test + void 이름이_딜러_라면_예외를_던진다() { + final String input = "딜러"; + + assertThatThrownBy(() -> new Name(input)) + .isInstanceOf(IllegalArgumentException.class); + } } class Name { private static final int UPPER_BOUND = 10; + private static final String RESTRICT = "딜러"; private final String value; @@ -40,11 +49,26 @@ public Name(final String value) { } private void validate(final String value) { + validateBlank(value); + validateLength(value); + validateRestrictWord(value); + } + + private void validateBlank(final String value) { if (value.isBlank()) { throw new IllegalArgumentException("이름은 존재해야 합니다. 현재 이름: " + value); } + } + + private void validateLength(final String value) { if (value.length() > UPPER_BOUND) { throw new IllegalArgumentException("이름은 " + UPPER_BOUND + "글자 이하여야 합니다. 현재 이름: " + value); } } + + private void validateRestrictWord(final String value) { + if (value.equals(RESTRICT)) { + throw new IllegalArgumentException("이름은 " + RESTRICT + "일 수 없습니다. 현재 이름: " + value); + } + } } From 76e478d38444704e56cfb57e9a5ee75009792441 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 13:26:48 +0900 Subject: [PATCH 04/80] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- README.md | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 54b41305fa..c56b66f233 100644 --- a/README.md +++ b/README.md @@ -25,27 +25,31 @@ graph TD BlackJackController --> InputView BlackJackController --> OutputView - BlackJackController --> BlackJackGame - BlackJackController --> DeckFactory + BlackJackController --> Participants + BlackJackController --> Deck + Participants --> Participant + Participant --> Player + Participant --> Dealer + Participant --> Cards + Player --> Name DeckFactory --> Deck - BlackJackGame --> Players - BlackJackGame --> Deck - BlackJackGame --> Result - Deck --> Cards Cards --> Card - Card --> Symbol - Card --> Rank - Players --> Player - Player --> Name + Card --> Suit + Card --> Number ``` ## 기능 구현 목록 +### 참가자 + +- [ ] 카드를 받는다. +- [ ] 점수를 확인한다. +- [ ] 카드 추가 여부를 결정한다. + ### 플레이어 - [ ] 이름을 가진다. - [x] 최소 1자, 최대 10자까지 가능하다. - - [ ] 앞, 뒤 공백은 제거한다. - [ ] 중간 공백은 허용한다. - [ ] 중복되는 이름은 가질 수 없다. - [x] `딜러`라는 이름은 가질 수 없다. @@ -56,6 +60,7 @@ graph TD - [ ] 문양을 가진다. - [ ] 숫자를 가진다. +- [ ] 점수를 계산한다. ### 덱 @@ -64,21 +69,10 @@ graph TD - [ ] 카드를 뽑는다. - [ ] 카드가 없으면 뽑을 수 없다. -### 블랙잭 게임 - -- [ ] 플레이어가 카드를 더 뽑을 수 있는지 판단한다. -- [ ] 플레이어의 카드를 추가한다. -- [ ] 점수를 계산한다. - -### 결과 - -- [ ] 딜러와 플레이어의 점수를 확인한다. -- [ ] 딜러가 몇 승 몇 패인지 확인한다. -- [ ] 플레이어의 승패 여부를 확인한다. - ### 입력 - [ ] 플레이어의 이름을 입력한다. + - [ ] 앞, 뒤 공백은 제거한다. - [ ] 카드를 받을 여부를 입력한다. ### 출력 From c5575b7e21c3b5f6f032fa9092adc606e07dcd6f Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 13:33:49 +0900 Subject: [PATCH 05/80] =?UTF-8?q?feat:=20=EC=9D=B4=EB=A6=84=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- README.md | 10 +++--- src/main/java/blackjack/domain/Name.java | 38 ++++++++++++++++++++ src/test/java/blackjack/domain/NameTest.java | 37 ------------------- 3 files changed, 43 insertions(+), 42 deletions(-) create mode 100644 src/main/java/blackjack/domain/Name.java diff --git a/README.md b/README.md index c56b66f233..c177231c61 100644 --- a/README.md +++ b/README.md @@ -45,16 +45,16 @@ graph TD - [ ] 카드를 받는다. - [ ] 점수를 확인한다. - [ ] 카드 추가 여부를 결정한다. +- [ ] 여러 명일 수 있다. + - [ ] 중복되는 이름은 가질 수 없다. + - [ ] 최대 5명이다. ### 플레이어 -- [ ] 이름을 가진다. +- [x] 이름을 가진다. - [x] 최소 1자, 최대 10자까지 가능하다. - - [ ] 중간 공백은 허용한다. - - [ ] 중복되는 이름은 가질 수 없다. + - [x] 중간 공백은 허용한다. - [x] `딜러`라는 이름은 가질 수 없다. -- [ ] 여러 명일 수 있다. - - [ ] 최대 5명이다. ### 카드 diff --git a/src/main/java/blackjack/domain/Name.java b/src/main/java/blackjack/domain/Name.java new file mode 100644 index 0000000000..2db96dd6f9 --- /dev/null +++ b/src/main/java/blackjack/domain/Name.java @@ -0,0 +1,38 @@ +package blackjack.domain; + +public class Name { + + private static final int UPPER_BOUND = 10; + private static final String RESTRICT = "딜러"; + + private final String value; + + public Name(final String value) { + validate(value); + this.value = value; + } + + private void validate(final String value) { + validateBlank(value); + validateLength(value); + validateRestrictWord(value); + } + + private void validateBlank(final String value) { + if (value.isBlank()) { + throw new IllegalArgumentException("이름은 존재해야 합니다. 현재 이름: " + value); + } + } + + private void validateLength(final String value) { + if (value.length() > UPPER_BOUND) { + throw new IllegalArgumentException("이름은 " + UPPER_BOUND + "글자 이하여야 합니다. 현재 이름: " + value); + } + } + + private void validateRestrictWord(final String value) { + if (value.equals(RESTRICT)) { + throw new IllegalArgumentException("이름은 " + RESTRICT + "일 수 없습니다. 현재 이름: " + value); + } + } +} diff --git a/src/test/java/blackjack/domain/NameTest.java b/src/test/java/blackjack/domain/NameTest.java index 571a74fb9e..d102b67d82 100644 --- a/src/test/java/blackjack/domain/NameTest.java +++ b/src/test/java/blackjack/domain/NameTest.java @@ -35,40 +35,3 @@ class NameTest { .isInstanceOf(IllegalArgumentException.class); } } - -class Name { - - private static final int UPPER_BOUND = 10; - private static final String RESTRICT = "딜러"; - - private final String value; - - public Name(final String value) { - validate(value); - this.value = value; - } - - private void validate(final String value) { - validateBlank(value); - validateLength(value); - validateRestrictWord(value); - } - - private void validateBlank(final String value) { - if (value.isBlank()) { - throw new IllegalArgumentException("이름은 존재해야 합니다. 현재 이름: " + value); - } - } - - private void validateLength(final String value) { - if (value.length() > UPPER_BOUND) { - throw new IllegalArgumentException("이름은 " + UPPER_BOUND + "글자 이하여야 합니다. 현재 이름: " + value); - } - } - - private void validateRestrictWord(final String value) { - if (value.equals(RESTRICT)) { - throw new IllegalArgumentException("이름은 " + RESTRICT + "일 수 없습니다. 현재 이름: " + value); - } - } -} From aeb41cc56621834e18fe35b32b609aa18a2074fa Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 13:43:12 +0900 Subject: [PATCH 06/80] =?UTF-8?q?feat:=20=EB=AC=B8=EC=96=91=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/domain/Suit.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/domain/Suit.java diff --git a/README.md b/README.md index c177231c61..2a07a8fc91 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ graph TD ### 카드 -- [ ] 문양을 가진다. +- [x] 문양을 가진다. - [ ] 숫자를 가진다. - [ ] 점수를 계산한다. diff --git a/src/main/java/blackjack/domain/Suit.java b/src/main/java/blackjack/domain/Suit.java new file mode 100644 index 0000000000..df2117ca4d --- /dev/null +++ b/src/main/java/blackjack/domain/Suit.java @@ -0,0 +1,19 @@ +package blackjack.domain; + +public enum Suit { + + SPADE("스페이드"), + DIAMOND("다이아몬드"), + HEART("하트"), + CLOVER("클로버"); + + private final String name; + + Suit(final String name) { + this.name = name; + } + + public String getName() { + return name; + } +} From 660568bc7e4b41bd5e5baa5da974284bfb1bdaab Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 13:54:10 +0900 Subject: [PATCH 07/80] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/domain/Number.java | 34 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/domain/Number.java diff --git a/README.md b/README.md index 2a07a8fc91..b965422863 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ graph TD ### 카드 - [x] 문양을 가진다. -- [ ] 숫자를 가진다. +- [x] 숫자를 가진다. - [ ] 점수를 계산한다. ### 덱 diff --git a/src/main/java/blackjack/domain/Number.java b/src/main/java/blackjack/domain/Number.java new file mode 100644 index 0000000000..beee6abcbd --- /dev/null +++ b/src/main/java/blackjack/domain/Number.java @@ -0,0 +1,34 @@ +package blackjack.domain; + +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 name; + private final int score; + + Number(final String name, final int score) { + this.name = name; + this.score = score; + } + + public String getName() { + return name; + } + + public int getScore() { + return score; + } +} From 55298124ab4d4d1bf90aefb09721804e185368cc Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 15:22:54 +0900 Subject: [PATCH 08/80] =?UTF-8?q?feat:=20=EB=AC=B8=EC=96=91=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=ED=99=95=EC=9D=B8=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 Co-authored-by: woo-chang --- src/test/java/blackjack/domain/CardTest.java | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/java/blackjack/domain/CardTest.java diff --git a/src/test/java/blackjack/domain/CardTest.java b/src/test/java/blackjack/domain/CardTest.java new file mode 100644 index 0000000000..c9eb1c5e02 --- /dev/null +++ b/src/test/java/blackjack/domain/CardTest.java @@ -0,0 +1,33 @@ +package blackjack.domain; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class CardTest { + + @Test + void 문양_이름을_확인한다() { + final Suit suit = Suit.SPADE; + + final Card card = new Card(suit); + + assertThat(card.getSuitName()).isEqualTo(suit.getName()); + } +} + +class Card { + + private final Suit suit; + + public Card(final Suit suit) { + this.suit = suit; + } + + public String getSuitName() { + return suit.getName(); + } +} \ No newline at end of file From f42b755315e0d97c85fcd9f62568028d3230b63d Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 15:27:54 +0900 Subject: [PATCH 09/80] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=ED=99=95=EC=9D=B8=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 Co-authored-by: woo-chang --- src/test/java/blackjack/domain/CardTest.java | 23 +++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/test/java/blackjack/domain/CardTest.java b/src/test/java/blackjack/domain/CardTest.java index c9eb1c5e02..0d842f1026 100644 --- a/src/test/java/blackjack/domain/CardTest.java +++ b/src/test/java/blackjack/domain/CardTest.java @@ -1,6 +1,6 @@ package blackjack.domain; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -12,21 +12,38 @@ class CardTest { @Test void 문양_이름을_확인한다() { final Suit suit = Suit.SPADE; + final Number number = Number.ACE; - final Card card = new Card(suit); + final Card card = new Card(number, suit); assertThat(card.getSuitName()).isEqualTo(suit.getName()); } + + @Test + void 숫자_이름을_확인한다() { + final Suit suit = Suit.SPADE; + final Number number = Number.ACE; + + final Card card = new Card(number, suit); + + assertThat(card.getNumberName()).isEqualTo(number.getName()); + } } class Card { + private final Number number; private final Suit suit; - public Card(final Suit suit) { + public Card(final Number number, final Suit suit) { + this.number = number; this.suit = suit; } + public String getNumberName() { + return number.getName(); + } + public String getSuitName() { return suit.getName(); } From 59212a2aca6b2e7a7201a0809a7786d7e8e8fd23 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 15:32:02 +0900 Subject: [PATCH 10/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=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 Co-authored-by: woo-chang --- src/main/java/blackjack/domain/Card.java | 24 ++++++++++++++++++++ src/test/java/blackjack/domain/CardTest.java | 23 ++++++------------- 2 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 src/main/java/blackjack/domain/Card.java diff --git a/src/main/java/blackjack/domain/Card.java b/src/main/java/blackjack/domain/Card.java new file mode 100644 index 0000000000..47a0c6bb2d --- /dev/null +++ b/src/main/java/blackjack/domain/Card.java @@ -0,0 +1,24 @@ +package blackjack.domain; + +public class Card { + + private final Number number; + private final Suit suit; + + public Card(final Number number, final Suit suit) { + this.number = number; + this.suit = suit; + } + + public int getScore() { + return number.getScore(); + } + + public String getNumberName() { + return number.getName(); + } + + public String getSuitName() { + return suit.getName(); + } +} diff --git a/src/test/java/blackjack/domain/CardTest.java b/src/test/java/blackjack/domain/CardTest.java index 0d842f1026..d2897f02e5 100644 --- a/src/test/java/blackjack/domain/CardTest.java +++ b/src/test/java/blackjack/domain/CardTest.java @@ -28,23 +28,14 @@ class CardTest { assertThat(card.getNumberName()).isEqualTo(number.getName()); } -} - -class Card { - - private final Number number; - private final Suit suit; - public Card(final Number number, final Suit suit) { - this.number = number; - this.suit = suit; - } + @Test + void 숫자_점수를_확인한다() { + final Suit suit = Suit.SPADE; + final Number number = Number.ACE; - public String getNumberName() { - return number.getName(); - } + final Card card = new Card(number, suit); - public String getSuitName() { - return suit.getName(); + assertThat(card.getScore()).isEqualTo(number.getScore()); } -} \ No newline at end of file +} From 365c18932d8aa55a6548ffed8f332b4193b50cfe Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 15:51:08 +0900 Subject: [PATCH 11/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=EA=B3=84?= =?UTF-8?q?=EC=82=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 Co-authored-by: woo-chang --- src/test/java/blackjack/domain/CardsTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/test/java/blackjack/domain/CardsTest.java diff --git a/src/test/java/blackjack/domain/CardsTest.java b/src/test/java/blackjack/domain/CardsTest.java new file mode 100644 index 0000000000..aeef0b59a6 --- /dev/null +++ b/src/test/java/blackjack/domain/CardsTest.java @@ -0,0 +1,46 @@ +package blackjack.domain; + +import static blackjack.domain.Number.JACK; +import static blackjack.domain.Number.TWO; +import static blackjack.domain.Suit.HEART; +import static blackjack.domain.Suit.SPADE; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class CardsTest { + + private static Stream generateCards() { + return Stream.of( + Arguments.of(List.of(new Card(TWO, SPADE), new Card(JACK, HEART)), 12), + Arguments.of(List.of(), 0) + ); + } + + @ParameterizedTest + @MethodSource("generateCards") + void 점수를_계산한다(final List cardPack, final int totalScore) { + final Cards cards = new Cards(cardPack); + + assertThat(cards.calculateScore()).isEqualTo(totalScore); + } +} + +class Cards { + + private final List cards; + + public Cards(final List cards) { + this.cards = cards; + } + + public int calculateScore() { + return cards.stream() + .mapToInt(Card::getScore) + .sum(); + } +} From 14cb9daf0b402232eb2a279020941330572f05d3 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 16:32:05 +0900 Subject: [PATCH 12/80] =?UTF-8?q?feat:=20=EC=97=90=EC=9D=B4=EC=8A=A4?= =?UTF-8?q?=EC=9D=B8=EC=A7=80=20=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- README.md | 1 + src/main/java/blackjack/domain/Card.java | 4 ++++ src/test/java/blackjack/domain/CardTest.java | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/README.md b/README.md index b965422863..9d6d28de8d 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ graph TD - [x] 문양을 가진다. - [x] 숫자를 가진다. - [ ] 점수를 계산한다. +- [x] 에이스인지 확인한다. ### 덱 diff --git a/src/main/java/blackjack/domain/Card.java b/src/main/java/blackjack/domain/Card.java index 47a0c6bb2d..cd52841300 100644 --- a/src/main/java/blackjack/domain/Card.java +++ b/src/main/java/blackjack/domain/Card.java @@ -10,6 +10,10 @@ public Card(final Number number, final Suit suit) { this.suit = suit; } + public boolean isAce() { + return number == Number.ACE; + } + public int getScore() { return number.getScore(); } diff --git a/src/test/java/blackjack/domain/CardTest.java b/src/test/java/blackjack/domain/CardTest.java index d2897f02e5..cbe5295cf6 100644 --- a/src/test/java/blackjack/domain/CardTest.java +++ b/src/test/java/blackjack/domain/CardTest.java @@ -38,4 +38,24 @@ class CardTest { assertThat(card.getScore()).isEqualTo(number.getScore()); } + + @Test + void 에이스라면_true_반환한다() { + final Suit suit = Suit.SPADE; + final Number number = Number.ACE; + + final Card card = new Card(number, suit); + + assertThat(card.isAce()).isTrue(); + } + + @Test + void 에이스가_아니라면_false_반환한다() { + final Suit suit = Suit.SPADE; + final Number number = Number.TEN; + + final Card card = new Card(number, suit); + + assertThat(card.isAce()).isFalse(); + } } From d668b8ceb9da9f7c86298a8d8a0bd1a168e53cf2 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 16:32:43 +0900 Subject: [PATCH 13/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=ED=95=A9?= =?UTF-8?q?=EA=B3=84=20=EA=B3=84=EC=82=B0=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 Co-authored-by: woo-chang --- README.md | 2 +- src/test/java/blackjack/domain/CardsTest.java | 51 +++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9d6d28de8d..917cb4fe75 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ graph TD - [x] 문양을 가진다. - [x] 숫자를 가진다. -- [ ] 점수를 계산한다. +- [x] 점수를 계산한다. - [x] 에이스인지 확인한다. ### 덱 diff --git a/src/test/java/blackjack/domain/CardsTest.java b/src/test/java/blackjack/domain/CardsTest.java index aeef0b59a6..620414116a 100644 --- a/src/test/java/blackjack/domain/CardsTest.java +++ b/src/test/java/blackjack/domain/CardsTest.java @@ -1,7 +1,11 @@ package blackjack.domain; +import static blackjack.domain.Number.ACE; import static blackjack.domain.Number.JACK; +import static blackjack.domain.Number.QUEEN; +import static blackjack.domain.Number.TEN; import static blackjack.domain.Number.TWO; +import static blackjack.domain.Suit.CLOVER; import static blackjack.domain.Suit.HEART; import static blackjack.domain.Suit.SPADE; import static org.assertj.core.api.Assertions.assertThat; @@ -14,33 +18,72 @@ class CardsTest { - private static Stream generateCards() { + private static Stream generateCardsWithoutACE() { return Stream.of( Arguments.of(List.of(new Card(TWO, SPADE), new Card(JACK, HEART)), 12), Arguments.of(List.of(), 0) ); } + private static Stream generateCardsWithACE() { + return Stream.of( + Arguments.of(List.of(new Card(ACE, SPADE), new Card(JACK, HEART)), 21), + Arguments.of(List.of(new Card(ACE, SPADE), new Card(ACE, HEART), new Card(TEN, CLOVER)), 12), + Arguments.of(List.of(new Card(ACE, SPADE), new Card(QUEEN, HEART), new Card(TEN, CLOVER)), 21), + Arguments.of(List.of(new Card(ACE, SPADE), new Card(ACE, HEART)), 12) + ); + } + @ParameterizedTest - @MethodSource("generateCards") + @MethodSource("generateCardsWithoutACE") void 점수를_계산한다(final List cardPack, final int totalScore) { final Cards cards = new Cards(cardPack); - assertThat(cards.calculateScore()).isEqualTo(totalScore); + assertThat(cards.calculateTotalScore()).isEqualTo(totalScore); + } + + @ParameterizedTest + @MethodSource("generateCardsWithACE") + void 에이스가_포함된_경우_최적의_점수를_계산한다(final List cardPack, final int totalScore) { + final Cards cards = new Cards(cardPack); + + assertThat(cards.calculateTotalScore()).isEqualTo(totalScore); } } class Cards { + private static final int MAXIMUM_SCORE = 21; + private static final int ACE_BONUS = 10; + private final List cards; public Cards(final List cards) { this.cards = cards; } - public int calculateScore() { + public int calculateTotalScore() { + final int score = getTotalScore(); + + if (isExistAce() && isScoreUpdatable(score)) { + return score + ACE_BONUS; + } + + return score; + } + + private int getTotalScore() { return cards.stream() .mapToInt(Card::getScore) .sum(); } + + private boolean isExistAce() { + return cards.stream() + .anyMatch(Card::isAce); + } + + private boolean isScoreUpdatable(final int score) { + return score + ACE_BONUS <= MAXIMUM_SCORE; + } } From 07e5c72823b26db7ae8b1740e35d78fe236ebcb2 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 16:44:39 +0900 Subject: [PATCH 14/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=EC=B5=9C?= =?UTF-8?q?=EB=8C=93=EA=B0=92=20=EC=B4=88=EA=B3=BC=20=EC=97=AC=EB=B6=80=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=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 Co-authored-by: woo-chang --- README.md | 1 + src/main/java/blackjack/domain/Cards.java | 44 +++++++++++++++++ src/test/java/blackjack/domain/CardsTest.java | 47 +++++++------------ 3 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 src/main/java/blackjack/domain/Cards.java diff --git a/README.md b/README.md index 917cb4fe75..2a6d01d532 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ graph TD - [x] 숫자를 가진다. - [x] 점수를 계산한다. - [x] 에이스인지 확인한다. +- [x] 점수 최댓값 초과 여부를 확인한다. ### 덱 diff --git a/src/main/java/blackjack/domain/Cards.java b/src/main/java/blackjack/domain/Cards.java new file mode 100644 index 0000000000..dda756be3a --- /dev/null +++ b/src/main/java/blackjack/domain/Cards.java @@ -0,0 +1,44 @@ +package blackjack.domain; + +import java.util.List; + +public class Cards { + + private static final int MAXIMUM_SCORE = 21; + private static final int ACE_BONUS = 10; + + private final List cards; + + public Cards(final List cards) { + this.cards = cards; + } + + public int calculateTotalScore() { + final int score = getTotalScore(); + + if (isExistAce() && isScoreUpdatable(score)) { + return score + ACE_BONUS; + } + + return score; + } + + private int getTotalScore() { + return cards.stream() + .mapToInt(Card::getScore) + .sum(); + } + + private boolean isExistAce() { + return cards.stream() + .anyMatch(Card::isAce); + } + + private boolean isScoreUpdatable(final int score) { + return score + ACE_BONUS <= MAXIMUM_SCORE; + } + + public boolean isTotalScoreOver() { + return getTotalScore() > MAXIMUM_SCORE; + } +} diff --git a/src/test/java/blackjack/domain/CardsTest.java b/src/test/java/blackjack/domain/CardsTest.java index 620414116a..700faa1f93 100644 --- a/src/test/java/blackjack/domain/CardsTest.java +++ b/src/test/java/blackjack/domain/CardsTest.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.stream.Stream; +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; @@ -49,41 +50,25 @@ private static Stream generateCardsWithACE() { assertThat(cards.calculateTotalScore()).isEqualTo(totalScore); } -} - -class Cards { - - private static final int MAXIMUM_SCORE = 21; - private static final int ACE_BONUS = 10; - - private final List cards; - - public Cards(final List cards) { - this.cards = cards; - } - - public int calculateTotalScore() { - final int score = getTotalScore(); - if (isExistAce() && isScoreUpdatable(score)) { - return score + ACE_BONUS; - } + @Test + void 점수_최댓값_초과라면_true_반환한다() { + final Cards cards = new Cards( + List.of(new Card(TEN, SPADE), + new Card(JACK, HEART), + new Card(QUEEN, CLOVER) + )); - return score; + assertThat(cards.isTotalScoreOver()).isTrue(); } - private int getTotalScore() { - return cards.stream() - .mapToInt(Card::getScore) - .sum(); - } - - private boolean isExistAce() { - return cards.stream() - .anyMatch(Card::isAce); - } + @Test + void 점수_최댓값_이하라면_false_반환한다() { + final Cards cards = new Cards( + List.of(new Card(TEN, SPADE), + new Card(JACK, HEART) + )); - private boolean isScoreUpdatable(final int score) { - return score + ACE_BONUS <= MAXIMUM_SCORE; + assertThat(cards.isTotalScoreOver()).isFalse(); } } From 36dacd50a3acff5797ec8509d2f3cae955a4822a Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 16:51:02 +0900 Subject: [PATCH 15/80] =?UTF-8?q?feat:=20=EC=9D=B4=EB=A6=84=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=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 Co-authored-by: woo-chang --- src/main/java/blackjack/domain/Name.java | 4 +++ .../java/blackjack/domain/PlayerTest.java | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/test/java/blackjack/domain/PlayerTest.java diff --git a/src/main/java/blackjack/domain/Name.java b/src/main/java/blackjack/domain/Name.java index 2db96dd6f9..ffa92e6f07 100644 --- a/src/main/java/blackjack/domain/Name.java +++ b/src/main/java/blackjack/domain/Name.java @@ -35,4 +35,8 @@ private void validateRestrictWord(final String value) { throw new IllegalArgumentException("이름은 " + RESTRICT + "일 수 없습니다. 현재 이름: " + value); } } + + public String getValue() { + return value; + } } diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java new file mode 100644 index 0000000000..71b4e8ef35 --- /dev/null +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -0,0 +1,28 @@ +package blackjack.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class PlayerTest { + + @Test + void 이름을_확인한다() { + final Player player = new Player("dazzle"); + + assertThat(player.getName()).isEqualTo("dazzle"); + } +} + +class Player { + + private final Name name; + + public Player(final String name) { + this.name = new Name(name); + } + + public String getName() { + return name.getValue(); + } +} From fe3be1eae61d576c546ba6e11954dd9f178be871 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 17:06:01 +0900 Subject: [PATCH 16/80] =?UTF-8?q?feat:=20=EB=B8=94=EB=9E=99=EC=9E=AD=20?= =?UTF-8?q?=EC=97=AC=EB=B6=80=20=ED=99=95=EC=9D=B8=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 Co-authored-by: woo-chang --- README.md | 1 + src/main/java/blackjack/domain/Cards.java | 4 ++++ src/test/java/blackjack/domain/CardsTest.java | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/README.md b/README.md index 2a6d01d532..292fbe6504 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ graph TD - [x] 점수를 계산한다. - [x] 에이스인지 확인한다. - [x] 점수 최댓값 초과 여부를 확인한다. +- [x] 블랙잭 여부를 확인한다. ### 덱 diff --git a/src/main/java/blackjack/domain/Cards.java b/src/main/java/blackjack/domain/Cards.java index dda756be3a..b18dc9668d 100644 --- a/src/main/java/blackjack/domain/Cards.java +++ b/src/main/java/blackjack/domain/Cards.java @@ -41,4 +41,8 @@ private boolean isScoreUpdatable(final int score) { public boolean isTotalScoreOver() { return getTotalScore() > MAXIMUM_SCORE; } + + public boolean isBlackJack() { + return getTotalScore() == MAXIMUM_SCORE; + } } diff --git a/src/test/java/blackjack/domain/CardsTest.java b/src/test/java/blackjack/domain/CardsTest.java index 700faa1f93..f97dd301aa 100644 --- a/src/test/java/blackjack/domain/CardsTest.java +++ b/src/test/java/blackjack/domain/CardsTest.java @@ -71,4 +71,25 @@ private static Stream generateCardsWithACE() { assertThat(cards.isTotalScoreOver()).isFalse(); } + + @Test + void 블랙잭이면_true_반환한다() { + final Cards cards = new Cards( + List.of(new Card(TEN, SPADE), + new Card(JACK, HEART), + new Card(ACE, CLOVER) + )); + + assertThat(cards.isBlackJack()).isTrue(); + } + + @Test + void 블랙잭이_아니면_false_반환한다() { + final Cards cards = new Cards( + List.of(new Card(TEN, SPADE), + new Card(JACK, HEART) + )); + + assertThat(cards.isBlackJack()).isFalse(); + } } From bf4991f5cbbe74eb6a8b8b281951f359518d6fbf Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 17:18:57 +0900 Subject: [PATCH 17/80] =?UTF-8?q?fix:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=AA=85=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=9E=98=EB=AA=BB?= =?UTF-8?q?=EB=90=9C=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/domain/Cards.java | 6 +++--- src/test/java/blackjack/domain/CardsTest.java | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 292fbe6504..d76161d82e 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ graph TD - [x] 점수를 계산한다. - [x] 에이스인지 확인한다. - [x] 점수 최댓값 초과 여부를 확인한다. -- [x] 블랙잭 여부를 확인한다. +- [x] 점수 최댓값 여부를 확인한다. ### 덱 diff --git a/src/main/java/blackjack/domain/Cards.java b/src/main/java/blackjack/domain/Cards.java index b18dc9668d..c26d07c319 100644 --- a/src/main/java/blackjack/domain/Cards.java +++ b/src/main/java/blackjack/domain/Cards.java @@ -39,10 +39,10 @@ private boolean isScoreUpdatable(final int score) { } public boolean isTotalScoreOver() { - return getTotalScore() > MAXIMUM_SCORE; + return calculateTotalScore() > MAXIMUM_SCORE; } - public boolean isBlackJack() { - return getTotalScore() == MAXIMUM_SCORE; + public boolean isMaximumScore() { + return calculateTotalScore() == MAXIMUM_SCORE; } } diff --git a/src/test/java/blackjack/domain/CardsTest.java b/src/test/java/blackjack/domain/CardsTest.java index f97dd301aa..12590e5157 100644 --- a/src/test/java/blackjack/domain/CardsTest.java +++ b/src/test/java/blackjack/domain/CardsTest.java @@ -73,23 +73,23 @@ private static Stream generateCardsWithACE() { } @Test - void 블랙잭이면_true_반환한다() { + void 최대_점수라면_true_반환한다() { final Cards cards = new Cards( List.of(new Card(TEN, SPADE), new Card(JACK, HEART), new Card(ACE, CLOVER) )); - assertThat(cards.isBlackJack()).isTrue(); + assertThat(cards.isMaximumScore()).isTrue(); } @Test - void 블랙잭이_아니면_false_반환한다() { + void 최대_점수_아니라면_false_반환한다() { final Cards cards = new Cards( List.of(new Card(TEN, SPADE), new Card(JACK, HEART) )); - assertThat(cards.isBlackJack()).isFalse(); + assertThat(cards.isMaximumScore()).isFalse(); } } From ed7070125a408dd7794e21b5951298ef5aaa973d Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 17:20:08 +0900 Subject: [PATCH 18/80] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EC=97=AC=EB=B6=80=20=ED=99=95=EC=9D=B8=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 Co-authored-by: woo-chang --- .../java/blackjack/domain/PlayerTest.java | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java index 71b4e8ef35..ac073b5799 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -2,27 +2,60 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.List; import org.junit.jupiter.api.Test; class PlayerTest { @Test void 이름을_확인한다() { - final Player player = new Player("dazzle"); + final Cards cards = new Cards(List.of( + new Card(Number.QUEEN, Suit.CLOVER), + new Card(Number.QUEEN, Suit.HEART) + )); + final Player player = new Player("dazzle", cards); assertThat(player.getName()).isEqualTo("dazzle"); } + + @Test + void 카드를_뽑을_수_있으면_true_반환한다() { + final Cards cards = new Cards(List.of( + new Card(Number.QUEEN, Suit.CLOVER), + new Card(Number.QUEEN, Suit.HEART) + )); + final Player player = new Player("kokodak", cards); + + assertThat(player.isHittable()).isTrue(); + } + + @Test + void 카드를_뽑을_수_없으면_false_반환한다() { + final Cards cards = new Cards(List.of( + new Card(Number.QUEEN, Suit.CLOVER), + new Card(Number.ACE, Suit.HEART) + )); + final Player player = new Player("kokodak", cards); + + assertThat(player.isHittable()).isFalse(); + } } class Player { private final Name name; + private final Cards cards; - public Player(final String name) { + public Player(final String name, final Cards cards) { this.name = new Name(name); + this.cards = cards; } public String getName() { return name.getValue(); } + + public boolean isHittable() { + return !cards.isMaximumScore() && !cards.isTotalScoreOver(); + } } From 4c494a05091ffcbc4922fa6767dfec7c07409c5b Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 17:40:06 +0900 Subject: [PATCH 19/80] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=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 Co-authored-by: woo-chang --- README.md | 1 + src/main/java/blackjack/domain/Cards.java | 9 +++++++++ src/test/java/blackjack/domain/CardsTest.java | 12 ++++++++++++ 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index d76161d82e..e07669609b 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ graph TD - [x] 에이스인지 확인한다. - [x] 점수 최댓값 초과 여부를 확인한다. - [x] 점수 최댓값 여부를 확인한다. +- [x] 카드를 추가한다. ### 덱 diff --git a/src/main/java/blackjack/domain/Cards.java b/src/main/java/blackjack/domain/Cards.java index c26d07c319..2a3351b294 100644 --- a/src/main/java/blackjack/domain/Cards.java +++ b/src/main/java/blackjack/domain/Cards.java @@ -1,5 +1,6 @@ package blackjack.domain; +import java.util.Collections; import java.util.List; public class Cards { @@ -13,6 +14,10 @@ public Cards(final List cards) { this.cards = cards; } + public void addCard(final Card card) { + cards.add(card); + } + public int calculateTotalScore() { final int score = getTotalScore(); @@ -45,4 +50,8 @@ public boolean isTotalScoreOver() { public boolean isMaximumScore() { return calculateTotalScore() == MAXIMUM_SCORE; } + + public List getCards() { + return Collections.unmodifiableList(cards); + } } diff --git a/src/test/java/blackjack/domain/CardsTest.java b/src/test/java/blackjack/domain/CardsTest.java index 12590e5157..5638daa27f 100644 --- a/src/test/java/blackjack/domain/CardsTest.java +++ b/src/test/java/blackjack/domain/CardsTest.java @@ -6,10 +6,12 @@ import static blackjack.domain.Number.TEN; import static blackjack.domain.Number.TWO; import static blackjack.domain.Suit.CLOVER; +import static blackjack.domain.Suit.DIAMOND; import static blackjack.domain.Suit.HEART; import static blackjack.domain.Suit.SPADE; import static org.assertj.core.api.Assertions.assertThat; +import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -92,4 +94,14 @@ private static Stream generateCardsWithACE() { assertThat(cards.isMaximumScore()).isFalse(); } + + @Test + void 카드를_추가한다() { + final Cards cards = new Cards(new ArrayList<>()); + + final Card card = new Card(ACE, DIAMOND); + cards.addCard(card); + + assertThat(cards.getCards()).hasSize(1); + } } From 43b6ec5a2282ba099d37fce73b4f69d3c43acac0 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 17:41:56 +0900 Subject: [PATCH 20/80] =?UTF-8?q?chore:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- src/test/java/blackjack/domain/CardTest.java | 1 + src/test/java/blackjack/domain/CardsTest.java | 4 ++++ src/test/java/blackjack/domain/PlayerTest.java | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/src/test/java/blackjack/domain/CardTest.java b/src/test/java/blackjack/domain/CardTest.java index cbe5295cf6..4eb8825978 100644 --- a/src/test/java/blackjack/domain/CardTest.java +++ b/src/test/java/blackjack/domain/CardTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; +@SuppressWarnings("NonAsciiCharacters") @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class CardTest { diff --git a/src/test/java/blackjack/domain/CardsTest.java b/src/test/java/blackjack/domain/CardsTest.java index 5638daa27f..e3670e3335 100644 --- a/src/test/java/blackjack/domain/CardsTest.java +++ b/src/test/java/blackjack/domain/CardsTest.java @@ -14,11 +14,15 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; 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; +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class CardsTest { private static Stream generateCardsWithoutACE() { diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java index ac073b5799..332441ffa3 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -3,8 +3,12 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.List; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class PlayerTest { @Test From 35fd4fc62acd5532d05eb19c6ae4c90cec29bfc7 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 17:59:00 +0900 Subject: [PATCH 21/80] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EB=B0=9B=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 Co-authored-by: woo-chang --- .../java/blackjack/domain/PlayerTest.java | 49 ++++++++++++++++--- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java index 332441ffa3..affda4b776 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -1,7 +1,15 @@ package blackjack.domain; +import static blackjack.domain.Number.ACE; +import static blackjack.domain.Number.KING; +import static blackjack.domain.Number.QUEEN; +import static blackjack.domain.Number.TWO; +import static blackjack.domain.Suit.CLOVER; +import static blackjack.domain.Suit.DIAMOND; +import static blackjack.domain.Suit.HEART; import static org.assertj.core.api.Assertions.assertThat; +import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -14,8 +22,8 @@ class PlayerTest { @Test void 이름을_확인한다() { final Cards cards = new Cards(List.of( - new Card(Number.QUEEN, Suit.CLOVER), - new Card(Number.QUEEN, Suit.HEART) + new Card(QUEEN, CLOVER), + new Card(QUEEN, HEART) )); final Player player = new Player("dazzle", cards); @@ -25,8 +33,8 @@ class PlayerTest { @Test void 카드를_뽑을_수_있으면_true_반환한다() { final Cards cards = new Cards(List.of( - new Card(Number.QUEEN, Suit.CLOVER), - new Card(Number.QUEEN, Suit.HEART) + new Card(QUEEN, CLOVER), + new Card(QUEEN, HEART) )); final Player player = new Player("kokodak", cards); @@ -36,13 +44,30 @@ class PlayerTest { @Test void 카드를_뽑을_수_없으면_false_반환한다() { final Cards cards = new Cards(List.of( - new Card(Number.QUEEN, Suit.CLOVER), - new Card(Number.ACE, Suit.HEART) + new Card(QUEEN, CLOVER), + new Card(ACE, HEART) )); final Player player = new Player("kokodak", cards); assertThat(player.isHittable()).isFalse(); } + + @Test + void 카드를_받는다() { + //given + List cardPack = new ArrayList<>(List.of( + new Card(QUEEN, CLOVER), + new Card(KING, HEART) + )); + final Cards cards = new Cards(cardPack); + final Player player = new Player("dazzle", cards); + + //when + player.hit(new Card(TWO, DIAMOND)); + + //then + assertThat(player.isHittable()).isFalse(); + } } class Player { @@ -55,11 +80,19 @@ public Player(final String name, final Cards cards) { this.cards = cards; } - public String getName() { - return name.getValue(); + public void hit(final Card card) { + cards.addCard(card); } public boolean isHittable() { return !cards.isMaximumScore() && !cards.isTotalScoreOver(); } + + public String getName() { + return name.getValue(); + } + + public Cards getCards() { + return cards; + } } From d3a6af1a3486f7010a00f3a9e65a00cb89ec3a4d Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 19:05:44 +0900 Subject: [PATCH 22/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=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 Co-authored-by: woo-chang --- src/main/java/blackjack/domain/Player.java | 32 ++++++++++++++++++ .../java/blackjack/domain/PlayerTest.java | 33 +++++-------------- 2 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 src/main/java/blackjack/domain/Player.java diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/Player.java new file mode 100644 index 0000000000..aba7ee67a2 --- /dev/null +++ b/src/main/java/blackjack/domain/Player.java @@ -0,0 +1,32 @@ +package blackjack.domain; + +public class Player { + + private final Name name; + private final Cards cards; + + public Player(final String name, final Cards cards) { + this.name = new Name(name); + this.cards = cards; + } + + public void hit(final Card card) { + cards.addCard(card); + } + + public boolean isHittable() { + return !cards.isMaximumScore() && !cards.isTotalScoreOver(); + } + + public int getScore() { + return cards.calculateTotalScore(); + } + + public String getName() { + return name.getValue(); + } + + public Cards getCards() { + return cards; + } +} diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java index affda4b776..386e3a650d 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -68,31 +68,16 @@ class PlayerTest { //then assertThat(player.isHittable()).isFalse(); } -} - -class Player { - - private final Name name; - private final Cards cards; - - public Player(final String name, final Cards cards) { - this.name = new Name(name); - this.cards = cards; - } - - public void hit(final Card card) { - cards.addCard(card); - } - - public boolean isHittable() { - return !cards.isMaximumScore() && !cards.isTotalScoreOver(); - } - public String getName() { - return name.getValue(); - } + @Test + void 점수를_확인한다() { + List cardPack = new ArrayList<>(List.of( + new Card(QUEEN, CLOVER), + new Card(KING, HEART) + )); + final Cards cards = new Cards(cardPack); + final Player player = new Player("dazzle", cards); - public Cards getCards() { - return cards; + assertThat(player.getScore()).isEqualTo(20); } } From 58f1fae3316add9c0197195ab034f2a5c722ec68 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 19:35:53 +0900 Subject: [PATCH 23/80] =?UTF-8?q?feat:=20=EA=B0=9C=EC=88=98=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=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 Co-authored-by: woo-chang --- README.md | 1 + src/main/java/blackjack/domain/Cards.java | 4 ++++ src/test/java/blackjack/domain/CardsTest.java | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e07669609b..80a09fc7d9 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ graph TD - [x] 점수 최댓값 초과 여부를 확인한다. - [x] 점수 최댓값 여부를 확인한다. - [x] 카드를 추가한다. +- [x] 카드 개수를 확인한다. ### 덱 diff --git a/src/main/java/blackjack/domain/Cards.java b/src/main/java/blackjack/domain/Cards.java index 2a3351b294..f8d81f9929 100644 --- a/src/main/java/blackjack/domain/Cards.java +++ b/src/main/java/blackjack/domain/Cards.java @@ -51,6 +51,10 @@ public boolean isMaximumScore() { return calculateTotalScore() == MAXIMUM_SCORE; } + public int count() { + return cards.size(); + } + public List getCards() { return Collections.unmodifiableList(cards); } diff --git a/src/test/java/blackjack/domain/CardsTest.java b/src/test/java/blackjack/domain/CardsTest.java index e3670e3335..dc3e7a3b98 100644 --- a/src/test/java/blackjack/domain/CardsTest.java +++ b/src/test/java/blackjack/domain/CardsTest.java @@ -106,6 +106,6 @@ private static Stream generateCardsWithACE() { final Card card = new Card(ACE, DIAMOND); cards.addCard(card); - assertThat(cards.getCards()).hasSize(1); + assertThat(cards.count()).isEqualTo(1); } } From 81f907ae921e6584afedf4b5614955ad47592c12 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 19:36:19 +0900 Subject: [PATCH 24/80] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EC=97=AC=EB=B6=80=20=ED=99=95=EC=9D=B8=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 Co-authored-by: woo-chang --- .../java/blackjack/domain/DealerTest.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/test/java/blackjack/domain/DealerTest.java diff --git a/src/test/java/blackjack/domain/DealerTest.java b/src/test/java/blackjack/domain/DealerTest.java new file mode 100644 index 0000000000..f8cb3334e8 --- /dev/null +++ b/src/test/java/blackjack/domain/DealerTest.java @@ -0,0 +1,78 @@ +package blackjack.domain; + +import static blackjack.domain.Number.QUEEN; +import static blackjack.domain.Number.SEVEN; +import static blackjack.domain.Number.SIX; +import static blackjack.domain.Number.TWO; +import static blackjack.domain.Suit.CLOVER; +import static blackjack.domain.Suit.DIAMOND; +import static blackjack.domain.Suit.HEART; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class DealerTest { + + @Test + void 카드를_뽑을_수_있으면_true_반환한다() { + final Cards cards = new Cards(List.of( + new Card(QUEEN, CLOVER), + new Card(SIX, HEART) + )); + final Dealer dealer = new Dealer(cards); + + assertThat(dealer.isHittable()).isTrue(); + } + + @Test + void 카드를_뽑을_수_없으면_false_반환한다() { + final Cards cards = new Cards(List.of( + new Card(QUEEN, CLOVER), + new Card(SEVEN, HEART) + )); + final Dealer dealer = new Dealer(cards); + + assertThat(dealer.isHittable()).isFalse(); + } + + @Test + void 소지한_카드가_3장이라면_false_반환한다() { + final Cards cards = new Cards(List.of( + new Card(TWO, CLOVER), + new Card(SIX, HEART), + new Card(SEVEN, DIAMOND) + )); + final Dealer dealer = new Dealer(cards); + + assertThat(dealer.isHittable()).isFalse(); + } +} + +class Dealer { + + private static final int MAXIMUM_CARD_COUNT = 2; + private static final int MAXIMUM_SCORE = 16; + + private final Cards cards; + + public Dealer(final Cards cards) { + this.cards = cards; + } + + public boolean isHittable() { + return isCardShortage() && isScoreLow(); + } + + private boolean isCardShortage() { + return cards.count() <= MAXIMUM_CARD_COUNT; + } + + private boolean isScoreLow() { + return cards.calculateTotalScore() <= MAXIMUM_SCORE; + } +} From 96833fdb4b7d66e0746baf10cb30a861e47e2eb1 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 19:39:08 +0900 Subject: [PATCH 25/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=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 Co-authored-by: woo-chang --- src/test/java/blackjack/domain/DealerTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/test/java/blackjack/domain/DealerTest.java b/src/test/java/blackjack/domain/DealerTest.java index f8cb3334e8..b990867b2f 100644 --- a/src/test/java/blackjack/domain/DealerTest.java +++ b/src/test/java/blackjack/domain/DealerTest.java @@ -51,6 +51,18 @@ class DealerTest { assertThat(dealer.isHittable()).isFalse(); } + + @Test + void 점수를_확인한다() { + final Cards cards = new Cards(List.of( + new Card(TWO, CLOVER), + new Card(SIX, HEART), + new Card(SEVEN, DIAMOND) + )); + final Dealer dealer = new Dealer(cards); + + assertThat(dealer.getScore()).isEqualTo(15); + } } class Dealer { @@ -75,4 +87,8 @@ private boolean isCardShortage() { private boolean isScoreLow() { return cards.calculateTotalScore() <= MAXIMUM_SCORE; } + + public int getScore() { + return cards.calculateTotalScore(); + } } From 5138dc4e2cab5cee3beafd5b720b4713426b26ac Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 19:45:46 +0900 Subject: [PATCH 26/80] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EB=B0=9B=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 Co-authored-by: woo-chang --- .../java/blackjack/domain/DealerTest.java | 25 ++++++++++++++++++- .../java/blackjack/domain/PlayerTest.java | 4 +-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/test/java/blackjack/domain/DealerTest.java b/src/test/java/blackjack/domain/DealerTest.java index b990867b2f..5593984c8f 100644 --- a/src/test/java/blackjack/domain/DealerTest.java +++ b/src/test/java/blackjack/domain/DealerTest.java @@ -1,5 +1,6 @@ package blackjack.domain; +import static blackjack.domain.Number.ACE; import static blackjack.domain.Number.QUEEN; import static blackjack.domain.Number.SEVEN; import static blackjack.domain.Number.SIX; @@ -9,6 +10,7 @@ import static blackjack.domain.Suit.HEART; import static org.assertj.core.api.Assertions.assertThat; +import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -52,6 +54,23 @@ class DealerTest { assertThat(dealer.isHittable()).isFalse(); } + @Test + void 카드를_받는다() { + //given + final List cardPack = new ArrayList<>(List.of( + new Card(QUEEN, CLOVER), + new Card(SIX, HEART) + )); + final Cards cards = new Cards(cardPack); + final Dealer dealer = new Dealer(cards); + + //when + dealer.hit(new Card(ACE, DIAMOND)); + + //then + assertThat(dealer.isHittable()).isFalse(); + } + @Test void 점수를_확인한다() { final Cards cards = new Cards(List.of( @@ -66,7 +85,7 @@ class DealerTest { } class Dealer { - + private static final int MAXIMUM_CARD_COUNT = 2; private static final int MAXIMUM_SCORE = 16; @@ -76,6 +95,10 @@ public Dealer(final Cards cards) { this.cards = cards; } + public void hit(final Card card) { + cards.addCard(card); + } + public boolean isHittable() { return isCardShortage() && isScoreLow(); } diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java index 386e3a650d..77ef20c9d7 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -55,7 +55,7 @@ class PlayerTest { @Test void 카드를_받는다() { //given - List cardPack = new ArrayList<>(List.of( + final List cardPack = new ArrayList<>(List.of( new Card(QUEEN, CLOVER), new Card(KING, HEART) )); @@ -71,7 +71,7 @@ class PlayerTest { @Test void 점수를_확인한다() { - List cardPack = new ArrayList<>(List.of( + final List cardPack = new ArrayList<>(List.of( new Card(QUEEN, CLOVER), new Card(KING, HEART) )); From 4e58c1dbc8586c6210fe7af0d391ac4ef8808c95 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 19:49:28 +0900 Subject: [PATCH 27/80] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- src/main/java/blackjack/domain/Dealer.java | 43 +++++++++++++++++++ .../java/blackjack/domain/DealerTest.java | 32 -------------- 2 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 src/main/java/blackjack/domain/Dealer.java diff --git a/src/main/java/blackjack/domain/Dealer.java b/src/main/java/blackjack/domain/Dealer.java new file mode 100644 index 0000000000..c27b4a0369 --- /dev/null +++ b/src/main/java/blackjack/domain/Dealer.java @@ -0,0 +1,43 @@ +package blackjack.domain; + +public class Dealer { + + private static final String DEALER_NAME = "딜러"; + private static final int MAXIMUM_CARD_COUNT = 2; + private static final int MAXIMUM_SCORE = 16; + + private final String name = DEALER_NAME; + private final Cards cards; + + public Dealer(final Cards cards) { + this.cards = cards; + } + + public void hit(final Card card) { + cards.addCard(card); + } + + public boolean isHittable() { + return isCardShortage() && isScoreLow(); + } + + private boolean isCardShortage() { + return cards.count() <= MAXIMUM_CARD_COUNT; + } + + private boolean isScoreLow() { + return cards.calculateTotalScore() <= MAXIMUM_SCORE; + } + + public int getScore() { + return cards.calculateTotalScore(); + } + + public String getName() { + return name; + } + + public Cards getCards() { + return cards; + } +} diff --git a/src/test/java/blackjack/domain/DealerTest.java b/src/test/java/blackjack/domain/DealerTest.java index 5593984c8f..c4c6805d54 100644 --- a/src/test/java/blackjack/domain/DealerTest.java +++ b/src/test/java/blackjack/domain/DealerTest.java @@ -83,35 +83,3 @@ class DealerTest { assertThat(dealer.getScore()).isEqualTo(15); } } - -class Dealer { - - private static final int MAXIMUM_CARD_COUNT = 2; - private static final int MAXIMUM_SCORE = 16; - - private final Cards cards; - - public Dealer(final Cards cards) { - this.cards = cards; - } - - public void hit(final Card card) { - cards.addCard(card); - } - - public boolean isHittable() { - return isCardShortage() && isScoreLow(); - } - - private boolean isCardShortage() { - return cards.count() <= MAXIMUM_CARD_COUNT; - } - - private boolean isScoreLow() { - return cards.calculateTotalScore() <= MAXIMUM_SCORE; - } - - public int getScore() { - return cards.calculateTotalScore(); - } -} From e3ae0a0671535e6f8d65baf449d495a1abf8b992 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 20:06:12 +0900 Subject: [PATCH 28/80] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- .../java/blackjack/domain/Participant.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/blackjack/domain/Participant.java diff --git a/src/main/java/blackjack/domain/Participant.java b/src/main/java/blackjack/domain/Participant.java new file mode 100644 index 0000000000..396ff233ef --- /dev/null +++ b/src/main/java/blackjack/domain/Participant.java @@ -0,0 +1,28 @@ +package blackjack.domain; + +import java.util.List; + +public abstract class Participant { + + private final Cards cards; + + protected Participant(final Cards cards) { + this.cards = cards; + } + + public void drawCard(final Card card) { + cards.addCard(card); + } + + public int getScore() { + return cards.calculateTotalScore(); + } + + public List getCards() { + return cards.getCards(); + } + + public abstract boolean isDrawable(); + + public abstract String getName(); +} From 4cdb38c941afbc9daceeda08913a558db165fefa Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 20:35:54 +0900 Subject: [PATCH 29/80] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=EC=99=80=20?= =?UTF-8?q?=ED=94=8C=EB=A0=88=EC=9D=B4=EC=96=B4=EA=B0=80=20=EC=B0=B8?= =?UTF-8?q?=EA=B0=80=EC=9E=90=20=EC=83=81=EC=86=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- README.md | 6 +++--- src/main/java/blackjack/domain/Dealer.java | 21 +++++-------------- .../java/blackjack/domain/Participant.java | 2 +- src/main/java/blackjack/domain/Player.java | 21 +++++-------------- .../java/blackjack/domain/DealerTest.java | 10 ++++----- .../java/blackjack/domain/PlayerTest.java | 8 +++---- 6 files changed, 23 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 80a09fc7d9..f088e28455 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,9 @@ graph TD ### 참가자 -- [ ] 카드를 받는다. -- [ ] 점수를 확인한다. -- [ ] 카드 추가 여부를 결정한다. +- [x] 카드를 받는다. +- [x] 점수를 확인한다. +- [x] 카드 추가 여부를 결정한다. - [ ] 여러 명일 수 있다. - [ ] 중복되는 이름은 가질 수 없다. - [ ] 최대 5명이다. diff --git a/src/main/java/blackjack/domain/Dealer.java b/src/main/java/blackjack/domain/Dealer.java index c27b4a0369..67a584464c 100644 --- a/src/main/java/blackjack/domain/Dealer.java +++ b/src/main/java/blackjack/domain/Dealer.java @@ -1,23 +1,19 @@ package blackjack.domain; -public class Dealer { +public class Dealer extends Participant { private static final String DEALER_NAME = "딜러"; private static final int MAXIMUM_CARD_COUNT = 2; private static final int MAXIMUM_SCORE = 16; private final String name = DEALER_NAME; - private final Cards cards; public Dealer(final Cards cards) { - this.cards = cards; + super(cards); } - public void hit(final Card card) { - cards.addCard(card); - } - - public boolean isHittable() { + @Override + public boolean isDrawable() { return isCardShortage() && isScoreLow(); } @@ -29,15 +25,8 @@ private boolean isScoreLow() { return cards.calculateTotalScore() <= MAXIMUM_SCORE; } - public int getScore() { - return cards.calculateTotalScore(); - } - + @Override public String getName() { return name; } - - public Cards getCards() { - return cards; - } } diff --git a/src/main/java/blackjack/domain/Participant.java b/src/main/java/blackjack/domain/Participant.java index 396ff233ef..49ce00df3d 100644 --- a/src/main/java/blackjack/domain/Participant.java +++ b/src/main/java/blackjack/domain/Participant.java @@ -4,7 +4,7 @@ public abstract class Participant { - private final Cards cards; + protected final Cards cards; protected Participant(final Cards cards) { this.cards = cards; diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/Player.java index aba7ee67a2..8f63751f6e 100644 --- a/src/main/java/blackjack/domain/Player.java +++ b/src/main/java/blackjack/domain/Player.java @@ -1,32 +1,21 @@ package blackjack.domain; -public class Player { +public class Player extends Participant { private final Name name; - private final Cards cards; public Player(final String name, final Cards cards) { + super(cards); this.name = new Name(name); - this.cards = cards; } - public void hit(final Card card) { - cards.addCard(card); - } - - public boolean isHittable() { + @Override + public boolean isDrawable() { return !cards.isMaximumScore() && !cards.isTotalScoreOver(); } - public int getScore() { - return cards.calculateTotalScore(); - } - + @Override public String getName() { return name.getValue(); } - - public Cards getCards() { - return cards; - } } diff --git a/src/test/java/blackjack/domain/DealerTest.java b/src/test/java/blackjack/domain/DealerTest.java index c4c6805d54..2558a33c3c 100644 --- a/src/test/java/blackjack/domain/DealerTest.java +++ b/src/test/java/blackjack/domain/DealerTest.java @@ -28,7 +28,7 @@ class DealerTest { )); final Dealer dealer = new Dealer(cards); - assertThat(dealer.isHittable()).isTrue(); + assertThat(dealer.isDrawable()).isTrue(); } @Test @@ -39,7 +39,7 @@ class DealerTest { )); final Dealer dealer = new Dealer(cards); - assertThat(dealer.isHittable()).isFalse(); + assertThat(dealer.isDrawable()).isFalse(); } @Test @@ -51,7 +51,7 @@ class DealerTest { )); final Dealer dealer = new Dealer(cards); - assertThat(dealer.isHittable()).isFalse(); + assertThat(dealer.isDrawable()).isFalse(); } @Test @@ -65,10 +65,10 @@ class DealerTest { final Dealer dealer = new Dealer(cards); //when - dealer.hit(new Card(ACE, DIAMOND)); + dealer.drawCard(new Card(ACE, DIAMOND)); //then - assertThat(dealer.isHittable()).isFalse(); + assertThat(dealer.isDrawable()).isFalse(); } @Test diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/PlayerTest.java index 77ef20c9d7..fa74ef8b30 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/PlayerTest.java @@ -38,7 +38,7 @@ class PlayerTest { )); final Player player = new Player("kokodak", cards); - assertThat(player.isHittable()).isTrue(); + assertThat(player.isDrawable()).isTrue(); } @Test @@ -49,7 +49,7 @@ class PlayerTest { )); final Player player = new Player("kokodak", cards); - assertThat(player.isHittable()).isFalse(); + assertThat(player.isDrawable()).isFalse(); } @Test @@ -63,10 +63,10 @@ class PlayerTest { final Player player = new Player("dazzle", cards); //when - player.hit(new Card(TWO, DIAMOND)); + player.drawCard(new Card(TWO, DIAMOND)); //then - assertThat(player.isHittable()).isFalse(); + assertThat(player.isDrawable()).isFalse(); } @Test From 2370e13dfd58c66b893c9cb5f8d02519acf34bf2 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 21:01:55 +0900 Subject: [PATCH 30/80] =?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=9D=B4=EB=A6=84=20=EC=A4=91=EB=B3=B5=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=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 Co-authored-by: woo-chang --- src/main/java/blackjack/domain/Name.java | 19 +++++++ src/main/java/blackjack/domain/Player.java | 19 +++++++ .../blackjack/domain/ParticipantsTest.java | 54 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/test/java/blackjack/domain/ParticipantsTest.java diff --git a/src/main/java/blackjack/domain/Name.java b/src/main/java/blackjack/domain/Name.java index ffa92e6f07..2905e18723 100644 --- a/src/main/java/blackjack/domain/Name.java +++ b/src/main/java/blackjack/domain/Name.java @@ -1,5 +1,7 @@ package blackjack.domain; +import java.util.Objects; + public class Name { private static final int UPPER_BOUND = 10; @@ -39,4 +41,21 @@ private void validateRestrictWord(final String value) { public String getValue() { return value; } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Name name = (Name) o; + return Objects.equals(value, name.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } } diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/Player.java index 8f63751f6e..13aa4a6241 100644 --- a/src/main/java/blackjack/domain/Player.java +++ b/src/main/java/blackjack/domain/Player.java @@ -1,5 +1,7 @@ package blackjack.domain; +import java.util.Objects; + public class Player extends Participant { private final Name name; @@ -18,4 +20,21 @@ public boolean isDrawable() { public String getName() { return name.getValue(); } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Player player = (Player) o; + return Objects.equals(name, player.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } } diff --git a/src/test/java/blackjack/domain/ParticipantsTest.java b/src/test/java/blackjack/domain/ParticipantsTest.java new file mode 100644 index 0000000000..97ff4b9b61 --- /dev/null +++ b/src/test/java/blackjack/domain/ParticipantsTest.java @@ -0,0 +1,54 @@ +package blackjack.domain; + +import static blackjack.domain.Number.ACE; +import static blackjack.domain.Suit.SPADE; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class ParticipantsTest { + + @Test + void 중복되는_이름이_존재하면_예외를_던진다() { + final Cards cards = new Cards(List.of( + new Card(ACE, SPADE) + )); + final List participants = List.of( + new Dealer(cards), + new Player("dazzle", cards), + new Player("dazzle", cards), + new Player("kokodak", cards)); + + assertThatThrownBy(() -> new Participants(participants)) + .isInstanceOf(IllegalArgumentException.class); + } +} + +class Participants { + + private final List participants; + + public Participants(final List participants) { + validate(participants); + this.participants = List.copyOf(participants); + } + + private void validate(final List participants) { + final Set uniqueParticipants = new HashSet<>(participants); + + if (uniqueParticipants.size() != participants.size()) { + throw new IllegalArgumentException("참가자 이름은 중복될 수 없습니다."); + } + } + + public List getParticipants() { + return participants; + } +} From 7603a6b4b944772322df8c916fb2b8dce373cae2 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 21:11:40 +0900 Subject: [PATCH 31/80] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90=20?= =?UTF-8?q?=EC=9D=B8=EC=9B=90=20=EA=B2=80=EC=A6=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 Co-authored-by: woo-chang --- README.md | 6 +-- .../java/blackjack/domain/Participants.java | 40 +++++++++++++++++++ .../blackjack/domain/ParticipantsTest.java | 37 ++++++++--------- 3 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 src/main/java/blackjack/domain/Participants.java diff --git a/README.md b/README.md index f088e28455..f1d471aa93 100644 --- a/README.md +++ b/README.md @@ -45,9 +45,9 @@ graph TD - [x] 카드를 받는다. - [x] 점수를 확인한다. - [x] 카드 추가 여부를 결정한다. -- [ ] 여러 명일 수 있다. - - [ ] 중복되는 이름은 가질 수 없다. - - [ ] 최대 5명이다. +- [x] 여러 명일 수 있다. + - [x] 중복되는 이름은 가질 수 없다. + - [x] 딜러 포함 최대 6명이다. ### 플레이어 diff --git a/src/main/java/blackjack/domain/Participants.java b/src/main/java/blackjack/domain/Participants.java new file mode 100644 index 0000000000..425750e38f --- /dev/null +++ b/src/main/java/blackjack/domain/Participants.java @@ -0,0 +1,40 @@ +package blackjack.domain; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Participants { + + private static final int MAXIMUM_PARTICIPANT_COUNT = 6; + + private final List participants; + + public Participants(final List participants) { + validate(participants); + this.participants = List.copyOf(participants); + } + + private void validate(final List participants) { + validateCount(participants); + validateDuplicate(participants); + } + + private void validateCount(final List participants) { + if (participants.size() > MAXIMUM_PARTICIPANT_COUNT) { + throw new IllegalArgumentException("참가자는 " + MAXIMUM_PARTICIPANT_COUNT + "명을 초과할 수 없습니다"); + } + } + + private void validateDuplicate(final List participants) { + final Set uniqueParticipants = new HashSet<>(participants); + + if (uniqueParticipants.size() != participants.size()) { + throw new IllegalArgumentException("참가자 이름은 중복될 수 없습니다."); + } + } + + public List getParticipants() { + return participants; + } +} diff --git a/src/test/java/blackjack/domain/ParticipantsTest.java b/src/test/java/blackjack/domain/ParticipantsTest.java index 97ff4b9b61..5e1026834f 100644 --- a/src/test/java/blackjack/domain/ParticipantsTest.java +++ b/src/test/java/blackjack/domain/ParticipantsTest.java @@ -4,9 +4,7 @@ import static blackjack.domain.Suit.SPADE; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @@ -29,26 +27,23 @@ class ParticipantsTest { assertThatThrownBy(() -> new Participants(participants)) .isInstanceOf(IllegalArgumentException.class); } -} - -class Participants { - - private final List participants; - - public Participants(final List participants) { - validate(participants); - this.participants = List.copyOf(participants); - } - - private void validate(final List participants) { - final Set uniqueParticipants = new HashSet<>(participants); - if (uniqueParticipants.size() != participants.size()) { - throw new IllegalArgumentException("참가자 이름은 중복될 수 없습니다."); - } - } + @Test + void 딜러_포함_참여자가_6명_초과라면_예외를_던진다() { + final Cards cards = new Cards(List.of( + new Card(ACE, SPADE) + )); + final List participants = List.of( + new Dealer(cards), + new Player("dazzle", cards), + new Player("kokodak", cards), + new Player("odo", cards), + new Player("hoi", cards), + new Player("gray", cards), + new Player("pobi", cards) + ); - public List getParticipants() { - return participants; + assertThatThrownBy(() -> new Participants(participants)) + .isInstanceOf(IllegalArgumentException.class); } } From d4b9c22b4880538fb04cdd15b49e26685c4eac56 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 21:14:52 +0900 Subject: [PATCH 32/80] =?UTF-8?q?refactor:=20=EC=99=B8=EB=B6=80=20?= =?UTF-8?q?=EC=B0=B8=EC=A1=B0=20=EB=81=8A=EB=8F=84=EB=A1=9D=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 Co-authored-by: woo-chang --- src/main/java/blackjack/domain/Cards.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/blackjack/domain/Cards.java b/src/main/java/blackjack/domain/Cards.java index f8d81f9929..6a58390e7c 100644 --- a/src/main/java/blackjack/domain/Cards.java +++ b/src/main/java/blackjack/domain/Cards.java @@ -1,5 +1,6 @@ package blackjack.domain; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -11,7 +12,7 @@ public class Cards { private final List cards; public Cards(final List cards) { - this.cards = cards; + this.cards = new ArrayList<>(cards); } public void addCard(final Card card) { From f4e414cc95c9bb9648923c49532d568e2a1d143d Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 21:27:22 +0900 Subject: [PATCH 33/80] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EB=BD=91=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 Co-authored-by: woo-chang --- src/test/java/blackjack/domain/DeckTest.java | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/test/java/blackjack/domain/DeckTest.java diff --git a/src/test/java/blackjack/domain/DeckTest.java b/src/test/java/blackjack/domain/DeckTest.java new file mode 100644 index 0000000000..93f669d146 --- /dev/null +++ b/src/test/java/blackjack/domain/DeckTest.java @@ -0,0 +1,39 @@ +package blackjack.domain; + +import static blackjack.domain.Number.ACE; +import static blackjack.domain.Suit.SPADE; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Stack; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class DeckTest { + + @Test + void 카드를_뽑는다() { + final Stack cards = new Stack<>(); + final Card card = new Card(ACE, SPADE); + cards.add(card); + + final Deck deck = new Deck(cards); + + assertThat(deck.draw()).isEqualTo(card); + } +} + +class Deck { + + private final Stack cards; + + public Deck(final Stack cards) { + this.cards = cards; + } + + public Card draw() { + return cards.pop(); + } +} From 559693ddd932e2b1bd029ee50572fd962508e840 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 21:34:23 +0900 Subject: [PATCH 34/80] =?UTF-8?q?feat:=20=EB=8D=B1=20=EC=83=81=ED=83=9C=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=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 Co-authored-by: woo-chang --- README.md | 6 +++--- src/test/java/blackjack/domain/DeckTest.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f1d471aa93..ed30935ac4 100644 --- a/README.md +++ b/README.md @@ -69,10 +69,10 @@ graph TD ### 덱 -- [ ] 카드 목록을 가진다. +- [x] 카드 목록을 가진다. - [ ] 카드를 섞는다. -- [ ] 카드를 뽑는다. - - [ ] 카드가 없으면 뽑을 수 없다. +- [x] 카드를 뽑는다. + - [x] 카드가 없으면 뽑을 수 없다. ### 입력 diff --git a/src/test/java/blackjack/domain/DeckTest.java b/src/test/java/blackjack/domain/DeckTest.java index 93f669d146..b7b7d3316d 100644 --- a/src/test/java/blackjack/domain/DeckTest.java +++ b/src/test/java/blackjack/domain/DeckTest.java @@ -3,6 +3,7 @@ import static blackjack.domain.Number.ACE; import static blackjack.domain.Suit.SPADE; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.Stack; import org.junit.jupiter.api.DisplayNameGeneration; @@ -23,6 +24,16 @@ class DeckTest { assertThat(deck.draw()).isEqualTo(card); } + + @Test + void 카드가_존재하지_않으면_예외를_던진다() { + final Stack cards = new Stack<>(); + + final Deck deck = new Deck(cards); + + assertThatThrownBy(deck::draw) + .isInstanceOf(IllegalStateException.class); + } } class Deck { @@ -34,6 +45,9 @@ public Deck(final Stack cards) { } public Card draw() { + if (cards.empty()) { + throw new IllegalStateException("덱에 더 이상의 카드가 없습니다."); + } return cards.pop(); } } From 6976a0d9225e71562092bf43add9a318e0ae3e70 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 21:36:49 +0900 Subject: [PATCH 35/80] =?UTF-8?q?feat:=20=EB=8D=B1=20=EC=84=9E=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 Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/domain/Deck.java | 24 ++++++++++++++++++++ src/test/java/blackjack/domain/DeckTest.java | 16 ------------- 3 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 src/main/java/blackjack/domain/Deck.java diff --git a/README.md b/README.md index ed30935ac4..db7bffae8e 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ graph TD ### 덱 - [x] 카드 목록을 가진다. -- [ ] 카드를 섞는다. +- [x] 카드를 섞는다. - [x] 카드를 뽑는다. - [x] 카드가 없으면 뽑을 수 없다. diff --git a/src/main/java/blackjack/domain/Deck.java b/src/main/java/blackjack/domain/Deck.java new file mode 100644 index 0000000000..720843b4bc --- /dev/null +++ b/src/main/java/blackjack/domain/Deck.java @@ -0,0 +1,24 @@ +package blackjack.domain; + +import java.util.Collections; +import java.util.Stack; + +public class Deck { + + private final Stack cards; + + public Deck(final Stack cards) { + this.cards = cards; + } + + public Card draw() { + if (cards.empty()) { + throw new IllegalStateException("덱에 더 이상의 카드가 없습니다."); + } + return cards.pop(); + } + + public void shuffle() { + Collections.shuffle(cards); + } +} diff --git a/src/test/java/blackjack/domain/DeckTest.java b/src/test/java/blackjack/domain/DeckTest.java index b7b7d3316d..174062dc27 100644 --- a/src/test/java/blackjack/domain/DeckTest.java +++ b/src/test/java/blackjack/domain/DeckTest.java @@ -35,19 +35,3 @@ class DeckTest { .isInstanceOf(IllegalStateException.class); } } - -class Deck { - - private final Stack cards; - - public Deck(final Stack cards) { - this.cards = cards; - } - - public Card draw() { - if (cards.empty()) { - throw new IllegalStateException("덱에 더 이상의 카드가 없습니다."); - } - return cards.pop(); - } -} From 944cc8518d8fdc78204e3047de47e18f7b33ea8b Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 21:49:30 +0900 Subject: [PATCH 36/80] =?UTF-8?q?feat:=20=EB=8D=B1=20=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A6=AC=20=EB=8F=84=EB=A9=94=EC=9D=B8=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- .../java/blackjack/domain/DeckFactory.java | 16 +++++++++++ .../blackjack/domain/DeckFactoryTest.java | 27 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/main/java/blackjack/domain/DeckFactory.java create mode 100644 src/test/java/blackjack/domain/DeckFactoryTest.java diff --git a/src/main/java/blackjack/domain/DeckFactory.java b/src/main/java/blackjack/domain/DeckFactory.java new file mode 100644 index 0000000000..6452ae9da0 --- /dev/null +++ b/src/main/java/blackjack/domain/DeckFactory.java @@ -0,0 +1,16 @@ +package blackjack.domain; + +import java.util.Stack; + +public class DeckFactory { + + public static Deck createWithCount(final Stack pack, int count) { + final Stack cards = new Stack<>(); + + while (count-- > 0) { + cards.addAll(pack); + } + + return new Deck(cards); + } +} diff --git a/src/test/java/blackjack/domain/DeckFactoryTest.java b/src/test/java/blackjack/domain/DeckFactoryTest.java new file mode 100644 index 0000000000..e848eea7ec --- /dev/null +++ b/src/test/java/blackjack/domain/DeckFactoryTest.java @@ -0,0 +1,27 @@ +package blackjack.domain; + +import static blackjack.domain.Number.ACE; +import static blackjack.domain.Suit.SPADE; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.Stack; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class DeckFactoryTest { + + @Test + void 덱을_생성한다() { + final Stack pack = new Stack<>(); + pack.add(new Card(ACE, SPADE)); + + final Deck deck = DeckFactory.createWithCount(pack, 1); + deck.draw(); + + assertThatThrownBy(deck::draw) + .isInstanceOf(IllegalStateException.class); + } +} From dca7962194f0ffced3b8c5a6a709836ea2732924 Mon Sep 17 00:00:00 2001 From: kokodak Date: Thu, 2 Mar 2023 21:53:25 +0900 Subject: [PATCH 37/80] =?UTF-8?q?chore:=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20=EB=B0=8F=20import=20=EC=B5=9C=EC=A0=81?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- .../blackjack/domain/{ => card}/Card.java | 2 +- .../blackjack/domain/{ => card}/Cards.java | 2 +- .../blackjack/domain/{ => card}/Deck.java | 2 +- .../domain/{ => card}/DeckFactory.java | 2 +- .../blackjack/domain/{ => card}/Number.java | 2 +- .../blackjack/domain/{ => card}/Suit.java | 2 +- .../domain/{ => participant}/Dealer.java | 4 +++- .../domain/{ => participant}/Name.java | 2 +- .../domain/{ => participant}/Participant.java | 4 +++- .../{ => participant}/Participants.java | 2 +- .../domain/{ => participant}/Player.java | 3 ++- .../blackjack/domain/{ => card}/CardTest.java | 2 +- .../domain/{ => card}/CardsTest.java | 22 +++++++++---------- .../domain/{ => card}/DeckFactoryTest.java | 6 ++--- .../blackjack/domain/{ => card}/DeckTest.java | 6 ++--- .../domain/{ => participant}/DealerTest.java | 20 +++++++++-------- .../domain/{ => participant}/NameTest.java | 2 +- .../{ => participant}/ParticipantsTest.java | 8 ++++--- .../domain/{ => participant}/PlayerTest.java | 18 ++++++++------- 19 files changed, 61 insertions(+), 50 deletions(-) rename src/main/java/blackjack/domain/{ => card}/Card.java (94%) rename src/main/java/blackjack/domain/{ => card}/Cards.java (97%) rename src/main/java/blackjack/domain/{ => card}/Deck.java (93%) rename src/main/java/blackjack/domain/{ => card}/DeckFactory.java (90%) rename src/main/java/blackjack/domain/{ => card}/Number.java (94%) rename src/main/java/blackjack/domain/{ => card}/Suit.java (90%) rename src/main/java/blackjack/domain/{ => participant}/Dealer.java (90%) rename src/main/java/blackjack/domain/{ => participant}/Name.java (97%) rename src/main/java/blackjack/domain/{ => participant}/Participant.java (81%) rename src/main/java/blackjack/domain/{ => participant}/Participants.java (96%) rename src/main/java/blackjack/domain/{ => participant}/Player.java (91%) rename src/test/java/blackjack/domain/{ => card}/CardTest.java (98%) rename src/test/java/blackjack/domain/{ => card}/CardsTest.java (87%) rename src/test/java/blackjack/domain/{ => card}/DeckFactoryTest.java (84%) rename src/test/java/blackjack/domain/{ => card}/DeckTest.java (88%) rename src/test/java/blackjack/domain/{ => participant}/DealerTest.java (81%) rename src/test/java/blackjack/domain/{ => participant}/NameTest.java (96%) rename src/test/java/blackjack/domain/{ => participant}/ParticipantsTest.java (88%) rename src/test/java/blackjack/domain/{ => participant}/PlayerTest.java (82%) diff --git a/src/main/java/blackjack/domain/Card.java b/src/main/java/blackjack/domain/card/Card.java similarity index 94% rename from src/main/java/blackjack/domain/Card.java rename to src/main/java/blackjack/domain/card/Card.java index cd52841300..0ffedbbf67 100644 --- a/src/main/java/blackjack/domain/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; public class Card { diff --git a/src/main/java/blackjack/domain/Cards.java b/src/main/java/blackjack/domain/card/Cards.java similarity index 97% rename from src/main/java/blackjack/domain/Cards.java rename to src/main/java/blackjack/domain/card/Cards.java index 6a58390e7c..21f6c91bc6 100644 --- a/src/main/java/blackjack/domain/Cards.java +++ b/src/main/java/blackjack/domain/card/Cards.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; import java.util.ArrayList; import java.util.Collections; diff --git a/src/main/java/blackjack/domain/Deck.java b/src/main/java/blackjack/domain/card/Deck.java similarity index 93% rename from src/main/java/blackjack/domain/Deck.java rename to src/main/java/blackjack/domain/card/Deck.java index 720843b4bc..62bcf84d50 100644 --- a/src/main/java/blackjack/domain/Deck.java +++ b/src/main/java/blackjack/domain/card/Deck.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; import java.util.Collections; import java.util.Stack; diff --git a/src/main/java/blackjack/domain/DeckFactory.java b/src/main/java/blackjack/domain/card/DeckFactory.java similarity index 90% rename from src/main/java/blackjack/domain/DeckFactory.java rename to src/main/java/blackjack/domain/card/DeckFactory.java index 6452ae9da0..292b8dc067 100644 --- a/src/main/java/blackjack/domain/DeckFactory.java +++ b/src/main/java/blackjack/domain/card/DeckFactory.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; import java.util.Stack; diff --git a/src/main/java/blackjack/domain/Number.java b/src/main/java/blackjack/domain/card/Number.java similarity index 94% rename from src/main/java/blackjack/domain/Number.java rename to src/main/java/blackjack/domain/card/Number.java index beee6abcbd..caf3b8eb21 100644 --- a/src/main/java/blackjack/domain/Number.java +++ b/src/main/java/blackjack/domain/card/Number.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; public enum Number { diff --git a/src/main/java/blackjack/domain/Suit.java b/src/main/java/blackjack/domain/card/Suit.java similarity index 90% rename from src/main/java/blackjack/domain/Suit.java rename to src/main/java/blackjack/domain/card/Suit.java index df2117ca4d..20c01c81e6 100644 --- a/src/main/java/blackjack/domain/Suit.java +++ b/src/main/java/blackjack/domain/card/Suit.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; public enum Suit { diff --git a/src/main/java/blackjack/domain/Dealer.java b/src/main/java/blackjack/domain/participant/Dealer.java similarity index 90% rename from src/main/java/blackjack/domain/Dealer.java rename to src/main/java/blackjack/domain/participant/Dealer.java index 67a584464c..bbbf49053a 100644 --- a/src/main/java/blackjack/domain/Dealer.java +++ b/src/main/java/blackjack/domain/participant/Dealer.java @@ -1,4 +1,6 @@ -package blackjack.domain; +package blackjack.domain.participant; + +import blackjack.domain.card.Cards; public class Dealer extends Participant { diff --git a/src/main/java/blackjack/domain/Name.java b/src/main/java/blackjack/domain/participant/Name.java similarity index 97% rename from src/main/java/blackjack/domain/Name.java rename to src/main/java/blackjack/domain/participant/Name.java index 2905e18723..bf968ab4b3 100644 --- a/src/main/java/blackjack/domain/Name.java +++ b/src/main/java/blackjack/domain/participant/Name.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.participant; import java.util.Objects; diff --git a/src/main/java/blackjack/domain/Participant.java b/src/main/java/blackjack/domain/participant/Participant.java similarity index 81% rename from src/main/java/blackjack/domain/Participant.java rename to src/main/java/blackjack/domain/participant/Participant.java index 49ce00df3d..362b359c20 100644 --- a/src/main/java/blackjack/domain/Participant.java +++ b/src/main/java/blackjack/domain/participant/Participant.java @@ -1,5 +1,7 @@ -package blackjack.domain; +package blackjack.domain.participant; +import blackjack.domain.card.Card; +import blackjack.domain.card.Cards; import java.util.List; public abstract class Participant { diff --git a/src/main/java/blackjack/domain/Participants.java b/src/main/java/blackjack/domain/participant/Participants.java similarity index 96% rename from src/main/java/blackjack/domain/Participants.java rename to src/main/java/blackjack/domain/participant/Participants.java index 425750e38f..1288f36d57 100644 --- a/src/main/java/blackjack/domain/Participants.java +++ b/src/main/java/blackjack/domain/participant/Participants.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.participant; import java.util.HashSet; import java.util.List; diff --git a/src/main/java/blackjack/domain/Player.java b/src/main/java/blackjack/domain/participant/Player.java similarity index 91% rename from src/main/java/blackjack/domain/Player.java rename to src/main/java/blackjack/domain/participant/Player.java index 13aa4a6241..431708deec 100644 --- a/src/main/java/blackjack/domain/Player.java +++ b/src/main/java/blackjack/domain/participant/Player.java @@ -1,5 +1,6 @@ -package blackjack.domain; +package blackjack.domain.participant; +import blackjack.domain.card.Cards; import java.util.Objects; public class Player extends Participant { diff --git a/src/test/java/blackjack/domain/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java similarity index 98% rename from src/test/java/blackjack/domain/CardTest.java rename to src/test/java/blackjack/domain/card/CardTest.java index 4eb8825978..c60d07ef40 100644 --- a/src/test/java/blackjack/domain/CardTest.java +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.card; import static org.assertj.core.api.Assertions.assertThat; diff --git a/src/test/java/blackjack/domain/CardsTest.java b/src/test/java/blackjack/domain/card/CardsTest.java similarity index 87% rename from src/test/java/blackjack/domain/CardsTest.java rename to src/test/java/blackjack/domain/card/CardsTest.java index dc3e7a3b98..bd4f8c372b 100644 --- a/src/test/java/blackjack/domain/CardsTest.java +++ b/src/test/java/blackjack/domain/card/CardsTest.java @@ -1,14 +1,14 @@ -package blackjack.domain; - -import static blackjack.domain.Number.ACE; -import static blackjack.domain.Number.JACK; -import static blackjack.domain.Number.QUEEN; -import static blackjack.domain.Number.TEN; -import static blackjack.domain.Number.TWO; -import static blackjack.domain.Suit.CLOVER; -import static blackjack.domain.Suit.DIAMOND; -import static blackjack.domain.Suit.HEART; -import static blackjack.domain.Suit.SPADE; +package blackjack.domain.card; + +import static blackjack.domain.card.Number.ACE; +import static blackjack.domain.card.Number.JACK; +import static blackjack.domain.card.Number.QUEEN; +import static blackjack.domain.card.Number.TEN; +import static blackjack.domain.card.Number.TWO; +import static blackjack.domain.card.Suit.CLOVER; +import static blackjack.domain.card.Suit.DIAMOND; +import static blackjack.domain.card.Suit.HEART; +import static blackjack.domain.card.Suit.SPADE; import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayList; diff --git a/src/test/java/blackjack/domain/DeckFactoryTest.java b/src/test/java/blackjack/domain/card/DeckFactoryTest.java similarity index 84% rename from src/test/java/blackjack/domain/DeckFactoryTest.java rename to src/test/java/blackjack/domain/card/DeckFactoryTest.java index e848eea7ec..9fe2cc4f86 100644 --- a/src/test/java/blackjack/domain/DeckFactoryTest.java +++ b/src/test/java/blackjack/domain/card/DeckFactoryTest.java @@ -1,7 +1,7 @@ -package blackjack.domain; +package blackjack.domain.card; -import static blackjack.domain.Number.ACE; -import static blackjack.domain.Suit.SPADE; +import static blackjack.domain.card.Number.ACE; +import static blackjack.domain.card.Suit.SPADE; import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.Stack; diff --git a/src/test/java/blackjack/domain/DeckTest.java b/src/test/java/blackjack/domain/card/DeckTest.java similarity index 88% rename from src/test/java/blackjack/domain/DeckTest.java rename to src/test/java/blackjack/domain/card/DeckTest.java index 174062dc27..71729dd96b 100644 --- a/src/test/java/blackjack/domain/DeckTest.java +++ b/src/test/java/blackjack/domain/card/DeckTest.java @@ -1,7 +1,7 @@ -package blackjack.domain; +package blackjack.domain.card; -import static blackjack.domain.Number.ACE; -import static blackjack.domain.Suit.SPADE; +import static blackjack.domain.card.Number.ACE; +import static blackjack.domain.card.Suit.SPADE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; diff --git a/src/test/java/blackjack/domain/DealerTest.java b/src/test/java/blackjack/domain/participant/DealerTest.java similarity index 81% rename from src/test/java/blackjack/domain/DealerTest.java rename to src/test/java/blackjack/domain/participant/DealerTest.java index 2558a33c3c..59c77525e3 100644 --- a/src/test/java/blackjack/domain/DealerTest.java +++ b/src/test/java/blackjack/domain/participant/DealerTest.java @@ -1,15 +1,17 @@ -package blackjack.domain; +package blackjack.domain.participant; -import static blackjack.domain.Number.ACE; -import static blackjack.domain.Number.QUEEN; -import static blackjack.domain.Number.SEVEN; -import static blackjack.domain.Number.SIX; -import static blackjack.domain.Number.TWO; -import static blackjack.domain.Suit.CLOVER; -import static blackjack.domain.Suit.DIAMOND; -import static blackjack.domain.Suit.HEART; +import static blackjack.domain.card.Number.ACE; +import static blackjack.domain.card.Number.QUEEN; +import static blackjack.domain.card.Number.SEVEN; +import static blackjack.domain.card.Number.SIX; +import static blackjack.domain.card.Number.TWO; +import static blackjack.domain.card.Suit.CLOVER; +import static blackjack.domain.card.Suit.DIAMOND; +import static blackjack.domain.card.Suit.HEART; import static org.assertj.core.api.Assertions.assertThat; +import blackjack.domain.card.Card; +import blackjack.domain.card.Cards; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; diff --git a/src/test/java/blackjack/domain/NameTest.java b/src/test/java/blackjack/domain/participant/NameTest.java similarity index 96% rename from src/test/java/blackjack/domain/NameTest.java rename to src/test/java/blackjack/domain/participant/NameTest.java index d102b67d82..19feb58abb 100644 --- a/src/test/java/blackjack/domain/NameTest.java +++ b/src/test/java/blackjack/domain/participant/NameTest.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack.domain.participant; import static org.assertj.core.api.Assertions.assertThatThrownBy; diff --git a/src/test/java/blackjack/domain/ParticipantsTest.java b/src/test/java/blackjack/domain/participant/ParticipantsTest.java similarity index 88% rename from src/test/java/blackjack/domain/ParticipantsTest.java rename to src/test/java/blackjack/domain/participant/ParticipantsTest.java index 5e1026834f..bec337ab5e 100644 --- a/src/test/java/blackjack/domain/ParticipantsTest.java +++ b/src/test/java/blackjack/domain/participant/ParticipantsTest.java @@ -1,9 +1,11 @@ -package blackjack.domain; +package blackjack.domain.participant; -import static blackjack.domain.Number.ACE; -import static blackjack.domain.Suit.SPADE; +import static blackjack.domain.card.Number.ACE; +import static blackjack.domain.card.Suit.SPADE; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import blackjack.domain.card.Card; +import blackjack.domain.card.Cards; import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; diff --git a/src/test/java/blackjack/domain/PlayerTest.java b/src/test/java/blackjack/domain/participant/PlayerTest.java similarity index 82% rename from src/test/java/blackjack/domain/PlayerTest.java rename to src/test/java/blackjack/domain/participant/PlayerTest.java index fa74ef8b30..2b1ce6ab90 100644 --- a/src/test/java/blackjack/domain/PlayerTest.java +++ b/src/test/java/blackjack/domain/participant/PlayerTest.java @@ -1,14 +1,16 @@ -package blackjack.domain; +package blackjack.domain.participant; -import static blackjack.domain.Number.ACE; -import static blackjack.domain.Number.KING; -import static blackjack.domain.Number.QUEEN; -import static blackjack.domain.Number.TWO; -import static blackjack.domain.Suit.CLOVER; -import static blackjack.domain.Suit.DIAMOND; -import static blackjack.domain.Suit.HEART; +import static blackjack.domain.card.Number.ACE; +import static blackjack.domain.card.Number.KING; +import static blackjack.domain.card.Number.QUEEN; +import static blackjack.domain.card.Number.TWO; +import static blackjack.domain.card.Suit.CLOVER; +import static blackjack.domain.card.Suit.DIAMOND; +import static blackjack.domain.card.Suit.HEART; import static org.assertj.core.api.Assertions.assertThat; +import blackjack.domain.card.Card; +import blackjack.domain.card.Cards; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; From ec75c7954cc75b8f7ed3e27de68a6dcb844fc8e0 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 11:18:22 +0900 Subject: [PATCH 38/80] =?UTF-8?q?feat:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=ED=8C=8C=EC=8B=B1=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 Co-authored-by: woo-chang --- src/test/java/blackjack/view/ParserTest.java | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/test/java/blackjack/view/ParserTest.java diff --git a/src/test/java/blackjack/view/ParserTest.java b/src/test/java/blackjack/view/ParserTest.java new file mode 100644 index 0000000000..093949780c --- /dev/null +++ b/src/test/java/blackjack/view/ParserTest.java @@ -0,0 +1,40 @@ +package blackjack.view; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class ParserTest { + + @Test + void 구분자를_기준으로_문자열을_파싱한다() { + final String input = "pobi,jason"; + + final List result = Parser.parseByDelimiter(input, ","); + + assertThat(result).containsExactly("pobi", "jason"); + } + + @Test + void 구분자를_기준으로_빈문자열을_파싱한다() { + final String input = ",,"; + + final List result = Parser.parseByDelimiter(input, ","); + + assertThat(result).containsExactly("", "", ""); + } +} + +class Parser { + + public static List parseByDelimiter(final String value, final String delimiter) { + return Arrays.asList(value.split(delimiter, -1)); + } +} From d27fd5c58c0be58e5fa849e04b1eaf53113c3a92 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 11:29:41 +0900 Subject: [PATCH 39/80] =?UTF-8?q?feat:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=EC=95=9E=EB=92=A4=20=EA=B3=B5=EB=B0=B1=20=EC=A0=9C=EA=B1=B0=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 Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/view/Parser.java | 18 ++++++++++++++++++ src/test/java/blackjack/view/ParserTest.java | 20 ++++++++++---------- 3 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 src/main/java/blackjack/view/Parser.java diff --git a/README.md b/README.md index db7bffae8e..e5f7a4e46d 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ graph TD ### 입력 - [ ] 플레이어의 이름을 입력한다. - - [ ] 앞, 뒤 공백은 제거한다. + - [x] 앞, 뒤 공백은 제거한다. - [ ] 카드를 받을 여부를 입력한다. ### 출력 diff --git a/src/main/java/blackjack/view/Parser.java b/src/main/java/blackjack/view/Parser.java new file mode 100644 index 0000000000..22cb2822be --- /dev/null +++ b/src/main/java/blackjack/view/Parser.java @@ -0,0 +1,18 @@ +package blackjack.view; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Parser { + + public static List parseByDelimiter(final String value, final String delimiter) { + return Arrays.asList(value.split(delimiter, -1)); + } + + public static List trim(final List values) { + return values.stream() + .map(String::trim) + .collect(Collectors.toList()); + } +} diff --git a/src/test/java/blackjack/view/ParserTest.java b/src/test/java/blackjack/view/ParserTest.java index 093949780c..2c8322eeaa 100644 --- a/src/test/java/blackjack/view/ParserTest.java +++ b/src/test/java/blackjack/view/ParserTest.java @@ -2,9 +2,7 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @@ -15,26 +13,28 @@ class ParserTest { @Test void 구분자를_기준으로_문자열을_파싱한다() { - final String input = "pobi,jason"; + final String value = "pobi,jason"; - final List result = Parser.parseByDelimiter(input, ","); + final List result = Parser.parseByDelimiter(value, ","); assertThat(result).containsExactly("pobi", "jason"); } @Test void 구분자를_기준으로_빈문자열을_파싱한다() { - final String input = ",,"; + final String value = ",,"; - final List result = Parser.parseByDelimiter(input, ","); + final List result = Parser.parseByDelimiter(value, ","); assertThat(result).containsExactly("", "", ""); } -} -class Parser { + @Test + void 문자열의_앞뒤_공백은_제거한다() { + final List value = List.of(" pobi", "jason ", " crong "); + + final List result = Parser.trim(value); - public static List parseByDelimiter(final String value, final String delimiter) { - return Arrays.asList(value.split(delimiter, -1)); + assertThat(result).containsExactly("pobi", "jason", "crong"); } } From 9b81e50f81812eeaec9d5605ed93185f2f8e356b Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 11:31:13 +0900 Subject: [PATCH 40/80] =?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=EB=AA=A9=EB=A1=9D=20=EC=9E=85?= =?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 Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/view/InputView.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/view/InputView.java diff --git a/README.md b/README.md index e5f7a4e46d..d6d67cd335 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ graph TD ### 입력 -- [ ] 플레이어의 이름을 입력한다. +- [x] 플레이어의 이름을 입력한다. - [x] 앞, 뒤 공백은 제거한다. - [ ] 카드를 받을 여부를 입력한다. diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java new file mode 100644 index 0000000000..7c3794ab55 --- /dev/null +++ b/src/main/java/blackjack/view/InputView.java @@ -0,0 +1,18 @@ +package blackjack.view; + +import java.util.List; +import java.util.Scanner; + +public class InputView { + + private final Scanner scanner = new Scanner(System.in); + + public List readPlayerNames() { + System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); + + final String input = scanner.nextLine(); + final List names = Parser.parseByDelimiter(input, ","); + + return Parser.trim(names); + } +} From 6ad67e291d61c0fe6e9f47014773453550f50414 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 11:51:48 +0900 Subject: [PATCH 41/80] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=EB=B0=9B?= =?UTF-8?q?=EC=9D=84=20=EC=97=AC=EB=B6=80=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 Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/view/InputView.java | 38 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d6d67cd335..4c086b83f6 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ graph TD - [x] 플레이어의 이름을 입력한다. - [x] 앞, 뒤 공백은 제거한다. -- [ ] 카드를 받을 여부를 입력한다. +- [x] 카드를 받을 여부를 입력한다. ### 출력 diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index 7c3794ab55..68d3d523c3 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 java.util.Arrays; import java.util.List; import java.util.Scanner; @@ -15,4 +16,41 @@ public List readPlayerNames() { return Parser.trim(names); } + + public boolean readDrawState(final String name) { + System.out.println(name + "은(는) 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); + + final String input = scanner.nextLine(); + + return DrawState.from(input).getState(); + } + + enum DrawState { + + YES("y", true), + NO("n", false); + + private final String symbol; + private final boolean state; + + DrawState(final String symbol, final boolean state) { + this.symbol = symbol; + this.state = state; + } + + public static DrawState from(final String symbol) { + return Arrays.stream(values()) + .filter(drawState -> drawState.getSymbol().equals(symbol)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("잘못된 입력입니다.")); + } + + public String getSymbol() { + return symbol; + } + + public boolean getState() { + return state; + } + } } From 100a9cbae787513a49a4396e984ba4d13dc8665a Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 14:36:48 +0900 Subject: [PATCH 42/80] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90?= =?UTF-8?q?=EB=93=A4=EC=9D=98=20=EC=B9=B4=EB=93=9C=20=ED=98=84=ED=99=A9=20?= =?UTF-8?q?=EC=B6=9C=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 Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/view/OutputView.java | 76 ++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/view/OutputView.java diff --git a/README.md b/README.md index 4c086b83f6..64f0eda54c 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ graph TD ### 출력 -- [ ] 딜러와 플레이어의 카드 현황을 출력한다. +- [x] 딜러와 플레이어의 카드 현황을 출력한다. - [ ] 딜러 카드 추가 여부를 출력한다. - [ ] 최종 결과를 출력한다. - [ ] 최종 승패를 출력한다. diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java new file mode 100644 index 0000000000..8d40e2559d --- /dev/null +++ b/src/main/java/blackjack/view/OutputView.java @@ -0,0 +1,76 @@ +package blackjack.view; + +import static java.text.MessageFormat.format; + +import java.util.List; +import java.util.stream.Collectors; + +public class OutputView { + + public void printDealCards(final ParticipantResponse dealer, final List players, final int count) { + System.out.println(format("{0}와 {1}에게 {2}장을 나누었습니다.", + dealer.getName(), getPlayerNamesFormat(players), count)); + + printHandedCardsWithoutScore(dealer); + players.forEach(this::printHandedCardsWithoutScore); + } + + private String getPlayerNamesFormat(final List players) { + return players.stream() + .map(ParticipantResponse::getName) + .collect(Collectors.joining(", ")); + } + + public void printHandedCardsWithoutScore(final ParticipantResponse participant) { + System.out.println(getHandedCards(participant)); + } + + private String getHandedCards(final ParticipantResponse participant) { + final CardsResponse cardsResponse = participant.getCardsResponse(); + final List cardInfos = cardsResponse.getCardInfos(); + + return format("{0}카드: {1}", participant.getName(), getCardInfosFormat(cardInfos)); + } + + private String getCardInfosFormat(final List cardInfos) { + return String.join(", ", cardInfos); + } +} + +class ParticipantResponse { + + private final String name; + private final CardsResponse cardsResponse; + + public ParticipantResponse(final String name, final CardsResponse cardsResponse) { + this.name = name; + this.cardsResponse = cardsResponse; + } + + public String getName() { + return name; + } + + public CardsResponse getCardsResponse() { + return cardsResponse; + } +} + +class CardsResponse { + + private final int totalScore; + private final List cardInfos; + + public CardsResponse(final int totalScore, final List cardInfos) { + this.totalScore = totalScore; + this.cardInfos = cardInfos; + } + + public int getTotalScore() { + return totalScore; + } + + public List getCardInfos() { + return cardInfos; + } +} From 9511cc98f4c204e27ab3be5a1446b40504251025 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 14:37:52 +0900 Subject: [PATCH 43/80] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80=20=EC=97=AC=EB=B6=80=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 Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/view/OutputView.java | 25 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 64f0eda54c..f3138b47b3 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,6 @@ graph TD ### 출력 - [x] 딜러와 플레이어의 카드 현황을 출력한다. -- [ ] 딜러 카드 추가 여부를 출력한다. +- [x] 딜러 카드 추가 여부를 출력한다. - [ ] 최종 결과를 출력한다. - [ ] 최종 승패를 출력한다. diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 8d40e2559d..870e4e0b4a 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -35,6 +35,31 @@ private String getHandedCards(final ParticipantResponse participant) { private String getCardInfosFormat(final List cardInfos) { return String.join(", ", cardInfos); } + + public void printDealerCardDrawn(final DealerStateResponse dealerStateResponse) { + if (dealerStateResponse.isDrawable()) { + System.out.println(format("딜러는 {0}이하라 한장의 카드를 더 받았습니다.", dealerStateResponse.getLimit())); + } + } +} + +class DealerStateResponse { + + private final boolean drawable; + private final int limit; + + public DealerStateResponse(final boolean drawable, final int limit) { + this.drawable = drawable; + this.limit = limit; + } + + public boolean isDrawable() { + return drawable; + } + + public int getLimit() { + return limit; + } } class ParticipantResponse { From 4e929add672aa82dbfc1d8ed262d54a9410441c0 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 14:43:09 +0900 Subject: [PATCH 44/80] =?UTF-8?q?feat:=20=EC=B5=9C=EC=A2=85=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=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 Co-authored-by: woo-chang --- README.md | 4 ++-- src/main/java/blackjack/view/OutputView.java | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f3138b47b3..8b1c3fb412 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,6 @@ graph TD ### 출력 - [x] 딜러와 플레이어의 카드 현황을 출력한다. -- [x] 딜러 카드 추가 여부를 출력한다. -- [ ] 최종 결과를 출력한다. +- [x] 딜러 카드 추현가 여부를 출력한다. +- [x] 최종 결과를 출력한다. - [ ] 최종 승패를 출력한다. diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 870e4e0b4a..f5c2f2a8ed 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -7,7 +7,8 @@ public class OutputView { - public void printDealCards(final ParticipantResponse dealer, final List players, final int count) { + public void printDealCards(final ParticipantResponse dealer, final List players, + final int count) { System.out.println(format("{0}와 {1}에게 {2}장을 나누었습니다.", dealer.getName(), getPlayerNamesFormat(players), count)); @@ -41,6 +42,17 @@ public void printDealerCardDrawn(final DealerStateResponse dealerStateResponse) System.out.println(format("딜러는 {0}이하라 한장의 카드를 더 받았습니다.", dealerStateResponse.getLimit())); } } + + public void printHandedCardsWithScore(final ParticipantResponse participant) { + final CardsResponse cardsResponse = participant.getCardsResponse(); + final int totalScore = cardsResponse.getTotalScore(); + + System.out.println(getHandedCards(participant) + " - " + getScoreFormat(totalScore)); + } + + private String getScoreFormat(final int totalScore) { + return format("결과: {0}", totalScore); + } } class DealerStateResponse { From a7f6e854c6f5ce3f119811eeb38693d768ece180 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 15:40:04 +0900 Subject: [PATCH 45/80] =?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 Co-authored-by: woo-chang --- README.md | 2 +- src/main/java/blackjack/view/OutputView.java | 94 +++++++++++++++++++- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8b1c3fb412..2021e313a9 100644 --- a/README.md +++ b/README.md @@ -85,4 +85,4 @@ graph TD - [x] 딜러와 플레이어의 카드 현황을 출력한다. - [x] 딜러 카드 추현가 여부를 출력한다. - [x] 최종 결과를 출력한다. -- [ ] 최종 승패를 출력한다. +- [x] 최종 승패를 출력한다. diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index f5c2f2a8ed..e8cddcf30c 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,12 +1,19 @@ package blackjack.view; +import static blackjack.view.Result.DRAW; +import static blackjack.view.Result.LOSE; +import static blackjack.view.Result.WIN; import static java.text.MessageFormat.format; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class OutputView { + private static final String RESULT_FORMAT = "{0}: {1}"; + public void printDealCards(final ParticipantResponse dealer, final List players, final int count) { System.out.println(format("{0}와 {1}에게 {2}장을 나누었습니다.", @@ -47,12 +54,97 @@ public void printHandedCardsWithScore(final ParticipantResponse participant) { final CardsResponse cardsResponse = participant.getCardsResponse(); final int totalScore = cardsResponse.getTotalScore(); - System.out.println(getHandedCards(participant) + " - " + getScoreFormat(totalScore)); + System.out.println(format("{0} - {1}", getHandedCards(participant), getScoreFormat(totalScore))); } private String getScoreFormat(final int totalScore) { return format("결과: {0}", totalScore); } + + public void printResult(final String dealerName, final List players) { + System.out.println("## 최종 승패"); + System.out.println(format(RESULT_FORMAT, dealerName, getDealerResult(players))); + players.forEach(this::printPlayerResult); + } + + private String getDealerResult(final List players) { + final Map playerResult = new HashMap<>(); + for (PlayerResultResponse player : players) { + final Result result = player.getResult(); + playerResult.put(result, playerResult.getOrDefault(result, 0) + 1); + } + + return format("{0}승 {1}무 {2}패", playerResult.get(LOSE), playerResult.get(DRAW), playerResult.get(WIN)); + } + + private void printPlayerResult(final PlayerResultResponse player) { + System.out.println(format(RESULT_FORMAT, player.getName(), player.getResult().getName())); + } +} + +enum Result { + + WIN("승"), + DRAW("무"), + LOSE("패"); + + private final String name; + + Result(final String name) { + this.name = name; + } + + public String getName() { + return name; + } +} + +class Score implements Comparable { + + private final int value; + + public Score(final int value) { + this.value = value; + } + + public Result compare(final Score score) { + final int result = this.compareTo(score); + if (result > 0) { + return WIN; + } + if (result < 0) { + return LOSE; + } + return DRAW; + } + + public int getValue() { + return value; + } + + @Override + public int compareTo(final Score score) { + return Integer.compare(value, score.getValue()); + } +} + +class PlayerResultResponse { + + private final String name; + private final Result result; + + public PlayerResultResponse(final String name, final Result result) { + this.name = name; + this.result = result; + } + + public String getName() { + return name; + } + + public Result getResult() { + return result; + } } class DealerStateResponse { From c7b445970ddaf74f4f917e7786bd0216e75fdf34 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:40:31 +0900 Subject: [PATCH 46/80] =?UTF-8?q?feat:=20=EB=B9=88=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=EB=A1=9C=20=EC=83=9D=EC=84=B1=EB=90=A0=20=EC=88=98=20?= =?UTF-8?q?=EC=9E=88=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 Co-authored-by: woo-chang --- .../java/blackjack/domain/card/Cards.java | 4 +++ .../blackjack/domain/participant/Dealer.java | 4 +++ .../domain/participant/Participant.java | 4 +++ .../domain/participant/ParticipantsTest.java | 32 +++++++------------ 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/main/java/blackjack/domain/card/Cards.java b/src/main/java/blackjack/domain/card/Cards.java index 21f6c91bc6..a53936c7e3 100644 --- a/src/main/java/blackjack/domain/card/Cards.java +++ b/src/main/java/blackjack/domain/card/Cards.java @@ -11,6 +11,10 @@ public class Cards { private final List cards; + public Cards() { + this(new ArrayList<>()); + } + public Cards(final List cards) { this.cards = new ArrayList<>(cards); } diff --git a/src/main/java/blackjack/domain/participant/Dealer.java b/src/main/java/blackjack/domain/participant/Dealer.java index bbbf49053a..6209980d46 100644 --- a/src/main/java/blackjack/domain/participant/Dealer.java +++ b/src/main/java/blackjack/domain/participant/Dealer.java @@ -10,6 +10,10 @@ public class Dealer extends Participant { private final String name = DEALER_NAME; + public Dealer() { + super(); + } + public Dealer(final Cards cards) { super(cards); } diff --git a/src/main/java/blackjack/domain/participant/Participant.java b/src/main/java/blackjack/domain/participant/Participant.java index 362b359c20..8e2be6d77c 100644 --- a/src/main/java/blackjack/domain/participant/Participant.java +++ b/src/main/java/blackjack/domain/participant/Participant.java @@ -8,6 +8,10 @@ public abstract class Participant { protected final Cards cards; + protected Participant() { + this(new Cards()); + } + protected Participant(final Cards cards) { this.cards = cards; } diff --git a/src/test/java/blackjack/domain/participant/ParticipantsTest.java b/src/test/java/blackjack/domain/participant/ParticipantsTest.java index bec337ab5e..3258147fd7 100644 --- a/src/test/java/blackjack/domain/participant/ParticipantsTest.java +++ b/src/test/java/blackjack/domain/participant/ParticipantsTest.java @@ -1,11 +1,7 @@ package blackjack.domain.participant; -import static blackjack.domain.card.Number.ACE; -import static blackjack.domain.card.Suit.SPADE; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import blackjack.domain.card.Card; -import blackjack.domain.card.Cards; import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -17,14 +13,11 @@ class ParticipantsTest { @Test void 중복되는_이름이_존재하면_예외를_던진다() { - final Cards cards = new Cards(List.of( - new Card(ACE, SPADE) - )); final List participants = List.of( - new Dealer(cards), - new Player("dazzle", cards), - new Player("dazzle", cards), - new Player("kokodak", cards)); + new Dealer(), + new Player("dazzle"), + new Player("dazzle"), + new Player("kokodak")); assertThatThrownBy(() -> new Participants(participants)) .isInstanceOf(IllegalArgumentException.class); @@ -32,17 +25,14 @@ class ParticipantsTest { @Test void 딜러_포함_참여자가_6명_초과라면_예외를_던진다() { - final Cards cards = new Cards(List.of( - new Card(ACE, SPADE) - )); final List participants = List.of( - new Dealer(cards), - new Player("dazzle", cards), - new Player("kokodak", cards), - new Player("odo", cards), - new Player("hoi", cards), - new Player("gray", cards), - new Player("pobi", cards) + new Dealer(), + new Player("dazzle"), + new Player("kokodak"), + new Player("odo"), + new Player("hoi"), + new Player("gray"), + new Player("pobi") ); assertThatThrownBy(() -> new Participants(participants)) From 7de79aafae7bacf78039d86723122e7608f3c475 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:41:47 +0900 Subject: [PATCH 47/80] =?UTF-8?q?feat:=20Cards=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=EC=97=90=20=EB=8C=80=ED=95=9C=20DTO=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- .../blackjack/view/dto/CardsResponse.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/blackjack/view/dto/CardsResponse.java diff --git a/src/main/java/blackjack/view/dto/CardsResponse.java b/src/main/java/blackjack/view/dto/CardsResponse.java new file mode 100644 index 0000000000..521c4e57ec --- /dev/null +++ b/src/main/java/blackjack/view/dto/CardsResponse.java @@ -0,0 +1,34 @@ +package blackjack.view.dto; + +import blackjack.domain.card.Card; +import java.util.List; +import java.util.stream.Collectors; + +public class CardsResponse { + + private final int totalScore; + private final List cardInfos; + + private CardsResponse(final int totalScore, final List cardInfos) { + this.totalScore = totalScore; + this.cardInfos = cardInfos; + } + + public static CardsResponse of(final int totalScore, final List cards) { + return new CardsResponse(totalScore, getCardInfos(cards)); + } + + private static List getCardInfos(final List cards) { + return cards.stream() + .map(card -> card.getNumberName() + card.getSuitName()) + .collect(Collectors.toList()); + } + + public int getTotalScore() { + return totalScore; + } + + public List getCardInfos() { + return cardInfos; + } +} From dfa82809ba1d85b6d90639a78c8fa71261bb1b08 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:42:07 +0900 Subject: [PATCH 48/80] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=EC=97=90=20=EB=8C=80=ED=95=9C=20DTO=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- .../view/dto/DealerStateResponse.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/blackjack/view/dto/DealerStateResponse.java diff --git a/src/main/java/blackjack/view/dto/DealerStateResponse.java b/src/main/java/blackjack/view/dto/DealerStateResponse.java new file mode 100644 index 0000000000..b112798824 --- /dev/null +++ b/src/main/java/blackjack/view/dto/DealerStateResponse.java @@ -0,0 +1,20 @@ +package blackjack.view.dto; + +public class DealerStateResponse { + + private final boolean drawable; + private final int limit; + + public DealerStateResponse(final boolean drawable, final int limit) { + this.drawable = drawable; + this.limit = limit; + } + + public boolean isDrawable() { + return drawable; + } + + public int getLimit() { + return limit; + } +} From f3808afc9b2df7c402f33ab219ed946d23259656 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:42:40 +0900 Subject: [PATCH 49/80] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20DTO=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- .../view/dto/ParticipantResponse.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/blackjack/view/dto/ParticipantResponse.java diff --git a/src/main/java/blackjack/view/dto/ParticipantResponse.java b/src/main/java/blackjack/view/dto/ParticipantResponse.java new file mode 100644 index 0000000000..e9efca1584 --- /dev/null +++ b/src/main/java/blackjack/view/dto/ParticipantResponse.java @@ -0,0 +1,28 @@ +package blackjack.view.dto; + +import blackjack.domain.participant.Participant; + +public class ParticipantResponse { + + private final String name; + private final CardsResponse cardsResponse; + + private ParticipantResponse(final String name, final CardsResponse cardsResponse) { + this.name = name; + this.cardsResponse = cardsResponse; + } + + public static ParticipantResponse from(final Participant participant) { + return new ParticipantResponse( + participant.getName(), CardsResponse.of(participant.getScore(), participant.getCards()) + ); + } + + public String getName() { + return name; + } + + public CardsResponse getCardsResponse() { + return cardsResponse; + } +} From a0c0aa330f9823cfc6f65ce84f53d27cb35fccde Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:43:05 +0900 Subject: [PATCH 50/80] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=20=EA=B2=B0=EA=B3=BC=EC=97=90=20=EB=8C=80=ED=95=9C=20?= =?UTF-8?q?DTO=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- .../view/dto/PlayerResultResponse.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/blackjack/view/dto/PlayerResultResponse.java diff --git a/src/main/java/blackjack/view/dto/PlayerResultResponse.java b/src/main/java/blackjack/view/dto/PlayerResultResponse.java new file mode 100644 index 0000000000..1a1aa254b2 --- /dev/null +++ b/src/main/java/blackjack/view/dto/PlayerResultResponse.java @@ -0,0 +1,22 @@ +package blackjack.view.dto; + +import blackjack.domain.result.Result; + +public class PlayerResultResponse { + + private final String name; + private final Result result; + + public PlayerResultResponse(final String name, final Result result) { + this.name = name; + this.result = result; + } + + public String getName() { + return name; + } + + public Result getResult() { + return result; + } +} From 8fe6bf6fe9b2d710f89d6c1abc3515d742609a43 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:43:45 +0900 Subject: [PATCH 51/80] =?UTF-8?q?feat:=20=EA=B2=B0=EA=B3=BC=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20=EC=97=B4=EA=B1=B0=ED=98=95=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- .../java/blackjack/domain/result/Result.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/blackjack/domain/result/Result.java 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..4b5a490497 --- /dev/null +++ b/src/main/java/blackjack/domain/result/Result.java @@ -0,0 +1,18 @@ +package blackjack.domain.result; + +public enum Result { + + WIN("승"), + DRAW("무"), + LOSE("패"); + + private final String name; + + Result(final String name) { + this.name = name; + } + + public String getName() { + return name; + } +} From e0c208d10deb2ed5449acb3e2e8f02b23fe9d5f8 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:44:09 +0900 Subject: [PATCH 52/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20=EB=8F=84=EB=A9=94=EC=9D=B8=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- .../java/blackjack/domain/result/Score.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/blackjack/domain/result/Score.java diff --git a/src/main/java/blackjack/domain/result/Score.java b/src/main/java/blackjack/domain/result/Score.java new file mode 100644 index 0000000000..998eb79040 --- /dev/null +++ b/src/main/java/blackjack/domain/result/Score.java @@ -0,0 +1,34 @@ +package blackjack.domain.result; + +import static blackjack.domain.result.Result.DRAW; +import static blackjack.domain.result.Result.LOSE; +import static blackjack.domain.result.Result.WIN; + +public class Score implements Comparable { + + private final int value; + + public Score(final int value) { + this.value = value; + } + + public Result compare(final Score score) { + final int result = this.compareTo(score); + if (result > 0) { + return WIN; + } + if (result < 0) { + return LOSE; + } + return DRAW; + } + + public int getValue() { + return value; + } + + @Override + public int compareTo(final Score score) { + return Integer.compare(value, score.getValue()); + } +} From 340f04889188b459ae8cf8fe513954db819210ba Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:45:28 +0900 Subject: [PATCH 53/80] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90?= =?UTF-8?q?=EB=93=A4=EC=9D=B4=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EB=BD=91?= =?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 Co-authored-by: woo-chang --- .../java/blackjack/domain/participant/Participants.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/blackjack/domain/participant/Participants.java b/src/main/java/blackjack/domain/participant/Participants.java index 1288f36d57..5c1e881016 100644 --- a/src/main/java/blackjack/domain/participant/Participants.java +++ b/src/main/java/blackjack/domain/participant/Participants.java @@ -1,8 +1,10 @@ package blackjack.domain.participant; +import blackjack.domain.card.Deck; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; public class Participants { @@ -34,6 +36,12 @@ private void validateDuplicate(final List participants) { } } + public void drawCard(final Deck deck, int count) { + while (count-- > 0) { + participants.forEach(participant -> participant.drawCard(deck.draw())); + } + } + public List getParticipants() { return participants; } From 53f7d744c2e030e0bb06ed38cc5aeb54129faf85 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:46:09 +0900 Subject: [PATCH 54/80] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90?= =?UTF-8?q?=EB=93=A4=20=EC=A4=91=20=EB=94=9C=EB=9F=AC=EC=99=80=20=ED=94=8C?= =?UTF-8?q?=EB=A0=88=EC=9D=B4=EC=96=B4=EB=A5=BC=20=EC=B0=BE=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 Co-authored-by: woo-chang --- .../domain/participant/Participants.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/blackjack/domain/participant/Participants.java b/src/main/java/blackjack/domain/participant/Participants.java index 5c1e881016..64435770a8 100644 --- a/src/main/java/blackjack/domain/participant/Participants.java +++ b/src/main/java/blackjack/domain/participant/Participants.java @@ -42,6 +42,21 @@ public void drawCard(final Deck deck, int count) { } } + public Dealer getDealer() { + return participants.stream() + .filter(Dealer.class::isInstance) + .map(Dealer.class::cast) + .findFirst() + .orElseThrow(() -> new IllegalStateException("딜러는 존재해야 합니다.")); + } + + public List getPlayers() { + return participants.stream() + .filter(Player.class::isInstance) + .map(Player.class::cast) + .collect(Collectors.toList()); + } + public List getParticipants() { return participants; } From 70230b292f9702b3e794d6fde0926b1cb1b8f82a Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:46:56 +0900 Subject: [PATCH 55/80] =?UTF-8?q?feat:=20=EC=9D=B4=EB=A6=84=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=EB=A7=8C=20=EC=83=9D=EC=84=B1=EB=90=A0=20=EC=88=98=20?= =?UTF-8?q?=EC=9E=88=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 Co-authored-by: woo-chang --- src/main/java/blackjack/domain/participant/Player.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/blackjack/domain/participant/Player.java b/src/main/java/blackjack/domain/participant/Player.java index 431708deec..e9db298f18 100644 --- a/src/main/java/blackjack/domain/participant/Player.java +++ b/src/main/java/blackjack/domain/participant/Player.java @@ -7,6 +7,10 @@ public class Player extends Participant { private final Name name; + public Player(final String name) { + this(name, new Cards()); + } + public Player(final String name, final Cards cards) { super(cards); this.name = new Name(name); From 0363e8ef36185a18ebd9655d880497bf577d571e Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 16:48:54 +0900 Subject: [PATCH 56/80] =?UTF-8?q?feat:=20=EB=8D=B1=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EB=BD=91=EC=95=84=20=EC=B0=B8?= =?UTF-8?q?=EA=B0=80=EC=9E=90=EB=93=A4=EC=97=90=EA=B2=8C=20=EB=82=98?= =?UTF-8?q?=EB=88=A0=EC=A3=BC=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 Co-authored-by: woo-chang --- .../java/blackjack/BlackJackApplication.java | 16 +++ .../controller/BlackJackController.java | 76 ++++++++++ src/main/java/blackjack/view/OutputView.java | 133 ++---------------- 3 files changed, 100 insertions(+), 125 deletions(-) create mode 100644 src/main/java/blackjack/BlackJackApplication.java create mode 100644 src/main/java/blackjack/controller/BlackJackController.java diff --git a/src/main/java/blackjack/BlackJackApplication.java b/src/main/java/blackjack/BlackJackApplication.java new file mode 100644 index 0000000000..6edf89d185 --- /dev/null +++ b/src/main/java/blackjack/BlackJackApplication.java @@ -0,0 +1,16 @@ +package blackjack; + +import blackjack.controller.BlackJackController; +import blackjack.view.InputView; +import blackjack.view.OutputView; + +public class BlackJackApplication { + + public static void main(String[] args) { + final InputView inputView = new InputView(); + final OutputView outputView = new OutputView(); + + final BlackJackController blackJackController = new BlackJackController(inputView, outputView); + blackJackController.run(); + } +} diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java new file mode 100644 index 0000000000..b14578b021 --- /dev/null +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -0,0 +1,76 @@ +package blackjack.controller; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Deck; +import blackjack.domain.card.DeckFactory; +import blackjack.domain.card.Number; +import blackjack.domain.card.Suit; +import blackjack.domain.participant.Dealer; +import blackjack.domain.participant.Participant; +import blackjack.domain.participant.Participants; +import blackjack.domain.participant.Player; +import blackjack.view.InputView; +import blackjack.view.OutputView; +import blackjack.view.dto.ParticipantResponse; +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; +import java.util.stream.Collectors; + +public class BlackJackController { + + private static final int INIT_DRAW_COUNT = 2; + private static final Stack TRUMP; + + static { + final Stack pack = new Stack<>(); + for (final Suit suit : Suit.values()) { + for (final Number number : Number.values()) { + pack.add(new Card(number, suit)); + } + } + TRUMP = pack; + } + + private final InputView inputView; + private final OutputView outputView; + + public BlackJackController(final InputView inputView, final OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public void run() { + final Participants participants = gatherParticipants(); + final Deck deck = DeckFactory.createWithCount(TRUMP, 1); + deck.shuffle(); + + } + + private Participants gatherParticipants() { + final List participants = new ArrayList<>(); + participants.add(new Dealer()); + participants.addAll(createPlayers()); + return new Participants(participants); + } + + private List createPlayers() { + final List playerNames = inputView.readPlayerNames(); + return playerNames.stream() + .map(Player::new) + .collect(Collectors.toList()); + } + + private void dealCards(Participants participants, Deck deck) { + participants.drawCard(deck, INIT_DRAW_COUNT); + final ParticipantResponse dealerResponse = ParticipantResponse.from(participants.getDealer()); + final List playerResponse = getPlayerResponse(participants.getPlayers()); + outputView.printDealCards(dealerResponse, playerResponse, INIT_DRAW_COUNT); + } + + private List getPlayerResponse(final List players) { + return players.stream() + .map(ParticipantResponse::from) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index e8cddcf30c..adeb87a038 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,10 +1,15 @@ package blackjack.view; -import static blackjack.view.Result.DRAW; -import static blackjack.view.Result.LOSE; -import static blackjack.view.Result.WIN; +import static blackjack.domain.result.Result.DRAW; +import static blackjack.domain.result.Result.LOSE; +import static blackjack.domain.result.Result.WIN; import static java.text.MessageFormat.format; +import blackjack.domain.result.Result; +import blackjack.view.dto.CardsResponse; +import blackjack.view.dto.DealerStateResponse; +import blackjack.view.dto.ParticipantResponse; +import blackjack.view.dto.PlayerResultResponse; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -81,125 +86,3 @@ private void printPlayerResult(final PlayerResultResponse player) { System.out.println(format(RESULT_FORMAT, player.getName(), player.getResult().getName())); } } - -enum Result { - - WIN("승"), - DRAW("무"), - LOSE("패"); - - private final String name; - - Result(final String name) { - this.name = name; - } - - public String getName() { - return name; - } -} - -class Score implements Comparable { - - private final int value; - - public Score(final int value) { - this.value = value; - } - - public Result compare(final Score score) { - final int result = this.compareTo(score); - if (result > 0) { - return WIN; - } - if (result < 0) { - return LOSE; - } - return DRAW; - } - - public int getValue() { - return value; - } - - @Override - public int compareTo(final Score score) { - return Integer.compare(value, score.getValue()); - } -} - -class PlayerResultResponse { - - private final String name; - private final Result result; - - public PlayerResultResponse(final String name, final Result result) { - this.name = name; - this.result = result; - } - - public String getName() { - return name; - } - - public Result getResult() { - return result; - } -} - -class DealerStateResponse { - - private final boolean drawable; - private final int limit; - - public DealerStateResponse(final boolean drawable, final int limit) { - this.drawable = drawable; - this.limit = limit; - } - - public boolean isDrawable() { - return drawable; - } - - public int getLimit() { - return limit; - } -} - -class ParticipantResponse { - - private final String name; - private final CardsResponse cardsResponse; - - public ParticipantResponse(final String name, final CardsResponse cardsResponse) { - this.name = name; - this.cardsResponse = cardsResponse; - } - - public String getName() { - return name; - } - - public CardsResponse getCardsResponse() { - return cardsResponse; - } -} - -class CardsResponse { - - private final int totalScore; - private final List cardInfos; - - public CardsResponse(final int totalScore, final List cardInfos) { - this.totalScore = totalScore; - this.cardInfos = cardInfos; - } - - public int getTotalScore() { - return totalScore; - } - - public List getCardInfos() { - return cardInfos; - } -} From bf2e4bf3894ba6dd78dfa8f78f35856a2bae108b Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 17:25:44 +0900 Subject: [PATCH 57/80] =?UTF-8?q?feat:=20=EB=B8=94=EB=9E=99=EC=9E=AD=20?= =?UTF-8?q?=EA=B2=8C=EC=9E=84=20=EC=8B=A4=ED=96=89=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 Co-authored-by: woo-chang --- .../controller/BlackJackController.java | 57 +++++++++++++++++++ .../domain/participant/Participant.java | 10 +++- src/main/java/blackjack/view/OutputView.java | 14 ++++- .../blackjack/view/dto/CardsResponse.java | 5 +- .../domain/participant/DealerTest.java | 2 +- .../domain/participant/PlayerTest.java | 2 +- 6 files changed, 83 insertions(+), 7 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index b14578b021..c94030f57a 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -9,9 +9,12 @@ import blackjack.domain.participant.Participant; import blackjack.domain.participant.Participants; import blackjack.domain.participant.Player; +import blackjack.domain.result.Score; import blackjack.view.InputView; import blackjack.view.OutputView; +import blackjack.view.dto.DealerStateResponse; import blackjack.view.dto.ParticipantResponse; +import blackjack.view.dto.PlayerResultResponse; import java.util.ArrayList; import java.util.List; import java.util.Stack; @@ -20,6 +23,7 @@ public class BlackJackController { private static final int INIT_DRAW_COUNT = 2; + private static final int DEALER_LIMIT = 16; private static final Stack TRUMP; static { @@ -43,14 +47,22 @@ public BlackJackController(final InputView inputView, final OutputView outputVie public void run() { final Participants participants = gatherParticipants(); final Deck deck = DeckFactory.createWithCount(TRUMP, 1); + deck.shuffle(); + dealCards(participants, deck); + + cardDraw(participants.getPlayers(), deck); + cardDraw(participants.getDealer(), deck); + printResult(participants); } private Participants gatherParticipants() { final List participants = new ArrayList<>(); + participants.add(new Dealer()); participants.addAll(createPlayers()); + return new Participants(participants); } @@ -63,8 +75,10 @@ private List createPlayers() { private void dealCards(Participants participants, Deck deck) { participants.drawCard(deck, INIT_DRAW_COUNT); + final ParticipantResponse dealerResponse = ParticipantResponse.from(participants.getDealer()); final List playerResponse = getPlayerResponse(participants.getPlayers()); + outputView.printDealCards(dealerResponse, playerResponse, INIT_DRAW_COUNT); } @@ -73,4 +87,47 @@ private List getPlayerResponse(final List players) .map(ParticipantResponse::from) .collect(Collectors.toList()); } + + private void cardDraw(final List players, final Deck deck) { + for (final Player player : players) { + cardDraw(player, deck); + } + } + + private void cardDraw(final Player player, final Deck deck) { + while (player.isDrawable() && inputView.readDrawState(player.getName())) { + player.drawCard(deck.draw()); + outputView.printHandedCardsWithoutScore(ParticipantResponse.from(player)); + } + } + + private void cardDraw(final Dealer dealer, final Deck deck) { + if (dealer.isDrawable()) { + dealer.drawCard(deck.draw()); + outputView.printDealerCardDrawn(new DealerStateResponse(true, DEALER_LIMIT)); + } + } + + private void printResult(final Participants participants) { + final List participantResponses = getParticipantResponses(participants.getParticipants()); + participantResponses.forEach(outputView::printHandedCardsWithScore); + + final Dealer dealer = participants.getDealer(); + + final List playerResultResponses = getPlayerResultResponses( + dealer.getScore(), participants.getPlayers()); + outputView.printResult(dealer.getName(), playerResultResponses); + } + + private List getParticipantResponses(final List participants) { + return participants.stream() + .map(ParticipantResponse::from) + .collect(Collectors.toList()); + } + + public List getPlayerResultResponses(final Score dealerScore, final List players) { + return players.stream() + .map(player -> new PlayerResultResponse(player.getName(), player.getWinningStatus(dealerScore))) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/blackjack/domain/participant/Participant.java b/src/main/java/blackjack/domain/participant/Participant.java index 8e2be6d77c..07a0715daf 100644 --- a/src/main/java/blackjack/domain/participant/Participant.java +++ b/src/main/java/blackjack/domain/participant/Participant.java @@ -2,6 +2,8 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Cards; +import blackjack.domain.result.Result; +import blackjack.domain.result.Score; import java.util.List; public abstract class Participant { @@ -20,8 +22,12 @@ public void drawCard(final Card card) { cards.addCard(card); } - public int getScore() { - return cards.calculateTotalScore(); + public Score getScore() { + return new Score(cards.calculateTotalScore()); + } + + public Result getWinningStatus(final Score score) { + return getScore().compare(score); } public List getCards() { diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index adeb87a038..c260662cdd 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -21,6 +21,7 @@ public class OutputView { public void printDealCards(final ParticipantResponse dealer, final List players, final int count) { + System.out.println(); System.out.println(format("{0}와 {1}에게 {2}장을 나누었습니다.", dealer.getName(), getPlayerNamesFormat(players), count)); @@ -51,7 +52,9 @@ private String getCardInfosFormat(final List cardInfos) { public void printDealerCardDrawn(final DealerStateResponse dealerStateResponse) { if (dealerStateResponse.isDrawable()) { + System.out.println(); System.out.println(format("딜러는 {0}이하라 한장의 카드를 더 받았습니다.", dealerStateResponse.getLimit())); + System.out.println(); } } @@ -67,13 +70,14 @@ private String getScoreFormat(final int totalScore) { } public void printResult(final String dealerName, final List players) { + System.out.println(); System.out.println("## 최종 승패"); System.out.println(format(RESULT_FORMAT, dealerName, getDealerResult(players))); players.forEach(this::printPlayerResult); } private String getDealerResult(final List players) { - final Map playerResult = new HashMap<>(); + final Map playerResult = initPlayerResult(); for (PlayerResultResponse player : players) { final Result result = player.getResult(); playerResult.put(result, playerResult.getOrDefault(result, 0) + 1); @@ -82,6 +86,14 @@ private String getDealerResult(final List players) { return format("{0}승 {1}무 {2}패", playerResult.get(LOSE), playerResult.get(DRAW), playerResult.get(WIN)); } + private Map initPlayerResult() { + final Map playerResult = new HashMap<>(); + for (final Result result : Result.values()) { + playerResult.put(result, 0); + } + return playerResult; + } + private void printPlayerResult(final PlayerResultResponse player) { System.out.println(format(RESULT_FORMAT, player.getName(), player.getResult().getName())); } diff --git a/src/main/java/blackjack/view/dto/CardsResponse.java b/src/main/java/blackjack/view/dto/CardsResponse.java index 521c4e57ec..50aa19a188 100644 --- a/src/main/java/blackjack/view/dto/CardsResponse.java +++ b/src/main/java/blackjack/view/dto/CardsResponse.java @@ -1,6 +1,7 @@ package blackjack.view.dto; import blackjack.domain.card.Card; +import blackjack.domain.result.Score; import java.util.List; import java.util.stream.Collectors; @@ -14,8 +15,8 @@ private CardsResponse(final int totalScore, final List cardInfos) { this.cardInfos = cardInfos; } - public static CardsResponse of(final int totalScore, final List cards) { - return new CardsResponse(totalScore, getCardInfos(cards)); + public static CardsResponse of(final Score score, final List cards) { + return new CardsResponse(score.getValue(), getCardInfos(cards)); } private static List getCardInfos(final List cards) { diff --git a/src/test/java/blackjack/domain/participant/DealerTest.java b/src/test/java/blackjack/domain/participant/DealerTest.java index 59c77525e3..c8dca2fe72 100644 --- a/src/test/java/blackjack/domain/participant/DealerTest.java +++ b/src/test/java/blackjack/domain/participant/DealerTest.java @@ -82,6 +82,6 @@ class DealerTest { )); final Dealer dealer = new Dealer(cards); - assertThat(dealer.getScore()).isEqualTo(15); + assertThat(dealer.getScore().getValue()).isEqualTo(15); } } diff --git a/src/test/java/blackjack/domain/participant/PlayerTest.java b/src/test/java/blackjack/domain/participant/PlayerTest.java index 2b1ce6ab90..2c96058b06 100644 --- a/src/test/java/blackjack/domain/participant/PlayerTest.java +++ b/src/test/java/blackjack/domain/participant/PlayerTest.java @@ -80,6 +80,6 @@ class PlayerTest { final Cards cards = new Cards(cardPack); final Player player = new Player("dazzle", cards); - assertThat(player.getScore()).isEqualTo(20); + assertThat(player.getScore().getValue()).isEqualTo(20); } } From 0ad7a728d15e34ecb433fc58443b15799ebe3110 Mon Sep 17 00:00:00 2001 From: kokodak Date: Fri, 3 Mar 2023 17:26:06 +0900 Subject: [PATCH 58/80] =?UTF-8?q?fix:=20=EA=B2=8C=EC=9E=84=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: woo-chang --- src/main/java/blackjack/domain/result/Score.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/blackjack/domain/result/Score.java b/src/main/java/blackjack/domain/result/Score.java index 998eb79040..5a8b9194cf 100644 --- a/src/main/java/blackjack/domain/result/Score.java +++ b/src/main/java/blackjack/domain/result/Score.java @@ -14,6 +14,11 @@ public Score(final int value) { public Result compare(final Score score) { final int result = this.compareTo(score); + if (value > 21) { + if (score.getValue() > 21) return DRAW; + return LOSE; + } + if (result > 0) { return WIN; } From 6518a499ff82e92a91f257a37fc2c13313d19b92 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Fri, 3 Mar 2023 20:16:33 +0900 Subject: [PATCH 59/80] =?UTF-8?q?refactor:=20TRUMP=EB=8A=94=20=EB=8D=B1?= =?UTF-8?q?=EC=9D=B4=20=EC=95=8C=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 17 +---------------- src/main/java/blackjack/domain/card/Deck.java | 12 ++++++++++++ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index c94030f57a..56bb17b781 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -1,10 +1,7 @@ package blackjack.controller; -import blackjack.domain.card.Card; import blackjack.domain.card.Deck; import blackjack.domain.card.DeckFactory; -import blackjack.domain.card.Number; -import blackjack.domain.card.Suit; import blackjack.domain.participant.Dealer; import blackjack.domain.participant.Participant; import blackjack.domain.participant.Participants; @@ -17,24 +14,12 @@ import blackjack.view.dto.PlayerResultResponse; import java.util.ArrayList; import java.util.List; -import java.util.Stack; import java.util.stream.Collectors; public class BlackJackController { private static final int INIT_DRAW_COUNT = 2; private static final int DEALER_LIMIT = 16; - private static final Stack TRUMP; - - static { - final Stack pack = new Stack<>(); - for (final Suit suit : Suit.values()) { - for (final Number number : Number.values()) { - pack.add(new Card(number, suit)); - } - } - TRUMP = pack; - } private final InputView inputView; private final OutputView outputView; @@ -46,7 +31,7 @@ public BlackJackController(final InputView inputView, final OutputView outputVie public void run() { final Participants participants = gatherParticipants(); - final Deck deck = DeckFactory.createWithCount(TRUMP, 1); + final Deck deck = DeckFactory.createWithCount(Deck.TRUMP, 1); deck.shuffle(); dealCards(participants, deck); diff --git a/src/main/java/blackjack/domain/card/Deck.java b/src/main/java/blackjack/domain/card/Deck.java index 62bcf84d50..2b5b4a64f2 100644 --- a/src/main/java/blackjack/domain/card/Deck.java +++ b/src/main/java/blackjack/domain/card/Deck.java @@ -5,6 +5,18 @@ public class Deck { + public static final Stack TRUMP; + + static { + final Stack pack = new Stack<>(); + for (final Suit suit : Suit.values()) { + for (final Number number : Number.values()) { + pack.add(new Card(number, suit)); + } + } + TRUMP = pack; + } + private final Stack cards; public Deck(final Stack cards) { From 24e9e03d07e3352dff0f503dd8a2c70e7b36995f Mon Sep 17 00:00:00 2001 From: woo-chang Date: Fri, 3 Mar 2023 20:33:06 +0900 Subject: [PATCH 60/80] =?UTF-8?q?refactor:=20=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B0=80=EB=8F=85=EC=84=B1=20=EC=9C=84?= =?UTF-8?q?=ED=95=B4=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=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 --- .../controller/BlackJackController.java | 2 +- src/main/java/blackjack/view/InputView.java | 29 +++++++++++-------- src/main/java/blackjack/view/Parser.java | 2 +- src/test/java/blackjack/view/ParserTest.java | 4 +-- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 56bb17b781..a314438549 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -80,7 +80,7 @@ private void cardDraw(final List players, final Deck deck) { } private void cardDraw(final Player player, final Deck deck) { - while (player.isDrawable() && inputView.readDrawState(player.getName())) { + while (player.isDrawable() && inputView.readMoreDraw(player.getName())) { player.drawCard(deck.draw()); outputView.printHandedCardsWithoutScore(ParticipantResponse.from(player)); } diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index 68d3d523c3..a64789c41a 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -1,5 +1,9 @@ package blackjack.view; +import static blackjack.view.InputView.MoreDraw.NO; +import static blackjack.view.InputView.MoreDraw.YES; +import static java.text.MessageFormat.format; + import java.util.Arrays; import java.util.List; import java.util.Scanner; @@ -12,20 +16,20 @@ public List readPlayerNames() { System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); final String input = scanner.nextLine(); - final List names = Parser.parseByDelimiter(input, ","); + final List names = Parser.split(input, ","); return Parser.trim(names); } - public boolean readDrawState(final String name) { - System.out.println(name + "은(는) 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); + public boolean readMoreDraw(final String name) { + System.out.println(format("{0}은(는) 한장의 카드를 더 받겠습니까?(예는 {1}, 아니오는 {2})", name, YES, NO)); final String input = scanner.nextLine(); - return DrawState.from(input).getState(); + return MoreDraw.from(input).getState(); } - enum DrawState { + enum MoreDraw { YES("y", true), NO("n", false); @@ -33,24 +37,25 @@ enum DrawState { private final String symbol; private final boolean state; - DrawState(final String symbol, final boolean state) { + MoreDraw(final String symbol, final boolean state) { this.symbol = symbol; this.state = state; } - public static DrawState from(final String symbol) { + public static MoreDraw from(final String symbol) { return Arrays.stream(values()) - .filter(drawState -> drawState.getSymbol().equals(symbol)) + .filter(moreDraw -> moreDraw.symbol.equals(symbol)) .findFirst() .orElseThrow(() -> new IllegalArgumentException("잘못된 입력입니다.")); } - public String getSymbol() { - return symbol; - } - public boolean getState() { return state; } + + @Override + public String toString() { + return symbol; + } } } diff --git a/src/main/java/blackjack/view/Parser.java b/src/main/java/blackjack/view/Parser.java index 22cb2822be..95cbe01174 100644 --- a/src/main/java/blackjack/view/Parser.java +++ b/src/main/java/blackjack/view/Parser.java @@ -6,7 +6,7 @@ public class Parser { - public static List parseByDelimiter(final String value, final String delimiter) { + public static List split(final String value, final String delimiter) { return Arrays.asList(value.split(delimiter, -1)); } diff --git a/src/test/java/blackjack/view/ParserTest.java b/src/test/java/blackjack/view/ParserTest.java index 2c8322eeaa..8fa4220973 100644 --- a/src/test/java/blackjack/view/ParserTest.java +++ b/src/test/java/blackjack/view/ParserTest.java @@ -15,7 +15,7 @@ class ParserTest { void 구분자를_기준으로_문자열을_파싱한다() { final String value = "pobi,jason"; - final List result = Parser.parseByDelimiter(value, ","); + final List result = Parser.split(value, ","); assertThat(result).containsExactly("pobi", "jason"); } @@ -24,7 +24,7 @@ class ParserTest { void 구분자를_기준으로_빈문자열을_파싱한다() { final String value = ",,"; - final List result = Parser.parseByDelimiter(value, ","); + final List result = Parser.split(value, ","); assertThat(result).containsExactly("", "", ""); } From 57cb277efe37c10d5cc2aa3bc2e2cfb5ec84cc47 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 15:12:28 +0900 Subject: [PATCH 61/80] =?UTF-8?q?refactor:=20=EC=B6=95=EC=95=BD=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EC=9D=80=20=EB=B3=80=EC=88=98=EB=AA=85?= =?UTF-8?q?=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/controller/BlackJackController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index a314438549..bd24e689ec 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -18,7 +18,7 @@ public class BlackJackController { - private static final int INIT_DRAW_COUNT = 2; + private static final int INITIAL_DRAW_COUNT = 2; private static final int DEALER_LIMIT = 16; private final InputView inputView; @@ -59,12 +59,12 @@ private List createPlayers() { } private void dealCards(Participants participants, Deck deck) { - participants.drawCard(deck, INIT_DRAW_COUNT); + participants.drawCard(deck, INITIAL_DRAW_COUNT); final ParticipantResponse dealerResponse = ParticipantResponse.from(participants.getDealer()); final List playerResponse = getPlayerResponse(participants.getPlayers()); - outputView.printDealCards(dealerResponse, playerResponse, INIT_DRAW_COUNT); + outputView.printDealCards(dealerResponse, playerResponse, INITIAL_DRAW_COUNT); } private List getPlayerResponse(final List players) { From 5f77913108f04b900f5cda0dd06aee56909e28d8 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 16:06:45 +0900 Subject: [PATCH 62/80] =?UTF-8?q?refactor:=20=EB=94=9C=EB=9F=AC=EA=B0=80?= =?UTF-8?q?=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EC=B6=94=EA=B0=80=EB=A1=9C=20?= =?UTF-8?q?=EB=BD=91=EC=9D=84=20=EC=88=98=20=EC=9E=88=EB=8A=94=20=EC=A0=90?= =?UTF-8?q?=EC=88=98=EB=8A=94=20=EB=94=9C=EB=9F=AC=EA=B0=80=20=EC=95=8C?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 7 ++-- .../blackjack/domain/participant/Dealer.java | 18 ++++----- src/main/java/blackjack/view/OutputView.java | 39 +++++++++++-------- .../view/dto/DealerStateResponse.java | 10 ++--- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index bd24e689ec..41bd3f5e2d 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -19,7 +19,6 @@ public class BlackJackController { private static final int INITIAL_DRAW_COUNT = 2; - private static final int DEALER_LIMIT = 16; private final InputView inputView; private final OutputView outputView; @@ -89,19 +88,19 @@ private void cardDraw(final Player player, final Deck deck) { private void cardDraw(final Dealer dealer, final Deck deck) { if (dealer.isDrawable()) { dealer.drawCard(deck.draw()); - outputView.printDealerCardDrawn(new DealerStateResponse(true, DEALER_LIMIT)); + outputView.printDealerDrawn(new DealerStateResponse(true, Dealer.MAXIMUM_DRAWABLE_SCORE)); } } private void printResult(final Participants participants) { final List participantResponses = getParticipantResponses(participants.getParticipants()); - participantResponses.forEach(outputView::printHandedCardsWithScore); + outputView.printHandedCardsWithScore(participantResponses); final Dealer dealer = participants.getDealer(); final List playerResultResponses = getPlayerResultResponses( dealer.getScore(), participants.getPlayers()); - outputView.printResult(dealer.getName(), playerResultResponses); + outputView.printFinalResult(dealer.getName(), playerResultResponses); } private List getParticipantResponses(final List participants) { diff --git a/src/main/java/blackjack/domain/participant/Dealer.java b/src/main/java/blackjack/domain/participant/Dealer.java index 6209980d46..bcbf89f07c 100644 --- a/src/main/java/blackjack/domain/participant/Dealer.java +++ b/src/main/java/blackjack/domain/participant/Dealer.java @@ -4,11 +4,11 @@ public class Dealer extends Participant { - private static final String DEALER_NAME = "딜러"; - private static final int MAXIMUM_CARD_COUNT = 2; - private static final int MAXIMUM_SCORE = 16; + public static final int MAXIMUM_DRAWABLE_SCORE = 16; + private static final String NAME = "딜러"; + private static final int MAXIMUM_DRAWABLE_CARD_COUNT = 2; - private final String name = DEALER_NAME; + private final String name = NAME; public Dealer() { super(); @@ -20,15 +20,15 @@ public Dealer(final Cards cards) { @Override public boolean isDrawable() { - return isCardShortage() && isScoreLow(); + return isDrawableCardCount() && isDrawableScore(); } - private boolean isCardShortage() { - return cards.count() <= MAXIMUM_CARD_COUNT; + private boolean isDrawableCardCount() { + return cards.count() <= MAXIMUM_DRAWABLE_CARD_COUNT; } - private boolean isScoreLow() { - return cards.calculateTotalScore() <= MAXIMUM_SCORE; + private boolean isDrawableScore() { + return cards.calculateTotalScore() <= MAXIMUM_DRAWABLE_SCORE; } @Override diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index c260662cdd..66cdf0cc49 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -17,16 +17,19 @@ public class OutputView { + private static final String LINE_SEPARATOR = System.lineSeparator(); private static final String RESULT_FORMAT = "{0}: {1}"; - public void printDealCards(final ParticipantResponse dealer, final List players, - final int count) { - System.out.println(); - System.out.println(format("{0}와 {1}에게 {2}장을 나누었습니다.", + public void printDealCards( + final ParticipantResponse dealer, final List players, final int count + ) { + System.out.println(LINE_SEPARATOR + format("{0}와 {1}에게 {2}장을 나누었습니다.", dealer.getName(), getPlayerNamesFormat(players), count)); printHandedCardsWithoutScore(dealer); players.forEach(this::printHandedCardsWithoutScore); + + System.out.println(); } private String getPlayerNamesFormat(final List players) { @@ -36,10 +39,10 @@ private String getPlayerNamesFormat(final List players) { } public void printHandedCardsWithoutScore(final ParticipantResponse participant) { - System.out.println(getHandedCards(participant)); + System.out.println(getHandedCardsWithoutScore(participant)); } - private String getHandedCards(final ParticipantResponse participant) { + private String getHandedCardsWithoutScore(final ParticipantResponse participant) { final CardsResponse cardsResponse = participant.getCardsResponse(); final List cardInfos = cardsResponse.getCardInfos(); @@ -50,28 +53,32 @@ private String getCardInfosFormat(final List cardInfos) { return String.join(", ", cardInfos); } - public void printDealerCardDrawn(final DealerStateResponse dealerStateResponse) { - if (dealerStateResponse.isDrawable()) { - System.out.println(); - System.out.println(format("딜러는 {0}이하라 한장의 카드를 더 받았습니다.", dealerStateResponse.getLimit())); - System.out.println(); + public void printDealerDrawn(final DealerStateResponse dealerStateResponse) { + if (dealerStateResponse.isDrawn()) { + System.out.println( + LINE_SEPARATOR + format("딜러는 {0}이하라 한장의 카드를 더 받았습니다.", dealerStateResponse.getLimit()) + ); } } - public void printHandedCardsWithScore(final ParticipantResponse participant) { + public void printHandedCardsWithScore(final List participantResponses) { + System.out.println(); + participantResponses.forEach(this::printHandedCardsWithScore); + } + + private void printHandedCardsWithScore(final ParticipantResponse participant) { final CardsResponse cardsResponse = participant.getCardsResponse(); final int totalScore = cardsResponse.getTotalScore(); - System.out.println(format("{0} - {1}", getHandedCards(participant), getScoreFormat(totalScore))); + System.out.println(format("{0} - {1}", getHandedCardsWithoutScore(participant), getScoreFormat(totalScore))); } private String getScoreFormat(final int totalScore) { return format("결과: {0}", totalScore); } - public void printResult(final String dealerName, final List players) { - System.out.println(); - System.out.println("## 최종 승패"); + public void printFinalResult(final String dealerName, final List players) { + System.out.println(LINE_SEPARATOR + "## 최종 승패"); System.out.println(format(RESULT_FORMAT, dealerName, getDealerResult(players))); players.forEach(this::printPlayerResult); } diff --git a/src/main/java/blackjack/view/dto/DealerStateResponse.java b/src/main/java/blackjack/view/dto/DealerStateResponse.java index b112798824..ffe733c4a2 100644 --- a/src/main/java/blackjack/view/dto/DealerStateResponse.java +++ b/src/main/java/blackjack/view/dto/DealerStateResponse.java @@ -2,16 +2,16 @@ public class DealerStateResponse { - private final boolean drawable; + private final boolean isDrawn; private final int limit; - public DealerStateResponse(final boolean drawable, final int limit) { - this.drawable = drawable; + public DealerStateResponse(final boolean isDrawn, final int limit) { + this.isDrawn = isDrawn; this.limit = limit; } - public boolean isDrawable() { - return drawable; + public boolean isDrawn() { + return isDrawn; } public int getLimit() { From f7eb06282fd62aab0d4d67817474321f6359e25a Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 16:10:37 +0900 Subject: [PATCH 63/80] =?UTF-8?q?refactor:=20ACE=20->=20Ace=20=EC=9D=BC?= =?UTF-8?q?=EA=B4=80=EC=84=B1=EC=9E=88=EB=8A=94=20=ED=91=9C=ED=98=84?= =?UTF-8?q?=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/test/java/blackjack/domain/card/CardsTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/blackjack/domain/card/CardsTest.java b/src/test/java/blackjack/domain/card/CardsTest.java index bd4f8c372b..70a473c2f8 100644 --- a/src/test/java/blackjack/domain/card/CardsTest.java +++ b/src/test/java/blackjack/domain/card/CardsTest.java @@ -25,14 +25,14 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class CardsTest { - private static Stream generateCardsWithoutACE() { + private static Stream generateCardsWithoutAce() { return Stream.of( Arguments.of(List.of(new Card(TWO, SPADE), new Card(JACK, HEART)), 12), Arguments.of(List.of(), 0) ); } - private static Stream generateCardsWithACE() { + private static Stream generateCardsWithAce() { return Stream.of( Arguments.of(List.of(new Card(ACE, SPADE), new Card(JACK, HEART)), 21), Arguments.of(List.of(new Card(ACE, SPADE), new Card(ACE, HEART), new Card(TEN, CLOVER)), 12), @@ -42,7 +42,7 @@ private static Stream generateCardsWithACE() { } @ParameterizedTest - @MethodSource("generateCardsWithoutACE") + @MethodSource("generateCardsWithoutAce") void 점수를_계산한다(final List cardPack, final int totalScore) { final Cards cards = new Cards(cardPack); @@ -50,7 +50,7 @@ private static Stream generateCardsWithACE() { } @ParameterizedTest - @MethodSource("generateCardsWithACE") + @MethodSource("generateCardsWithAce") void 에이스가_포함된_경우_최적의_점수를_계산한다(final List cardPack, final int totalScore) { final Cards cards = new Cards(cardPack); From 90d09d3ddc257730d83c8c2cc2fdce7615ce20a5 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 17:14:32 +0900 Subject: [PATCH 64/80] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EB=AA=A9=EC=A0=81=EC=9D=84=20=EB=AA=85=ED=99=95=ED=9E=88?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 21 +++++----- .../controller/BlackJackController.java | 14 +------ .../domain/participant/Participants.java | 42 ++++++++++++------- .../domain/participant/NameTest.java | 2 +- .../domain/participant/ParticipantsTest.java | 29 +++++++------ 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 2021e313a9..ffa3569dc0 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,12 @@ - 블랙잭 게임은 딜러와 플레이어가 진행한다. - 카드의 합이 21에 가장 가까운 숫자를 가지는 쪽이 이기는 게임이다. - 숫자 계산은 카드의 숫자를 기본으로 하되, - - Ace: 1 or 11 - - King, Queen, Jack: 10 + - Ace: 1 or 11 + - King, Queen, Jack: 10 - 게임을 시작하면, 딜러와 플레이어는 두 장의 카드를 지급받는다. - 플레이어는 카드 숫자의 합이 21 미만이라면 카드를 계속 뽑을 수 있다. - 딜러는 카드 숫자의 합이 16 이하인 경우에만 1장의 카드를 추가로 받아야 한다. - - 딜러는 최대 3장의 카드만 받을 수 있다. + - 딜러는 최대 3장의 카드만 받을 수 있다. - 게임이 완료되면, 각 플레이어의 승패를 출력한다. ## 도메인 다이어그램 @@ -46,15 +46,16 @@ graph TD - [x] 점수를 확인한다. - [x] 카드 추가 여부를 결정한다. - [x] 여러 명일 수 있다. - - [x] 중복되는 이름은 가질 수 없다. - - [x] 딜러 포함 최대 6명이다. + - [x] 딜러는 포함되어야 한다. + - [x] 중복되는 이름은 가질 수 없다. + - [x] 최대 6명이다. ### 플레이어 - [x] 이름을 가진다. - - [x] 최소 1자, 최대 10자까지 가능하다. - - [x] 중간 공백은 허용한다. - - [x] `딜러`라는 이름은 가질 수 없다. + - [x] 최소 1자, 최대 10자까지 가능하다. + - [x] 중간 공백은 허용한다. + - [x] `딜러`라는 이름은 가질 수 없다. ### 카드 @@ -72,12 +73,12 @@ graph TD - [x] 카드 목록을 가진다. - [x] 카드를 섞는다. - [x] 카드를 뽑는다. - - [x] 카드가 없으면 뽑을 수 없다. + - [x] 카드가 없으면 뽑을 수 없다. ### 입력 - [x] 플레이어의 이름을 입력한다. - - [x] 앞, 뒤 공백은 제거한다. + - [x] 앞, 뒤 공백은 제거한다. - [x] 카드를 받을 여부를 입력한다. ### 출력 diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 41bd3f5e2d..0e3ec71129 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -12,7 +12,6 @@ import blackjack.view.dto.DealerStateResponse; import blackjack.view.dto.ParticipantResponse; import blackjack.view.dto.PlayerResultResponse; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -29,7 +28,7 @@ public BlackJackController(final InputView inputView, final OutputView outputVie } public void run() { - final Participants participants = gatherParticipants(); + final Participants participants = Participants.of(new Dealer(), gatherPlayers()); final Deck deck = DeckFactory.createWithCount(Deck.TRUMP, 1); deck.shuffle(); @@ -41,16 +40,7 @@ public void run() { printResult(participants); } - private Participants gatherParticipants() { - final List participants = new ArrayList<>(); - - participants.add(new Dealer()); - participants.addAll(createPlayers()); - - return new Participants(participants); - } - - private List createPlayers() { + private List gatherPlayers() { final List playerNames = inputView.readPlayerNames(); return playerNames.stream() .map(Player::new) diff --git a/src/main/java/blackjack/domain/participant/Participants.java b/src/main/java/blackjack/domain/participant/Participants.java index 64435770a8..fa6b9de980 100644 --- a/src/main/java/blackjack/domain/participant/Participants.java +++ b/src/main/java/blackjack/domain/participant/Participants.java @@ -1,38 +1,48 @@ package blackjack.domain.participant; import blackjack.domain.card.Deck; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; public class Participants { - private static final int MAXIMUM_PARTICIPANT_COUNT = 6; + private static final int MAXIMUM_PLAYER_COUNT = 5; private final List participants; - public Participants(final List participants) { - validate(participants); - this.participants = List.copyOf(participants); + private Participants(final List participants) { + this.participants = participants; } - private void validate(final List participants) { - validateCount(participants); - validateDuplicate(participants); + public static Participants of(final Dealer dealer, final List players) { + validate(players); + + final List recruits = new ArrayList<>(); + recruits.add(dealer); + recruits.addAll(players); + + return new Participants(recruits); + } + + private static void validate(final List players) { + validateCount(players); + validateDuplicate(players); } - private void validateCount(final List participants) { - if (participants.size() > MAXIMUM_PARTICIPANT_COUNT) { - throw new IllegalArgumentException("참가자는 " + MAXIMUM_PARTICIPANT_COUNT + "명을 초과할 수 없습니다"); + private static void validateCount(final List players) { + if (players.size() > MAXIMUM_PLAYER_COUNT) { + throw new IllegalArgumentException("플레이어는 " + MAXIMUM_PLAYER_COUNT + "명을 초과할 수 없습니다"); } } - private void validateDuplicate(final List participants) { - final Set uniqueParticipants = new HashSet<>(participants); + private static void validateDuplicate(final List players) { + final HashSet uniquePlayers = new HashSet<>(players); - if (uniqueParticipants.size() != participants.size()) { - throw new IllegalArgumentException("참가자 이름은 중복될 수 없습니다."); + if (players.size() != uniquePlayers.size()) { + throw new IllegalArgumentException("플레이어 이름은 중복될 수 없습니다."); } } @@ -58,6 +68,6 @@ public List getPlayers() { } public List getParticipants() { - return participants; + return Collections.unmodifiableList(participants); } } diff --git a/src/test/java/blackjack/domain/participant/NameTest.java b/src/test/java/blackjack/domain/participant/NameTest.java index 19feb58abb..db67a70d4e 100644 --- a/src/test/java/blackjack/domain/participant/NameTest.java +++ b/src/test/java/blackjack/domain/participant/NameTest.java @@ -13,7 +13,7 @@ class NameTest { @Test - void 이름이_존재하지_않으면_예외를_던진다() { + void 이름이_공백이면_예외를_던실진다() { final String input = " "; assertThatThrownBy(() -> new Name(input)) diff --git a/src/test/java/blackjack/domain/participant/ParticipantsTest.java b/src/test/java/blackjack/domain/participant/ParticipantsTest.java index 3258147fd7..d1cca688db 100644 --- a/src/test/java/blackjack/domain/participant/ParticipantsTest.java +++ b/src/test/java/blackjack/domain/participant/ParticipantsTest.java @@ -12,30 +12,29 @@ class ParticipantsTest { @Test - void 중복되는_이름이_존재하면_예외를_던진다() { - final List participants = List.of( - new Dealer(), - new Player("dazzle"), - new Player("dazzle"), - new Player("kokodak")); + void 중복된_플레이어_이름이_존재하면_예외를_던진다() { + final Dealer dealer = new Dealer(); + final List players = List.of( + new Player("toney"), + new Player("toney")); - assertThatThrownBy(() -> new Participants(participants)) + assertThatThrownBy(() -> Participants.of(dealer, players)) .isInstanceOf(IllegalArgumentException.class); } @Test - void 딜러_포함_참여자가_6명_초과라면_예외를_던진다() { - final List participants = List.of( - new Dealer(), + void 플레이어가_5명_초과라면_예외를_던진다() { + final Dealer dealer = new Dealer(); + final List players = List.of( new Player("dazzle"), new Player("kokodak"), - new Player("odo"), - new Player("hoi"), - new Player("gray"), - new Player("pobi") + new Player("toney"), + new Player("pobi"), + new Player("crong"), + new Player("jason") ); - assertThatThrownBy(() -> new Participants(participants)) + assertThatThrownBy(() -> Participants.of(dealer, players)) .isInstanceOf(IllegalArgumentException.class); } } From 9bddfc324ef23119ae52caa2ace7fa6517e7bae0 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 17:41:27 +0900 Subject: [PATCH 65/80] =?UTF-8?q?refactor:=20=EA=B3=84=EC=B8=B5=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=98=EB=AF=B8=EB=A5=BC=20=EB=AA=85=ED=99=95=ED=95=98?= =?UTF-8?q?=EA=B2=8C=20=EC=A0=84=EB=8B=AC=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/participant/DealerTest.java | 3 -- .../domain/participant/PlayerTest.java | 42 ++++++++++--------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/test/java/blackjack/domain/participant/DealerTest.java b/src/test/java/blackjack/domain/participant/DealerTest.java index c8dca2fe72..fe460fa43f 100644 --- a/src/test/java/blackjack/domain/participant/DealerTest.java +++ b/src/test/java/blackjack/domain/participant/DealerTest.java @@ -58,7 +58,6 @@ class DealerTest { @Test void 카드를_받는다() { - //given final List cardPack = new ArrayList<>(List.of( new Card(QUEEN, CLOVER), new Card(SIX, HEART) @@ -66,10 +65,8 @@ class DealerTest { final Cards cards = new Cards(cardPack); final Dealer dealer = new Dealer(cards); - //when dealer.drawCard(new Card(ACE, DIAMOND)); - //then assertThat(dealer.isDrawable()).isFalse(); } diff --git a/src/test/java/blackjack/domain/participant/PlayerTest.java b/src/test/java/blackjack/domain/participant/PlayerTest.java index 2c96058b06..6bef836253 100644 --- a/src/test/java/blackjack/domain/participant/PlayerTest.java +++ b/src/test/java/blackjack/domain/participant/PlayerTest.java @@ -15,6 +15,7 @@ import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @SuppressWarnings("NonAsciiCharacters") @@ -32,31 +33,34 @@ class PlayerTest { assertThat(player.getName()).isEqualTo("dazzle"); } - @Test - void 카드를_뽑을_수_있으면_true_반환한다() { - final Cards cards = new Cards(List.of( - new Card(QUEEN, CLOVER), - new Card(QUEEN, HEART) - )); - final Player player = new Player("kokodak", cards); + @Nested + class isDrawable_메소드는 { - assertThat(player.isDrawable()).isTrue(); - } + @Test + void 점수가_21미만이면_true_반환한다() { + final Cards cards = new Cards(List.of( + new Card(QUEEN, CLOVER), + new Card(QUEEN, HEART) + )); //20점 + final Player player = new Player("kokodak", cards); - @Test - void 카드를_뽑을_수_없으면_false_반환한다() { - final Cards cards = new Cards(List.of( - new Card(QUEEN, CLOVER), - new Card(ACE, HEART) - )); - final Player player = new Player("kokodak", cards); + assertThat(player.isDrawable()).isTrue(); + } - assertThat(player.isDrawable()).isFalse(); + @Test + void 점수가_21이상이면_false_반환한다() { + final Cards cards = new Cards(List.of( + new Card(QUEEN, CLOVER), + new Card(ACE, HEART) + )); //21점 + final Player player = new Player("kokodak", cards); + + assertThat(player.isDrawable()).isFalse(); + } } @Test void 카드를_받는다() { - //given final List cardPack = new ArrayList<>(List.of( new Card(QUEEN, CLOVER), new Card(KING, HEART) @@ -64,10 +68,8 @@ class PlayerTest { final Cards cards = new Cards(cardPack); final Player player = new Player("dazzle", cards); - //when player.drawCard(new Card(TWO, DIAMOND)); - //then assertThat(player.isDrawable()).isFalse(); } From 8f294bbd735925a0fd70a44d4de1a2a3920c85a3 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 17:48:15 +0900 Subject: [PATCH 66/80] =?UTF-8?q?refactor:=20=EC=9E=90=EB=B0=94=20?= =?UTF-8?q?=EC=BB=A8=EB=B2=A4=EC=85=98=EC=97=90=20=EB=94=B0=EB=9D=BC=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 0e3ec71129..c90d7d5e4e 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -34,8 +34,8 @@ public void run() { deck.shuffle(); dealCards(participants, deck); - cardDraw(participants.getPlayers(), deck); - cardDraw(participants.getDealer(), deck); + drawCard(participants.getPlayers(), deck); + drawCard(participants.getDealer(), deck); printResult(participants); } @@ -62,20 +62,20 @@ private List getPlayerResponse(final List players) .collect(Collectors.toList()); } - private void cardDraw(final List players, final Deck deck) { + private void drawCard(final List players, final Deck deck) { for (final Player player : players) { - cardDraw(player, deck); + drawCard(player, deck); } } - private void cardDraw(final Player player, final Deck deck) { + private void drawCard(final Player player, final Deck deck) { while (player.isDrawable() && inputView.readMoreDraw(player.getName())) { player.drawCard(deck.draw()); outputView.printHandedCardsWithoutScore(ParticipantResponse.from(player)); } } - private void cardDraw(final Dealer dealer, final Deck deck) { + private void drawCard(final Dealer dealer, final Deck deck) { if (dealer.isDrawable()) { dealer.drawCard(deck.draw()); outputView.printDealerDrawn(new DealerStateResponse(true, Dealer.MAXIMUM_DRAWABLE_SCORE)); From ed5db59910d3c23c7b9220da8612a3074b4e3074 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 19:12:10 +0900 Subject: [PATCH 67/80] =?UTF-8?q?refactor:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EB=BD=91=EC=9D=84=20=EB=95=8C=20=EA=B2=80=EC=A6=9D=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/participant/Participant.java | 3 + .../domain/participant/DealerTest.java | 89 +++++++++++++------ .../domain/participant/PlayerTest.java | 38 +++++--- 3 files changed, 94 insertions(+), 36 deletions(-) diff --git a/src/main/java/blackjack/domain/participant/Participant.java b/src/main/java/blackjack/domain/participant/Participant.java index 07a0715daf..17efb6730a 100644 --- a/src/main/java/blackjack/domain/participant/Participant.java +++ b/src/main/java/blackjack/domain/participant/Participant.java @@ -19,6 +19,9 @@ protected Participant(final Cards cards) { } public void drawCard(final Card card) { + if (!isDrawable()) { + throw new IllegalStateException("더 이상 카드를 뽑을 수 없습니다."); + } cards.addCard(card); } diff --git a/src/test/java/blackjack/domain/participant/DealerTest.java b/src/test/java/blackjack/domain/participant/DealerTest.java index fe460fa43f..bdd222903b 100644 --- a/src/test/java/blackjack/domain/participant/DealerTest.java +++ b/src/test/java/blackjack/domain/participant/DealerTest.java @@ -9,6 +9,7 @@ import static blackjack.domain.card.Suit.DIAMOND; import static blackjack.domain.card.Suit.HEART; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import blackjack.domain.card.Card; import blackjack.domain.card.Cards; @@ -16,44 +17,80 @@ import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @SuppressWarnings("NonAsciiCharacters") @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class DealerTest { - @Test - void 카드를_뽑을_수_있으면_true_반환한다() { - final Cards cards = new Cards(List.of( - new Card(QUEEN, CLOVER), - new Card(SIX, HEART) - )); - final Dealer dealer = new Dealer(cards); + @Nested + class isDrawable_메서드는 { - assertThat(dealer.isDrawable()).isTrue(); - } + @Test + void 카드가_2장_이하이고_점수가_16점_이하라면_true_반환한다() { + final Cards cards = new Cards(List.of( + new Card(QUEEN, CLOVER), + new Card(SIX, HEART) + )); //16점 + final Dealer dealer = new Dealer(cards); - @Test - void 카드를_뽑을_수_없으면_false_반환한다() { - final Cards cards = new Cards(List.of( - new Card(QUEEN, CLOVER), - new Card(SEVEN, HEART) - )); - final Dealer dealer = new Dealer(cards); + assertThat(dealer.isDrawable()).isTrue(); + } - assertThat(dealer.isDrawable()).isFalse(); + @Test + void 카드가_2장_이하이고_점수가_16점_초과라면_false_반환한다() { + final Cards cards = new Cards(List.of( + new Card(QUEEN, CLOVER), + new Card(SEVEN, HEART) + )); //17점 + final Dealer dealer = new Dealer(cards); + + assertThat(dealer.isDrawable()).isFalse(); + } + + @Test + void 카드가_2장_초과라면_false_반환한다() { + final Cards cards = new Cards(List.of( + new Card(TWO, CLOVER), + new Card(SIX, HEART), + new Card(SEVEN, DIAMOND) + )); // 15점 + final Dealer dealer = new Dealer(cards); + + assertThat(dealer.isDrawable()).isFalse(); + } } - @Test - void 소지한_카드가_3장이라면_false_반환한다() { - final Cards cards = new Cards(List.of( - new Card(TWO, CLOVER), - new Card(SIX, HEART), - new Card(SEVEN, DIAMOND) - )); - final Dealer dealer = new Dealer(cards); + @Nested + class drawCard_메서드는 { - assertThat(dealer.isDrawable()).isFalse(); + @Test + void 카드를_받을_수_없는_상태라면_예외를_던진다() { + final List cardPack = new ArrayList<>(List.of( + new Card(QUEEN, CLOVER), + new Card(ACE, HEART) + )); + final Cards cards = new Cards(cardPack); + final Dealer dealer = new Dealer(cards); + + assertThatThrownBy(() -> dealer.drawCard(new Card(TWO, DIAMOND))) + .isInstanceOf(IllegalStateException.class); + } + + @Test + void 카드를_받을_수_있는_상태라면_카드를_받는다() { + final List cardPack = new ArrayList<>(List.of( + new Card(QUEEN, CLOVER), + new Card(SIX, HEART) + )); + final Cards cards = new Cards(cardPack); + final Dealer dealer = new Dealer(cards); + + dealer.drawCard(new Card(TWO, DIAMOND)); + + assertThat(dealer.isDrawable()).isFalse(); + } } @Test diff --git a/src/test/java/blackjack/domain/participant/PlayerTest.java b/src/test/java/blackjack/domain/participant/PlayerTest.java index 6bef836253..120ad2e5a8 100644 --- a/src/test/java/blackjack/domain/participant/PlayerTest.java +++ b/src/test/java/blackjack/domain/participant/PlayerTest.java @@ -8,6 +8,7 @@ import static blackjack.domain.card.Suit.DIAMOND; import static blackjack.domain.card.Suit.HEART; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import blackjack.domain.card.Card; import blackjack.domain.card.Cards; @@ -59,18 +60,35 @@ class isDrawable_메소드는 { } } - @Test - void 카드를_받는다() { - final List cardPack = new ArrayList<>(List.of( - new Card(QUEEN, CLOVER), - new Card(KING, HEART) - )); - final Cards cards = new Cards(cardPack); - final Player player = new Player("dazzle", cards); + @Nested + class drawCard_메서드는 { - player.drawCard(new Card(TWO, DIAMOND)); + @Test + void 카드를_받을_수_없는_상태라면_예외를_던진다() { + final List cardPack = new ArrayList<>(List.of( + new Card(QUEEN, CLOVER), + new Card(ACE, HEART) + )); + final Cards cards = new Cards(cardPack); + final Player player = new Player("dazzle", cards); + + assertThatThrownBy(() -> player.drawCard(new Card(TWO, DIAMOND))) + .isInstanceOf(IllegalStateException.class); + } - assertThat(player.isDrawable()).isFalse(); + @Test + void 카드를_받을_수_있는_상태라면_카드를_받는다() { + final List cardPack = new ArrayList<>(List.of( + new Card(QUEEN, CLOVER), + new Card(KING, HEART) + )); + final Cards cards = new Cards(cardPack); + final Player player = new Player("dazzle", cards); + + player.drawCard(new Card(TWO, DIAMOND)); + + assertThat(player.isDrawable()).isFalse(); + } } @Test From 0223fcada6af34bc06631237d9e909b7a2798e5d Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 19:37:02 +0900 Subject: [PATCH 68/80] =?UTF-8?q?refactor:=20NPE=20=EB=B0=A9=EC=A7=80=20?= =?UTF-8?q?=EB=B0=8F=20=EB=B3=80=EC=88=98,=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/participant/Name.java | 14 +++++++------- .../blackjack/domain/participant/NameTest.java | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/blackjack/domain/participant/Name.java b/src/main/java/blackjack/domain/participant/Name.java index bf968ab4b3..c09726fe3c 100644 --- a/src/main/java/blackjack/domain/participant/Name.java +++ b/src/main/java/blackjack/domain/participant/Name.java @@ -4,8 +4,8 @@ public class Name { - private static final int UPPER_BOUND = 10; private static final String RESTRICT = "딜러"; + private static final int MAXIMUM_LENGTH = 10; private final String value; @@ -17,23 +17,23 @@ public Name(final String value) { private void validate(final String value) { validateBlank(value); validateLength(value); - validateRestrictWord(value); + validateRestrict(value); } private void validateBlank(final String value) { if (value.isBlank()) { - throw new IllegalArgumentException("이름은 존재해야 합니다. 현재 이름: " + value); + throw new IllegalArgumentException("이름은 공백일 수 없습니다. 현재 이름: " + value); } } private void validateLength(final String value) { - if (value.length() > UPPER_BOUND) { - throw new IllegalArgumentException("이름은 " + UPPER_BOUND + "글자 이하여야 합니다. 현재 이름: " + value); + if (value.length() > MAXIMUM_LENGTH) { + throw new IllegalArgumentException("이름은 " + MAXIMUM_LENGTH + "글자 이하여야 합니다. 현재 이름: " + value); } } - private void validateRestrictWord(final String value) { - if (value.equals(RESTRICT)) { + private void validateRestrict(final String value) { + if (Objects.equals(RESTRICT, value)) { throw new IllegalArgumentException("이름은 " + RESTRICT + "일 수 없습니다. 현재 이름: " + value); } } diff --git a/src/test/java/blackjack/domain/participant/NameTest.java b/src/test/java/blackjack/domain/participant/NameTest.java index db67a70d4e..2918000861 100644 --- a/src/test/java/blackjack/domain/participant/NameTest.java +++ b/src/test/java/blackjack/domain/participant/NameTest.java @@ -13,7 +13,7 @@ class NameTest { @Test - void 이름이_공백이면_예외를_던실진다() { + void 이름이_공백이면_예외를_던진다() { final String input = " "; assertThatThrownBy(() -> new Name(input)) @@ -28,7 +28,7 @@ class NameTest { } @Test - void 이름이_딜러_라면_예외를_던진다() { + void 이름이_딜러라면_예외를_던진다() { final String input = "딜러"; assertThatThrownBy(() -> new Name(input)) From 7eeedc2564ee56684d6051e35a3c98bf2c45ae5d Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 19:44:30 +0900 Subject: [PATCH 69/80] =?UTF-8?q?refactor:=20=EC=B0=B8=EA=B0=80=EC=9E=90?= =?UTF-8?q?=EC=97=90=EA=B2=8C=20=EB=94=9C=EB=9F=AC=20=EC=97=AC=EB=B6=80?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=EB=A9=94=EC=8B=9C=EC=A7=80?= =?UTF-8?q?=EB=A5=BC=20=EB=8D=98=EC=A7=80=EB=8F=84=EB=A1=9D=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 --- src/main/java/blackjack/domain/participant/Dealer.java | 5 +++++ .../java/blackjack/domain/participant/Participant.java | 2 ++ .../java/blackjack/domain/participant/Participants.java | 4 ++-- src/main/java/blackjack/domain/participant/Player.java | 5 +++++ src/test/java/blackjack/domain/participant/DealerTest.java | 7 +++++++ src/test/java/blackjack/domain/participant/PlayerTest.java | 7 +++++++ 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/blackjack/domain/participant/Dealer.java b/src/main/java/blackjack/domain/participant/Dealer.java index bcbf89f07c..c58410d82b 100644 --- a/src/main/java/blackjack/domain/participant/Dealer.java +++ b/src/main/java/blackjack/domain/participant/Dealer.java @@ -31,6 +31,11 @@ private boolean isDrawableScore() { return cards.calculateTotalScore() <= MAXIMUM_DRAWABLE_SCORE; } + @Override + public boolean isDealer() { + return true; + } + @Override public String getName() { return name; diff --git a/src/main/java/blackjack/domain/participant/Participant.java b/src/main/java/blackjack/domain/participant/Participant.java index 17efb6730a..0f94b1a6fd 100644 --- a/src/main/java/blackjack/domain/participant/Participant.java +++ b/src/main/java/blackjack/domain/participant/Participant.java @@ -39,5 +39,7 @@ public List getCards() { public abstract boolean isDrawable(); + public abstract boolean isDealer(); + public abstract String getName(); } diff --git a/src/main/java/blackjack/domain/participant/Participants.java b/src/main/java/blackjack/domain/participant/Participants.java index fa6b9de980..fa2a7c422e 100644 --- a/src/main/java/blackjack/domain/participant/Participants.java +++ b/src/main/java/blackjack/domain/participant/Participants.java @@ -54,7 +54,7 @@ public void drawCard(final Deck deck, int count) { public Dealer getDealer() { return participants.stream() - .filter(Dealer.class::isInstance) + .filter(Participant::isDealer) .map(Dealer.class::cast) .findFirst() .orElseThrow(() -> new IllegalStateException("딜러는 존재해야 합니다.")); @@ -62,7 +62,7 @@ public Dealer getDealer() { public List getPlayers() { return participants.stream() - .filter(Player.class::isInstance) + .filter(participant -> !participant.isDealer()) .map(Player.class::cast) .collect(Collectors.toList()); } diff --git a/src/main/java/blackjack/domain/participant/Player.java b/src/main/java/blackjack/domain/participant/Player.java index e9db298f18..16294b8327 100644 --- a/src/main/java/blackjack/domain/participant/Player.java +++ b/src/main/java/blackjack/domain/participant/Player.java @@ -21,6 +21,11 @@ public boolean isDrawable() { return !cards.isMaximumScore() && !cards.isTotalScoreOver(); } + @Override + public boolean isDealer() { + return false; + } + @Override public String getName() { return name.getValue(); diff --git a/src/test/java/blackjack/domain/participant/DealerTest.java b/src/test/java/blackjack/domain/participant/DealerTest.java index bdd222903b..e31c72eead 100644 --- a/src/test/java/blackjack/domain/participant/DealerTest.java +++ b/src/test/java/blackjack/domain/participant/DealerTest.java @@ -118,4 +118,11 @@ class drawCard_메서드는 { assertThat(dealer.getScore().getValue()).isEqualTo(15); } + + @Test + void 딜러는_딜러이다() { + final Dealer dealer = new Dealer(); + + assertThat(dealer.isDealer()).isTrue(); + } } diff --git a/src/test/java/blackjack/domain/participant/PlayerTest.java b/src/test/java/blackjack/domain/participant/PlayerTest.java index 120ad2e5a8..39059ddde6 100644 --- a/src/test/java/blackjack/domain/participant/PlayerTest.java +++ b/src/test/java/blackjack/domain/participant/PlayerTest.java @@ -102,4 +102,11 @@ class drawCard_메서드는 { assertThat(player.getScore().getValue()).isEqualTo(20); } + + @Test + void 플레이어는_딜러가_아니다() { + final Player player = new Player("toney"); + + assertThat(player.isDealer()).isFalse(); + } } From 1a85ff7147d067eb9ecaf26e007c7c69fe8a9403 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 19:48:11 +0900 Subject: [PATCH 70/80] =?UTF-8?q?refactor:=20=EA=B1=B0=EC=A7=93=20?= =?UTF-8?q?=EC=84=B1=EA=B3=B5=EC=9D=84=20=EB=A7=89=EA=B8=B0=EC=9C=84?= =?UTF-8?q?=ED=95=B4=20=EA=B2=80=EC=A6=9D=EB=B6=80=EC=97=90=EB=8A=94=20?= =?UTF-8?q?=ED=95=98=EB=93=9C=EC=BD=94=EB=94=A9=20=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/blackjack/domain/card/CardTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/blackjack/domain/card/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java index c60d07ef40..db5ba82858 100644 --- a/src/test/java/blackjack/domain/card/CardTest.java +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -17,7 +17,7 @@ class CardTest { final Card card = new Card(number, suit); - assertThat(card.getSuitName()).isEqualTo(suit.getName()); + assertThat(card.getSuitName()).isEqualTo("스페이드"); } @Test @@ -27,7 +27,7 @@ class CardTest { final Card card = new Card(number, suit); - assertThat(card.getNumberName()).isEqualTo(number.getName()); + assertThat(card.getNumberName()).isEqualTo("A"); } @Test @@ -37,7 +37,7 @@ class CardTest { final Card card = new Card(number, suit); - assertThat(card.getScore()).isEqualTo(number.getScore()); + assertThat(card.getScore()).isEqualTo(1); } @Test From cf364cf7d39b068f33b8067c881340f0539f0db2 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 20:34:29 +0900 Subject: [PATCH 71/80] =?UTF-8?q?refactor:=20TRUMP=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EC=8B=9C=20flatMap=20=EC=82=AC=EC=9A=A9=20=EB=B0=8F=20?= =?UTF-8?q?=EB=8D=B1=20=EC=83=9D=EC=84=B1=20=EC=8B=9C=20=EC=84=9E=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 1 - src/main/java/blackjack/domain/card/Deck.java | 19 ++++++++++--------- .../java/blackjack/domain/card/DeckTest.java | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index c90d7d5e4e..0d7ef58da3 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -31,7 +31,6 @@ public void run() { final Participants participants = Participants.of(new Dealer(), gatherPlayers()); final Deck deck = DeckFactory.createWithCount(Deck.TRUMP, 1); - deck.shuffle(); dealCards(participants, deck); drawCard(participants.getPlayers(), deck); diff --git a/src/main/java/blackjack/domain/card/Deck.java b/src/main/java/blackjack/domain/card/Deck.java index 2b5b4a64f2..9f3d7bbcb4 100644 --- a/src/main/java/blackjack/domain/card/Deck.java +++ b/src/main/java/blackjack/domain/card/Deck.java @@ -1,7 +1,10 @@ package blackjack.domain.card; +import java.util.Arrays; import java.util.Collections; +import java.util.List; import java.util.Stack; +import java.util.stream.Collectors; public class Deck { @@ -9,17 +12,19 @@ public class Deck { static { final Stack pack = new Stack<>(); - for (final Suit suit : Suit.values()) { - for (final Number number : Number.values()) { - pack.add(new Card(number, suit)); - } - } + final List cards = Arrays.stream(Suit.values()) + .flatMap(suit -> Arrays.stream(Number.values()) + .map(number -> new Card(number, suit)) + ) + .collect(Collectors.toList()); + pack.addAll(cards); TRUMP = pack; } private final Stack cards; public Deck(final Stack cards) { + Collections.shuffle(cards); this.cards = cards; } @@ -29,8 +34,4 @@ public Card draw() { } return cards.pop(); } - - public void shuffle() { - Collections.shuffle(cards); - } } diff --git a/src/test/java/blackjack/domain/card/DeckTest.java b/src/test/java/blackjack/domain/card/DeckTest.java index 71729dd96b..28a1c500f5 100644 --- a/src/test/java/blackjack/domain/card/DeckTest.java +++ b/src/test/java/blackjack/domain/card/DeckTest.java @@ -18,7 +18,7 @@ class DeckTest { void 카드를_뽑는다() { final Stack cards = new Stack<>(); final Card card = new Card(ACE, SPADE); - cards.add(card); + cards.push(card); final Deck deck = new Deck(cards); From b15c4c2de3e827aa084807a85d92620ae5270ce4 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 20:37:04 +0900 Subject: [PATCH 72/80] =?UTF-8?q?refactor:=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=84=20=EB=AA=85=EC=8B=9C?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20Name=20->=20PlayerName=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/participant/Player.java | 4 ++-- .../domain/participant/{Name.java => PlayerName.java} | 8 ++++---- .../participant/{NameTest.java => PlayerNameTest.java} | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) rename src/main/java/blackjack/domain/participant/{Name.java => PlayerName.java} (89%) rename src/test/java/blackjack/domain/participant/{NameTest.java => PlayerNameTest.java} (84%) diff --git a/src/main/java/blackjack/domain/participant/Player.java b/src/main/java/blackjack/domain/participant/Player.java index 16294b8327..80d1dee7d6 100644 --- a/src/main/java/blackjack/domain/participant/Player.java +++ b/src/main/java/blackjack/domain/participant/Player.java @@ -5,7 +5,7 @@ public class Player extends Participant { - private final Name name; + private final PlayerName name; public Player(final String name) { this(name, new Cards()); @@ -13,7 +13,7 @@ public Player(final String name) { public Player(final String name, final Cards cards) { super(cards); - this.name = new Name(name); + this.name = new PlayerName(name); } @Override diff --git a/src/main/java/blackjack/domain/participant/Name.java b/src/main/java/blackjack/domain/participant/PlayerName.java similarity index 89% rename from src/main/java/blackjack/domain/participant/Name.java rename to src/main/java/blackjack/domain/participant/PlayerName.java index c09726fe3c..21c86d55be 100644 --- a/src/main/java/blackjack/domain/participant/Name.java +++ b/src/main/java/blackjack/domain/participant/PlayerName.java @@ -2,14 +2,14 @@ import java.util.Objects; -public class Name { +public class PlayerName { private static final String RESTRICT = "딜러"; private static final int MAXIMUM_LENGTH = 10; private final String value; - public Name(final String value) { + public PlayerName(final String value) { validate(value); this.value = value; } @@ -50,8 +50,8 @@ public boolean equals(final Object o) { if (o == null || getClass() != o.getClass()) { return false; } - Name name = (Name) o; - return Objects.equals(value, name.value); + PlayerName playerName = (PlayerName) o; + return Objects.equals(value, playerName.value); } @Override diff --git a/src/test/java/blackjack/domain/participant/NameTest.java b/src/test/java/blackjack/domain/participant/PlayerNameTest.java similarity index 84% rename from src/test/java/blackjack/domain/participant/NameTest.java rename to src/test/java/blackjack/domain/participant/PlayerNameTest.java index 2918000861..bf09773193 100644 --- a/src/test/java/blackjack/domain/participant/NameTest.java +++ b/src/test/java/blackjack/domain/participant/PlayerNameTest.java @@ -10,20 +10,20 @@ @SuppressWarnings("NonAsciiCharacters") @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) -class NameTest { +class PlayerNameTest { @Test void 이름이_공백이면_예외를_던진다() { final String input = " "; - assertThatThrownBy(() -> new Name(input)) + assertThatThrownBy(() -> new PlayerName(input)) .isInstanceOf(IllegalArgumentException.class); } @ParameterizedTest @ValueSource(strings = {"", "abcdeabcdea"}) void 이름은_1자_이상_10자_이하가_아니라면_예외를_던진다(final String input) { - assertThatThrownBy(() -> new Name(input)) + assertThatThrownBy(() -> new PlayerName(input)) .isInstanceOf(IllegalArgumentException.class); } @@ -31,7 +31,7 @@ class NameTest { void 이름이_딜러라면_예외를_던진다() { final String input = "딜러"; - assertThatThrownBy(() -> new Name(input)) + assertThatThrownBy(() -> new PlayerName(input)) .isInstanceOf(IllegalArgumentException.class); } } From 6506c3108e50602118c0a9ffeef3726d4a7c191e Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 21:02:54 +0900 Subject: [PATCH 73/80] =?UTF-8?q?refactor:=20=EC=B0=B8=EC=97=AC=EC=9E=90?= =?UTF-8?q?=20=EB=AA=A9=EB=A1=9D=20=EC=83=9D=EC=84=B1=20=EC=B1=85=EC=9E=84?= =?UTF-8?q?=EC=9D=84=20=EC=83=9D=EC=84=B1=EC=9E=90=EB=A1=9C=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 --- .../controller/BlackJackController.java | 2 +- .../domain/participant/Participants.java | 18 +++++------ .../domain/participant/ParticipantsTest.java | 31 ++++++++++++++++--- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 0d7ef58da3..1b0434f0bf 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -28,7 +28,7 @@ public BlackJackController(final InputView inputView, final OutputView outputVie } public void run() { - final Participants participants = Participants.of(new Dealer(), gatherPlayers()); + final Participants participants = new Participants(new Dealer(), gatherPlayers()); final Deck deck = DeckFactory.createWithCount(Deck.TRUMP, 1); dealCards(participants, deck); diff --git a/src/main/java/blackjack/domain/participant/Participants.java b/src/main/java/blackjack/domain/participant/Participants.java index fa2a7c422e..c7164afc5d 100644 --- a/src/main/java/blackjack/domain/participant/Participants.java +++ b/src/main/java/blackjack/domain/participant/Participants.java @@ -13,32 +13,28 @@ public class Participants { private final List participants; - private Participants(final List participants) { - this.participants = participants; - } - - public static Participants of(final Dealer dealer, final List players) { + public Participants(final Dealer dealer, final List players) { validate(players); final List recruits = new ArrayList<>(); recruits.add(dealer); recruits.addAll(players); - return new Participants(recruits); + this.participants = recruits; } - private static void validate(final List players) { + private void validate(final List players) { validateCount(players); validateDuplicate(players); } - private static void validateCount(final List players) { + private void validateCount(final List players) { if (players.size() > MAXIMUM_PLAYER_COUNT) { throw new IllegalArgumentException("플레이어는 " + MAXIMUM_PLAYER_COUNT + "명을 초과할 수 없습니다"); } } - private static void validateDuplicate(final List players) { + private void validateDuplicate(final List players) { final HashSet uniquePlayers = new HashSet<>(players); if (players.size() != uniquePlayers.size()) { @@ -46,8 +42,8 @@ private static void validateDuplicate(final List players) { } } - public void drawCard(final Deck deck, int count) { - while (count-- > 0) { + public void drawCard(final Deck deck, final int count) { + for (int i = 0; i < count; i++) { participants.forEach(participant -> participant.drawCard(deck.draw())); } } diff --git a/src/test/java/blackjack/domain/participant/ParticipantsTest.java b/src/test/java/blackjack/domain/participant/ParticipantsTest.java index d1cca688db..1d537493f4 100644 --- a/src/test/java/blackjack/domain/participant/ParticipantsTest.java +++ b/src/test/java/blackjack/domain/participant/ParticipantsTest.java @@ -1,5 +1,6 @@ package blackjack.domain.participant; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.List; @@ -14,11 +15,9 @@ class ParticipantsTest { @Test void 중복된_플레이어_이름이_존재하면_예외를_던진다() { final Dealer dealer = new Dealer(); - final List players = List.of( - new Player("toney"), - new Player("toney")); + final List players = List.of(new Player("toney"), new Player("toney")); - assertThatThrownBy(() -> Participants.of(dealer, players)) + assertThatThrownBy(() -> new Participants(dealer, players)) .isInstanceOf(IllegalArgumentException.class); } @@ -34,7 +33,29 @@ class ParticipantsTest { new Player("jason") ); - assertThatThrownBy(() -> Participants.of(dealer, players)) + assertThatThrownBy(() -> new Participants(dealer, players)) .isInstanceOf(IllegalArgumentException.class); } + + @Test + void 딜러를_확인한다() { + final Dealer dealer = new Dealer(); + final List players = List.of(new Player("toney"), new Player("dazzle")); + + final Participants participants = new Participants(dealer, players); + + assertThat(participants.getDealer()).isEqualTo(dealer); + } + + @Test + void 플레이어를_확인한다() { + final Dealer dealer = new Dealer(); + final Player firstPlayer = new Player("toney"); + final Player secondPlayer = new Player("dazzle"); + final List players = List.of(firstPlayer, secondPlayer); + + final Participants participants = new Participants(dealer, players); + + assertThat(participants.getPlayers()).containsExactly(firstPlayer, secondPlayer); + } } From 60cd11c6ff79738d2b6457b38c8925d78e5f158b Mon Sep 17 00:00:00 2001 From: woo-chang Date: Sun, 5 Mar 2023 21:31:18 +0900 Subject: [PATCH 74/80] =?UTF-8?q?refactor:=20Deck=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EC=B1=85=EC=9E=84=EC=9D=80=20Deck=EC=9D=B4=20=EA=B0=80?= =?UTF-8?q?=EC=A7=80=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.java | 3 +- src/main/java/blackjack/domain/card/Deck.java | 8 +++++ .../blackjack/domain/card/DeckFactory.java | 16 --------- .../domain/card/DeckFactoryTest.java | 27 --------------- .../java/blackjack/domain/card/DeckTest.java | 33 +++++++++++-------- 5 files changed, 28 insertions(+), 59 deletions(-) delete mode 100644 src/main/java/blackjack/domain/card/DeckFactory.java delete mode 100644 src/test/java/blackjack/domain/card/DeckFactoryTest.java diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 1b0434f0bf..0d35efc248 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -1,7 +1,6 @@ package blackjack.controller; import blackjack.domain.card.Deck; -import blackjack.domain.card.DeckFactory; import blackjack.domain.participant.Dealer; import blackjack.domain.participant.Participant; import blackjack.domain.participant.Participants; @@ -29,7 +28,7 @@ public BlackJackController(final InputView inputView, final OutputView outputVie public void run() { final Participants participants = new Participants(new Dealer(), gatherPlayers()); - final Deck deck = DeckFactory.createWithCount(Deck.TRUMP, 1); + final Deck deck = Deck.createUsingTrump(1); dealCards(participants, deck); diff --git a/src/main/java/blackjack/domain/card/Deck.java b/src/main/java/blackjack/domain/card/Deck.java index 9f3d7bbcb4..f2b61f4c64 100644 --- a/src/main/java/blackjack/domain/card/Deck.java +++ b/src/main/java/blackjack/domain/card/Deck.java @@ -28,6 +28,14 @@ public Deck(final Stack cards) { this.cards = cards; } + public static Deck createUsingTrump(final int count) { + final Stack pack = new Stack<>(); + for (int i = 0; i < count; i++) { + pack.addAll(TRUMP); + } + return new Deck(pack); + } + public Card draw() { if (cards.empty()) { throw new IllegalStateException("덱에 더 이상의 카드가 없습니다."); diff --git a/src/main/java/blackjack/domain/card/DeckFactory.java b/src/main/java/blackjack/domain/card/DeckFactory.java deleted file mode 100644 index 292b8dc067..0000000000 --- a/src/main/java/blackjack/domain/card/DeckFactory.java +++ /dev/null @@ -1,16 +0,0 @@ -package blackjack.domain.card; - -import java.util.Stack; - -public class DeckFactory { - - public static Deck createWithCount(final Stack pack, int count) { - final Stack cards = new Stack<>(); - - while (count-- > 0) { - cards.addAll(pack); - } - - return new Deck(cards); - } -} diff --git a/src/test/java/blackjack/domain/card/DeckFactoryTest.java b/src/test/java/blackjack/domain/card/DeckFactoryTest.java deleted file mode 100644 index 9fe2cc4f86..0000000000 --- a/src/test/java/blackjack/domain/card/DeckFactoryTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package blackjack.domain.card; - -import static blackjack.domain.card.Number.ACE; -import static blackjack.domain.card.Suit.SPADE; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import java.util.Stack; -import org.junit.jupiter.api.DisplayNameGeneration; -import org.junit.jupiter.api.DisplayNameGenerator; -import org.junit.jupiter.api.Test; - -@SuppressWarnings("NonAsciiCharacters") -@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) -class DeckFactoryTest { - - @Test - void 덱을_생성한다() { - final Stack pack = new Stack<>(); - pack.add(new Card(ACE, SPADE)); - - final Deck deck = DeckFactory.createWithCount(pack, 1); - deck.draw(); - - assertThatThrownBy(deck::draw) - .isInstanceOf(IllegalStateException.class); - } -} diff --git a/src/test/java/blackjack/domain/card/DeckTest.java b/src/test/java/blackjack/domain/card/DeckTest.java index 28a1c500f5..cc77590554 100644 --- a/src/test/java/blackjack/domain/card/DeckTest.java +++ b/src/test/java/blackjack/domain/card/DeckTest.java @@ -8,30 +8,35 @@ import java.util.Stack; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @SuppressWarnings("NonAsciiCharacters") @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class DeckTest { - @Test - void 카드를_뽑는다() { - final Stack cards = new Stack<>(); - final Card card = new Card(ACE, SPADE); - cards.push(card); + @Nested + class draw_메서드는 { - final Deck deck = new Deck(cards); + @Test + void 카드가_남아있으면_카드를_뽑는다() { + final Stack cards = new Stack<>(); + final Card card = new Card(ACE, SPADE); + cards.push(card); - assertThat(deck.draw()).isEqualTo(card); - } + final Deck deck = new Deck(cards); + + assertThat(deck.draw()).isEqualTo(card); + } - @Test - void 카드가_존재하지_않으면_예외를_던진다() { - final Stack cards = new Stack<>(); + @Test + void 카드가_남아있지_않으면_예외를_던진다() { + final Stack cards = new Stack<>(); - final Deck deck = new Deck(cards); + final Deck deck = new Deck(cards); - assertThatThrownBy(deck::draw) - .isInstanceOf(IllegalStateException.class); + assertThatThrownBy(deck::draw) + .isInstanceOf(IllegalStateException.class); + } } } From 2458f44c798dcdb9346196b3e4d40e4e7d9fd6dd Mon Sep 17 00:00:00 2001 From: woo-chang Date: Mon, 6 Mar 2023 00:07:01 +0900 Subject: [PATCH 75/80] =?UTF-8?q?refactor:=20=EB=B8=94=EB=9E=99=EC=9E=AD?= =?UTF-8?q?=20=EA=B2=8C=EC=9E=84=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 21 +++++- .../controller/BlackJackController.java | 75 ++++++++++++++----- src/main/java/blackjack/domain/card/Deck.java | 2 +- .../blackjack/domain/participant/Dealer.java | 20 ++++- .../domain/participant/Participant.java | 12 +-- .../{result => participant}/Result.java | 12 ++- .../java/blackjack/domain/result/Score.java | 39 ---------- src/main/java/blackjack/view/OutputView.java | 32 +++----- .../blackjack/view/dto/CardsResponse.java | 15 +--- .../view/dto/DealerResultResponse.java | 23 ++++++ .../view/dto/ParticipantResponse.java | 10 +-- .../view/dto/PlayerResultResponse.java | 2 +- .../domain/participant/DealerTest.java | 2 +- .../domain/participant/PlayerTest.java | 2 +- 14 files changed, 145 insertions(+), 122 deletions(-) rename src/main/java/blackjack/domain/{result => participant}/Result.java (51%) delete mode 100644 src/main/java/blackjack/domain/result/Score.java create mode 100644 src/main/java/blackjack/view/dto/DealerResultResponse.java diff --git a/README.md b/README.md index ffa3569dc0..41d732ff26 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,19 @@ - 딜러는 최대 3장의 카드만 받을 수 있다. - 게임이 완료되면, 각 플레이어의 승패를 출력한다. +## 블랙잭 규칙 + +- `승`, `무`, `패`는 플레이어에 결과를 나타낸다. +- 플레이어 점수가 21점 초과일 때 + - 딜러 점수 상관없이 `패` +- 플레이어 점수가 21점 이하일 때 + - 딜러 점수가 21점 초과일 떄 + - 플레이어 `승` + - 딜러 점수가 21점 이하일 때 + - 플레이어 점수가 높으면 `승` + - 플레이어 점수가 낮으면 `패` + - 플레어어 점수와 같으면 `무` + ## 도메인 다이어그램 ```mermaid @@ -27,12 +40,13 @@ graph TD BlackJackController --> OutputView BlackJackController --> Participants BlackJackController --> Deck + InputView --> Parser Participants --> Participant Participant --> Player Participant --> Dealer Participant --> Cards - Player --> Name - DeckFactory --> Deck + Player --> PlayerName + Dealer --> Result Cards --> Card Card --> Suit Card --> Number @@ -71,7 +85,6 @@ graph TD ### 덱 - [x] 카드 목록을 가진다. -- [x] 카드를 섞는다. - [x] 카드를 뽑는다. - [x] 카드가 없으면 뽑을 수 없다. @@ -84,6 +97,6 @@ graph TD ### 출력 - [x] 딜러와 플레이어의 카드 현황을 출력한다. -- [x] 딜러 카드 추현가 여부를 출력한다. +- [x] 딜러 카드 추가 여부를 출력한다. - [x] 최종 결과를 출력한다. - [x] 최종 승패를 출력한다. diff --git a/src/main/java/blackjack/controller/BlackJackController.java b/src/main/java/blackjack/controller/BlackJackController.java index 0d35efc248..a9cbbea76a 100644 --- a/src/main/java/blackjack/controller/BlackJackController.java +++ b/src/main/java/blackjack/controller/BlackJackController.java @@ -1,17 +1,22 @@ package blackjack.controller; +import blackjack.domain.card.Card; import blackjack.domain.card.Deck; import blackjack.domain.participant.Dealer; import blackjack.domain.participant.Participant; import blackjack.domain.participant.Participants; import blackjack.domain.participant.Player; -import blackjack.domain.result.Score; +import blackjack.domain.participant.Result; import blackjack.view.InputView; import blackjack.view.OutputView; +import blackjack.view.dto.CardsResponse; +import blackjack.view.dto.DealerResultResponse; import blackjack.view.dto.DealerStateResponse; import blackjack.view.dto.ParticipantResponse; import blackjack.view.dto.PlayerResultResponse; +import java.util.EnumMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class BlackJackController { @@ -45,21 +50,40 @@ private List gatherPlayers() { .collect(Collectors.toList()); } - private void dealCards(Participants participants, Deck deck) { + private void dealCards(final Participants participants, final Deck deck) { participants.drawCard(deck, INITIAL_DRAW_COUNT); - final ParticipantResponse dealerResponse = ParticipantResponse.from(participants.getDealer()); - final List playerResponse = getPlayerResponse(participants.getPlayers()); + final ParticipantResponse dealerResponse = getHiddenDealerResponse(participants.getDealer()); + final List playerResponse = getParticipantResponses(participants.getPlayers()); outputView.printDealCards(dealerResponse, playerResponse, INITIAL_DRAW_COUNT); } - private List getPlayerResponse(final List players) { - return players.stream() - .map(ParticipantResponse::from) + private ParticipantResponse getHiddenDealerResponse(final Dealer dealer) { + final List hiddenCards = dealer.getCards().subList(0, INITIAL_DRAW_COUNT - 1); + final CardsResponse cardsResponse = new CardsResponse(-1, getCardInfos(hiddenCards)); + return new ParticipantResponse(dealer.getName(), cardsResponse); + } + + private List getCardInfos(final List cards) { + return cards.stream() + .map(card -> card.getNumberName() + card.getSuitName()) .collect(Collectors.toList()); } + private List getParticipantResponses(final List participants) { + return participants.stream() + .map(this::getParticipantResponse) + .collect(Collectors.toList()); + } + + private ParticipantResponse getParticipantResponse(final Participant participant) { + final CardsResponse cardsResponse = new CardsResponse( + participant.getScore(), getCardInfos(participant.getCards()) + ); + return new ParticipantResponse(participant.getName(), cardsResponse); + } + private void drawCard(final List players, final Deck deck) { for (final Player player : players) { drawCard(player, deck); @@ -69,14 +93,14 @@ private void drawCard(final List players, final Deck deck) { private void drawCard(final Player player, final Deck deck) { while (player.isDrawable() && inputView.readMoreDraw(player.getName())) { player.drawCard(deck.draw()); - outputView.printHandedCardsWithoutScore(ParticipantResponse.from(player)); + outputView.printHandedCardsWithoutScore(getParticipantResponse(player)); } } private void drawCard(final Dealer dealer, final Deck deck) { if (dealer.isDrawable()) { dealer.drawCard(deck.draw()); - outputView.printDealerDrawn(new DealerStateResponse(true, Dealer.MAXIMUM_DRAWABLE_SCORE)); + outputView.printDealerDrawn(new DealerStateResponse(true, dealer.getMaximumDrawableScore())); } } @@ -85,21 +109,34 @@ private void printResult(final Participants participants) { outputView.printHandedCardsWithScore(participantResponses); final Dealer dealer = participants.getDealer(); + final List players = participants.getPlayers(); - final List playerResultResponses = getPlayerResultResponses( - dealer.getScore(), participants.getPlayers()); - outputView.printFinalResult(dealer.getName(), playerResultResponses); - } + final List playerResult = getPlayerResults(dealer, players); + final DealerResultResponse dealerResult = getDealerResult(dealer, playerResult); - private List getParticipantResponses(final List participants) { - return participants.stream() - .map(ParticipantResponse::from) - .collect(Collectors.toList()); + outputView.printFinalResult(dealerResult, playerResult); } - public List getPlayerResultResponses(final Score dealerScore, final List players) { + private List getPlayerResults(final Dealer dealer, final List players) { return players.stream() - .map(player -> new PlayerResultResponse(player.getName(), player.getWinningStatus(dealerScore))) + .map(player -> new PlayerResultResponse(player.getName(), dealer.showResult(player.getScore()))) .collect(Collectors.toList()); } + + private DealerResultResponse getDealerResult(final Dealer dealer, final List playerResults) { + final Map dealerResult = initResult(); + for (final PlayerResultResponse playerResult : playerResults) { + final Result result = playerResult.getResult().reverse(); + dealerResult.put(result, dealerResult.get(result) + 1); + } + return new DealerResultResponse(dealer.getName(), dealerResult); + } + + private Map initResult() { + final Map initResult = new EnumMap<>(Result.class); + for (final Result result : Result.values()) { + initResult.put(result, 0); + } + return initResult; + } } diff --git a/src/main/java/blackjack/domain/card/Deck.java b/src/main/java/blackjack/domain/card/Deck.java index f2b61f4c64..0ebc84e0ea 100644 --- a/src/main/java/blackjack/domain/card/Deck.java +++ b/src/main/java/blackjack/domain/card/Deck.java @@ -8,7 +8,7 @@ public class Deck { - public static final Stack TRUMP; + private static final Stack TRUMP; static { final Stack pack = new Stack<>(); diff --git a/src/main/java/blackjack/domain/participant/Dealer.java b/src/main/java/blackjack/domain/participant/Dealer.java index c58410d82b..08bf899e6b 100644 --- a/src/main/java/blackjack/domain/participant/Dealer.java +++ b/src/main/java/blackjack/domain/participant/Dealer.java @@ -4,8 +4,8 @@ public class Dealer extends Participant { - public static final int MAXIMUM_DRAWABLE_SCORE = 16; private static final String NAME = "딜러"; + private static final int MAXIMUM_DRAWABLE_SCORE = 16; private static final int MAXIMUM_DRAWABLE_CARD_COUNT = 2; private final String name = NAME; @@ -40,4 +40,22 @@ public boolean isDealer() { public String getName() { return name; } + + public Result showResult(final int score) { + final int dealerScore = getScore(); + if (score > BLACK_JACK_SCORE) { + return Result.LOSE; + } + if (dealerScore > BLACK_JACK_SCORE || score > dealerScore) { + return Result.WIN; + } + if (score < dealerScore) { + return Result.LOSE; + } + return Result.DRAW; + } + + public int getMaximumDrawableScore() { + return MAXIMUM_DRAWABLE_SCORE; + } } diff --git a/src/main/java/blackjack/domain/participant/Participant.java b/src/main/java/blackjack/domain/participant/Participant.java index 0f94b1a6fd..a2f3711c48 100644 --- a/src/main/java/blackjack/domain/participant/Participant.java +++ b/src/main/java/blackjack/domain/participant/Participant.java @@ -2,12 +2,12 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Cards; -import blackjack.domain.result.Result; -import blackjack.domain.result.Score; import java.util.List; public abstract class Participant { + protected static final int BLACK_JACK_SCORE = 21; + protected final Cards cards; protected Participant() { @@ -25,12 +25,8 @@ public void drawCard(final Card card) { cards.addCard(card); } - public Score getScore() { - return new Score(cards.calculateTotalScore()); - } - - public Result getWinningStatus(final Score score) { - return getScore().compare(score); + public int getScore() { + return cards.calculateTotalScore(); } public List getCards() { diff --git a/src/main/java/blackjack/domain/result/Result.java b/src/main/java/blackjack/domain/participant/Result.java similarity index 51% rename from src/main/java/blackjack/domain/result/Result.java rename to src/main/java/blackjack/domain/participant/Result.java index 4b5a490497..4cd3534388 100644 --- a/src/main/java/blackjack/domain/result/Result.java +++ b/src/main/java/blackjack/domain/participant/Result.java @@ -1,4 +1,4 @@ -package blackjack.domain.result; +package blackjack.domain.participant; public enum Result { @@ -12,6 +12,16 @@ public enum Result { this.name = name; } + public Result reverse() { + if (this == WIN) { + return LOSE; + } + if (this == LOSE) { + return WIN; + } + return DRAW; + } + public String getName() { return name; } diff --git a/src/main/java/blackjack/domain/result/Score.java b/src/main/java/blackjack/domain/result/Score.java deleted file mode 100644 index 5a8b9194cf..0000000000 --- a/src/main/java/blackjack/domain/result/Score.java +++ /dev/null @@ -1,39 +0,0 @@ -package blackjack.domain.result; - -import static blackjack.domain.result.Result.DRAW; -import static blackjack.domain.result.Result.LOSE; -import static blackjack.domain.result.Result.WIN; - -public class Score implements Comparable { - - private final int value; - - public Score(final int value) { - this.value = value; - } - - public Result compare(final Score score) { - final int result = this.compareTo(score); - if (value > 21) { - if (score.getValue() > 21) return DRAW; - return LOSE; - } - - if (result > 0) { - return WIN; - } - if (result < 0) { - return LOSE; - } - return DRAW; - } - - public int getValue() { - return value; - } - - @Override - public int compareTo(final Score score) { - return Integer.compare(value, score.getValue()); - } -} diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 66cdf0cc49..ffd81ae013 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,16 +1,16 @@ package blackjack.view; -import static blackjack.domain.result.Result.DRAW; -import static blackjack.domain.result.Result.LOSE; -import static blackjack.domain.result.Result.WIN; +import static blackjack.domain.participant.Result.DRAW; +import static blackjack.domain.participant.Result.LOSE; +import static blackjack.domain.participant.Result.WIN; import static java.text.MessageFormat.format; -import blackjack.domain.result.Result; +import blackjack.domain.participant.Result; import blackjack.view.dto.CardsResponse; +import blackjack.view.dto.DealerResultResponse; import blackjack.view.dto.DealerStateResponse; import blackjack.view.dto.ParticipantResponse; import blackjack.view.dto.PlayerResultResponse; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -77,28 +77,14 @@ private String getScoreFormat(final int totalScore) { return format("결과: {0}", totalScore); } - public void printFinalResult(final String dealerName, final List players) { + public void printFinalResult(final DealerResultResponse dealer, final List players) { System.out.println(LINE_SEPARATOR + "## 최종 승패"); - System.out.println(format(RESULT_FORMAT, dealerName, getDealerResult(players))); + System.out.println(format(RESULT_FORMAT, dealer.getName(), getDealerResult(dealer.getResult()))); players.forEach(this::printPlayerResult); } - private String getDealerResult(final List players) { - final Map playerResult = initPlayerResult(); - for (PlayerResultResponse player : players) { - final Result result = player.getResult(); - playerResult.put(result, playerResult.getOrDefault(result, 0) + 1); - } - - return format("{0}승 {1}무 {2}패", playerResult.get(LOSE), playerResult.get(DRAW), playerResult.get(WIN)); - } - - private Map initPlayerResult() { - final Map playerResult = new HashMap<>(); - for (final Result result : Result.values()) { - playerResult.put(result, 0); - } - return playerResult; + private String getDealerResult(final Map result) { + return format("{0}승 {1}무 {2}패", result.get(WIN), result.get(DRAW), result.get(LOSE)); } private void printPlayerResult(final PlayerResultResponse player) { diff --git a/src/main/java/blackjack/view/dto/CardsResponse.java b/src/main/java/blackjack/view/dto/CardsResponse.java index 50aa19a188..42fc131aad 100644 --- a/src/main/java/blackjack/view/dto/CardsResponse.java +++ b/src/main/java/blackjack/view/dto/CardsResponse.java @@ -1,30 +1,17 @@ package blackjack.view.dto; -import blackjack.domain.card.Card; -import blackjack.domain.result.Score; import java.util.List; -import java.util.stream.Collectors; public class CardsResponse { private final int totalScore; private final List cardInfos; - private CardsResponse(final int totalScore, final List cardInfos) { + public CardsResponse(final int totalScore, final List cardInfos) { this.totalScore = totalScore; this.cardInfos = cardInfos; } - public static CardsResponse of(final Score score, final List cards) { - return new CardsResponse(score.getValue(), getCardInfos(cards)); - } - - private static List getCardInfos(final List cards) { - return cards.stream() - .map(card -> card.getNumberName() + card.getSuitName()) - .collect(Collectors.toList()); - } - public int getTotalScore() { return totalScore; } diff --git a/src/main/java/blackjack/view/dto/DealerResultResponse.java b/src/main/java/blackjack/view/dto/DealerResultResponse.java new file mode 100644 index 0000000000..d7b8a6fb42 --- /dev/null +++ b/src/main/java/blackjack/view/dto/DealerResultResponse.java @@ -0,0 +1,23 @@ +package blackjack.view.dto; + +import blackjack.domain.participant.Result; +import java.util.Map; + +public class DealerResultResponse { + + private final String name; + private final Map result; + + public DealerResultResponse(final String name, final Map result) { + this.name = name; + this.result = result; + } + + public String getName() { + return name; + } + + public Map getResult() { + return result; + } +} diff --git a/src/main/java/blackjack/view/dto/ParticipantResponse.java b/src/main/java/blackjack/view/dto/ParticipantResponse.java index e9efca1584..47d21d60c0 100644 --- a/src/main/java/blackjack/view/dto/ParticipantResponse.java +++ b/src/main/java/blackjack/view/dto/ParticipantResponse.java @@ -1,23 +1,15 @@ package blackjack.view.dto; -import blackjack.domain.participant.Participant; - public class ParticipantResponse { private final String name; private final CardsResponse cardsResponse; - private ParticipantResponse(final String name, final CardsResponse cardsResponse) { + public ParticipantResponse(final String name, final CardsResponse cardsResponse) { this.name = name; this.cardsResponse = cardsResponse; } - public static ParticipantResponse from(final Participant participant) { - return new ParticipantResponse( - participant.getName(), CardsResponse.of(participant.getScore(), participant.getCards()) - ); - } - public String getName() { return name; } diff --git a/src/main/java/blackjack/view/dto/PlayerResultResponse.java b/src/main/java/blackjack/view/dto/PlayerResultResponse.java index 1a1aa254b2..45187d53bd 100644 --- a/src/main/java/blackjack/view/dto/PlayerResultResponse.java +++ b/src/main/java/blackjack/view/dto/PlayerResultResponse.java @@ -1,6 +1,6 @@ package blackjack.view.dto; -import blackjack.domain.result.Result; +import blackjack.domain.participant.Result; public class PlayerResultResponse { diff --git a/src/test/java/blackjack/domain/participant/DealerTest.java b/src/test/java/blackjack/domain/participant/DealerTest.java index e31c72eead..8f3c34b4cd 100644 --- a/src/test/java/blackjack/domain/participant/DealerTest.java +++ b/src/test/java/blackjack/domain/participant/DealerTest.java @@ -116,7 +116,7 @@ class drawCard_메서드는 { )); final Dealer dealer = new Dealer(cards); - assertThat(dealer.getScore().getValue()).isEqualTo(15); + assertThat(dealer.getScore()).isEqualTo(15); } @Test diff --git a/src/test/java/blackjack/domain/participant/PlayerTest.java b/src/test/java/blackjack/domain/participant/PlayerTest.java index 39059ddde6..40c51d6465 100644 --- a/src/test/java/blackjack/domain/participant/PlayerTest.java +++ b/src/test/java/blackjack/domain/participant/PlayerTest.java @@ -100,7 +100,7 @@ class drawCard_메서드는 { final Cards cards = new Cards(cardPack); final Player player = new Player("dazzle", cards); - assertThat(player.getScore().getValue()).isEqualTo(20); + assertThat(player.getScore()).isEqualTo(20); } @Test From 4daf6f815e3d13467754a34ef8f1f96a87e39086 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Mon, 6 Mar 2023 00:56:05 +0900 Subject: [PATCH 76/80] =?UTF-8?q?refactor:=20=EB=94=9C=EB=9F=AC=20?= =?UTF-8?q?=EC=8A=B9=ED=8C=A8=20=EC=97=AC=EB=B6=80=20=ED=99=95=EC=9D=B8=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/participant/Dealer.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/blackjack/domain/participant/Dealer.java b/src/main/java/blackjack/domain/participant/Dealer.java index 08bf899e6b..fc41ed4fb8 100644 --- a/src/main/java/blackjack/domain/participant/Dealer.java +++ b/src/main/java/blackjack/domain/participant/Dealer.java @@ -43,15 +43,12 @@ public String getName() { public Result showResult(final int score) { final int dealerScore = getScore(); - if (score > BLACK_JACK_SCORE) { + if (score > BLACK_JACK_SCORE || score < dealerScore) { return Result.LOSE; } - if (dealerScore > BLACK_JACK_SCORE || score > dealerScore) { + if (dealerScore > BLACK_JACK_SCORE || dealerScore < score) { return Result.WIN; } - if (score < dealerScore) { - return Result.LOSE; - } return Result.DRAW; } From 24f34f16c7477096dfd8f1a482cb4759a039affb Mon Sep 17 00:00:00 2001 From: woo-chang Date: Mon, 6 Mar 2023 11:26:07 +0900 Subject: [PATCH 77/80] =?UTF-8?q?refactor:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=EC=97=90=EC=84=9C=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EB=90=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/card/Cards.java | 12 +----- .../blackjack/domain/participant/Player.java | 2 +- .../java/blackjack/domain/card/CardsTest.java | 42 ------------------- 3 files changed, 3 insertions(+), 53 deletions(-) diff --git a/src/main/java/blackjack/domain/card/Cards.java b/src/main/java/blackjack/domain/card/Cards.java index a53936c7e3..50e06b748f 100644 --- a/src/main/java/blackjack/domain/card/Cards.java +++ b/src/main/java/blackjack/domain/card/Cards.java @@ -6,8 +6,8 @@ public class Cards { - private static final int MAXIMUM_SCORE = 21; private static final int ACE_BONUS = 10; + private static final int MAXIMUM_UPDATABLE_SCORE = 21; private final List cards; @@ -45,15 +45,7 @@ private boolean isExistAce() { } private boolean isScoreUpdatable(final int score) { - return score + ACE_BONUS <= MAXIMUM_SCORE; - } - - public boolean isTotalScoreOver() { - return calculateTotalScore() > MAXIMUM_SCORE; - } - - public boolean isMaximumScore() { - return calculateTotalScore() == MAXIMUM_SCORE; + return score + ACE_BONUS <= MAXIMUM_UPDATABLE_SCORE; } public int count() { diff --git a/src/main/java/blackjack/domain/participant/Player.java b/src/main/java/blackjack/domain/participant/Player.java index 80d1dee7d6..5e85f47c8d 100644 --- a/src/main/java/blackjack/domain/participant/Player.java +++ b/src/main/java/blackjack/domain/participant/Player.java @@ -18,7 +18,7 @@ public Player(final String name, final Cards cards) { @Override public boolean isDrawable() { - return !cards.isMaximumScore() && !cards.isTotalScoreOver(); + return cards.calculateTotalScore() < BLACK_JACK_SCORE; } @Override diff --git a/src/test/java/blackjack/domain/card/CardsTest.java b/src/test/java/blackjack/domain/card/CardsTest.java index 70a473c2f8..4cea664ffc 100644 --- a/src/test/java/blackjack/domain/card/CardsTest.java +++ b/src/test/java/blackjack/domain/card/CardsTest.java @@ -57,48 +57,6 @@ private static Stream generateCardsWithAce() { assertThat(cards.calculateTotalScore()).isEqualTo(totalScore); } - @Test - void 점수_최댓값_초과라면_true_반환한다() { - final Cards cards = new Cards( - List.of(new Card(TEN, SPADE), - new Card(JACK, HEART), - new Card(QUEEN, CLOVER) - )); - - assertThat(cards.isTotalScoreOver()).isTrue(); - } - - @Test - void 점수_최댓값_이하라면_false_반환한다() { - final Cards cards = new Cards( - List.of(new Card(TEN, SPADE), - new Card(JACK, HEART) - )); - - assertThat(cards.isTotalScoreOver()).isFalse(); - } - - @Test - void 최대_점수라면_true_반환한다() { - final Cards cards = new Cards( - List.of(new Card(TEN, SPADE), - new Card(JACK, HEART), - new Card(ACE, CLOVER) - )); - - assertThat(cards.isMaximumScore()).isTrue(); - } - - @Test - void 최대_점수_아니라면_false_반환한다() { - final Cards cards = new Cards( - List.of(new Card(TEN, SPADE), - new Card(JACK, HEART) - )); - - assertThat(cards.isMaximumScore()).isFalse(); - } - @Test void 카드를_추가한다() { final Cards cards = new Cards(new ArrayList<>()); From be64ca75b4823aa6d9ccd51a224e024bb6209477 Mon Sep 17 00:00:00 2001 From: woo-chang Date: Mon, 6 Mar 2023 11:34:26 +0900 Subject: [PATCH 78/80] =?UTF-8?q?refactor:=20=EB=94=9C=EB=9F=AC=EA=B0=80?= =?UTF-8?q?=20=EA=B2=B0=EA=B3=BC=20=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/participant/Dealer.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/blackjack/domain/participant/Dealer.java b/src/main/java/blackjack/domain/participant/Dealer.java index fc41ed4fb8..053b428ad2 100644 --- a/src/main/java/blackjack/domain/participant/Dealer.java +++ b/src/main/java/blackjack/domain/participant/Dealer.java @@ -43,12 +43,15 @@ public String getName() { public Result showResult(final int score) { final int dealerScore = getScore(); - if (score > BLACK_JACK_SCORE || score < dealerScore) { + if (score > BLACK_JACK_SCORE) { return Result.LOSE; } if (dealerScore > BLACK_JACK_SCORE || dealerScore < score) { return Result.WIN; } + if (dealerScore > score) { + return Result.LOSE; + } return Result.DRAW; } From 601b98d53e172b3faae38a1169119050c35f74aa Mon Sep 17 00:00:00 2001 From: woo-chang Date: Mon, 6 Mar 2023 11:56:47 +0900 Subject: [PATCH 79/80] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=ED=95=98=EC=A7=80=20=EB=AA=BB=ED=96=88=EB=8D=98=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EC=97=90=20=EB=8C=80=ED=95=9C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/card/CardTest.java | 31 ++++--- .../domain/participant/DealerTest.java | 81 +++++++++++++++++++ .../domain/participant/ResultTest.java | 37 +++++++++ 3 files changed, 136 insertions(+), 13 deletions(-) create mode 100644 src/test/java/blackjack/domain/participant/ResultTest.java diff --git a/src/test/java/blackjack/domain/card/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java index db5ba82858..d1b46805ad 100644 --- a/src/test/java/blackjack/domain/card/CardTest.java +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @SuppressWarnings("NonAsciiCharacters") @@ -40,23 +41,27 @@ class CardTest { assertThat(card.getScore()).isEqualTo(1); } - @Test - void 에이스라면_true_반환한다() { - final Suit suit = Suit.SPADE; - final Number number = Number.ACE; + @Nested + class isAce_메서드는 { - final Card card = new Card(number, suit); + @Test + void 카드가_에이스라면_true_반환한다() { + final Suit suit = Suit.SPADE; + final Number number = Number.ACE; - assertThat(card.isAce()).isTrue(); - } + final Card card = new Card(number, suit); - @Test - void 에이스가_아니라면_false_반환한다() { - final Suit suit = Suit.SPADE; - final Number number = Number.TEN; + assertThat(card.isAce()).isTrue(); + } - final Card card = new Card(number, suit); + @Test + void 카드가_에이스가_아니라면_false_반환한다() { + final Suit suit = Suit.SPADE; + final Number number = Number.TEN; + + final Card card = new Card(number, suit); - assertThat(card.isAce()).isFalse(); + assertThat(card.isAce()).isFalse(); + } } } diff --git a/src/test/java/blackjack/domain/participant/DealerTest.java b/src/test/java/blackjack/domain/participant/DealerTest.java index 8f3c34b4cd..eb5b93c83f 100644 --- a/src/test/java/blackjack/domain/participant/DealerTest.java +++ b/src/test/java/blackjack/domain/participant/DealerTest.java @@ -1,6 +1,7 @@ package blackjack.domain.participant; import static blackjack.domain.card.Number.ACE; +import static blackjack.domain.card.Number.FIVE; import static blackjack.domain.card.Number.QUEEN; import static blackjack.domain.card.Number.SEVEN; import static blackjack.domain.card.Number.SIX; @@ -8,6 +9,9 @@ import static blackjack.domain.card.Suit.CLOVER; import static blackjack.domain.card.Suit.DIAMOND; import static blackjack.domain.card.Suit.HEART; +import static blackjack.domain.participant.Result.DRAW; +import static blackjack.domain.participant.Result.LOSE; +import static blackjack.domain.participant.Result.WIN; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -125,4 +129,81 @@ class drawCard_메서드는 { assertThat(dealer.isDealer()).isTrue(); } + + @Test + void 딜러의_이름은_딜러이다() { + final Dealer dealer = new Dealer(); + + assertThat(dealer.getName()).isEqualTo("딜러"); + } + + @Test + void 딜러가_카드를_뽑을_수_있는_최대_점수는_16점이다() { + final Dealer dealer = new Dealer(); + + assertThat(dealer.getMaximumDrawableScore()).isEqualTo(16); + } + + @Nested + class showResult_메서드는 { + + @Nested + class 플레이어_점수가_블랙잭_점수를_초과하면 { + + @Test + void LOSE_반환한다() { + final Dealer dealer = new Dealer(); + + assertThat(dealer.showResult(22)).isEqualTo(LOSE); + } + } + + @Nested + class 딜러_점수가_블랙잭_점수를_초과하면 { + + @Test + void 플레이어_점수가_블랙잭_점수_이하라면_WIN_반환한다() { + final Dealer dealer = new Dealer(); + + assertThat(dealer.showResult(21)).isEqualTo(WIN); + } + } + + @Nested + class 딜러와_플레이어_점수_모두_블랙잭_점수_이하라면 { + + @Test + void 딜러_점수가_플레이어_점수보다_낮으면_WIN_반환한다() { + final Cards cards = new Cards(List.of( + new Card(ACE, HEART), + new Card(FIVE, DIAMOND) + )); //16점 + final Dealer dealer = new Dealer(cards); + + assertThat(dealer.showResult(17)).isEqualTo(WIN); + } + + @Test + void 딜러_점수가_플레이어_점수보다_높으면_LOSE_반환한다() { + final Cards cards = new Cards(List.of( + new Card(ACE, HEART), + new Card(FIVE, DIAMOND) + )); //16점 + final Dealer dealer = new Dealer(cards); + + assertThat(dealer.showResult(15)).isEqualTo(LOSE); + } + + @Test + void 점수_같으면_DRAW_반환한다() { + final Cards cards = new Cards(List.of( + new Card(ACE, HEART), + new Card(FIVE, DIAMOND) + )); //16점 + final Dealer dealer = new Dealer(cards); + + assertThat(dealer.showResult(16)).isEqualTo(DRAW); + } + } + } } diff --git a/src/test/java/blackjack/domain/participant/ResultTest.java b/src/test/java/blackjack/domain/participant/ResultTest.java new file mode 100644 index 0000000000..e431e25873 --- /dev/null +++ b/src/test/java/blackjack/domain/participant/ResultTest.java @@ -0,0 +1,37 @@ +package blackjack.domain.participant; + +import static blackjack.domain.participant.Result.DRAW; +import static blackjack.domain.participant.Result.LOSE; +import static blackjack.domain.participant.Result.WIN; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +class ResultTest { + + @Nested + class reverse_메서드는 { + + @Test + void WIN_이라면_LOSE_반환한다() { + final Result result = WIN; + + assertThat(result.reverse()).isEqualTo(LOSE); + } + + @Test + void LOSE_라면_WIN_반환한다() { + final Result result = LOSE; + + assertThat(result.reverse()).isEqualTo(WIN); + } + + @Test + void DRAW_라면_DRAW_반환한다() { + final Result result = DRAW; + + assertThat(result.reverse()).isEqualTo(DRAW); + } + } +} From 44bd9647d91379dbd922b34a64048f65fef1e49b Mon Sep 17 00:00:00 2001 From: woo-chang Date: Mon, 6 Mar 2023 17:59:57 +0900 Subject: [PATCH 80/80] =?UTF-8?q?feat:=20=EC=A0=9C=EB=84=A4=EB=A6=AD=20?= =?UTF-8?q?=EB=AF=B8=EC=85=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../techcourse/jcf/mission/GenericStudy.java | 99 +++++++++++ .../java/techcourse/jcf/mission/README.md | 25 +++ .../jcf/mission/SimpleArrayList.java | 166 ++++++++++++++++++ .../techcourse/jcf/mission/SimpleList.java | 58 ++++++ 4 files changed, 348 insertions(+) create mode 100644 src/test/java/techcourse/jcf/mission/GenericStudy.java create mode 100644 src/test/java/techcourse/jcf/mission/README.md create mode 100644 src/test/java/techcourse/jcf/mission/SimpleArrayList.java create mode 100644 src/test/java/techcourse/jcf/mission/SimpleList.java diff --git a/src/test/java/techcourse/jcf/mission/GenericStudy.java b/src/test/java/techcourse/jcf/mission/GenericStudy.java new file mode 100644 index 0000000000..f1412059db --- /dev/null +++ b/src/test/java/techcourse/jcf/mission/GenericStudy.java @@ -0,0 +1,99 @@ +package techcourse.jcf.mission; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +public class GenericStudy { + + @Test + void 정수_타입_리스트를_생성한다() { + SimpleList values = new SimpleArrayList(); + values.add(1); + values.add(2); + + Integer first = values.get(0); + Integer second = values.get(1); + + assertAll( + () -> assertThat(first).isEqualTo(1), + () -> assertThat(second).isEqualTo(2) + ); + } + + @Test + void 배열을_받아서_리스트로_변환한다() { + String[] arrays = {"first", "second"}; + + SimpleList values = SimpleList.fromArrayToList(arrays); + + String first = values.get(0); + String second = values.get(1); + + assertAll( + () -> assertThat(first).isEqualTo("first"), + () -> assertThat(second).isEqualTo("second") + ); + } + + @Test + void 숫자타입이라면_모든_값을_더한다() { + Double[] doubleArrays = {0.5, 0.7}; + Integer[] intArrays = {1, 2}; + + SimpleList doubleList = SimpleList.fromArrayToList(doubleArrays); + SimpleList intList = SimpleList.fromArrayToList(intArrays); + + double doubleSum = SimpleList.sum(doubleList); + double intSum = SimpleList.sum(intList); + + assertAll( + () -> assertThat(doubleSum).isEqualTo(1.2), + () -> assertThat(intSum).isEqualTo(3) + ); + } + + @Test + void 음수를_필터링한다() { + Double[] doubleArrays = {-0.1, 0.5, 0.7}; + Integer[] intArrays = {-10, 1, 2}; + + SimpleList doubleList = SimpleList.fromArrayToList(doubleArrays); + SimpleList intList = SimpleList.fromArrayToList(intArrays); + + SimpleList filteredDoubleList = SimpleList.filterNegative(doubleList); + SimpleList filteredIntList = SimpleList.filterNegative(intList); + + assertAll( + () -> assertThat(filteredDoubleList.size()).isEqualTo(2), + () -> assertThat(filteredDoubleList.get(0)).isEqualTo(0.5), + () -> assertThat(filteredDoubleList.get(1)).isEqualTo(0.7), + () -> assertThat(filteredIntList.size()).isEqualTo(2), + () -> assertThat(filteredIntList.get(0)).isEqualTo(1), + () -> assertThat(filteredIntList.get(1)).isEqualTo(2) + ); + } + + @Test + void 리스트를_다른_리스트로_복사한다() { + class Printer { + } + class LaserPrinter extends Printer { + } + + LaserPrinter laserPrinter = new LaserPrinter(); + + SimpleList printers = new SimpleArrayList<>(); + SimpleList laserPrinters = new SimpleArrayList<>(); + laserPrinters.add(laserPrinter); + + SimpleList.copy(laserPrinters, printers); + + assertThat(printers.get(0)).isEqualTo(laserPrinter); + } +} diff --git a/src/test/java/techcourse/jcf/mission/README.md b/src/test/java/techcourse/jcf/mission/README.md new file mode 100644 index 0000000000..54a98b388f --- /dev/null +++ b/src/test/java/techcourse/jcf/mission/README.md @@ -0,0 +1,25 @@ +## 제네릭 이해하기 + +### 미션 1 + +기존 SimpleList는 String 타입의 데이터만 다룰 수 있다. +제네릭을 사용하여 String 이외의 타입도 다룰 수 있도록 개선해본다. + +### 미션 2 + +배열을 받아 SimpleList로 변환하는 메서드를 구현해본다. +제네릭을 사용하여 외부에서 생성되는 리스트의 타입을 지정할 수 있어야 한다. + +### 미션 3 + +숫자 타입의 SimpleList를 받아 모든 값을 더해주는 메서드를 구현해본다. +String과 같은 숫자가 아닌 타입이 들어올 경우 예외가 발생해야 한다. + +### 미션 4 + +숫자 타입의 SimpleList를 받아 음수를 제외하고 반환하는 메서드를 구현해본다. + +### 미션 5 + +리스트의 값을 다른 리스트로 복사하는 메서드를 구현해본다. +해당 메서드는 같은 타입이 아닌 상속 관계의 타입도 복사가 가능해야 한다. diff --git a/src/test/java/techcourse/jcf/mission/SimpleArrayList.java b/src/test/java/techcourse/jcf/mission/SimpleArrayList.java new file mode 100644 index 0000000000..ba514bdafb --- /dev/null +++ b/src/test/java/techcourse/jcf/mission/SimpleArrayList.java @@ -0,0 +1,166 @@ +package techcourse.jcf.mission; + +import static java.text.MessageFormat.format; + +import java.util.Arrays; + +public class SimpleArrayList implements SimpleList { + + private static final int DEFAULT_CAPACITY = 10; + + private Object[] values; + private int size; + + public SimpleArrayList() { + this(DEFAULT_CAPACITY); + } + + public SimpleArrayList(final int capacity) { + validateCapacity(capacity); + initCapacity(capacity); + this.size = 0; + } + + private void validateCapacity(final int capacity) { + if (capacity < 0) { + throw new IllegalArgumentException(format("용량은 음수가 될 수 없습니다. 입력: {0}", capacity)); + } + } + + private void initCapacity(final int capacity) { + if (capacity < DEFAULT_CAPACITY) { + this.values = new Object[DEFAULT_CAPACITY]; + } else { + this.values = new Object[capacity]; + } + } + + @Override + public boolean add(final T value) { + if (size == values.length) { + values = expand(); + } + addElement(size, value); + return true; + } + + private Object[] expand() { + final int expandCapacity = calculateExpandCapacity(values.length); + return Arrays.copyOf(values, expandCapacity); + } + + private int calculateExpandCapacity(final int capacity) { + return capacity + (int) (capacity * 0.5); + } + + private void addElement(final int index, final T value) { + values[index] = value; + size++; + } + + @Override + public void add(final int index, final T value) { + validateIndexForAdd(index); + if (size == values.length) { + values = expand(); + } + shiftRight(index, size - 1); + addElement(index, value); + } + + private void validateIndexForAdd(final int index) { + if (index < 0 || size < index) { + throw new IndexOutOfBoundsException(format("해당 인덱스에서 작업을 수행할 수 없습니다. 인덱스: {0}, 크기: {1}", index, size)); + } + } + + private void shiftRight(final int startIndex, final int endIndex) { + for (int i = endIndex; i >= startIndex; i--) { + values[i + 1] = values[i]; + } + } + + @Override + public T set(final int index, final T value) { + validateIndex(index); + final T oldValue = (T) values[index]; + values[index] = value; + return oldValue; + } + + private void validateIndex(final int index) { + if (index < 0 || size <= index) { + throw new IndexOutOfBoundsException(format("해당 인덱스에서 작업을 수행할 수 없습니다. 인덱스: {0}, 크기: {1}", index, size)); + } + } + + @Override + public T get(final int index) { + validateIndex(index); + return (T) values[index]; + } + + @Override + public boolean contains(final T value) { + final int index = indexOf(value); + if (index == -1) { + return false; + } + return true; + } + + @Override + public int indexOf(final T value) { + for (int i = 0; i < size; i++) { + if (values[i].equals(value)) { + return i; + } + } + return -1; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean remove(final T value) { + final int index = indexOf(value); + if (index == -1) { + return false; + } + shiftLeft(index + 1, size - 1); + size--; + return true; + } + + private void shiftLeft(final int startIndex, final int endIndex) { + for (int i = startIndex; i <= endIndex; i++) { + values[i - 1] = values[i]; + } + values[endIndex] = null; + } + + @Override + public T remove(final int index) { + validateIndex(index); + final T value = (T) values[index]; + shiftLeft(index + 1, size - 1); + size--; + return value; + } + + @Override + public void clear() { + for (int i = 0; i < size; i++) { + values[i] = null; + } + size = 0; + } +} diff --git a/src/test/java/techcourse/jcf/mission/SimpleList.java b/src/test/java/techcourse/jcf/mission/SimpleList.java new file mode 100644 index 0000000000..6930c899e6 --- /dev/null +++ b/src/test/java/techcourse/jcf/mission/SimpleList.java @@ -0,0 +1,58 @@ +package techcourse.jcf.mission; + +public interface SimpleList { + + static SimpleList fromArrayToList(final T[] values) { + final SimpleArrayList result = new SimpleArrayList<>(); + for (final T value : values) { + result.add(value); + } + return result; + } + + static double sum(final SimpleList list) { + double sum = 0; + for (int i = 0; i < list.size(); i++) { + sum += list.get(i).doubleValue(); + } + return sum; + } + + static SimpleList filterNegative(final SimpleList list) { + final SimpleList result = new SimpleArrayList<>(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i).doubleValue() >= 0) { + result.add(list.get(i)); + } + } + return result; + } + + static void copy(final SimpleList origin, final SimpleList copy) { + for (int i = 0; i < origin.size(); i++) { + copy.add(origin.get(i)); + } + } + + boolean add(T value); + + void add(int index, T value); + + T set(int index, T value); + + T get(int index); + + boolean contains(T value); + + int indexOf(T value); + + int size(); + + boolean isEmpty(); + + boolean remove(T value); + + T remove(int index); + + void clear(); +}