|
1 | 1 | package blackjack.controller
|
2 | 2 |
|
3 |
| -import blackjack.domain.Dealer |
4 |
| -import blackjack.domain.Deck |
5 |
| -import blackjack.domain.GameResult |
6 |
| -import blackjack.domain.GameTable |
7 |
| -import blackjack.domain.Participant |
8 |
| -import blackjack.domain.Player |
| 3 | +import blackjack.domain.card.Deck |
| 4 | +import blackjack.domain.player.Dealer |
| 5 | +import blackjack.domain.player.Player |
9 | 6 | import blackjack.view.InputView
|
10 | 7 | import blackjack.view.ResultView
|
11 | 8 |
|
12 |
| -data class BlackjackGame( |
13 |
| - private val inputView: InputView, |
14 |
| - private val resultView: ResultView, |
15 |
| -) { |
| 9 | +object BlackjackGame { |
16 | 10 | fun start() {
|
17 |
| - val gameTable = GameTable(Deck.create()) |
18 |
| - val participants = playGame(gameTable) |
19 |
| - printCard(participants) |
20 |
| - printGameResult(participants) |
| 11 | + val gameTable = setUp() |
| 12 | + initDeal(gameTable) |
| 13 | + turnStart(gameTable) |
| 14 | + ResultView.printAfterTurn(gameTable) |
21 | 15 | }
|
22 | 16 |
|
23 |
| - private fun playGame(gameTable: GameTable): List<Participant> { |
24 |
| - val participants = setUpInitCard(gameTable) |
25 |
| - val (players, dealer) = Participant.separate(participants) |
26 |
| - val gamedPlayers = playersTurn(players, gameTable) |
27 |
| - resultView.linebreak() |
28 |
| - val gamedDealer = dealerTurn(dealer, gameTable) |
29 |
| - return gamedPlayers + gamedDealer |
| 17 | + private fun setUp(): GameTable { |
| 18 | + val gameTable = GameTable(Deck(), Dealer(), getPlayers()) |
| 19 | + ResultView.linebreak() |
| 20 | + return gameTable |
30 | 21 | }
|
31 | 22 |
|
32 |
| - private fun setUpInitCard(gameTable: GameTable): List<Participant> { |
33 |
| - val participants = gameTable.dealInitCard(getParticipants()) |
34 |
| - resultView.linebreak() |
35 |
| - resultView.printInitCardReceive(participants) |
36 |
| - resultView.printParticipantsCard(participants = participants, printScore = false) |
37 |
| - resultView.linebreak() |
38 |
| - return participants |
39 |
| - } |
40 |
| - |
41 |
| - private fun getParticipants(): List<Participant> { |
42 |
| - return buildList { |
43 |
| - add(Dealer.create()) |
44 |
| - addAll(inputView.inputNames().map { Player.create(name = it) }) |
45 |
| - } |
46 |
| - } |
47 |
| - |
48 |
| - private fun playersTurn( |
49 |
| - participants: List<Participant>, |
50 |
| - gameTable: GameTable, |
51 |
| - ): List<Participant> { |
52 |
| - return participants.map { playerTurn(it, gameTable) } |
53 |
| - } |
54 |
| - |
55 |
| - private tailrec fun playerTurn( |
56 |
| - player: Participant, |
57 |
| - gameTable: GameTable, |
58 |
| - ): Participant { |
59 |
| - if (!player.canHit() || !inputView.inputHit(player)) { |
60 |
| - return player |
61 |
| - } |
62 |
| - val hitPlayer = gameTable.hit(player) |
63 |
| - resultView.printParticipantCard(participant = hitPlayer, printScore = false) |
64 |
| - return playerTurn(hitPlayer, gameTable) |
65 |
| - } |
66 |
| - |
67 |
| - private tailrec fun dealerTurn( |
68 |
| - dealer: Participant, |
69 |
| - gameTable: GameTable, |
70 |
| - ): Participant { |
71 |
| - if (!dealer.canHit()) { |
72 |
| - return dealer |
73 |
| - } |
74 |
| - resultView.printDealerHit() |
75 |
| - return dealerTurn(gameTable.hit(dealer), gameTable) |
76 |
| - } |
| 23 | + private fun getPlayers(): List<Player> = InputView.inputNames().map { Player(it) } |
77 | 24 |
|
78 |
| - private fun printCard(participants: List<Participant>) { |
79 |
| - resultView.linebreak() |
80 |
| - resultView.printParticipantsCard(participants = participants, printScore = true) |
| 25 | + private fun initDeal(gameTable: GameTable) { |
| 26 | + gameTable.dealInitCard() |
| 27 | + ResultView.printDealInitCard(gameTable) |
81 | 28 | }
|
82 | 29 |
|
83 |
| - private fun printGameResult(participants: List<Participant>) { |
84 |
| - resultView.linebreak() |
85 |
| - resultView.printGameResult(GameResult.from(participants)) |
| 30 | + private fun turnStart(gameTable: GameTable) { |
| 31 | + gameTable.playersTurn() |
| 32 | + ResultView.linebreak() |
| 33 | + gameTable.dealerTurn() |
86 | 34 | }
|
87 | 35 | }
|
0 commit comments