-
Notifications
You must be signed in to change notification settings - Fork 313
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
[step4] 블랙잭 (베팅) & step3 코드리뷰 반영 #812
base: sarahan774
Are you sure you want to change the base?
Changes from all commits
dc0a080
0f27917
daa723b
4fd6961
126be39
ce186a3
8b26a92
ec35146
38c0b9d
995344b
3e26696
9944f63
17e1d6b
bf2dcc2
ae042c1
1ab98c9
6861336
d6ac32a
204492f
c2de14a
7f3f1ce
4ab56d9
4d5b3ca
8876e4b
e77f4f5
28a20fa
334f5d8
4a2a268
7c81404
3c5be6d
223331d
fde1866
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package blackjack.domain | ||
|
||
import blackjack.domain.GameResult.* | ||
import java.math.BigDecimal | ||
import java.math.RoundingMode | ||
|
||
@JvmInline | ||
value class BetMoney(private val amount: BigDecimal) { | ||
fun getAmount(gameResult: GameResult): BigDecimal { | ||
return when (gameResult) { | ||
BLACK_JACK -> getAmountOnBlackJack() | ||
WIN -> getOriginalBetAmount() | ||
PUSH -> getOriginalBetAmount() | ||
LOSE -> getAmountOnLose() | ||
BUST -> getAmountOnBust() | ||
} | ||
} | ||
|
||
private fun getOriginalBetAmount(): BigDecimal { | ||
return amount | ||
} | ||
|
||
private fun getAmountOnBlackJack(): BigDecimal { | ||
return amount.multiply((1.5).toBigDecimal()).setScale(0, RoundingMode.DOWN) | ||
} | ||
|
||
private fun getAmountOnBust(): BigDecimal { | ||
return -(amount) | ||
} | ||
|
||
private fun getAmountOnLose(): BigDecimal { | ||
return -(amount) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,27 @@ | ||
package blackjack.domain | ||
|
||
class BlackJackResultManager( | ||
private val dealer: Dealer, | ||
private val players: Players, | ||
private val playerResultCalculator: PlayerResultCalculator = PlayerResultCalculator(), | ||
) { | ||
fun getResult(): BlackJackResult { | ||
val dealerScore = dealer.cardsSum | ||
val playersWinLose = | ||
players.value.associateWith { player -> | ||
playerResultCalculator.calculate(dealerScore, player.cardsSum) | ||
} | ||
import java.math.BigDecimal | ||
|
||
val dealerWinCount = playersWinLose.count { it.value == PlayerResult.LOSE } | ||
val dealerLoseCount = playersWinLose.count { it.value == PlayerResult.WIN } | ||
return BlackJackResult(dealerWinCount, dealerLoseCount, PlayerToResultMap(playersWinLose)) | ||
object BlackJackResultManager { | ||
fun getResult( | ||
dealer: Dealer, | ||
players: Players, | ||
): BlackJackResult { | ||
val playersProfits = | ||
players.getPlayersToProfitMoney( | ||
dealer.isBlackJackInitially, | ||
dealer.cardsSum, | ||
) | ||
return BlackJackResult(playersProfits) | ||
} | ||
} | ||
|
||
data class BlackJackResult( | ||
val dealerWinCount: Int, | ||
val dealerLoseCount: Int, | ||
val playerToResultMap: PlayerToResultMap, | ||
) | ||
|
||
@JvmInline | ||
value class PlayerToResultMap(val value: Map<Player, PlayerResult>) | ||
val playerToProfit: PlayerToProfitMoney, | ||
) { | ||
val dealerProfitMoney: ProfitMoney get() = ProfitMoney().apply { set(-playerToProfit.getAllProfitSum) } | ||
} | ||
|
||
enum class PlayerResult { | ||
WIN, | ||
LOSE, | ||
data class PlayerToProfitMoney(val value: Map<Player, ProfitMoney>) { | ||
val getAllProfitSum: BigDecimal get() = value.values.sumOf { it.getCurrentProfit() } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package blackjack.domain | ||
|
||
enum class GameResult { | ||
WIN, | ||
BUST, | ||
LOSE, | ||
PUSH, | ||
BLACK_JACK, | ||
; | ||
|
||
companion object { | ||
fun getGameResultsWith( | ||
isPlayerBlackJackInitially: Boolean, | ||
isDealerBlackJackInitially: Boolean, | ||
dealerCardSum: Int, | ||
playerCardSum: Int, | ||
Comment on lines
+13
to
+16
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. 4개의 파라미터를 다루어야 하는 부분은 부담으로 다가올 수 있을것 같습니다. 4개의 파라미터가 타입도 2개씩 동일하여 순서를 잘못 지켜 주입한다면 의도치 않게 동작할 수도 있을것 같네요! 이러한 값을 추상화하여 표현해볼 수 없을지 한번 고민해보시면 좋을것 같습니다. |
||
): GameResult { | ||
return when { | ||
isPlayerBlackJackInitially && isDealerBlackJackInitially.not() -> BLACK_JACK | ||
isPlayerBlackJackInitially && isDealerBlackJackInitially -> PUSH | ||
else -> fromScores(dealerCardSum, playerCardSum) | ||
} | ||
} | ||
|
||
private fun fromScores( | ||
dealerScore: Int, | ||
playerScore: Int, | ||
): GameResult { | ||
return when { | ||
dealerScore > Card.MAX_SUM -> WIN // Dealer bust | ||
playerScore > Card.MAX_SUM -> BUST // Player bust | ||
dealerScore > playerScore -> LOSE | ||
playerScore > dealerScore -> WIN | ||
else -> PUSH | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,15 @@ package blackjack.domain | |
|
||
abstract class Participant( | ||
private val drawCard: () -> Card, | ||
initialCardCount: Int = 2, | ||
) { | ||
private val _cards = mutableListOf<Card>() | ||
val cards: Cards = Cards(_cards) | ||
val cardsSum: Int get() = cards.sumValues() | ||
val isBlackJackInitially: Boolean | ||
|
||
init { | ||
repeat(initialCardCount) { addCard(drawCard()) } | ||
repeat(INITIAL_CARD_COUNT) { addCard(drawCard()) } | ||
isBlackJackInitially = cardsSum == Card.MAX_SUM | ||
Comment on lines
+9
to
+13
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. 여러가지 상태를 관리하는것 같습니다. Cards를 가지고 점수를 계산하고 이러한 상태가 관리되는것 같은데요 디자인 패턴중 상태패턴을 이용한다면 이러한 부분을 개선해볼 수 있을것 같습니다. 반드시 개선을 바라는 부분은 아니니 참고만 해주셔도 좋습니다. 😄 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. @pci2676 요거 혹시 간단하게 예시를 주실 수 있을까요? 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. 블랙잭 피드백에 나와있는 내용이군요! 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. 제가 드리는 리뷰를 반드시 반영할 필요는 없으니 참고만 해주셔도 됩니다~ |
||
} | ||
|
||
private fun addCard(card: Card) { | ||
|
@@ -29,4 +30,8 @@ abstract class Participant( | |
} | ||
|
||
abstract fun isAddCardEnabled(): Boolean | ||
|
||
companion object { | ||
private const val INITIAL_CARD_COUNT = 2 | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package blackjack.domain | ||
|
||
import java.math.BigDecimal | ||
|
||
class ProfitMoney { | ||
private var current: BigDecimal = BigDecimal.ZERO | ||
|
||
fun getCurrentProfit(): BigDecimal { | ||
return current | ||
} | ||
|
||
fun set(amount: BigDecimal) { | ||
current = amount | ||
} | ||
} | ||
Comment on lines
+5
to
+15
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. kotlin을 사용하신다면 가능한 불변한 객체를 설계하여 사용하시는것을 권장드립니다~ https://kotlinlang.org/docs/coding-conventions.html#idiomatic-use-of-language-features |
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.
이러한 연관관계를 GameResult의 멤버변수로 표현해보는 방향은 어떻게 생각하시나요?