-
Notifications
You must be signed in to change notification settings - Fork 312
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 all 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,41 @@ | ||
package blackjack | ||
|
||
import blackjack.domain.BlackJack | ||
import blackjack.domain.ShuffledCardDeck | ||
import blackjack.domain.Player | ||
import blackjack.view.InputView | ||
import blackjack.view.OutputView | ||
|
||
fun main() { | ||
val names = InputView.inputNames() | ||
val players = names.map { Player(it, ShuffledCardDeck()) } | ||
val cardDeck = ShuffledCardDeck() | ||
val playerNames = InputView.inputNames() | ||
val blackjack = BlackJack(cardDeck, *playerNames.toTypedArray()) | ||
|
||
OutputView.printPlayersCards(players) | ||
players.forEach { obtainCard(it) } | ||
OutputView.printPlayerResult(players) | ||
OutputView.printParticipantOpenedCards(blackjack.openCardsOfParticipant()) | ||
obtainCards(playerNames, blackjack) | ||
OutputView.printCompareResults(blackjack.compareResults()) | ||
} | ||
|
||
private fun obtainCard(player: Player) { | ||
while (isObtainCard(player)) { | ||
player.obtain() | ||
OutputView.printPlayerCards(player) | ||
private fun obtainCards(playerNames: List<String>, blackjack: BlackJack) { | ||
obtainCardsForPlayers(playerNames, blackjack) | ||
obtainCardsForDealer(blackjack) | ||
OutputView.printParticipantHands(blackjack.participants()) | ||
} | ||
|
||
private fun obtainCardsForDealer(blackjack: BlackJack) { | ||
while (blackjack.isDealerObtainable()) { | ||
blackjack.obtainDealerCard() | ||
OutputView.printObtainDealerCard() | ||
} | ||
} | ||
|
||
private fun isObtainCard(player: Player): Boolean { | ||
return player.isObtainable() && InputView.inputIsObtainCard(player.name) | ||
private fun obtainCardsForPlayers(playerNames: List<String>, blackjack: BlackJack) { | ||
playerNames.forEach { obtainCardsForPlayer(it, blackjack) } | ||
} | ||
|
||
private fun obtainCardsForPlayer(name: String, blackjack: BlackJack) { | ||
val wantToTake = { InputView.inputIsObtainCard(name) } | ||
while (blackjack.isPlayerObtainable(name, wantToTake)) { | ||
val cards = blackjack.obtainPlayerCard(name) | ||
OutputView.printParticipantCards(name, cards) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package blackjack.domain | ||
|
||
import java.util.function.Supplier | ||
|
||
class BlackJack( | ||
private val cardDeck: CardDeck, | ||
vararg names: String | ||
) { | ||
private val dealer = Dealer(cardDeck.next(), cardDeck.next()) | ||
private val players = names.map { Player(it, cardDeck.next(), cardDeck.next()) } | ||
|
||
fun openCardsOfParticipant(): Map<String, List<Card>> { | ||
return participants().associate { it.name to it.openCards() } | ||
} | ||
|
||
fun isDealerObtainable(): Boolean { | ||
return dealer.isObtainable() | ||
} | ||
|
||
fun obtainDealerCard(): List<Card> { | ||
dealer.obtain(cardDeck.next()) | ||
return dealer.hands | ||
} | ||
|
||
fun isPlayerObtainable(name: String, wantToTake: Supplier<Boolean>): Boolean { | ||
val player = players.first { it.name == name } | ||
return player.isObtainable() && wantToTake.get() | ||
} | ||
|
||
fun obtainPlayerCard(name: String): List<Card> { | ||
val player = players.first { it.name == name } | ||
player.obtain(cardDeck.next()) | ||
return player.hands | ||
} | ||
|
||
fun compareResults(): Map<String, CompareResult> { | ||
return dealer.compareWith(*players.toTypedArray()) | ||
} | ||
|
||
fun participants(): List<Participant> { | ||
return players + listOf(dealer) | ||
} | ||
} |
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,36 @@ | ||
package blackjack.domain | ||
|
||
class Dealer(card1: Card, card2: Card): Participant("딜러", card1, card2) { | ||
|
||
override fun isObtainable(): Boolean { | ||
return sumOfCards() < 17 | ||
} | ||
|
||
override fun openCards(): List<Card> { | ||
return listOf(hands.first()) | ||
} | ||
|
||
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,26 @@ | ||
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, card1: Card, card2: Card, | ||
) { | ||
private val cards = Cards(card1, card2) | ||
|
||
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(card: Card) { | ||
require(isObtainable()) { "카드를 획득할 수 없습니다." } | ||
cards.add(card) | ||
} | ||
|
||
fun sumOfCards(): Int { | ||
return cards.sum() | ||
} | ||
|
||
fun isMoreThanBlackjack(): Boolean { | ||
return sumOfCards() > BLACKJACK_SCORE | ||
} | ||
|
||
abstract fun isObtainable(): Boolean | ||
abstract fun openCards(): List<Card> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,16 @@ | ||
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 | ||
name: String, | ||
card1: Card, | ||
card2: Card, | ||
) : Participant(name, card1, card2) { | ||
|
||
fun obtain() { | ||
require(isObtainable()) { "카드를 획득할 수 없습니다." } | ||
cards.add(cardDeck.next()) | ||
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. 플레이어는 더이상 카드를 받지 않지 않는 선언을 할수 있습니다. |
||
} | ||
|
||
fun sumOfCards(): Int { | ||
return cards.sum() | ||
} | ||
|
||
fun isObtainable(): Boolean { | ||
return cards.isLessThanBlackjack() | ||
override fun openCards(): List<Card> { | ||
return hands.subList(0, 2) | ||
} | ||
} |
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 클래스 추출했습니다.