Skip to content

Commit bca0a30

Browse files
committed
fix: 출력구문 수정
1 parent c041297 commit bca0a30

36 files changed

+548
-531
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@
3737
### 기능 구현사항
3838
- [x] 딜러는 처음받는 2장의 합계가 16이하면 반드시 1장의 카드를 추가로 받는다
3939
- [x] 딜러가 21을 초과하면 남은 플레이어들은 패에 상관없이 승리한다
40-
- [x] 게임 완료 후 각 플레이어별로 승패를 출력한다
40+
- [x] 게임 완료 후 각 플레이어별로 승패를 출력한다
41+
- [x] data class , 일반 클래스 설정 일관성 유지
42+
- [x] 도메인 패키지 분리
43+
- [x] 객체 상태를 객체가 관리
44+
- [x] 게임 관련 로직 분리

src/main/kotlin/blackjack/Main.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package blackjack
22

33
import blackjack.controller.BlackjackGame
4-
import blackjack.view.InputView
5-
import blackjack.view.ResultView
64

75
fun main() {
8-
BlackjackGame(InputView, ResultView).start()
6+
BlackjackGame.start()
97
}
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,35 @@
11
package blackjack.controller
22

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
96
import blackjack.view.InputView
107
import blackjack.view.ResultView
118

12-
data class BlackjackGame(
13-
private val inputView: InputView,
14-
private val resultView: ResultView,
15-
) {
9+
object BlackjackGame {
1610
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)
2115
}
2216

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
3021
}
3122

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) }
7724

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)
8128
}
8229

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()
8634
}
8735
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package blackjack.controller
2+
3+
import blackjack.domain.card.Deck
4+
import blackjack.domain.game.GameResult
5+
import blackjack.domain.player.Dealer
6+
import blackjack.domain.player.Player
7+
import blackjack.view.InputView
8+
import blackjack.view.ResultView
9+
10+
class GameTable(
11+
val deck: Deck,
12+
val dealer: Dealer,
13+
val players: List<Player>,
14+
) {
15+
fun dealInitCard() =
16+
repeat(INIT_CARD_DRAW_COUNT) {
17+
dealer.hit(deck.draw())
18+
players.forEach { it.hit(deck.draw()) }
19+
}
20+
21+
fun playersTurn() = players.forEach { playerTurn(it) }
22+
23+
fun dealerTurn() {
24+
if (!dealer.canHit()) {
25+
return
26+
}
27+
ResultView.printDealerHit()
28+
dealer.hit(deck.draw())
29+
dealerTurn()
30+
}
31+
32+
fun getGameResult(): GameResult = GameResult.from(dealer, players)
33+
34+
private fun playerTurn(player: Player) {
35+
if (!player.canHit() || !InputView.inputHit(player)) {
36+
return
37+
}
38+
player.hit(deck.draw())
39+
ResultView.printPlayerCard(player)
40+
playerTurn(player)
41+
}
42+
43+
companion object {
44+
const val INIT_CARD_DRAW_COUNT = 2
45+
}
46+
}

src/main/kotlin/blackjack/domain/Cards.kt

-46
This file was deleted.

src/main/kotlin/blackjack/domain/Deck.kt

-14
This file was deleted.

src/main/kotlin/blackjack/domain/GameResult.kt

-27
This file was deleted.

src/main/kotlin/blackjack/domain/GameTable.kt

-21
This file was deleted.

src/main/kotlin/blackjack/domain/Participant.kt

-63
This file was deleted.

src/main/kotlin/blackjack/domain/Card.kt src/main/kotlin/blackjack/domain/card/Card.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package blackjack.domain
1+
package blackjack.domain.card
22

3-
import blackjack.domain.Rank.ACE
3+
import blackjack.domain.card.Rank.ACE
44

55
data class Card(
66
val rank: Rank,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package blackjack.domain.card
2+
3+
class Deck(private val cards: MutableList<Card> = Card.ALL.shuffled().toMutableList()) {
4+
fun draw(): Card {
5+
check(cards.isNotEmpty()) { "카드가 모두 소진되었습니다" }
6+
return cards.removeFirst()
7+
}
8+
}

0 commit comments

Comments
 (0)