-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
549 additions
and
532 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
package blackjack | ||
|
||
import blackjack.controller.BlackjackGame | ||
import blackjack.view.InputView | ||
import blackjack.view.ResultView | ||
|
||
fun main() { | ||
BlackjackGame(InputView, ResultView).start() | ||
BlackjackGame.start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,40 @@ | ||
package blackjack.controller | ||
|
||
import blackjack.domain.Dealer | ||
import blackjack.domain.Deck | ||
import blackjack.domain.GameResult | ||
import blackjack.domain.GameTable | ||
import blackjack.domain.Participant | ||
import blackjack.domain.Player | ||
import blackjack.domain.card.Deck | ||
import blackjack.domain.player.Dealer | ||
import blackjack.domain.player.Player | ||
import blackjack.view.InputView | ||
import blackjack.view.ResultView | ||
|
||
data class BlackjackGame( | ||
private val inputView: InputView, | ||
private val resultView: ResultView, | ||
) { | ||
object BlackjackGame { | ||
fun start() { | ||
val gameTable = GameTable(Deck.create()) | ||
val participants = playGame(gameTable) | ||
printCard(participants) | ||
printGameResult(participants) | ||
val gameTable = setUp() | ||
initDeal(gameTable) | ||
participantsTurn(gameTable) | ||
printAfterTurns(gameTable) | ||
} | ||
|
||
private fun playGame(gameTable: GameTable): List<Participant> { | ||
val participants = setUpInitCard(gameTable) | ||
val (players, dealer) = Participant.separate(participants) | ||
val gamedPlayers = playersTurn(players, gameTable) | ||
resultView.linebreak() | ||
val gamedDealer = dealerTurn(dealer, gameTable) | ||
return gamedPlayers + gamedDealer | ||
private fun setUp(): GameTable { | ||
val gameTable = GameTable(Deck(), Dealer(), getPlayers()) | ||
ResultView.linebreak() | ||
return gameTable | ||
} | ||
|
||
private fun setUpInitCard(gameTable: GameTable): List<Participant> { | ||
val participants = gameTable.dealInitCard(getParticipants()) | ||
resultView.linebreak() | ||
resultView.printInitCardReceive(participants) | ||
resultView.printParticipantsCard(participants = participants, printScore = false) | ||
resultView.linebreak() | ||
return participants | ||
} | ||
|
||
private fun getParticipants(): List<Participant> { | ||
return buildList { | ||
add(Dealer.create()) | ||
addAll(inputView.inputNames().map { Player.create(name = it) }) | ||
} | ||
} | ||
|
||
private fun playersTurn( | ||
participants: List<Participant>, | ||
gameTable: GameTable, | ||
): List<Participant> { | ||
return participants.map { playerTurn(it, gameTable) } | ||
} | ||
|
||
private tailrec fun playerTurn( | ||
player: Participant, | ||
gameTable: GameTable, | ||
): Participant { | ||
if (!player.canHit() || !inputView.inputHit(player)) { | ||
return player | ||
} | ||
val hitPlayer = gameTable.hit(player) | ||
resultView.printParticipantCard(participant = hitPlayer, printScore = false) | ||
return playerTurn(hitPlayer, gameTable) | ||
} | ||
private fun getPlayers(): List<Player> = InputView.inputNames().map { Player(it) } | ||
|
||
private tailrec fun dealerTurn( | ||
dealer: Participant, | ||
gameTable: GameTable, | ||
): Participant { | ||
if (!dealer.canHit()) { | ||
return dealer | ||
} | ||
resultView.printDealerHit() | ||
return dealerTurn(gameTable.hit(dealer), gameTable) | ||
private fun initDeal(gameTable: GameTable) { | ||
gameTable.dealInitCard() | ||
ResultView.printDealInitCard(gameTable) | ||
} | ||
|
||
private fun printCard(participants: List<Participant>) { | ||
resultView.linebreak() | ||
resultView.printParticipantsCard(participants = participants, printScore = true) | ||
private fun participantsTurn(gameTable: GameTable) { | ||
gameTable.playersTurn() | ||
ResultView.linebreak() | ||
gameTable.dealerTurn() | ||
} | ||
|
||
private fun printGameResult(participants: List<Participant>) { | ||
resultView.linebreak() | ||
resultView.printGameResult(GameResult.from(participants)) | ||
private fun printAfterTurns(gameTable: GameTable) { | ||
ResultView.printFinalHand(gameTable) | ||
ResultView.printGameResult(gameTable) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package blackjack.controller | ||
|
||
import blackjack.domain.card.Deck | ||
import blackjack.domain.game.GameResult | ||
import blackjack.domain.player.Dealer | ||
import blackjack.domain.player.Player | ||
import blackjack.view.InputView | ||
import blackjack.view.ResultView | ||
|
||
class GameTable( | ||
val deck: Deck, | ||
val dealer: Dealer, | ||
val players: List<Player>, | ||
) { | ||
fun dealInitCard() = | ||
repeat(INIT_CARD_DRAW_COUNT) { | ||
dealer.hit(deck.draw()) | ||
players.forEach { it.hit(deck.draw()) } | ||
} | ||
|
||
fun playersTurn() = players.forEach { playerTurn(it) } | ||
|
||
fun dealerTurn() { | ||
if (!dealer.canHit()) { | ||
return | ||
} | ||
ResultView.printDealerHit() | ||
dealer.hit(deck.draw()) | ||
dealerTurn() | ||
} | ||
|
||
fun getGameResult(): GameResult = GameResult.from(dealer, players) | ||
|
||
private fun playerTurn(player: Player) { | ||
if (!player.canHit() || !InputView.inputHit(player)) { | ||
return | ||
} | ||
player.hit(deck.draw()) | ||
ResultView.printPlayerCard(player) | ||
playerTurn(player) | ||
} | ||
|
||
companion object { | ||
const val INIT_CARD_DRAW_COUNT = 2 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
src/main/kotlin/blackjack/domain/Card.kt → ...main/kotlin/blackjack/domain/card/Card.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package blackjack.domain.card | ||
|
||
class Deck(private val cards: MutableList<Card> = Card.ALL.shuffled().toMutableList()) { | ||
fun draw(): Card { | ||
check(cards.isNotEmpty()) { "카드가 모두 소진되었습니다" } | ||
return cards.removeFirst() | ||
} | ||
} |
Oops, something went wrong.