From 585c554ac9f4dbec3488cf1554f86e1ccdf7dadd Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 22:44:31 +0900 Subject: [PATCH 01/44] =?UTF-8?q?refactor:=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=ED=8F=AC=EB=A7=B7=20=EB=A7=9E=EC=B6=94=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/GameManager.kt | 4 ++-- src/main/kotlin/blackjack/ui/OutputManager.kt | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/blackjack/GameManager.kt b/src/main/kotlin/blackjack/GameManager.kt index 3af5e814a..70f56e053 100644 --- a/src/main/kotlin/blackjack/GameManager.kt +++ b/src/main/kotlin/blackjack/GameManager.kt @@ -28,11 +28,12 @@ class GameManager( val result = playBlackJack() + outputManager.printDealerResultGame(dealer) + players.forEach { outputManager.printPlayerResultGame(it) } - outputManager.printDealerResultGame(dealer) outputManager.printResult(result) } @@ -62,7 +63,6 @@ class GameManager( while (dealer.shouldDraw()) { outputManager.printDealerCanDrawMessage() dealer.drawCard(CardDeck.draw(DRAW_CARD)) - outputManager.printDealerCards(dealer) } } diff --git a/src/main/kotlin/blackjack/ui/OutputManager.kt b/src/main/kotlin/blackjack/ui/OutputManager.kt index 46b105b07..c286979c3 100644 --- a/src/main/kotlin/blackjack/ui/OutputManager.kt +++ b/src/main/kotlin/blackjack/ui/OutputManager.kt @@ -18,6 +18,8 @@ class OutputManager { players.forEach { println("${it.name.value}: ${parsingCardsToString(it.cards)}") } + + printEnter() } fun printPlayerCards(player: Player) { @@ -33,10 +35,13 @@ class OutputManager { } fun printDealerResultGame(dealer: Dealer) { + printEnter() println("딜러 카드: ${parsingCardsToString(dealer.cards)} - 결과: ${dealer.resultScore()}") } fun printResult(gameResult: GameResult) { + printEnter() + println(RESULT_MESSAGE) gameResult.resultMap.forEach { println("${it.key.value} : ${parsingGameResult(it.value)}") } @@ -84,18 +89,25 @@ class OutputManager { } fun printFirstTurn2(players: List) { + printEnter() val names: String = players.joinToString(", ") { it.name.value } println("딜러와 ${names}에게 2장의 카드를 나누었습니다.") } fun printDealerCanDrawMessage() { + printEnter() println(DEALER_MESSAGE) } + private fun printEnter() { + println() + } + companion object { - private const val DEALER_MESSAGE = "16이하라 한장의 카드를 더 받았습니다." + private const val DEALER_MESSAGE = "딜러는 16이하라 한장의 카드를 더 받았습니다." private const val WIN = "승" private const val LOSE = "패" + private const val RESULT_MESSAGE = "## 최종승패" } } From 60c5695431a92d773b58ae4381ad003729a982eb Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 22:57:56 +0900 Subject: [PATCH 02/44] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=20=EB=B2=84=EA=B7=B8=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/GameResult.kt | 33 ++++++++++++++++--------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/blackjack/GameResult.kt b/src/main/kotlin/blackjack/GameResult.kt index 1c17ea49c..a56835da0 100644 --- a/src/main/kotlin/blackjack/GameResult.kt +++ b/src/main/kotlin/blackjack/GameResult.kt @@ -12,24 +12,35 @@ class GameResult( val resultMap: Map init { - - val (winCount, loseCount) = when { - dealer.isBust -> players.partition { !it.isBust } - else -> players.partition { it.resultScore() < dealer.resultScore() } - } + val winner = findWinner(players, dealer) + val loser = findLoser(players, dealer) val playerResult = mutableMapOf().apply { - winCount.map { this[it.name] = Result.Win() } - loseCount.map { this[it.name] = Result.Lose() } + loser.map { this[it.name] = Result.Lose() } + winner.map { this[it.name] = Result.Win() } } resultMap = mapOf( - Name("딜러") to Result.DealerResult(winCount.size, loseCount.size) + Name("딜러") to Result.DealerResult(loser.size, winner.size) ) + playerResult } - companion object { - private const val WIN: String = "승" - private const val LOSE: String = "패" + private fun findWinner(players: List, dealer: Dealer): List { + if (dealer.isBust) { + return players.filter { !it.isBust } + } + + return players.filter { !it.isBust }.filter { it.resultScore() > dealer.resultScore() } + } + + private fun findLoser(players: List, dealer: Dealer): List { + if (dealer.isBust) { + return players.filter { it.isBust } + } + + val bustPlayer = players.filter { it.isBust } + val loser = players.filter { !it.isBust }.filter { it.resultScore() < dealer.resultScore() } + + return bustPlayer + loser } } From 3fb5c38b46553e47ff7939ff932288ab68ab8d7f Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 22:58:23 +0900 Subject: [PATCH 03/44] feat: ktlint --- src/main/kotlin/blackjack/GameResult.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/GameResult.kt b/src/main/kotlin/blackjack/GameResult.kt index a56835da0..8d5538dd6 100644 --- a/src/main/kotlin/blackjack/GameResult.kt +++ b/src/main/kotlin/blackjack/GameResult.kt @@ -30,7 +30,7 @@ class GameResult( return players.filter { !it.isBust } } - return players.filter { !it.isBust }.filter { it.resultScore() > dealer.resultScore() } + return players.filter { !it.isBust }.filter { it.resultScore() > dealer.resultScore() } } private fun findLoser(players: List, dealer: Dealer): List { From 8deccc4f634b5ee1f3639e424ea13a68393c6ce4 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 23:00:20 +0900 Subject: [PATCH 04/44] =?UTF-8?q?feat:=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/GameManager.kt | 4 +--- src/main/kotlin/blackjack/ui/OutputManager.kt | 6 +----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/blackjack/GameManager.kt b/src/main/kotlin/blackjack/GameManager.kt index 70f56e053..604110c74 100644 --- a/src/main/kotlin/blackjack/GameManager.kt +++ b/src/main/kotlin/blackjack/GameManager.kt @@ -23,7 +23,7 @@ class GameManager( players.forEach { it.drawCard(CardDeck.draw(FIRST_DRAW)) } dealer.drawCard(CardDeck.draw(FIRST_DRAW)) - outputManager.printFirstTurn2(players) + outputManager.printFirstTurn(players) outputManager.printPlayersAndDealerCards(players, dealer) val result = playBlackJack() @@ -33,8 +33,6 @@ class GameManager( players.forEach { outputManager.printPlayerResultGame(it) } - - outputManager.printResult(result) } diff --git a/src/main/kotlin/blackjack/ui/OutputManager.kt b/src/main/kotlin/blackjack/ui/OutputManager.kt index c286979c3..fddebdb0b 100644 --- a/src/main/kotlin/blackjack/ui/OutputManager.kt +++ b/src/main/kotlin/blackjack/ui/OutputManager.kt @@ -26,10 +26,6 @@ class OutputManager { println("${player.name.value}: ${parsingCardsToString(player.cards)}") } - fun printDealerCards(dealer: Dealer) { - println("딜러: ${parsingCardsToString(dealer.cards)}") - } - fun printPlayerResultGame(player: Player) { println("${player.name.value} 카드: ${parsingCardsToString(player.cards)} - 결과: ${player.resultScore()}") } @@ -88,7 +84,7 @@ class OutputManager { } } - fun printFirstTurn2(players: List) { + fun printFirstTurn(players: List) { printEnter() val names: String = players.joinToString(", ") { it.name.value } From 81a9b87ef7d572a9ab3d19273de17c1cbe47c0a3 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 23:02:16 +0900 Subject: [PATCH 05/44] =?UTF-8?q?refactor:=20object=20=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/GameResult.kt | 4 ++-- src/main/kotlin/blackjack/participant/Result.kt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/blackjack/GameResult.kt b/src/main/kotlin/blackjack/GameResult.kt index 8d5538dd6..4b34f4d84 100644 --- a/src/main/kotlin/blackjack/GameResult.kt +++ b/src/main/kotlin/blackjack/GameResult.kt @@ -16,8 +16,8 @@ class GameResult( val loser = findLoser(players, dealer) val playerResult = mutableMapOf().apply { - loser.map { this[it.name] = Result.Lose() } - winner.map { this[it.name] = Result.Win() } + loser.map { this[it.name] = Result.Lose } + winner.map { this[it.name] = Result.Win } } resultMap = mapOf( diff --git a/src/main/kotlin/blackjack/participant/Result.kt b/src/main/kotlin/blackjack/participant/Result.kt index 5950390c6..38e682a35 100644 --- a/src/main/kotlin/blackjack/participant/Result.kt +++ b/src/main/kotlin/blackjack/participant/Result.kt @@ -1,7 +1,7 @@ package blackjack.participant sealed interface Result { - class Win : Result - class Lose : Result + object Win : Result + object Lose : Result class DealerResult(val win: Int, val lose: Int) : Result } From dbc4ac9786b84f7b4f3f9faeb74dc5a929637b9f Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 23:04:57 +0900 Subject: [PATCH 06/44] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/participant/Name.kt | 10 +++++++++- src/test/kotlin/blackjack/participant/NameTest.kt | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/blackjack/participant/NameTest.kt diff --git a/src/main/kotlin/blackjack/participant/Name.kt b/src/main/kotlin/blackjack/participant/Name.kt index 98da8e574..d3020bacc 100644 --- a/src/main/kotlin/blackjack/participant/Name.kt +++ b/src/main/kotlin/blackjack/participant/Name.kt @@ -3,4 +3,12 @@ package blackjack.participant @JvmInline value class Name( val value: String -) \ No newline at end of file +) { + init { + require(value.isNotBlank()) { VALID_MESSAGE } + } + + companion object { + private const val VALID_MESSAGE: String = "빈 값은 이름이 될 수 없습니다." + } +} diff --git a/src/test/kotlin/blackjack/participant/NameTest.kt b/src/test/kotlin/blackjack/participant/NameTest.kt new file mode 100644 index 000000000..a07e1df94 --- /dev/null +++ b/src/test/kotlin/blackjack/participant/NameTest.kt @@ -0,0 +1,12 @@ +package blackjack.participant + +import io.kotest.assertions.throwables.shouldThrow +import org.junit.jupiter.api.Test + +class NameTest { + + @Test + fun `빈 문자열은 이름이 될 수 없다`() { + shouldThrow { Name("") } + } +} From ba515fecc006b8c59fe6eab46f1515408d25eca7 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 23:06:40 +0900 Subject: [PATCH 07/44] =?UTF-8?q?refactor:=20default=20arguments=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/GameManager.kt | 2 +- src/main/kotlin/blackjack/participant/Player.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/blackjack/GameManager.kt b/src/main/kotlin/blackjack/GameManager.kt index 604110c74..2854d7ff3 100644 --- a/src/main/kotlin/blackjack/GameManager.kt +++ b/src/main/kotlin/blackjack/GameManager.kt @@ -68,7 +68,7 @@ class GameManager( private fun joinPlayers(): List { val playerNames: List = inputManager.inputPlayerNames() - return playerNames.map { Player(Name(it), ScoreCalculator()) } + return playerNames.map { Player(Name(it)) } } private fun joinDealer(): Dealer { diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index f4a08da4f..ebd96fa17 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -5,7 +5,7 @@ import blackjack.card.BlackJackCard class Player( val name: Name, - scoreCalculator: ScoreCalculator + scoreCalculator: ScoreCalculator = ScoreCalculator() ) { private val blackjackStrategy: BlackjackStrategy = BlackjackStrategy(scoreCalculator) From 821edf8db09fa8d1632667d0aa9dd4ef1f8711e6 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 23:11:57 +0900 Subject: [PATCH 08/44] =?UTF-8?q?doc:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index b4a4e9446..eaa999a77 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ - 입력 - [x] 게임에 참여할 사람을 입력받는다. - [x] 한장의 카드를 더 받을지 말지 입력받는다. + - [] 플레이어의 배팅 금액을 입력받는다. - 출력 - [x] 플레이어가 가지고 있는 카드를 출력한다. @@ -26,9 +27,13 @@ - [x] 플레이어는 카드를 가질 수 있다. - [x] 블랙잭 게임이 있다. - [x] 게임 시작시 두 장의 카드를 지급받는다. + - [] 처음 두 장의 카드 합이 21일 경우 베팅 금액의 1.5배를 딜러에게 받는다. + - [] 딜러와 플레이어 동시에 블랙잭인 경우 플레이어는 베팅한 금액을 돌려받는다. - [x] 플레이어는 가지고 있는 카드가 21을 넘지 않은 경우 카드를 계속 뽑을 수 있다. + - [] 카드를 추가로 뽑아 21을 초과하는 경우 배팅 금액을 모두 잃는다. - [x] 딜러는 플레이어들이 전부 카드를 뽑은 뒤에 카드를 뽑는다. - [x] 딜러가 21을 초과하면 그 시점까지 남아 있던 플레이어들은 패에 상관없이 승리한다. + - [] 남아 있는 플레이어들은 베팅 금액을 받는다. - [x] 블랙잭 점수 계산기가 있다. - [x] Ace는 1 또는 11로 계산할 수 있다. - [x] King, Queen, Jack 는 10으로 계산한다 From 99c2a8253261ab9dae72b99aef4af3ffa7cca42c Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 23:43:53 +0900 Subject: [PATCH 09/44] =?UTF-8?q?feat:=20=EB=B2=A0=ED=8C=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/participant/BetingAmount.kt | 14 ++++++++++++++ .../blackjack/participant/BetingAmountTest.kt | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/kotlin/blackjack/participant/BetingAmount.kt create mode 100644 src/test/kotlin/blackjack/participant/BetingAmountTest.kt diff --git a/src/main/kotlin/blackjack/participant/BetingAmount.kt b/src/main/kotlin/blackjack/participant/BetingAmount.kt new file mode 100644 index 000000000..5c3f785ba --- /dev/null +++ b/src/main/kotlin/blackjack/participant/BetingAmount.kt @@ -0,0 +1,14 @@ +package blackjack.participant + +@JvmInline +value class BetingAmount( + val amount: Int +) { + init { + require(amount > 0) { VALID_MESSAGE } + } + + companion object { + private const val VALID_MESSAGE: String = "베팅 금액은 0 이하일 수 없습니다." + } +} diff --git a/src/test/kotlin/blackjack/participant/BetingAmountTest.kt b/src/test/kotlin/blackjack/participant/BetingAmountTest.kt new file mode 100644 index 000000000..54676317c --- /dev/null +++ b/src/test/kotlin/blackjack/participant/BetingAmountTest.kt @@ -0,0 +1,12 @@ +package blackjack.participant + +import io.kotest.assertions.throwables.shouldThrow +import org.junit.jupiter.api.Test + +class BetingAmountTest { + + @Test + fun `베팅 금액은 0이하일 수 없다`() { + shouldThrow { BetingAmount(-1) } + } +} From 7596635dcf6ec6a82bee66c0a04559d4009c4b58 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 23:44:54 +0900 Subject: [PATCH 10/44] =?UTF-8?q?feat:=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=98=A4=ED=83=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../participant/{BetingAmount.kt => BettingAmount.kt} | 2 +- .../participant/{BetingAmountTest.kt => BettingAmountTest.kt} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/main/kotlin/blackjack/participant/{BetingAmount.kt => BettingAmount.kt} (90%) rename src/test/kotlin/blackjack/participant/{BetingAmountTest.kt => BettingAmountTest.kt} (66%) diff --git a/src/main/kotlin/blackjack/participant/BetingAmount.kt b/src/main/kotlin/blackjack/participant/BettingAmount.kt similarity index 90% rename from src/main/kotlin/blackjack/participant/BetingAmount.kt rename to src/main/kotlin/blackjack/participant/BettingAmount.kt index 5c3f785ba..6b2205133 100644 --- a/src/main/kotlin/blackjack/participant/BetingAmount.kt +++ b/src/main/kotlin/blackjack/participant/BettingAmount.kt @@ -1,7 +1,7 @@ package blackjack.participant @JvmInline -value class BetingAmount( +value class BettingAmount( val amount: Int ) { init { diff --git a/src/test/kotlin/blackjack/participant/BetingAmountTest.kt b/src/test/kotlin/blackjack/participant/BettingAmountTest.kt similarity index 66% rename from src/test/kotlin/blackjack/participant/BetingAmountTest.kt rename to src/test/kotlin/blackjack/participant/BettingAmountTest.kt index 54676317c..7f7fe9e56 100644 --- a/src/test/kotlin/blackjack/participant/BetingAmountTest.kt +++ b/src/test/kotlin/blackjack/participant/BettingAmountTest.kt @@ -3,10 +3,10 @@ package blackjack.participant import io.kotest.assertions.throwables.shouldThrow import org.junit.jupiter.api.Test -class BetingAmountTest { +class BettingAmountTest { @Test fun `베팅 금액은 0이하일 수 없다`() { - shouldThrow { BetingAmount(-1) } + shouldThrow { BettingAmount(-1) } } } From f91a9bd64631338a39376b98f63c6ab7f5c1c1ca Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Tue, 5 Dec 2023 23:50:28 +0900 Subject: [PATCH 11/44] =?UTF-8?q?feat:=20Player=EA=B0=80=20=EB=B2=A0?= =?UTF-8?q?=ED=8C=85=20=EA=B8=88=EC=95=A1=EC=9D=84=20=EA=B0=80=EC=A7=80?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/participant/BettingAmount.kt | 2 +- src/main/kotlin/blackjack/participant/Player.kt | 3 +++ src/main/kotlin/blackjack/ui/InputManager.kt | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/blackjack/participant/BettingAmount.kt b/src/main/kotlin/blackjack/participant/BettingAmount.kt index 6b2205133..ebd9ace9a 100644 --- a/src/main/kotlin/blackjack/participant/BettingAmount.kt +++ b/src/main/kotlin/blackjack/participant/BettingAmount.kt @@ -5,7 +5,7 @@ value class BettingAmount( val amount: Int ) { init { - require(amount > 0) { VALID_MESSAGE } + require(amount >= 0) { VALID_MESSAGE } } companion object { diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index ebd96fa17..e7ebcee14 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -5,8 +5,11 @@ import blackjack.card.BlackJackCard class Player( val name: Name, + val bettingAmount: BettingAmount = BettingAmount(0), scoreCalculator: ScoreCalculator = ScoreCalculator() ) { + constructor(name: Name, scoreCalculator: ScoreCalculator): this(name, BettingAmount(0), scoreCalculator) + constructor(name: Name): this(name, BettingAmount(0), ScoreCalculator()) private val blackjackStrategy: BlackjackStrategy = BlackjackStrategy(scoreCalculator) diff --git a/src/main/kotlin/blackjack/ui/InputManager.kt b/src/main/kotlin/blackjack/ui/InputManager.kt index bb17be52a..619270d28 100644 --- a/src/main/kotlin/blackjack/ui/InputManager.kt +++ b/src/main/kotlin/blackjack/ui/InputManager.kt @@ -8,6 +8,12 @@ class InputManager { return inputUserValue().replace("\\s".toRegex(), "").split(",") } + fun inputBettingAmount(player: Player): Int { + println() + println("${player.name.value}의 베팅 금액은?") + return inputUserValue().toInt() + } + private fun inputUserValue(): String { val input = readln() require(input.isNotBlank()) { INPUT_NOT_NULL_MESSAGE } From 09a86f95cbf2f1e8e2a7773b7365e3af359c5073 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Wed, 6 Dec 2023 22:21:39 +0900 Subject: [PATCH 12/44] =?UTF-8?q?feat:=20=EB=B2=A0=ED=8C=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/GameManager.kt | 3 ++- src/main/kotlin/blackjack/participant/Dealer.kt | 1 + src/main/kotlin/blackjack/participant/Player.kt | 3 +-- src/main/kotlin/blackjack/ui/InputManager.kt | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/blackjack/GameManager.kt b/src/main/kotlin/blackjack/GameManager.kt index 2854d7ff3..6bc7609d2 100644 --- a/src/main/kotlin/blackjack/GameManager.kt +++ b/src/main/kotlin/blackjack/GameManager.kt @@ -1,6 +1,7 @@ package blackjack import blackjack.card.CardDeck +import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name import blackjack.participant.Player @@ -68,7 +69,7 @@ class GameManager( private fun joinPlayers(): List { val playerNames: List = inputManager.inputPlayerNames() - return playerNames.map { Player(Name(it)) } + return playerNames.map { Player(Name(it), BettingAmount(inputManager.inputBettingAmount(it))) } } private fun joinDealer(): Dealer { diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index 14784b8fa..b8850da3d 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -7,6 +7,7 @@ class Dealer( scoreCalculator: ScoreCalculator ) { private val blackjackStrategy: BlackjackStrategy = BlackjackStrategy(scoreCalculator) + val bettingAmount: BettingAmount = BettingAmount(0) val cards get() = blackjackStrategy.cards diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index e7ebcee14..d770db96a 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -8,8 +8,7 @@ class Player( val bettingAmount: BettingAmount = BettingAmount(0), scoreCalculator: ScoreCalculator = ScoreCalculator() ) { - constructor(name: Name, scoreCalculator: ScoreCalculator): this(name, BettingAmount(0), scoreCalculator) - constructor(name: Name): this(name, BettingAmount(0), ScoreCalculator()) + constructor(name: Name, scoreCalculator: ScoreCalculator) : this(name, BettingAmount(0), scoreCalculator) private val blackjackStrategy: BlackjackStrategy = BlackjackStrategy(scoreCalculator) diff --git a/src/main/kotlin/blackjack/ui/InputManager.kt b/src/main/kotlin/blackjack/ui/InputManager.kt index 619270d28..8c4a65826 100644 --- a/src/main/kotlin/blackjack/ui/InputManager.kt +++ b/src/main/kotlin/blackjack/ui/InputManager.kt @@ -8,9 +8,9 @@ class InputManager { return inputUserValue().replace("\\s".toRegex(), "").split(",") } - fun inputBettingAmount(player: Player): Int { + fun inputBettingAmount(playerName: String): Int { println() - println("${player.name.value}의 베팅 금액은?") + println("${playerName}의 베팅 금액은?") return inputUserValue().toInt() } From f9ffe937ccf737c6b79746ada1e61e43eb1efc54 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Wed, 6 Dec 2023 22:47:18 +0900 Subject: [PATCH 13/44] =?UTF-8?q?feat:=20=EB=B8=94=EB=9E=99=EC=9E=AD=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 23 ++++++++++++---- .../participant/BlackjackStrategy.kt | 15 +++++++++-- .../kotlin/blackjack/participant/Player.kt | 1 + .../blackjack/participant/status/Blackjack.kt | 4 +++ .../blackjack/participant/status/Hit.kt | 4 +++ .../blackjack/participant/status/Status.kt | 4 +++ .../participant/BlackjackStrategyTest.kt | 26 +++++++++++++++++++ 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/blackjack/participant/status/Blackjack.kt create mode 100644 src/main/kotlin/blackjack/participant/status/Hit.kt create mode 100644 src/main/kotlin/blackjack/participant/status/Status.kt create mode 100644 src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt diff --git a/README.md b/README.md index eaa999a77..16c36a304 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,9 @@ - [x] 플레이어는 카드를 가질 수 있다. - [x] 블랙잭 게임이 있다. - [x] 게임 시작시 두 장의 카드를 지급받는다. - - [] 처음 두 장의 카드 합이 21일 경우 베팅 금액의 1.5배를 딜러에게 받는다. - - [] 딜러와 플레이어 동시에 블랙잭인 경우 플레이어는 베팅한 금액을 돌려받는다. - [x] 플레이어는 가지고 있는 카드가 21을 넘지 않은 경우 카드를 계속 뽑을 수 있다. - - [] 카드를 추가로 뽑아 21을 초과하는 경우 배팅 금액을 모두 잃는다. - [x] 딜러는 플레이어들이 전부 카드를 뽑은 뒤에 카드를 뽑는다. - [x] 딜러가 21을 초과하면 그 시점까지 남아 있던 플레이어들은 패에 상관없이 승리한다. - - [] 남아 있는 플레이어들은 베팅 금액을 받는다. - [x] 블랙잭 점수 계산기가 있다. - [x] Ace는 1 또는 11로 계산할 수 있다. - [x] King, Queen, Jack 는 10으로 계산한다 @@ -42,6 +38,19 @@ - [x] 딜러는 처음에 받은 2장의 합계가 16이하라면 반드시 한장의 카드를 받는다. - [x] 16점 이하면 계속 카드를 뽑는다. - [x] 17점 이상이면 카드를 추가로 받을 수 없다. + - [] 상태라는 클래스가 있다. + - [] 히트라는 상태가 있다 + - [] 히트는 먼저 받은 두 장의 합이 21에 못미치는 경우다. + - [] 버스트라는 상태가 있다. + - [] 버스트는 카드 총합이 21을 넘은 경우다. + - [] 카드를 추가로 뽑아 21을 초과하는 경우 배팅 금액을 모두 잃는다. + - [] 딜러가 버스트라면 남아 있는 플레이어들은 베팅 금액을 받는다. + - [] 스탠드라는 상태가 있다. + - [] 스탠드는 더 이상 카드를 뽑지 않는 경우다. + - [] 블랙잭 이라는 생태가 있다. + - [] 처음 받은 두 장의 카드의 총 합이 21이 되는 경우이다. + - [] 베팅 금액의 1.5배를 딜러에게 받는다. + - [] 딜러와 플레이어 동시에 블랙잭인 경우 플레이어는 베팅한 금액을 돌려받는다. ## 용어 정리 @@ -50,4 +59,8 @@ - 플레이어가 버스트 당하면 패배가 확정된다. - 딜러가 버스트 당하면 그 시점까지 남아있는 모든 플레이어가 승리한다. - 블랙잭(Blackjack) - - A한장과 10, J, Q, L 로 21을 이루는 경우 베팅 금액의 1.5배를 돌려준다. \ No newline at end of file + - A한장과 10, J, Q, L 로 21을 이루는 경우 베팅 금액의 1.5배를 돌려준다. +- 히트(Hit) + - 먼저 받은 두 장의 합이 21에 못미치는 경우 +- 스탠드(Stand) + - 더 이상 카드를 뽑지 않고 넘어간 경우 \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index ccfbcc6fb..f2bf9b074 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -2,23 +2,34 @@ package blackjack.participant import blackjack.ScoreCalculator import blackjack.card.BlackJackCard +import blackjack.participant.status.Blackjack +import blackjack.participant.status.Status class BlackjackStrategy( private val scoreCalculator: ScoreCalculator, ) { var cards: List = emptyList() + lateinit var status: Status - val isBust get() = resultScore() > BUST + val isBust get() = resultScore() > BLACKJACK fun drawCard(cards: List) { this.cards += cards + if (cards.size == FIRST_TURN_DRAW && isBlackjack()) { + status = Blackjack() + } } fun resultScore(): Int { return scoreCalculator.calculateGameScore(cards) } + private fun isBlackjack(): Boolean { + return resultScore() == BLACKJACK + } + companion object { - private const val BUST: Int = 21 + private const val BLACKJACK: Int = 21 + private const val FIRST_TURN_DRAW: Int = 2 } } \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index d770db96a..b9851283c 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -2,6 +2,7 @@ package blackjack.participant import blackjack.ScoreCalculator import blackjack.card.BlackJackCard +import blackjack.participant.status.Status class Player( val name: Name, diff --git a/src/main/kotlin/blackjack/participant/status/Blackjack.kt b/src/main/kotlin/blackjack/participant/status/Blackjack.kt new file mode 100644 index 000000000..5546fee90 --- /dev/null +++ b/src/main/kotlin/blackjack/participant/status/Blackjack.kt @@ -0,0 +1,4 @@ +package blackjack.participant.status + +class Blackjack : Status { +} \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/status/Hit.kt b/src/main/kotlin/blackjack/participant/status/Hit.kt new file mode 100644 index 000000000..e2007577a --- /dev/null +++ b/src/main/kotlin/blackjack/participant/status/Hit.kt @@ -0,0 +1,4 @@ +package blackjack.participant.status + +class Hit : Status { +} \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/status/Status.kt b/src/main/kotlin/blackjack/participant/status/Status.kt new file mode 100644 index 000000000..c5899516a --- /dev/null +++ b/src/main/kotlin/blackjack/participant/status/Status.kt @@ -0,0 +1,4 @@ +package blackjack.participant.status + +interface Status { +} \ No newline at end of file diff --git a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt new file mode 100644 index 000000000..318d03489 --- /dev/null +++ b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt @@ -0,0 +1,26 @@ +package blackjack.participant + +import blackjack.ScoreCalculator +import blackjack.card.AceCard +import blackjack.card.CardPattern +import blackjack.card.CardPicture +import blackjack.card.PictureCard +import blackjack.participant.status.Blackjack +import io.kotest.matchers.types.shouldBeInstanceOf +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +class BlackjackStrategyTest { + + @Test + fun `처음 두 장을 드로우 했을 때 합이 21이면 블랙잭상태이다`() { + val blackjackStrategy = BlackjackStrategy(ScoreCalculator()) + blackjackStrategy.drawCard(listOf( + PictureCard(CardPicture.JACK, CardPattern.SPADE), + AceCard(CardPattern.SPADE), + )) + + blackjackStrategy.status.shouldBeInstanceOf() + } + +} \ No newline at end of file From 8debf0ce8e36c07f067e78edd09750b8703a129a Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Wed, 6 Dec 2023 22:52:14 +0900 Subject: [PATCH 14/44] =?UTF-8?q?feat:=20=ED=9E=88=ED=8A=B8=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/participant/BlackjackStrategy.kt | 2 ++ .../blackjack/participant/BlackjackStrategyTest.kt | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index f2bf9b074..f3622ee43 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -3,6 +3,7 @@ package blackjack.participant import blackjack.ScoreCalculator import blackjack.card.BlackJackCard import blackjack.participant.status.Blackjack +import blackjack.participant.status.Hit import blackjack.participant.status.Status class BlackjackStrategy( @@ -15,6 +16,7 @@ class BlackjackStrategy( fun drawCard(cards: List) { this.cards += cards + status = Hit() if (cards.size == FIRST_TURN_DRAW && isBlackjack()) { status = Blackjack() } diff --git a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt index 318d03489..f9789652e 100644 --- a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt +++ b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt @@ -6,6 +6,7 @@ import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.PictureCard import blackjack.participant.status.Blackjack +import blackjack.participant.status.Hit import io.kotest.matchers.types.shouldBeInstanceOf import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test @@ -23,4 +24,14 @@ class BlackjackStrategyTest { blackjackStrategy.status.shouldBeInstanceOf() } + @Test + fun `처음 두 장을 드로우 했을 때 합이 21이 아니면 히트 상태이다`() { + val blackjackStrategy = BlackjackStrategy(ScoreCalculator()) + blackjackStrategy.drawCard(listOf( + PictureCard(CardPicture.JACK, CardPattern.SPADE), + PictureCard(CardPicture.JACK, CardPattern.SPADE) + )) + + blackjackStrategy.status.shouldBeInstanceOf() + } } \ No newline at end of file From 532524de3d69426f10e654841efb95793b2ab207 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Wed, 6 Dec 2023 22:54:35 +0900 Subject: [PATCH 15/44] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=84=B0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/participant/BlackjackStrategy.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index f3622ee43..46971050f 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -10,18 +10,20 @@ class BlackjackStrategy( private val scoreCalculator: ScoreCalculator, ) { var cards: List = emptyList() - lateinit var status: Status + var status: Status = Hit() val isBust get() = resultScore() > BLACKJACK fun drawCard(cards: List) { this.cards += cards - status = Hit() - if (cards.size == FIRST_TURN_DRAW && isBlackjack()) { + + if (isFirstTurn(cards) && isBlackjack()) { status = Blackjack() } } + private fun isFirstTurn(cards: List): Boolean = cards.size == FIRST_TURN_DRAW + fun resultScore(): Int { return scoreCalculator.calculateGameScore(cards) } From f24fb805b274d8caf4d1308d067d36f8ca84feb0 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Wed, 6 Dec 2023 23:02:25 +0900 Subject: [PATCH 16/44] =?UTF-8?q?feat:=20=EC=8A=A4=ED=83=A0=EB=93=9C=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../participant/BlackjackStrategy.kt | 9 +++++ .../blackjack/participant/status/Stand.kt | 3 ++ .../participant/BlackjackStrategyTest.kt | 36 +++++++++++++------ 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/blackjack/participant/status/Stand.kt diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index 46971050f..7ab20e7b9 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -4,6 +4,7 @@ import blackjack.ScoreCalculator import blackjack.card.BlackJackCard import blackjack.participant.status.Blackjack import blackjack.participant.status.Hit +import blackjack.participant.status.Stand import blackjack.participant.status.Status class BlackjackStrategy( @@ -16,6 +17,9 @@ class BlackjackStrategy( fun drawCard(cards: List) { this.cards += cards + if (cards.size == NORMAL_DRAW && !isBust2()) { + status = Stand() + } if (isFirstTurn(cards) && isBlackjack()) { status = Blackjack() @@ -32,8 +36,13 @@ class BlackjackStrategy( return resultScore() == BLACKJACK } + private fun isBust2(): Boolean { + return resultScore() > BLACKJACK + } + companion object { private const val BLACKJACK: Int = 21 private const val FIRST_TURN_DRAW: Int = 2 + private const val NORMAL_DRAW: Int = 1 } } \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/status/Stand.kt b/src/main/kotlin/blackjack/participant/status/Stand.kt new file mode 100644 index 000000000..38363e7a2 --- /dev/null +++ b/src/main/kotlin/blackjack/participant/status/Stand.kt @@ -0,0 +1,3 @@ +package blackjack.participant.status + +class Stand : Status diff --git a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt index f9789652e..d8d25e4a7 100644 --- a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt +++ b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt @@ -7,8 +7,8 @@ import blackjack.card.CardPicture import blackjack.card.PictureCard import blackjack.participant.status.Blackjack import blackjack.participant.status.Hit +import blackjack.participant.status.Stand import io.kotest.matchers.types.shouldBeInstanceOf -import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test class BlackjackStrategyTest { @@ -16,10 +16,12 @@ class BlackjackStrategyTest { @Test fun `처음 두 장을 드로우 했을 때 합이 21이면 블랙잭상태이다`() { val blackjackStrategy = BlackjackStrategy(ScoreCalculator()) - blackjackStrategy.drawCard(listOf( - PictureCard(CardPicture.JACK, CardPattern.SPADE), - AceCard(CardPattern.SPADE), - )) + blackjackStrategy.drawCard( + listOf( + PictureCard(CardPicture.JACK, CardPattern.SPADE), + AceCard(CardPattern.SPADE), + ) + ) blackjackStrategy.status.shouldBeInstanceOf() } @@ -27,11 +29,25 @@ class BlackjackStrategyTest { @Test fun `처음 두 장을 드로우 했을 때 합이 21이 아니면 히트 상태이다`() { val blackjackStrategy = BlackjackStrategy(ScoreCalculator()) - blackjackStrategy.drawCard(listOf( - PictureCard(CardPicture.JACK, CardPattern.SPADE), - PictureCard(CardPicture.JACK, CardPattern.SPADE) - )) + blackjackStrategy.drawCard( + listOf( + PictureCard(CardPicture.JACK, CardPattern.SPADE), + PictureCard(CardPicture.JACK, CardPattern.SPADE) + ) + ) blackjackStrategy.status.shouldBeInstanceOf() } -} \ No newline at end of file + + @Test + fun `합이 21미만이고 더 이상 카드를 뽑지 않으면 스탠드 상태이다`() { + val blackjackStrategy = BlackjackStrategy(ScoreCalculator()) + blackjackStrategy.drawCard( + listOf( + PictureCard(CardPicture.JACK, CardPattern.SPADE) + ) + ) + + blackjackStrategy.status.shouldBeInstanceOf() + } +} From 9f18bccfda71afa82f0f5ce8c6b0badbb0749f71 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Wed, 6 Dec 2023 23:07:37 +0900 Subject: [PATCH 17/44] =?UTF-8?q?feat:=20=EB=B2=84=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/participant/BlackjackStrategy.kt | 8 ++++++-- .../kotlin/blackjack/participant/status/Bust.kt | 3 +++ .../participant/BlackjackStrategyTest.kt | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/blackjack/participant/status/Bust.kt diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index 7ab20e7b9..02629e2ad 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -3,6 +3,7 @@ package blackjack.participant import blackjack.ScoreCalculator import blackjack.card.BlackJackCard import blackjack.participant.status.Blackjack +import blackjack.participant.status.Bust import blackjack.participant.status.Hit import blackjack.participant.status.Stand import blackjack.participant.status.Status @@ -17,10 +18,14 @@ class BlackjackStrategy( fun drawCard(cards: List) { this.cards += cards - if (cards.size == NORMAL_DRAW && !isBust2()) { + if (!isFirstTurn(cards) && !isBust2()) { status = Stand() } + if (!isFirstTurn(cards) && isBust2()) { + status = Bust() + } + if (isFirstTurn(cards) && isBlackjack()) { status = Blackjack() } @@ -43,6 +48,5 @@ class BlackjackStrategy( companion object { private const val BLACKJACK: Int = 21 private const val FIRST_TURN_DRAW: Int = 2 - private const val NORMAL_DRAW: Int = 1 } } \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/status/Bust.kt b/src/main/kotlin/blackjack/participant/status/Bust.kt new file mode 100644 index 000000000..293ff982a --- /dev/null +++ b/src/main/kotlin/blackjack/participant/status/Bust.kt @@ -0,0 +1,3 @@ +package blackjack.participant.status + +class Bust : Status diff --git a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt index d8d25e4a7..c785fbb59 100644 --- a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt +++ b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt @@ -6,6 +6,7 @@ import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.PictureCard import blackjack.participant.status.Blackjack +import blackjack.participant.status.Bust import blackjack.participant.status.Hit import blackjack.participant.status.Stand import io.kotest.matchers.types.shouldBeInstanceOf @@ -50,4 +51,18 @@ class BlackjackStrategyTest { blackjackStrategy.status.shouldBeInstanceOf() } + + @Test + fun `합이 21이상이면 버스트 상태이다`() { + val blackjackStrategy = BlackjackStrategy(ScoreCalculator()) + blackjackStrategy.drawCard( + listOf( + PictureCard(CardPicture.JACK, CardPattern.SPADE), + PictureCard(CardPicture.JACK, CardPattern.SPADE), + PictureCard(CardPicture.JACK, CardPattern.SPADE) + ) + ) + + blackjackStrategy.status.shouldBeInstanceOf() + } } From 0742d5cdf4b9f787d07ac5bab62f5a7fc8e4dc23 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Wed, 6 Dec 2023 23:09:40 +0900 Subject: [PATCH 18/44] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=84=B0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../participant/BlackjackStrategy.kt | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index 02629e2ad..ba8ae857a 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -18,16 +18,15 @@ class BlackjackStrategy( fun drawCard(cards: List) { this.cards += cards - if (!isFirstTurn(cards) && !isBust2()) { - status = Stand() - } - if (!isFirstTurn(cards) && isBust2()) { - status = Bust() - } + changeStatus(cards) + } - if (isFirstTurn(cards) && isBlackjack()) { - status = Blackjack() + private fun changeStatus(cards: List) { + when { + !isFirstTurn(cards) && !isBust -> status = Stand() + !isFirstTurn(cards) && isBust -> status = Bust() + isFirstTurn(cards) && isBlackjack() -> status = Blackjack() } } @@ -41,12 +40,8 @@ class BlackjackStrategy( return resultScore() == BLACKJACK } - private fun isBust2(): Boolean { - return resultScore() > BLACKJACK - } - companion object { private const val BLACKJACK: Int = 21 private const val FIRST_TURN_DRAW: Int = 2 } -} \ No newline at end of file +} From 772fafc1809edcb5a94ca968d69c2dbd44c51d19 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Wed, 6 Dec 2023 23:11:17 +0900 Subject: [PATCH 19/44] doc: README.md update --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 16c36a304..2371134c2 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ - 입력 - [x] 게임에 참여할 사람을 입력받는다. - [x] 한장의 카드를 더 받을지 말지 입력받는다. - - [] 플레이어의 배팅 금액을 입력받는다. + - [x] 플레이어의 배팅 금액을 입력받는다. - 출력 - [x] 플레이어가 가지고 있는 카드를 출력한다. @@ -38,17 +38,17 @@ - [x] 딜러는 처음에 받은 2장의 합계가 16이하라면 반드시 한장의 카드를 받는다. - [x] 16점 이하면 계속 카드를 뽑는다. - [x] 17점 이상이면 카드를 추가로 받을 수 없다. - - [] 상태라는 클래스가 있다. - - [] 히트라는 상태가 있다 - - [] 히트는 먼저 받은 두 장의 합이 21에 못미치는 경우다. - - [] 버스트라는 상태가 있다. - - [] 버스트는 카드 총합이 21을 넘은 경우다. + - [x] 상태라는 클래스가 있다. + - [x] 히트라는 상태가 있다 + - [x] 히트는 먼저 받은 두 장의 합이 21에 못미치는 경우다. + - [x] 버스트라는 상태가 있다. + - [x] 버스트는 카드 총합이 21을 넘은 경우다. - [] 카드를 추가로 뽑아 21을 초과하는 경우 배팅 금액을 모두 잃는다. - [] 딜러가 버스트라면 남아 있는 플레이어들은 베팅 금액을 받는다. - - [] 스탠드라는 상태가 있다. - - [] 스탠드는 더 이상 카드를 뽑지 않는 경우다. - - [] 블랙잭 이라는 생태가 있다. - - [] 처음 받은 두 장의 카드의 총 합이 21이 되는 경우이다. + - [x] 스탠드라는 상태가 있다. + - [x] 스탠드는 더 이상 카드를 뽑지 않는 경우다. + - [x] 블랙잭 이라는 생태가 있다. + - [x] 처음 받은 두 장의 카드의 총 합이 21이 되는 경우이다. - [] 베팅 금액의 1.5배를 딜러에게 받는다. - [] 딜러와 플레이어 동시에 블랙잭인 경우 플레이어는 베팅한 금액을 돌려받는다. From d631fb56ef598b40a118a2796bf1e3c25a4ac6fc Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Wed, 6 Dec 2023 23:35:31 +0900 Subject: [PATCH 20/44] =?UTF-8?q?feat:=20=EB=8B=A4=EC=9D=8C=20=EC=9E=91?= =?UTF-8?q?=EC=97=85=EC=9D=84=20=EC=9C=84=ED=95=9C=20=EC=A4=91=EA=B0=84?= =?UTF-8?q?=EC=A0=80=EC=9E=A5...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/GameResult.kt | 18 ++++++++++++++++++ .../kotlin/blackjack/participant/Dealer.kt | 2 ++ .../kotlin/blackjack/participant/Player.kt | 2 ++ .../blackjack/participant/status/Status.kt | 4 ++++ 4 files changed, 26 insertions(+) diff --git a/src/main/kotlin/blackjack/GameResult.kt b/src/main/kotlin/blackjack/GameResult.kt index 4b34f4d84..1ba8af732 100644 --- a/src/main/kotlin/blackjack/GameResult.kt +++ b/src/main/kotlin/blackjack/GameResult.kt @@ -1,9 +1,11 @@ package blackjack +import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name import blackjack.participant.Player import blackjack.participant.Result +import blackjack.participant.status.Blackjack class GameResult( players: List, @@ -11,7 +13,14 @@ class GameResult( ) { val resultMap: Map + val resultMap2: Map + init { + if (dealer.status is Blackjack) { + + + } + val winner = findWinner(players, dealer) val loser = findLoser(players, dealer) @@ -20,9 +29,18 @@ class GameResult( winner.map { this[it.name] = Result.Win } } + val playerResult2 = mutableMapOf().apply { + loser.map { this[it.name] = it.status.calculateBettingAmount(Result.Lose, it.bettingAmount) } + winner.map { this[it.name] =it.status.calculateBettingAmount(Result.Win, it.bettingAmount) } + } + resultMap = mapOf( Name("딜러") to Result.DealerResult(loser.size, winner.size) ) + playerResult + + resultMap2 = mapOf( + Name("딜러") to dealer.status.calculateBettingAmount(Result.DealerResult(loser.size, winner.size),dealer.bettingAmount) + ) + playerResult2 } private fun findWinner(players: List, dealer: Dealer): List { diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index b8850da3d..333dd8407 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -13,6 +13,8 @@ class Dealer( val isBust get() = blackjackStrategy.isBust + val status get() = blackjackStrategy.status + fun drawCard(cards: List) { blackjackStrategy.drawCard(cards) } diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index b9851283c..255b185a1 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -17,6 +17,8 @@ class Player( val isBust get() = blackjackStrategy.isBust + val status get() = blackjackStrategy.status + fun drawCard(cards: List) { blackjackStrategy.drawCard(cards) } diff --git a/src/main/kotlin/blackjack/participant/status/Status.kt b/src/main/kotlin/blackjack/participant/status/Status.kt index c5899516a..bd3adf01e 100644 --- a/src/main/kotlin/blackjack/participant/status/Status.kt +++ b/src/main/kotlin/blackjack/participant/status/Status.kt @@ -1,4 +1,8 @@ package blackjack.participant.status +import blackjack.participant.BettingAmount +import blackjack.participant.Result + interface Status { + fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount } \ No newline at end of file From dc12e12a79171e8eeaa10b676ba7e6774b100179 Mon Sep 17 00:00:00 2001 From: Ted1_Lee Date: Thu, 7 Dec 2023 13:36:35 +0900 Subject: [PATCH 21/44] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/participant/BettingAmount.kt | 10 ++++++++++ src/main/kotlin/blackjack/participant/Player.kt | 1 - .../blackjack/participant/status/Blackjack.kt | 14 ++++++++++++++ .../kotlin/blackjack/participant/status/Bust.kt | 9 ++++++++- .../kotlin/blackjack/participant/status/Hit.kt | 14 ++++++++++++++ .../blackjack/participant/status/Stand.kt | 17 ++++++++++++++++- 6 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/BettingAmount.kt b/src/main/kotlin/blackjack/participant/BettingAmount.kt index ebd9ace9a..4929567a4 100644 --- a/src/main/kotlin/blackjack/participant/BettingAmount.kt +++ b/src/main/kotlin/blackjack/participant/BettingAmount.kt @@ -1,5 +1,7 @@ package blackjack.participant +import kotlin.math.roundToInt + @JvmInline value class BettingAmount( val amount: Int @@ -8,6 +10,14 @@ value class BettingAmount( require(amount >= 0) { VALID_MESSAGE } } + fun changeNegative(): BettingAmount { + return BettingAmount(amount * -1) + } + + fun winToBlackjack(): BettingAmount { + return BettingAmount((amount * 1.5).roundToInt()) + } + companion object { private const val VALID_MESSAGE: String = "베팅 금액은 0 이하일 수 없습니다." } diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index 255b185a1..385e7f8db 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -2,7 +2,6 @@ package blackjack.participant import blackjack.ScoreCalculator import blackjack.card.BlackJackCard -import blackjack.participant.status.Status class Player( val name: Name, diff --git a/src/main/kotlin/blackjack/participant/status/Blackjack.kt b/src/main/kotlin/blackjack/participant/status/Blackjack.kt index 5546fee90..dce7a0980 100644 --- a/src/main/kotlin/blackjack/participant/status/Blackjack.kt +++ b/src/main/kotlin/blackjack/participant/status/Blackjack.kt @@ -1,4 +1,18 @@ package blackjack.participant.status +import blackjack.participant.BettingAmount +import blackjack.participant.Result + class Blackjack : Status { + override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { + return when(result) { + is Result.Win -> bettingAmount.winToBlackjack() + is Result.Lose -> BettingAmount(0) + else -> throw IllegalArgumentException(ERROR_MESSAGE) + } + } + + companion object { + private const val ERROR_MESSAGE: String = "잘못된 값입니다." + } } \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/status/Bust.kt b/src/main/kotlin/blackjack/participant/status/Bust.kt index 293ff982a..44b381dee 100644 --- a/src/main/kotlin/blackjack/participant/status/Bust.kt +++ b/src/main/kotlin/blackjack/participant/status/Bust.kt @@ -1,3 +1,10 @@ package blackjack.participant.status -class Bust : Status +import blackjack.participant.BettingAmount +import blackjack.participant.Result + +class Bust : Status { + override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { + return bettingAmount.changeNegative() + } +} diff --git a/src/main/kotlin/blackjack/participant/status/Hit.kt b/src/main/kotlin/blackjack/participant/status/Hit.kt index e2007577a..e27515c94 100644 --- a/src/main/kotlin/blackjack/participant/status/Hit.kt +++ b/src/main/kotlin/blackjack/participant/status/Hit.kt @@ -1,4 +1,18 @@ package blackjack.participant.status +import blackjack.participant.BettingAmount +import blackjack.participant.Result + class Hit : Status { + override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { + return when(result) { + is Result.Win -> bettingAmount + is Result.Lose -> BettingAmount(0) + else -> throw IllegalArgumentException(ERROR_MESSAGE) + } + } + + companion object { + private const val ERROR_MESSAGE: String = "잘못된 값입니다." + } } \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/status/Stand.kt b/src/main/kotlin/blackjack/participant/status/Stand.kt index 38363e7a2..72a370052 100644 --- a/src/main/kotlin/blackjack/participant/status/Stand.kt +++ b/src/main/kotlin/blackjack/participant/status/Stand.kt @@ -1,3 +1,18 @@ package blackjack.participant.status -class Stand : Status +import blackjack.participant.BettingAmount +import blackjack.participant.Result + +class Stand : Status { + override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { + return when(result) { + is Result.Win -> bettingAmount + is Result.Lose -> BettingAmount(0) + else -> throw IllegalArgumentException(ERROR_MESSAGE) + } + } + + companion object { + private const val ERROR_MESSAGE: String = "잘못된 값입니다." + } +} From 85a1efb9253acd3d13341059aea6ce56b099ca34 Mon Sep 17 00:00:00 2001 From: Ted1_Lee Date: Thu, 7 Dec 2023 13:46:39 +0900 Subject: [PATCH 22/44] =?UTF-8?q?feat:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../participant/status/StatusTest.kt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/test/kotlin/blackjack/participant/status/StatusTest.kt diff --git a/src/test/kotlin/blackjack/participant/status/StatusTest.kt b/src/test/kotlin/blackjack/participant/status/StatusTest.kt new file mode 100644 index 000000000..b513320bd --- /dev/null +++ b/src/test/kotlin/blackjack/participant/status/StatusTest.kt @@ -0,0 +1,37 @@ +package blackjack.participant.status + +import blackjack.participant.BettingAmount +import blackjack.participant.Result +import org.junit.jupiter.api.Test +import io.kotest.matchers.shouldBe + +class StatusTest { + + @Test + fun `HIT 상태에서 이기면 배팅금액을 그대로 받는다`() { + val hit = Hit() + val result = hit.calculateBettingAmount(Result.Win, BettingAmount(1000)) + result.amount shouldBe 1000 + } + + @Test + fun `Stand 상태에서 이기면 배팅금액을 그대로 받는다`() { + val stand = Stand() + val result = stand.calculateBettingAmount(Result.Win, BettingAmount(1000)) + result.amount shouldBe 1000 + } + + @Test + fun `Bust 상태는 배팅 금액을 잃는다`() { + val bust = Bust() + val result = bust.calculateBettingAmount(Result.Win, BettingAmount(1000)) + result.amount shouldBe 0 + } + + @Test + fun `블랙잭 상태에서 이기면 배팅금액의 1점 5배를 받는다`() { + val blackjack = Blackjack() + val result = blackjack.calculateBettingAmount(Result.Win, BettingAmount(1000)) + result.amount shouldBe 1500 + } +} From 3464f6a231e7a3f440b258c87b8588530ec6b1d6 Mon Sep 17 00:00:00 2001 From: Ted1_Lee Date: Thu, 7 Dec 2023 17:31:49 +0900 Subject: [PATCH 23/44] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=EA=B0=80=20=ED=94=8C=EB=A0=88=EC=9D=B4=EC=96=B4=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=EC=9D=98=20=EC=A0=90=EC=88=98=EC=99=80=20?= =?UTF-8?q?=EC=9E=90=EC=8B=A0=EC=9D=98=20=EC=A0=90=EC=88=98=EB=A5=BC=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/participant/Dealer.kt | 17 ++++ .../kotlin/blackjack/domain/DealerTest.kt | 94 +++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index 333dd8407..69992a51c 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -2,6 +2,7 @@ package blackjack.participant import blackjack.ScoreCalculator import blackjack.card.BlackJackCard +import blackjack.participant.status.Bust class Dealer( scoreCalculator: ScoreCalculator @@ -27,6 +28,22 @@ class Dealer( return blackjackStrategy.resultScore() < MAX_DRAW_SCORE } + fun matchingScore(player: Player): Result { + if (player.isBust) { + return Result.Lose + } + + if (status is Bust) { + return Result.Win + } + + return when(resultScore() > player.resultScore()) { + true -> Result.Lose + else -> Result.Win + } + + } + companion object { private const val MAX_DRAW_SCORE: Int = 17 } diff --git a/src/test/kotlin/blackjack/domain/DealerTest.kt b/src/test/kotlin/blackjack/domain/DealerTest.kt index a5346e51d..1702add1a 100644 --- a/src/test/kotlin/blackjack/domain/DealerTest.kt +++ b/src/test/kotlin/blackjack/domain/DealerTest.kt @@ -2,9 +2,15 @@ package blackjack.domain import blackjack.ScoreCalculator import blackjack.card.CardPattern +import blackjack.card.CardPicture import blackjack.card.NormalCard +import blackjack.card.PictureCard import blackjack.participant.Dealer +import blackjack.participant.Name +import blackjack.participant.Player +import blackjack.participant.Result import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeInstanceOf import org.junit.jupiter.api.Test class DealerTest { @@ -35,4 +41,92 @@ class DealerTest { dealer.shouldDraw() shouldBe false } + + @Test + fun `딜러가 버스트 상태이고 플레이어가 버스트 상태가 아니라면 플레이어가 승리한다`() { + val dealer = Dealer(scoreCalculator) + dealer.drawCard( + listOf( + NormalCard(5, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + NormalCard(8, CardPattern.CLOVER), + ) + ) + + val player = Player(Name("홍길동"), scoreCalculator) + player.drawCard( + listOf( + PictureCard(CardPicture.KING, CardPattern.CLOVER), + PictureCard(CardPicture.KING, CardPattern.SPADE) + ) + ) + + dealer.matchingScore(player).shouldBeInstanceOf() + } + + @Test + fun `플레이어가 버스트 상태라면 플레이어가 패배한다`() { + val dealer = Dealer(scoreCalculator) + dealer.drawCard( + listOf( + NormalCard(5, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + NormalCard(8, CardPattern.CLOVER), + ) + ) + + val player = Player(Name("홍길동"), scoreCalculator) + player.drawCard( + listOf( + NormalCard(9, CardPattern.CLOVER), + PictureCard(CardPicture.KING, CardPattern.CLOVER), + PictureCard(CardPicture.KING, CardPattern.SPADE), + NormalCard(8, CardPattern.CLOVER), + ) + ) + + dealer.matchingScore(player).shouldBeInstanceOf() + } + + @Test + fun `플레이어가 점수가 딜러보다 높으면 플레이어가 승리한다`() { + val dealer = Dealer(scoreCalculator) + dealer.drawCard( + listOf( + NormalCard(5, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + ) + ) + + val player = Player(Name("홍길동"), scoreCalculator) + player.drawCard( + listOf( + NormalCard(9, CardPattern.CLOVER), + PictureCard(CardPicture.KING, CardPattern.CLOVER), + ) + ) + + dealer.matchingScore(player).shouldBeInstanceOf() + } + + @Test + fun `플레이어가 점수가 딜러보다 낮으면 플레이어가 패배한다`() { + val dealer = Dealer(scoreCalculator) + dealer.drawCard( + listOf( + NormalCard(5, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + ) + ) + + val player = Player(Name("홍길동"), scoreCalculator) + player.drawCard( + listOf( + NormalCard(4, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + ) + ) + + dealer.matchingScore(player).shouldBeInstanceOf() + } } From 243166db1e5a6c91947c12d2fe05bc55d7ab4b59 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Thu, 7 Dec 2023 23:06:32 +0900 Subject: [PATCH 24/44] =?UTF-8?q?feat:=20lint=20=EC=9E=91=EC=97=85=20?= =?UTF-8?q?=EB=B0=8F=20=EA=B9=A8=EC=A7=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=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/participant/BettingAmount.kt | 8 -------- src/main/kotlin/blackjack/participant/status/Hit.kt | 4 ++-- .../kotlin/blackjack/participant/BettingAmountTest.kt | 7 ++++--- .../kotlin/blackjack/participant/status/StatusTest.kt | 4 ++-- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/BettingAmount.kt b/src/main/kotlin/blackjack/participant/BettingAmount.kt index 4929567a4..42a232679 100644 --- a/src/main/kotlin/blackjack/participant/BettingAmount.kt +++ b/src/main/kotlin/blackjack/participant/BettingAmount.kt @@ -6,10 +6,6 @@ import kotlin.math.roundToInt value class BettingAmount( val amount: Int ) { - init { - require(amount >= 0) { VALID_MESSAGE } - } - fun changeNegative(): BettingAmount { return BettingAmount(amount * -1) } @@ -17,8 +13,4 @@ value class BettingAmount( fun winToBlackjack(): BettingAmount { return BettingAmount((amount * 1.5).roundToInt()) } - - companion object { - private const val VALID_MESSAGE: String = "베팅 금액은 0 이하일 수 없습니다." - } } diff --git a/src/main/kotlin/blackjack/participant/status/Hit.kt b/src/main/kotlin/blackjack/participant/status/Hit.kt index e27515c94..ddcef2ea6 100644 --- a/src/main/kotlin/blackjack/participant/status/Hit.kt +++ b/src/main/kotlin/blackjack/participant/status/Hit.kt @@ -5,7 +5,7 @@ import blackjack.participant.Result class Hit : Status { override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { - return when(result) { + return when (result) { is Result.Win -> bettingAmount is Result.Lose -> BettingAmount(0) else -> throw IllegalArgumentException(ERROR_MESSAGE) @@ -15,4 +15,4 @@ class Hit : Status { companion object { private const val ERROR_MESSAGE: String = "잘못된 값입니다." } -} \ No newline at end of file +} diff --git a/src/test/kotlin/blackjack/participant/BettingAmountTest.kt b/src/test/kotlin/blackjack/participant/BettingAmountTest.kt index 7f7fe9e56..a8fb7211e 100644 --- a/src/test/kotlin/blackjack/participant/BettingAmountTest.kt +++ b/src/test/kotlin/blackjack/participant/BettingAmountTest.kt @@ -1,12 +1,13 @@ package blackjack.participant -import io.kotest.assertions.throwables.shouldThrow +import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test class BettingAmountTest { @Test - fun `베팅 금액은 0이하일 수 없다`() { - shouldThrow { BettingAmount(-1) } + fun `베팅 금액은 금액을 가진다`() { + val amount = BettingAmount(1000) + amount.amount shouldBe 1000 } } diff --git a/src/test/kotlin/blackjack/participant/status/StatusTest.kt b/src/test/kotlin/blackjack/participant/status/StatusTest.kt index b513320bd..993f17217 100644 --- a/src/test/kotlin/blackjack/participant/status/StatusTest.kt +++ b/src/test/kotlin/blackjack/participant/status/StatusTest.kt @@ -2,8 +2,8 @@ package blackjack.participant.status import blackjack.participant.BettingAmount import blackjack.participant.Result -import org.junit.jupiter.api.Test import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test class StatusTest { @@ -25,7 +25,7 @@ class StatusTest { fun `Bust 상태는 배팅 금액을 잃는다`() { val bust = Bust() val result = bust.calculateBettingAmount(Result.Win, BettingAmount(1000)) - result.amount shouldBe 0 + result.amount shouldBe -1000 } @Test From ad1a372e10fea8a814653b7790ac9932e629e68d Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Thu, 7 Dec 2023 23:42:31 +0900 Subject: [PATCH 25/44] =?UTF-8?q?feat:=20=EB=B2=A0=ED=8C=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EA=B3=84=EC=82=B0=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/GameResult.kt | 52 +++---------------- .../blackjack/participant/BettingAmount.kt | 8 +++ .../kotlin/blackjack/participant/Dealer.kt | 22 ++++++-- .../blackjack/participant/status/Hit.kt | 2 +- .../blackjack/participant/status/Stand.kt | 2 +- 5 files changed, 35 insertions(+), 51 deletions(-) diff --git a/src/main/kotlin/blackjack/GameResult.kt b/src/main/kotlin/blackjack/GameResult.kt index 1ba8af732..64f4a0d98 100644 --- a/src/main/kotlin/blackjack/GameResult.kt +++ b/src/main/kotlin/blackjack/GameResult.kt @@ -4,61 +4,21 @@ import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name import blackjack.participant.Player -import blackjack.participant.Result -import blackjack.participant.status.Blackjack class GameResult( players: List, dealer: Dealer ) { - val resultMap: Map - - val resultMap2: Map + val resultMap: Map init { - if (dealer.status is Blackjack) { - - - } - - val winner = findWinner(players, dealer) - val loser = findLoser(players, dealer) + val playerResult = mutableMapOf() - val playerResult = mutableMapOf().apply { - loser.map { this[it.name] = Result.Lose } - winner.map { this[it.name] = Result.Win } + players.forEach { + val result = dealer.matchingScore(it) + playerResult[it.name] = it.status.calculateBettingAmount(result, it.bettingAmount) } - val playerResult2 = mutableMapOf().apply { - loser.map { this[it.name] = it.status.calculateBettingAmount(Result.Lose, it.bettingAmount) } - winner.map { this[it.name] =it.status.calculateBettingAmount(Result.Win, it.bettingAmount) } - } - - resultMap = mapOf( - Name("딜러") to Result.DealerResult(loser.size, winner.size) - ) + playerResult - - resultMap2 = mapOf( - Name("딜러") to dealer.status.calculateBettingAmount(Result.DealerResult(loser.size, winner.size),dealer.bettingAmount) - ) + playerResult2 - } - - private fun findWinner(players: List, dealer: Dealer): List { - if (dealer.isBust) { - return players.filter { !it.isBust } - } - - return players.filter { !it.isBust }.filter { it.resultScore() > dealer.resultScore() } - } - - private fun findLoser(players: List, dealer: Dealer): List { - if (dealer.isBust) { - return players.filter { it.isBust } - } - - val bustPlayer = players.filter { it.isBust } - val loser = players.filter { !it.isBust }.filter { it.resultScore() < dealer.resultScore() } - - return bustPlayer + loser + resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + playerResult } } diff --git a/src/main/kotlin/blackjack/participant/BettingAmount.kt b/src/main/kotlin/blackjack/participant/BettingAmount.kt index 42a232679..7e5be591c 100644 --- a/src/main/kotlin/blackjack/participant/BettingAmount.kt +++ b/src/main/kotlin/blackjack/participant/BettingAmount.kt @@ -13,4 +13,12 @@ value class BettingAmount( fun winToBlackjack(): BettingAmount { return BettingAmount((amount * 1.5).roundToInt()) } + + fun plusAmount(bettingAmount: BettingAmount): BettingAmount { + return BettingAmount(amount + bettingAmount.amount) + } + + fun minusAmount(bettingAmount: BettingAmount): BettingAmount { + return BettingAmount(amount - bettingAmount.amount) + } } diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index 69992a51c..56ea42426 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -2,13 +2,14 @@ package blackjack.participant import blackjack.ScoreCalculator import blackjack.card.BlackJackCard +import blackjack.participant.status.Blackjack import blackjack.participant.status.Bust class Dealer( scoreCalculator: ScoreCalculator ) { private val blackjackStrategy: BlackjackStrategy = BlackjackStrategy(scoreCalculator) - val bettingAmount: BettingAmount = BettingAmount(0) + var bettingAmount: BettingAmount = BettingAmount(0) val cards get() = blackjackStrategy.cards @@ -29,19 +30,34 @@ class Dealer( } fun matchingScore(player: Player): Result { + if (player.status is Blackjack) { + return when (status) { + is Blackjack -> Result.Lose + else -> { + bettingAmount = bettingAmount.minusAmount(player.status.calculateBettingAmount(Result.Win, player.bettingAmount)) + return Result.Win + } + } + } + + if (player.isBust) { + bettingAmount = bettingAmount.plusAmount(player.bettingAmount) return Result.Lose } if (status is Bust) { + bettingAmount = bettingAmount.minusAmount(player.bettingAmount) return Result.Win } return when(resultScore() > player.resultScore()) { - true -> Result.Lose + true -> { + bettingAmount = bettingAmount.plusAmount(player.bettingAmount) + Result.Lose + } else -> Result.Win } - } companion object { diff --git a/src/main/kotlin/blackjack/participant/status/Hit.kt b/src/main/kotlin/blackjack/participant/status/Hit.kt index ddcef2ea6..0074eda10 100644 --- a/src/main/kotlin/blackjack/participant/status/Hit.kt +++ b/src/main/kotlin/blackjack/participant/status/Hit.kt @@ -7,7 +7,7 @@ class Hit : Status { override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { return when (result) { is Result.Win -> bettingAmount - is Result.Lose -> BettingAmount(0) + is Result.Lose -> bettingAmount.changeNegative() else -> throw IllegalArgumentException(ERROR_MESSAGE) } } diff --git a/src/main/kotlin/blackjack/participant/status/Stand.kt b/src/main/kotlin/blackjack/participant/status/Stand.kt index 72a370052..e98c3e7ce 100644 --- a/src/main/kotlin/blackjack/participant/status/Stand.kt +++ b/src/main/kotlin/blackjack/participant/status/Stand.kt @@ -7,7 +7,7 @@ class Stand : Status { override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { return when(result) { is Result.Win -> bettingAmount - is Result.Lose -> BettingAmount(0) + is Result.Lose -> bettingAmount.changeNegative() else -> throw IllegalArgumentException(ERROR_MESSAGE) } } From e90f8a63aa8905681d7bee45cb9960a8ea1d81ff Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Thu, 7 Dec 2023 23:43:14 +0900 Subject: [PATCH 26/44] =?UTF-8?q?feat:=20=EB=A6=B0=ED=8A=B8=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/participant/Dealer.kt | 13 +++++++++---- src/main/kotlin/blackjack/ui/OutputManager.kt | 13 +------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index 56ea42426..647126183 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -7,7 +7,7 @@ import blackjack.participant.status.Bust class Dealer( scoreCalculator: ScoreCalculator -) { +) { private val blackjackStrategy: BlackjackStrategy = BlackjackStrategy(scoreCalculator) var bettingAmount: BettingAmount = BettingAmount(0) @@ -34,13 +34,17 @@ class Dealer( return when (status) { is Blackjack -> Result.Lose else -> { - bettingAmount = bettingAmount.minusAmount(player.status.calculateBettingAmount(Result.Win, player.bettingAmount)) + bettingAmount = bettingAmount.minusAmount( + player.status.calculateBettingAmount( + Result.Win, + player.bettingAmount + ) + ) return Result.Win } } } - if (player.isBust) { bettingAmount = bettingAmount.plusAmount(player.bettingAmount) return Result.Lose @@ -51,11 +55,12 @@ class Dealer( return Result.Win } - return when(resultScore() > player.resultScore()) { + return when (resultScore() > player.resultScore()) { true -> { bettingAmount = bettingAmount.plusAmount(player.bettingAmount) Result.Lose } + else -> Result.Win } } diff --git a/src/main/kotlin/blackjack/ui/OutputManager.kt b/src/main/kotlin/blackjack/ui/OutputManager.kt index fddebdb0b..557e203bf 100644 --- a/src/main/kotlin/blackjack/ui/OutputManager.kt +++ b/src/main/kotlin/blackjack/ui/OutputManager.kt @@ -9,7 +9,6 @@ import blackjack.card.NormalCard import blackjack.card.PictureCard import blackjack.participant.Dealer import blackjack.participant.Player -import blackjack.participant.Result class OutputManager { fun printPlayersAndDealerCards(players: List, dealer: Dealer) { @@ -39,15 +38,7 @@ class OutputManager { printEnter() println(RESULT_MESSAGE) gameResult.resultMap.forEach { - println("${it.key.value} : ${parsingGameResult(it.value)}") - } - } - - private fun parsingGameResult(result: Result): String { - return when(result) { - is Result.Win -> WIN - is Result.Lose -> LOSE - is Result.DealerResult -> "${result.win} $WIN ${result.lose} $LOSE" + println("${it.key.value} : ${it.value.amount}") } } @@ -102,8 +93,6 @@ class OutputManager { companion object { private const val DEALER_MESSAGE = "딜러는 16이하라 한장의 카드를 더 받았습니다." - private const val WIN = "승" - private const val LOSE = "패" private const val RESULT_MESSAGE = "## 최종승패" } } From af98d6a53fdd067884d54f0d109a29e6547ea82a Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Thu, 7 Dec 2023 23:46:15 +0900 Subject: [PATCH 27/44] =?UTF-8?q?test:=20=EA=B9=A8=EC=A7=84=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=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/participant/Result.kt | 1 - src/test/kotlin/blackjack/GameResultTest.kt | 14 ++++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/Result.kt b/src/main/kotlin/blackjack/participant/Result.kt index 38e682a35..8d6a92261 100644 --- a/src/main/kotlin/blackjack/participant/Result.kt +++ b/src/main/kotlin/blackjack/participant/Result.kt @@ -3,5 +3,4 @@ package blackjack.participant sealed interface Result { object Win : Result object Lose : Result - class DealerResult(val win: Int, val lose: Int) : Result } diff --git a/src/test/kotlin/blackjack/GameResultTest.kt b/src/test/kotlin/blackjack/GameResultTest.kt index 3801e2af6..2437a9c2c 100644 --- a/src/test/kotlin/blackjack/GameResultTest.kt +++ b/src/test/kotlin/blackjack/GameResultTest.kt @@ -4,10 +4,12 @@ import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.NormalCard import blackjack.card.PictureCard +import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name import blackjack.participant.Player import blackjack.participant.Result +import io.kotest.matchers.shouldBe import io.kotest.matchers.types.shouldBeInstanceOf import org.junit.jupiter.api.Test @@ -15,9 +17,9 @@ class GameResultTest { private val scoreCalculator: ScoreCalculator = ScoreCalculator() @Test - fun `딜러가 버스트면 해당 시점에 살아있는 나머지 사용자는 승리한다`() { + fun `딜러가 버스트면 해당 시점에 살아있는 나머지 사용자는 베팅 금액을 잃지 않는다`() { val name = "홍길동" - val player = Player(Name(name), scoreCalculator) + val player = Player(Name(name), BettingAmount(1000)) player.drawCard( listOf( NormalCard(9, CardPattern.CLOVER), @@ -34,13 +36,13 @@ class GameResultTest { ) val result = GameResult(listOf(player), dealer) - result.resultMap[player.name].shouldBeInstanceOf() + result.resultMap[player.name]?.amount shouldBe 1000 } @Test - fun `딜러가 버스트여도 사용자가 버스트이면 사용자는 패배한다`() { + fun `딜러가 버스트여도 사용자가 버스트이면 사용자는 배팅 금액을 잃는다`() { val name = "홍길동" - val player = Player(Name(name), scoreCalculator) + val player = Player(Name(name), BettingAmount(1000)) player.drawCard( listOf( PictureCard(CardPicture.KING, CardPattern.CLOVER), @@ -58,6 +60,6 @@ class GameResultTest { ) val result = GameResult(listOf(player), dealer) - result.resultMap[player.name].shouldBeInstanceOf() + result.resultMap[player.name]?.amount shouldBe -1000 } } From 68ed8cb0505c65dcbb1bdee88d99ad20a28b6142 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Thu, 7 Dec 2023 23:47:09 +0900 Subject: [PATCH 28/44] =?UTF-8?q?feat:=20=EC=95=88=EC=93=B0=EB=8A=94=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=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/participant/status/Blackjack.kt | 7 +------ src/main/kotlin/blackjack/participant/status/Hit.kt | 5 ----- src/main/kotlin/blackjack/participant/status/Stand.kt | 5 ----- 3 files changed, 1 insertion(+), 16 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/status/Blackjack.kt b/src/main/kotlin/blackjack/participant/status/Blackjack.kt index dce7a0980..a2e19e658 100644 --- a/src/main/kotlin/blackjack/participant/status/Blackjack.kt +++ b/src/main/kotlin/blackjack/participant/status/Blackjack.kt @@ -8,11 +8,6 @@ class Blackjack : Status { return when(result) { is Result.Win -> bettingAmount.winToBlackjack() is Result.Lose -> BettingAmount(0) - else -> throw IllegalArgumentException(ERROR_MESSAGE) } } - - companion object { - private const val ERROR_MESSAGE: String = "잘못된 값입니다." - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/blackjack/participant/status/Hit.kt b/src/main/kotlin/blackjack/participant/status/Hit.kt index 0074eda10..f67dabcde 100644 --- a/src/main/kotlin/blackjack/participant/status/Hit.kt +++ b/src/main/kotlin/blackjack/participant/status/Hit.kt @@ -8,11 +8,6 @@ class Hit : Status { return when (result) { is Result.Win -> bettingAmount is Result.Lose -> bettingAmount.changeNegative() - else -> throw IllegalArgumentException(ERROR_MESSAGE) } } - - companion object { - private const val ERROR_MESSAGE: String = "잘못된 값입니다." - } } diff --git a/src/main/kotlin/blackjack/participant/status/Stand.kt b/src/main/kotlin/blackjack/participant/status/Stand.kt index e98c3e7ce..b35d94dfd 100644 --- a/src/main/kotlin/blackjack/participant/status/Stand.kt +++ b/src/main/kotlin/blackjack/participant/status/Stand.kt @@ -8,11 +8,6 @@ class Stand : Status { return when(result) { is Result.Win -> bettingAmount is Result.Lose -> bettingAmount.changeNegative() - else -> throw IllegalArgumentException(ERROR_MESSAGE) } } - - companion object { - private const val ERROR_MESSAGE: String = "잘못된 값입니다." - } } From 46af015fda1e296a1578a62e6040669a1bacc0cc Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Thu, 7 Dec 2023 23:52:29 +0900 Subject: [PATCH 29/44] =?UTF-8?q?feat:=20isBust=EB=A5=BC=20Status=EB=A5=BC?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8A=94=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=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/participant/BlackjackStrategy.kt | 1 - src/main/kotlin/blackjack/participant/Dealer.kt | 4 +--- src/main/kotlin/blackjack/participant/Player.kt | 2 -- src/test/kotlin/blackjack/domain/PlayerTest.kt | 4 +++- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index ba8ae857a..b1c3ed05d 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -18,7 +18,6 @@ class BlackjackStrategy( fun drawCard(cards: List) { this.cards += cards - changeStatus(cards) } diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index 647126183..7b94268b9 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -13,8 +13,6 @@ class Dealer( val cards get() = blackjackStrategy.cards - val isBust get() = blackjackStrategy.isBust - val status get() = blackjackStrategy.status fun drawCard(cards: List) { @@ -45,7 +43,7 @@ class Dealer( } } - if (player.isBust) { + if (player.status is Bust) { bettingAmount = bettingAmount.plusAmount(player.bettingAmount) return Result.Lose } diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index 385e7f8db..d3532d656 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -14,8 +14,6 @@ class Player( val cards get() = blackjackStrategy.cards - val isBust get() = blackjackStrategy.isBust - val status get() = blackjackStrategy.status fun drawCard(cards: List) { diff --git a/src/test/kotlin/blackjack/domain/PlayerTest.kt b/src/test/kotlin/blackjack/domain/PlayerTest.kt index ab3617e6b..d9d71374a 100644 --- a/src/test/kotlin/blackjack/domain/PlayerTest.kt +++ b/src/test/kotlin/blackjack/domain/PlayerTest.kt @@ -8,7 +8,9 @@ import blackjack.card.NormalCard import blackjack.card.PictureCard import blackjack.participant.Name import blackjack.participant.Player +import blackjack.participant.status.Bust import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeInstanceOf import org.junit.jupiter.api.Test class PlayerTest { @@ -72,6 +74,6 @@ class PlayerTest { ) ) - player.isBust shouldBe true + player.status.shouldBeInstanceOf() } } From 353af0a5ec1b2a524beb9832bc3e9df330de5911 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Thu, 7 Dec 2023 23:52:57 +0900 Subject: [PATCH 30/44] feat: ktlint --- src/test/kotlin/blackjack/GameResultTest.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/kotlin/blackjack/GameResultTest.kt b/src/test/kotlin/blackjack/GameResultTest.kt index 2437a9c2c..0e2663dbb 100644 --- a/src/test/kotlin/blackjack/GameResultTest.kt +++ b/src/test/kotlin/blackjack/GameResultTest.kt @@ -8,9 +8,7 @@ import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name import blackjack.participant.Player -import blackjack.participant.Result import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeInstanceOf import org.junit.jupiter.api.Test class GameResultTest { From cbf31f3bcad8ce3cfa3483c707300c7a88b886d6 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Sat, 9 Dec 2023 21:54:02 +0900 Subject: [PATCH 31/44] =?UTF-8?q?feat:=20mutableMapOf=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=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/GameResult.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/blackjack/GameResult.kt b/src/main/kotlin/blackjack/GameResult.kt index 64f4a0d98..bfdf18b97 100644 --- a/src/main/kotlin/blackjack/GameResult.kt +++ b/src/main/kotlin/blackjack/GameResult.kt @@ -12,13 +12,11 @@ class GameResult( val resultMap: Map init { - val playerResult = mutableMapOf() - - players.forEach { - val result = dealer.matchingScore(it) - playerResult[it.name] = it.status.calculateBettingAmount(result, it.bettingAmount) - } - - resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + playerResult + resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + + players.map { player -> + val result = dealer.matchingScore(player) + val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) + player.name to bettingAmount + } } } From 6a4649689587562653301751480e476e9bf30efc Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Sat, 9 Dec 2023 22:52:50 +0900 Subject: [PATCH 32/44] =?UTF-8?q?feat:=20isBust=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=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 --- .../kotlin/blackjack/participant/BlackjackStrategy.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index b1c3ed05d..79c895242 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -14,7 +14,9 @@ class BlackjackStrategy( var cards: List = emptyList() var status: Status = Hit() - val isBust get() = resultScore() > BLACKJACK + fun isBust(): Boolean { + return resultScore() > BLACKJACK + } fun drawCard(cards: List) { this.cards += cards @@ -23,8 +25,8 @@ class BlackjackStrategy( private fun changeStatus(cards: List) { when { - !isFirstTurn(cards) && !isBust -> status = Stand() - !isFirstTurn(cards) && isBust -> status = Bust() + !isFirstTurn(cards) && !isBust() -> status = Stand() + !isFirstTurn(cards) && isBust() -> status = Bust() isFirstTurn(cards) && isBlackjack() -> status = Blackjack() } } From 9caaa16f527802aaf9a7888c447679dda1a9e5b7 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Sun, 10 Dec 2023 00:12:02 +0900 Subject: [PATCH 33/44] =?UTF-8?q?feat:=20DSL=EA=B3=BC=20operator=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/blackjack/participant/BettingAmount.kt | 15 ++++++++++----- src/main/kotlin/blackjack/participant/Dealer.kt | 4 ++-- src/main/kotlin/blackjack/participant/Player.kt | 2 +- .../kotlin/blackjack/participant/status/Bust.kt | 2 +- .../kotlin/blackjack/participant/status/Hit.kt | 2 +- .../kotlin/blackjack/participant/status/Stand.kt | 2 +- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/BettingAmount.kt b/src/main/kotlin/blackjack/participant/BettingAmount.kt index 7e5be591c..c27c54de1 100644 --- a/src/main/kotlin/blackjack/participant/BettingAmount.kt +++ b/src/main/kotlin/blackjack/participant/BettingAmount.kt @@ -6,19 +6,24 @@ import kotlin.math.roundToInt value class BettingAmount( val amount: Int ) { - fun changeNegative(): BettingAmount { - return BettingAmount(amount * -1) - } +// fun changeNegative(): BettingAmount { +// return BettingAmount(amount * -1) +// } fun winToBlackjack(): BettingAmount { return BettingAmount((amount * 1.5).roundToInt()) } - fun plusAmount(bettingAmount: BettingAmount): BettingAmount { - return BettingAmount(amount + bettingAmount.amount) + operator fun unaryMinus(): BettingAmount { + return BettingAmount(amount * -1) } + fun minusAmount(bettingAmount: BettingAmount): BettingAmount { return BettingAmount(amount - bettingAmount.amount) } } + +infix operator fun BettingAmount.plus(bettingAmount: BettingAmount): BettingAmount { + return BettingAmount(amount + bettingAmount.amount) +} \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index 7b94268b9..fc034552a 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -44,7 +44,7 @@ class Dealer( } if (player.status is Bust) { - bettingAmount = bettingAmount.plusAmount(player.bettingAmount) + bettingAmount += player.bettingAmount return Result.Lose } @@ -55,7 +55,7 @@ class Dealer( return when (resultScore() > player.resultScore()) { true -> { - bettingAmount = bettingAmount.plusAmount(player.bettingAmount) + bettingAmount += player.bettingAmount Result.Lose } diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index d3532d656..cfa1190ae 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -25,6 +25,6 @@ class Player( } fun shouldDraw(): Boolean { - return !blackjackStrategy.isBust + return !blackjackStrategy.isBust() } } diff --git a/src/main/kotlin/blackjack/participant/status/Bust.kt b/src/main/kotlin/blackjack/participant/status/Bust.kt index 44b381dee..f6821e3fc 100644 --- a/src/main/kotlin/blackjack/participant/status/Bust.kt +++ b/src/main/kotlin/blackjack/participant/status/Bust.kt @@ -5,6 +5,6 @@ import blackjack.participant.Result class Bust : Status { override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { - return bettingAmount.changeNegative() + return -bettingAmount } } diff --git a/src/main/kotlin/blackjack/participant/status/Hit.kt b/src/main/kotlin/blackjack/participant/status/Hit.kt index f67dabcde..5490d3005 100644 --- a/src/main/kotlin/blackjack/participant/status/Hit.kt +++ b/src/main/kotlin/blackjack/participant/status/Hit.kt @@ -7,7 +7,7 @@ class Hit : Status { override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { return when (result) { is Result.Win -> bettingAmount - is Result.Lose -> bettingAmount.changeNegative() + is Result.Lose -> -bettingAmount } } } diff --git a/src/main/kotlin/blackjack/participant/status/Stand.kt b/src/main/kotlin/blackjack/participant/status/Stand.kt index b35d94dfd..ef592412e 100644 --- a/src/main/kotlin/blackjack/participant/status/Stand.kt +++ b/src/main/kotlin/blackjack/participant/status/Stand.kt @@ -7,7 +7,7 @@ class Stand : Status { override fun calculateBettingAmount(result: Result, bettingAmount: BettingAmount): BettingAmount { return when(result) { is Result.Win -> bettingAmount - is Result.Lose -> bettingAmount.changeNegative() + is Result.Lose -> -bettingAmount } } } From 9eee8bb8c86afc0edda9d10bb7b71595a4954b0d Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Sun, 10 Dec 2023 00:18:28 +0900 Subject: [PATCH 34/44] =?UTF-8?q?feat:=20DSL=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/GameResult.kt | 11 +++++------ .../kotlin/blackjack/participant/BettingAmount.kt | 11 +++++------ src/main/kotlin/blackjack/participant/Dealer.kt | 7 +++---- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/blackjack/GameResult.kt b/src/main/kotlin/blackjack/GameResult.kt index bfdf18b97..101a2ebfc 100644 --- a/src/main/kotlin/blackjack/GameResult.kt +++ b/src/main/kotlin/blackjack/GameResult.kt @@ -12,11 +12,10 @@ class GameResult( val resultMap: Map init { - resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + - players.map { player -> - val result = dealer.matchingScore(player) - val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) - player.name to bettingAmount - } + resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + players.map { player -> + val result = dealer.matchingScore(player) + val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) + player.name to bettingAmount + } } } diff --git a/src/main/kotlin/blackjack/participant/BettingAmount.kt b/src/main/kotlin/blackjack/participant/BettingAmount.kt index c27c54de1..4cced4d3c 100644 --- a/src/main/kotlin/blackjack/participant/BettingAmount.kt +++ b/src/main/kotlin/blackjack/participant/BettingAmount.kt @@ -17,13 +17,12 @@ value class BettingAmount( operator fun unaryMinus(): BettingAmount { return BettingAmount(amount * -1) } - - - fun minusAmount(bettingAmount: BettingAmount): BettingAmount { - return BettingAmount(amount - bettingAmount.amount) - } } infix operator fun BettingAmount.plus(bettingAmount: BettingAmount): BettingAmount { return BettingAmount(amount + bettingAmount.amount) -} \ No newline at end of file +} + +infix operator fun BettingAmount.minus(bettingAmount: BettingAmount): BettingAmount { + return BettingAmount(amount - bettingAmount.amount) +} diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index fc034552a..b3795c298 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -32,12 +32,11 @@ class Dealer( return when (status) { is Blackjack -> Result.Lose else -> { - bettingAmount = bettingAmount.minusAmount( - player.status.calculateBettingAmount( + bettingAmount -= player.status.calculateBettingAmount( Result.Win, player.bettingAmount ) - ) + return Result.Win } } @@ -49,7 +48,7 @@ class Dealer( } if (status is Bust) { - bettingAmount = bettingAmount.minusAmount(player.bettingAmount) + bettingAmount -= player.bettingAmount return Result.Win } From b662b3678b2736fdcf8c14a864fc3fb8a52ee8be Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Sun, 10 Dec 2023 00:26:22 +0900 Subject: [PATCH 35/44] =?UTF-8?q?feat:=20game=20package=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/GameManager.kt | 2 ++ src/main/kotlin/blackjack/{ => game}/GameResult.kt | 2 +- src/main/kotlin/blackjack/{ => game}/ScoreCalculator.kt | 2 +- src/main/kotlin/blackjack/participant/BlackjackStrategy.kt | 2 +- src/main/kotlin/blackjack/participant/Dealer.kt | 2 +- src/main/kotlin/blackjack/participant/Player.kt | 2 +- src/main/kotlin/blackjack/ui/OutputManager.kt | 2 +- src/test/kotlin/blackjack/GameResultTest.kt | 2 ++ src/test/kotlin/blackjack/ScoreCalculatorTest.kt | 1 + src/test/kotlin/blackjack/domain/DealerTest.kt | 2 +- src/test/kotlin/blackjack/domain/PlayerTest.kt | 2 +- src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt | 2 +- 12 files changed, 14 insertions(+), 9 deletions(-) rename src/main/kotlin/blackjack/{ => game}/GameResult.kt (96%) rename src/main/kotlin/blackjack/{ => game}/ScoreCalculator.kt (98%) diff --git a/src/main/kotlin/blackjack/GameManager.kt b/src/main/kotlin/blackjack/GameManager.kt index 6bc7609d2..e8d79bb7a 100644 --- a/src/main/kotlin/blackjack/GameManager.kt +++ b/src/main/kotlin/blackjack/GameManager.kt @@ -1,6 +1,8 @@ package blackjack import blackjack.card.CardDeck +import blackjack.game.GameResult +import blackjack.game.ScoreCalculator import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name diff --git a/src/main/kotlin/blackjack/GameResult.kt b/src/main/kotlin/blackjack/game/GameResult.kt similarity index 96% rename from src/main/kotlin/blackjack/GameResult.kt rename to src/main/kotlin/blackjack/game/GameResult.kt index 101a2ebfc..17f4cfc7a 100644 --- a/src/main/kotlin/blackjack/GameResult.kt +++ b/src/main/kotlin/blackjack/game/GameResult.kt @@ -1,4 +1,4 @@ -package blackjack +package blackjack.game import blackjack.participant.BettingAmount import blackjack.participant.Dealer diff --git a/src/main/kotlin/blackjack/ScoreCalculator.kt b/src/main/kotlin/blackjack/game/ScoreCalculator.kt similarity index 98% rename from src/main/kotlin/blackjack/ScoreCalculator.kt rename to src/main/kotlin/blackjack/game/ScoreCalculator.kt index c459aea7b..52adfacc6 100644 --- a/src/main/kotlin/blackjack/ScoreCalculator.kt +++ b/src/main/kotlin/blackjack/game/ScoreCalculator.kt @@ -1,4 +1,4 @@ -package blackjack +package blackjack.game import blackjack.card.AceCard import blackjack.card.BlackJackCard diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index 79c895242..94abaa72b 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -1,6 +1,6 @@ package blackjack.participant -import blackjack.ScoreCalculator +import blackjack.game.ScoreCalculator import blackjack.card.BlackJackCard import blackjack.participant.status.Blackjack import blackjack.participant.status.Bust diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index b3795c298..33edfa68f 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -1,6 +1,6 @@ package blackjack.participant -import blackjack.ScoreCalculator +import blackjack.game.ScoreCalculator import blackjack.card.BlackJackCard import blackjack.participant.status.Blackjack import blackjack.participant.status.Bust diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index cfa1190ae..337b15785 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -1,6 +1,6 @@ package blackjack.participant -import blackjack.ScoreCalculator +import blackjack.game.ScoreCalculator import blackjack.card.BlackJackCard class Player( diff --git a/src/main/kotlin/blackjack/ui/OutputManager.kt b/src/main/kotlin/blackjack/ui/OutputManager.kt index 557e203bf..5280ef4b2 100644 --- a/src/main/kotlin/blackjack/ui/OutputManager.kt +++ b/src/main/kotlin/blackjack/ui/OutputManager.kt @@ -1,6 +1,6 @@ package blackjack.ui -import blackjack.GameResult +import blackjack.game.GameResult import blackjack.card.AceCard import blackjack.card.BlackJackCard import blackjack.card.CardPattern diff --git a/src/test/kotlin/blackjack/GameResultTest.kt b/src/test/kotlin/blackjack/GameResultTest.kt index 0e2663dbb..d9ed84dca 100644 --- a/src/test/kotlin/blackjack/GameResultTest.kt +++ b/src/test/kotlin/blackjack/GameResultTest.kt @@ -4,6 +4,8 @@ import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.NormalCard import blackjack.card.PictureCard +import blackjack.game.GameResult +import blackjack.game.ScoreCalculator import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name diff --git a/src/test/kotlin/blackjack/ScoreCalculatorTest.kt b/src/test/kotlin/blackjack/ScoreCalculatorTest.kt index 4a5c3f0e6..8a344a92c 100644 --- a/src/test/kotlin/blackjack/ScoreCalculatorTest.kt +++ b/src/test/kotlin/blackjack/ScoreCalculatorTest.kt @@ -6,6 +6,7 @@ import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.NormalCard import blackjack.card.PictureCard +import blackjack.game.ScoreCalculator import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest diff --git a/src/test/kotlin/blackjack/domain/DealerTest.kt b/src/test/kotlin/blackjack/domain/DealerTest.kt index 1702add1a..c6f1d3721 100644 --- a/src/test/kotlin/blackjack/domain/DealerTest.kt +++ b/src/test/kotlin/blackjack/domain/DealerTest.kt @@ -1,6 +1,6 @@ package blackjack.domain -import blackjack.ScoreCalculator +import blackjack.game.ScoreCalculator import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.NormalCard diff --git a/src/test/kotlin/blackjack/domain/PlayerTest.kt b/src/test/kotlin/blackjack/domain/PlayerTest.kt index d9d71374a..a7413c714 100644 --- a/src/test/kotlin/blackjack/domain/PlayerTest.kt +++ b/src/test/kotlin/blackjack/domain/PlayerTest.kt @@ -1,6 +1,6 @@ package blackjack.domain -import blackjack.ScoreCalculator +import blackjack.game.ScoreCalculator import blackjack.card.CardDeck import blackjack.card.CardPattern import blackjack.card.CardPicture diff --git a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt index c785fbb59..c67e6733e 100644 --- a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt +++ b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt @@ -1,6 +1,6 @@ package blackjack.participant -import blackjack.ScoreCalculator +import blackjack.game.ScoreCalculator import blackjack.card.AceCard import blackjack.card.CardPattern import blackjack.card.CardPicture From c772fe837be9afdf62f8b6247cb2f3d0c0d94350 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Sun, 10 Dec 2023 22:30:29 +0900 Subject: [PATCH 36/44] =?UTF-8?q?feat:=20test=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/participant/BettingAmount.kt | 3 --- .../participant/BettingAmountTest.kt | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/BettingAmount.kt b/src/main/kotlin/blackjack/participant/BettingAmount.kt index 4cced4d3c..6c16cb1d2 100644 --- a/src/main/kotlin/blackjack/participant/BettingAmount.kt +++ b/src/main/kotlin/blackjack/participant/BettingAmount.kt @@ -6,9 +6,6 @@ import kotlin.math.roundToInt value class BettingAmount( val amount: Int ) { -// fun changeNegative(): BettingAmount { -// return BettingAmount(amount * -1) -// } fun winToBlackjack(): BettingAmount { return BettingAmount((amount * 1.5).roundToInt()) diff --git a/src/test/kotlin/blackjack/participant/BettingAmountTest.kt b/src/test/kotlin/blackjack/participant/BettingAmountTest.kt index a8fb7211e..fa37a48ff 100644 --- a/src/test/kotlin/blackjack/participant/BettingAmountTest.kt +++ b/src/test/kotlin/blackjack/participant/BettingAmountTest.kt @@ -10,4 +10,28 @@ class BettingAmountTest { val amount = BettingAmount(1000) amount.amount shouldBe 1000 } + + @Test + fun `베팅 금액을 더한다`() { + val amount1 = BettingAmount(1000) + val amount2 = BettingAmount(2000) + val actual = amount1 + amount2 + + actual.amount shouldBe 3000 + } + + @Test + fun `베팅 금액을 뺀다`() { + val amount1 = BettingAmount(1000) + val amount2 = BettingAmount(2000) + val actual = amount1 - amount2 + + actual.amount shouldBe -1000 + } + + @Test + fun `베팅 금액을 음수로 만든다`() { + val amount = BettingAmount(1000) + amount.unaryMinus().amount shouldBe -1000 + } } From a61b1886c959dccbabecdc41749b5ba784c6b8bb Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Sun, 10 Dec 2023 22:36:24 +0900 Subject: [PATCH 37/44] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EA=B3=84=EC=82=B0=EC=9D=84=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?GameResultGenerator=20=EC=98=A4=EB=B8=8C=EC=A0=9D=ED=8A=B8=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/GameManager.kt | 18 ++++++++++++++---- src/main/kotlin/blackjack/game/GameResult.kt | 17 ++--------------- .../blackjack/game/GameResultGenerator.kt | 18 ++++++++++++++++++ ...esultTest.kt => GameResultGeneratorTest.kt} | 7 ++++--- 4 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 src/main/kotlin/blackjack/game/GameResultGenerator.kt rename src/test/kotlin/blackjack/{GameResultTest.kt => GameResultGeneratorTest.kt} (89%) diff --git a/src/main/kotlin/blackjack/GameManager.kt b/src/main/kotlin/blackjack/GameManager.kt index e8d79bb7a..4009d7649 100644 --- a/src/main/kotlin/blackjack/GameManager.kt +++ b/src/main/kotlin/blackjack/GameManager.kt @@ -2,6 +2,7 @@ package blackjack import blackjack.card.CardDeck import blackjack.game.GameResult +import blackjack.game.GameResultGenerator import blackjack.game.ScoreCalculator import blackjack.participant.BettingAmount import blackjack.participant.Dealer @@ -29,7 +30,9 @@ class GameManager( outputManager.printFirstTurn(players) outputManager.printPlayersAndDealerCards(players, dealer) - val result = playBlackJack() + playBlackJack() + + val result = GameResultGenerator.generateGameResult(players, dealer) outputManager.printDealerResultGame(dealer) @@ -39,13 +42,20 @@ class GameManager( outputManager.printResult(result) } - private fun playBlackJack(): GameResult { +// private fun playBlackJack(): GameResult { +// players.forEach { +// playerDraw(it) +// } +// dealerDraw(dealer) +// +// return GameResult(players, dealer) +// } + + private fun playBlackJack() { players.forEach { playerDraw(it) } dealerDraw(dealer) - - return GameResult(players, dealer) } private fun playerDraw(player: Player) { diff --git a/src/main/kotlin/blackjack/game/GameResult.kt b/src/main/kotlin/blackjack/game/GameResult.kt index 17f4cfc7a..4f7998c37 100644 --- a/src/main/kotlin/blackjack/game/GameResult.kt +++ b/src/main/kotlin/blackjack/game/GameResult.kt @@ -1,21 +1,8 @@ package blackjack.game import blackjack.participant.BettingAmount -import blackjack.participant.Dealer import blackjack.participant.Name -import blackjack.participant.Player -class GameResult( - players: List, - dealer: Dealer -) { +data class GameResult( val resultMap: Map - - init { - resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + players.map { player -> - val result = dealer.matchingScore(player) - val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) - player.name to bettingAmount - } - } -} +) diff --git a/src/main/kotlin/blackjack/game/GameResultGenerator.kt b/src/main/kotlin/blackjack/game/GameResultGenerator.kt new file mode 100644 index 000000000..d2fd2246a --- /dev/null +++ b/src/main/kotlin/blackjack/game/GameResultGenerator.kt @@ -0,0 +1,18 @@ +package blackjack.game + +import blackjack.participant.Dealer +import blackjack.participant.Name +import blackjack.participant.Player + +object GameResultGenerator { + + fun generateGameResult(players: List, dealer: Dealer): GameResult { + val resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + players.map { player -> + val result = dealer.matchingScore(player) + val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) + player.name to bettingAmount + } + + return GameResult(resultMap) + } +} \ No newline at end of file diff --git a/src/test/kotlin/blackjack/GameResultTest.kt b/src/test/kotlin/blackjack/GameResultGeneratorTest.kt similarity index 89% rename from src/test/kotlin/blackjack/GameResultTest.kt rename to src/test/kotlin/blackjack/GameResultGeneratorTest.kt index d9ed84dca..07ab43535 100644 --- a/src/test/kotlin/blackjack/GameResultTest.kt +++ b/src/test/kotlin/blackjack/GameResultGeneratorTest.kt @@ -5,6 +5,7 @@ import blackjack.card.CardPicture import blackjack.card.NormalCard import blackjack.card.PictureCard import blackjack.game.GameResult +import blackjack.game.GameResultGenerator import blackjack.game.ScoreCalculator import blackjack.participant.BettingAmount import blackjack.participant.Dealer @@ -13,7 +14,7 @@ import blackjack.participant.Player import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test -class GameResultTest { +class GameResultGeneratorTest { private val scoreCalculator: ScoreCalculator = ScoreCalculator() @Test @@ -34,7 +35,7 @@ class GameResultTest { NormalCard(2, CardPattern.CLOVER), ) ) - val result = GameResult(listOf(player), dealer) + val result = GameResultGenerator.generateGameResult(listOf(player), dealer) result.resultMap[player.name]?.amount shouldBe 1000 } @@ -58,7 +59,7 @@ class GameResultTest { NormalCard(2, CardPattern.CLOVER), ) ) - val result = GameResult(listOf(player), dealer) + val result = GameResultGenerator.generateGameResult(listOf(player), dealer) result.resultMap[player.name]?.amount shouldBe -1000 } From 2ca702cdf7cc2b0d9118806a21c0fa6cc8525d56 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Sun, 10 Dec 2023 22:36:56 +0900 Subject: [PATCH 38/44] feat: ktlint --- src/main/kotlin/blackjack/participant/Dealer.kt | 8 ++++---- src/test/kotlin/blackjack/domain/DealerTest.kt | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index 33edfa68f..fb85fecc3 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -1,7 +1,7 @@ package blackjack.participant -import blackjack.game.ScoreCalculator import blackjack.card.BlackJackCard +import blackjack.game.ScoreCalculator import blackjack.participant.status.Blackjack import blackjack.participant.status.Bust @@ -33,9 +33,9 @@ class Dealer( is Blackjack -> Result.Lose else -> { bettingAmount -= player.status.calculateBettingAmount( - Result.Win, - player.bettingAmount - ) + Result.Win, + player.bettingAmount + ) return Result.Win } diff --git a/src/test/kotlin/blackjack/domain/DealerTest.kt b/src/test/kotlin/blackjack/domain/DealerTest.kt index c6f1d3721..0c1809257 100644 --- a/src/test/kotlin/blackjack/domain/DealerTest.kt +++ b/src/test/kotlin/blackjack/domain/DealerTest.kt @@ -1,10 +1,10 @@ package blackjack.domain -import blackjack.game.ScoreCalculator import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.NormalCard import blackjack.card.PictureCard +import blackjack.game.ScoreCalculator import blackjack.participant.Dealer import blackjack.participant.Name import blackjack.participant.Player From 3693ab234764b44a0ec6ff4ec82e7a7d84dd7de3 Mon Sep 17 00:00:00 2001 From: JunHyun Lee Date: Sun, 10 Dec 2023 22:48:14 +0900 Subject: [PATCH 39/44] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=EC=9D=98=20?= =?UTF-8?q?=EC=A0=90=EC=88=98=20=EA=B3=84=EC=82=B0=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=EC=9D=84=20=EB=B6=84=EB=A6=AC=ED=95=98=EA=B8=B0=20=EC=9C=84?= =?UTF-8?q?=ED=95=B4=20=EC=83=88=EB=A1=9C=EC=9A=B4=20=ED=81=B4=EB=9E=98?= =?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 --- src/main/kotlin/blackjack/GameManager.kt | 1 - .../blackjack/game/GameResultGenerator.kt | 45 ++++- .../blackjack/game/MatchingScoreCalculator.kt | 60 ++++++ .../kotlin/blackjack/participant/Dealer.kt | 70 ++++--- .../blackjack/GameResultGeneratorTest.kt | 1 - .../kotlin/blackjack/domain/DealerTest.kt | 180 +++++++++--------- .../game/MatchingScoreCalculatorTest.kt | 104 ++++++++++ 7 files changed, 329 insertions(+), 132 deletions(-) create mode 100644 src/main/kotlin/blackjack/game/MatchingScoreCalculator.kt create mode 100644 src/test/kotlin/blackjack/game/MatchingScoreCalculatorTest.kt diff --git a/src/main/kotlin/blackjack/GameManager.kt b/src/main/kotlin/blackjack/GameManager.kt index 4009d7649..9e4b5c3c2 100644 --- a/src/main/kotlin/blackjack/GameManager.kt +++ b/src/main/kotlin/blackjack/GameManager.kt @@ -1,7 +1,6 @@ package blackjack import blackjack.card.CardDeck -import blackjack.game.GameResult import blackjack.game.GameResultGenerator import blackjack.game.ScoreCalculator import blackjack.participant.BettingAmount diff --git a/src/main/kotlin/blackjack/game/GameResultGenerator.kt b/src/main/kotlin/blackjack/game/GameResultGenerator.kt index d2fd2246a..6ca643c51 100644 --- a/src/main/kotlin/blackjack/game/GameResultGenerator.kt +++ b/src/main/kotlin/blackjack/game/GameResultGenerator.kt @@ -1,18 +1,61 @@ package blackjack.game +import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name import blackjack.participant.Player +import blackjack.participant.Result +import blackjack.participant.minus +import blackjack.participant.plus +import blackjack.participant.status.Blackjack +import blackjack.participant.status.Bust object GameResultGenerator { fun generateGameResult(players: List, dealer: Dealer): GameResult { val resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + players.map { player -> - val result = dealer.matchingScore(player) +// val result = dealer.matchingScore(player) + val result = matchingScore(player, dealer) val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) player.name to bettingAmount } return GameResult(resultMap) } + + private fun matchingScore(player: Player, dealer: Dealer): Result { + var bettingAmount: BettingAmount = BettingAmount(0) + if (player.status is Blackjack) { + return when (dealer.status) { + is Blackjack -> Result.Lose + else -> { + bettingAmount -= player.status.calculateBettingAmount( + Result.Win, + player.bettingAmount + ) + + return Result.Win + } + } + } + + if (player.status is Bust) { + bettingAmount += player.bettingAmount + return Result.Lose + } + + if (dealer.status is Bust) { + bettingAmount -= player.bettingAmount + return Result.Win + } + + return when (dealer.resultScore() > player.resultScore()) { + true -> { + bettingAmount += player.bettingAmount + Result.Lose + } + + else -> Result.Win + } + } } \ No newline at end of file diff --git a/src/main/kotlin/blackjack/game/MatchingScoreCalculator.kt b/src/main/kotlin/blackjack/game/MatchingScoreCalculator.kt new file mode 100644 index 000000000..8f5776119 --- /dev/null +++ b/src/main/kotlin/blackjack/game/MatchingScoreCalculator.kt @@ -0,0 +1,60 @@ +package blackjack.game + +import blackjack.participant.BettingAmount +import blackjack.participant.Dealer +import blackjack.participant.Player +import blackjack.participant.Result +import blackjack.participant.minus +import blackjack.participant.plus +import blackjack.participant.status.Blackjack +import blackjack.participant.status.Bust + +object MatchingScoreCalculator { + +// fun generateGameResult(players: List, dealer: Dealer): GameResult { +// val resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + players.map { player -> +//// val result = dealer.matchingScore(player) +// val result = matchingScore(player, dealer) +// val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) +// player.name to bettingAmount +// } +// +// return GameResult(resultMap) +// } + + fun matchingScore(player: Player, dealer: Dealer): Result { + var bettingAmount: BettingAmount = BettingAmount(0) + if (player.status is Blackjack) { + return when (dealer.status) { + is Blackjack -> Result.Lose + else -> { + bettingAmount -= player.status.calculateBettingAmount( + Result.Win, + player.bettingAmount + ) + + return Result.Win + } + } + } + + if (player.status is Bust) { + bettingAmount += player.bettingAmount + return Result.Lose + } + + if (dealer.status is Bust) { + bettingAmount -= player.bettingAmount + return Result.Win + } + + return when (dealer.resultScore() > player.resultScore()) { + true -> { + bettingAmount += player.bettingAmount + Result.Lose + } + + else -> Result.Win + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index fb85fecc3..400ee099b 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -2,8 +2,6 @@ package blackjack.participant import blackjack.card.BlackJackCard import blackjack.game.ScoreCalculator -import blackjack.participant.status.Blackjack -import blackjack.participant.status.Bust class Dealer( scoreCalculator: ScoreCalculator @@ -27,40 +25,40 @@ class Dealer( return blackjackStrategy.resultScore() < MAX_DRAW_SCORE } - fun matchingScore(player: Player): Result { - if (player.status is Blackjack) { - return when (status) { - is Blackjack -> Result.Lose - else -> { - bettingAmount -= player.status.calculateBettingAmount( - Result.Win, - player.bettingAmount - ) - - return Result.Win - } - } - } - - if (player.status is Bust) { - bettingAmount += player.bettingAmount - return Result.Lose - } - - if (status is Bust) { - bettingAmount -= player.bettingAmount - return Result.Win - } - - return when (resultScore() > player.resultScore()) { - true -> { - bettingAmount += player.bettingAmount - Result.Lose - } - - else -> Result.Win - } - } +// fun matchingScore(player: Player): Result { +// if (player.status is Blackjack) { +// return when (status) { +// is Blackjack -> Result.Lose +// else -> { +// bettingAmount -= player.status.calculateBettingAmount( +// Result.Win, +// player.bettingAmount +// ) +// +// return Result.Win +// } +// } +// } +// +// if (player.status is Bust) { +// bettingAmount += player.bettingAmount +// return Result.Lose +// } +// +// if (status is Bust) { +// bettingAmount -= player.bettingAmount +// return Result.Win +// } +// +// return when (resultScore() > player.resultScore()) { +// true -> { +// bettingAmount += player.bettingAmount +// Result.Lose +// } +// +// else -> Result.Win +// } +// } companion object { private const val MAX_DRAW_SCORE: Int = 17 diff --git a/src/test/kotlin/blackjack/GameResultGeneratorTest.kt b/src/test/kotlin/blackjack/GameResultGeneratorTest.kt index 07ab43535..2fe39b0b7 100644 --- a/src/test/kotlin/blackjack/GameResultGeneratorTest.kt +++ b/src/test/kotlin/blackjack/GameResultGeneratorTest.kt @@ -4,7 +4,6 @@ import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.NormalCard import blackjack.card.PictureCard -import blackjack.game.GameResult import blackjack.game.GameResultGenerator import blackjack.game.ScoreCalculator import blackjack.participant.BettingAmount diff --git a/src/test/kotlin/blackjack/domain/DealerTest.kt b/src/test/kotlin/blackjack/domain/DealerTest.kt index 0c1809257..a3274b26f 100644 --- a/src/test/kotlin/blackjack/domain/DealerTest.kt +++ b/src/test/kotlin/blackjack/domain/DealerTest.kt @@ -1,16 +1,10 @@ package blackjack.domain import blackjack.card.CardPattern -import blackjack.card.CardPicture import blackjack.card.NormalCard -import blackjack.card.PictureCard import blackjack.game.ScoreCalculator import blackjack.participant.Dealer -import blackjack.participant.Name -import blackjack.participant.Player -import blackjack.participant.Result import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeInstanceOf import org.junit.jupiter.api.Test class DealerTest { @@ -42,91 +36,91 @@ class DealerTest { dealer.shouldDraw() shouldBe false } - @Test - fun `딜러가 버스트 상태이고 플레이어가 버스트 상태가 아니라면 플레이어가 승리한다`() { - val dealer = Dealer(scoreCalculator) - dealer.drawCard( - listOf( - NormalCard(5, CardPattern.CLOVER), - NormalCard(10, CardPattern.CLOVER), - NormalCard(8, CardPattern.CLOVER), - ) - ) - - val player = Player(Name("홍길동"), scoreCalculator) - player.drawCard( - listOf( - PictureCard(CardPicture.KING, CardPattern.CLOVER), - PictureCard(CardPicture.KING, CardPattern.SPADE) - ) - ) - - dealer.matchingScore(player).shouldBeInstanceOf() - } - - @Test - fun `플레이어가 버스트 상태라면 플레이어가 패배한다`() { - val dealer = Dealer(scoreCalculator) - dealer.drawCard( - listOf( - NormalCard(5, CardPattern.CLOVER), - NormalCard(10, CardPattern.CLOVER), - NormalCard(8, CardPattern.CLOVER), - ) - ) - - val player = Player(Name("홍길동"), scoreCalculator) - player.drawCard( - listOf( - NormalCard(9, CardPattern.CLOVER), - PictureCard(CardPicture.KING, CardPattern.CLOVER), - PictureCard(CardPicture.KING, CardPattern.SPADE), - NormalCard(8, CardPattern.CLOVER), - ) - ) - - dealer.matchingScore(player).shouldBeInstanceOf() - } - - @Test - fun `플레이어가 점수가 딜러보다 높으면 플레이어가 승리한다`() { - val dealer = Dealer(scoreCalculator) - dealer.drawCard( - listOf( - NormalCard(5, CardPattern.CLOVER), - NormalCard(10, CardPattern.CLOVER), - ) - ) - - val player = Player(Name("홍길동"), scoreCalculator) - player.drawCard( - listOf( - NormalCard(9, CardPattern.CLOVER), - PictureCard(CardPicture.KING, CardPattern.CLOVER), - ) - ) - - dealer.matchingScore(player).shouldBeInstanceOf() - } - - @Test - fun `플레이어가 점수가 딜러보다 낮으면 플레이어가 패배한다`() { - val dealer = Dealer(scoreCalculator) - dealer.drawCard( - listOf( - NormalCard(5, CardPattern.CLOVER), - NormalCard(10, CardPattern.CLOVER), - ) - ) - - val player = Player(Name("홍길동"), scoreCalculator) - player.drawCard( - listOf( - NormalCard(4, CardPattern.CLOVER), - NormalCard(10, CardPattern.CLOVER), - ) - ) - - dealer.matchingScore(player).shouldBeInstanceOf() - } +// @Test +// fun `딜러가 버스트 상태이고 플레이어가 버스트 상태가 아니라면 플레이어가 승리한다`() { +// val dealer = Dealer(scoreCalculator) +// dealer.drawCard( +// listOf( +// NormalCard(5, CardPattern.CLOVER), +// NormalCard(10, CardPattern.CLOVER), +// NormalCard(8, CardPattern.CLOVER), +// ) +// ) +// +// val player = Player(Name("홍길동"), scoreCalculator) +// player.drawCard( +// listOf( +// PictureCard(CardPicture.KING, CardPattern.CLOVER), +// PictureCard(CardPicture.KING, CardPattern.SPADE) +// ) +// ) +// +// dealer.matchingScore(player).shouldBeInstanceOf() +// } +// +// @Test +// fun `플레이어가 버스트 상태라면 플레이어가 패배한다`() { +// val dealer = Dealer(scoreCalculator) +// dealer.drawCard( +// listOf( +// NormalCard(5, CardPattern.CLOVER), +// NormalCard(10, CardPattern.CLOVER), +// NormalCard(8, CardPattern.CLOVER), +// ) +// ) +// +// val player = Player(Name("홍길동"), scoreCalculator) +// player.drawCard( +// listOf( +// NormalCard(9, CardPattern.CLOVER), +// PictureCard(CardPicture.KING, CardPattern.CLOVER), +// PictureCard(CardPicture.KING, CardPattern.SPADE), +// NormalCard(8, CardPattern.CLOVER), +// ) +// ) +// +// dealer.matchingScore(player).shouldBeInstanceOf() +// } +// +// @Test +// fun `플레이어가 점수가 딜러보다 높으면 플레이어가 승리한다`() { +// val dealer = Dealer(scoreCalculator) +// dealer.drawCard( +// listOf( +// NormalCard(5, CardPattern.CLOVER), +// NormalCard(10, CardPattern.CLOVER), +// ) +// ) +// +// val player = Player(Name("홍길동"), scoreCalculator) +// player.drawCard( +// listOf( +// NormalCard(9, CardPattern.CLOVER), +// PictureCard(CardPicture.KING, CardPattern.CLOVER), +// ) +// ) +// +// dealer.matchingScore(player).shouldBeInstanceOf() +// } +// +// @Test +// fun `플레이어가 점수가 딜러보다 낮으면 플레이어가 패배한다`() { +// val dealer = Dealer(scoreCalculator) +// dealer.drawCard( +// listOf( +// NormalCard(5, CardPattern.CLOVER), +// NormalCard(10, CardPattern.CLOVER), +// ) +// ) +// +// val player = Player(Name("홍길동"), scoreCalculator) +// player.drawCard( +// listOf( +// NormalCard(4, CardPattern.CLOVER), +// NormalCard(10, CardPattern.CLOVER), +// ) +// ) +// +// dealer.matchingScore(player).shouldBeInstanceOf() +// } } diff --git a/src/test/kotlin/blackjack/game/MatchingScoreCalculatorTest.kt b/src/test/kotlin/blackjack/game/MatchingScoreCalculatorTest.kt new file mode 100644 index 000000000..eda5a30f5 --- /dev/null +++ b/src/test/kotlin/blackjack/game/MatchingScoreCalculatorTest.kt @@ -0,0 +1,104 @@ +package blackjack.game + +import blackjack.card.CardPattern +import blackjack.card.CardPicture +import blackjack.card.NormalCard +import blackjack.card.PictureCard +import blackjack.participant.Dealer +import blackjack.participant.Name +import blackjack.participant.Player +import blackjack.participant.Result +import io.kotest.matchers.types.shouldBeInstanceOf +import org.junit.jupiter.api.Test + +class MatchingScoreCalculatorTest { + private val scoreCalculator: ScoreCalculator = ScoreCalculator() + + @Test + fun `딜러가 버스트 상태이고 플레이어가 버스트 상태가 아니라면 플레이어가 승리한다`() { + val dealer = Dealer(scoreCalculator) + dealer.drawCard( + listOf( + NormalCard(5, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + NormalCard(8, CardPattern.CLOVER), + ) + ) + + val player = Player(Name("홍길동"), scoreCalculator) + player.drawCard( + listOf( + PictureCard(CardPicture.KING, CardPattern.CLOVER), + PictureCard(CardPicture.KING, CardPattern.SPADE) + ) + ) + + MatchingScoreCalculator.matchingScore(player, dealer).shouldBeInstanceOf() + } + + @Test + fun `플레이어가 버스트 상태라면 플레이어가 패배한다`() { + val dealer = Dealer(scoreCalculator) + dealer.drawCard( + listOf( + NormalCard(5, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + NormalCard(8, CardPattern.CLOVER), + ) + ) + + val player = Player(Name("홍길동"), scoreCalculator) + player.drawCard( + listOf( + NormalCard(9, CardPattern.CLOVER), + PictureCard(CardPicture.KING, CardPattern.CLOVER), + PictureCard(CardPicture.KING, CardPattern.SPADE), + NormalCard(8, CardPattern.CLOVER), + ) + ) + + MatchingScoreCalculator.matchingScore(player, dealer).shouldBeInstanceOf() + } + + @Test + fun `플레이어가 점수가 딜러보다 높으면 플레이어가 승리한다`() { + val dealer = Dealer(scoreCalculator) + dealer.drawCard( + listOf( + NormalCard(5, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + ) + ) + + val player = Player(Name("홍길동"), scoreCalculator) + player.drawCard( + listOf( + NormalCard(9, CardPattern.CLOVER), + PictureCard(CardPicture.KING, CardPattern.CLOVER), + ) + ) + + MatchingScoreCalculator.matchingScore(player, dealer).shouldBeInstanceOf() + } + + @Test + fun `플레이어가 점수가 딜러보다 낮으면 플레이어가 패배한다`() { + val dealer = Dealer(scoreCalculator) + dealer.drawCard( + listOf( + NormalCard(5, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + ) + ) + + val player = Player(Name("홍길동"), scoreCalculator) + player.drawCard( + listOf( + NormalCard(4, CardPattern.CLOVER), + NormalCard(10, CardPattern.CLOVER), + ) + ) + + MatchingScoreCalculator.matchingScore(player, dealer).shouldBeInstanceOf() + } +} From 4ebdeb78855a1626f39baacf90799c14f87d1982 Mon Sep 17 00:00:00 2001 From: Ted1_Lee Date: Mon, 11 Dec 2023 09:27:55 +0900 Subject: [PATCH 40/44] =?UTF-8?q?feat:=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD=20game=20->=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/GameManager.kt | 4 ++-- src/main/kotlin/blackjack/participant/BlackjackStrategy.kt | 2 +- src/main/kotlin/blackjack/participant/Dealer.kt | 2 +- src/main/kotlin/blackjack/participant/Player.kt | 2 +- src/main/kotlin/blackjack/{game => supoort}/GameResult.kt | 2 +- .../kotlin/blackjack/{game => supoort}/GameResultGenerator.kt | 2 +- .../blackjack/{game => supoort}/MatchingScoreCalculator.kt | 2 +- .../kotlin/blackjack/{game => supoort}/ScoreCalculator.kt | 2 +- src/main/kotlin/blackjack/ui/OutputManager.kt | 2 +- src/test/kotlin/blackjack/GameResultGeneratorTest.kt | 4 ++-- src/test/kotlin/blackjack/ScoreCalculatorTest.kt | 2 +- src/test/kotlin/blackjack/domain/DealerTest.kt | 2 +- src/test/kotlin/blackjack/domain/PlayerTest.kt | 2 +- .../kotlin/blackjack/participant/BlackjackStrategyTest.kt | 2 +- .../{game => supoort}/MatchingScoreCalculatorTest.kt | 2 +- 15 files changed, 17 insertions(+), 17 deletions(-) rename src/main/kotlin/blackjack/{game => supoort}/GameResult.kt (85%) rename src/main/kotlin/blackjack/{game => supoort}/GameResultGenerator.kt (98%) rename src/main/kotlin/blackjack/{game => supoort}/MatchingScoreCalculator.kt (98%) rename src/main/kotlin/blackjack/{game => supoort}/ScoreCalculator.kt (98%) rename src/test/kotlin/blackjack/{game => supoort}/MatchingScoreCalculatorTest.kt (99%) diff --git a/src/main/kotlin/blackjack/GameManager.kt b/src/main/kotlin/blackjack/GameManager.kt index 9e4b5c3c2..7a02c6250 100644 --- a/src/main/kotlin/blackjack/GameManager.kt +++ b/src/main/kotlin/blackjack/GameManager.kt @@ -1,8 +1,8 @@ package blackjack import blackjack.card.CardDeck -import blackjack.game.GameResultGenerator -import blackjack.game.ScoreCalculator +import blackjack.supoort.GameResultGenerator +import blackjack.supoort.ScoreCalculator import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name diff --git a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt index 94abaa72b..ceb2ef383 100644 --- a/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt +++ b/src/main/kotlin/blackjack/participant/BlackjackStrategy.kt @@ -1,6 +1,6 @@ package blackjack.participant -import blackjack.game.ScoreCalculator +import blackjack.supoort.ScoreCalculator import blackjack.card.BlackJackCard import blackjack.participant.status.Blackjack import blackjack.participant.status.Bust diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index 400ee099b..c8e6bc749 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -1,7 +1,7 @@ package blackjack.participant import blackjack.card.BlackJackCard -import blackjack.game.ScoreCalculator +import blackjack.supoort.ScoreCalculator class Dealer( scoreCalculator: ScoreCalculator diff --git a/src/main/kotlin/blackjack/participant/Player.kt b/src/main/kotlin/blackjack/participant/Player.kt index 337b15785..0dc6e22d3 100644 --- a/src/main/kotlin/blackjack/participant/Player.kt +++ b/src/main/kotlin/blackjack/participant/Player.kt @@ -1,6 +1,6 @@ package blackjack.participant -import blackjack.game.ScoreCalculator +import blackjack.supoort.ScoreCalculator import blackjack.card.BlackJackCard class Player( diff --git a/src/main/kotlin/blackjack/game/GameResult.kt b/src/main/kotlin/blackjack/supoort/GameResult.kt similarity index 85% rename from src/main/kotlin/blackjack/game/GameResult.kt rename to src/main/kotlin/blackjack/supoort/GameResult.kt index 4f7998c37..5f8582231 100644 --- a/src/main/kotlin/blackjack/game/GameResult.kt +++ b/src/main/kotlin/blackjack/supoort/GameResult.kt @@ -1,4 +1,4 @@ -package blackjack.game +package blackjack.supoort import blackjack.participant.BettingAmount import blackjack.participant.Name diff --git a/src/main/kotlin/blackjack/game/GameResultGenerator.kt b/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt similarity index 98% rename from src/main/kotlin/blackjack/game/GameResultGenerator.kt rename to src/main/kotlin/blackjack/supoort/GameResultGenerator.kt index 6ca643c51..1187d4e3c 100644 --- a/src/main/kotlin/blackjack/game/GameResultGenerator.kt +++ b/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt @@ -1,4 +1,4 @@ -package blackjack.game +package blackjack.supoort import blackjack.participant.BettingAmount import blackjack.participant.Dealer diff --git a/src/main/kotlin/blackjack/game/MatchingScoreCalculator.kt b/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt similarity index 98% rename from src/main/kotlin/blackjack/game/MatchingScoreCalculator.kt rename to src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt index 8f5776119..cfb22bd27 100644 --- a/src/main/kotlin/blackjack/game/MatchingScoreCalculator.kt +++ b/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt @@ -1,4 +1,4 @@ -package blackjack.game +package blackjack.supoort import blackjack.participant.BettingAmount import blackjack.participant.Dealer diff --git a/src/main/kotlin/blackjack/game/ScoreCalculator.kt b/src/main/kotlin/blackjack/supoort/ScoreCalculator.kt similarity index 98% rename from src/main/kotlin/blackjack/game/ScoreCalculator.kt rename to src/main/kotlin/blackjack/supoort/ScoreCalculator.kt index 52adfacc6..a223ff936 100644 --- a/src/main/kotlin/blackjack/game/ScoreCalculator.kt +++ b/src/main/kotlin/blackjack/supoort/ScoreCalculator.kt @@ -1,4 +1,4 @@ -package blackjack.game +package blackjack.supoort import blackjack.card.AceCard import blackjack.card.BlackJackCard diff --git a/src/main/kotlin/blackjack/ui/OutputManager.kt b/src/main/kotlin/blackjack/ui/OutputManager.kt index 5280ef4b2..7fac65c4f 100644 --- a/src/main/kotlin/blackjack/ui/OutputManager.kt +++ b/src/main/kotlin/blackjack/ui/OutputManager.kt @@ -1,6 +1,6 @@ package blackjack.ui -import blackjack.game.GameResult +import blackjack.supoort.GameResult import blackjack.card.AceCard import blackjack.card.BlackJackCard import blackjack.card.CardPattern diff --git a/src/test/kotlin/blackjack/GameResultGeneratorTest.kt b/src/test/kotlin/blackjack/GameResultGeneratorTest.kt index 2fe39b0b7..09dc55d62 100644 --- a/src/test/kotlin/blackjack/GameResultGeneratorTest.kt +++ b/src/test/kotlin/blackjack/GameResultGeneratorTest.kt @@ -4,8 +4,8 @@ import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.NormalCard import blackjack.card.PictureCard -import blackjack.game.GameResultGenerator -import blackjack.game.ScoreCalculator +import blackjack.supoort.GameResultGenerator +import blackjack.supoort.ScoreCalculator import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name diff --git a/src/test/kotlin/blackjack/ScoreCalculatorTest.kt b/src/test/kotlin/blackjack/ScoreCalculatorTest.kt index 8a344a92c..8e2637945 100644 --- a/src/test/kotlin/blackjack/ScoreCalculatorTest.kt +++ b/src/test/kotlin/blackjack/ScoreCalculatorTest.kt @@ -6,7 +6,7 @@ import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.NormalCard import blackjack.card.PictureCard -import blackjack.game.ScoreCalculator +import blackjack.supoort.ScoreCalculator import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest diff --git a/src/test/kotlin/blackjack/domain/DealerTest.kt b/src/test/kotlin/blackjack/domain/DealerTest.kt index a3274b26f..a119cc4d9 100644 --- a/src/test/kotlin/blackjack/domain/DealerTest.kt +++ b/src/test/kotlin/blackjack/domain/DealerTest.kt @@ -2,7 +2,7 @@ package blackjack.domain import blackjack.card.CardPattern import blackjack.card.NormalCard -import blackjack.game.ScoreCalculator +import blackjack.supoort.ScoreCalculator import blackjack.participant.Dealer import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test diff --git a/src/test/kotlin/blackjack/domain/PlayerTest.kt b/src/test/kotlin/blackjack/domain/PlayerTest.kt index a7413c714..8fd207074 100644 --- a/src/test/kotlin/blackjack/domain/PlayerTest.kt +++ b/src/test/kotlin/blackjack/domain/PlayerTest.kt @@ -1,6 +1,6 @@ package blackjack.domain -import blackjack.game.ScoreCalculator +import blackjack.supoort.ScoreCalculator import blackjack.card.CardDeck import blackjack.card.CardPattern import blackjack.card.CardPicture diff --git a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt index c67e6733e..f218e8b80 100644 --- a/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt +++ b/src/test/kotlin/blackjack/participant/BlackjackStrategyTest.kt @@ -1,6 +1,6 @@ package blackjack.participant -import blackjack.game.ScoreCalculator +import blackjack.supoort.ScoreCalculator import blackjack.card.AceCard import blackjack.card.CardPattern import blackjack.card.CardPicture diff --git a/src/test/kotlin/blackjack/game/MatchingScoreCalculatorTest.kt b/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt similarity index 99% rename from src/test/kotlin/blackjack/game/MatchingScoreCalculatorTest.kt rename to src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt index eda5a30f5..56f0cb3e2 100644 --- a/src/test/kotlin/blackjack/game/MatchingScoreCalculatorTest.kt +++ b/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt @@ -1,4 +1,4 @@ -package blackjack.game +package blackjack.supoort import blackjack.card.CardPattern import blackjack.card.CardPicture From 9067668c1241edf4e5c1e3e2c8ae6d7b1531cdaf Mon Sep 17 00:00:00 2001 From: Ted1_Lee Date: Mon, 11 Dec 2023 09:32:13 +0900 Subject: [PATCH 41/44] feat: ktlint --- .../kotlin/blackjack/participant/Dealer.kt | 35 ------- .../blackjack/supoort/GameResultGenerator.kt | 3 +- .../supoort/MatchingScoreCalculator.kt | 13 +-- src/main/kotlin/blackjack/ui/InputManager.kt | 12 --- .../kotlin/blackjack/domain/DealerTest.kt | 92 +------------------ 5 files changed, 4 insertions(+), 151 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index c8e6bc749..4192ab410 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -25,41 +25,6 @@ class Dealer( return blackjackStrategy.resultScore() < MAX_DRAW_SCORE } -// fun matchingScore(player: Player): Result { -// if (player.status is Blackjack) { -// return when (status) { -// is Blackjack -> Result.Lose -// else -> { -// bettingAmount -= player.status.calculateBettingAmount( -// Result.Win, -// player.bettingAmount -// ) -// -// return Result.Win -// } -// } -// } -// -// if (player.status is Bust) { -// bettingAmount += player.bettingAmount -// return Result.Lose -// } -// -// if (status is Bust) { -// bettingAmount -= player.bettingAmount -// return Result.Win -// } -// -// return when (resultScore() > player.resultScore()) { -// true -> { -// bettingAmount += player.bettingAmount -// Result.Lose -// } -// -// else -> Result.Win -// } -// } - companion object { private const val MAX_DRAW_SCORE: Int = 17 } diff --git a/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt b/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt index 1187d4e3c..ef8fd306b 100644 --- a/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt +++ b/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt @@ -14,7 +14,6 @@ object GameResultGenerator { fun generateGameResult(players: List, dealer: Dealer): GameResult { val resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + players.map { player -> -// val result = dealer.matchingScore(player) val result = matchingScore(player, dealer) val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) player.name to bettingAmount @@ -24,7 +23,7 @@ object GameResultGenerator { } private fun matchingScore(player: Player, dealer: Dealer): Result { - var bettingAmount: BettingAmount = BettingAmount(0) + var bettingAmount = BettingAmount(0) if (player.status is Blackjack) { return when (dealer.status) { is Blackjack -> Result.Lose diff --git a/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt b/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt index cfb22bd27..f1f3799e0 100644 --- a/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt +++ b/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt @@ -11,19 +11,8 @@ import blackjack.participant.status.Bust object MatchingScoreCalculator { -// fun generateGameResult(players: List, dealer: Dealer): GameResult { -// val resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + players.map { player -> -//// val result = dealer.matchingScore(player) -// val result = matchingScore(player, dealer) -// val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) -// player.name to bettingAmount -// } -// -// return GameResult(resultMap) -// } - fun matchingScore(player: Player, dealer: Dealer): Result { - var bettingAmount: BettingAmount = BettingAmount(0) + var bettingAmount = BettingAmount(0) if (player.status is Blackjack) { return when (dealer.status) { is Blackjack -> Result.Lose diff --git a/src/main/kotlin/blackjack/ui/InputManager.kt b/src/main/kotlin/blackjack/ui/InputManager.kt index 8c4a65826..4aff3c346 100644 --- a/src/main/kotlin/blackjack/ui/InputManager.kt +++ b/src/main/kotlin/blackjack/ui/InputManager.kt @@ -1,7 +1,5 @@ package blackjack.ui -import blackjack.participant.Player - class InputManager { fun inputPlayerNames(): List { println(INPUT_PLAYER_NAMES_MESSAGE) @@ -22,15 +20,6 @@ class InputManager { fun inputShouldDrawCard(name: String): Int { println("${name}는 $INPUT_SHOULD_DRAW_CARD_MESSAGE") - return when (readln()) { - "Y", "y" -> 1 - "N", "n" -> 0 - else -> 0 - } - } - - fun inputShouldDrawCard(player: Player): Int { - println("${player.name}는 $INPUT_SHOULD_DRAW_CARD_MESSAGE") return when (readln()) { "Y", "y" -> CHOOSE_DRAW "N", "n" -> CHOOSE_NOT_DRAW @@ -40,7 +29,6 @@ class InputManager { companion object { private const val INPUT_NOT_NULL_MESSAGE = "입력값을 입력해주세요." - private const val INPUT_DEALER_MESSAGE = "16이하라 한장의 카드를 더 받았습니다." private const val INPUT_PLAYER_NAMES_MESSAGE: String = "게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)" private const val INPUT_SHOULD_DRAW_CARD_MESSAGE: String = "한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)" private const val CHOOSE_DRAW: Int = 1 diff --git a/src/test/kotlin/blackjack/domain/DealerTest.kt b/src/test/kotlin/blackjack/domain/DealerTest.kt index a119cc4d9..a04ee49bd 100644 --- a/src/test/kotlin/blackjack/domain/DealerTest.kt +++ b/src/test/kotlin/blackjack/domain/DealerTest.kt @@ -1,11 +1,11 @@ package blackjack.domain +import org.junit.jupiter.api.Test +import io.kotest.matchers.shouldBe import blackjack.card.CardPattern import blackjack.card.NormalCard import blackjack.supoort.ScoreCalculator import blackjack.participant.Dealer -import io.kotest.matchers.shouldBe -import org.junit.jupiter.api.Test class DealerTest { private val scoreCalculator: ScoreCalculator = ScoreCalculator() @@ -35,92 +35,4 @@ class DealerTest { dealer.shouldDraw() shouldBe false } - -// @Test -// fun `딜러가 버스트 상태이고 플레이어가 버스트 상태가 아니라면 플레이어가 승리한다`() { -// val dealer = Dealer(scoreCalculator) -// dealer.drawCard( -// listOf( -// NormalCard(5, CardPattern.CLOVER), -// NormalCard(10, CardPattern.CLOVER), -// NormalCard(8, CardPattern.CLOVER), -// ) -// ) -// -// val player = Player(Name("홍길동"), scoreCalculator) -// player.drawCard( -// listOf( -// PictureCard(CardPicture.KING, CardPattern.CLOVER), -// PictureCard(CardPicture.KING, CardPattern.SPADE) -// ) -// ) -// -// dealer.matchingScore(player).shouldBeInstanceOf() -// } -// -// @Test -// fun `플레이어가 버스트 상태라면 플레이어가 패배한다`() { -// val dealer = Dealer(scoreCalculator) -// dealer.drawCard( -// listOf( -// NormalCard(5, CardPattern.CLOVER), -// NormalCard(10, CardPattern.CLOVER), -// NormalCard(8, CardPattern.CLOVER), -// ) -// ) -// -// val player = Player(Name("홍길동"), scoreCalculator) -// player.drawCard( -// listOf( -// NormalCard(9, CardPattern.CLOVER), -// PictureCard(CardPicture.KING, CardPattern.CLOVER), -// PictureCard(CardPicture.KING, CardPattern.SPADE), -// NormalCard(8, CardPattern.CLOVER), -// ) -// ) -// -// dealer.matchingScore(player).shouldBeInstanceOf() -// } -// -// @Test -// fun `플레이어가 점수가 딜러보다 높으면 플레이어가 승리한다`() { -// val dealer = Dealer(scoreCalculator) -// dealer.drawCard( -// listOf( -// NormalCard(5, CardPattern.CLOVER), -// NormalCard(10, CardPattern.CLOVER), -// ) -// ) -// -// val player = Player(Name("홍길동"), scoreCalculator) -// player.drawCard( -// listOf( -// NormalCard(9, CardPattern.CLOVER), -// PictureCard(CardPicture.KING, CardPattern.CLOVER), -// ) -// ) -// -// dealer.matchingScore(player).shouldBeInstanceOf() -// } -// -// @Test -// fun `플레이어가 점수가 딜러보다 낮으면 플레이어가 패배한다`() { -// val dealer = Dealer(scoreCalculator) -// dealer.drawCard( -// listOf( -// NormalCard(5, CardPattern.CLOVER), -// NormalCard(10, CardPattern.CLOVER), -// ) -// ) -// -// val player = Player(Name("홍길동"), scoreCalculator) -// player.drawCard( -// listOf( -// NormalCard(4, CardPattern.CLOVER), -// NormalCard(10, CardPattern.CLOVER), -// ) -// ) -// -// dealer.matchingScore(player).shouldBeInstanceOf() -// } } From 70b8d6c45aed877aafd83c8ac204cbc975d62770 Mon Sep 17 00:00:00 2001 From: Ted1_Lee Date: Mon, 11 Dec 2023 09:42:35 +0900 Subject: [PATCH 42/44] =?UTF-8?q?feat:=20MatchingScoreCalculator=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EC=97=AD=ED=95=A0=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/supoort/GameResultGenerator.kt | 17 +--- .../supoort/MatchingScoreCalculator.kt | 45 ++++------- .../{ => supoort}/GameResultGeneratorTest.kt | 4 +- .../supoort/MatchingScoreCalculatorTest.kt | 81 ++----------------- 4 files changed, 29 insertions(+), 118 deletions(-) rename src/test/kotlin/blackjack/{ => supoort}/GameResultGeneratorTest.kt (95%) diff --git a/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt b/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt index ef8fd306b..fa3ea36d9 100644 --- a/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt +++ b/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt @@ -1,19 +1,17 @@ package blackjack.supoort -import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name import blackjack.participant.Player import blackjack.participant.Result -import blackjack.participant.minus -import blackjack.participant.plus import blackjack.participant.status.Blackjack import blackjack.participant.status.Bust object GameResultGenerator { fun generateGameResult(players: List, dealer: Dealer): GameResult { - val resultMap = mapOf(Name("딜러") to dealer.bettingAmount) + players.map { player -> + val dealerBettingAmount = MatchingScoreCalculator.matchingScoreForDealer(players, dealer) + val resultMap = mapOf(Name("딜러") to dealerBettingAmount) + players.map { player -> val result = matchingScore(player, dealer) val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) player.name to bettingAmount @@ -23,38 +21,29 @@ object GameResultGenerator { } private fun matchingScore(player: Player, dealer: Dealer): Result { - var bettingAmount = BettingAmount(0) if (player.status is Blackjack) { return when (dealer.status) { is Blackjack -> Result.Lose else -> { - bettingAmount -= player.status.calculateBettingAmount( - Result.Win, - player.bettingAmount - ) - return Result.Win } } } if (player.status is Bust) { - bettingAmount += player.bettingAmount return Result.Lose } if (dealer.status is Bust) { - bettingAmount -= player.bettingAmount return Result.Win } return when (dealer.resultScore() > player.resultScore()) { true -> { - bettingAmount += player.bettingAmount Result.Lose } else -> Result.Win } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt b/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt index f1f3799e0..8c1f8a5af 100644 --- a/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt +++ b/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt @@ -11,39 +11,28 @@ import blackjack.participant.status.Bust object MatchingScoreCalculator { - fun matchingScore(player: Player, dealer: Dealer): Result { + fun matchingScoreForDealer(players: List, dealer: Dealer): BettingAmount { var bettingAmount = BettingAmount(0) - if (player.status is Blackjack) { - return when (dealer.status) { - is Blackjack -> Result.Lose - else -> { - bettingAmount -= player.status.calculateBettingAmount( - Result.Win, - player.bettingAmount - ) - - return Result.Win - } + players.forEach { + if (it.status is Blackjack && dealer.status !is Blackjack) { + bettingAmount -= it.status.calculateBettingAmount( + Result.Win, + it.bettingAmount + ) } - } - - if (player.status is Bust) { - bettingAmount += player.bettingAmount - return Result.Lose - } - if (dealer.status is Bust) { - bettingAmount -= player.bettingAmount - return Result.Win - } + if (it.status is Bust) { + bettingAmount += it.bettingAmount + } - return when (dealer.resultScore() > player.resultScore()) { - true -> { - bettingAmount += player.bettingAmount - Result.Lose + if (dealer.status is Bust) { + bettingAmount -= it.bettingAmount } - else -> Result.Win + if (dealer.resultScore() > it.resultScore()) { + bettingAmount += it.bettingAmount + } } + return bettingAmount } -} \ No newline at end of file +} diff --git a/src/test/kotlin/blackjack/GameResultGeneratorTest.kt b/src/test/kotlin/blackjack/supoort/GameResultGeneratorTest.kt similarity index 95% rename from src/test/kotlin/blackjack/GameResultGeneratorTest.kt rename to src/test/kotlin/blackjack/supoort/GameResultGeneratorTest.kt index 09dc55d62..9e20a7e3e 100644 --- a/src/test/kotlin/blackjack/GameResultGeneratorTest.kt +++ b/src/test/kotlin/blackjack/supoort/GameResultGeneratorTest.kt @@ -1,11 +1,9 @@ -package blackjack +package blackjack.supoort import blackjack.card.CardPattern import blackjack.card.CardPicture import blackjack.card.NormalCard import blackjack.card.PictureCard -import blackjack.supoort.GameResultGenerator -import blackjack.supoort.ScoreCalculator import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name diff --git a/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt b/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt index 56f0cb3e2..e4b29939f 100644 --- a/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt +++ b/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt @@ -1,67 +1,21 @@ package blackjack.supoort +import blackjack.card.AceCard import blackjack.card.CardPattern -import blackjack.card.CardPicture import blackjack.card.NormalCard -import blackjack.card.PictureCard +import blackjack.participant.BettingAmount import blackjack.participant.Dealer import blackjack.participant.Name import blackjack.participant.Player -import blackjack.participant.Result -import io.kotest.matchers.types.shouldBeInstanceOf +import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test class MatchingScoreCalculatorTest { private val scoreCalculator: ScoreCalculator = ScoreCalculator() - @Test - fun `딜러가 버스트 상태이고 플레이어가 버스트 상태가 아니라면 플레이어가 승리한다`() { - val dealer = Dealer(scoreCalculator) - dealer.drawCard( - listOf( - NormalCard(5, CardPattern.CLOVER), - NormalCard(10, CardPattern.CLOVER), - NormalCard(8, CardPattern.CLOVER), - ) - ) - - val player = Player(Name("홍길동"), scoreCalculator) - player.drawCard( - listOf( - PictureCard(CardPicture.KING, CardPattern.CLOVER), - PictureCard(CardPicture.KING, CardPattern.SPADE) - ) - ) - - MatchingScoreCalculator.matchingScore(player, dealer).shouldBeInstanceOf() - } - - @Test - fun `플레이어가 버스트 상태라면 플레이어가 패배한다`() { - val dealer = Dealer(scoreCalculator) - dealer.drawCard( - listOf( - NormalCard(5, CardPattern.CLOVER), - NormalCard(10, CardPattern.CLOVER), - NormalCard(8, CardPattern.CLOVER), - ) - ) - - val player = Player(Name("홍길동"), scoreCalculator) - player.drawCard( - listOf( - NormalCard(9, CardPattern.CLOVER), - PictureCard(CardPicture.KING, CardPattern.CLOVER), - PictureCard(CardPicture.KING, CardPattern.SPADE), - NormalCard(8, CardPattern.CLOVER), - ) - ) - - MatchingScoreCalculator.matchingScore(player, dealer).shouldBeInstanceOf() - } @Test - fun `플레이어가 점수가 딜러보다 높으면 플레이어가 승리한다`() { + fun `플레이어가 블랙잭 상태로 이기면 딜러에게서 배팅금액을 받는다`() { val dealer = Dealer(scoreCalculator) dealer.drawCard( listOf( @@ -70,35 +24,16 @@ class MatchingScoreCalculatorTest { ) ) - val player = Player(Name("홍길동"), scoreCalculator) + val player = Player(Name("홍길동"), BettingAmount(1000)) player.drawCard( listOf( - NormalCard(9, CardPattern.CLOVER), - PictureCard(CardPicture.KING, CardPattern.CLOVER), - ) - ) - - MatchingScoreCalculator.matchingScore(player, dealer).shouldBeInstanceOf() - } - - @Test - fun `플레이어가 점수가 딜러보다 낮으면 플레이어가 패배한다`() { - val dealer = Dealer(scoreCalculator) - dealer.drawCard( - listOf( - NormalCard(5, CardPattern.CLOVER), + AceCard(CardPattern.CLOVER), NormalCard(10, CardPattern.CLOVER), ) ) - val player = Player(Name("홍길동"), scoreCalculator) - player.drawCard( - listOf( - NormalCard(4, CardPattern.CLOVER), - NormalCard(10, CardPattern.CLOVER), - ) - ) + val dealerBettingAmount = MatchingScoreCalculator.matchingScoreForDealer(listOf(player), dealer) - MatchingScoreCalculator.matchingScore(player, dealer).shouldBeInstanceOf() + dealerBettingAmount.amount shouldBe -1500 } } From cfc087fad07a965a6c4bc454366da79198c27206 Mon Sep 17 00:00:00 2001 From: Ted1_Lee Date: Mon, 11 Dec 2023 09:46:51 +0900 Subject: [PATCH 43/44] feat: ktlint --- src/main/kotlin/blackjack/participant/Dealer.kt | 2 -- src/main/kotlin/blackjack/supoort/GameResultGenerator.kt | 4 ++-- .../kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt | 1 - 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/blackjack/participant/Dealer.kt b/src/main/kotlin/blackjack/participant/Dealer.kt index 4192ab410..80a3d268b 100644 --- a/src/main/kotlin/blackjack/participant/Dealer.kt +++ b/src/main/kotlin/blackjack/participant/Dealer.kt @@ -8,9 +8,7 @@ class Dealer( ) { private val blackjackStrategy: BlackjackStrategy = BlackjackStrategy(scoreCalculator) var bettingAmount: BettingAmount = BettingAmount(0) - val cards get() = blackjackStrategy.cards - val status get() = blackjackStrategy.status fun drawCard(cards: List) { diff --git a/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt b/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt index fa3ea36d9..58aed4a49 100644 --- a/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt +++ b/src/main/kotlin/blackjack/supoort/GameResultGenerator.kt @@ -12,7 +12,7 @@ object GameResultGenerator { fun generateGameResult(players: List, dealer: Dealer): GameResult { val dealerBettingAmount = MatchingScoreCalculator.matchingScoreForDealer(players, dealer) val resultMap = mapOf(Name("딜러") to dealerBettingAmount) + players.map { player -> - val result = matchingScore(player, dealer) + val result = matchingResult(player, dealer) val bettingAmount = player.status.calculateBettingAmount(result, player.bettingAmount) player.name to bettingAmount } @@ -20,7 +20,7 @@ object GameResultGenerator { return GameResult(resultMap) } - private fun matchingScore(player: Player, dealer: Dealer): Result { + private fun matchingResult(player: Player, dealer: Dealer): Result { if (player.status is Blackjack) { return when (dealer.status) { is Blackjack -> Result.Lose diff --git a/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt b/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt index e4b29939f..fb9c32b46 100644 --- a/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt +++ b/src/test/kotlin/blackjack/supoort/MatchingScoreCalculatorTest.kt @@ -13,7 +13,6 @@ import org.junit.jupiter.api.Test class MatchingScoreCalculatorTest { private val scoreCalculator: ScoreCalculator = ScoreCalculator() - @Test fun `플레이어가 블랙잭 상태로 이기면 딜러에게서 배팅금액을 받는다`() { val dealer = Dealer(scoreCalculator) From 1edce576f0f9c8d69e7fbf8e16ab0d944d3addcb Mon Sep 17 00:00:00 2001 From: Ted1_Lee Date: Mon, 8 Jan 2024 11:32:33 +0900 Subject: [PATCH 44/44] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/blackjack/GameManager.kt | 13 +-------- .../supoort/MatchingScoreCalculator.kt | 28 ++++++------------- 2 files changed, 10 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/blackjack/GameManager.kt b/src/main/kotlin/blackjack/GameManager.kt index 7a02c6250..cd081d5f9 100644 --- a/src/main/kotlin/blackjack/GameManager.kt +++ b/src/main/kotlin/blackjack/GameManager.kt @@ -35,21 +35,10 @@ class GameManager( outputManager.printDealerResultGame(dealer) - players.forEach { - outputManager.printPlayerResultGame(it) - } + players.forEach(outputManager::printPlayerResultGame) outputManager.printResult(result) } -// private fun playBlackJack(): GameResult { -// players.forEach { -// playerDraw(it) -// } -// dealerDraw(dealer) -// -// return GameResult(players, dealer) -// } - private fun playBlackJack() { players.forEach { playerDraw(it) diff --git a/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt b/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt index 8c1f8a5af..cba9afc5f 100644 --- a/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt +++ b/src/main/kotlin/blackjack/supoort/MatchingScoreCalculator.kt @@ -12,27 +12,17 @@ import blackjack.participant.status.Bust object MatchingScoreCalculator { fun matchingScoreForDealer(players: List, dealer: Dealer): BettingAmount { - var bettingAmount = BettingAmount(0) - players.forEach { - if (it.status is Blackjack && dealer.status !is Blackjack) { - bettingAmount -= it.status.calculateBettingAmount( - Result.Win, - it.bettingAmount - ) - } - - if (it.status is Bust) { - bettingAmount += it.bettingAmount - } - - if (dealer.status is Bust) { - bettingAmount -= it.bettingAmount - } - - if (dealer.resultScore() > it.resultScore()) { - bettingAmount += it.bettingAmount + val bettingAmount = players.fold(BettingAmount(0)) { acc, player -> + val updatedAmount = when { + player.status is Blackjack && dealer.status !is Blackjack -> acc - player.status.calculateBettingAmount(Result.Win, player.bettingAmount) + player.status is Bust -> acc + player.bettingAmount + dealer.status is Bust -> acc - player.bettingAmount + dealer.resultScore() > player.resultScore() -> acc + player.bettingAmount + else -> acc } + BettingAmount(updatedAmount.amount) } + return bettingAmount } }