-
Notifications
You must be signed in to change notification settings - Fork 318
[Step4] 블랙잭(베팅) #797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: kimihiqq
Are you sure you want to change the base?
[Step4] 블랙잭(베팅) #797
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ | |
## Step3 | ||
- [x] 딜러 카드 관리 | ||
- [x] 게임 진행 로직(딜러) | ||
|
||
## Step4 | ||
- [x] 게임 진행 로직(베팅) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,11 @@ class Game( | |
private val deck: Deck, | ||
private val participants: GameParticipants, | ||
) { | ||
companion object { | ||
private const val BUST_THRESHOLD = 21 | ||
private const val BLACKJACK_MULTIPLIER = 1.5 | ||
} | ||
|
||
fun start() { | ||
participants.distributeInitialCards(deck) | ||
} | ||
|
@@ -16,7 +21,33 @@ class Game( | |
participants.dealer.playTurn(deck) | ||
} | ||
|
||
fun calculateResults(): GameResult { | ||
return GameResultCalculator.calculate(participants.dealer, participants.players) | ||
fun calculateFinalResults(): Map<String, Int> { | ||
val dealerBlackjack = participants.dealer.isBlackjack() | ||
val dealerScore = participants.dealer.getTotalValue() | ||
|
||
val playerResults = | ||
participants.players.getPlayers() | ||
.associate { player -> | ||
player.name to calculatePlayerResult(player, dealerScore, dealerBlackjack) | ||
} | ||
|
||
val dealerProfit = playerResults.values.sum() * -1 | ||
return playerResults + ("Dealer" to dealerProfit) | ||
Comment on lines
+28
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. player result 객체를 만들어보는건 어떨까요? 🙂 |
||
} | ||
|
||
private fun calculatePlayerResult( | ||
player: Player, | ||
dealerScore: Int, | ||
dealerBlackjack: Boolean, | ||
): Int { | ||
val playerBlackjack = player.isBlackjack() | ||
return when { | ||
player.getTotalValue() > BUST_THRESHOLD -> -player.getBettingAmount() | ||
dealerScore > BUST_THRESHOLD -> player.getBettingAmount() | ||
dealerBlackjack && playerBlackjack -> 0 | ||
playerBlackjack -> (player.getBettingAmount() * BLACKJACK_MULTIPLIER).toInt() | ||
player.getTotalValue() > dealerScore -> player.getBettingAmount() | ||
else -> -player.getBettingAmount() | ||
} | ||
Comment on lines
+38
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Score 라는 객체를 만들어 일부 혹은 전체 기능의 일을 위임해보면 어떨까요? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
package blackjack.domain | ||
|
||
class Player(val name: String) : Participant() | ||
class Player( | ||
val name: String, | ||
private var bettingAmount: Int, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 변수의 값은 바뀌지 않아보여요 ! |
||
) : Participant() { | ||
fun getBettingAmount(): Int { | ||
return bettingAmount | ||
} | ||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코틀린은 외부에서 사용할 게터는 자동으로 정의해주기 때문에 필요없어보여요 😀 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,11 @@ object InputView { | |
return readlnOrNull()?.split(",")?.map { it.trim() } ?: emptyList() | ||
} | ||
|
||
fun getBettingAmount(playerName: String): Int { | ||
println("$playerName 의 배팅 금액은?") | ||
return readlnOrNull()?.toIntOrNull() ?: 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 베팅 금액이 없으면 게임을 시작하지 못하게 해야 하지 않을까요? 🧐 |
||
} | ||
|
||
fun askToHit(playerName: String): Boolean { | ||
println("$playerName 은 한 장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)") | ||
return readlnOrNull()?.lowercase() == "y" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import io.kotest.matchers.shouldBe | |
|
||
class PlayersTest : StringSpec({ | ||
"Players should distribute two cards to each player" { | ||
val players = Players(listOf(Player("pobi"), Player("jason"))) | ||
val players = Players(listOf(Player("pobi", 1000), Player("jason", 1000))) | ||
Comment on lines
7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. players 도 getter 가 있는데, 코틀린 기본 게터를 사용해보는게 어떨까요? |
||
val deck = Deck() | ||
|
||
players.distributeInitialCards(deck) | ||
|
@@ -17,7 +17,7 @@ class PlayersTest : StringSpec({ | |
} | ||
|
||
"Players should allow a specific player to draw a card" { | ||
val players = Players(listOf(Player("pobi"), Player("jason"))) | ||
val players = Players(listOf(Player("pobi", 1000), Player("jason", 1000))) | ||
val deck = Deck() | ||
|
||
players.distributeInitialCards(deck) | ||
|
@@ -28,7 +28,7 @@ class PlayersTest : StringSpec({ | |
} | ||
|
||
"Players should throw exception if a non-existent player tries to hit" { | ||
val players = Players(listOf(Player("pobi"), Player("jason"))) | ||
val players = Players(listOf(Player("pobi", 1000), Player("jason", 1000))) | ||
val deck = Deck() | ||
|
||
shouldThrow<NoSuchElementException> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앞에 -(마이너스) 를 달아서 간단하게 표현해도 좋을 것 같아요 🙂