-
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
Step3: 블랙잭(딜러) #677
base: wilgur513
Are you sure you want to change the base?
Step3: 블랙잭(딜러) #677
Changes from 12 commits
7632dbd
46dc4e3
ee1e71d
604658b
1921a1e
f335407
a45d6fe
cfdfb7d
21c71a2
77f3671
b37b6c3
adb91b3
1fac2fd
cd65ff5
8c995ac
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 |
---|---|---|
@@ -1,26 +1,55 @@ | ||
package blackjack | ||
|
||
import blackjack.domain.Dealer | ||
import blackjack.domain.ShuffledCardDeck | ||
import blackjack.domain.Player | ||
import blackjack.view.InputView | ||
import blackjack.view.OutputView | ||
|
||
fun main() { | ||
val cardDeck = ShuffledCardDeck() | ||
val (dealer, players) = createParticipants(cardDeck) | ||
obtainCards(players, dealer) | ||
compareBetweenDealerAndPlayers(dealer, players) | ||
} | ||
|
||
private fun createParticipants(cardDeck: ShuffledCardDeck): Pair<Dealer, List<Player>> { | ||
val dealer = Dealer(cardDeck) | ||
val players = createPlayers(cardDeck) | ||
OutputView.printParticipantOpenedCards(listOf(dealer) + players) | ||
return Pair(dealer, players) | ||
} | ||
|
||
private fun createPlayers(cardDeck: ShuffledCardDeck): List<Player> { | ||
val names = InputView.inputNames() | ||
val players = names.map { Player(it, ShuffledCardDeck()) } | ||
return names.map { Player(it, cardDeck) } | ||
} | ||
|
||
OutputView.printPlayersCards(players) | ||
players.forEach { obtainCard(it) } | ||
OutputView.printPlayerResult(players) | ||
private fun obtainCards(players: List<Player>, dealer: Dealer) { | ||
players.forEach { obtainPlayerCard(it) } | ||
obtainDealerCard(dealer) | ||
} | ||
|
||
private fun obtainCard(player: Player) { | ||
while (isObtainCard(player)) { | ||
private fun obtainPlayerCard(player: Player) { | ||
while (isPlayerObtainCard(player)) { | ||
player.obtain() | ||
OutputView.printPlayerCards(player) | ||
OutputView.printParticipantCards(player.name, player.hands) | ||
} | ||
} | ||
|
||
private fun isObtainCard(player: Player): Boolean { | ||
private fun isPlayerObtainCard(player: Player): Boolean { | ||
return player.isObtainable() && InputView.inputIsObtainCard(player.name) | ||
} | ||
|
||
private fun obtainDealerCard(dealer: Dealer) { | ||
while(dealer.isObtainable()) { | ||
dealer.obtain() | ||
OutputView.printObtainDealerCard() | ||
} | ||
} | ||
|
||
private fun compareBetweenDealerAndPlayers(dealer: Dealer, players: List<Player>) { | ||
val compareResults = dealer.compareWith(*players.toTypedArray()) | ||
OutputView.printParticipantHands(listOf(dealer) + players) | ||
OutputView.printCompareResults(compareResults) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package blackjack.domain | ||
|
||
enum class CompareResult { | ||
DEALER_LOSE, | ||
DRAW, | ||
DEALER_WIN | ||
; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package blackjack.domain | ||
|
||
class Dealer( | ||
cardDeck: CardDeck | ||
): Participant("딜러", cardDeck) { | ||
override val openedCards = listOf(hands.first()) | ||
|
||
override fun isObtainable(): Boolean { | ||
return sumOfCards() < 17 | ||
} | ||
|
||
fun compareWith(vararg players: Player): Map<String, CompareResult> { | ||
return players.associate { it.name to compareWith(it) } | ||
} | ||
|
||
private fun compareWith(player: Player): CompareResult { | ||
if (isMoreThanBlackjack()) { | ||
return CompareResult.DEALER_LOSE | ||
} | ||
if (player.isMoreThanBlackjack()) { | ||
return CompareResult.DEALER_WIN | ||
} | ||
return compareBySumOfCards(player) | ||
} | ||
|
||
private fun compareBySumOfCards(player: Player): CompareResult { | ||
if (sumOfCards() == player.sumOfCards()) { | ||
return CompareResult.DRAW | ||
} | ||
if (sumOfCards() < player.sumOfCards()) { | ||
return CompareResult.DEALER_LOSE | ||
} | ||
return CompareResult.DEALER_WIN | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package blackjack.domain | ||
|
||
abstract class Participant( | ||
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 정의 👍 |
||
val name: String, | ||
private val cardDeck: CardDeck, | ||
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. Card를 받도록 수정했습니다 |
||
) { | ||
private val cards = Cards(cardDeck.next(), cardDeck.next()) | ||
abstract val openedCards: List<Card> | ||
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. openedCards 를 상태값으로 가지는것도 좋지만 반드시 가지고 있어야 하는것은 아닐것 같아요. |
||
|
||
val hands | ||
get() = cards.values | ||
Comment on lines
+8
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. hands 추가 👍 |
||
|
||
fun obtain() { | ||
require(isObtainable()) { "카드를 획득할 수 없습니다." } | ||
cards.add(cardDeck.next()) | ||
} | ||
|
||
fun sumOfCards(): Int { | ||
return cards.sum() | ||
} | ||
|
||
fun isMoreThanBlackjack(): Boolean { | ||
return sumOfCards() > BLACKJACK_SCORE | ||
} | ||
|
||
abstract fun isObtainable(): Boolean | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,12 @@ | ||
package blackjack.domain | ||
|
||
class Player( | ||
val name: String, | ||
private val cardDeck: CardDeck, | ||
) { | ||
private val cards = Cards(cardDeck.next(), cardDeck.next()) | ||
val hands | ||
get() = cards.values | ||
|
||
fun obtain() { | ||
require(isObtainable()) { "카드를 획득할 수 없습니다." } | ||
cards.add(cardDeck.next()) | ||
} | ||
|
||
fun sumOfCards(): Int { | ||
return cards.sum() | ||
} | ||
|
||
fun isObtainable(): Boolean { | ||
return cards.isLessThanBlackjack() | ||
class Player ( | ||
name: String, | ||
cardDeck: CardDeck, | ||
) : Participant(name, cardDeck) { | ||
override val openedCards = hands.subList(0, 2) | ||
|
||
override fun isObtainable(): Boolean { | ||
return sumOfCards() < BLACKJACK_SCORE | ||
Comment on lines
+9
to
+10
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 comment
The reason will be displayed to describe this comment to others. Learn more.
main 함수가 너무 많은 책임을 가지고 있는것 같습니다.
블랙잭 게임을 진행하기 위한 클래스를 정의해보는건 어떨까요 ?
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.
Blackjack 클래스 추출했습니다.