Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step3: 블랙잭(딜러) #553

Open
wants to merge 20 commits into
base: shlee4290
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
12c86aa
refactor(step2): infix function 활용
peter-shlee Jul 8, 2023
5ce4251
refactor(step2): Players 클래스를 domain으로 옮기고 입출력 관련 코드와 분리
peter-shlee Jul 8, 2023
520ee4a
refactor(step2): Game이 players를 들고있도록 수정
peter-shlee Jul 8, 2023
19cace4
refactor(step2): Game -> Turn으로 이름 변경 및 게임 진행 방식 수정
peter-shlee Jul 8, 2023
f60f2ee
refactor(step2): Turn과 InitialTurn 분리
peter-shlee Jul 8, 2023
ca87c66
feat(step3): TODO 작성
peter-shlee Jul 8, 2023
a519b5d
feat(step3): 게임 시작 시 딜러에게 카드 2장 지급, 1장 오픈 & 게임 진행 시 딜러 카드 합계가 16 이하이면…
peter-shlee Jul 8, 2023
99009b4
feat(step3): 딜러 21 초과하면 플레이어 승리 & 플레이어 별 승패 출력
peter-shlee Jul 8, 2023
99535ec
refactor(step3): 불필요한 메서드 제거
peter-shlee Jul 8, 2023
d911e3b
refactor(step3): Player, Dealer가 state 기반으로 동작하도록 변경
peter-shlee Jul 8, 2023
50d4b40
refactor(step3): Turn이 state 기반으로 동작하도록 변경
peter-shlee Jul 8, 2023
8abb360
feat(step3): InitialTurn 카드 나눠주는 순서 변경
peter-shlee Jul 8, 2023
ca809df
feat(step3): 카드 나눠주는 순서 블랙잭 룰에 맞춰 수정
peter-shlee Jul 8, 2023
5238474
refactor(step3): player hit 여부 결정 시 콜백 이용하도록 변경
peter-shlee Jul 9, 2023
77cdaf6
refactor(step3): DealerState, PlayerState를 State로 합침
peter-shlee Jul 9, 2023
8d90951
refactor(step3): 승패 결정을 Dealer가 하도록 수정
peter-shlee Jul 9, 2023
73cebac
refactor(step3): Players 위임 메서드 추가
peter-shlee Jul 9, 2023
4e4022e
refactor(step3): Dealer.onHit을 생성자 주입 받을 수 있게 변경
peter-shlee Jul 9, 2023
bc6a7e4
refactor(step3): 점수 상수를 모아놓은 클래스 추가
peter-shlee Jul 9, 2023
52788be
refactor(step3): Score에서 점수 계산 수행
peter-shlee Jul 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(step3): 점수 상수를 모아놓은 클래스 추가
peter-shlee committed Jul 9, 2023
commit bc6a7e4f297b8dfb2e342454dbbd5287c978019b
6 changes: 6 additions & 0 deletions src/main/kotlin/domain/Score.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package domain

object Score {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금은 Score과 관련된 기능을 수행하는 유틸 클래스가 되어버린 것 같아요.
구현하시는 블랙잭 시스템에 Score라는 타입을 새롭게 정의하는 건 어떨까요?

value class Score(private val value: Int) {
    init {
        // score 제약 조건
    }
    
    val isBlackJack: Boolean = this == BLACKJACK_SCORE
    companion object {
        
    }
}

const val BLACKJACK = 21
const val DEALER_HIT_ON_MAX = 16
}
6 changes: 3 additions & 3 deletions src/main/kotlin/domain/card/Cards.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package domain.card

import domain.Score

class Cards(cards: List<Card> = emptyList()) {

private val cards: MutableList<Card> = cards.toMutableList()
@@ -24,7 +26,7 @@ class Cards(cards: List<Card> = emptyList()) {
}

return sumOfAllCases.filter {
it <= BLACKJACK_POINT
it <= Score.BLACKJACK
}.maxOrNull() ?: sumOfAllCases.min()
}

@@ -56,8 +58,6 @@ class Cards(cards: List<Card> = emptyList()) {
}

companion object {
const val BLACKJACK_POINT = 21

fun all(): Cards {
return Cards(Card.all())
}
11 changes: 4 additions & 7 deletions src/main/kotlin/domain/gamer/dealer/Dealer.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package domain.gamer.dealer

import domain.Score
import domain.State
import domain.card.CardDeck
import domain.card.Cards
@@ -47,14 +48,10 @@ class Dealer(
private fun newState(): State {
val result = cards.score()
return when {
result > Cards.BLACKJACK_POINT -> State.Bust
result == Cards.BLACKJACK_POINT -> State.BlackJack
result > DEALER_MAX_POINT -> State.Stay
result > Score.BLACKJACK -> State.Bust
result == Score.BLACKJACK -> State.BlackJack
result > Score.DEALER_HIT_ON_MAX -> State.Stay
else -> State.Hit
}
}

companion object {
const val DEALER_MAX_POINT = 16
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/domain/gamer/player/Player.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package domain.gamer.player

import domain.Score
import domain.State
import domain.card.CardDeck
import domain.card.Cards
@@ -32,8 +33,8 @@ open class Player(
private fun newState(): State {
val result = cards.score()
return when {
result == Cards.BLACKJACK_POINT -> State.BlackJack
result < Cards.BLACKJACK_POINT -> State.Hit
result == Score.BLACKJACK -> State.BlackJack
result < Score.BLACKJACK -> State.Hit
else -> State.Bust
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/presentation/ResultView.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package presentation

import domain.Result
import domain.Score
import domain.gamer.Gamer
import domain.gamer.dealer.Dealer
import domain.gamer.player.Player
@@ -22,7 +23,7 @@ object ResultView {
}

fun printDealerReceiveCardMessage() {
println("딜러는 ${Dealer.DEALER_MAX_POINT}이하라 한장의 카드를 더 받았습니다.")
println("딜러는 ${Score.DEALER_HIT_ON_MAX}이하라 한장의 카드를 더 받았습니다.")
println()
}

12 changes: 6 additions & 6 deletions src/test/kotlin/blackjack/StateForTest.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package blackjack

import domain.Score
import domain.State
import domain.card.Cards
import domain.gamer.dealer.Dealer

fun DealerState(cards: Cards): State {
val result = cards.score()
return when {
result > Cards.BLACKJACK_POINT -> State.Bust
result == Cards.BLACKJACK_POINT -> State.BlackJack
result > Dealer.DEALER_MAX_POINT -> State.Stay
result > Score.BLACKJACK -> State.Bust
result == Score.BLACKJACK -> State.BlackJack
result > Score.DEALER_HIT_ON_MAX -> State.Stay
else -> State.Hit
}
}

fun PlayerState(cards: Cards): State {
val result = cards.score()
return when {
result == Cards.BLACKJACK_POINT -> State.BlackJack
result < Cards.BLACKJACK_POINT -> State.Hit
result == Score.BLACKJACK -> State.BlackJack
result < Score.BLACKJACK -> State.Hit
else -> State.Bust
}
}