Skip to content

Commit

Permalink
refactor: 피드백반영
Browse files Browse the repository at this point in the history
  • Loading branch information
DuhanMo committed Dec 11, 2024
1 parent c041297 commit f044e83
Show file tree
Hide file tree
Showing 36 changed files with 549 additions and 532 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@
### 기능 구현사항
- [x] 딜러는 처음받는 2장의 합계가 16이하면 반드시 1장의 카드를 추가로 받는다
- [x] 딜러가 21을 초과하면 남은 플레이어들은 패에 상관없이 승리한다
- [x] 게임 완료 후 각 플레이어별로 승패를 출력한다
- [x] 게임 완료 후 각 플레이어별로 승패를 출력한다
- [x] data class , 일반 클래스 설정 일관성 유지
- [x] 도메인 패키지 분리
- [x] 객체 상태를 객체가 관리
- [x] 게임 관련 로직 분리
4 changes: 1 addition & 3 deletions src/main/kotlin/blackjack/Main.kt
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()
}
93 changes: 23 additions & 70 deletions src/main/kotlin/blackjack/controller/BlackjackGame.kt
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)
}
}
46 changes: 46 additions & 0 deletions src/main/kotlin/blackjack/controller/GameTable.kt
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
}
}
46 changes: 0 additions & 46 deletions src/main/kotlin/blackjack/domain/Cards.kt

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/kotlin/blackjack/domain/Deck.kt

This file was deleted.

27 changes: 0 additions & 27 deletions src/main/kotlin/blackjack/domain/GameResult.kt

This file was deleted.

21 changes: 0 additions & 21 deletions src/main/kotlin/blackjack/domain/GameTable.kt

This file was deleted.

63 changes: 0 additions & 63 deletions src/main/kotlin/blackjack/domain/Participant.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package blackjack.domain
package blackjack.domain.card

import blackjack.domain.Rank.ACE
import blackjack.domain.card.Rank.ACE

data class Card(
val rank: Rank,
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/blackjack/domain/card/Deck.kt
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()
}
}
Loading

0 comments on commit f044e83

Please sign in to comment.