From 9a3072588e514572f408646a4209578a856514f2 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 13:40:52 +0900 Subject: [PATCH 01/67] =?UTF-8?q?docs(README)=20:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..b49903690 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,18 @@ +## 기능 구현 목록 +- [ ] 카드들을 구현한다. + - [ ] 문양을 구현한다. + - [ ] 숫자를 구현한다. +- [ ] 카드덱을 만든다. +- [ ] 딜러랑 유저를 만든다. + - [ ] 유저 각자의 카드덱을 만든다. +- [ ] 카드의 합을 구한다. +- [ ] 최종결과를 반환한다. + +### 입력 구현 목록 +- [ ] 사람들의 이름을 입력받는다. +- [ ] 카드를 더 받을지 입력 받는다. +### 출력 구현 목록 +- [ ] 나눠준 카드를 출력한다. +- [ ] 현재 카드 상태를 출력한다. +- [ ] 카드의 결과를 반환한다. +- \ No newline at end of file From e3a31d0fcdf1c9be0468b1e5115936d3e79d57f2 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 14:13:05 +0900 Subject: [PATCH 02/67] =?UTF-8?q?feat(CardMark)=20:=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EB=93=A4=EC=9D=98=20=EB=AC=B8=EC=96=91=EC=9D=84=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 5 ++-- src/main/kotlin/blackjack/domain/CardMark.kt | 8 ++++++ .../kotlin/blackjack/domain/CardMarkTest.kt | 26 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/CardMark.kt create mode 100644 src/test/kotlin/blackjack/domain/CardMarkTest.kt diff --git a/docs/README.md b/docs/README.md index b49903690..205d0a2a7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ ## 기능 구현 목록 - [ ] 카드들을 구현한다. - - [ ] 문양을 구현한다. + - [X] 문양을 구현한다. - [ ] 숫자를 구현한다. - [ ] 카드덱을 만든다. - [ ] 딜러랑 유저를 만든다. @@ -14,5 +14,4 @@ ### 출력 구현 목록 - [ ] 나눠준 카드를 출력한다. - [ ] 현재 카드 상태를 출력한다. -- [ ] 카드의 결과를 반환한다. -- \ No newline at end of file +- [ ] 카드의 결과를 반환한다. \ No newline at end of file diff --git a/src/main/kotlin/blackjack/domain/CardMark.kt b/src/main/kotlin/blackjack/domain/CardMark.kt new file mode 100644 index 000000000..f015b1e4e --- /dev/null +++ b/src/main/kotlin/blackjack/domain/CardMark.kt @@ -0,0 +1,8 @@ +package blackjack.domain + +enum class CardMark(val title: String) { + CLOVER("클로버"), + HEART("하트"), + SPADE("스페이드"), + DIA("다이아"), +} diff --git a/src/test/kotlin/blackjack/domain/CardMarkTest.kt b/src/test/kotlin/blackjack/domain/CardMarkTest.kt new file mode 100644 index 000000000..4179ec150 --- /dev/null +++ b/src/test/kotlin/blackjack/domain/CardMarkTest.kt @@ -0,0 +1,26 @@ +package blackjack.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class CardMarkTest { + @Test + fun `클로버를 반환한다`() { + assertThat(CardMark.CLOVER.title).isEqualTo("클로버") + } + + @Test + fun `하트를 반환한다`() { + assertThat(CardMark.HEART.title).isEqualTo("하트") + } + + @Test + fun `스페이드를 반환한다`() { + assertThat(CardMark.SPADE.title).isEqualTo("스페이드") + } + + @Test + fun `다이아를 반환한다`() { + assertThat(CardMark.DIA.title).isEqualTo("다이아") + } +} From 7bebf1efccaeb3230d0f96db13d66c7d6cdc1ec5 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 14:13:24 +0900 Subject: [PATCH 03/67] =?UTF-8?q?feat(CardValue)=20:=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EB=93=A4=EC=9D=98=20=EC=88=AB=EC=9E=90=EB=A5=BC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/blackjack/domain/CardValue.kt | 17 +++++ .../kotlin/blackjack/domain/CardValueTest.kt | 72 +++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/blackjack/domain/CardValue.kt create mode 100644 src/test/kotlin/blackjack/domain/CardValueTest.kt diff --git a/docs/README.md b/docs/README.md index 205d0a2a7..af158ef90 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,7 +1,7 @@ ## 기능 구현 목록 - [ ] 카드들을 구현한다. - [X] 문양을 구현한다. - - [ ] 숫자를 구현한다. + - [X] 숫자를 구현한다. - [ ] 카드덱을 만든다. - [ ] 딜러랑 유저를 만든다. - [ ] 유저 각자의 카드덱을 만든다. diff --git a/src/main/kotlin/blackjack/domain/CardValue.kt b/src/main/kotlin/blackjack/domain/CardValue.kt new file mode 100644 index 000000000..cc92f4368 --- /dev/null +++ b/src/main/kotlin/blackjack/domain/CardValue.kt @@ -0,0 +1,17 @@ +package blackjack.domain + +enum class CardValue(val value: Int) { + ACE(11), + KING(10), + QUEEN(10), + JACK(10), + TEN(10), + NINE(9), + EIGHT(8), + SEVEN(7), + SIX(6), + FIVE(5), + FOUR(4), + THREE(3), + TWO(2), +} diff --git a/src/test/kotlin/blackjack/domain/CardValueTest.kt b/src/test/kotlin/blackjack/domain/CardValueTest.kt new file mode 100644 index 000000000..1fa86dfd0 --- /dev/null +++ b/src/test/kotlin/blackjack/domain/CardValueTest.kt @@ -0,0 +1,72 @@ +package blackjack.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class CardValueTest { + + @Test + fun `A의 값은 11이다`() { + assertThat(CardValue.ACE.value).isEqualTo(11) + } + + @Test + fun `K의 값은 10이다`() { + assertThat(CardValue.KING.value).isEqualTo(10) + } + + @Test + fun `Q의 값은 10이다`() { + assertThat(CardValue.QUEEN.value).isEqualTo(10) + } + + @Test + fun `J의 값은 10이다`() { + assertThat(CardValue.JACK.value).isEqualTo(10) + } + + @Test + fun `TEN의 값은 10이다`() { + assertThat(CardValue.TEN.value).isEqualTo(10) + } + + @Test + fun `NINE의 값은 9이다`() { + assertThat(CardValue.NINE.value).isEqualTo(9) + } + + @Test + fun `EIGHT의 값은 8이다`() { + assertThat(CardValue.EIGHT.value).isEqualTo(8) + } + + @Test + fun `SEVEN의 값은 7이다`() { + assertThat(CardValue.SEVEN.value).isEqualTo(7) + } + + @Test + fun `SIX의 값은 6이다`() { + assertThat(CardValue.SIX.value).isEqualTo(6) + } + + @Test + fun `FIVE의 값은 5이다`() { + assertThat(CardValue.FIVE.value).isEqualTo(5) + } + + @Test + fun `FOUR의 값은 4이다`() { + assertThat(CardValue.FOUR.value).isEqualTo(4) + } + + @Test + fun `THREE의 값은 3이다`() { + assertThat(CardValue.THREE.value).isEqualTo(3) + } + + @Test + fun `TWO의 값은 2이다`() { + assertThat(CardValue.TWO.value).isEqualTo(2) + } +} From 5e0266baed2d9bd6fa25ca7012637743194c9b48 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 14:13:39 +0900 Subject: [PATCH 04/67] =?UTF-8?q?feat(Card)=20:=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EB=93=A4=EC=9D=84=20=EA=B5=AC=ED=98=84=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/blackjack/domain/Card.kt | 3 ++ src/test/kotlin/blackjack/domain/CardTest.kt | 34 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/blackjack/domain/Card.kt create mode 100644 src/test/kotlin/blackjack/domain/CardTest.kt diff --git a/docs/README.md b/docs/README.md index af158ef90..3edc268e6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,5 @@ ## 기능 구현 목록 -- [ ] 카드들을 구현한다. +- [X] 카드들을 구현한다. - [X] 문양을 구현한다. - [X] 숫자를 구현한다. - [ ] 카드덱을 만든다. diff --git a/src/main/kotlin/blackjack/domain/Card.kt b/src/main/kotlin/blackjack/domain/Card.kt new file mode 100644 index 000000000..a60379718 --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Card.kt @@ -0,0 +1,3 @@ +package blackjack.domain + +class Card(val mark: CardMark, val value: CardValue) diff --git a/src/test/kotlin/blackjack/domain/CardTest.kt b/src/test/kotlin/blackjack/domain/CardTest.kt new file mode 100644 index 000000000..5086df39d --- /dev/null +++ b/src/test/kotlin/blackjack/domain/CardTest.kt @@ -0,0 +1,34 @@ +package blackjack.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class CardTest { + @Test + fun `카드의 문양과 숫자를 가져올 수 있다`() { + val card = Card(CardMark.CLOVER, CardValue.ACE) + assertThat(card.mark).isEqualTo(CardMark.CLOVER) + assertThat(card.value).isEqualTo(CardValue.ACE) + } + + @Test + fun `카드의 문양과 숫자를 가져올 수 있다2`() { + val card = Card(CardMark.HEART, CardValue.TEN) + assertThat(card.mark).isEqualTo(CardMark.HEART) + assertThat(card.value).isEqualTo(CardValue.TEN) + } + + @Test + fun `카드의 문양과 숫자를 가져올 수 있다3`() { + val card = Card(CardMark.SPADE, CardValue.TWO) + assertThat(card.mark).isEqualTo(CardMark.SPADE) + assertThat(card.value).isEqualTo(CardValue.TWO) + } + + @Test + fun `카드의 문양과 숫자를 가져올 수 있다4`() { + val card = Card(CardMark.DIA, CardValue.JACK) + assertThat(card.mark).isEqualTo(CardMark.DIA) + assertThat(card.value).isEqualTo(CardValue.JACK) + } +} From de3e7b2a0e9c4a089016480fb09e6cb655eaf759 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 14:47:07 +0900 Subject: [PATCH 05/67] =?UTF-8?q?feat(CardDeck)=20:=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EB=8D=B1=EC=9D=84=20=EB=A7=8C=EB=93=A0=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/blackjack/domain/Card.kt | 13 ++++++- src/main/kotlin/blackjack/domain/CardDeck.kt | 23 ++++++++++++ .../kotlin/blackjack/domain/CardDeckTest.kt | 35 +++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/CardDeck.kt create mode 100644 src/test/kotlin/blackjack/domain/CardDeckTest.kt diff --git a/docs/README.md b/docs/README.md index 3edc268e6..1b41d40a9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ - [X] 카드들을 구현한다. - [X] 문양을 구현한다. - [X] 숫자를 구현한다. -- [ ] 카드덱을 만든다. +- [X] 카드덱을 만든다. - [ ] 딜러랑 유저를 만든다. - [ ] 유저 각자의 카드덱을 만든다. - [ ] 카드의 합을 구한다. diff --git a/src/main/kotlin/blackjack/domain/Card.kt b/src/main/kotlin/blackjack/domain/Card.kt index a60379718..75a1aa003 100644 --- a/src/main/kotlin/blackjack/domain/Card.kt +++ b/src/main/kotlin/blackjack/domain/Card.kt @@ -1,3 +1,14 @@ package blackjack.domain -class Card(val mark: CardMark, val value: CardValue) +class Card(val mark: CardMark, val value: CardValue) { + + companion object { + private val CARDS: List = + CardMark.values().map { mark -> + CardValue.values().map { value -> + Card(mark, value) + } + }.flatten() + fun all(): List = CARDS + } +} diff --git a/src/main/kotlin/blackjack/domain/CardDeck.kt b/src/main/kotlin/blackjack/domain/CardDeck.kt new file mode 100644 index 000000000..335af71df --- /dev/null +++ b/src/main/kotlin/blackjack/domain/CardDeck.kt @@ -0,0 +1,23 @@ +package blackjack.domain + +class CardDeck(cards: List) { + private val cards: MutableList = cards.toMutableList() + + init { + require(cards.toSet().size == cards.size) { ERROR_EXIST_DUPLICATE_CARDS } + require(cards.size == CARDS_SIZE) { ERROR_INVALID_CARDS_SIZE } + } + + val size: Int + get() = cards.size + + fun shuffle() = cards.shuffle() + + fun draw(): Card = cards.removeFirst() + + companion object { + private const val CARDS_SIZE = 52 + private const val ERROR_INVALID_CARDS_SIZE = "카드덱 초기 사이즈는 52장이어야 합니다." + private const val ERROR_EXIST_DUPLICATE_CARDS = "카드덱에는 중복이 없어야 합니다." + } +} diff --git a/src/test/kotlin/blackjack/domain/CardDeckTest.kt b/src/test/kotlin/blackjack/domain/CardDeckTest.kt new file mode 100644 index 000000000..18c7e005e --- /dev/null +++ b/src/test/kotlin/blackjack/domain/CardDeckTest.kt @@ -0,0 +1,35 @@ +package blackjack.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +class CardDeckTest { + + @Test + fun `카드덱 잘 있는지 확인`() { + val cardDeck = CardDeck(Card.all()) + assertThat(cardDeck.size).isEqualTo(52) + } + + @Test + fun `카드 draw가 잘 되었는지 확인`() { + val cardDeck = CardDeck(Card.all()) + cardDeck.draw() + assertThat(cardDeck.size).isEqualTo(51) + } + + @Test + fun `초기 카드는 52장이다`() { + assertThrows { + CardDeck(Card.all().take(51)) + } + } + + @Test + fun `카드는 중복이 없어야 합니다`() { + assertThrows { + CardDeck(Card.all() + Card.all()) + } + } +} From ebb3b1b5646bb7317b610d4630226b45037eec96 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 15:24:38 +0900 Subject: [PATCH 06/67] =?UTF-8?q?feat(Cards)=20:=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EA=B0=81=EC=9E=90=EC=9D=98=20=EC=B9=B4=EB=93=9C=ED=8C=A8?= =?UTF-8?q?=EB=A5=BC=20=EB=A7=8C=EB=93=A0=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/blackjack/domain/Card.kt | 2 +- src/main/kotlin/blackjack/domain/Cards.kt | 10 ++++++++++ src/test/kotlin/blackjack/domain/CardsTest.kt | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/Cards.kt create mode 100644 src/test/kotlin/blackjack/domain/CardsTest.kt diff --git a/docs/README.md b/docs/README.md index 1b41d40a9..6bed07a4f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,7 +4,7 @@ - [X] 숫자를 구현한다. - [X] 카드덱을 만든다. - [ ] 딜러랑 유저를 만든다. - - [ ] 유저 각자의 카드덱을 만든다. + - [X] 유저 각자의 카드덱을 만든다. - [ ] 카드의 합을 구한다. - [ ] 최종결과를 반환한다. diff --git a/src/main/kotlin/blackjack/domain/Card.kt b/src/main/kotlin/blackjack/domain/Card.kt index 75a1aa003..d075010b0 100644 --- a/src/main/kotlin/blackjack/domain/Card.kt +++ b/src/main/kotlin/blackjack/domain/Card.kt @@ -1,6 +1,6 @@ package blackjack.domain -class Card(val mark: CardMark, val value: CardValue) { +data class Card(val mark: CardMark, val value: CardValue) { companion object { private val CARDS: List = diff --git a/src/main/kotlin/blackjack/domain/Cards.kt b/src/main/kotlin/blackjack/domain/Cards.kt new file mode 100644 index 000000000..a85028d51 --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Cards.kt @@ -0,0 +1,10 @@ +package blackjack.domain + +class Cards(private val cards: MutableSet = mutableSetOf()) { + val size: Int + get() = cards.size + + fun toList() = cards.toList() + + fun add(card: Card) = cards.add(card) +} diff --git a/src/test/kotlin/blackjack/domain/CardsTest.kt b/src/test/kotlin/blackjack/domain/CardsTest.kt new file mode 100644 index 000000000..4ce47f665 --- /dev/null +++ b/src/test/kotlin/blackjack/domain/CardsTest.kt @@ -0,0 +1,16 @@ +package blackjack.domain + +import blackjack.domain.CardMark.CLOVER +import blackjack.domain.CardValue.EIGHT +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class CardsTest { + @Test + fun `카드를 추가할 수 있다`() { + val cards = Cards() + cards.add(Card(CLOVER, EIGHT)) + assertThat(cards.size).isEqualTo(1) + assertThat(cards.toList()[0]).isEqualTo(Card(CLOVER, EIGHT)) + } +} From 99211ec58e0377cb1841b378ba36f9b109af80fc Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 15:56:51 +0900 Subject: [PATCH 07/67] =?UTF-8?q?feat(User)=20:=20=EB=94=9C=EB=9F=AC?= =?UTF-8?q?=EB=9E=91=20=EC=9C=A0=EC=A0=80=EB=A5=BC=20=EB=A7=8C=EB=93=A0?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/blackjack/domain/User.kt | 17 ++++++++++++ src/test/kotlin/blackjack/domain/UserTest.kt | 29 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/blackjack/domain/User.kt create mode 100644 src/test/kotlin/blackjack/domain/UserTest.kt diff --git a/docs/README.md b/docs/README.md index 6bed07a4f..3ddfe0d6c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3,7 +3,7 @@ - [X] 문양을 구현한다. - [X] 숫자를 구현한다. - [X] 카드덱을 만든다. -- [ ] 딜러랑 유저를 만든다. +- [X] 딜러랑 유저를 만든다. - [X] 유저 각자의 카드덱을 만든다. - [ ] 카드의 합을 구한다. - [ ] 최종결과를 반환한다. diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt new file mode 100644 index 000000000..e25459c9b --- /dev/null +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -0,0 +1,17 @@ +package blackjack.domain + +class User(val name: String) { + val cards = Cards() + val score: Int + get() { + var score = cards.toList().sumOf { it.value.value } + if (score > 21 && cards.toList().filter { it.value == CardValue.ACE }.isNotEmpty()) { + score -= 10 + } + return score + } + + fun draw(card: Card) { + cards.add(card) + } +} diff --git a/src/test/kotlin/blackjack/domain/UserTest.kt b/src/test/kotlin/blackjack/domain/UserTest.kt new file mode 100644 index 000000000..b8bf5566b --- /dev/null +++ b/src/test/kotlin/blackjack/domain/UserTest.kt @@ -0,0 +1,29 @@ +package blackjack.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class UserTest { + @Test + fun `카드를 뽑을 수 있다`() { + val user = User("아크") + user.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) + assertThat(user.cards.size).isEqualTo(1) + } + + @Test + fun `점수의 합을 반환한다`() { + val user = User("아크") + user.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) + user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + assertThat(user.score).isEqualTo(16) + } +} + +/* +User가 할일은? +카드를 가지고 있고 +점수를 가지고 있고 +더 갈지 안갈지 입력받고 처리한다. + + */ From 3505d6fd32005121dbebd821abae617d25e6d9df Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 16:45:06 +0900 Subject: [PATCH 08/67] =?UTF-8?q?docs(README)=20:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=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 --- docs/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/README.md b/docs/README.md index 3ddfe0d6c..b82d6a22d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,6 +6,8 @@ - [X] 딜러랑 유저를 만든다. - [X] 유저 각자의 카드덱을 만든다. - [ ] 카드의 합을 구한다. +- [ ] 게임 초기 세팅을 한다. +- [ ] 중간결과를 반환한다. - [ ] 최종결과를 반환한다. ### 입력 구현 목록 From 7240f041c2de0bd60d12afe875a8beb11834b107 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 17:06:08 +0900 Subject: [PATCH 09/67] =?UTF-8?q?feat(BlackJackGame)=20:=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EC=B4=88=EA=B8=B0=20=EC=84=B8=ED=8C=85=EC=9D=84=20?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/domain/BlackJackGame.kt | 18 +++++++++++++++ .../blackjack/domain/BlackJackGameTest.kt | 22 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/main/kotlin/blackjack/domain/BlackJackGame.kt create mode 100644 src/test/kotlin/blackjack/domain/BlackJackGameTest.kt diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt new file mode 100644 index 000000000..775d0470f --- /dev/null +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -0,0 +1,18 @@ +package blackjack.domain + +class BlackJackGame(names: List) { + val dealer = User("딜러") + private val cardDeck = CardDeck(Card.all()) + val users: List = names.map { User(it) } + + fun setUp() { + cardDeck.shuffle() + dealer.draw(cardDeck.draw()) + dealer.draw(cardDeck.draw()) + + users.map { + it.draw(cardDeck.draw()) + it.draw(cardDeck.draw()) + } + } +} diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt new file mode 100644 index 000000000..3703b6267 --- /dev/null +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -0,0 +1,22 @@ +package blackjack.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class BlackJackGameTest { + + @Test + fun `초기세팅이 된다`() { + // given + val names = listOf("아크", "로피") + // when + val blackJackGame = BlackJackGame(names) + blackJackGame.setUp() + + // then + assertThat(blackJackGame.dealer.cards.size).isEqualTo(2) + blackJackGame.users.forEach { user -> + assertThat(user.cards.size).isEqualTo(2) + } + } +} From a5e59e7e241253ee61bf47dad0ce7d3957a6a585 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Tue, 28 Feb 2023 20:18:17 +0900 Subject: [PATCH 10/67] =?UTF-8?q?feat(BlackJackGame)=20:=20=EC=A4=91?= =?UTF-8?q?=EA=B0=84=EA=B2=B0=EA=B3=BC=EB=A5=BC=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 +-- .../kotlin/blackjack/domain/BlackJackGame.kt | 25 ++++++++++++++++++- .../kotlin/blackjack/domain/GameStatus.kt | 7 ++++++ .../blackjack/domain/BlackJackGameTest.kt | 25 +++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/GameStatus.kt diff --git a/docs/README.md b/docs/README.md index b82d6a22d..0dcd70600 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,8 +6,8 @@ - [X] 딜러랑 유저를 만든다. - [X] 유저 각자의 카드덱을 만든다. - [ ] 카드의 합을 구한다. -- [ ] 게임 초기 세팅을 한다. -- [ ] 중간결과를 반환한다. +- [X] 게임 초기 세팅을 한다. +- [X] 중간결과를 반환한다. - [ ] 최종결과를 반환한다. ### 입력 구현 목록 diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 775d0470f..ad92eed91 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -4,8 +4,12 @@ class BlackJackGame(names: List) { val dealer = User("딜러") private val cardDeck = CardDeck(Card.all()) val users: List = names.map { User(it) } + var userIndex: Int = 0 + private var status: GameStatus = GameStatus.START - fun setUp() { + val isRunning + get() = status == GameStatus.RUNNING + fun setUp(): User { cardDeck.shuffle() dealer.draw(cardDeck.draw()) dealer.draw(cardDeck.draw()) @@ -14,5 +18,24 @@ class BlackJackGame(names: List) { it.draw(cardDeck.draw()) it.draw(cardDeck.draw()) } + status = GameStatus.RUNNING + return users[userIndex] + } + + fun progress(user: User, command: String): User { + when (command) { + "y" -> { + user.draw(cardDeck.draw()) + if (user.score >= 21) { + userIndex++ + } + if (userIndex >= users.size) { + status = GameStatus.END + return User("") + } + } + "n" -> userIndex++ + } + return users[userIndex] } } diff --git a/src/main/kotlin/blackjack/domain/GameStatus.kt b/src/main/kotlin/blackjack/domain/GameStatus.kt new file mode 100644 index 000000000..8eb11e15b --- /dev/null +++ b/src/main/kotlin/blackjack/domain/GameStatus.kt @@ -0,0 +1,7 @@ +package blackjack.domain + +enum class GameStatus { + START, + RUNNING, + END, +} diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index 3703b6267..27e1d456d 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -19,4 +19,29 @@ class BlackJackGameTest { assertThat(user.cards.size).isEqualTo(2) } } + + @Test + fun `y를 누를 누르면 한장이 뽑아진다`() { + // given + val names = listOf("아크", "로피") + // when + val blackJackGame = BlackJackGame(names) + val user = blackJackGame.setUp() + blackJackGame.progress(user, "y") + + // then + assertThat(user.cards.size).isEqualTo(3) + } + + @Test + fun `n을 누르면 다음 사람 차례다`() { + // given + val names = listOf("아크", "로피") + // when + val blackJackGame = BlackJackGame(names) + val user = blackJackGame.setUp() + blackJackGame.progress(user, "n") + + assertThat(blackJackGame.userIndex).isEqualTo(1) + } } From 389045287a6bff2af44bb61b1d091a3cc8ead17b Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 14:38:00 +0900 Subject: [PATCH 11/67] =?UTF-8?q?feat(BlackJackGame)=20:=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=EC=9D=98=20=ED=95=A9=EC=9D=84=20=EA=B5=AC=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/blackjack/domain/CardValue.kt | 2 +- src/main/kotlin/blackjack/domain/Cards.kt | 2 ++ src/main/kotlin/blackjack/domain/User.kt | 10 ++++++++-- .../kotlin/blackjack/domain/CardValueTest.kt | 4 ++-- src/test/kotlin/blackjack/domain/CardsTest.kt | 10 ++++++++++ src/test/kotlin/blackjack/domain/UserTest.kt | 16 ++++++++++++++++ 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/docs/README.md b/docs/README.md index 0dcd70600..18105e117 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,7 +5,7 @@ - [X] 카드덱을 만든다. - [X] 딜러랑 유저를 만든다. - [X] 유저 각자의 카드덱을 만든다. -- [ ] 카드의 합을 구한다. +- [X] 카드의 합을 구한다. - [X] 게임 초기 세팅을 한다. - [X] 중간결과를 반환한다. - [ ] 최종결과를 반환한다. diff --git a/src/main/kotlin/blackjack/domain/CardValue.kt b/src/main/kotlin/blackjack/domain/CardValue.kt index cc92f4368..b4e80a095 100644 --- a/src/main/kotlin/blackjack/domain/CardValue.kt +++ b/src/main/kotlin/blackjack/domain/CardValue.kt @@ -1,7 +1,7 @@ package blackjack.domain enum class CardValue(val value: Int) { - ACE(11), + ACE(1), KING(10), QUEEN(10), JACK(10), diff --git a/src/main/kotlin/blackjack/domain/Cards.kt b/src/main/kotlin/blackjack/domain/Cards.kt index a85028d51..a13b9a3c9 100644 --- a/src/main/kotlin/blackjack/domain/Cards.kt +++ b/src/main/kotlin/blackjack/domain/Cards.kt @@ -7,4 +7,6 @@ class Cards(private val cards: MutableSet = mutableSetOf()) { fun toList() = cards.toList() fun add(card: Card) = cards.add(card) + + fun containsACE() = cards.map { it.value }.contains(CardValue.ACE) } diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index e25459c9b..89563455b 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -3,10 +3,16 @@ package blackjack.domain class User(val name: String) { val cards = Cards() val score: Int + get() = if (maxScore <= 21) maxScore else minScore + + val minScore: Int + get() = cards.toList().sumOf { it.value.value } + + val maxScore: Int get() { var score = cards.toList().sumOf { it.value.value } - if (score > 21 && cards.toList().filter { it.value == CardValue.ACE }.isNotEmpty()) { - score -= 10 + if (cards.containsACE() && score <= 21 - 10) { + score += 10 } return score } diff --git a/src/test/kotlin/blackjack/domain/CardValueTest.kt b/src/test/kotlin/blackjack/domain/CardValueTest.kt index 1fa86dfd0..753092aa3 100644 --- a/src/test/kotlin/blackjack/domain/CardValueTest.kt +++ b/src/test/kotlin/blackjack/domain/CardValueTest.kt @@ -6,8 +6,8 @@ import org.junit.jupiter.api.Test class CardValueTest { @Test - fun `A의 값은 11이다`() { - assertThat(CardValue.ACE.value).isEqualTo(11) + fun `A의 값은 1이다`() { + assertThat(CardValue.ACE.value).isEqualTo(1) } @Test diff --git a/src/test/kotlin/blackjack/domain/CardsTest.kt b/src/test/kotlin/blackjack/domain/CardsTest.kt index 4ce47f665..3864433bc 100644 --- a/src/test/kotlin/blackjack/domain/CardsTest.kt +++ b/src/test/kotlin/blackjack/domain/CardsTest.kt @@ -1,6 +1,7 @@ package blackjack.domain import blackjack.domain.CardMark.CLOVER +import blackjack.domain.CardValue.ACE import blackjack.domain.CardValue.EIGHT import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test @@ -13,4 +14,13 @@ class CardsTest { assertThat(cards.size).isEqualTo(1) assertThat(cards.toList()[0]).isEqualTo(Card(CLOVER, EIGHT)) } + + @Test + fun `ACE 카드가 있는지 확인 할 수 있다`() { + val cards = Cards() + cards.add(Card(CLOVER, EIGHT)) + assertThat(cards.containsACE()).isFalse + cards.add(Card(CLOVER, ACE)) + assertThat(cards.containsACE()).isTrue + } } diff --git a/src/test/kotlin/blackjack/domain/UserTest.kt b/src/test/kotlin/blackjack/domain/UserTest.kt index b8bf5566b..1ef51b173 100644 --- a/src/test/kotlin/blackjack/domain/UserTest.kt +++ b/src/test/kotlin/blackjack/domain/UserTest.kt @@ -18,6 +18,22 @@ class UserTest { user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) assertThat(user.score).isEqualTo(16) } + + @Test + fun `나올 수 있는 최저 점수를 반환한다`() { + val user = User("아크") + user.draw(Card(CardMark.CLOVER, CardValue.ACE)) + user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + assertThat(user.minScore).isEqualTo(9) + } + + @Test + fun `나올 수 있는 최고 점수를 반환한다`() { + val user = User("아크") + user.draw(Card(CardMark.CLOVER, CardValue.ACE)) + user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + assertThat(user.maxScore).isEqualTo(19) + } } /* From 018f574ffca08dd61775c7cd967a0990b7380798 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 15:29:29 +0900 Subject: [PATCH 12/67] =?UTF-8?q?feat(BlackJackGame)=20:=20=EC=B5=9C?= =?UTF-8?q?=EC=A2=85=EA=B2=B0=EA=B3=BC=EB=A5=BC=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../kotlin/blackjack/domain/BlackJackGame.kt | 17 ++-- src/main/kotlin/blackjack/domain/Outcome.kt | 22 +++++ .../blackjack/domain/BlackJackGameTest.kt | 11 +++ .../kotlin/blackjack/domain/OutcomeTest.kt | 94 +++++++++++++++++++ 5 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/Outcome.kt create mode 100644 src/test/kotlin/blackjack/domain/OutcomeTest.kt diff --git a/docs/README.md b/docs/README.md index 18105e117..e2f0c6436 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,7 +8,7 @@ - [X] 카드의 합을 구한다. - [X] 게임 초기 세팅을 한다. - [X] 중간결과를 반환한다. -- [ ] 최종결과를 반환한다. +- [X] 최종결과를 반환한다. ### 입력 구현 목록 - [ ] 사람들의 이름을 입력받는다. diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index ad92eed91..846e5aac4 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -26,16 +26,19 @@ class BlackJackGame(names: List) { when (command) { "y" -> { user.draw(cardDeck.draw()) - if (user.score >= 21) { - userIndex++ - } - if (userIndex >= users.size) { - status = GameStatus.END - return User("") - } + if (user.minScore >= 21) { userIndex++ } } "n" -> userIndex++ } + if (userIndex >= users.size) { + status = GameStatus.END + while (dealer.maxScore < 17) { dealer.draw(cardDeck.draw()) } + return User("") + } return users[userIndex] } + + fun getResult(): List { + return users.map { user -> Outcome.of(dealer, user) } + } } diff --git a/src/main/kotlin/blackjack/domain/Outcome.kt b/src/main/kotlin/blackjack/domain/Outcome.kt new file mode 100644 index 000000000..7629cf511 --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Outcome.kt @@ -0,0 +1,22 @@ +package blackjack.domain + +import java.lang.IllegalStateException + +enum class Outcome { + WIN, DRAW, LOSE; + + companion object { + fun of(dealer: User, user: User): Outcome = + when { + dealer.score > 21 && user.score > 21 -> DRAW + dealer.score > 21 -> WIN + user.score > 21 -> LOSE + + dealer.score == user.score -> DRAW + user.score > dealer.score -> WIN + dealer.score > user.score -> LOSE + + else -> throw IllegalStateException() + } + } +} diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index 27e1d456d..3004ddcd9 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -44,4 +44,15 @@ class BlackJackGameTest { assertThat(blackJackGame.userIndex).isEqualTo(1) } + + @Test + fun `게임의 결과를 반환한다`() { + // given + val names = listOf("아크", "로피") + // when + val blackJackGame = BlackJackGame(names) + blackJackGame.setUp() + // then + assertThat(blackJackGame.getResult().size).isEqualTo(2) + } } diff --git a/src/test/kotlin/blackjack/domain/OutcomeTest.kt b/src/test/kotlin/blackjack/domain/OutcomeTest.kt new file mode 100644 index 000000000..8fcbcc510 --- /dev/null +++ b/src/test/kotlin/blackjack/domain/OutcomeTest.kt @@ -0,0 +1,94 @@ +package blackjack.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class OutcomeTest { + @Test + fun `딜러와 유저의 점수가 같으면 비긴다`() { + val dealer = User("딜러") + val user = User("아크") + dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) + dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) + + user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + user.draw(Card(CardMark.HEART, CardValue.SEVEN)) + + assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.DRAW) + } + + @Test + fun `딜러와 유저의 점수가 둘 다 21점 이상이면 비긴다`() { + val dealer = User("딜러") + val user = User("아크") + dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) + dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) + dealer.draw(Card(CardMark.HEART, CardValue.NINE)) + + user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + user.draw(Card(CardMark.HEART, CardValue.SEVEN)) + user.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) + + assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.DRAW) + } + + @Test + fun `딜러 점수가 유저 점수보다 크면 진다`() { + val dealer = User("딜러") + val user = User("아크") + dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) + dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) + dealer.draw(Card(CardMark.HEART, CardValue.THREE)) + + user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + user.draw(Card(CardMark.HEART, CardValue.SEVEN)) + user.draw(Card(CardMark.CLOVER, CardValue.TWO)) + + assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.LOSE) + } + + @Test + fun `유저 점수가 21을 넘으면 진다`() { + val dealer = User("딜러") + val user = User("아크") + dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) + dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) + dealer.draw(Card(CardMark.HEART, CardValue.THREE)) + + user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + user.draw(Card(CardMark.HEART, CardValue.SEVEN)) + user.draw(Card(CardMark.CLOVER, CardValue.KING)) + + assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.LOSE) + } + + @Test + fun `유저 점수가 딜러 점수보다 크면 이긴다`() { + val dealer = User("딜러") + val user = User("아크") + dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) + dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) + dealer.draw(Card(CardMark.HEART, CardValue.TWO)) + + user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + user.draw(Card(CardMark.HEART, CardValue.SEVEN)) + user.draw(Card(CardMark.CLOVER, CardValue.THREE)) + + assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.WIN) + } + + @Test + fun `딜러 점수가 21을 넘으면 이긴다`() { + val dealer = User("딜러") + val user = User("아크") + dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) + dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) + dealer.draw(Card(CardMark.HEART, CardValue.KING)) + + user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + user.draw(Card(CardMark.HEART, CardValue.SEVEN)) + user.draw(Card(CardMark.CLOVER, CardValue.THREE)) + + assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.WIN) + } +} From 59766eab6f8d5fe736a19f1e33bfc2be190f8141 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 15:40:16 +0900 Subject: [PATCH 13/67] =?UTF-8?q?feat(InputView)=20:=20=EC=82=AC=EB=9E=8C?= =?UTF-8?q?=EB=93=A4=EC=9D=98=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EB=B0=9B=EB=8A=94=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../controller/BlackJackController.kt | 19 +++++++++++++++++++ src/main/kotlin/blackjack/view/InputView.kt | 8 ++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/blackjack/controller/BlackJackController.kt create mode 100644 src/main/kotlin/blackjack/view/InputView.kt diff --git a/docs/README.md b/docs/README.md index e2f0c6436..131bb8769 100644 --- a/docs/README.md +++ b/docs/README.md @@ -11,7 +11,7 @@ - [X] 최종결과를 반환한다. ### 입력 구현 목록 -- [ ] 사람들의 이름을 입력받는다. +- [X] 사람들의 이름을 입력받는다. - [ ] 카드를 더 받을지 입력 받는다. ### 출력 구현 목록 - [ ] 나눠준 카드를 출력한다. diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt new file mode 100644 index 000000000..5f7beefd0 --- /dev/null +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -0,0 +1,19 @@ +package blackjack.controller + +import blackjack.domain.BlackJackGame +import blackjack.domain.Card +import blackjack.domain.CardDeck +import blackjack.view.InputView +import blackjack.view.OutputView + +class BlackJackController( + val inputView: InputView, + val outputView: OutputView, +) { + fun run() { + val cardDeck = CardDeck(Card.all()) + cardDeck.shuffle() + val names = inputView.inputParticipants() + val blackJackGame = BlackJackGame(names) + } +} diff --git a/src/main/kotlin/blackjack/view/InputView.kt b/src/main/kotlin/blackjack/view/InputView.kt new file mode 100644 index 000000000..a2a2ea854 --- /dev/null +++ b/src/main/kotlin/blackjack/view/InputView.kt @@ -0,0 +1,8 @@ +package blackjack.view + +class InputView { + fun inputParticipants(): List { + println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)") + return readln().split(",").map { it.trim() } + } +} From b5a2618d6ceaec2ae2aa5016d7a128ea33cc4254 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 16:07:19 +0900 Subject: [PATCH 14/67] =?UTF-8?q?feat(OutputView)=20:=20=EB=82=98=EB=88=A0?= =?UTF-8?q?=EC=A4=80=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../controller/BlackJackController.kt | 2 ++ src/main/kotlin/blackjack/view/OutputView.kt | 29 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/blackjack/view/OutputView.kt diff --git a/docs/README.md b/docs/README.md index 131bb8769..0f5cda2b4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -14,6 +14,6 @@ - [X] 사람들의 이름을 입력받는다. - [ ] 카드를 더 받을지 입력 받는다. ### 출력 구현 목록 -- [ ] 나눠준 카드를 출력한다. +- [X] 나눠준 카드를 출력한다. - [ ] 현재 카드 상태를 출력한다. - [ ] 카드의 결과를 반환한다. \ No newline at end of file diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 5f7beefd0..22116e609 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -15,5 +15,7 @@ class BlackJackController( cardDeck.shuffle() val names = inputView.inputParticipants() val blackJackGame = BlackJackGame(names) + var user = blackJackGame.setUp() + outputView.outputInitState(blackJackGame.dealer, blackJackGame.users) } } diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt new file mode 100644 index 000000000..8e9f46226 --- /dev/null +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -0,0 +1,29 @@ +package blackjack.view + +import blackjack.domain.Outcome +import blackjack.domain.User + +class OutputView { + fun outputInitState(dealer: User, users: List) { + println("\n딜러와 ${users.map{it.name}} 에게 2장의 나누었습니다.") + outputCard(dealer) + println("") + + users.forEach { user -> + outputCard(user) + println("") + } + } + + fun outputCard(user: User) { + println("## 최종 승패") + print("${user.name}카드") + user.cards.toList().forEach { + print(" ${it.value.value}${it.mark.title}") + } + } + + private fun outputScore(user: User) { + print("- 결과 ${user.score}") + } +} From a3a1cf8ca27b266efc74a5bd7c88620beb2c5a3f Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 16:08:52 +0900 Subject: [PATCH 15/67] =?UTF-8?q?feat(InputView)=20:=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EB=8D=94=20=EB=B0=9B=EC=9D=84=EC=A7=80=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EB=B0=9B=EB=8A=94=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/blackjack/controller/BlackJackController.kt | 5 +++++ src/main/kotlin/blackjack/view/InputView.kt | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 0f5cda2b4..55aafae39 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,7 @@ ### 입력 구현 목록 - [X] 사람들의 이름을 입력받는다. -- [ ] 카드를 더 받을지 입력 받는다. +- [X] 카드를 더 받을지 입력 받는다. ### 출력 구현 목록 - [X] 나눠준 카드를 출력한다. - [ ] 현재 카드 상태를 출력한다. diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 22116e609..036ae97ee 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -17,5 +17,10 @@ class BlackJackController( val blackJackGame = BlackJackGame(names) var user = blackJackGame.setUp() outputView.outputInitState(blackJackGame.dealer, blackJackGame.users) + + while (blackJackGame.isRunning) { + val prev = user + user = blackJackGame.progress(user, inputView.inputDrawMore(user.name)) + } } } diff --git a/src/main/kotlin/blackjack/view/InputView.kt b/src/main/kotlin/blackjack/view/InputView.kt index a2a2ea854..0003a26f1 100644 --- a/src/main/kotlin/blackjack/view/InputView.kt +++ b/src/main/kotlin/blackjack/view/InputView.kt @@ -5,4 +5,9 @@ class InputView { println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)") return readln().split(",").map { it.trim() } } + + fun inputDrawMore(name: String): String { + println("\n${name}는 한장의 카드를 더 받겠습니까?") + return readln() + } } From de0c72f2e214e94fd9853c506f0c4b40bf530efc Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 16:10:10 +0900 Subject: [PATCH 16/67] =?UTF-8?q?feat(OutPutView)=20:=20=ED=98=84=EC=9E=AC?= =?UTF-8?q?=20=EC=B9=B4=EB=93=9C=20=EC=83=81=ED=83=9C=EB=A5=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/blackjack/controller/BlackJackController.kt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 55aafae39..a0a8a5fc0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -15,5 +15,5 @@ - [X] 카드를 더 받을지 입력 받는다. ### 출력 구현 목록 - [X] 나눠준 카드를 출력한다. -- [ ] 현재 카드 상태를 출력한다. +- [X] 현재 카드 상태를 출력한다. - [ ] 카드의 결과를 반환한다. \ No newline at end of file diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 036ae97ee..83f3dace1 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -21,6 +21,7 @@ class BlackJackController( while (blackJackGame.isRunning) { val prev = user user = blackJackGame.progress(user, inputView.inputDrawMore(user.name)) + outputView.outputCard(prev) } } } From 466c804542bd0ab5c9e76a52aed85eeff01375ff Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 16:11:31 +0900 Subject: [PATCH 17/67] =?UTF-8?q?feat(OutPutView)=20:=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EC=9D=98=20=EA=B2=B0=EA=B3=BC=EB=A5=BC=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/blackjack/Application.kt | 10 +++++++ .../controller/BlackJackController.kt | 2 ++ src/main/kotlin/blackjack/view/OutputView.kt | 28 +++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/blackjack/Application.kt diff --git a/docs/README.md b/docs/README.md index a0a8a5fc0..c95608ed1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,4 +16,4 @@ ### 출력 구현 목록 - [X] 나눠준 카드를 출력한다. - [X] 현재 카드 상태를 출력한다. -- [ ] 카드의 결과를 반환한다. \ No newline at end of file +- [X] 카드의 결과를 반환한다. \ No newline at end of file diff --git a/src/main/kotlin/blackjack/Application.kt b/src/main/kotlin/blackjack/Application.kt new file mode 100644 index 000000000..fca60eb48 --- /dev/null +++ b/src/main/kotlin/blackjack/Application.kt @@ -0,0 +1,10 @@ +package blackjack + +import blackjack.controller.BlackJackController +import blackjack.view.InputView +import blackjack.view.OutputView + +fun main() { + val blackJackController = BlackJackController(InputView(), OutputView()) + blackJackController.run() +} diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 83f3dace1..d01802048 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -23,5 +23,7 @@ class BlackJackController( user = blackJackGame.progress(user, inputView.inputDrawMore(user.name)) outputView.outputCard(prev) } + + outputView.outputResult(blackJackGame.dealer, blackJackGame.users, blackJackGame.getResult()) } } diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index 8e9f46226..6d93d9697 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -15,6 +15,21 @@ class OutputView { } } + fun outputResult(dealer: User, users: List, outcomes: List) { + println("") + outputCard(dealer) + outputScore(dealer) + println("") + + users.forEach { user -> + outputCard(user) + outputScore(user) + println("") + } + println("") + outputOutcomes(dealer, users, outcomes) + } + fun outputCard(user: User) { println("## 최종 승패") print("${user.name}카드") @@ -26,4 +41,17 @@ class OutputView { private fun outputScore(user: User) { print("- 결과 ${user.score}") } + + private fun outputOutcomes(dealer: User, users: List, outcomes: List) { + println("${dealer.name}: ${outcomes.count{ it == Outcome.LOSE}}승 ${outcomes.count{ it == Outcome.WIN}}패") + users.forEachIndexed { index, user -> outputOutcome(user, outcomes[index]) } + } + + fun outputOutcome(user: User, outcome: Outcome) { + when (outcome) { + Outcome.WIN -> "승" + Outcome.DRAW -> "무" + Outcome.LOSE -> "패" + }.let { println("${user.name}: $it") } + } } From 1bc13ec52210e4559b2d2c73dfce04d2493f0a9b Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 19:43:49 +0900 Subject: [PATCH 18/67] =?UTF-8?q?feat(BlackJackGame)=20:=20=EB=B8=94?= =?UTF-8?q?=EB=9E=99=EC=9E=AD=20=EA=B2=8C=EC=9E=84=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=20DSL=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.kt | 25 ++++---- src/main/kotlin/blackjack/domain/BlackJack.kt | 10 +++ .../blackjack/domain/BlackJackBuilder.kt | 34 +++++++++++ .../kotlin/blackjack/domain/BlackJackGame.kt | 51 ++++++---------- src/main/kotlin/blackjack/domain/CardDeck.kt | 2 - src/main/kotlin/blackjack/view/OutputView.kt | 2 +- .../blackjack/domain/BlackJackBuilderTest.kt | 25 ++++++++ .../blackjack/domain/BlackJackGameTest.kt | 61 +++++-------------- .../kotlin/blackjack/domain/BlackJackTest.kt | 23 +++++++ 9 files changed, 140 insertions(+), 93 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/BlackJack.kt create mode 100644 src/main/kotlin/blackjack/domain/BlackJackBuilder.kt create mode 100644 src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt create mode 100644 src/test/kotlin/blackjack/domain/BlackJackTest.kt diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index d01802048..04921eb98 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -1,8 +1,8 @@ package blackjack.controller +import blackjack.domain.BlackJackBuilder import blackjack.domain.BlackJackGame import blackjack.domain.Card -import blackjack.domain.CardDeck import blackjack.view.InputView import blackjack.view.OutputView @@ -11,19 +11,18 @@ class BlackJackController( val outputView: OutputView, ) { fun run() { - val cardDeck = CardDeck(Card.all()) - cardDeck.shuffle() - val names = inputView.inputParticipants() - val blackJackGame = BlackJackGame(names) - var user = blackJackGame.setUp() - outputView.outputInitState(blackJackGame.dealer, blackJackGame.users) - - while (blackJackGame.isRunning) { - val prev = user - user = blackJackGame.progress(user, inputView.inputDrawMore(user.name)) - outputView.outputCard(prev) + val blackJack = BlackJackBuilder.init { + cardDeck(Card.all().shuffled()) + dealer("딜러") + users(inputView.inputParticipants()) } + outputView.outputInitState(blackJack.dealer, blackJack.users) - outputView.outputResult(blackJackGame.dealer, blackJackGame.users, blackJackGame.getResult()) + BlackJackGame().apply { + input(inputView::inputDrawMore) + output(outputView::outputCard) + run(blackJack) + } + outputView.outputResult(blackJack.dealer, blackJack.users, blackJack.result) } } diff --git a/src/main/kotlin/blackjack/domain/BlackJack.kt b/src/main/kotlin/blackjack/domain/BlackJack.kt new file mode 100644 index 000000000..de85d5383 --- /dev/null +++ b/src/main/kotlin/blackjack/domain/BlackJack.kt @@ -0,0 +1,10 @@ +package blackjack.domain + +data class BlackJack( + val dealer: User, + val users: List, + val cardDeck: CardDeck, +) { + val result: List + get() = users.map { user -> Outcome.of(dealer, user) } +} diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt new file mode 100644 index 000000000..16d8cc57b --- /dev/null +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -0,0 +1,34 @@ +package blackjack.domain + +class BlackJackBuilder { + private lateinit var cardDeck: CardDeck + private lateinit var dealer: User + private lateinit var users: List + + fun cardDeck(cards: List) { + cardDeck = CardDeck(cards) + } + + fun dealer(name: String) { + dealer = User(name) + dealer.draw(cardDeck.draw()) + dealer.draw(cardDeck.draw()) + } + + fun users(names: List) { + users = names.map { User(it) } + users.map { + it.draw(cardDeck.draw()) + it.draw(cardDeck.draw()) + } + } + + fun build(): BlackJack { + return BlackJack(dealer, users, cardDeck) + } + + companion object { + fun init(block: BlackJackBuilder.() -> Unit): BlackJack = + BlackJackBuilder().apply(block).build() + } +} diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 846e5aac4..5902a297f 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -1,44 +1,31 @@ package blackjack.domain -class BlackJackGame(names: List) { - val dealer = User("딜러") - private val cardDeck = CardDeck(Card.all()) - val users: List = names.map { User(it) } - var userIndex: Int = 0 - private var status: GameStatus = GameStatus.START +class BlackJackGame() { + lateinit var input: (String) -> String + lateinit var output: (User) -> Unit - val isRunning - get() = status == GameStatus.RUNNING - fun setUp(): User { - cardDeck.shuffle() - dealer.draw(cardDeck.draw()) - dealer.draw(cardDeck.draw()) + fun input(func: (String) -> String) { + input = func + } - users.map { - it.draw(cardDeck.draw()) - it.draw(cardDeck.draw()) - } - status = GameStatus.RUNNING - return users[userIndex] + fun output(func: (User) -> Unit) { + output = func } - fun progress(user: User, command: String): User { - when (command) { - "y" -> { - user.draw(cardDeck.draw()) - if (user.minScore >= 21) { userIndex++ } - } - "n" -> userIndex++ - } - if (userIndex >= users.size) { - status = GameStatus.END + fun run(blackJack: BlackJack) { + blackJack.run { + users.forEach { user -> command(user, blackJack.cardDeck) } while (dealer.maxScore < 17) { dealer.draw(cardDeck.draw()) } - return User("") } - return users[userIndex] } - fun getResult(): List { - return users.map { user -> Outcome.of(dealer, user) } + fun command(user: User, cardDeck: CardDeck) { + if (input(user.name) == "y") { + user.draw(cardDeck.draw()) + output(user) + if (user.minScore < 21) { + command(user, cardDeck) + } + } } } diff --git a/src/main/kotlin/blackjack/domain/CardDeck.kt b/src/main/kotlin/blackjack/domain/CardDeck.kt index 335af71df..d2c2510e4 100644 --- a/src/main/kotlin/blackjack/domain/CardDeck.kt +++ b/src/main/kotlin/blackjack/domain/CardDeck.kt @@ -11,8 +11,6 @@ class CardDeck(cards: List) { val size: Int get() = cards.size - fun shuffle() = cards.shuffle() - fun draw(): Card = cards.removeFirst() companion object { diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index 6d93d9697..2e30ef639 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -31,7 +31,6 @@ class OutputView { } fun outputCard(user: User) { - println("## 최종 승패") print("${user.name}카드") user.cards.toList().forEach { print(" ${it.value.value}${it.mark.title}") @@ -43,6 +42,7 @@ class OutputView { } private fun outputOutcomes(dealer: User, users: List, outcomes: List) { + println("## 최종 승패") println("${dealer.name}: ${outcomes.count{ it == Outcome.LOSE}}승 ${outcomes.count{ it == Outcome.WIN}}패") users.forEachIndexed { index, user -> outputOutcome(user, outcomes[index]) } } diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt new file mode 100644 index 000000000..e255f73cd --- /dev/null +++ b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt @@ -0,0 +1,25 @@ +package blackjack.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll + +class BlackJackBuilderTest { + @Test + fun `세팅이 된다`() { + val blackJack = BlackJackBuilder.init { + cardDeck(Card.all().shuffled()) + dealer("딜러") + users(listOf("아크", "로피")) + } + assertAll( + { assertThat(blackJack.users[0].name).isEqualTo("아크") }, + { assertThat(blackJack.users[0].cards.size).isEqualTo(2) }, + { assertThat(blackJack.users[1].name).isEqualTo("로피") }, + { assertThat(blackJack.users[1].cards.size).isEqualTo(2) }, + { assertThat(blackJack.dealer.name).isEqualTo("딜러") }, + { assertThat(blackJack.dealer.cards.size).isEqualTo(2) }, + { assertThat(blackJack.cardDeck.size).isEqualTo(46) }, + ) + } +} diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index 3004ddcd9..6fed0aaec 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -1,58 +1,29 @@ package blackjack.domain -import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow class BlackJackGameTest { - @Test - fun `초기세팅이 된다`() { - // given - val names = listOf("아크", "로피") - // when - val blackJackGame = BlackJackGame(names) - blackJackGame.setUp() - - // then - assertThat(blackJackGame.dealer.cards.size).isEqualTo(2) - blackJackGame.users.forEach { user -> - assertThat(user.cards.size).isEqualTo(2) + fun `게임을 실행한다`() { + val blackJack = BlackJackBuilder.init { + cardDeck(Card.all().shuffled()) + dealer("딜러") + users(listOf("아크", "로피")) } - } - - @Test - fun `y를 누를 누르면 한장이 뽑아진다`() { - // given - val names = listOf("아크", "로피") - // when - val blackJackGame = BlackJackGame(names) - val user = blackJackGame.setUp() - blackJackGame.progress(user, "y") - // then - assertThat(user.cards.size).isEqualTo(3) + assertDoesNotThrow { + BlackJackGame().apply { + input(::inputDrawMore) + output(::outputCard) + run(blackJack) + } + } } - @Test - fun `n을 누르면 다음 사람 차례다`() { - // given - val names = listOf("아크", "로피") - // when - val blackJackGame = BlackJackGame(names) - val user = blackJackGame.setUp() - blackJackGame.progress(user, "n") - - assertThat(blackJackGame.userIndex).isEqualTo(1) + private fun inputDrawMore(string: String): String { + return "y" } - @Test - fun `게임의 결과를 반환한다`() { - // given - val names = listOf("아크", "로피") - // when - val blackJackGame = BlackJackGame(names) - blackJackGame.setUp() - // then - assertThat(blackJackGame.getResult().size).isEqualTo(2) - } + private fun outputCard(user: User) = null } diff --git a/src/test/kotlin/blackjack/domain/BlackJackTest.kt b/src/test/kotlin/blackjack/domain/BlackJackTest.kt new file mode 100644 index 000000000..98cd54150 --- /dev/null +++ b/src/test/kotlin/blackjack/domain/BlackJackTest.kt @@ -0,0 +1,23 @@ +package blackjack.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class BlackJackTest { + @Test + fun `게임 결과를 반환한다`() { + val blackJack = BlackJack( + dealer = User("딜러"), + users = listOf(User("아크"), User("로피")), + cardDeck = CardDeck(Card.all()), + ).apply { + dealer.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) + dealer.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) + users[0].draw(Card(CardMark.CLOVER, CardValue.NINE)) + users[0].draw(Card(CardMark.CLOVER, CardValue.NINE)) + users[1].draw(Card(CardMark.CLOVER, CardValue.ACE)) + users[1].draw(Card(CardMark.CLOVER, CardValue.QUEEN)) + } + assertThat(blackJack.result).isEqualTo(listOf(Outcome.LOSE, Outcome.WIN)) + } +} From 47644150b0254b80b968273e72974f2dd65564cc Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 19:49:14 +0900 Subject: [PATCH 19/67] =?UTF-8?q?refactor(BlackJack)=20:=20=EB=A7=A4?= =?UTF-8?q?=EC=A7=81=EB=84=98=EB=B2=84=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackGame.kt | 12 +++++++++--- src/main/kotlin/blackjack/domain/Outcome.kt | 7 ++++--- src/main/kotlin/blackjack/domain/User.kt | 8 ++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 5902a297f..2d6e68c02 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -15,17 +15,23 @@ class BlackJackGame() { fun run(blackJack: BlackJack) { blackJack.run { users.forEach { user -> command(user, blackJack.cardDeck) } - while (dealer.maxScore < 17) { dealer.draw(cardDeck.draw()) } + while (dealer.maxScore < DEALER_MIN_NUMBER) { dealer.draw(cardDeck.draw()) } } } fun command(user: User, cardDeck: CardDeck) { - if (input(user.name) == "y") { + if (input(user.name) in DRAW_COMMANDS) { user.draw(cardDeck.draw()) output(user) - if (user.minScore < 21) { + if (user.minScore < BLACKJACK_NUMBER) { command(user, cardDeck) } } } + + companion object { + const val BLACKJACK_NUMBER = 21 + private const val DEALER_MIN_NUMBER = 17 + private val DRAW_COMMANDS = listOf("Y", "y") + } } diff --git a/src/main/kotlin/blackjack/domain/Outcome.kt b/src/main/kotlin/blackjack/domain/Outcome.kt index 7629cf511..b94547c50 100644 --- a/src/main/kotlin/blackjack/domain/Outcome.kt +++ b/src/main/kotlin/blackjack/domain/Outcome.kt @@ -1,5 +1,6 @@ package blackjack.domain +import blackjack.domain.BlackJackGame.Companion.BLACKJACK_NUMBER import java.lang.IllegalStateException enum class Outcome { @@ -8,9 +9,9 @@ enum class Outcome { companion object { fun of(dealer: User, user: User): Outcome = when { - dealer.score > 21 && user.score > 21 -> DRAW - dealer.score > 21 -> WIN - user.score > 21 -> LOSE + dealer.score > BLACKJACK_NUMBER && user.score > BLACKJACK_NUMBER -> DRAW + dealer.score > BLACKJACK_NUMBER -> WIN + user.score > BLACKJACK_NUMBER -> LOSE dealer.score == user.score -> DRAW user.score > dealer.score -> WIN diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 89563455b..5467e12a5 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -1,5 +1,6 @@ package blackjack.domain +import blackjack.domain.BlackJackGame.Companion.BLACKJACK_NUMBER class User(val name: String) { val cards = Cards() val score: Int @@ -11,8 +12,8 @@ class User(val name: String) { val maxScore: Int get() { var score = cards.toList().sumOf { it.value.value } - if (cards.containsACE() && score <= 21 - 10) { - score += 10 + if (cards.containsACE() && score <= BLACKJACK_NUMBER - ACE_OTHER_NUMBER_DIFF) { + score += ACE_OTHER_NUMBER_DIFF } return score } @@ -20,4 +21,7 @@ class User(val name: String) { fun draw(card: Card) { cards.add(card) } + companion object { + private const val ACE_OTHER_NUMBER_DIFF = 10 + } } From 30cae0feb11e17ed68d811d72b7d7eb39598ed0b Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 19:49:35 +0900 Subject: [PATCH 20/67] =?UTF-8?q?refactor(gameStatus)=20:=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=EC=97=86=EB=8A=94=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/GameStatus.kt | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 src/main/kotlin/blackjack/domain/GameStatus.kt diff --git a/src/main/kotlin/blackjack/domain/GameStatus.kt b/src/main/kotlin/blackjack/domain/GameStatus.kt deleted file mode 100644 index 8eb11e15b..000000000 --- a/src/main/kotlin/blackjack/domain/GameStatus.kt +++ /dev/null @@ -1,7 +0,0 @@ -package blackjack.domain - -enum class GameStatus { - START, - RUNNING, - END, -} From 17b90cb4efad9e5a46352cc84d110ec47830dee5 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 20:18:06 +0900 Subject: [PATCH 21/67] =?UTF-8?q?refactor(Participants)=20:=20=EC=9D=B8?= =?UTF-8?q?=EC=8A=A4=ED=84=B4=EC=8A=A4=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.kt | 3 +-- src/main/kotlin/blackjack/domain/BlackJack.kt | 11 ++++++--- .../blackjack/domain/BlackJackBuilder.kt | 24 ++++++------------- .../kotlin/blackjack/domain/Participants.kt | 6 +++++ .../blackjack/domain/ParticipantsBuilder.kt | 24 +++++++++++++++++++ .../blackjack/domain/BlackJackBuilderTest.kt | 3 +-- .../blackjack/domain/BlackJackGameTest.kt | 3 +-- .../kotlin/blackjack/domain/BlackJackTest.kt | 3 +-- 8 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/Participants.kt create mode 100644 src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 04921eb98..56dbdd937 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -13,8 +13,7 @@ class BlackJackController( fun run() { val blackJack = BlackJackBuilder.init { cardDeck(Card.all().shuffled()) - dealer("딜러") - users(inputView.inputParticipants()) + participants("딜러", inputView.inputParticipants()) } outputView.outputInitState(blackJack.dealer, blackJack.users) diff --git a/src/main/kotlin/blackjack/domain/BlackJack.kt b/src/main/kotlin/blackjack/domain/BlackJack.kt index de85d5383..f8cd43f73 100644 --- a/src/main/kotlin/blackjack/domain/BlackJack.kt +++ b/src/main/kotlin/blackjack/domain/BlackJack.kt @@ -1,10 +1,15 @@ package blackjack.domain data class BlackJack( - val dealer: User, - val users: List, val cardDeck: CardDeck, + val participants: Participants, ) { val result: List - get() = users.map { user -> Outcome.of(dealer, user) } + get() = participants.users.map { user -> Outcome.of(participants.dealer, user) } + + val dealer: User + get() = participants.dealer + + val users: List + get() = participants.users } diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index 16d8cc57b..38a9ad8cb 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -2,29 +2,19 @@ package blackjack.domain class BlackJackBuilder { private lateinit var cardDeck: CardDeck - private lateinit var dealer: User - private lateinit var users: List - + private lateinit var participants: Participants fun cardDeck(cards: List) { cardDeck = CardDeck(cards) } - fun dealer(name: String) { - dealer = User(name) - dealer.draw(cardDeck.draw()) - dealer.draw(cardDeck.draw()) - } - - fun users(names: List) { - users = names.map { User(it) } - users.map { - it.draw(cardDeck.draw()) - it.draw(cardDeck.draw()) - } + fun participants(name: String, names: List) { + participants = ParticipantsBuilder().apply { + dealer(name, cardDeck) + users(names, cardDeck) + }.build() } - fun build(): BlackJack { - return BlackJack(dealer, users, cardDeck) + return BlackJack(cardDeck, participants) } companion object { diff --git a/src/main/kotlin/blackjack/domain/Participants.kt b/src/main/kotlin/blackjack/domain/Participants.kt new file mode 100644 index 000000000..25407eb9c --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Participants.kt @@ -0,0 +1,6 @@ +package blackjack.domain + +data class Participants( + val dealer: User, + val users: List, +) diff --git a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt new file mode 100644 index 000000000..fda1b74cb --- /dev/null +++ b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt @@ -0,0 +1,24 @@ +package blackjack.domain + +class ParticipantsBuilder { + + private lateinit var dealer: User + private lateinit var users: List + fun dealer(name: String, cardDeck: CardDeck) { + dealer = User(name) + dealer.draw(cardDeck.draw()) + dealer.draw(cardDeck.draw()) + } + + fun users(names: List, cardDeck: CardDeck) { + users = names.map { User(it) } + users.map { + it.draw(cardDeck.draw()) + it.draw(cardDeck.draw()) + } + } + + fun build(): Participants { + return Participants(dealer, users) + } +} diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt index e255f73cd..aa8344c40 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt @@ -9,8 +9,7 @@ class BlackJackBuilderTest { fun `세팅이 된다`() { val blackJack = BlackJackBuilder.init { cardDeck(Card.all().shuffled()) - dealer("딜러") - users(listOf("아크", "로피")) + participants("딜러", listOf("아크", "로피")) } assertAll( { assertThat(blackJack.users[0].name).isEqualTo("아크") }, diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index 6fed0aaec..de14e7b24 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -8,8 +8,7 @@ class BlackJackGameTest { fun `게임을 실행한다`() { val blackJack = BlackJackBuilder.init { cardDeck(Card.all().shuffled()) - dealer("딜러") - users(listOf("아크", "로피")) + participants("딜러", listOf("아크", "로피")) } assertDoesNotThrow { diff --git a/src/test/kotlin/blackjack/domain/BlackJackTest.kt b/src/test/kotlin/blackjack/domain/BlackJackTest.kt index 98cd54150..c9a12c2ef 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackTest.kt @@ -7,8 +7,7 @@ class BlackJackTest { @Test fun `게임 결과를 반환한다`() { val blackJack = BlackJack( - dealer = User("딜러"), - users = listOf(User("아크"), User("로피")), + participants = Participants(User("딜러"), listOf(User("아크"), User("로피"))), cardDeck = CardDeck(Card.all()), ).apply { dealer.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) From f5c7282e4f9961cd441be9766b13f1e5c159d071 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 20:24:41 +0900 Subject: [PATCH 22/67] =?UTF-8?q?refactor(BlackJack)=20:=20private=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/controller/BlackJackController.kt | 4 ++-- src/main/kotlin/blackjack/domain/BlackJackGame.kt | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 56dbdd937..abaee133d 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -7,8 +7,8 @@ import blackjack.view.InputView import blackjack.view.OutputView class BlackJackController( - val inputView: InputView, - val outputView: OutputView, + private val inputView: InputView, + private val outputView: OutputView, ) { fun run() { val blackJack = BlackJackBuilder.init { diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 2d6e68c02..278a3f2c1 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -1,8 +1,8 @@ package blackjack.domain -class BlackJackGame() { - lateinit var input: (String) -> String - lateinit var output: (User) -> Unit +class BlackJackGame { + private lateinit var input: (String) -> String + private lateinit var output: (User) -> Unit fun input(func: (String) -> String) { input = func @@ -19,7 +19,7 @@ class BlackJackGame() { } } - fun command(user: User, cardDeck: CardDeck) { + private fun command(user: User, cardDeck: CardDeck) { if (input(user.name) in DRAW_COMMANDS) { user.draw(cardDeck.draw()) output(user) From 591d1dbec9af18848835c6a22f3fce4d5891292f Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 20:29:11 +0900 Subject: [PATCH 23/67] =?UTF-8?q?refactor(CardDeck)=20:=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackGame.kt | 4 ++-- src/main/kotlin/blackjack/domain/CardDeck.kt | 2 +- src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt | 8 ++++---- src/test/kotlin/blackjack/domain/CardDeckTest.kt | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 278a3f2c1..a4d6683e3 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -15,13 +15,13 @@ class BlackJackGame { fun run(blackJack: BlackJack) { blackJack.run { users.forEach { user -> command(user, blackJack.cardDeck) } - while (dealer.maxScore < DEALER_MIN_NUMBER) { dealer.draw(cardDeck.draw()) } + while (dealer.maxScore < DEALER_MIN_NUMBER) { dealer.draw(cardDeck.nextCard()) } } } private fun command(user: User, cardDeck: CardDeck) { if (input(user.name) in DRAW_COMMANDS) { - user.draw(cardDeck.draw()) + user.draw(cardDeck.nextCard()) output(user) if (user.minScore < BLACKJACK_NUMBER) { command(user, cardDeck) diff --git a/src/main/kotlin/blackjack/domain/CardDeck.kt b/src/main/kotlin/blackjack/domain/CardDeck.kt index d2c2510e4..33ec11a68 100644 --- a/src/main/kotlin/blackjack/domain/CardDeck.kt +++ b/src/main/kotlin/blackjack/domain/CardDeck.kt @@ -11,7 +11,7 @@ class CardDeck(cards: List) { val size: Int get() = cards.size - fun draw(): Card = cards.removeFirst() + fun nextCard(): Card = cards.removeFirst() companion object { private const val CARDS_SIZE = 52 diff --git a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt index fda1b74cb..297a75c33 100644 --- a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt +++ b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt @@ -6,15 +6,15 @@ class ParticipantsBuilder { private lateinit var users: List fun dealer(name: String, cardDeck: CardDeck) { dealer = User(name) - dealer.draw(cardDeck.draw()) - dealer.draw(cardDeck.draw()) + dealer.draw(cardDeck.nextCard()) + dealer.draw(cardDeck.nextCard()) } fun users(names: List, cardDeck: CardDeck) { users = names.map { User(it) } users.map { - it.draw(cardDeck.draw()) - it.draw(cardDeck.draw()) + it.draw(cardDeck.nextCard()) + it.draw(cardDeck.nextCard()) } } diff --git a/src/test/kotlin/blackjack/domain/CardDeckTest.kt b/src/test/kotlin/blackjack/domain/CardDeckTest.kt index 18c7e005e..1faea619e 100644 --- a/src/test/kotlin/blackjack/domain/CardDeckTest.kt +++ b/src/test/kotlin/blackjack/domain/CardDeckTest.kt @@ -15,7 +15,7 @@ class CardDeckTest { @Test fun `카드 draw가 잘 되었는지 확인`() { val cardDeck = CardDeck(Card.all()) - cardDeck.draw() + cardDeck.nextCard() assertThat(cardDeck.size).isEqualTo(51) } From 2b0181e601ceeb721fa0d948cf94532e90cfe9c1 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 20:29:49 +0900 Subject: [PATCH 24/67] =?UTF-8?q?refactor(CardDeck)=20:=20=EC=83=81?= =?UTF-8?q?=EC=88=98=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/CardDeck.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/domain/CardDeck.kt b/src/main/kotlin/blackjack/domain/CardDeck.kt index 33ec11a68..4f1d6c2de 100644 --- a/src/main/kotlin/blackjack/domain/CardDeck.kt +++ b/src/main/kotlin/blackjack/domain/CardDeck.kt @@ -15,7 +15,7 @@ class CardDeck(cards: List) { companion object { private const val CARDS_SIZE = 52 - private const val ERROR_INVALID_CARDS_SIZE = "카드덱 초기 사이즈는 52장이어야 합니다." + private const val ERROR_INVALID_CARDS_SIZE = "카드덱 초기 사이즈는 ${CARDS_SIZE}장이어야 합니다." private const val ERROR_EXIST_DUPLICATE_CARDS = "카드덱에는 중복이 없어야 합니다." } } From dd9b03f9f5193d4be895321d21f0c229ceb3de60 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 20:31:45 +0900 Subject: [PATCH 25/67] =?UTF-8?q?refactor(User)=20:=20=EB=A7=A4=EC=A7=81?= =?UTF-8?q?=EB=84=98=EB=B2=84=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/User.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 5467e12a5..1f2f6e178 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -4,7 +4,7 @@ import blackjack.domain.BlackJackGame.Companion.BLACKJACK_NUMBER class User(val name: String) { val cards = Cards() val score: Int - get() = if (maxScore <= 21) maxScore else minScore + get() = if (maxScore <= BLACKJACK_NUMBER) maxScore else minScore val minScore: Int get() = cards.toList().sumOf { it.value.value } From 3e470956499f90eb7a03f1ae8bb8a1809265d25e Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 20:42:42 +0900 Subject: [PATCH 26/67] =?UTF-8?q?refactor(Participants)=20:=20DSL=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.kt | 26 ++++++++++++------- .../blackjack/domain/BlackJackBuilder.kt | 17 ++++++++---- .../blackjack/domain/ParticipantsBuilder.kt | 10 ++----- .../blackjack/domain/BlackJackBuilderTest.kt | 6 ++++- .../blackjack/domain/BlackJackGameTest.kt | 5 +++- 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index abaee133d..8a85cb159 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -1,5 +1,6 @@ package blackjack.controller +import blackjack.domain.BlackJack import blackjack.domain.BlackJackBuilder import blackjack.domain.BlackJackGame import blackjack.domain.Card @@ -11,17 +12,24 @@ class BlackJackController( private val outputView: OutputView, ) { fun run() { - val blackJack = BlackJackBuilder.init { - cardDeck(Card.all().shuffled()) - participants("딜러", inputView.inputParticipants()) - } + val blackJack = setBlackJack() outputView.outputInitState(blackJack.dealer, blackJack.users) + runBlackJack(blackJack) + outputView.outputResult(blackJack.dealer, blackJack.users, blackJack.result) + } - BlackJackGame().apply { - input(inputView::inputDrawMore) - output(outputView::outputCard) - run(blackJack) + private fun setBlackJack(): BlackJack = BlackJackBuilder.init { + cardDeck(Card.all().shuffled()) + participants { + dealer("딜러") + users(inputView.inputParticipants()) } - outputView.outputResult(blackJack.dealer, blackJack.users, blackJack.result) + draw() + } + + private fun runBlackJack(blackJack: BlackJack) = BlackJackGame().apply { + input(inputView::inputDrawMore) + output(outputView::outputCard) + run(blackJack) } } diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index 38a9ad8cb..ac1060e28 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -7,12 +7,19 @@ class BlackJackBuilder { cardDeck = CardDeck(cards) } - fun participants(name: String, names: List) { - participants = ParticipantsBuilder().apply { - dealer(name, cardDeck) - users(names, cardDeck) - }.build() + fun participants(block: ParticipantsBuilder.() -> Unit) { + participants = ParticipantsBuilder().apply { block() }.build() } + + fun draw() { + participants.dealer.draw(cardDeck.nextCard()) + participants.dealer.draw(cardDeck.nextCard()) + participants.users.map { + it.draw(cardDeck.nextCard()) + it.draw(cardDeck.nextCard()) + } + } + fun build(): BlackJack { return BlackJack(cardDeck, participants) } diff --git a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt index 297a75c33..84be2a289 100644 --- a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt +++ b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt @@ -4,18 +4,12 @@ class ParticipantsBuilder { private lateinit var dealer: User private lateinit var users: List - fun dealer(name: String, cardDeck: CardDeck) { + fun dealer(name: String) { dealer = User(name) - dealer.draw(cardDeck.nextCard()) - dealer.draw(cardDeck.nextCard()) } - fun users(names: List, cardDeck: CardDeck) { + fun users(names: List) { users = names.map { User(it) } - users.map { - it.draw(cardDeck.nextCard()) - it.draw(cardDeck.nextCard()) - } } fun build(): Participants { diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt index aa8344c40..5c1de8705 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt @@ -9,7 +9,11 @@ class BlackJackBuilderTest { fun `세팅이 된다`() { val blackJack = BlackJackBuilder.init { cardDeck(Card.all().shuffled()) - participants("딜러", listOf("아크", "로피")) + participants { + dealer("딜러") + users(listOf("아크", "로피")) + } + draw() } assertAll( { assertThat(blackJack.users[0].name).isEqualTo("아크") }, diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index de14e7b24..b8894c118 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -8,7 +8,10 @@ class BlackJackGameTest { fun `게임을 실행한다`() { val blackJack = BlackJackBuilder.init { cardDeck(Card.all().shuffled()) - participants("딜러", listOf("아크", "로피")) + participants { + dealer("딜러") + users(listOf("아크", "로피")) + } } assertDoesNotThrow { From 60a6d56dc28874d9cdef995f1ff39ca473b7893b Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 20:57:14 +0900 Subject: [PATCH 27/67] =?UTF-8?q?refactor(BlackJackGame)=20:=20=ED=99=95?= =?UTF-8?q?=EC=9E=A5=20=ED=95=A8=EC=88=98=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.kt | 3 +- .../kotlin/blackjack/domain/BlackJackGame.kt | 40 +++++++++++-------- src/main/kotlin/blackjack/domain/User.kt | 3 ++ src/main/kotlin/blackjack/view/OutputView.kt | 4 ++ 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 8a85cb159..ac2ed1d7b 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -30,6 +30,7 @@ class BlackJackController( private fun runBlackJack(blackJack: BlackJack) = BlackJackGame().apply { input(inputView::inputDrawMore) output(outputView::outputCard) - run(blackJack) + dealerOutput(outputView::outputDealerDraw) + blackJack.run() } } diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index a4d6683e3..4c21ce176 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -3,32 +3,40 @@ package blackjack.domain class BlackJackGame { private lateinit var input: (String) -> String private lateinit var output: (User) -> Unit + private lateinit var dealerOutput: () -> Unit - fun input(func: (String) -> String) { - input = func - } + fun input(func: (String) -> String) { input = func } + + fun output(func: (User) -> Unit) { output = func } - fun output(func: (User) -> Unit) { - output = func + fun dealerOutput(func: () -> Unit) { dealerOutput = func } + + fun BlackJack.run() { + this.usersTurn() + this.dealerTurn() } - fun run(blackJack: BlackJack) { - blackJack.run { - users.forEach { user -> command(user, blackJack.cardDeck) } - while (dealer.maxScore < DEALER_MIN_NUMBER) { dealer.draw(cardDeck.nextCard()) } + private fun BlackJack.usersTurn() = + users.forEach { user -> command(user, this.cardDeck) } + + private fun BlackJack.dealerTurn() { + while (dealer.maxScore < DEALER_MIN_NUMBER) { + dealer.draw(cardDeck.nextCard()) + dealerOutput() } } private fun command(user: User, cardDeck: CardDeck) { - if (input(user.name) in DRAW_COMMANDS) { - user.draw(cardDeck.nextCard()) - output(user) - if (user.minScore < BLACKJACK_NUMBER) { - command(user, cardDeck) - } - } + if (user.name.isDrawCommand()) return + + user.draw(cardDeck.nextCard()) + output(user) + + if (user.isBust) { command(user, cardDeck) } } + private fun String.isDrawCommand() = input(this) !in DRAW_COMMANDS + companion object { const val BLACKJACK_NUMBER = 21 private const val DEALER_MIN_NUMBER = 17 diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 1f2f6e178..9eef9181e 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -18,6 +18,9 @@ class User(val name: String) { return score } + val isBust: Boolean + get() = minScore < BLACKJACK_NUMBER + fun draw(card: Card) { cards.add(card) } diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index 2e30ef639..16b3a46fd 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -37,6 +37,10 @@ class OutputView { } } + fun outputDealerDraw() { + println("딜러는 16이하라 한장의 카드를 더 받았습니다.") + } + private fun outputScore(user: User) { print("- 결과 ${user.score}") } From 6f7795c4d0c2f94aed5ca97685af5cb384388d27 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Wed, 1 Mar 2023 21:24:46 +0900 Subject: [PATCH 28/67] =?UTF-8?q?fix(Test)=20:=20=EC=B4=88=EA=B8=B0?= =?UTF-8?q?=ED=99=94=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/blackjack/domain/BlackJackGameTest.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index b8894c118..16c6a90a2 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -12,13 +12,15 @@ class BlackJackGameTest { dealer("딜러") users(listOf("아크", "로피")) } + draw() } assertDoesNotThrow { BlackJackGame().apply { input(::inputDrawMore) output(::outputCard) - run(blackJack) + dealerOutput(::outputDealer) + blackJack.run() } } } @@ -28,4 +30,5 @@ class BlackJackGameTest { } private fun outputCard(user: User) = null + private fun outputDealer() = null } From 5b8a7b6114ced1c292efdb6ba6c2e8db4e907928 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 13:37:36 +0900 Subject: [PATCH 29/67] =?UTF-8?q?refactor(Name)=20:=20name=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EB=9E=98=ED=95=91=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/domain/BlackJackGame.kt | 2 +- src/main/kotlin/blackjack/domain/Name.kt | 15 +++++++++++++ src/main/kotlin/blackjack/domain/User.kt | 3 ++- .../blackjack/domain/BlackJackBuilderTest.kt | 6 ++--- src/test/kotlin/blackjack/domain/NameTest.kt | 22 +++++++++++++++++++ 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/Name.kt create mode 100644 src/test/kotlin/blackjack/domain/NameTest.kt diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 4c21ce176..3402f0c9e 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -27,7 +27,7 @@ class BlackJackGame { } private fun command(user: User, cardDeck: CardDeck) { - if (user.name.isDrawCommand()) return + if (user.name.toString().isDrawCommand()) return user.draw(cardDeck.nextCard()) output(user) diff --git a/src/main/kotlin/blackjack/domain/Name.kt b/src/main/kotlin/blackjack/domain/Name.kt new file mode 100644 index 000000000..febfa26b5 --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Name.kt @@ -0,0 +1,15 @@ +package blackjack.domain + +@JvmInline +value class Name(private val value: String) { + init { + require(value.length <= NAME_LENGTH_MAX) { ERROR_NAME_LENGTH_MAX } + } + + override fun toString(): String = value + + companion object { + private const val NAME_LENGTH_MAX = 20 + private const val ERROR_NAME_LENGTH_MAX = "이름의 최대 글자수는 ${NAME_LENGTH_MAX}글자 입니다." + } +} diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 9eef9181e..240ab32e7 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -1,7 +1,8 @@ package blackjack.domain import blackjack.domain.BlackJackGame.Companion.BLACKJACK_NUMBER -class User(val name: String) { +class User(name: String) { + val name = Name(name) val cards = Cards() val score: Int get() = if (maxScore <= BLACKJACK_NUMBER) maxScore else minScore diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt index 5c1de8705..7ffad8564 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt @@ -16,11 +16,11 @@ class BlackJackBuilderTest { draw() } assertAll( - { assertThat(blackJack.users[0].name).isEqualTo("아크") }, + { assertThat(blackJack.users[0].name.toString()).isEqualTo("아크") }, { assertThat(blackJack.users[0].cards.size).isEqualTo(2) }, - { assertThat(blackJack.users[1].name).isEqualTo("로피") }, + { assertThat(blackJack.users[1].name.toString()).isEqualTo("로피") }, { assertThat(blackJack.users[1].cards.size).isEqualTo(2) }, - { assertThat(blackJack.dealer.name).isEqualTo("딜러") }, + { assertThat(blackJack.dealer.name.toString()).isEqualTo("딜러") }, { assertThat(blackJack.dealer.cards.size).isEqualTo(2) }, { assertThat(blackJack.cardDeck.size).isEqualTo(46) }, ) diff --git a/src/test/kotlin/blackjack/domain/NameTest.kt b/src/test/kotlin/blackjack/domain/NameTest.kt new file mode 100644 index 000000000..0895af64d --- /dev/null +++ b/src/test/kotlin/blackjack/domain/NameTest.kt @@ -0,0 +1,22 @@ +package blackjack.domain + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow +import org.junit.jupiter.api.assertThrows + +class NameTest { + + @Test + fun `이름은 20자 이하다`() { + assertDoesNotThrow { + Name("아크,로피,아크,로피,아크,로피,아크") + } + } + + @Test + fun `이름이 21자 이상이면 에러난다`() { + assertThrows { + Name("가나다라마바사아자차카타파하abcdefjhijklmnopqrstuvwxyz") + } + } +} From 85a844554b6e0e3a8c590abe669c6c7ab8983d23 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 13:41:02 +0900 Subject: [PATCH 30/67] =?UTF-8?q?refactor(Participants)=20:=20DSL=20?= =?UTF-8?q?=EC=98=A4=EB=B8=8C=EC=A0=9D=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackBuilder.kt | 2 +- src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index ac1060e28..12ab4a026 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -8,7 +8,7 @@ class BlackJackBuilder { } fun participants(block: ParticipantsBuilder.() -> Unit) { - participants = ParticipantsBuilder().apply { block() }.build() + participants = ParticipantsBuilder.init { block() } } fun draw() { diff --git a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt index 84be2a289..03d8e0092 100644 --- a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt +++ b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt @@ -15,4 +15,9 @@ class ParticipantsBuilder { fun build(): Participants { return Participants(dealer, users) } + + companion object { + fun init(block: ParticipantsBuilder.() -> Unit): Participants = + ParticipantsBuilder().apply(block).build() + } } From 9e9639423f1239ce5b1d152348964c38e8be3455 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 13:49:24 +0900 Subject: [PATCH 31/67] =?UTF-8?q?refactor(OutputView)=20:=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EB=AC=B8=EC=96=91=20=ED=99=95=EC=9E=A5=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/CardMark.kt | 10 +++---- src/main/kotlin/blackjack/view/OutputView.kt | 21 ++++++++++++++- .../kotlin/blackjack/domain/CardMarkTest.kt | 26 ------------------- 3 files changed, 25 insertions(+), 32 deletions(-) delete mode 100644 src/test/kotlin/blackjack/domain/CardMarkTest.kt diff --git a/src/main/kotlin/blackjack/domain/CardMark.kt b/src/main/kotlin/blackjack/domain/CardMark.kt index f015b1e4e..fa6ba6931 100644 --- a/src/main/kotlin/blackjack/domain/CardMark.kt +++ b/src/main/kotlin/blackjack/domain/CardMark.kt @@ -1,8 +1,8 @@ package blackjack.domain -enum class CardMark(val title: String) { - CLOVER("클로버"), - HEART("하트"), - SPADE("스페이드"), - DIA("다이아"), +enum class CardMark { + CLOVER, + HEART, + SPADE, + DIA, } diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index 16b3a46fd..df0a73b81 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -1,5 +1,7 @@ package blackjack.view +import blackjack.domain.CardMark +import blackjack.domain.CardValue import blackjack.domain.Outcome import blackjack.domain.User @@ -33,7 +35,7 @@ class OutputView { fun outputCard(user: User) { print("${user.name}카드") user.cards.toList().forEach { - print(" ${it.value.value}${it.mark.title}") + print(" ${it.value.pattern()}${it.mark.name()}") } } @@ -58,4 +60,21 @@ class OutputView { Outcome.LOSE -> "패" }.let { println("${user.name}: $it") } } + + private fun CardMark.name(): String = + when (this) { + CardMark.CLOVER -> "클로버" + CardMark.DIA -> "다이아몬드" + CardMark.HEART -> "하트" + CardMark.SPADE -> "스페이드" + } + + private fun CardValue.pattern(): String = + when (this) { + CardValue.ACE -> "A" + CardValue.KING -> "K" + CardValue.QUEEN -> "Q" + CardValue.JACK -> "J" + else -> this.value.toString() + } } diff --git a/src/test/kotlin/blackjack/domain/CardMarkTest.kt b/src/test/kotlin/blackjack/domain/CardMarkTest.kt deleted file mode 100644 index 4179ec150..000000000 --- a/src/test/kotlin/blackjack/domain/CardMarkTest.kt +++ /dev/null @@ -1,26 +0,0 @@ -package blackjack.domain - -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test - -class CardMarkTest { - @Test - fun `클로버를 반환한다`() { - assertThat(CardMark.CLOVER.title).isEqualTo("클로버") - } - - @Test - fun `하트를 반환한다`() { - assertThat(CardMark.HEART.title).isEqualTo("하트") - } - - @Test - fun `스페이드를 반환한다`() { - assertThat(CardMark.SPADE.title).isEqualTo("스페이드") - } - - @Test - fun `다이아를 반환한다`() { - assertThat(CardMark.DIA.title).isEqualTo("다이아") - } -} From 528dd60dae23388299a3b536d17f5c9be1f89e7b Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 13:58:33 +0900 Subject: [PATCH 32/67] =?UTF-8?q?refactor(OutputView)=20:=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EA=B2=B0=EA=B3=BC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.kt | 4 +- .../kotlin/blackjack/domain/Participants.kt | 4 +- src/main/kotlin/blackjack/view/OutputView.kt | 43 +++++++++---------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index ac2ed1d7b..b51699c6c 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -13,9 +13,9 @@ class BlackJackController( ) { fun run() { val blackJack = setBlackJack() - outputView.outputInitState(blackJack.dealer, blackJack.users) + outputView.outputInitState(blackJack) runBlackJack(blackJack) - outputView.outputResult(blackJack.dealer, blackJack.users, blackJack.result) + outputView.outputResult(blackJack) } private fun setBlackJack(): BlackJack = BlackJackBuilder.init { diff --git a/src/main/kotlin/blackjack/domain/Participants.kt b/src/main/kotlin/blackjack/domain/Participants.kt index 25407eb9c..ea8e254b2 100644 --- a/src/main/kotlin/blackjack/domain/Participants.kt +++ b/src/main/kotlin/blackjack/domain/Participants.kt @@ -3,4 +3,6 @@ package blackjack.domain data class Participants( val dealer: User, val users: List, -) +) { + fun all() = listOf(dealer) + users +} diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index df0a73b81..a8ef2ef40 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -1,39 +1,36 @@ package blackjack.view +import blackjack.domain.BlackJack import blackjack.domain.CardMark import blackjack.domain.CardValue import blackjack.domain.Outcome import blackjack.domain.User class OutputView { - fun outputInitState(dealer: User, users: List) { - println("\n딜러와 ${users.map{it.name}} 에게 2장의 나누었습니다.") - outputCard(dealer) - println("") - - users.forEach { user -> + fun outputInitState(blackJack: BlackJack) { + print("\n딜러와 ${blackJack.users.map{it.name}.joinToString(", ")} 에게 2장의 나누었습니다.") + outputCardForDealer(blackJack.dealer) + blackJack.users.forEach { user -> outputCard(user) - println("") } } - fun outputResult(dealer: User, users: List, outcomes: List) { - println("") - outputCard(dealer) - outputScore(dealer) - println("") - - users.forEach { user -> + fun outputResult(blackJack: BlackJack) { + blackJack.participants.all().forEach { user -> outputCard(user) outputScore(user) - println("") } println("") - outputOutcomes(dealer, users, outcomes) + outputOutcomes(blackJack) + } + + fun outputCardForDealer(user: User) { + print("\n${user.name}카드:") + print(" ${user.cards.toList()[0].value.pattern()}${user.cards.toList()[0].mark.name()}") } fun outputCard(user: User) { - print("${user.name}카드") + print("\n${user.name}카드") user.cards.toList().forEach { print(" ${it.value.pattern()}${it.mark.name()}") } @@ -47,13 +44,15 @@ class OutputView { print("- 결과 ${user.score}") } - private fun outputOutcomes(dealer: User, users: List, outcomes: List) { - println("## 최종 승패") - println("${dealer.name}: ${outcomes.count{ it == Outcome.LOSE}}승 ${outcomes.count{ it == Outcome.WIN}}패") - users.forEachIndexed { index, user -> outputOutcome(user, outcomes[index]) } + private fun outputOutcomes(blackJack: BlackJack) { + blackJack.run { + println("\n## 최종 승패") + println("${dealer.name}: ${result.count { it == Outcome.LOSE }}승 ${result.count { it == Outcome.WIN }}패") + users.forEachIndexed { index, user -> outputOutcome(user, result[index]) } + } } - fun outputOutcome(user: User, outcome: Outcome) { + private fun outputOutcome(user: User, outcome: Outcome) { when (outcome) { Outcome.WIN -> "승" Outcome.DRAW -> "무" From 0f707d580426f4962d886dc2a3493e091c00aaa2 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 14:57:27 +0900 Subject: [PATCH 33/67] =?UTF-8?q?refactor(BlackJackGame)=20:=20output?= =?UTF-8?q?=EC=9D=84=20=EC=9D=B8=EC=9E=90=EB=A1=9C=20=EC=A0=84=EB=8B=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.kt | 14 ++++++------ .../kotlin/blackjack/domain/BlackJackGame.kt | 22 ++++++------------- .../blackjack/domain/BlackJackGameTest.kt | 5 ++--- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index b51699c6c..f90fad738 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -14,7 +14,7 @@ class BlackJackController( fun run() { val blackJack = setBlackJack() outputView.outputInitState(blackJack) - runBlackJack(blackJack) + startBlackJack(blackJack) outputView.outputResult(blackJack) } @@ -27,10 +27,10 @@ class BlackJackController( draw() } - private fun runBlackJack(blackJack: BlackJack) = BlackJackGame().apply { - input(inputView::inputDrawMore) - output(outputView::outputCard) - dealerOutput(outputView::outputDealerDraw) - blackJack.run() - } + private fun startBlackJack(blackJack: BlackJack) = + BlackJackGame().apply { + input(inputView::inputDrawMore) + blackJack.usersTurn(outputView::outputCard) + blackJack.dealerTurn(outputView::outputDealerDraw) + } } diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 3402f0c9e..5f60a59c0 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -2,27 +2,20 @@ package blackjack.domain class BlackJackGame { private lateinit var input: (String) -> String - private lateinit var output: (User) -> Unit - private lateinit var dealerOutput: () -> Unit fun input(func: (String) -> String) { input = func } - fun output(func: (User) -> Unit) { output = func } - - fun dealerOutput(func: () -> Unit) { dealerOutput = func } - - fun BlackJack.run() { - this.usersTurn() - this.dealerTurn() + fun BlackJack.usersTurn(output: (User) -> Unit) { + users.forEach { user -> + command(user, this.cardDeck) + output(user) + } } - private fun BlackJack.usersTurn() = - users.forEach { user -> command(user, this.cardDeck) } - - private fun BlackJack.dealerTurn() { + fun BlackJack.dealerTurn(output: () -> Unit) { while (dealer.maxScore < DEALER_MIN_NUMBER) { dealer.draw(cardDeck.nextCard()) - dealerOutput() + output() } } @@ -30,7 +23,6 @@ class BlackJackGame { if (user.name.toString().isDrawCommand()) return user.draw(cardDeck.nextCard()) - output(user) if (user.isBust) { command(user, cardDeck) } } diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index 16c6a90a2..3d8d8f4e3 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -18,9 +18,8 @@ class BlackJackGameTest { assertDoesNotThrow { BlackJackGame().apply { input(::inputDrawMore) - output(::outputCard) - dealerOutput(::outputDealer) - blackJack.run() + blackJack.usersTurn(::outputCard) + blackJack.dealerTurn(::outputDealer) } } } From a1e48458ed3fbfff2de531b6d08d1d6703ad87f6 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 14:58:36 +0900 Subject: [PATCH 34/67] =?UTF-8?q?refactor(BlackJackBuilder)=20:=20?= =?UTF-8?q?=ED=8C=8C=EC=83=9D=20=EC=9D=B8=EC=9E=90=20=ED=99=9C=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackBuilder.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index 12ab4a026..80df83869 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -12,9 +12,7 @@ class BlackJackBuilder { } fun draw() { - participants.dealer.draw(cardDeck.nextCard()) - participants.dealer.draw(cardDeck.nextCard()) - participants.users.map { + participants.all().map { it.draw(cardDeck.nextCard()) it.draw(cardDeck.nextCard()) } From c3e8861f3c864089b142f6e7d094466ec38ecfc1 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 15:17:56 +0900 Subject: [PATCH 35/67] =?UTF-8?q?refactor(User)=20:=20=EC=84=A0=EC=96=B8?= =?UTF-8?q?=ED=98=95=20=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98=EB=B0=8D=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/domain/BlackJackGame.kt | 6 +++--- src/main/kotlin/blackjack/domain/User.kt | 20 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 5f60a59c0..96ca439ad 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -20,14 +20,14 @@ class BlackJackGame { } private fun command(user: User, cardDeck: CardDeck) { - if (user.name.toString().isDrawCommand()) return + if (user.isDrawCommand()) return user.draw(cardDeck.nextCard()) - if (user.isBust) { command(user, cardDeck) } + if (user.isNotBust) { command(user, cardDeck) } } - private fun String.isDrawCommand() = input(this) !in DRAW_COMMANDS + private fun User.isDrawCommand() = input(this.name.toString()) !in DRAW_COMMANDS companion object { const val BLACKJACK_NUMBER = 21 diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 240ab32e7..66dc52884 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -5,22 +5,22 @@ class User(name: String) { val name = Name(name) val cards = Cards() val score: Int - get() = if (maxScore <= BLACKJACK_NUMBER) maxScore else minScore + get() = if (isMaxScoreInRange) maxScore else minScore val minScore: Int get() = cards.toList().sumOf { it.value.value } val maxScore: Int - get() { - var score = cards.toList().sumOf { it.value.value } - if (cards.containsACE() && score <= BLACKJACK_NUMBER - ACE_OTHER_NUMBER_DIFF) { - score += ACE_OTHER_NUMBER_DIFF - } - return score - } + get() = minScore + if (cards.containsACE() && validateAceCondition) ACE_OTHER_NUMBER_DIFF else 0 - val isBust: Boolean - get() = minScore < BLACKJACK_NUMBER + val isNotBust: Boolean + get() = minScore <= BLACKJACK_NUMBER + + private val validateAceCondition: Boolean + get() = minScore + ACE_OTHER_NUMBER_DIFF <= BLACKJACK_NUMBER + + private val isMaxScoreInRange: Boolean + get() = maxScore <= BLACKJACK_NUMBER fun draw(card: Card) { cards.add(card) From 89ee94e916839b8c110807912b092353dffe876c Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 15:19:00 +0900 Subject: [PATCH 36/67] =?UTF-8?q?fix(BlackJackGame)=20:=20=EB=AC=B4?= =?UTF-8?q?=ED=95=9C=EB=B0=98=EB=B3=B5=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackGame.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 96ca439ad..6f9da82ef 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -13,7 +13,7 @@ class BlackJackGame { } fun BlackJack.dealerTurn(output: () -> Unit) { - while (dealer.maxScore < DEALER_MIN_NUMBER) { + if (dealer.maxScore < DEALER_MIN_NUMBER) { dealer.draw(cardDeck.nextCard()) output() } From 5549988ae58f9013e9e39c741938112f308aa35d Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 16:10:43 +0900 Subject: [PATCH 37/67] =?UTF-8?q?refactor(User)=20:=20=EB=94=9C=EB=9F=AC?= =?UTF-8?q?=20=EB=93=9C=EB=A1=9C=EC=9A=B0=20=EC=A1=B0=EA=B1=B4=20=EC=84=A0?= =?UTF-8?q?=EC=96=B8=ED=98=95=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackGame.kt | 3 +-- src/main/kotlin/blackjack/domain/User.kt | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 6f9da82ef..887caa859 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -13,7 +13,7 @@ class BlackJackGame { } fun BlackJack.dealerTurn(output: () -> Unit) { - if (dealer.maxScore < DEALER_MIN_NUMBER) { + if (dealer.checkDealerScore) { dealer.draw(cardDeck.nextCard()) output() } @@ -31,7 +31,6 @@ class BlackJackGame { companion object { const val BLACKJACK_NUMBER = 21 - private const val DEALER_MIN_NUMBER = 17 private val DRAW_COMMANDS = listOf("Y", "y") } } diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 66dc52884..8070979dd 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -16,6 +16,9 @@ class User(name: String) { val isNotBust: Boolean get() = minScore <= BLACKJACK_NUMBER + val checkDealerScore: Boolean + get() = maxScore < DEALER_MIN_NUMBER + private val validateAceCondition: Boolean get() = minScore + ACE_OTHER_NUMBER_DIFF <= BLACKJACK_NUMBER @@ -26,6 +29,7 @@ class User(name: String) { cards.add(card) } companion object { + private const val DEALER_MIN_NUMBER = 17 private const val ACE_OTHER_NUMBER_DIFF = 10 } } From b58161fd504f6f07e7d61e36e61077037de1840f Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 16:12:18 +0900 Subject: [PATCH 38/67] =?UTF-8?q?refactor(OutputView)=20:=20=EB=9D=84?= =?UTF-8?q?=EC=96=B4=EC=93=B0=EA=B8=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/view/OutputView.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index a8ef2ef40..1da55456c 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -37,7 +37,7 @@ class OutputView { } fun outputDealerDraw() { - println("딜러는 16이하라 한장의 카드를 더 받았습니다.") + println("\n딜러는 16이하라 한장의 카드를 더 받았습니다.") } private fun outputScore(user: User) { From aecfaa02da15c470b3ab422bf20d142974c8213b Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 16:37:00 +0900 Subject: [PATCH 39/67] =?UTF-8?q?refactor(User)=20:=20=EC=9C=A0=EC=A0=80?= =?UTF-8?q?=EC=99=80=20=EB=94=9C=EB=9F=AC=EC=9D=98=20=EC=8A=A4=EC=BD=94?= =?UTF-8?q?=EC=96=B4=20=EA=B3=84=EC=82=B0=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.kt | 4 +- src/main/kotlin/blackjack/domain/BlackJack.kt | 8 +-- .../kotlin/blackjack/domain/BlackJackGame.kt | 23 +++--- src/main/kotlin/blackjack/domain/Dealer.kt | 6 ++ src/main/kotlin/blackjack/domain/Guest.kt | 6 ++ src/main/kotlin/blackjack/domain/Outcome.kt | 12 ++-- .../kotlin/blackjack/domain/Participants.kt | 6 +- .../blackjack/domain/ParticipantsBuilder.kt | 12 ++-- src/main/kotlin/blackjack/domain/Score.kt | 29 ++++++++ src/main/kotlin/blackjack/domain/User.kt | 28 +------- src/main/kotlin/blackjack/view/OutputView.kt | 6 +- .../blackjack/domain/BlackJackBuilderTest.kt | 10 +-- .../blackjack/domain/BlackJackGameTest.kt | 4 +- .../kotlin/blackjack/domain/BlackJackTest.kt | 10 +-- .../kotlin/blackjack/domain/OutcomeTest.kt | 70 +++++++++---------- src/test/kotlin/blackjack/domain/UserTest.kt | 28 +------- 16 files changed, 128 insertions(+), 134 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/Dealer.kt create mode 100644 src/main/kotlin/blackjack/domain/Guest.kt create mode 100644 src/main/kotlin/blackjack/domain/Score.kt diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index f90fad738..e118385c0 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -22,7 +22,7 @@ class BlackJackController( cardDeck(Card.all().shuffled()) participants { dealer("딜러") - users(inputView.inputParticipants()) + guests(inputView.inputParticipants()) } draw() } @@ -30,7 +30,7 @@ class BlackJackController( private fun startBlackJack(blackJack: BlackJack) = BlackJackGame().apply { input(inputView::inputDrawMore) - blackJack.usersTurn(outputView::outputCard) + blackJack.guestsTurn(outputView::outputCard) blackJack.dealerTurn(outputView::outputDealerDraw) } } diff --git a/src/main/kotlin/blackjack/domain/BlackJack.kt b/src/main/kotlin/blackjack/domain/BlackJack.kt index f8cd43f73..4c9d992ce 100644 --- a/src/main/kotlin/blackjack/domain/BlackJack.kt +++ b/src/main/kotlin/blackjack/domain/BlackJack.kt @@ -5,11 +5,11 @@ data class BlackJack( val participants: Participants, ) { val result: List - get() = participants.users.map { user -> Outcome.of(participants.dealer, user) } + get() = participants.guests.map { guest -> Outcome.of(participants.dealer, guest) } - val dealer: User + val dealer: Dealer get() = participants.dealer - val users: List - get() = participants.users + val guests: List + get() = participants.guests } diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 887caa859..2c60e2f45 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -5,29 +5,28 @@ class BlackJackGame { fun input(func: (String) -> String) { input = func } - fun BlackJack.usersTurn(output: (User) -> Unit) { - users.forEach { user -> - command(user, this.cardDeck) - output(user) + fun BlackJack.guestsTurn(output: (User) -> Unit) { + guests.forEach { guest -> + while (guest.isContinue) { + command(guest, cardDeck) + output(guest) + } } } fun BlackJack.dealerTurn(output: () -> Unit) { - if (dealer.checkDealerScore) { + if (dealer.isContinue) { dealer.draw(cardDeck.nextCard()) output() } } - private fun command(user: User, cardDeck: CardDeck) { - if (user.isDrawCommand()) return - - user.draw(cardDeck.nextCard()) - - if (user.isNotBust) { command(user, cardDeck) } + private fun command(guest: Guest, cardDeck: CardDeck) { + if (guest.isDrawCommand()) return + guest.draw(cardDeck.nextCard()) } - private fun User.isDrawCommand() = input(this.name.toString()) !in DRAW_COMMANDS + private fun Guest.isDrawCommand() = input(this.name.toString()) !in DRAW_COMMANDS companion object { const val BLACKJACK_NUMBER = 21 diff --git a/src/main/kotlin/blackjack/domain/Dealer.kt b/src/main/kotlin/blackjack/domain/Dealer.kt new file mode 100644 index 000000000..0c917642c --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Dealer.kt @@ -0,0 +1,6 @@ +package blackjack.domain + +class Dealer(name: String) : User(name) { + override val isContinue: Boolean // dealer + get() = Score(cards).checkDealerScore +} diff --git a/src/main/kotlin/blackjack/domain/Guest.kt b/src/main/kotlin/blackjack/domain/Guest.kt new file mode 100644 index 000000000..eedda0d74 --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Guest.kt @@ -0,0 +1,6 @@ +package blackjack.domain + +class Guest(name: String) : User(name) { + override val isContinue: Boolean // dealer + get() = Score(cards).isNotBust +} diff --git a/src/main/kotlin/blackjack/domain/Outcome.kt b/src/main/kotlin/blackjack/domain/Outcome.kt index b94547c50..ecc967a49 100644 --- a/src/main/kotlin/blackjack/domain/Outcome.kt +++ b/src/main/kotlin/blackjack/domain/Outcome.kt @@ -7,15 +7,15 @@ enum class Outcome { WIN, DRAW, LOSE; companion object { - fun of(dealer: User, user: User): Outcome = + fun of(dealer: Dealer, guest: Guest): Outcome = when { - dealer.score > BLACKJACK_NUMBER && user.score > BLACKJACK_NUMBER -> DRAW + dealer.score > BLACKJACK_NUMBER && guest.score > BLACKJACK_NUMBER -> DRAW dealer.score > BLACKJACK_NUMBER -> WIN - user.score > BLACKJACK_NUMBER -> LOSE + guest.score > BLACKJACK_NUMBER -> LOSE - dealer.score == user.score -> DRAW - user.score > dealer.score -> WIN - dealer.score > user.score -> LOSE + dealer.score == guest.score -> DRAW + guest.score > dealer.score -> WIN + dealer.score > guest.score -> LOSE else -> throw IllegalStateException() } diff --git a/src/main/kotlin/blackjack/domain/Participants.kt b/src/main/kotlin/blackjack/domain/Participants.kt index ea8e254b2..a8ac7ce61 100644 --- a/src/main/kotlin/blackjack/domain/Participants.kt +++ b/src/main/kotlin/blackjack/domain/Participants.kt @@ -1,8 +1,8 @@ package blackjack.domain data class Participants( - val dealer: User, - val users: List, + val dealer: Dealer, + val guests: List, ) { - fun all() = listOf(dealer) + users + fun all() = listOf(dealer) + guests } diff --git a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt index 03d8e0092..78d1c8db0 100644 --- a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt +++ b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt @@ -2,18 +2,18 @@ package blackjack.domain class ParticipantsBuilder { - private lateinit var dealer: User - private lateinit var users: List + private lateinit var dealer: Dealer + private lateinit var guests: List fun dealer(name: String) { - dealer = User(name) + dealer = Dealer(name) } - fun users(names: List) { - users = names.map { User(it) } + fun guests(names: List) { + guests = names.map { Guest(it) } } fun build(): Participants { - return Participants(dealer, users) + return Participants(dealer, guests) } companion object { diff --git a/src/main/kotlin/blackjack/domain/Score.kt b/src/main/kotlin/blackjack/domain/Score.kt new file mode 100644 index 000000000..ba21cfcf7 --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Score.kt @@ -0,0 +1,29 @@ +package blackjack.domain + +class Score(val cards: Cards) { + val score: Int + get() = if (isMaxScoreInRange) maxScore else minScore + + val isNotBust: Boolean + get() = minScore <= BlackJackGame.BLACKJACK_NUMBER + + val checkDealerScore: Boolean + get() = maxScore < DEALER_MIN_NUMBER + + private val minScore: Int + get() = cards.toList().sumOf { it.value.value } + + private val maxScore: Int + get() = minScore + if (cards.containsACE() && validateAceCondition) ACE_OTHER_NUMBER_DIFF else 0 + + private val validateAceCondition: Boolean + get() = minScore + ACE_OTHER_NUMBER_DIFF <= BlackJackGame.BLACKJACK_NUMBER + + private val isMaxScoreInRange: Boolean + get() = maxScore <= BlackJackGame.BLACKJACK_NUMBER + + companion object { + private const val DEALER_MIN_NUMBER = 17 + private const val ACE_OTHER_NUMBER_DIFF = 10 + } +} diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 8070979dd..d808b4cfa 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -1,35 +1,13 @@ package blackjack.domain -import blackjack.domain.BlackJackGame.Companion.BLACKJACK_NUMBER -class User(name: String) { +abstract class User(name: String) { val name = Name(name) val cards = Cards() val score: Int - get() = if (isMaxScoreInRange) maxScore else minScore - - val minScore: Int - get() = cards.toList().sumOf { it.value.value } - - val maxScore: Int - get() = minScore + if (cards.containsACE() && validateAceCondition) ACE_OTHER_NUMBER_DIFF else 0 - - val isNotBust: Boolean - get() = minScore <= BLACKJACK_NUMBER - - val checkDealerScore: Boolean - get() = maxScore < DEALER_MIN_NUMBER - - private val validateAceCondition: Boolean - get() = minScore + ACE_OTHER_NUMBER_DIFF <= BLACKJACK_NUMBER - - private val isMaxScoreInRange: Boolean - get() = maxScore <= BLACKJACK_NUMBER + get() = Score(cards).score + abstract val isContinue: Boolean fun draw(card: Card) { cards.add(card) } - companion object { - private const val DEALER_MIN_NUMBER = 17 - private const val ACE_OTHER_NUMBER_DIFF = 10 - } } diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index 1da55456c..a59425abf 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -8,9 +8,9 @@ import blackjack.domain.User class OutputView { fun outputInitState(blackJack: BlackJack) { - print("\n딜러와 ${blackJack.users.map{it.name}.joinToString(", ")} 에게 2장의 나누었습니다.") + print("\n딜러와 ${blackJack.guests.map{it.name}.joinToString(", ")} 에게 2장의 나누었습니다.") outputCardForDealer(blackJack.dealer) - blackJack.users.forEach { user -> + blackJack.guests.forEach { user -> outputCard(user) } } @@ -48,7 +48,7 @@ class OutputView { blackJack.run { println("\n## 최종 승패") println("${dealer.name}: ${result.count { it == Outcome.LOSE }}승 ${result.count { it == Outcome.WIN }}패") - users.forEachIndexed { index, user -> outputOutcome(user, result[index]) } + guests.forEachIndexed { index, user -> outputOutcome(user, result[index]) } } } diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt index 7ffad8564..33c1b8a65 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt @@ -11,15 +11,15 @@ class BlackJackBuilderTest { cardDeck(Card.all().shuffled()) participants { dealer("딜러") - users(listOf("아크", "로피")) + guests(listOf("아크", "로피")) } draw() } assertAll( - { assertThat(blackJack.users[0].name.toString()).isEqualTo("아크") }, - { assertThat(blackJack.users[0].cards.size).isEqualTo(2) }, - { assertThat(blackJack.users[1].name.toString()).isEqualTo("로피") }, - { assertThat(blackJack.users[1].cards.size).isEqualTo(2) }, + { assertThat(blackJack.guests[0].name.toString()).isEqualTo("아크") }, + { assertThat(blackJack.guests[0].cards.size).isEqualTo(2) }, + { assertThat(blackJack.guests[1].name.toString()).isEqualTo("로피") }, + { assertThat(blackJack.guests[1].cards.size).isEqualTo(2) }, { assertThat(blackJack.dealer.name.toString()).isEqualTo("딜러") }, { assertThat(blackJack.dealer.cards.size).isEqualTo(2) }, { assertThat(blackJack.cardDeck.size).isEqualTo(46) }, diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index 3d8d8f4e3..edd3e1fa8 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -10,7 +10,7 @@ class BlackJackGameTest { cardDeck(Card.all().shuffled()) participants { dealer("딜러") - users(listOf("아크", "로피")) + guests(listOf("아크", "로피")) } draw() } @@ -18,7 +18,7 @@ class BlackJackGameTest { assertDoesNotThrow { BlackJackGame().apply { input(::inputDrawMore) - blackJack.usersTurn(::outputCard) + blackJack.guestsTurn(::outputCard) blackJack.dealerTurn(::outputDealer) } } diff --git a/src/test/kotlin/blackjack/domain/BlackJackTest.kt b/src/test/kotlin/blackjack/domain/BlackJackTest.kt index c9a12c2ef..c79006c03 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackTest.kt @@ -7,15 +7,15 @@ class BlackJackTest { @Test fun `게임 결과를 반환한다`() { val blackJack = BlackJack( - participants = Participants(User("딜러"), listOf(User("아크"), User("로피"))), + participants = Participants(Dealer("딜러"), listOf(Guest("아크"), Guest("로피"))), cardDeck = CardDeck(Card.all()), ).apply { dealer.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) dealer.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) - users[0].draw(Card(CardMark.CLOVER, CardValue.NINE)) - users[0].draw(Card(CardMark.CLOVER, CardValue.NINE)) - users[1].draw(Card(CardMark.CLOVER, CardValue.ACE)) - users[1].draw(Card(CardMark.CLOVER, CardValue.QUEEN)) + guests[0].draw(Card(CardMark.CLOVER, CardValue.NINE)) + guests[0].draw(Card(CardMark.CLOVER, CardValue.NINE)) + guests[1].draw(Card(CardMark.CLOVER, CardValue.ACE)) + guests[1].draw(Card(CardMark.CLOVER, CardValue.QUEEN)) } assertThat(blackJack.result).isEqualTo(listOf(Outcome.LOSE, Outcome.WIN)) } diff --git a/src/test/kotlin/blackjack/domain/OutcomeTest.kt b/src/test/kotlin/blackjack/domain/OutcomeTest.kt index 8fcbcc510..6dcf65fa4 100644 --- a/src/test/kotlin/blackjack/domain/OutcomeTest.kt +++ b/src/test/kotlin/blackjack/domain/OutcomeTest.kt @@ -6,89 +6,89 @@ import org.junit.jupiter.api.Test class OutcomeTest { @Test fun `딜러와 유저의 점수가 같으면 비긴다`() { - val dealer = User("딜러") - val user = User("아크") + val dealer = Dealer("딜러") + val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) - user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) - user.draw(Card(CardMark.HEART, CardValue.SEVEN)) + guest.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) - assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.DRAW) + assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.DRAW) } @Test fun `딜러와 유저의 점수가 둘 다 21점 이상이면 비긴다`() { - val dealer = User("딜러") - val user = User("아크") + val dealer = Dealer("딜러") + val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) dealer.draw(Card(CardMark.HEART, CardValue.NINE)) - user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) - user.draw(Card(CardMark.HEART, CardValue.SEVEN)) - user.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) + guest.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) + guest.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) - assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.DRAW) + assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.DRAW) } @Test fun `딜러 점수가 유저 점수보다 크면 진다`() { - val dealer = User("딜러") - val user = User("아크") + val dealer = Dealer("딜러") + val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) dealer.draw(Card(CardMark.HEART, CardValue.THREE)) - user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) - user.draw(Card(CardMark.HEART, CardValue.SEVEN)) - user.draw(Card(CardMark.CLOVER, CardValue.TWO)) + guest.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) + guest.draw(Card(CardMark.CLOVER, CardValue.TWO)) - assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.LOSE) + assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.LOSE) } @Test fun `유저 점수가 21을 넘으면 진다`() { - val dealer = User("딜러") - val user = User("아크") + val dealer = Dealer("딜러") + val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) dealer.draw(Card(CardMark.HEART, CardValue.THREE)) - user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) - user.draw(Card(CardMark.HEART, CardValue.SEVEN)) - user.draw(Card(CardMark.CLOVER, CardValue.KING)) + guest.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) + guest.draw(Card(CardMark.CLOVER, CardValue.KING)) - assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.LOSE) + assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.LOSE) } @Test fun `유저 점수가 딜러 점수보다 크면 이긴다`() { - val dealer = User("딜러") - val user = User("아크") + val dealer = Dealer("딜러") + val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) dealer.draw(Card(CardMark.HEART, CardValue.TWO)) - user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) - user.draw(Card(CardMark.HEART, CardValue.SEVEN)) - user.draw(Card(CardMark.CLOVER, CardValue.THREE)) + guest.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) + guest.draw(Card(CardMark.CLOVER, CardValue.THREE)) - assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.WIN) + assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.WIN) } @Test fun `딜러 점수가 21을 넘으면 이긴다`() { - val dealer = User("딜러") - val user = User("아크") + val dealer = Dealer("딜러") + val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) dealer.draw(Card(CardMark.HEART, CardValue.KING)) - user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) - user.draw(Card(CardMark.HEART, CardValue.SEVEN)) - user.draw(Card(CardMark.CLOVER, CardValue.THREE)) + guest.draw(Card(CardMark.SPADE, CardValue.EIGHT)) + guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) + guest.draw(Card(CardMark.CLOVER, CardValue.THREE)) - assertThat(Outcome.of(dealer, user)).isEqualTo(Outcome.WIN) + assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.WIN) } } diff --git a/src/test/kotlin/blackjack/domain/UserTest.kt b/src/test/kotlin/blackjack/domain/UserTest.kt index 1ef51b173..4772815b2 100644 --- a/src/test/kotlin/blackjack/domain/UserTest.kt +++ b/src/test/kotlin/blackjack/domain/UserTest.kt @@ -6,40 +6,16 @@ import org.junit.jupiter.api.Test class UserTest { @Test fun `카드를 뽑을 수 있다`() { - val user = User("아크") + val user = Guest("아크") user.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) assertThat(user.cards.size).isEqualTo(1) } @Test fun `점수의 합을 반환한다`() { - val user = User("아크") + val user = Guest("아크") user.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) assertThat(user.score).isEqualTo(16) } - - @Test - fun `나올 수 있는 최저 점수를 반환한다`() { - val user = User("아크") - user.draw(Card(CardMark.CLOVER, CardValue.ACE)) - user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) - assertThat(user.minScore).isEqualTo(9) - } - - @Test - fun `나올 수 있는 최고 점수를 반환한다`() { - val user = User("아크") - user.draw(Card(CardMark.CLOVER, CardValue.ACE)) - user.draw(Card(CardMark.SPADE, CardValue.EIGHT)) - assertThat(user.maxScore).isEqualTo(19) - } } - -/* -User가 할일은? -카드를 가지고 있고 -점수를 가지고 있고 -더 갈지 안갈지 입력받고 처리한다. - - */ From d497eb029602e23f0aec710d8b5e962b68532c68 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 16:41:41 +0900 Subject: [PATCH 40/67] =?UTF-8?q?test(Test)=20:=20DSL=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/Test.kt | 98 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/test/kotlin/Test.kt diff --git a/src/test/kotlin/Test.kt b/src/test/kotlin/Test.kt new file mode 100644 index 000000000..76c7e95ee --- /dev/null +++ b/src/test/kotlin/Test.kt @@ -0,0 +1,98 @@ +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class PersonBuilder { + private lateinit var name: String + private var company: String? = null + private lateinit var skills: Skills + private lateinit var languages: Languages + + fun name(name: String) { + this.name = name + } + + fun company(company: String) { + this.company = company + } + + fun skills(block: SkillsBuilder.() -> Unit) { + skills = SkillsBuilder().apply { block() }.build() + } + + fun languages(block: LanguagesBuilder.() -> Unit) { + languages = LanguagesBuilder().apply { block() }.build() + } + + fun build(): Person = Person(name, company, skills, languages) +} + +class SkillsBuilder { + val soft = MutableList(0) { "" } + val hard = MutableList(0) { "" } + fun soft(name: String) = soft.add(name) + + fun hard(name: String) = hard.add(name) + + fun build(): Skills = Skills(soft, hard) +} + +class LanguagesBuilder { + var korean = 0 + var english = 0 + + fun build(): Languages = Languages(korean, english) + + infix fun String.level(grade: Int) { + when (this) { + "Korean" -> korean = grade + "English" -> english = grade + } + } +} + +fun introduce(block: PersonBuilder.() -> Unit): Person { + val personBuilder = PersonBuilder().apply(block) + return personBuilder.build() +} + +data class Person( + var name: String, + var company: String?, + var skills: Skills, + var languages: Languages, +) + +data class Skills( + val soft: List, + val hard: List, +) + +data class Languages( + val korean: Int, + val english: Int, +) + +class Test { + @Test + fun company() { + val person = introduce { + name("로피") + company("우아한형제들") + skills { + soft("A passion for problem solving") + soft("Good communication skills") + hard("Kotlin") + } + languages { + "Korean" level 5 + "English" level 3 + } + } + assertThat(person.name).isEqualTo("로피") + assertThat(person.company).isEqualTo("우아한형제들") + assertThat(person.skills.soft.size).isEqualTo(2) + assertThat(person.skills.hard.size).isEqualTo(1) + assertThat(person.languages.korean).isEqualTo(5) + assertThat(person.languages.english).isEqualTo(3) + } +} From 5ac413c39af7a063fb3666f65305b91423df2cb9 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Thu, 2 Mar 2023 20:29:15 +0900 Subject: [PATCH 41/67] =?UTF-8?q?fix(BlackJackGame)=20:=20=EC=B7=A8?= =?UTF-8?q?=EC=86=8C=20=EC=95=88=EB=90=98=EB=8A=94=20=EB=B2=84=EA=B7=B8=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 --- .../kotlin/blackjack/domain/BlackJackGame.kt | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 2c60e2f45..a3f14c0d6 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -5,14 +5,8 @@ class BlackJackGame { fun input(func: (String) -> String) { input = func } - fun BlackJack.guestsTurn(output: (User) -> Unit) { - guests.forEach { guest -> - while (guest.isContinue) { - command(guest, cardDeck) - output(guest) - } - } - } + fun BlackJack.guestsTurn(output: (User) -> Unit) = + guests.forEach { guest -> guestTurn(guest, output) } fun BlackJack.dealerTurn(output: () -> Unit) { if (dealer.isContinue) { @@ -21,12 +15,22 @@ class BlackJackGame { } } - private fun command(guest: Guest, cardDeck: CardDeck) { - if (guest.isDrawCommand()) return + private fun BlackJack.guestTurn(guest: Guest, output: (User) -> Unit) { + when (guest.getCommand()) { + in DRAW_COMMANDS -> draw(guest, output) + else -> output(guest) + } + } + + private fun BlackJack.draw(guest: Guest, output: (User) -> Unit) { guest.draw(cardDeck.nextCard()) + output(guest) + if (guest.isContinue) { + guestTurn(guest, output) + } } - private fun Guest.isDrawCommand() = input(this.name.toString()) !in DRAW_COMMANDS + private fun Guest.getCommand() = input(this.name.toString()) companion object { const val BLACKJACK_NUMBER = 21 From 4ac7f57a25603dd21e0e7de5b41983bb94e2b3bf Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:14:13 +0900 Subject: [PATCH 42/67] =?UTF-8?q?refactor(OutputView)=20:=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=ED=8F=AC=EB=A7=B7=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/view/OutputView.kt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index a59425abf..445b2064a 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -24,24 +24,22 @@ class OutputView { outputOutcomes(blackJack) } - fun outputCardForDealer(user: User) { - print("\n${user.name}카드:") - print(" ${user.cards.toList()[0].value.pattern()}${user.cards.toList()[0].mark.name()}") + private fun outputCardForDealer(user: User) { + val cardText = user.cards.toList()[0].let { it.value.pattern() + it.mark.name() } + print("\n${user.name}카드: $cardText") } fun outputCard(user: User) { - print("\n${user.name}카드") - user.cards.toList().forEach { - print(" ${it.value.pattern()}${it.mark.name()}") - } + val cardText = user.cards.toList().joinToString(", ") { it.value.pattern() + it.mark.name() } + print("\n${user.name}카드: $cardText") } fun outputDealerDraw() { - println("\n딜러는 16이하라 한장의 카드를 더 받았습니다.") + println("\n\n딜러는 16이하라 한장의 카드를 더 받았습니다.") } private fun outputScore(user: User) { - print("- 결과 ${user.score}") + print(" - 결과: ${user.score}") } private fun outputOutcomes(blackJack: BlackJack) { From aa8ee2f809a9bf182c458e22c7aaf77b54d3328b Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:18:28 +0900 Subject: [PATCH 43/67] =?UTF-8?q?refactor(OutputView)=20:=20=EB=94=9C?= =?UTF-8?q?=EB=9F=AC=EC=9D=98=20=EC=9D=B4=EB=A6=84=EC=9D=84=20default=20va?= =?UTF-8?q?lue=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.kt | 2 +- src/main/kotlin/blackjack/domain/Dealer.kt | 2 +- .../kotlin/blackjack/domain/ParticipantsBuilder.kt | 4 ++-- .../kotlin/blackjack/domain/BlackJackBuilderTest.kt | 2 +- .../kotlin/blackjack/domain/BlackJackGameTest.kt | 2 +- src/test/kotlin/blackjack/domain/BlackJackTest.kt | 2 +- src/test/kotlin/blackjack/domain/OutcomeTest.kt | 12 ++++++------ 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index e118385c0..1b324f242 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -21,7 +21,7 @@ class BlackJackController( private fun setBlackJack(): BlackJack = BlackJackBuilder.init { cardDeck(Card.all().shuffled()) participants { - dealer("딜러") + dealer() guests(inputView.inputParticipants()) } draw() diff --git a/src/main/kotlin/blackjack/domain/Dealer.kt b/src/main/kotlin/blackjack/domain/Dealer.kt index 0c917642c..983c1e253 100644 --- a/src/main/kotlin/blackjack/domain/Dealer.kt +++ b/src/main/kotlin/blackjack/domain/Dealer.kt @@ -1,6 +1,6 @@ package blackjack.domain -class Dealer(name: String) : User(name) { +class Dealer(name: String = "딜러") : User(name) { override val isContinue: Boolean // dealer get() = Score(cards).checkDealerScore } diff --git a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt index 78d1c8db0..bcba1c23e 100644 --- a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt +++ b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt @@ -4,8 +4,8 @@ class ParticipantsBuilder { private lateinit var dealer: Dealer private lateinit var guests: List - fun dealer(name: String) { - dealer = Dealer(name) + fun dealer() { + dealer = Dealer() } fun guests(names: List) { diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt index 33c1b8a65..feeda83e0 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt @@ -10,7 +10,7 @@ class BlackJackBuilderTest { val blackJack = BlackJackBuilder.init { cardDeck(Card.all().shuffled()) participants { - dealer("딜러") + dealer() guests(listOf("아크", "로피")) } draw() diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index edd3e1fa8..35486f7ab 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -9,7 +9,7 @@ class BlackJackGameTest { val blackJack = BlackJackBuilder.init { cardDeck(Card.all().shuffled()) participants { - dealer("딜러") + dealer() guests(listOf("아크", "로피")) } draw() diff --git a/src/test/kotlin/blackjack/domain/BlackJackTest.kt b/src/test/kotlin/blackjack/domain/BlackJackTest.kt index c79006c03..1bb1efb96 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackTest.kt @@ -7,7 +7,7 @@ class BlackJackTest { @Test fun `게임 결과를 반환한다`() { val blackJack = BlackJack( - participants = Participants(Dealer("딜러"), listOf(Guest("아크"), Guest("로피"))), + participants = Participants(Dealer(), listOf(Guest("아크"), Guest("로피"))), cardDeck = CardDeck(Card.all()), ).apply { dealer.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) diff --git a/src/test/kotlin/blackjack/domain/OutcomeTest.kt b/src/test/kotlin/blackjack/domain/OutcomeTest.kt index 6dcf65fa4..78f1b4091 100644 --- a/src/test/kotlin/blackjack/domain/OutcomeTest.kt +++ b/src/test/kotlin/blackjack/domain/OutcomeTest.kt @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test class OutcomeTest { @Test fun `딜러와 유저의 점수가 같으면 비긴다`() { - val dealer = Dealer("딜러") + val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) @@ -19,7 +19,7 @@ class OutcomeTest { @Test fun `딜러와 유저의 점수가 둘 다 21점 이상이면 비긴다`() { - val dealer = Dealer("딜러") + val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) @@ -34,7 +34,7 @@ class OutcomeTest { @Test fun `딜러 점수가 유저 점수보다 크면 진다`() { - val dealer = Dealer("딜러") + val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) @@ -49,7 +49,7 @@ class OutcomeTest { @Test fun `유저 점수가 21을 넘으면 진다`() { - val dealer = Dealer("딜러") + val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) @@ -64,7 +64,7 @@ class OutcomeTest { @Test fun `유저 점수가 딜러 점수보다 크면 이긴다`() { - val dealer = Dealer("딜러") + val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) @@ -79,7 +79,7 @@ class OutcomeTest { @Test fun `딜러 점수가 21을 넘으면 이긴다`() { - val dealer = Dealer("딜러") + val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) dealer.draw(Card(CardMark.DIA, CardValue.SEVEN)) From 7300126ad360c1c4aee0869351a32e9b82b361aa Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:20:33 +0900 Subject: [PATCH 44/67] =?UTF-8?q?refactor(InputView)=20:=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EC=95=88=EB=82=B4=20=EB=AC=B8=EA=B5=AC=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/kotlin/blackjack/view/InputView.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/view/InputView.kt b/src/main/kotlin/blackjack/view/InputView.kt index 0003a26f1..4509efabf 100644 --- a/src/main/kotlin/blackjack/view/InputView.kt +++ b/src/main/kotlin/blackjack/view/InputView.kt @@ -7,7 +7,7 @@ class InputView { } fun inputDrawMore(name: String): String { - println("\n${name}는 한장의 카드를 더 받겠습니까?") + println("\n${name}는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)") return readln() } } From dbc172859728721906bff13ec3a96f15e2a18532 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:27:05 +0900 Subject: [PATCH 45/67] =?UTF-8?q?refactor(Name)=20:=20=EC=9D=B4=EB=A6=84?= =?UTF-8?q?=EC=9D=98=20=EA=B8=80=EC=9E=90=EC=88=98=20=EC=A0=9C=ED=95=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/Name.kt | 5 +++-- src/test/kotlin/blackjack/domain/NameTest.kt | 17 ++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/Name.kt b/src/main/kotlin/blackjack/domain/Name.kt index febfa26b5..fd95267dc 100644 --- a/src/main/kotlin/blackjack/domain/Name.kt +++ b/src/main/kotlin/blackjack/domain/Name.kt @@ -3,13 +3,14 @@ package blackjack.domain @JvmInline value class Name(private val value: String) { init { - require(value.length <= NAME_LENGTH_MAX) { ERROR_NAME_LENGTH_MAX } + require(value.length in NAME_LENGTH_MIN..NAME_LENGTH_MAX) { ERROR_NAME_LENGTH_MAX } } override fun toString(): String = value companion object { + private const val NAME_LENGTH_MIN = 1 private const val NAME_LENGTH_MAX = 20 - private const val ERROR_NAME_LENGTH_MAX = "이름의 최대 글자수는 ${NAME_LENGTH_MAX}글자 입니다." + private const val ERROR_NAME_LENGTH_MAX = "이름의 글자수는 $NAME_LENGTH_MIN~${NAME_LENGTH_MAX}사이 입니다." } } diff --git a/src/test/kotlin/blackjack/domain/NameTest.kt b/src/test/kotlin/blackjack/domain/NameTest.kt index 0895af64d..279a4f753 100644 --- a/src/test/kotlin/blackjack/domain/NameTest.kt +++ b/src/test/kotlin/blackjack/domain/NameTest.kt @@ -1,22 +1,25 @@ package blackjack.domain -import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource class NameTest { - @Test - fun `이름은 20자 이하다`() { + @ParameterizedTest + @ValueSource(strings = ["아", "아크,로피,아크,로피,아크,로피,아크"]) + fun `이름은 1~20자 이하다`(name: String) { assertDoesNotThrow { - Name("아크,로피,아크,로피,아크,로피,아크") + Name(name) } } - @Test - fun `이름이 21자 이상이면 에러난다`() { + @ParameterizedTest + @ValueSource(strings = ["", "가나다라마바사아자차카타파하abcdefjhijklmnopqrstuvwxyz"]) + fun `이름은 1~20자 이외면 에러난다`(name: String) { assertThrows { - Name("가나다라마바사아자차카타파하abcdefjhijklmnopqrstuvwxyz") + Name(name) } } } From f4066c266acb554c3ce21cb4b9fc1f5fb874089c Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:36:07 +0900 Subject: [PATCH 46/67] =?UTF-8?q?refactor(BlackJackGame)=20:=20=EB=A9=A4?= =?UTF-8?q?=EB=B2=84=EB=B3=80=EC=88=98=20=EC=A0=95=EC=9D=98=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20=EB=B0=8F=20=EC=BB=A4=EB=A7=A8=EB=93=9C=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackGame.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index a3f14c0d6..1547f1684 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -16,9 +16,10 @@ class BlackJackGame { } private fun BlackJack.guestTurn(guest: Guest, output: (User) -> Unit) { - when (guest.getCommand()) { + when (getCommand(guest.name)) { in DRAW_COMMANDS -> draw(guest, output) - else -> output(guest) + in END_TURN_COMMANDS -> output(guest) + else -> this.guestTurn(guest, output) } } @@ -30,10 +31,11 @@ class BlackJackGame { } } - private fun Guest.getCommand() = input(this.name.toString()) + private fun getCommand(name: Name) = input(name.toString()) companion object { const val BLACKJACK_NUMBER = 21 private val DRAW_COMMANDS = listOf("Y", "y") + private val END_TURN_COMMANDS = listOf("N", "n") } } From 71711db6c7765f3e331009196848dcb188512728 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:39:19 +0900 Subject: [PATCH 47/67] =?UTF-8?q?refactor(Score)=20:=20=EC=97=AD=ED=95=A0?= =?UTF-8?q?=20=EC=9E=AC=EB=B6=80=EC=97=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/Dealer.kt | 8 ++++++-- src/main/kotlin/blackjack/domain/Guest.kt | 4 ++-- src/main/kotlin/blackjack/domain/Score.kt | 11 ++--------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/Dealer.kt b/src/main/kotlin/blackjack/domain/Dealer.kt index 983c1e253..d641dc174 100644 --- a/src/main/kotlin/blackjack/domain/Dealer.kt +++ b/src/main/kotlin/blackjack/domain/Dealer.kt @@ -1,6 +1,10 @@ package blackjack.domain class Dealer(name: String = "딜러") : User(name) { - override val isContinue: Boolean // dealer - get() = Score(cards).checkDealerScore + override val isContinue: Boolean + get() = Score(cards).maxScore < DEALER_MIN_NUMBER + + companion object { + private const val DEALER_MIN_NUMBER = 17 + } } diff --git a/src/main/kotlin/blackjack/domain/Guest.kt b/src/main/kotlin/blackjack/domain/Guest.kt index eedda0d74..c4f8137c5 100644 --- a/src/main/kotlin/blackjack/domain/Guest.kt +++ b/src/main/kotlin/blackjack/domain/Guest.kt @@ -1,6 +1,6 @@ package blackjack.domain class Guest(name: String) : User(name) { - override val isContinue: Boolean // dealer - get() = Score(cards).isNotBust + override val isContinue: Boolean + get() = Score(cards).minScore <= BlackJackGame.BLACKJACK_NUMBER } diff --git a/src/main/kotlin/blackjack/domain/Score.kt b/src/main/kotlin/blackjack/domain/Score.kt index ba21cfcf7..c25a2f8bc 100644 --- a/src/main/kotlin/blackjack/domain/Score.kt +++ b/src/main/kotlin/blackjack/domain/Score.kt @@ -4,16 +4,10 @@ class Score(val cards: Cards) { val score: Int get() = if (isMaxScoreInRange) maxScore else minScore - val isNotBust: Boolean - get() = minScore <= BlackJackGame.BLACKJACK_NUMBER - - val checkDealerScore: Boolean - get() = maxScore < DEALER_MIN_NUMBER - - private val minScore: Int + val minScore: Int get() = cards.toList().sumOf { it.value.value } - private val maxScore: Int + val maxScore: Int get() = minScore + if (cards.containsACE() && validateAceCondition) ACE_OTHER_NUMBER_DIFF else 0 private val validateAceCondition: Boolean @@ -23,7 +17,6 @@ class Score(val cards: Cards) { get() = maxScore <= BlackJackGame.BLACKJACK_NUMBER companion object { - private const val DEALER_MIN_NUMBER = 17 private const val ACE_OTHER_NUMBER_DIFF = 10 } } From cd1824f9f7c66fa21306a797f16072b3576da87e Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:53:04 +0900 Subject: [PATCH 48/67] =?UTF-8?q?refactor(User)=20:=20=EB=AF=B8=EB=B0=98?= =?UTF-8?q?=EC=98=81=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackGame.kt | 2 ++ src/main/kotlin/blackjack/domain/Guest.kt | 3 +++ src/main/kotlin/blackjack/domain/User.kt | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index 1547f1684..ac273af8c 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -9,6 +9,7 @@ class BlackJackGame { guests.forEach { guest -> guestTurn(guest, output) } fun BlackJack.dealerTurn(output: () -> Unit) { + if (dealer.isBlackJack) return if (dealer.isContinue) { dealer.draw(cardDeck.nextCard()) output() @@ -16,6 +17,7 @@ class BlackJackGame { } private fun BlackJack.guestTurn(guest: Guest, output: (User) -> Unit) { + if (guest.isBlackJack) return when (getCommand(guest.name)) { in DRAW_COMMANDS -> draw(guest, output) in END_TURN_COMMANDS -> output(guest) diff --git a/src/main/kotlin/blackjack/domain/Guest.kt b/src/main/kotlin/blackjack/domain/Guest.kt index c4f8137c5..17ed35348 100644 --- a/src/main/kotlin/blackjack/domain/Guest.kt +++ b/src/main/kotlin/blackjack/domain/Guest.kt @@ -2,5 +2,8 @@ package blackjack.domain class Guest(name: String) : User(name) { override val isContinue: Boolean + get() = isNotBust && isBlackJack.not() + + private val isNotBust: Boolean get() = Score(cards).minScore <= BlackJackGame.BLACKJACK_NUMBER } diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index d808b4cfa..0bfe1cdac 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -7,6 +7,10 @@ abstract class User(name: String) { get() = Score(cards).score abstract val isContinue: Boolean + + val isBlackJack: Boolean + get() = Score(cards).score == BlackJackGame.BLACKJACK_NUMBER + fun draw(card: Card) { cards.add(card) } From 8f8cbabf70b3e9fb9245296333df70959df31841 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:11:35 +0900 Subject: [PATCH 49/67] =?UTF-8?q?refactor(Outcome)=20:=20=ED=99=95?= =?UTF-8?q?=EC=9E=A5=ED=95=A8=EC=88=98=EB=A5=BC=20=EC=A0=81=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EA=B0=80=EB=8F=85=EC=84=B1=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJack.kt | 4 +++- src/main/kotlin/blackjack/domain/Outcome.kt | 14 ++++++------- .../kotlin/blackjack/domain/OutcomeTest.kt | 21 ++++++++++--------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJack.kt b/src/main/kotlin/blackjack/domain/BlackJack.kt index 4c9d992ce..0423659a0 100644 --- a/src/main/kotlin/blackjack/domain/BlackJack.kt +++ b/src/main/kotlin/blackjack/domain/BlackJack.kt @@ -1,11 +1,13 @@ package blackjack.domain +import blackjack.domain.Outcome.Companion.winTo + data class BlackJack( val cardDeck: CardDeck, val participants: Participants, ) { val result: List - get() = participants.guests.map { guest -> Outcome.of(participants.dealer, guest) } + get() = participants.guests.map { guest -> guest.winTo(participants.dealer) } val dealer: Dealer get() = participants.dealer diff --git a/src/main/kotlin/blackjack/domain/Outcome.kt b/src/main/kotlin/blackjack/domain/Outcome.kt index ecc967a49..eb7ed0555 100644 --- a/src/main/kotlin/blackjack/domain/Outcome.kt +++ b/src/main/kotlin/blackjack/domain/Outcome.kt @@ -7,15 +7,15 @@ enum class Outcome { WIN, DRAW, LOSE; companion object { - fun of(dealer: Dealer, guest: Guest): Outcome = + fun User.winTo(other: User): Outcome = when { - dealer.score > BLACKJACK_NUMBER && guest.score > BLACKJACK_NUMBER -> DRAW - dealer.score > BLACKJACK_NUMBER -> WIN - guest.score > BLACKJACK_NUMBER -> LOSE + other.score > BLACKJACK_NUMBER && this.score > BLACKJACK_NUMBER -> DRAW + other.score > BLACKJACK_NUMBER -> WIN + this.score > BLACKJACK_NUMBER -> LOSE - dealer.score == guest.score -> DRAW - guest.score > dealer.score -> WIN - dealer.score > guest.score -> LOSE + other.score == this.score -> DRAW + this.score > other.score -> WIN + other.score > this.score -> LOSE else -> throw IllegalStateException() } diff --git a/src/test/kotlin/blackjack/domain/OutcomeTest.kt b/src/test/kotlin/blackjack/domain/OutcomeTest.kt index 78f1b4091..80222e2b6 100644 --- a/src/test/kotlin/blackjack/domain/OutcomeTest.kt +++ b/src/test/kotlin/blackjack/domain/OutcomeTest.kt @@ -1,5 +1,6 @@ package blackjack.domain +import blackjack.domain.Outcome.Companion.winTo import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test @@ -14,7 +15,7 @@ class OutcomeTest { guest.draw(Card(CardMark.SPADE, CardValue.EIGHT)) guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) - assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.DRAW) + assertThat(guest.winTo(dealer)).isEqualTo(Outcome.DRAW) } @Test @@ -29,11 +30,11 @@ class OutcomeTest { guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) guest.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) - assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.DRAW) + assertThat(guest.winTo(dealer)).isEqualTo(Outcome.DRAW) } @Test - fun `딜러 점수가 유저 점수보다 크면 진다`() { + fun `딜러 점수가 유저 점수보다 크면 유저가 진다`() { val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) @@ -44,11 +45,11 @@ class OutcomeTest { guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) guest.draw(Card(CardMark.CLOVER, CardValue.TWO)) - assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.LOSE) + assertThat(guest.winTo(dealer)).isEqualTo(Outcome.LOSE) } @Test - fun `유저 점수가 21을 넘으면 진다`() { + fun `유저 점수가 21을 넘으면 유저가 진다`() { val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) @@ -59,11 +60,11 @@ class OutcomeTest { guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) guest.draw(Card(CardMark.CLOVER, CardValue.KING)) - assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.LOSE) + assertThat(guest.winTo(dealer)).isEqualTo(Outcome.LOSE) } @Test - fun `유저 점수가 딜러 점수보다 크면 이긴다`() { + fun `유저 점수가 딜러 점수보다 크면 유저가 이긴다`() { val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) @@ -74,11 +75,11 @@ class OutcomeTest { guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) guest.draw(Card(CardMark.CLOVER, CardValue.THREE)) - assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.WIN) + assertThat(guest.winTo(dealer)).isEqualTo(Outcome.WIN) } @Test - fun `딜러 점수가 21을 넘으면 이긴다`() { + fun `딜러 점수가 21을 넘으면 유저가 이긴다`() { val dealer = Dealer() val guest = Guest("아크") dealer.draw(Card(CardMark.CLOVER, CardValue.EIGHT)) @@ -89,6 +90,6 @@ class OutcomeTest { guest.draw(Card(CardMark.HEART, CardValue.SEVEN)) guest.draw(Card(CardMark.CLOVER, CardValue.THREE)) - assertThat(Outcome.of(dealer, guest)).isEqualTo(Outcome.WIN) + assertThat(guest.winTo(dealer)).isEqualTo(Outcome.WIN) } } From ff971bee7fceeef5a5dc88480d4743dfabfc8c95 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:20:30 +0900 Subject: [PATCH 50/67] =?UTF-8?q?refactor(Card)=20:=20Card=EC=9D=98=20?= =?UTF-8?q?=EC=97=AD=ED=95=A0=EC=9D=84=20Cards=EC=99=80=20CardDeck?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B6=84=EB=B0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.kt | 4 ++-- .../kotlin/blackjack/domain/BlackJackBuilder.kt | 1 + src/main/kotlin/blackjack/domain/Card.kt | 13 +------------ src/main/kotlin/blackjack/domain/CardDeck.kt | 2 ++ src/main/kotlin/blackjack/domain/Cards.kt | 10 ++++++++++ .../kotlin/blackjack/domain/BlackJackBuilderTest.kt | 2 +- .../kotlin/blackjack/domain/BlackJackGameTest.kt | 2 +- src/test/kotlin/blackjack/domain/BlackJackTest.kt | 2 +- src/test/kotlin/blackjack/domain/CardDeckTest.kt | 8 ++++---- 9 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 1b324f242..d10283194 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -3,7 +3,7 @@ package blackjack.controller import blackjack.domain.BlackJack import blackjack.domain.BlackJackBuilder import blackjack.domain.BlackJackGame -import blackjack.domain.Card +import blackjack.domain.Cards import blackjack.view.InputView import blackjack.view.OutputView @@ -19,7 +19,7 @@ class BlackJackController( } private fun setBlackJack(): BlackJack = BlackJackBuilder.init { - cardDeck(Card.all().shuffled()) + cardDeck(Cards.all()) participants { dealer() guests(inputView.inputParticipants()) diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index 80df83869..a64f15ee8 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -5,6 +5,7 @@ class BlackJackBuilder { private lateinit var participants: Participants fun cardDeck(cards: List) { cardDeck = CardDeck(cards) + cardDeck.shuffle() } fun participants(block: ParticipantsBuilder.() -> Unit) { diff --git a/src/main/kotlin/blackjack/domain/Card.kt b/src/main/kotlin/blackjack/domain/Card.kt index d075010b0..cb4ba7d16 100644 --- a/src/main/kotlin/blackjack/domain/Card.kt +++ b/src/main/kotlin/blackjack/domain/Card.kt @@ -1,14 +1,3 @@ package blackjack.domain -data class Card(val mark: CardMark, val value: CardValue) { - - companion object { - private val CARDS: List = - CardMark.values().map { mark -> - CardValue.values().map { value -> - Card(mark, value) - } - }.flatten() - fun all(): List = CARDS - } -} +data class Card(val mark: CardMark, val value: CardValue) diff --git a/src/main/kotlin/blackjack/domain/CardDeck.kt b/src/main/kotlin/blackjack/domain/CardDeck.kt index 4f1d6c2de..8199404ac 100644 --- a/src/main/kotlin/blackjack/domain/CardDeck.kt +++ b/src/main/kotlin/blackjack/domain/CardDeck.kt @@ -13,6 +13,8 @@ class CardDeck(cards: List) { fun nextCard(): Card = cards.removeFirst() + fun shuffle() = cards.shuffle() + companion object { private const val CARDS_SIZE = 52 private const val ERROR_INVALID_CARDS_SIZE = "카드덱 초기 사이즈는 ${CARDS_SIZE}장이어야 합니다." diff --git a/src/main/kotlin/blackjack/domain/Cards.kt b/src/main/kotlin/blackjack/domain/Cards.kt index a13b9a3c9..6ee0c99b6 100644 --- a/src/main/kotlin/blackjack/domain/Cards.kt +++ b/src/main/kotlin/blackjack/domain/Cards.kt @@ -9,4 +9,14 @@ class Cards(private val cards: MutableSet = mutableSetOf()) { fun add(card: Card) = cards.add(card) fun containsACE() = cards.map { it.value }.contains(CardValue.ACE) + + companion object { + private val CARDS: List = + CardMark.values().map { mark -> + CardValue.values().map { value -> + Card(mark, value) + } + }.flatten() + fun all(): List = CARDS + } } diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt index feeda83e0..10591847a 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt @@ -8,7 +8,7 @@ class BlackJackBuilderTest { @Test fun `세팅이 된다`() { val blackJack = BlackJackBuilder.init { - cardDeck(Card.all().shuffled()) + cardDeck(Cards.all()) participants { dealer() guests(listOf("아크", "로피")) diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index 35486f7ab..173c59d23 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -7,7 +7,7 @@ class BlackJackGameTest { @Test fun `게임을 실행한다`() { val blackJack = BlackJackBuilder.init { - cardDeck(Card.all().shuffled()) + cardDeck(Cards.all()) participants { dealer() guests(listOf("아크", "로피")) diff --git a/src/test/kotlin/blackjack/domain/BlackJackTest.kt b/src/test/kotlin/blackjack/domain/BlackJackTest.kt index 1bb1efb96..70563a48c 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackTest.kt @@ -8,7 +8,7 @@ class BlackJackTest { fun `게임 결과를 반환한다`() { val blackJack = BlackJack( participants = Participants(Dealer(), listOf(Guest("아크"), Guest("로피"))), - cardDeck = CardDeck(Card.all()), + cardDeck = CardDeck(Cards.all()), ).apply { dealer.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) dealer.draw(Card(CardMark.CLOVER, CardValue.QUEEN)) diff --git a/src/test/kotlin/blackjack/domain/CardDeckTest.kt b/src/test/kotlin/blackjack/domain/CardDeckTest.kt index 1faea619e..f7c3bac22 100644 --- a/src/test/kotlin/blackjack/domain/CardDeckTest.kt +++ b/src/test/kotlin/blackjack/domain/CardDeckTest.kt @@ -8,13 +8,13 @@ class CardDeckTest { @Test fun `카드덱 잘 있는지 확인`() { - val cardDeck = CardDeck(Card.all()) + val cardDeck = CardDeck(Cards.all()) assertThat(cardDeck.size).isEqualTo(52) } @Test fun `카드 draw가 잘 되었는지 확인`() { - val cardDeck = CardDeck(Card.all()) + val cardDeck = CardDeck(Cards.all()) cardDeck.nextCard() assertThat(cardDeck.size).isEqualTo(51) } @@ -22,14 +22,14 @@ class CardDeckTest { @Test fun `초기 카드는 52장이다`() { assertThrows { - CardDeck(Card.all().take(51)) + CardDeck(Cards.all().take(51)) } } @Test fun `카드는 중복이 없어야 합니다`() { assertThrows { - CardDeck(Card.all() + Card.all()) + CardDeck(Cards.all() + Cards.all()) } } } From c410c87bf307302e9aea5d3daa933bf46efe511f Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:38:42 +0900 Subject: [PATCH 51/67] =?UTF-8?q?refactor(CardTest)=20:=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EA=B0=80?= =?UTF-8?q?=EB=8F=85=EC=84=B1=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/domain/CardDeckTest.kt | 10 +-- .../kotlin/blackjack/domain/CardValueTest.kt | 70 ++----------------- 2 files changed, 11 insertions(+), 69 deletions(-) diff --git a/src/test/kotlin/blackjack/domain/CardDeckTest.kt b/src/test/kotlin/blackjack/domain/CardDeckTest.kt index f7c3bac22..f52b89693 100644 --- a/src/test/kotlin/blackjack/domain/CardDeckTest.kt +++ b/src/test/kotlin/blackjack/domain/CardDeckTest.kt @@ -7,29 +7,29 @@ import org.junit.jupiter.api.assertThrows class CardDeckTest { @Test - fun `카드덱 잘 있는지 확인`() { + fun `초기 카드 덱은 52장이다`() { val cardDeck = CardDeck(Cards.all()) assertThat(cardDeck.size).isEqualTo(52) } @Test - fun `카드 draw가 잘 되었는지 확인`() { + fun `카드를 뽑으면 숫자가 줄어든다`() { val cardDeck = CardDeck(Cards.all()) cardDeck.nextCard() assertThat(cardDeck.size).isEqualTo(51) } @Test - fun `초기 카드는 52장이다`() { + fun `초기 카드가 52장이 아니면 에러난다`() { assertThrows { CardDeck(Cards.all().take(51)) } } @Test - fun `카드는 중복이 없어야 합니다`() { + fun `초기 카드에 중복이 있으면 에러난다`() { assertThrows { - CardDeck(Cards.all() + Cards.all()) + CardDeck(Cards.all().take(26) + Cards.all().take(26)) } } } diff --git a/src/test/kotlin/blackjack/domain/CardValueTest.kt b/src/test/kotlin/blackjack/domain/CardValueTest.kt index 753092aa3..544c4e222 100644 --- a/src/test/kotlin/blackjack/domain/CardValueTest.kt +++ b/src/test/kotlin/blackjack/domain/CardValueTest.kt @@ -1,72 +1,14 @@ package blackjack.domain import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource class CardValueTest { - @Test - fun `A의 값은 1이다`() { - assertThat(CardValue.ACE.value).isEqualTo(1) - } - - @Test - fun `K의 값은 10이다`() { - assertThat(CardValue.KING.value).isEqualTo(10) - } - - @Test - fun `Q의 값은 10이다`() { - assertThat(CardValue.QUEEN.value).isEqualTo(10) - } - - @Test - fun `J의 값은 10이다`() { - assertThat(CardValue.JACK.value).isEqualTo(10) - } - - @Test - fun `TEN의 값은 10이다`() { - assertThat(CardValue.TEN.value).isEqualTo(10) - } - - @Test - fun `NINE의 값은 9이다`() { - assertThat(CardValue.NINE.value).isEqualTo(9) - } - - @Test - fun `EIGHT의 값은 8이다`() { - assertThat(CardValue.EIGHT.value).isEqualTo(8) - } - - @Test - fun `SEVEN의 값은 7이다`() { - assertThat(CardValue.SEVEN.value).isEqualTo(7) - } - - @Test - fun `SIX의 값은 6이다`() { - assertThat(CardValue.SIX.value).isEqualTo(6) - } - - @Test - fun `FIVE의 값은 5이다`() { - assertThat(CardValue.FIVE.value).isEqualTo(5) - } - - @Test - fun `FOUR의 값은 4이다`() { - assertThat(CardValue.FOUR.value).isEqualTo(4) - } - - @Test - fun `THREE의 값은 3이다`() { - assertThat(CardValue.THREE.value).isEqualTo(3) - } - - @Test - fun `TWO의 값은 2이다`() { - assertThat(CardValue.TWO.value).isEqualTo(2) + @ParameterizedTest + @CsvSource(value = ["ACE,1", "KING,10", "QUEEN,10", "JACK,10", "TEN,10", "NINE,9", "EIGHT,8", "SEVEN,7", "SIX,6", "FIVE,5", "FOUR,4", "THREE,3", "TWO,2"]) + fun `카드의 값들이 나온다`(cardValue: CardValue, value: Int) { + assertThat(cardValue.value).isEqualTo(value) } } From bf0c973712063650f162671139d1307b3f4b8a3e Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:41:18 +0900 Subject: [PATCH 52/67] =?UTF-8?q?refactor(CardTest)=20:=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EA=B0=80?= =?UTF-8?q?=EB=8F=85=EC=84=B1=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/blackjack/domain/CardTest.kt | 35 +++++--------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/src/test/kotlin/blackjack/domain/CardTest.kt b/src/test/kotlin/blackjack/domain/CardTest.kt index 5086df39d..6b547f2ce 100644 --- a/src/test/kotlin/blackjack/domain/CardTest.kt +++ b/src/test/kotlin/blackjack/domain/CardTest.kt @@ -1,34 +1,15 @@ package blackjack.domain import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource class CardTest { - @Test - fun `카드의 문양과 숫자를 가져올 수 있다`() { - val card = Card(CardMark.CLOVER, CardValue.ACE) - assertThat(card.mark).isEqualTo(CardMark.CLOVER) - assertThat(card.value).isEqualTo(CardValue.ACE) - } - - @Test - fun `카드의 문양과 숫자를 가져올 수 있다2`() { - val card = Card(CardMark.HEART, CardValue.TEN) - assertThat(card.mark).isEqualTo(CardMark.HEART) - assertThat(card.value).isEqualTo(CardValue.TEN) - } - - @Test - fun `카드의 문양과 숫자를 가져올 수 있다3`() { - val card = Card(CardMark.SPADE, CardValue.TWO) - assertThat(card.mark).isEqualTo(CardMark.SPADE) - assertThat(card.value).isEqualTo(CardValue.TWO) - } - - @Test - fun `카드의 문양과 숫자를 가져올 수 있다4`() { - val card = Card(CardMark.DIA, CardValue.JACK) - assertThat(card.mark).isEqualTo(CardMark.DIA) - assertThat(card.value).isEqualTo(CardValue.JACK) + @ParameterizedTest + @CsvSource(value = ["CLOVER, ACE", "HEART, TEN", "SPADE, TWO", "DIA, JACK"]) + fun `카드의 문양과 숫자를 가져올 수 있다`(mark: CardMark, value: CardValue) { + val card = Card(mark, value) + assertThat(card.mark).isEqualTo(mark) + assertThat(card.value).isEqualTo(value) } } From 8c750d94742be6a2435be781b672d61ef0c9f6b1 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 17:12:49 +0900 Subject: [PATCH 53/67] =?UTF-8?q?refactor(BlackJack)=20:=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=83=81=EC=88=98=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/Guest.kt | 6 +++++- src/main/kotlin/blackjack/domain/Outcome.kt | 3 +-- src/main/kotlin/blackjack/domain/Score.kt | 5 +++-- src/main/kotlin/blackjack/domain/User.kt | 6 +++++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/Guest.kt b/src/main/kotlin/blackjack/domain/Guest.kt index 17ed35348..d9c916f0b 100644 --- a/src/main/kotlin/blackjack/domain/Guest.kt +++ b/src/main/kotlin/blackjack/domain/Guest.kt @@ -5,5 +5,9 @@ class Guest(name: String) : User(name) { get() = isNotBust && isBlackJack.not() private val isNotBust: Boolean - get() = Score(cards).minScore <= BlackJackGame.BLACKJACK_NUMBER + get() = Score(cards).minScore <= BLACKJACK_NUMBER + + companion object { + private const val BLACKJACK_NUMBER = 21 + } } diff --git a/src/main/kotlin/blackjack/domain/Outcome.kt b/src/main/kotlin/blackjack/domain/Outcome.kt index eb7ed0555..fadc41b45 100644 --- a/src/main/kotlin/blackjack/domain/Outcome.kt +++ b/src/main/kotlin/blackjack/domain/Outcome.kt @@ -1,12 +1,11 @@ package blackjack.domain - -import blackjack.domain.BlackJackGame.Companion.BLACKJACK_NUMBER import java.lang.IllegalStateException enum class Outcome { WIN, DRAW, LOSE; companion object { + private const val BLACKJACK_NUMBER = 21 fun User.winTo(other: User): Outcome = when { other.score > BLACKJACK_NUMBER && this.score > BLACKJACK_NUMBER -> DRAW diff --git a/src/main/kotlin/blackjack/domain/Score.kt b/src/main/kotlin/blackjack/domain/Score.kt index c25a2f8bc..9e18c1a51 100644 --- a/src/main/kotlin/blackjack/domain/Score.kt +++ b/src/main/kotlin/blackjack/domain/Score.kt @@ -11,12 +11,13 @@ class Score(val cards: Cards) { get() = minScore + if (cards.containsACE() && validateAceCondition) ACE_OTHER_NUMBER_DIFF else 0 private val validateAceCondition: Boolean - get() = minScore + ACE_OTHER_NUMBER_DIFF <= BlackJackGame.BLACKJACK_NUMBER + get() = minScore + ACE_OTHER_NUMBER_DIFF <= BLACKJACK_NUMBER private val isMaxScoreInRange: Boolean - get() = maxScore <= BlackJackGame.BLACKJACK_NUMBER + get() = maxScore <= BLACKJACK_NUMBER companion object { private const val ACE_OTHER_NUMBER_DIFF = 10 + private const val BLACKJACK_NUMBER = 21 } } diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 0bfe1cdac..518661d16 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -9,9 +9,13 @@ abstract class User(name: String) { abstract val isContinue: Boolean val isBlackJack: Boolean - get() = Score(cards).score == BlackJackGame.BLACKJACK_NUMBER + get() = Score(cards).score == BLACKJACK_NUMBER fun draw(card: Card) { cards.add(card) } + + companion object { + private const val BLACKJACK_NUMBER = 21 + } } From 60ed19dcf7683ca791defc876ed9e488b4da49cb Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Fri, 3 Mar 2023 17:18:08 +0900 Subject: [PATCH 54/67] =?UTF-8?q?refactor(Builder)=20:=20init=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.kt | 3 ++- src/main/kotlin/blackjack/domain/BlackJackBuilder.kt | 11 +++++------ .../kotlin/blackjack/domain/ParticipantsBuilder.kt | 5 ----- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index d10283194..80851909b 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -4,6 +4,7 @@ import blackjack.domain.BlackJack import blackjack.domain.BlackJackBuilder import blackjack.domain.BlackJackGame import blackjack.domain.Cards +import blackjack.domain.introduce import blackjack.view.InputView import blackjack.view.OutputView @@ -18,7 +19,7 @@ class BlackJackController( outputView.outputResult(blackJack) } - private fun setBlackJack(): BlackJack = BlackJackBuilder.init { + private fun setBlackJack(): BlackJack = introduce { cardDeck(Cards.all()) participants { dealer() diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index a64f15ee8..c0f87545e 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -1,5 +1,9 @@ package blackjack.domain +fun introduce(block: BlackJackBuilder.() -> Unit): BlackJack { + return BlackJackBuilder().apply(block).build() +} + class BlackJackBuilder { private lateinit var cardDeck: CardDeck private lateinit var participants: Participants @@ -9,7 +13,7 @@ class BlackJackBuilder { } fun participants(block: ParticipantsBuilder.() -> Unit) { - participants = ParticipantsBuilder.init { block() } + participants = ParticipantsBuilder().apply(block).build() } fun draw() { @@ -22,9 +26,4 @@ class BlackJackBuilder { fun build(): BlackJack { return BlackJack(cardDeck, participants) } - - companion object { - fun init(block: BlackJackBuilder.() -> Unit): BlackJack = - BlackJackBuilder().apply(block).build() - } } diff --git a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt index bcba1c23e..56f86b05c 100644 --- a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt +++ b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt @@ -15,9 +15,4 @@ class ParticipantsBuilder { fun build(): Participants { return Participants(dealer, guests) } - - companion object { - fun init(block: ParticipantsBuilder.() -> Unit): Participants = - ParticipantsBuilder().apply(block).build() - } } From c96c354939a865d46378db469eaf669fee434050 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Sun, 5 Mar 2023 16:56:19 +0900 Subject: [PATCH 55/67] =?UTF-8?q?feat=20(study)=20:=20=EC=88=99=EC=A0=9C?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/Test.kt | 98 ---------------------------- src/test/kotlin/study/DSLTest.kt | 97 +++++++++++++++++++++++++++ src/test/kotlin/study/LambdaTest1.kt | 31 +++++++++ src/test/kotlin/study/LambdaTest2.kt | 37 +++++++++++ 4 files changed, 165 insertions(+), 98 deletions(-) delete mode 100644 src/test/kotlin/Test.kt create mode 100644 src/test/kotlin/study/DSLTest.kt create mode 100644 src/test/kotlin/study/LambdaTest1.kt create mode 100644 src/test/kotlin/study/LambdaTest2.kt diff --git a/src/test/kotlin/Test.kt b/src/test/kotlin/Test.kt deleted file mode 100644 index 76c7e95ee..000000000 --- a/src/test/kotlin/Test.kt +++ /dev/null @@ -1,98 +0,0 @@ -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test - -class PersonBuilder { - private lateinit var name: String - private var company: String? = null - private lateinit var skills: Skills - private lateinit var languages: Languages - - fun name(name: String) { - this.name = name - } - - fun company(company: String) { - this.company = company - } - - fun skills(block: SkillsBuilder.() -> Unit) { - skills = SkillsBuilder().apply { block() }.build() - } - - fun languages(block: LanguagesBuilder.() -> Unit) { - languages = LanguagesBuilder().apply { block() }.build() - } - - fun build(): Person = Person(name, company, skills, languages) -} - -class SkillsBuilder { - val soft = MutableList(0) { "" } - val hard = MutableList(0) { "" } - fun soft(name: String) = soft.add(name) - - fun hard(name: String) = hard.add(name) - - fun build(): Skills = Skills(soft, hard) -} - -class LanguagesBuilder { - var korean = 0 - var english = 0 - - fun build(): Languages = Languages(korean, english) - - infix fun String.level(grade: Int) { - when (this) { - "Korean" -> korean = grade - "English" -> english = grade - } - } -} - -fun introduce(block: PersonBuilder.() -> Unit): Person { - val personBuilder = PersonBuilder().apply(block) - return personBuilder.build() -} - -data class Person( - var name: String, - var company: String?, - var skills: Skills, - var languages: Languages, -) - -data class Skills( - val soft: List, - val hard: List, -) - -data class Languages( - val korean: Int, - val english: Int, -) - -class Test { - @Test - fun company() { - val person = introduce { - name("로피") - company("우아한형제들") - skills { - soft("A passion for problem solving") - soft("Good communication skills") - hard("Kotlin") - } - languages { - "Korean" level 5 - "English" level 3 - } - } - assertThat(person.name).isEqualTo("로피") - assertThat(person.company).isEqualTo("우아한형제들") - assertThat(person.skills.soft.size).isEqualTo(2) - assertThat(person.skills.hard.size).isEqualTo(1) - assertThat(person.languages.korean).isEqualTo(5) - assertThat(person.languages.english).isEqualTo(3) - } -} diff --git a/src/test/kotlin/study/DSLTest.kt b/src/test/kotlin/study/DSLTest.kt new file mode 100644 index 000000000..ec9380348 --- /dev/null +++ b/src/test/kotlin/study/DSLTest.kt @@ -0,0 +1,97 @@ +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll + +class PersonBuilder { + private lateinit var name: String + private var company: String? = null + private lateinit var skills: Skills + private lateinit var languages: Languages + + fun name(block: () -> String) { name = block() } + + fun company(block: () -> String) { company = block() } + + fun skills(block: SkillsBuilder.() -> Unit) { + skills = SkillsBuilder().apply(block).build() + } + + fun languages(block: LanguagesBuilder.() -> Unit) { + languages = LanguagesBuilder().apply(block).build() + } + + fun build(): Person = Person(name, company, skills, languages) +} + +class SkillsBuilder { + private val soft = MutableList(0) { "" } + private val hard = MutableList(0) { "" } + fun soft(block: () -> String) = soft.add(block()) + + fun hard(block: () -> String) = hard.add(block()) + + fun build(): Skills = Skills(soft, hard) +} + +class LanguagesBuilder { + private val languages: MutableMap = mutableMapOf() + + fun build(): Languages = Languages(languages.toMap()) + + infix fun String.level(grade: Int) { + languages[this] = grade + } +} + +fun person(block: PersonBuilder.() -> Unit): Person { + val personBuilder = PersonBuilder().apply(block) + return personBuilder.build() +} + +data class Person( + var name: String, + var company: String?, + var skills: Skills, + var languages: Languages, +) + +data class Skills( + val soft: List, + val hard: List, +) + +data class Languages( + private val languages: Map, +) { + operator fun get(key: String) = languages[key] + fun size() = languages.size +} + +class DSLTest { + @Test + fun company() { + val person = person { + name { "아크" } + company { "우아한형제들" } + skills { + soft { "A passion for problem solving" } + soft { "Good communication skills" } + hard { "Kotlin" } + } + languages { + "Korean" level 5 + "English" level 3 + } + } + assertAll( + { assertThat(person.name).isEqualTo("아크") }, + { assertThat(person.company).isEqualTo("우아한형제들") }, + { assertThat(person.skills.soft[0]).isEqualTo("A passion for problem solving") }, + { assertThat(person.skills.soft[1]).isEqualTo("Good communication skills") }, + { assertThat(person.skills.hard[0]).isEqualTo("Kotlin") }, + { assertThat(person.languages["Korean"]).isEqualTo(5) }, + { assertThat(person.languages["English"]).isEqualTo(3) }, + { assertThat(person.languages.size()).isEqualTo(2) }, + ) + } +} diff --git a/src/test/kotlin/study/LambdaTest1.kt b/src/test/kotlin/study/LambdaTest1.kt new file mode 100644 index 000000000..e5eb0d286 --- /dev/null +++ b/src/test/kotlin/study/LambdaTest1.kt @@ -0,0 +1,31 @@ +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class LambdaTest1 { + @Test + fun `이동`() { + val car = Car("아크", 0) + val actual: Car = car.move { true } + assertThat(actual).isEqualTo(Car("아크", 1)) + } + + @Test + fun `정지`() { + val car = Car("아크", 0) + val actual: Car = car.move { false } + assertThat(actual).isEqualTo(Car("아크", 0)) + } +} + +data class Car(val name: String, val position: Int) { + fun move(moveStrategy: MoveStrategy): Car { + if (moveStrategy.isMovable()) { + return copy(position = position + 1) + } + return this + } +} + +fun interface MoveStrategy { + fun isMovable(): Boolean +} diff --git a/src/test/kotlin/study/LambdaTest2.kt b/src/test/kotlin/study/LambdaTest2.kt new file mode 100644 index 000000000..4cf615836 --- /dev/null +++ b/src/test/kotlin/study/LambdaTest2.kt @@ -0,0 +1,37 @@ +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +val numbers: List = listOf(1, 2, 3, 4, 5, 6) + +class LambdaTest2 { + @Test + fun sumAll() { + assertThat(sumWith(numbers) { true }) + .isEqualTo(21) + } + + @Test + fun sumAllEven() { + assertThat(sumWith(numbers) { it % 2 == 0 }) + .isEqualTo(12) + } + + @Test + fun sumAllOverThree() { + assertThat(sumWith(numbers) { it > 3 }) + .isEqualTo(15) + } +} + +fun interface Condition { + operator fun invoke(int: Int): Boolean +} +fun sumWith(numbers: List, condition: Condition): Int { + var total = 0 + for (number in numbers) { + if (condition(number)) { + total += number + } + } + return total +} From da1b57db8f0fc15a5996eddb00844e0f63506995 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Sun, 5 Mar 2023 17:04:51 +0900 Subject: [PATCH 56/67] =?UTF-8?q?style=20(BlackJack)=20:=20=EB=9D=84?= =?UTF-8?q?=EC=96=B4=EC=93=B0=EA=B8=B0=20=EB=B6=80=EB=B6=84=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.kt | 1 - .../kotlin/blackjack/domain/BlackJackBuilder.kt | 8 +++----- src/main/kotlin/blackjack/domain/Cards.kt | 10 ++++------ src/main/kotlin/blackjack/domain/Outcome.kt | 1 + .../kotlin/blackjack/domain/ParticipantsBuilder.kt | 14 ++++---------- src/main/kotlin/blackjack/domain/User.kt | 8 +++----- .../blackjack/domain/BlackJackBuilderTest.kt | 2 +- .../kotlin/blackjack/domain/BlackJackGameTest.kt | 3 ++- src/test/kotlin/blackjack/domain/CardDeckTest.kt | 1 - src/test/kotlin/blackjack/domain/CardValueTest.kt | 1 - src/test/kotlin/blackjack/domain/NameTest.kt | 1 - 11 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 80851909b..0b334a5b9 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -1,7 +1,6 @@ package blackjack.controller import blackjack.domain.BlackJack -import blackjack.domain.BlackJackBuilder import blackjack.domain.BlackJackGame import blackjack.domain.Cards import blackjack.domain.introduce diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index c0f87545e..87de59af4 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -16,11 +16,9 @@ class BlackJackBuilder { participants = ParticipantsBuilder().apply(block).build() } - fun draw() { - participants.all().map { - it.draw(cardDeck.nextCard()) - it.draw(cardDeck.nextCard()) - } + fun draw() = participants.all().map { + it.draw(cardDeck.nextCard()) + it.draw(cardDeck.nextCard()) } fun build(): BlackJack { diff --git a/src/main/kotlin/blackjack/domain/Cards.kt b/src/main/kotlin/blackjack/domain/Cards.kt index 6ee0c99b6..985e45985 100644 --- a/src/main/kotlin/blackjack/domain/Cards.kt +++ b/src/main/kotlin/blackjack/domain/Cards.kt @@ -11,12 +11,10 @@ class Cards(private val cards: MutableSet = mutableSetOf()) { fun containsACE() = cards.map { it.value }.contains(CardValue.ACE) companion object { - private val CARDS: List = - CardMark.values().map { mark -> - CardValue.values().map { value -> - Card(mark, value) - } - }.flatten() + private val CARDS: List = CardMark.values().flatMap { mark -> + CardValue.values().map { value -> Card(mark, value) } + } + fun all(): List = CARDS } } diff --git a/src/main/kotlin/blackjack/domain/Outcome.kt b/src/main/kotlin/blackjack/domain/Outcome.kt index fadc41b45..64119690c 100644 --- a/src/main/kotlin/blackjack/domain/Outcome.kt +++ b/src/main/kotlin/blackjack/domain/Outcome.kt @@ -6,6 +6,7 @@ enum class Outcome { companion object { private const val BLACKJACK_NUMBER = 21 + fun User.winTo(other: User): Outcome = when { other.score > BLACKJACK_NUMBER && this.score > BLACKJACK_NUMBER -> DRAW diff --git a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt index 56f86b05c..b8cc236fe 100644 --- a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt +++ b/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt @@ -1,18 +1,12 @@ package blackjack.domain class ParticipantsBuilder { - private lateinit var dealer: Dealer private lateinit var guests: List - fun dealer() { - dealer = Dealer() - } - fun guests(names: List) { - guests = names.map { Guest(it) } - } + fun dealer() { dealer = Dealer() } + + fun guests(names: List) { guests = names.map { Guest(it) } } - fun build(): Participants { - return Participants(dealer, guests) - } + fun build(): Participants = Participants(dealer, guests) } diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 518661d16..94e0ca75e 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -6,14 +6,12 @@ abstract class User(name: String) { val score: Int get() = Score(cards).score - abstract val isContinue: Boolean - val isBlackJack: Boolean get() = Score(cards).score == BLACKJACK_NUMBER - fun draw(card: Card) { - cards.add(card) - } + abstract val isContinue: Boolean + + fun draw(card: Card) { cards.add(card) } companion object { private const val BLACKJACK_NUMBER = 21 diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt index 10591847a..3a2d0b8e2 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt @@ -7,7 +7,7 @@ import org.junit.jupiter.api.assertAll class BlackJackBuilderTest { @Test fun `세팅이 된다`() { - val blackJack = BlackJackBuilder.init { + val blackJack = introduce { cardDeck(Cards.all()) participants { dealer() diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index 173c59d23..9afb87790 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -6,7 +6,7 @@ import org.junit.jupiter.api.assertDoesNotThrow class BlackJackGameTest { @Test fun `게임을 실행한다`() { - val blackJack = BlackJackBuilder.init { + val blackJack = introduce { cardDeck(Cards.all()) participants { dealer() @@ -29,5 +29,6 @@ class BlackJackGameTest { } private fun outputCard(user: User) = null + private fun outputDealer() = null } diff --git a/src/test/kotlin/blackjack/domain/CardDeckTest.kt b/src/test/kotlin/blackjack/domain/CardDeckTest.kt index f52b89693..34ab42138 100644 --- a/src/test/kotlin/blackjack/domain/CardDeckTest.kt +++ b/src/test/kotlin/blackjack/domain/CardDeckTest.kt @@ -5,7 +5,6 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows class CardDeckTest { - @Test fun `초기 카드 덱은 52장이다`() { val cardDeck = CardDeck(Cards.all()) diff --git a/src/test/kotlin/blackjack/domain/CardValueTest.kt b/src/test/kotlin/blackjack/domain/CardValueTest.kt index 544c4e222..40b97dbd5 100644 --- a/src/test/kotlin/blackjack/domain/CardValueTest.kt +++ b/src/test/kotlin/blackjack/domain/CardValueTest.kt @@ -5,7 +5,6 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.CsvSource class CardValueTest { - @ParameterizedTest @CsvSource(value = ["ACE,1", "KING,10", "QUEEN,10", "JACK,10", "TEN,10", "NINE,9", "EIGHT,8", "SEVEN,7", "SIX,6", "FIVE,5", "FOUR,4", "THREE,3", "TWO,2"]) fun `카드의 값들이 나온다`(cardValue: CardValue, value: Int) { diff --git a/src/test/kotlin/blackjack/domain/NameTest.kt b/src/test/kotlin/blackjack/domain/NameTest.kt index 279a4f753..ce399c4cc 100644 --- a/src/test/kotlin/blackjack/domain/NameTest.kt +++ b/src/test/kotlin/blackjack/domain/NameTest.kt @@ -6,7 +6,6 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource class NameTest { - @ParameterizedTest @ValueSource(strings = ["아", "아크,로피,아크,로피,아크,로피,아크"]) fun `이름은 1~20자 이하다`(name: String) { From b1dfd38107d34dbb0de2ba6215f13d20af361bad Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:42:28 +0900 Subject: [PATCH 57/67] =?UTF-8?q?feat=20(BlackJack):=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=ED=8D=BC=ED=8B=B0=EB=8A=94=20=EC=83=81=ED=83=9C=EB=A7=8C=20?= =?UTF-8?q?=EA=B0=80=EC=A7=80=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJack.kt | 5 ++--- src/main/kotlin/blackjack/view/OutputView.kt | 4 ++-- src/test/kotlin/blackjack/domain/BlackJackTest.kt | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJack.kt b/src/main/kotlin/blackjack/domain/BlackJack.kt index 0423659a0..6110424e7 100644 --- a/src/main/kotlin/blackjack/domain/BlackJack.kt +++ b/src/main/kotlin/blackjack/domain/BlackJack.kt @@ -6,12 +6,11 @@ data class BlackJack( val cardDeck: CardDeck, val participants: Participants, ) { - val result: List - get() = participants.guests.map { guest -> guest.winTo(participants.dealer) } - val dealer: Dealer get() = participants.dealer val guests: List get() = participants.guests + + fun getResult(): List = participants.guests.map { guest -> guest.winTo(participants.dealer) } } diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index 445b2064a..c6508a964 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -45,8 +45,8 @@ class OutputView { private fun outputOutcomes(blackJack: BlackJack) { blackJack.run { println("\n## 최종 승패") - println("${dealer.name}: ${result.count { it == Outcome.LOSE }}승 ${result.count { it == Outcome.WIN }}패") - guests.forEachIndexed { index, user -> outputOutcome(user, result[index]) } + println("${dealer.name}: ${getResult().count { it == Outcome.LOSE }}승 ${getResult().count { it == Outcome.WIN }}패") + guests.forEachIndexed { index, user -> outputOutcome(user, getResult()[index]) } } } diff --git a/src/test/kotlin/blackjack/domain/BlackJackTest.kt b/src/test/kotlin/blackjack/domain/BlackJackTest.kt index 70563a48c..076a7f823 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackTest.kt @@ -17,6 +17,6 @@ class BlackJackTest { guests[1].draw(Card(CardMark.CLOVER, CardValue.ACE)) guests[1].draw(Card(CardMark.CLOVER, CardValue.QUEEN)) } - assertThat(blackJack.result).isEqualTo(listOf(Outcome.LOSE, Outcome.WIN)) + assertThat(blackJack.getResult()).isEqualTo(listOf(Outcome.LOSE, Outcome.WIN)) } } From 9cb6249a85d40b8aa86489992d09dab4ce8d034d Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:45:06 +0900 Subject: [PATCH 58/67] =?UTF-8?q?feat=20(Cards):=20Cards=20=EB=B6=88?= =?UTF-8?q?=EB=B3=80=EA=B0=9D=EC=B2=B4=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/Cards.kt | 4 ++-- src/main/kotlin/blackjack/domain/User.kt | 4 ++-- src/test/kotlin/blackjack/domain/CardsTest.kt | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/Cards.kt b/src/main/kotlin/blackjack/domain/Cards.kt index 985e45985..96127af54 100644 --- a/src/main/kotlin/blackjack/domain/Cards.kt +++ b/src/main/kotlin/blackjack/domain/Cards.kt @@ -1,12 +1,12 @@ package blackjack.domain -class Cards(private val cards: MutableSet = mutableSetOf()) { +class Cards(private val cards: Set = setOf()) { val size: Int get() = cards.size fun toList() = cards.toList() - fun add(card: Card) = cards.add(card) + operator fun plus(card: Card): Cards = Cards(cards.plus(card)) fun containsACE() = cards.map { it.value }.contains(CardValue.ACE) diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/User.kt index 94e0ca75e..87caf92f7 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/User.kt @@ -2,7 +2,7 @@ package blackjack.domain abstract class User(name: String) { val name = Name(name) - val cards = Cards() + var cards = Cards() val score: Int get() = Score(cards).score @@ -11,7 +11,7 @@ abstract class User(name: String) { abstract val isContinue: Boolean - fun draw(card: Card) { cards.add(card) } + fun draw(card: Card) { cards += card } companion object { private const val BLACKJACK_NUMBER = 21 diff --git a/src/test/kotlin/blackjack/domain/CardsTest.kt b/src/test/kotlin/blackjack/domain/CardsTest.kt index 3864433bc..dedc1ad81 100644 --- a/src/test/kotlin/blackjack/domain/CardsTest.kt +++ b/src/test/kotlin/blackjack/domain/CardsTest.kt @@ -9,18 +9,18 @@ import org.junit.jupiter.api.Test class CardsTest { @Test fun `카드를 추가할 수 있다`() { - val cards = Cards() - cards.add(Card(CLOVER, EIGHT)) + var cards = Cards() + cards += Card(CLOVER, EIGHT) assertThat(cards.size).isEqualTo(1) assertThat(cards.toList()[0]).isEqualTo(Card(CLOVER, EIGHT)) } @Test fun `ACE 카드가 있는지 확인 할 수 있다`() { - val cards = Cards() - cards.add(Card(CLOVER, EIGHT)) + var cards = Cards() + cards += Card(CLOVER, EIGHT) assertThat(cards.containsACE()).isFalse - cards.add(Card(CLOVER, ACE)) + cards += Card(CLOVER, ACE) assertThat(cards.containsACE()).isTrue } } From 1e8a542134a66172d1e9c8996562b3efc241999f Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:45:58 +0900 Subject: [PATCH 59/67] =?UTF-8?q?feat=20(Cards):=20=EC=9A=A9=EB=8F=84?= =?UTF-8?q?=EC=97=90=20=EB=A7=9E=EB=8A=94=20=ED=95=A8=EC=88=98=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/controller/BlackJackController.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 0b334a5b9..7115617c1 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -12,13 +12,13 @@ class BlackJackController( private val outputView: OutputView, ) { fun run() { - val blackJack = setBlackJack() + val blackJack = setUpBlackJack() outputView.outputInitState(blackJack) startBlackJack(blackJack) outputView.outputResult(blackJack) } - private fun setBlackJack(): BlackJack = introduce { + private fun setUpBlackJack(): BlackJack = introduce { cardDeck(Cards.all()) participants { dealer() From fefb027906bfa88d179c29d211f70d22eec1a5c8 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:46:44 +0900 Subject: [PATCH 60/67] =?UTF-8?q?feat=20(BlackJackBuilder):=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=EC=9D=B4=20=ED=95=84=EC=9A=94=EC=97=86=EB=8A=94=20?= =?UTF-8?q?=EC=BB=AC=EB=A0=89=EC=85=98=20forEach=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackBuilder.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index 87de59af4..bff7ba02f 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -16,7 +16,7 @@ class BlackJackBuilder { participants = ParticipantsBuilder().apply(block).build() } - fun draw() = participants.all().map { + fun draw() = participants.all().forEach { it.draw(cardDeck.nextCard()) it.draw(cardDeck.nextCard()) } From 8da9717279aa9cf8dad4aa2f29e0180866bec233 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:47:29 +0900 Subject: [PATCH 61/67] =?UTF-8?q?feat=20(BlackJackBuilder):=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=ED=95=A8=EC=88=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/controller/BlackJackController.kt | 4 ++-- src/main/kotlin/blackjack/domain/BlackJackBuilder.kt | 2 +- src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt | 2 +- src/test/kotlin/blackjack/domain/BlackJackGameTest.kt | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 7115617c1..ccbc6f838 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -3,7 +3,7 @@ package blackjack.controller import blackjack.domain.BlackJack import blackjack.domain.BlackJackGame import blackjack.domain.Cards -import blackjack.domain.introduce +import blackjack.domain.blackJack import blackjack.view.InputView import blackjack.view.OutputView @@ -18,7 +18,7 @@ class BlackJackController( outputView.outputResult(blackJack) } - private fun setUpBlackJack(): BlackJack = introduce { + private fun setUpBlackJack(): BlackJack = blackJack { cardDeck(Cards.all()) participants { dealer() diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index bff7ba02f..d0108483a 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -1,6 +1,6 @@ package blackjack.domain -fun introduce(block: BlackJackBuilder.() -> Unit): BlackJack { +fun blackJack(block: BlackJackBuilder.() -> Unit): BlackJack { return BlackJackBuilder().apply(block).build() } diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt index 3a2d0b8e2..9fdd2fb6c 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt @@ -7,7 +7,7 @@ import org.junit.jupiter.api.assertAll class BlackJackBuilderTest { @Test fun `세팅이 된다`() { - val blackJack = introduce { + val blackJack = blackJack { cardDeck(Cards.all()) participants { dealer() diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index 9afb87790..e5574d459 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -6,7 +6,7 @@ import org.junit.jupiter.api.assertDoesNotThrow class BlackJackGameTest { @Test fun `게임을 실행한다`() { - val blackJack = introduce { + val blackJack = blackJack { cardDeck(Cards.all()) participants { dealer() From 93d72d042c33eaa6213d709ba61fdeff4b7d8f25 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:48:37 +0900 Subject: [PATCH 62/67] =?UTF-8?q?feat=20(CardDeck):=20=EB=B0=96=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=85=94=ED=94=8C=ED=95=9C=20=ED=9B=84=EC=97=90=20?= =?UTF-8?q?=EB=93=A4=EC=96=B4=EC=98=A4=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/BlackJackBuilder.kt | 5 +---- src/main/kotlin/blackjack/domain/CardDeck.kt | 3 --- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt index d0108483a..f5273b3ba 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt @@ -7,10 +7,7 @@ fun blackJack(block: BlackJackBuilder.() -> Unit): BlackJack { class BlackJackBuilder { private lateinit var cardDeck: CardDeck private lateinit var participants: Participants - fun cardDeck(cards: List) { - cardDeck = CardDeck(cards) - cardDeck.shuffle() - } + fun cardDeck(cards: List) { cardDeck = CardDeck(cards.shuffled()) } fun participants(block: ParticipantsBuilder.() -> Unit) { participants = ParticipantsBuilder().apply(block).build() diff --git a/src/main/kotlin/blackjack/domain/CardDeck.kt b/src/main/kotlin/blackjack/domain/CardDeck.kt index 8199404ac..a05cd603c 100644 --- a/src/main/kotlin/blackjack/domain/CardDeck.kt +++ b/src/main/kotlin/blackjack/domain/CardDeck.kt @@ -12,9 +12,6 @@ class CardDeck(cards: List) { get() = cards.size fun nextCard(): Card = cards.removeFirst() - - fun shuffle() = cards.shuffle() - companion object { private const val CARDS_SIZE = 52 private const val ERROR_INVALID_CARDS_SIZE = "카드덱 초기 사이즈는 ${CARDS_SIZE}장이어야 합니다." From 564f9556983911e69233eff96bd8c89e88ed1de6 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:57:13 +0900 Subject: [PATCH 63/67] =?UTF-8?q?feat=20(NameTest):=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8B=90=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EA=B2=BD=EA=B3=84?= =?UTF-8?q?=EA=B0=92=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/blackjack/domain/NameTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/blackjack/domain/NameTest.kt b/src/test/kotlin/blackjack/domain/NameTest.kt index ce399c4cc..1be488108 100644 --- a/src/test/kotlin/blackjack/domain/NameTest.kt +++ b/src/test/kotlin/blackjack/domain/NameTest.kt @@ -7,7 +7,7 @@ import org.junit.jupiter.params.provider.ValueSource class NameTest { @ParameterizedTest - @ValueSource(strings = ["아", "아크,로피,아크,로피,아크,로피,아크"]) + @ValueSource(strings = ["아", "01234567890123456789"]) fun `이름은 1~20자 이하다`(name: String) { assertDoesNotThrow { Name(name) @@ -15,7 +15,7 @@ class NameTest { } @ParameterizedTest - @ValueSource(strings = ["", "가나다라마바사아자차카타파하abcdefjhijklmnopqrstuvwxyz"]) + @ValueSource(strings = ["", "012345678901234567890"]) fun `이름은 1~20자 이외면 에러난다`(name: String) { assertThrows { Name(name) From f374a5f97a309783294a910f318e93c449d3ea8a Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 14:05:47 +0900 Subject: [PATCH 64/67] =?UTF-8?q?feat=20(BlackJackGame):=20=EB=A9=A4?= =?UTF-8?q?=EB=B2=84=20=ED=99=95=EC=9E=A5=ED=95=A8=EC=88=98=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackJackController.kt | 6 ++--- .../kotlin/blackjack/domain/BlackJackGame.kt | 25 ++++++++----------- .../blackjack/domain/BlackJackGameTest.kt | 6 ++--- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index ccbc6f838..4f72d2487 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -29,8 +29,8 @@ class BlackJackController( private fun startBlackJack(blackJack: BlackJack) = BlackJackGame().apply { - input(inputView::inputDrawMore) - blackJack.guestsTurn(outputView::outputCard) - blackJack.dealerTurn(outputView::outputDealerDraw) + getCommand = inputView::inputDrawMore + guestsTurn(blackJack.guests, blackJack.cardDeck, outputView::outputCard) + dealerTurn(blackJack.dealer, blackJack.cardDeck, outputView::outputDealerDraw) } } diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/BlackJackGame.kt index ac273af8c..119e14b70 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/BlackJackGame.kt @@ -1,14 +1,12 @@ package blackjack.domain class BlackJackGame { - private lateinit var input: (String) -> String + lateinit var getCommand: (String) -> String - fun input(func: (String) -> String) { input = func } + fun guestsTurn(guests: List, cardDeck: CardDeck, output: (User) -> Unit) = + guests.forEach { guest -> guestTurn(guest, cardDeck, output) } - fun BlackJack.guestsTurn(output: (User) -> Unit) = - guests.forEach { guest -> guestTurn(guest, output) } - - fun BlackJack.dealerTurn(output: () -> Unit) { + fun dealerTurn(dealer: Dealer, cardDeck: CardDeck, output: () -> Unit) { if (dealer.isBlackJack) return if (dealer.isContinue) { dealer.draw(cardDeck.nextCard()) @@ -16,27 +14,24 @@ class BlackJackGame { } } - private fun BlackJack.guestTurn(guest: Guest, output: (User) -> Unit) { + private fun guestTurn(guest: Guest, cardDeck: CardDeck, output: (User) -> Unit) { if (guest.isBlackJack) return - when (getCommand(guest.name)) { - in DRAW_COMMANDS -> draw(guest, output) + when (getCommand(guest.name.toString())) { + in DRAW_COMMANDS -> draw(guest, cardDeck, output) in END_TURN_COMMANDS -> output(guest) - else -> this.guestTurn(guest, output) + else -> this.guestTurn(guest, cardDeck, output) } } - private fun BlackJack.draw(guest: Guest, output: (User) -> Unit) { + private fun draw(guest: Guest, cardDeck: CardDeck, output: (User) -> Unit) { guest.draw(cardDeck.nextCard()) output(guest) if (guest.isContinue) { - guestTurn(guest, output) + guestTurn(guest, cardDeck, output) } } - private fun getCommand(name: Name) = input(name.toString()) - companion object { - const val BLACKJACK_NUMBER = 21 private val DRAW_COMMANDS = listOf("Y", "y") private val END_TURN_COMMANDS = listOf("N", "n") } diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt index e5574d459..895b5fcc7 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt @@ -17,9 +17,9 @@ class BlackJackGameTest { assertDoesNotThrow { BlackJackGame().apply { - input(::inputDrawMore) - blackJack.guestsTurn(::outputCard) - blackJack.dealerTurn(::outputDealer) + getCommand = ::inputDrawMore + guestsTurn(blackJack.guests, blackJack.cardDeck, ::outputCard) + dealerTurn(blackJack.dealer, blackJack.cardDeck, ::outputDealer) } } } From 95b5e7a5421ee9b9d5566ed8657bc65e7c52513b Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 14:07:59 +0900 Subject: [PATCH 65/67] =?UTF-8?q?feat=20(Participants):=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=EC=9D=98=20=EB=B0=98=ED=99=98=ED=83=80=EC=9E=85?= =?UTF-8?q?=EC=9D=84=20=EB=AA=85=EC=8B=9C=EC=A0=81=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/Participants.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/domain/Participants.kt b/src/main/kotlin/blackjack/domain/Participants.kt index a8ac7ce61..167119490 100644 --- a/src/main/kotlin/blackjack/domain/Participants.kt +++ b/src/main/kotlin/blackjack/domain/Participants.kt @@ -4,5 +4,5 @@ data class Participants( val dealer: Dealer, val guests: List, ) { - fun all() = listOf(dealer) + guests + fun all(): List = listOf(dealer) + guests } From e485867cbbc1372c253c9a66e1b966d46f7d2db1 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 20:30:46 +0900 Subject: [PATCH 66/67] =?UTF-8?q?refactor=20(Blackjack):=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=EB=B3=84=EB=A1=9C=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackJackController.kt | 8 ++++---- .../blackjack/domain/{ => blackjack}/BlackJack.kt | 9 +++++++-- .../domain/{ => blackjack}/BlackJackBuilder.kt | 11 +++++++---- .../blackjack/domain/{ => blackjack}/BlackJackGame.kt | 7 ++++++- src/main/kotlin/blackjack/domain/{ => card}/Card.kt | 2 +- .../kotlin/blackjack/domain/{ => card}/CardDeck.kt | 2 +- .../kotlin/blackjack/domain/{ => card}/CardMark.kt | 2 +- .../kotlin/blackjack/domain/{ => card}/CardValue.kt | 2 +- src/main/kotlin/blackjack/domain/{ => card}/Cards.kt | 2 +- .../blackjack/domain/{ => participants}/Dealer.kt | 2 +- .../blackjack/domain/{ => participants}/Guest.kt | 2 +- .../blackjack/domain/{ => participants}/Name.kt | 2 +- .../domain/{ => participants}/Participants.kt | 2 +- .../domain/{ => participants}/ParticipantsBuilder.kt | 2 +- .../blackjack/domain/{ => participants}/Score.kt | 4 +++- .../blackjack/domain/{ => participants}/User.kt | 5 ++++- .../kotlin/blackjack/domain/{ => result}/Outcome.kt | 3 ++- src/main/kotlin/blackjack/view/OutputView.kt | 10 +++++----- .../domain/{ => blackjack}/BlackJackBuilderTest.kt | 3 ++- .../domain/{ => blackjack}/BlackJackGameTest.kt | 4 +++- .../blackjack/domain/{ => blackjack}/BlackJackTest.kt | 11 ++++++++++- .../blackjack/domain/{ => card}/CardDeckTest.kt | 2 +- .../kotlin/blackjack/domain/{ => card}/CardTest.kt | 2 +- .../blackjack/domain/{ => card}/CardValueTest.kt | 2 +- .../kotlin/blackjack/domain/{ => card}/CardsTest.kt | 8 ++++---- .../blackjack/domain/{ => participants}/NameTest.kt | 2 +- .../blackjack/domain/{ => participants}/UserTest.kt | 5 ++++- .../blackjack/domain/{ => result}/OutcomeTest.kt | 11 ++++++++--- 28 files changed, 83 insertions(+), 44 deletions(-) rename src/main/kotlin/blackjack/domain/{ => blackjack}/BlackJack.kt (51%) rename src/main/kotlin/blackjack/domain/{ => blackjack}/BlackJackBuilder.kt (67%) rename src/main/kotlin/blackjack/domain/{ => blackjack}/BlackJackGame.kt (85%) rename src/main/kotlin/blackjack/domain/{ => card}/Card.kt (66%) rename src/main/kotlin/blackjack/domain/{ => card}/CardDeck.kt (95%) rename src/main/kotlin/blackjack/domain/{ => card}/CardMark.kt (69%) rename src/main/kotlin/blackjack/domain/{ => card}/CardValue.kt (87%) rename src/main/kotlin/blackjack/domain/{ => card}/Cards.kt (94%) rename src/main/kotlin/blackjack/domain/{ => participants}/Dealer.kt (85%) rename src/main/kotlin/blackjack/domain/{ => participants}/Guest.kt (88%) rename src/main/kotlin/blackjack/domain/{ => participants}/Name.kt (92%) rename src/main/kotlin/blackjack/domain/{ => participants}/Participants.kt (78%) rename src/main/kotlin/blackjack/domain/{ => participants}/ParticipantsBuilder.kt (88%) rename src/main/kotlin/blackjack/domain/{ => participants}/Score.kt (89%) rename src/main/kotlin/blackjack/domain/{ => participants}/User.kt (78%) rename src/main/kotlin/blackjack/domain/{ => result}/Outcome.kt (89%) rename src/test/kotlin/blackjack/domain/{ => blackjack}/BlackJackBuilderTest.kt (93%) rename src/test/kotlin/blackjack/domain/{ => blackjack}/BlackJackGameTest.kt (88%) rename src/test/kotlin/blackjack/domain/{ => blackjack}/BlackJackTest.kt (68%) rename src/test/kotlin/blackjack/domain/{ => card}/CardDeckTest.kt (96%) rename src/test/kotlin/blackjack/domain/{ => card}/CardTest.kt (94%) rename src/test/kotlin/blackjack/domain/{ => card}/CardValueTest.kt (94%) rename src/test/kotlin/blackjack/domain/{ => card}/CardsTest.kt (79%) rename src/test/kotlin/blackjack/domain/{ => participants}/NameTest.kt (94%) rename src/test/kotlin/blackjack/domain/{ => participants}/UserTest.kt (79%) rename src/test/kotlin/blackjack/domain/{ => result}/OutcomeTest.kt (92%) diff --git a/src/main/kotlin/blackjack/controller/BlackJackController.kt b/src/main/kotlin/blackjack/controller/BlackJackController.kt index 4f72d2487..7ed7bad13 100644 --- a/src/main/kotlin/blackjack/controller/BlackJackController.kt +++ b/src/main/kotlin/blackjack/controller/BlackJackController.kt @@ -1,9 +1,9 @@ package blackjack.controller -import blackjack.domain.BlackJack -import blackjack.domain.BlackJackGame -import blackjack.domain.Cards -import blackjack.domain.blackJack +import blackjack.domain.blackjack.BlackJack +import blackjack.domain.blackjack.BlackJackGame +import blackjack.domain.blackjack.blackJack +import blackjack.domain.card.Cards import blackjack.view.InputView import blackjack.view.OutputView diff --git a/src/main/kotlin/blackjack/domain/BlackJack.kt b/src/main/kotlin/blackjack/domain/blackjack/BlackJack.kt similarity index 51% rename from src/main/kotlin/blackjack/domain/BlackJack.kt rename to src/main/kotlin/blackjack/domain/blackjack/BlackJack.kt index 6110424e7..d8714de17 100644 --- a/src/main/kotlin/blackjack/domain/BlackJack.kt +++ b/src/main/kotlin/blackjack/domain/blackjack/BlackJack.kt @@ -1,6 +1,11 @@ -package blackjack.domain +package blackjack.domain.blackjack -import blackjack.domain.Outcome.Companion.winTo +import blackjack.domain.card.CardDeck +import blackjack.domain.participants.Dealer +import blackjack.domain.participants.Guest +import blackjack.domain.participants.Participants +import blackjack.domain.result.Outcome +import blackjack.domain.result.Outcome.Companion.winTo data class BlackJack( val cardDeck: CardDeck, diff --git a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt b/src/main/kotlin/blackjack/domain/blackjack/BlackJackBuilder.kt similarity index 67% rename from src/main/kotlin/blackjack/domain/BlackJackBuilder.kt rename to src/main/kotlin/blackjack/domain/blackjack/BlackJackBuilder.kt index f5273b3ba..40e039f99 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackBuilder.kt +++ b/src/main/kotlin/blackjack/domain/blackjack/BlackJackBuilder.kt @@ -1,4 +1,9 @@ -package blackjack.domain +package blackjack.domain.blackjack + +import blackjack.domain.card.Card +import blackjack.domain.card.CardDeck +import blackjack.domain.participants.Participants +import blackjack.domain.participants.ParticipantsBuilder fun blackJack(block: BlackJackBuilder.() -> Unit): BlackJack { return BlackJackBuilder().apply(block).build() @@ -18,7 +23,5 @@ class BlackJackBuilder { it.draw(cardDeck.nextCard()) } - fun build(): BlackJack { - return BlackJack(cardDeck, participants) - } + fun build(): BlackJack = BlackJack(cardDeck, participants) } diff --git a/src/main/kotlin/blackjack/domain/BlackJackGame.kt b/src/main/kotlin/blackjack/domain/blackjack/BlackJackGame.kt similarity index 85% rename from src/main/kotlin/blackjack/domain/BlackJackGame.kt rename to src/main/kotlin/blackjack/domain/blackjack/BlackJackGame.kt index 119e14b70..21acd1dd1 100644 --- a/src/main/kotlin/blackjack/domain/BlackJackGame.kt +++ b/src/main/kotlin/blackjack/domain/blackjack/BlackJackGame.kt @@ -1,4 +1,9 @@ -package blackjack.domain +package blackjack.domain.blackjack + +import blackjack.domain.card.CardDeck +import blackjack.domain.participants.Dealer +import blackjack.domain.participants.Guest +import blackjack.domain.participants.User class BlackJackGame { lateinit var getCommand: (String) -> String diff --git a/src/main/kotlin/blackjack/domain/Card.kt b/src/main/kotlin/blackjack/domain/card/Card.kt similarity index 66% rename from src/main/kotlin/blackjack/domain/Card.kt rename to src/main/kotlin/blackjack/domain/card/Card.kt index cb4ba7d16..506959af0 100644 --- a/src/main/kotlin/blackjack/domain/Card.kt +++ b/src/main/kotlin/blackjack/domain/card/Card.kt @@ -1,3 +1,3 @@ -package blackjack.domain +package blackjack.domain.card data class Card(val mark: CardMark, val value: CardValue) diff --git a/src/main/kotlin/blackjack/domain/CardDeck.kt b/src/main/kotlin/blackjack/domain/card/CardDeck.kt similarity index 95% rename from src/main/kotlin/blackjack/domain/CardDeck.kt rename to src/main/kotlin/blackjack/domain/card/CardDeck.kt index a05cd603c..7b8cce676 100644 --- a/src/main/kotlin/blackjack/domain/CardDeck.kt +++ b/src/main/kotlin/blackjack/domain/card/CardDeck.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.card class CardDeck(cards: List) { private val cards: MutableList = cards.toMutableList() diff --git a/src/main/kotlin/blackjack/domain/CardMark.kt b/src/main/kotlin/blackjack/domain/card/CardMark.kt similarity index 69% rename from src/main/kotlin/blackjack/domain/CardMark.kt rename to src/main/kotlin/blackjack/domain/card/CardMark.kt index fa6ba6931..6a3d1453c 100644 --- a/src/main/kotlin/blackjack/domain/CardMark.kt +++ b/src/main/kotlin/blackjack/domain/card/CardMark.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.card enum class CardMark { CLOVER, diff --git a/src/main/kotlin/blackjack/domain/CardValue.kt b/src/main/kotlin/blackjack/domain/card/CardValue.kt similarity index 87% rename from src/main/kotlin/blackjack/domain/CardValue.kt rename to src/main/kotlin/blackjack/domain/card/CardValue.kt index b4e80a095..a647a7a05 100644 --- a/src/main/kotlin/blackjack/domain/CardValue.kt +++ b/src/main/kotlin/blackjack/domain/card/CardValue.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.card enum class CardValue(val value: Int) { ACE(1), diff --git a/src/main/kotlin/blackjack/domain/Cards.kt b/src/main/kotlin/blackjack/domain/card/Cards.kt similarity index 94% rename from src/main/kotlin/blackjack/domain/Cards.kt rename to src/main/kotlin/blackjack/domain/card/Cards.kt index 96127af54..489104cbb 100644 --- a/src/main/kotlin/blackjack/domain/Cards.kt +++ b/src/main/kotlin/blackjack/domain/card/Cards.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.card class Cards(private val cards: Set = setOf()) { val size: Int diff --git a/src/main/kotlin/blackjack/domain/Dealer.kt b/src/main/kotlin/blackjack/domain/participants/Dealer.kt similarity index 85% rename from src/main/kotlin/blackjack/domain/Dealer.kt rename to src/main/kotlin/blackjack/domain/participants/Dealer.kt index d641dc174..b5e62bee5 100644 --- a/src/main/kotlin/blackjack/domain/Dealer.kt +++ b/src/main/kotlin/blackjack/domain/participants/Dealer.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.participants class Dealer(name: String = "딜러") : User(name) { override val isContinue: Boolean diff --git a/src/main/kotlin/blackjack/domain/Guest.kt b/src/main/kotlin/blackjack/domain/participants/Guest.kt similarity index 88% rename from src/main/kotlin/blackjack/domain/Guest.kt rename to src/main/kotlin/blackjack/domain/participants/Guest.kt index d9c916f0b..224e2819b 100644 --- a/src/main/kotlin/blackjack/domain/Guest.kt +++ b/src/main/kotlin/blackjack/domain/participants/Guest.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.participants class Guest(name: String) : User(name) { override val isContinue: Boolean diff --git a/src/main/kotlin/blackjack/domain/Name.kt b/src/main/kotlin/blackjack/domain/participants/Name.kt similarity index 92% rename from src/main/kotlin/blackjack/domain/Name.kt rename to src/main/kotlin/blackjack/domain/participants/Name.kt index fd95267dc..e4c8330f1 100644 --- a/src/main/kotlin/blackjack/domain/Name.kt +++ b/src/main/kotlin/blackjack/domain/participants/Name.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.participants @JvmInline value class Name(private val value: String) { diff --git a/src/main/kotlin/blackjack/domain/Participants.kt b/src/main/kotlin/blackjack/domain/participants/Participants.kt similarity index 78% rename from src/main/kotlin/blackjack/domain/Participants.kt rename to src/main/kotlin/blackjack/domain/participants/Participants.kt index 167119490..0de611a59 100644 --- a/src/main/kotlin/blackjack/domain/Participants.kt +++ b/src/main/kotlin/blackjack/domain/participants/Participants.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.participants data class Participants( val dealer: Dealer, diff --git a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt b/src/main/kotlin/blackjack/domain/participants/ParticipantsBuilder.kt similarity index 88% rename from src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt rename to src/main/kotlin/blackjack/domain/participants/ParticipantsBuilder.kt index b8cc236fe..5aaf59517 100644 --- a/src/main/kotlin/blackjack/domain/ParticipantsBuilder.kt +++ b/src/main/kotlin/blackjack/domain/participants/ParticipantsBuilder.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.participants class ParticipantsBuilder { private lateinit var dealer: Dealer diff --git a/src/main/kotlin/blackjack/domain/Score.kt b/src/main/kotlin/blackjack/domain/participants/Score.kt similarity index 89% rename from src/main/kotlin/blackjack/domain/Score.kt rename to src/main/kotlin/blackjack/domain/participants/Score.kt index 9e18c1a51..ea7ab8ee5 100644 --- a/src/main/kotlin/blackjack/domain/Score.kt +++ b/src/main/kotlin/blackjack/domain/participants/Score.kt @@ -1,4 +1,6 @@ -package blackjack.domain +package blackjack.domain.participants + +import blackjack.domain.card.Cards class Score(val cards: Cards) { val score: Int diff --git a/src/main/kotlin/blackjack/domain/User.kt b/src/main/kotlin/blackjack/domain/participants/User.kt similarity index 78% rename from src/main/kotlin/blackjack/domain/User.kt rename to src/main/kotlin/blackjack/domain/participants/User.kt index 87caf92f7..1dfe00062 100644 --- a/src/main/kotlin/blackjack/domain/User.kt +++ b/src/main/kotlin/blackjack/domain/participants/User.kt @@ -1,4 +1,7 @@ -package blackjack.domain +package blackjack.domain.participants + +import blackjack.domain.card.Card +import blackjack.domain.card.Cards abstract class User(name: String) { val name = Name(name) diff --git a/src/main/kotlin/blackjack/domain/Outcome.kt b/src/main/kotlin/blackjack/domain/result/Outcome.kt similarity index 89% rename from src/main/kotlin/blackjack/domain/Outcome.kt rename to src/main/kotlin/blackjack/domain/result/Outcome.kt index 64119690c..2a91eecc9 100644 --- a/src/main/kotlin/blackjack/domain/Outcome.kt +++ b/src/main/kotlin/blackjack/domain/result/Outcome.kt @@ -1,4 +1,5 @@ -package blackjack.domain +package blackjack.domain.result +import blackjack.domain.participants.User import java.lang.IllegalStateException enum class Outcome { diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index c6508a964..da55b3f69 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -1,10 +1,10 @@ package blackjack.view -import blackjack.domain.BlackJack -import blackjack.domain.CardMark -import blackjack.domain.CardValue -import blackjack.domain.Outcome -import blackjack.domain.User +import blackjack.domain.blackjack.BlackJack +import blackjack.domain.card.CardMark +import blackjack.domain.card.CardValue +import blackjack.domain.participants.User +import blackjack.domain.result.Outcome class OutputView { fun outputInitState(blackJack: BlackJack) { diff --git a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt b/src/test/kotlin/blackjack/domain/blackjack/BlackJackBuilderTest.kt similarity index 93% rename from src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt rename to src/test/kotlin/blackjack/domain/blackjack/BlackJackBuilderTest.kt index 9fdd2fb6c..156eaffa9 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackBuilderTest.kt +++ b/src/test/kotlin/blackjack/domain/blackjack/BlackJackBuilderTest.kt @@ -1,5 +1,6 @@ -package blackjack.domain +package blackjack.domain.blackjack +import blackjack.domain.card.Cards import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertAll diff --git a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt b/src/test/kotlin/blackjack/domain/blackjack/BlackJackGameTest.kt similarity index 88% rename from src/test/kotlin/blackjack/domain/BlackJackGameTest.kt rename to src/test/kotlin/blackjack/domain/blackjack/BlackJackGameTest.kt index 895b5fcc7..5540b1da0 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackGameTest.kt +++ b/src/test/kotlin/blackjack/domain/blackjack/BlackJackGameTest.kt @@ -1,5 +1,7 @@ -package blackjack.domain +package blackjack.domain.blackjack +import blackjack.domain.card.Cards +import blackjack.domain.participants.User import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow diff --git a/src/test/kotlin/blackjack/domain/BlackJackTest.kt b/src/test/kotlin/blackjack/domain/blackjack/BlackJackTest.kt similarity index 68% rename from src/test/kotlin/blackjack/domain/BlackJackTest.kt rename to src/test/kotlin/blackjack/domain/blackjack/BlackJackTest.kt index 076a7f823..153ab61d3 100644 --- a/src/test/kotlin/blackjack/domain/BlackJackTest.kt +++ b/src/test/kotlin/blackjack/domain/blackjack/BlackJackTest.kt @@ -1,5 +1,14 @@ -package blackjack.domain +package blackjack.domain.blackjack +import blackjack.domain.card.Card +import blackjack.domain.card.CardDeck +import blackjack.domain.card.CardMark +import blackjack.domain.card.CardValue +import blackjack.domain.card.Cards +import blackjack.domain.participants.Dealer +import blackjack.domain.participants.Guest +import blackjack.domain.participants.Participants +import blackjack.domain.result.Outcome import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test diff --git a/src/test/kotlin/blackjack/domain/CardDeckTest.kt b/src/test/kotlin/blackjack/domain/card/CardDeckTest.kt similarity index 96% rename from src/test/kotlin/blackjack/domain/CardDeckTest.kt rename to src/test/kotlin/blackjack/domain/card/CardDeckTest.kt index 34ab42138..d29c727fe 100644 --- a/src/test/kotlin/blackjack/domain/CardDeckTest.kt +++ b/src/test/kotlin/blackjack/domain/card/CardDeckTest.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.card import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test diff --git a/src/test/kotlin/blackjack/domain/CardTest.kt b/src/test/kotlin/blackjack/domain/card/CardTest.kt similarity index 94% rename from src/test/kotlin/blackjack/domain/CardTest.kt rename to src/test/kotlin/blackjack/domain/card/CardTest.kt index 6b547f2ce..2cb04a5a4 100644 --- a/src/test/kotlin/blackjack/domain/CardTest.kt +++ b/src/test/kotlin/blackjack/domain/card/CardTest.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.card import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.params.ParameterizedTest diff --git a/src/test/kotlin/blackjack/domain/CardValueTest.kt b/src/test/kotlin/blackjack/domain/card/CardValueTest.kt similarity index 94% rename from src/test/kotlin/blackjack/domain/CardValueTest.kt rename to src/test/kotlin/blackjack/domain/card/CardValueTest.kt index 40b97dbd5..7379f0e10 100644 --- a/src/test/kotlin/blackjack/domain/CardValueTest.kt +++ b/src/test/kotlin/blackjack/domain/card/CardValueTest.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.card import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.params.ParameterizedTest diff --git a/src/test/kotlin/blackjack/domain/CardsTest.kt b/src/test/kotlin/blackjack/domain/card/CardsTest.kt similarity index 79% rename from src/test/kotlin/blackjack/domain/CardsTest.kt rename to src/test/kotlin/blackjack/domain/card/CardsTest.kt index dedc1ad81..7b7a1f9c5 100644 --- a/src/test/kotlin/blackjack/domain/CardsTest.kt +++ b/src/test/kotlin/blackjack/domain/card/CardsTest.kt @@ -1,8 +1,8 @@ -package blackjack.domain +package blackjack.domain.card -import blackjack.domain.CardMark.CLOVER -import blackjack.domain.CardValue.ACE -import blackjack.domain.CardValue.EIGHT +import blackjack.domain.card.CardMark.CLOVER +import blackjack.domain.card.CardValue.ACE +import blackjack.domain.card.CardValue.EIGHT import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test diff --git a/src/test/kotlin/blackjack/domain/NameTest.kt b/src/test/kotlin/blackjack/domain/participants/NameTest.kt similarity index 94% rename from src/test/kotlin/blackjack/domain/NameTest.kt rename to src/test/kotlin/blackjack/domain/participants/NameTest.kt index 1be488108..862a3fc9f 100644 --- a/src/test/kotlin/blackjack/domain/NameTest.kt +++ b/src/test/kotlin/blackjack/domain/participants/NameTest.kt @@ -1,4 +1,4 @@ -package blackjack.domain +package blackjack.domain.participants import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.api.assertThrows diff --git a/src/test/kotlin/blackjack/domain/UserTest.kt b/src/test/kotlin/blackjack/domain/participants/UserTest.kt similarity index 79% rename from src/test/kotlin/blackjack/domain/UserTest.kt rename to src/test/kotlin/blackjack/domain/participants/UserTest.kt index 4772815b2..44ed52a89 100644 --- a/src/test/kotlin/blackjack/domain/UserTest.kt +++ b/src/test/kotlin/blackjack/domain/participants/UserTest.kt @@ -1,5 +1,8 @@ -package blackjack.domain +package blackjack.domain.participants +import blackjack.domain.card.Card +import blackjack.domain.card.CardMark +import blackjack.domain.card.CardValue import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test diff --git a/src/test/kotlin/blackjack/domain/OutcomeTest.kt b/src/test/kotlin/blackjack/domain/result/OutcomeTest.kt similarity index 92% rename from src/test/kotlin/blackjack/domain/OutcomeTest.kt rename to src/test/kotlin/blackjack/domain/result/OutcomeTest.kt index 80222e2b6..5c9141ce5 100644 --- a/src/test/kotlin/blackjack/domain/OutcomeTest.kt +++ b/src/test/kotlin/blackjack/domain/result/OutcomeTest.kt @@ -1,6 +1,11 @@ -package blackjack.domain - -import blackjack.domain.Outcome.Companion.winTo +package blackjack.domain.result + +import blackjack.domain.card.Card +import blackjack.domain.card.CardMark +import blackjack.domain.card.CardValue +import blackjack.domain.participants.Dealer +import blackjack.domain.participants.Guest +import blackjack.domain.result.Outcome.Companion.winTo import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test From e3844a189fbfc46eae0706458553cff7220763e1 Mon Sep 17 00:00:00 2001 From: re4rk <37167652+re4rk@users.noreply.github.com> Date: Mon, 6 Mar 2023 20:55:36 +0900 Subject: [PATCH 67/67] =?UTF-8?q?refactor=20(score):=20=EB=B6=88=EB=B3=80?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=EB=A1=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/domain/card/Cards.kt | 4 ++++ .../kotlin/blackjack/domain/participants/Dealer.kt | 2 +- .../kotlin/blackjack/domain/participants/Guest.kt | 2 +- .../kotlin/blackjack/domain/participants/Score.kt | 13 ++++++------- .../kotlin/blackjack/domain/participants/User.kt | 5 +++-- src/test/kotlin/blackjack/domain/card/CardsTest.kt | 8 ++++++++ 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/card/Cards.kt b/src/main/kotlin/blackjack/domain/card/Cards.kt index 489104cbb..1c25a6dbf 100644 --- a/src/main/kotlin/blackjack/domain/card/Cards.kt +++ b/src/main/kotlin/blackjack/domain/card/Cards.kt @@ -1,6 +1,10 @@ package blackjack.domain.card +import blackjack.domain.participants.Score + class Cards(private val cards: Set = setOf()) { + val result = Score(this) + val size: Int get() = cards.size diff --git a/src/main/kotlin/blackjack/domain/participants/Dealer.kt b/src/main/kotlin/blackjack/domain/participants/Dealer.kt index b5e62bee5..e16be08be 100644 --- a/src/main/kotlin/blackjack/domain/participants/Dealer.kt +++ b/src/main/kotlin/blackjack/domain/participants/Dealer.kt @@ -2,7 +2,7 @@ package blackjack.domain.participants class Dealer(name: String = "딜러") : User(name) { override val isContinue: Boolean - get() = Score(cards).maxScore < DEALER_MIN_NUMBER + get() = cards.result.maxScore < DEALER_MIN_NUMBER companion object { private const val DEALER_MIN_NUMBER = 17 diff --git a/src/main/kotlin/blackjack/domain/participants/Guest.kt b/src/main/kotlin/blackjack/domain/participants/Guest.kt index 224e2819b..1fb7222b0 100644 --- a/src/main/kotlin/blackjack/domain/participants/Guest.kt +++ b/src/main/kotlin/blackjack/domain/participants/Guest.kt @@ -5,7 +5,7 @@ class Guest(name: String) : User(name) { get() = isNotBust && isBlackJack.not() private val isNotBust: Boolean - get() = Score(cards).minScore <= BLACKJACK_NUMBER + get() = cards.result.minScore <= BLACKJACK_NUMBER companion object { private const val BLACKJACK_NUMBER = 21 diff --git a/src/main/kotlin/blackjack/domain/participants/Score.kt b/src/main/kotlin/blackjack/domain/participants/Score.kt index ea7ab8ee5..456f682d7 100644 --- a/src/main/kotlin/blackjack/domain/participants/Score.kt +++ b/src/main/kotlin/blackjack/domain/participants/Score.kt @@ -2,15 +2,14 @@ package blackjack.domain.participants import blackjack.domain.card.Cards -class Score(val cards: Cards) { - val score: Int - get() = if (isMaxScoreInRange) maxScore else minScore +class Score(cards: Cards) { - val minScore: Int - get() = cards.toList().sumOf { it.value.value } + val minScore: Int = cards.toList().sumOf { it.value.value } - val maxScore: Int - get() = minScore + if (cards.containsACE() && validateAceCondition) ACE_OTHER_NUMBER_DIFF else 0 + val maxScore: Int = + minScore + if (cards.containsACE() && validateAceCondition) ACE_OTHER_NUMBER_DIFF else 0 + + fun score(): Int = if (isMaxScoreInRange) maxScore else minScore private val validateAceCondition: Boolean get() = minScore + ACE_OTHER_NUMBER_DIFF <= BLACKJACK_NUMBER diff --git a/src/main/kotlin/blackjack/domain/participants/User.kt b/src/main/kotlin/blackjack/domain/participants/User.kt index 1dfe00062..a5a932d94 100644 --- a/src/main/kotlin/blackjack/domain/participants/User.kt +++ b/src/main/kotlin/blackjack/domain/participants/User.kt @@ -6,11 +6,12 @@ import blackjack.domain.card.Cards abstract class User(name: String) { val name = Name(name) var cards = Cards() + val score: Int - get() = Score(cards).score + get() = cards.result.score() val isBlackJack: Boolean - get() = Score(cards).score == BLACKJACK_NUMBER + get() = cards.result.score() == BLACKJACK_NUMBER abstract val isContinue: Boolean diff --git a/src/test/kotlin/blackjack/domain/card/CardsTest.kt b/src/test/kotlin/blackjack/domain/card/CardsTest.kt index 7b7a1f9c5..c8b918f76 100644 --- a/src/test/kotlin/blackjack/domain/card/CardsTest.kt +++ b/src/test/kotlin/blackjack/domain/card/CardsTest.kt @@ -23,4 +23,12 @@ class CardsTest { cards += Card(CLOVER, ACE) assertThat(cards.containsACE()).isTrue } + + @Test + fun `점수의 합을 반환한다`() { + var cards = Cards() + cards += Card(CLOVER, EIGHT) + cards += Card(CardMark.SPADE, CardValue.EIGHT) + assertThat(cards.result.score()).isEqualTo(16) + } }