Skip to content

Commit c041297

Browse files
committed
feat: 게임 완료 후 각 플레이어별로 승패를 출력한다
1 parent 823396e commit c041297

File tree

9 files changed

+87
-20
lines changed

9 files changed

+87
-20
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
### 기능 구현사항
3838
- [x] 딜러는 처음받는 2장의 합계가 16이하면 반드시 1장의 카드를 추가로 받는다
3939
- [x] 딜러가 21을 초과하면 남은 플레이어들은 패에 상관없이 승리한다
40-
- [ ] 게임 완료 후 각 플레이어별로 승패를 출력한다
40+
- [x] 게임 완료 후 각 플레이어별로 승패를 출력한다

src/main/kotlin/blackjack/controller/BlackjackGame.kt

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ package blackjack.controller
22

33
import blackjack.domain.Dealer
44
import blackjack.domain.Deck
5+
import blackjack.domain.GameResult
56
import blackjack.domain.GameTable
67
import blackjack.domain.Participant
78
import blackjack.domain.Player
89
import blackjack.view.InputView
910
import blackjack.view.ResultView
1011

11-
class BlackjackGame(
12+
data class BlackjackGame(
1213
private val inputView: InputView,
1314
private val resultView: ResultView,
1415
) {
1516
fun start() {
1617
val gameTable = GameTable(Deck.create())
1718
val participants = playGame(gameTable)
19+
printCard(participants)
1820
printGameResult(participants)
1921
}
2022

@@ -36,6 +38,13 @@ class BlackjackGame(
3638
return participants
3739
}
3840

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+
3948
private fun playersTurn(
4049
participants: List<Participant>,
4150
gameTable: GameTable,
@@ -51,7 +60,7 @@ class BlackjackGame(
5160
return player
5261
}
5362
val hitPlayer = gameTable.hit(player)
54-
resultView.printParticipantCard(participant = player, printScore = false)
63+
resultView.printParticipantCard(participant = hitPlayer, printScore = false)
5564
return playerTurn(hitPlayer, gameTable)
5665
}
5766

@@ -66,15 +75,13 @@ class BlackjackGame(
6675
return dealerTurn(gameTable.hit(dealer), gameTable)
6776
}
6877

69-
private fun printGameResult(participants: List<Participant>) {
78+
private fun printCard(participants: List<Participant>) {
7079
resultView.linebreak()
7180
resultView.printParticipantsCard(participants = participants, printScore = true)
7281
}
7382

74-
private fun getParticipants(): List<Participant> {
75-
return buildList {
76-
add(Dealer.create())
77-
addAll(inputView.inputNames().map { Player.create(name = it) })
78-
}
83+
private fun printGameResult(participants: List<Participant>) {
84+
resultView.linebreak()
85+
resultView.printGameResult(GameResult.from(participants))
7986
}
8087
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package blackjack.domain
2+
3+
import blackjack.domain.MatchResult.DRAW
4+
import blackjack.domain.MatchResult.LOSS
5+
import blackjack.domain.MatchResult.WIN
6+
import blackjack.domain.dto.DealerGameResult
7+
import blackjack.domain.dto.PlayerGameResult
8+
9+
data class GameResult(
10+
val dealerGameResult: DealerGameResult,
11+
val playerGameResults: List<PlayerGameResult>,
12+
) {
13+
companion object {
14+
fun from(participants: List<Participant>): GameResult {
15+
val (players, dealer) = Participant.separate(participants)
16+
val playerGameResults = players.map { player -> PlayerGameResult(player, player.compareScore(dealer)) }
17+
return GameResult(
18+
DealerGameResult(
19+
winCount = playerGameResults.count { it.result == LOSS },
20+
lossCount = playerGameResults.count { it.result == WIN },
21+
drawCount = playerGameResults.count { it.result == DRAW },
22+
),
23+
playerGameResults,
24+
)
25+
}
26+
}
27+
}

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

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

3-
class GameTable(
3+
data class GameTable(
44
private val deck: Deck,
55
) {
66
fun dealInitCard(participants: List<Participant>): List<Participant> {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package blackjack.domain
22

3-
enum class MatchResult {
4-
WIN,
5-
LOSS,
6-
DRAW,
3+
enum class MatchResult(val description: String) {
4+
WIN(""),
5+
LOSS(""),
6+
DRAW(""),
77
}

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package blackjack.domain
22

3+
import blackjack.domain.MatchResult.LOSS
34
import blackjack.domain.MatchResult.WIN
45

56
sealed class Participant(val name: String, val cards: Cards) {
7+
val isBust: Boolean
8+
get() = cards.isBust
9+
610
abstract fun canHit(): Boolean
711

812
abstract fun hit(card: Card): Participant
@@ -24,10 +28,11 @@ class Player(name: String, cards: Cards) : Participant(name, cards) {
2428
}
2529

2630
fun compareScore(dealer: Dealer): MatchResult {
27-
if (dealer.isBust) {
28-
return WIN
31+
return when {
32+
dealer.isBust -> WIN
33+
this.isBust -> LOSS
34+
else -> cards.compareScore(dealer.cards)
2935
}
30-
return cards.compareScore(dealer.cards)
3136
}
3237

3338
companion object {
@@ -40,9 +45,6 @@ class Player(name: String, cards: Cards) : Participant(name, cards) {
4045
}
4146

4247
class Dealer(cards: Cards) : Participant("딜러", cards) {
43-
val isBust: Boolean
44-
get() = cards.isBust
45-
4648
override fun canHit(): Boolean {
4749
return cards.scoreLowerThan(DEALER_SCORE_LIMIT)
4850
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package blackjack.domain.dto
2+
3+
data class DealerGameResult(
4+
val winCount: Int,
5+
val lossCount: Int,
6+
val drawCount: Int,
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package blackjack.domain.dto
2+
3+
import blackjack.domain.MatchResult
4+
import blackjack.domain.Player
5+
6+
data class PlayerGameResult(
7+
val player: Player,
8+
val result: MatchResult,
9+
)

src/main/kotlin/blackjack/view/ResultView.kt

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package blackjack.view
22

3+
import blackjack.domain.GameResult
34
import blackjack.domain.GameTable.Companion.INIT_CARD_DRAW_COUNT
45
import blackjack.domain.Participant
56

@@ -31,6 +32,20 @@ object ResultView {
3132
println("딜러는 16이하라 한장의 카드를 더 받았습니다.")
3233
}
3334

35+
fun printGameResult(gameResult: GameResult) {
36+
val winMessage =
37+
if (gameResult.dealerGameResult.winCount > 0) "${gameResult.dealerGameResult.winCount}" else ""
38+
val lossMessage =
39+
if (gameResult.dealerGameResult.lossCount > 0) "${gameResult.dealerGameResult.lossCount}" else ""
40+
val drawMessage =
41+
if (gameResult.dealerGameResult.drawCount > 0) "${gameResult.dealerGameResult.drawCount}" else ""
42+
println("## 최종 승패")
43+
println("딜러: $winMessage $lossMessage $drawMessage")
44+
gameResult.playerGameResults.forEach {
45+
println("${it.player.name} ${it.result.description}")
46+
}
47+
}
48+
3449
fun linebreak() {
3550
println()
3651
}

0 commit comments

Comments
 (0)