-
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] : ๐ 4๋จ๊ณ - ๋ธ๋์ญ(๋ฒ ํ ) #703
base: dlwnsgus777
Are you sure you want to change the base?
Conversation
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.
์๋ ํ์ธ์ ์คํ๋!
๊ตฌํ์ ์ ํด์ฃผ์
จ์ต๋๋ค ๐ฏ ๊น๋ํ ๊ตฌํ์ด ์ข์์ต๋๋ค.
ํ
์คํธ ์ผ์ด์ค๋ค์ด ์ง๊ด์ ์ผ๋ก ๋ณํ ๋ชจ์ต์ด ์ธ์์ ์ด๋ค์ ๐
๊ณ ๋ฏผํด๋ณด์๋ฉด ์ข์๋งํ ์ฝ๋ฉํธ๋ฅผ ๋ฌ์์ผ๋ ํ์ธํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.
๊ธฐํ์ด ์ง๋ฌ๋๋ฐ๋ ๋ฏธ์
์งํํ์๋ ๋ชจ์ต์ด ๋๋จํ์ญ๋๋ค!
๊ถ๊ธํ ์ ์ด๋ ๋
ผ์ํ๊ณ ์ถ์ ๋ด์ฉ์ด ์์ผ๋ฉด ์ธ์ ๋ DM ์ฃผ์ธ์! ๐ ๐ช
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.
๋ค์ ์ฝ๋ฉํธ์ ๋ํด ๊ณ ๋ฏผํด๋ณด์ธ์ ๐
#656 (comment)
val playerResult = mutableMapOf<Name, BettingAmount>() | ||
|
||
val (winCount, loseCount) = when { | ||
dealer.isBust -> players.partition { !it.isBust } | ||
else -> players.partition { it.resultScore() < dealer.resultScore() } | ||
players.forEach { | ||
val result = dealer.matchingScore(it) | ||
playerResult[it.name] = it.status.calculateBettingAmount(result, it.bettingAmount) | ||
} |
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.
mutableMapOf ์ฌ์ฉ ์์ด players.map { ... }
๋ง์ผ๋ก๋ ๊ตฌํํ ์ ์์ต๋๋ค!
companion object { | ||
private const val WIN: String = "์น" | ||
private const val LOSE: String = "ํจ" | ||
resultMap = mapOf(Name("๋๋ฌ") to dealer.bettingAmount) + playerResult |
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๋ฅผ ํตํด ํ๋ ์ด์ด์ ๋๋ฌ์ ๊ณตํต๋ ๊ฒฐ๊ณผ๋ฅผ ์กฐํํ ์ ์๊ฒ ๋์์ง๋ง ๋ถํ์ํ ๋๋ฌ์ Name ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ฒ ๋์์ด์.
๊ฐ๋ฐ์๋ค๋ง๋ค ์๊ฒฌ์ด ๊ฐ๋ฆด ์ ์์ ๊ฒ ๊ฐ์๋ฐ ์คํ๋์ ์ด๋ป๊ฒ ์๊ฐํ์๋์?
import kotlin.math.roundToInt | ||
|
||
@JvmInline | ||
value class BettingAmount( |
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.
value class ํ์ฉ ๐
operator overloading๋ ์ ์ฉํด๋ณด๋ฉด ์ด๋จ๊น์?
https://kotlinlang.org/docs/operator-overloading.html
private fun isBlackjack(): Boolean { | ||
return resultScore() == BLACKJACK | ||
} |
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.
isBust์ isBlackjack()์ ๊ฐ๊ฐ ํ๋กํผํฐ, ํจ์๋ก ์ ์ธ๋์๋ค์.
val isBust get() = resultScore() > BLACKJACK
์ด๋ค ์ํฉ์์ ํ๋กํผํฐ๋ฅผ ์ ํธํ๊ณ ํจ์๋ฅผ ์ ํธํด์ผ ํ ๊น์?
๋ค์ ๊ธ์ด ๋์๋์์ผ๋ฉด ์ข๊ฒ ์ด์.
private val blackjackStrategy: BlackjackStrategy = BlackjackStrategy(scoreCalculator) | ||
var bettingAmount: BettingAmount = BettingAmount(0) |
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.
matchingScore()
์์ ๋ฆฌํดํ ๋ BettingAmount ์ ๋ณด๋ฅผ ๊ฐ์ด ๋ฐํํ๋ค๋ฉด ๊ตณ์ด variable์ ์ ์ธํ์ง ์์๋ ๋ ๊ฒ ๊ฐ์์
๋ชจ๋ ์ํฐํฐ๋ฅผ ์๊ฒ ์ ์งํ๋ค.
3๊ฐ ์ด์์ ์ธ์คํด์ค ๋ณ์๋ฅผ ๊ฐ์ง ํด๋์ค๋ฅผ ์ฐ์ง ์๋๋ค.
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.
๋๋ถ์ด ์ง๊ธ ๋๋ฌ์์ ํ๊ณ ์๋ ์ผ์ด ๋๋ฌด ๋ง์ต๋๋ค.
matchingScore()
๊ณผ ๊ฐ์ ๋ก์ง์ด ๋๋ฌ์ ์๋๊ฒ ์ ์ ํ ๊น์?
์น/ํจ ๊ณ์ฐ ๋ฐ ๋ฐฐํ
๊ธ์ก ๊ฒฐ๊ณผ๋ฅผ ์ป์ด์ค๊ธฐ ์ํด์ ๋๋ฌ์๊ฒ ์์ฒญํ๋ ๊ฒ์ด ์๋, ๋ณ๋์ ๊ฐ์ฒด์๊ฒ ์์ฒญํ๋ ํํ๋ก ๋ฐ๊ฟ๋ณด๋ฉด ์ด๋จ๊น์?
changeStatus(cards) | ||
} | ||
|
||
private fun changeStatus(cards: List<BlackJackCard>) { | ||
when { | ||
!isFirstTurn(cards) && !isBust -> status = Stand() | ||
!isFirstTurn(cards) && isBust -> status = Bust() | ||
isFirstTurn(cards) && isBlackjack() -> status = Blackjack() | ||
} | ||
} |
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.
๋ง์ง๋ง ๊ฐ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ด๋ฌํ ๋ก์ง๋ ๊ฐ๊ฐ์ ์ํ ๊ฐ์ฒด์์ ํ๋จํ ์ ์๋๋ก ๋ฐ๊ฟ๋ณด๋ฉด ์ด๋จ๊น์? (์ํ ํจํด)
์ด ์ฝ๋ฉํธ ๋ฐ์์ ์ ํ์ฌํญ์ผ๋ก ์งํํ์
๋ ๋ฉ๋๋ค ๐
์๋ ํ์ธ์ ๋ฆฌ๋ทฐ์ด๋!! ๊ฐ์ ์ง๋ ํ์๋ ๋ฆฌ๋ทฐํด์ฃผ์ ์ ๋ ๊ฐ์ฌํ ๋ง์์ ๋๋ค ๐ ํผ๋๋ฐฑ ๋ฐ์ํ๋ฉด์ ๊ถ๊ธํ ์ ์ด ์๊ฒจ ๋ช๊ฐ์ง ์ง๋ฌธ๋๋ ค๋ด ๋๋ค!!
๋ํ ๋ค๋ฅธ ๋ค๋ง ์ด์๊ฐ์ ์๋ฌธ์ ์ฝ๋์ ๋ชจ์ ์ข ๋ ๊ฐ๋ ์ฑ ์๊ฒ ์ฒ๋ฆฌํ ๋ฐฉ๋ฒ์ ์๊ฐํด๋ณด๊ฒ ์ต๋๋ค! ๊ณ ๋ฏผํ ๋ถ๋ถ๋๋ฌ์ ๋ํ ์ฑ
์์ด ๋ง์ ๋ถ๋ฆฌ๋ฅผ ํ๋ ๋์ค ์ด๋ด ๋ ์ด๋ค์์ผ๋ก ๋ฆฌํฉํฐ๋งํ ์ง ๊ฐ์ด ์กํ์ง ์์ ์ง๋ฌธ๋๋ ค๋ด ๋๋ค!! ์๋ถํ๋๋ฆฝ๋๋ค!! ๐ข |
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.
์๋ ํ์ธ์ ์คํ๋! ๋ฆฌ๋ทฐ ๋ฐ์์ ์ ํด์ฃผ์ จ์ต๋๋ค ๐ฏ
์กฐ๊ธ ๊ณ ๋ฏผํด๋ณด์๋ฉด ์ข์ ๋์ ์ ์ธ ๋ด์ฉ์ ์ฝ๋ฉํธ๋ก ๋ฌ์์ผ๋
ํธํ๊ฒ ๊ณ ๋ฏผํด๋ณด์๊ณ ๋ง์๊ป ๋์ ํด๋ณด์ธ์.
์๋ง ๋ค์ ๋ฆฌ๋ทฐ ์์ฒญ์์๋ ๋จธ์งํ ์ ์์ ๊ฒ ๊ฐ์์.
๋ง์ง๋ง๊น์ง ํ์ดํ
์
๋๋ค ๐ช
import blackjack.participant.status.Blackjack | ||
import blackjack.participant.status.Bust | ||
|
||
object MatchingScoreCalculator { |
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.
๋๋ฌ์ ๋ํ ์ฑ ์์ด ๋ง์ ๋ถ๋ฆฌ๋ฅผ ํ๋ ๋์ค GameResultGenerator ์ MatchingScoreCalculator ํด๋์ค๊ฐ ํ์๋์๋๋ฐ
์ด ๋์ ๋ก์ง์ด ๋น์ทํ ๋ถ๋ถ์ด ๋ง์ต๋๋ค... ๋จ์ํ return ๊ฐ๋ง ๋ค๋ฅด๋ค๋ ๋๋์ธ๋ฐ
์ด๋ด ๋ ์ด๋ค์์ผ๋ก ๋ฆฌํฉํฐ๋งํ ์ง ๊ฐ์ด ์กํ์ง ์์ ์ง๋ฌธ๋๋ ค๋ด ๋๋ค!!
#703 (comment)
์ด ์ฝ๋ฉํธ๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ํ ํจํด์ผ๋ก ๋ง๋ค์ด๋ณด๋ฉด ์ด๋จ๊น์? ์ง๊ธ๋ Status ๊ฐ์ฒด๊ฐ ์์ง๋ง ๋ ๋ง์ ์ฑ
์์ ์ํ๋ก ์์ํ ์ ์์ต๋๋ค.
์ด๋์๋ถํฐ ํจํด์ ๋ง๋ค์ด์ผ ํ ์ง ๊ฐ์ด ์ ์ค์ง ์๋๋ค๋ฉด ์ํ ๊ตฌ์กฐ๋ฅผ ์ฒ์๋ถํฐ ๊ตฌํํด๋ณด์๋ ๊ฒ๋ ์ถ์ฒ๋๋ ค์ ๐ ์ฒ์์๋ ์ด๋ ต๊ฒ ๋๊ปด์ง๋ ๋ธ๋์ญ ๋ฃฐ๋ ๋ค์ ๋ง๋ค์ด๋ณด๋ฉด ์๊ฐ๋ณด๋ค ์ฝ๊ฒ ๊ตฌํ๋ ๊ฑฐ์์.
var bettingAmount = BettingAmount(0) | ||
players.forEach { |
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.
Kotlin Collection์ fold, reduce ๋ฑ์ API๋ฅผ ํ์ฉํ๋ฉด ์ด ๋ก์ง๋ var ์ ์ธ ์์ด ๋ถ๋ณ์ผ๋ก ๋ง๋ค ์ ์์ต๋๋ค.
์ด ์ฝ๋ฉํธ๋ ์ฐธ๊ณ ๋ง ํด์ฃผ์
๋ ์ข์ต๋๋ค!
import blackjack.participant.Name | ||
|
||
data class GameResult( | ||
val resultMap: Map<Name, BettingAmount> |
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.
Name ๊ฐ์ฒด์ฒ๋ผ ์์๊ฐ์ ๊ฐ์ผ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ์ด์ ์ ๋ํด์ ๋๋ฆ๋๋ก์ ์๊ฐ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค!
- ์ฝ๋ ๊ฐ๋ ์ฑ์ ์ํด
- ์ด๋ฆ์ ๋ํ ์๊ตฌ์ฌํญ์ด ์๊ฒผ์ ๋ ์ฑ
์์ Name์ด ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ ์ ์ง๋ณด์ ์ธก๋ฉด์์ ์ข๋ค (Name ํด๋์ค๋ง ์์ ํ๋ฉด ๋๊ธฐ ๋๋ฌธ)
๋ํ ๋ค๋ฅธ Player ๊ฐ์ฒด์์๋ ์ฌ์ฉํ๊ณ ์๊ธฐ๋ ํ๊ณ ๋ชจ๋ ์์๊ฐ์ ํฌ์ฅํ๋ผ๋ ๊ณผ์ ์ ์๊ตฌ์ฌํญ์ด๊ธฐ๋ ํ๊ธฐ ๋๋ฌธ์ ๋ถํ์ํ ๋๋ฌ์ Name ๊ฐ์ฒด ๋ฅผ ์์ฑํ๊ฒ ๋๋๋ผ๋ ๊ด์ฐฎ๋ค๋ ์๊ฐ์ ๋๋ค!
๋ค๋ง ์ด์๊ฐ์ ์๋ฌธ์ ์ฝ๋์ ๋ชจ์ mapOf(Name("๋๋ฌ") to dealer.bettingAmount) ์ด๋ฐ ํ์ ๋๋ฌธ์ ์๊ฒจ๋ฌ๋ค๊ณ ์๊ฐํฉ๋๋ค
์ข ๋ ๊ฐ๋ ์ฑ ์๊ฒ ์ฒ๋ฆฌํ ๋ฐฉ๋ฒ์ ์๊ฐํด๋ณด๊ฒ ์ต๋๋ค!
์คํ๋์ด ๋๋ฌ์ ํ๋ ์ด์ด์ ๊ด๊ณ๋ฅผ ์์์ด ์๋, ์กฐํฉ์ ํตํด ๊ตฌํํด์ฃผ์ ๋งํผ
์ด ๋์ ๊ฐ์ ํ์
์ผ๋ก ๊ตฌํํ์ง ์์ ์๋ ์์ง ์์๊น์?
์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ด์ ๐
val resultMap: Map<Name, BettingAmount> | |
data class GameResult( | |
val playerResultMap: Map<Name, BettingAmount> | |
val dealerResult: BettingAmount |
๋ ๋ง์ฝ GameResult๊ฐ playerResultMap๋ฅผ ์๊ณ ์๋ค๋ฉด ๊ตณ์ด dealerResult๋ฅผ ์ธ๋ถ์์ ๊ณ์ฐํด์ค ํ์ ์์ด GameResult๊ฐ ์์ฒด์ ์ผ๋ก ํ๋จํ ์ ์์ ๊ฒ ๊ฐ์์. ์ฆ, Generator ํด๋์ค์ ํ์ ์ ๋ฌด๋ ๊ฐ์ด ๊ณ ๋ฏผํด๋ณด์๋ฉด ์ข๊ฒ ์ต๋๋ค.
// private fun playBlackJack(): GameResult { | ||
// players.forEach { | ||
// playerDraw(it) | ||
// } | ||
// dealerDraw(dealer) | ||
// | ||
// return GameResult(players, dealer) | ||
// } |
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.
์ฌ์ฉํ์ง ์๋ ํจ์๋ ์ญ์ ํด์ฃผ์ธ์ ๐ VCS์ ๋ชจ๋ ๋จ์์์ผ๋ ์ฝ๋๋ฅผ ์ญ์ ํ๋ ๊ฒ์ ํธํ๊ฒ ์๊ฐํ์ ๋ ๋ฉ๋๋ค.
players.forEach { | ||
outputManager.printPlayerResultGame(it) | ||
} |
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.
์ด๋ฌํ ๊ตฌ๋ฌธ์ ํจ์ reference๋ก ๋๊ธธ ์ ์์ต๋๋ค.
players.forEach { | |
outputManager.printPlayerResultGame(it) | |
} | |
players.forEach(outputManager::printPlayerResultGame) |
lambda๋ก ์์ฑํ๋ ๊ฒ๊ณผ ์ด๋ค ์ฐจ์ด๊ฐ ์๋์ง ์ฐธ๊ณ ํ ์ ์๋ ๊ธ์ ์๊ฐํด๋๋ฆด๊ฒ์ ๐
https://proandroiddev.com/kotlin-lambda-vs-method-reference-fdbd175f6845
์ฐธ๊ณ ๋ก ์ ๋ ๊ฐ๋ฅํ ๊ณณ์์๋ ๊ผญ ํจ์ reference๋ฅผ ํ์ฉํ๋ ๊ฒ์ ์งํฅํฉ๋๋ค. lambda๋ก ๊ฐ์ ธ์จ ๋ฐ์ดํฐ๋ฅผ ๋ณ๋๋ก ์กฐ์ํ์ง ์๊ณ ๊ทธ๋๋ก ํจ์ ์๊ทธ๋์ฒ๋ก ๋๊ธด๋ค๋ ๊ฒ์ ๋๋ฌ๋ผ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค.
์๋ ํ์ธ์ ๋ฆฌ๋ทฐ์ด๋!!
๊ฐ์ ์ข ๋ฃ ํ์๋ ๋ฆฌ๋ทฐํด์ฃผ์ ๋ค๊ณ ํ์๋... ๊ทธ์ ๊ฐ์ฌํ ๋ฐ๋ฆ์ ๋๋คใ ใ ใ ๐
์์ฃผ๋ฅผ ๋ชฉํ๋ก ๋ฌ๋ ค๋ณด๊ฒ ์ต๋๋ค!!!
์ด๋ฒ ๋จ๊ณ๋ ์๋ถํ๋๋ฆฝ๋๋ค!!
๊ณ ๋ฏผํ ๋ถ๋ถ
ํ์ฐธ ๊ณ ๋ฏผํ๋ค๊ฐ
GameResult
์์ ํ๋ ๊ฒฐ๊ณผ ๊ณ์ฐ์Dealer
์๊ฒ ์์ํด ๊ฒฐ๊ณผ๋ฅผ ๊ณ์ฐํ๋ ํจ์ฌ ์์ํจ์ ๋๊ผ์ต๋๋ค!๊ทธ๋ฌ๋ ๊ฐ์ฒด ๊ฐ์ ์ฑ ์? ์ญํ ์ด ์ ๋๋์ด์ง์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค...
ํ์ง๋ง ์ด๋์๋ถํฐ ์๋๋๊ฐ์ผํ ์ง ๊ฐ์ด ์์ง ์์ ๋ฆฌ๋ทฐ์ ํ์ ๋น๋ ค๋ณด๋ ค pr๋๋ ค๋ด ๋๋คใ ใ