From 165ad5aeac3164e99ac282d29f436ba389ae2490 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Thu, 2 Jan 2025 14:37:21 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20List=EB=A5=BC=20?= =?UTF-8?q?=EC=9D=BC=EA=B8=89=20=EC=BB=AC=EB=A0=89=EC=85=98=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=A7=8C=EB=93=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/BlackjackApplication.kt | 31 ++++++++----------- src/main/kotlin/blackjack/domain/Dealer.kt | 4 +++ .../kotlin/blackjack/domain/Participant.kt | 7 +++++ .../kotlin/blackjack/domain/Participants.kt | 10 ++++++ src/main/kotlin/blackjack/domain/Player.kt | 2 ++ src/main/kotlin/blackjack/view/OutputView.kt | 3 +- 6 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/Participants.kt diff --git a/src/main/kotlin/blackjack/BlackjackApplication.kt b/src/main/kotlin/blackjack/BlackjackApplication.kt index 492498569..f00333481 100644 --- a/src/main/kotlin/blackjack/BlackjackApplication.kt +++ b/src/main/kotlin/blackjack/BlackjackApplication.kt @@ -3,7 +3,7 @@ package blackjack import blackjack.domain.BlackjackResults import blackjack.domain.BlackjackShoe import blackjack.domain.Dealer -import blackjack.domain.Participant +import blackjack.domain.Participants import blackjack.view.InputView import blackjack.view.OutputView @@ -15,29 +15,24 @@ class BlackjackApplication( fun run() { val blackjackShoe = BlackjackShoe() val (dealer, participantList) = ready(blackjackShoe) - play(dealer = dealer, participantList = participantList, blackjackShoe = blackjackShoe) - finish(dealer = dealer, participantList = participantList) + play(dealer = dealer, participants = participantList, blackjackShoe = blackjackShoe) + finish(dealer = dealer, participants = participantList) } - private fun ready(blackjackShoe: BlackjackShoe): Pair> { - val participantNames = inputView.getParticipantNames() + private fun ready(blackjackShoe: BlackjackShoe): Pair { + val participantNames: List = inputView.getParticipantNames() val dealer = Dealer() - val participants = participantNames.map { name -> Participant(name.trim()) } - - dealer.receiveCard(blackjackShoe.draw()) - participants.forEach { participant -> - repeat(2) { - participant.receiveCard(blackjackShoe.draw()) - } - } + dealer.setupCard(blackjackShoe = blackjackShoe) + val participants = Participants(participantNames = participantNames.toTypedArray()) + participants.setupCard(blackjackShoe = blackjackShoe) outputView.showReady(dealer = dealer, participants = participants) return dealer to participants } - private fun play(dealer: Dealer, participantList: List, blackjackShoe: BlackjackShoe) { - participantList.forEach { participant -> + private fun play(dealer: Dealer, participants: Participants, blackjackShoe: BlackjackShoe) { + participants.forEach { participant -> while (participant.canReceiveCard && inputView.getMoreCard(participant)) { val card = blackjackShoe.draw() participant.receiveCard(card) @@ -52,10 +47,10 @@ class BlackjackApplication( } } - private fun finish(dealer: Dealer, participantList: List) { + private fun finish(dealer: Dealer, participants: Participants) { outputView.showDealerInfo(dealer = dealer) - outputView.showParticipantsInfo(participantList) - outputView.showResult(BlackjackResults(dealer = dealer, participants = participantList)) + outputView.showParticipantsInfo(participants) + outputView.showResult(BlackjackResults(dealer = dealer, participants = participants)) } } diff --git a/src/main/kotlin/blackjack/domain/Dealer.kt b/src/main/kotlin/blackjack/domain/Dealer.kt index 711b5defb..a48cf3d9e 100644 --- a/src/main/kotlin/blackjack/domain/Dealer.kt +++ b/src/main/kotlin/blackjack/domain/Dealer.kt @@ -7,6 +7,10 @@ data class Dealer( override val canReceiveCard: Boolean get() = score <= CAN_RECEIVE_CARD_SCORE + override fun setupCard(blackjackShoe: BlackjackShoe) { + receiveCard(blackjackShoe.draw()) + } + companion object { private const val DEFAULT_DEALER_NAME = "딜러" private const val CAN_RECEIVE_CARD_SCORE = 16 diff --git a/src/main/kotlin/blackjack/domain/Participant.kt b/src/main/kotlin/blackjack/domain/Participant.kt index 153182bb9..92e1052c0 100644 --- a/src/main/kotlin/blackjack/domain/Participant.kt +++ b/src/main/kotlin/blackjack/domain/Participant.kt @@ -8,7 +8,14 @@ data class Participant( return score <= CAN_RECEIVE_CARD_SCORE } + override fun setupCard(blackjackShoe: BlackjackShoe) { + repeat(NUMBER_OF_INIT_CARD) { + receiveCard(blackjackShoe.draw()) + } + } + companion object { private const val CAN_RECEIVE_CARD_SCORE = 21 + private const val NUMBER_OF_INIT_CARD = 2 } } diff --git a/src/main/kotlin/blackjack/domain/Participants.kt b/src/main/kotlin/blackjack/domain/Participants.kt new file mode 100644 index 000000000..57305084f --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Participants.kt @@ -0,0 +1,10 @@ +package blackjack.domain + +class Participants(private val participants: List): List by participants { + + constructor(vararg participantNames: String): this(participantNames.map { name -> Participant(name.trim()) }) + + fun setupCard(blackjackShoe: BlackjackShoe) { + participants.forEach { participant -> participant.setupCard(blackjackShoe) } + } +} diff --git a/src/main/kotlin/blackjack/domain/Player.kt b/src/main/kotlin/blackjack/domain/Player.kt index 3899a1a08..ea35a2f4b 100644 --- a/src/main/kotlin/blackjack/domain/Player.kt +++ b/src/main/kotlin/blackjack/domain/Player.kt @@ -19,6 +19,8 @@ abstract class Player( return score } + abstract fun setupCard(blackjackShoe: BlackjackShoe) + fun receiveCard(card: Card) { _cardList.add(card) } diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index 9831cd27e..b8db5603e 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -4,9 +4,10 @@ import blackjack.domain.BlackjackResults import blackjack.domain.Card import blackjack.domain.Dealer import blackjack.domain.Participant +import blackjack.domain.Participants class OutputView { - fun showReady(dealer: Dealer, participants: List) { + fun showReady(dealer: Dealer, participants: Participants) { println("\n${dealer.name}와 ${participants.joinToString { it.name }}에게 2장의 나누었습니다.") showDealerCard(dealer) From a5954def83fc32673551eb7ab6c4cae87f5752c0 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Thu, 2 Jan 2025 15:11:28 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=2021=EC=A0=90=EC=9D=B4=20=EC=B4=88?= =?UTF-8?q?=EA=B3=BC=ED=95=98=EB=8A=94=20Bust=20=EC=BC=80=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/domain/BlackjackResults.kt | 4 +- src/main/kotlin/blackjack/domain/Const.kt | 5 ++ .../kotlin/blackjack/BlackjackResultsTest.kt | 59 +++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/blackjack/domain/Const.kt diff --git a/src/main/kotlin/blackjack/domain/BlackjackResults.kt b/src/main/kotlin/blackjack/domain/BlackjackResults.kt index e71e5754e..e1382544f 100644 --- a/src/main/kotlin/blackjack/domain/BlackjackResults.kt +++ b/src/main/kotlin/blackjack/domain/BlackjackResults.kt @@ -11,8 +11,8 @@ class BlackjackResults( val participantScore = participant.score when { - participantScore > dealerScore -> BlackjackResult.WIN - participantScore < dealerScore -> BlackjackResult.LOSE + (participantScore <= Const.NUMBER_BLACKJACK && participantScore > dealerScore) || dealerScore > Const.NUMBER_BLACKJACK -> BlackjackResult.WIN + (dealerScore <= Const.NUMBER_BLACKJACK && participantScore < dealerScore) || participantScore > Const.NUMBER_BLACKJACK -> BlackjackResult.LOSE else -> BlackjackResult.DRAW } } diff --git a/src/main/kotlin/blackjack/domain/Const.kt b/src/main/kotlin/blackjack/domain/Const.kt new file mode 100644 index 000000000..05bb0951b --- /dev/null +++ b/src/main/kotlin/blackjack/domain/Const.kt @@ -0,0 +1,5 @@ +package blackjack.domain + +object Const { + const val NUMBER_BLACKJACK = 21 +} diff --git a/src/test/kotlin/blackjack/BlackjackResultsTest.kt b/src/test/kotlin/blackjack/BlackjackResultsTest.kt index ed8ec14e4..8dc3af30d 100644 --- a/src/test/kotlin/blackjack/BlackjackResultsTest.kt +++ b/src/test/kotlin/blackjack/BlackjackResultsTest.kt @@ -70,4 +70,63 @@ class BlackjackResultsTest { actual.shouldNotBeNull() actual shouldBe 1 } + + @Test + fun `딜러가 버스트면 참가자의 승`() { + val blackjackResults = BlackjackResults( + Dealer().apply { + receiveCard(Card(Suit.SPADE, Rank.KING)) + receiveCard(Card(Suit.SPADE, Rank.KING)) + receiveCard(Card(Suit.SPADE, Rank.KING)) + }, + listOf( + Participant("a").apply { + receiveCard(Card(Suit.SPADE, Rank.KING)) + receiveCard(Card(Suit.SPADE, Rank.KING)) + }, + Participant("b").apply { + receiveCard(Card(Suit.SPADE, Rank.FIVE)) + receiveCard(Card(Suit.SPADE, Rank.SIX)) + }, + Participant("c").apply { + receiveCard(Card(Suit.SPADE, Rank.TWO)) + receiveCard(Card(Suit.SPADE, Rank.TWO)) + }, + ) + ) + + val actual = blackjackResults.result[BlackjackResults.BlackjackResult.WIN]?.size + actual.shouldNotBeNull() + actual shouldBe 3 + } + + @Test + fun `참가자가 버스트면 딜러가 승`() { + val blackjackResults = BlackjackResults( + Dealer().apply { + receiveCard(Card(Suit.SPADE, Rank.KING)) + receiveCard(Card(Suit.SPADE, Rank.KING)) + }, + listOf( + Participant("a").apply { + receiveCard(Card(Suit.SPADE, Rank.KING)) + receiveCard(Card(Suit.SPADE, Rank.KING)) + }, + Participant("b").apply { + receiveCard(Card(Suit.SPADE, Rank.KING)) + receiveCard(Card(Suit.SPADE, Rank.KING)) + }, + Participant("c").apply { + receiveCard(Card(Suit.SPADE, Rank.KING)) + receiveCard(Card(Suit.SPADE, Rank.KING)) + receiveCard(Card(Suit.SPADE, Rank.KING)) + }, + ) + ) + + val actual = blackjackResults.result[BlackjackResults.BlackjackResult.LOSE]?.size + println(blackjackResults.result) + actual.shouldNotBeNull() + actual shouldBe 1 + } } From 129b572a62052b85b4916ba40f12389c1df720d0 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 13:29:17 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20Participant=EA=B0=80=20betting=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=EC=9D=84=20=EB=B0=9B=EC=9D=84=20=EC=88=98=20?= =?UTF-8?q?=EC=9E=88=EB=8F=84=EB=A1=9D=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/BlackjackApplication.kt | 3 ++- src/main/kotlin/blackjack/domain/Participant.kt | 2 ++ src/main/kotlin/blackjack/view/InputView.kt | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/BlackjackApplication.kt b/src/main/kotlin/blackjack/BlackjackApplication.kt index f00333481..59aefaa78 100644 --- a/src/main/kotlin/blackjack/BlackjackApplication.kt +++ b/src/main/kotlin/blackjack/BlackjackApplication.kt @@ -21,10 +21,11 @@ class BlackjackApplication( private fun ready(blackjackShoe: BlackjackShoe): Pair { val participantNames: List = inputView.getParticipantNames() + val participants = Participants(participantNames = participantNames.toTypedArray()) + inputView.requestBettingMoney(participants) val dealer = Dealer() dealer.setupCard(blackjackShoe = blackjackShoe) - val participants = Participants(participantNames = participantNames.toTypedArray()) participants.setupCard(blackjackShoe = blackjackShoe) outputView.showReady(dealer = dealer, participants = participants) diff --git a/src/main/kotlin/blackjack/domain/Participant.kt b/src/main/kotlin/blackjack/domain/Participant.kt index 92e1052c0..2ac1e765a 100644 --- a/src/main/kotlin/blackjack/domain/Participant.kt +++ b/src/main/kotlin/blackjack/domain/Participant.kt @@ -3,6 +3,8 @@ package blackjack.domain data class Participant( override val name: String ) : Player(name = name) { + var bettingMoney: Int = 0 + override val canReceiveCard: Boolean get() { return score <= CAN_RECEIVE_CARD_SCORE diff --git a/src/main/kotlin/blackjack/view/InputView.kt b/src/main/kotlin/blackjack/view/InputView.kt index 42fae717f..9f51e1069 100644 --- a/src/main/kotlin/blackjack/view/InputView.kt +++ b/src/main/kotlin/blackjack/view/InputView.kt @@ -1,6 +1,7 @@ package blackjack.view import blackjack.domain.Participant +import blackjack.domain.Participants class InputView { @@ -19,4 +20,13 @@ class InputView { } } + fun requestBettingMoney(participants: Participants) { + println() + participants.forEach { participant -> + println("${participant.name}의 배팅 금액은?") + val bettingMoney = readlnOrNull()?.toIntOrNull() ?: error("배팅은 ") + participant.bettingMoney = bettingMoney + } + } + } From 6f81637c7e64adaa914a8d421cb5df3933b63400 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 14:34:07 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20BlackjackResults=EA=B0=80=20?= =?UTF-8?q?=EC=B5=9C=EC=A2=85=20=EC=88=98=EC=9D=B5=EC=9D=84=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=ED=95=98=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 기존에 승패가 나왔지만 이제는 최종 수익이 나오도록 함 --- .../blackjack/domain/BlackjackResults.kt | 23 +++++----- src/main/kotlin/blackjack/domain/Player.kt | 2 + src/main/kotlin/blackjack/view/OutputView.kt | 17 ++------ .../kotlin/blackjack/BlackjackResultsTest.kt | 42 +++++++------------ 4 files changed, 31 insertions(+), 53 deletions(-) diff --git a/src/main/kotlin/blackjack/domain/BlackjackResults.kt b/src/main/kotlin/blackjack/domain/BlackjackResults.kt index e1382544f..bf20be480 100644 --- a/src/main/kotlin/blackjack/domain/BlackjackResults.kt +++ b/src/main/kotlin/blackjack/domain/BlackjackResults.kt @@ -5,22 +5,21 @@ class BlackjackResults( val participants: List, ) { - val result: Map> = run { + val result: List = run { val dealerScore = dealer.score - participants.groupBy { participant -> - val participantScore = participant.score - when { - (participantScore <= Const.NUMBER_BLACKJACK && participantScore > dealerScore) || dealerScore > Const.NUMBER_BLACKJACK -> BlackjackResult.WIN - (dealerScore <= Const.NUMBER_BLACKJACK && participantScore < dealerScore) || participantScore > Const.NUMBER_BLACKJACK -> BlackjackResult.LOSE - else -> BlackjackResult.DRAW + participants.forEach { participant -> + val participantScore = participant.score + val bettingMoney = when { + (participantScore <= Const.NUMBER_BLACKJACK && participantScore > dealerScore) || dealerScore > Const.NUMBER_BLACKJACK -> participant.bettingMoney + (dealerScore <= Const.NUMBER_BLACKJACK && participantScore < dealerScore) || participantScore > Const.NUMBER_BLACKJACK -> -participant.bettingMoney + else -> 0 } + + dealer.money -= bettingMoney + participant.money += bettingMoney } - } - enum class BlackjackResult { - WIN, - LOSE, - DRAW, + listOf(dealer, *participants.toTypedArray()) } } diff --git a/src/main/kotlin/blackjack/domain/Player.kt b/src/main/kotlin/blackjack/domain/Player.kt index ea35a2f4b..cf3115953 100644 --- a/src/main/kotlin/blackjack/domain/Player.kt +++ b/src/main/kotlin/blackjack/domain/Player.kt @@ -19,6 +19,8 @@ abstract class Player( return score } + var money: Int = 0 + abstract fun setupCard(blackjackShoe: BlackjackShoe) fun receiveCard(card: Card) { diff --git a/src/main/kotlin/blackjack/view/OutputView.kt b/src/main/kotlin/blackjack/view/OutputView.kt index b8db5603e..bf4960d5f 100644 --- a/src/main/kotlin/blackjack/view/OutputView.kt +++ b/src/main/kotlin/blackjack/view/OutputView.kt @@ -40,20 +40,9 @@ class OutputView { } fun showResult(blackjackResults: BlackjackResults) { - println("\n## 최종승패") - val result = blackjackResults.result - val participantWin = result[BlackjackResults.BlackjackResult.WIN].orEmpty() - val participantLose = result[BlackjackResults.BlackjackResult.LOSE].orEmpty() - val participantDraw = result[BlackjackResults.BlackjackResult.DRAW].orEmpty() - println("${blackjackResults.dealer.name}: ${participantLose.size}승 ${participantWin.size}패 ${participantDraw.size}무") - participantWin.forEach { - println("${it.name}: 승") - } - participantLose.forEach { - println("${it.name}: 패") - } - participantDraw.forEach { - println("${it.name}: 무") + println("\n## 최종 수익") + blackjackResults.result.forEach { player -> + println("${player.name}: ${player.money}") } } diff --git a/src/test/kotlin/blackjack/BlackjackResultsTest.kt b/src/test/kotlin/blackjack/BlackjackResultsTest.kt index 8dc3af30d..ac3980be1 100644 --- a/src/test/kotlin/blackjack/BlackjackResultsTest.kt +++ b/src/test/kotlin/blackjack/BlackjackResultsTest.kt @@ -22,13 +22,14 @@ class BlackjackResultsTest { Participant("a").apply { receiveCard(Card(Suit.SPADE, Rank.KING)) receiveCard(Card(Suit.SPADE, Rank.NINE)) + bettingMoney = 10000 } ) ) - val actual = blackjackResults.result[BlackjackResults.BlackjackResult.LOSE]?.size + val actual = blackjackResults.result.first().money actual.shouldNotBeNull() - actual shouldBe 1 + actual shouldBe 10000 } @Test @@ -42,13 +43,14 @@ class BlackjackResultsTest { Participant("a").apply { receiveCard(Card(Suit.SPADE, Rank.KING)) receiveCard(Card(Suit.SPADE, Rank.KING)) + bettingMoney = 10000 } ) ) - val actual = blackjackResults.result[BlackjackResults.BlackjackResult.WIN]?.size + val actual = blackjackResults.result.first().money actual.shouldNotBeNull() - actual shouldBe 1 + actual shouldBe -10000 } @Test @@ -62,13 +64,14 @@ class BlackjackResultsTest { Participant("a").apply { receiveCard(Card(Suit.SPADE, Rank.KING)) receiveCard(Card(Suit.SPADE, Rank.KING)) + bettingMoney = 10000 } ) ) - val actual = blackjackResults.result[BlackjackResults.BlackjackResult.DRAW]?.size + val actual = blackjackResults.result.first().money actual.shouldNotBeNull() - actual shouldBe 1 + actual shouldBe 0 } @Test @@ -83,21 +86,14 @@ class BlackjackResultsTest { Participant("a").apply { receiveCard(Card(Suit.SPADE, Rank.KING)) receiveCard(Card(Suit.SPADE, Rank.KING)) - }, - Participant("b").apply { - receiveCard(Card(Suit.SPADE, Rank.FIVE)) - receiveCard(Card(Suit.SPADE, Rank.SIX)) - }, - Participant("c").apply { - receiveCard(Card(Suit.SPADE, Rank.TWO)) - receiveCard(Card(Suit.SPADE, Rank.TWO)) + bettingMoney = 10000 }, ) ) - val actual = blackjackResults.result[BlackjackResults.BlackjackResult.WIN]?.size + val actual = blackjackResults.result.first().money actual.shouldNotBeNull() - actual shouldBe 3 + actual shouldBe -10000 } @Test @@ -111,22 +107,14 @@ class BlackjackResultsTest { Participant("a").apply { receiveCard(Card(Suit.SPADE, Rank.KING)) receiveCard(Card(Suit.SPADE, Rank.KING)) - }, - Participant("b").apply { - receiveCard(Card(Suit.SPADE, Rank.KING)) - receiveCard(Card(Suit.SPADE, Rank.KING)) - }, - Participant("c").apply { - receiveCard(Card(Suit.SPADE, Rank.KING)) - receiveCard(Card(Suit.SPADE, Rank.KING)) receiveCard(Card(Suit.SPADE, Rank.KING)) + bettingMoney = 10000 }, ) ) - val actual = blackjackResults.result[BlackjackResults.BlackjackResult.LOSE]?.size - println(blackjackResults.result) + val actual = blackjackResults.result.first().money actual.shouldNotBeNull() - actual shouldBe 1 + actual shouldBe 10000 } }