-
Notifications
You must be signed in to change notification settings - Fork 312
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
peter-shlee
wants to merge
20
commits into
next-step:shlee4290
Choose a base branch
from
peter-shlee:step3
base: shlee4290
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Step3: 블랙잭(딜러) #553
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
12c86aa
refactor(step2): infix function 활용
peter-shlee 5ce4251
refactor(step2): Players 클래스를 domain으로 옮기고 입출력 관련 코드와 분리
peter-shlee 520ee4a
refactor(step2): Game이 players를 들고있도록 수정
peter-shlee 19cace4
refactor(step2): Game -> Turn으로 이름 변경 및 게임 진행 방식 수정
peter-shlee f60f2ee
refactor(step2): Turn과 InitialTurn 분리
peter-shlee ca87c66
feat(step3): TODO 작성
peter-shlee a519b5d
feat(step3): 게임 시작 시 딜러에게 카드 2장 지급, 1장 오픈 & 게임 진행 시 딜러 카드 합계가 16 이하이면…
peter-shlee 99009b4
feat(step3): 딜러 21 초과하면 플레이어 승리 & 플레이어 별 승패 출력
peter-shlee 99535ec
refactor(step3): 불필요한 메서드 제거
peter-shlee d911e3b
refactor(step3): Player, Dealer가 state 기반으로 동작하도록 변경
peter-shlee 50d4b40
refactor(step3): Turn이 state 기반으로 동작하도록 변경
peter-shlee 8abb360
feat(step3): InitialTurn 카드 나눠주는 순서 변경
peter-shlee ca809df
feat(step3): 카드 나눠주는 순서 블랙잭 룰에 맞춰 수정
peter-shlee 5238474
refactor(step3): player hit 여부 결정 시 콜백 이용하도록 변경
peter-shlee 77cdaf6
refactor(step3): DealerState, PlayerState를 State로 합침
peter-shlee 8d90951
refactor(step3): 승패 결정을 Dealer가 하도록 수정
peter-shlee 73cebac
refactor(step3): Players 위임 메서드 추가
peter-shlee 4e4022e
refactor(step3): Dealer.onHit을 생성자 주입 받을 수 있게 변경
peter-shlee bc6a7e4
refactor(step3): 점수 상수를 모아놓은 클래스 추가
peter-shlee 52788be
refactor(step3): Score에서 점수 계산 수행
peter-shlee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
package controller | ||
|
||
import domain.Game | ||
import domain.Player | ||
import domain.dealer.Dealer | ||
import domain.player.Player | ||
import domain.player.Players | ||
import domain.turn.Game | ||
import domain.turn.InitialTurn | ||
import presentation.InputView | ||
import presentation.ResultView | ||
|
||
fun main() { | ||
val game = Game( | ||
InputView.getPlayerNames() | ||
.map { Player(it) } | ||
) | ||
game.start() | ||
ResultView.printInitialState(game.players) | ||
val playerNames = InputView.getPlayerNames() | ||
val players = Players(playerNames.map { Player(it) }) | ||
val dealer = Dealer() | ||
|
||
val game = Game(InitialTurn, dealer, players) | ||
game.proceed() | ||
ResultView.printInitialState(dealer, players.list) | ||
dealer.addOnHitCallback { | ||
ResultView.printDealerReceiveCardMessage() | ||
} | ||
|
||
while (true) { | ||
val playerReceiveMoreCard = Players(game.playersCanReceiveMoreCard()) | ||
if (playerReceiveMoreCard.noMorePlayer()) break | ||
game.playersCanTakeNextTurn().list.filterNot { | ||
InputView.askReceiveCard(it.name) | ||
}.forEach { | ||
peter-shlee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it.stay() | ||
} | ||
|
||
game.proceed() | ||
|
||
playerReceiveMoreCard.hit(game) | ||
if (game.isFinish) { | ||
game.result?.let { | ||
ResultView.printResult(it) | ||
} | ||
break | ||
} | ||
|
||
ResultView.printResult(game.players) | ||
ResultView.printResult(dealer, players.list) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package domain | ||
|
||
import domain.player.Players | ||
|
||
data class Result( | ||
val winners: Players, | ||
val losers: Players, | ||
) { | ||
val numOfWinner = winners.list.size | ||
peter-shlee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val numOfLoser = losers.list.size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package domain.dealer | ||
|
||
import domain.card.CardDeck | ||
import domain.card.Cards | ||
|
||
class Dealer(initialState: DealerState = DealerState.Hit(Cards())) { | ||
peter-shlee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private var state: DealerState = initialState | ||
private var onHit: (() -> Unit)? = null | ||
peter-shlee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
val cards: Cards | ||
get() = state.cards | ||
|
||
val canReceiveMoreCard: Boolean | ||
get() = state.canReceiveMoreCard | ||
|
||
val isBlackJack: Boolean | ||
get() = state is DealerState.BlackJack | ||
|
||
val isBust: Boolean | ||
get() = state is DealerState.Bust | ||
|
||
val result: Int | ||
get() = state.cards.result() | ||
|
||
fun hit(cardDeck: CardDeck) { | ||
val capturedState = state | ||
if (capturedState is DealerState.Hit) { | ||
state = capturedState.hit(cardDeck.pop()) | ||
onHit?.invoke() | ||
} | ||
} | ||
|
||
fun addOnHitCallback(callback: (() -> Unit)) { | ||
onHit = callback | ||
} | ||
|
||
companion object { | ||
const val DEALER_MAX_POINT = 16 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package domain.dealer | ||
|
||
import domain.card.Card | ||
import domain.card.Cards | ||
|
||
sealed interface DealerState { | ||
peter-shlee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val cards: Cards | ||
|
||
val canReceiveMoreCard: Boolean | ||
|
||
class Hit(override val cards: Cards) : DealerState { | ||
fun hit(card: Card): DealerState { | ||
cards.add(card) | ||
return DealerState(cards) | ||
} | ||
|
||
override val canReceiveMoreCard: Boolean = cards.result() <= Dealer.DEALER_MAX_POINT | ||
} | ||
|
||
class Bust(override val cards: Cards) : DealerState { | ||
override val canReceiveMoreCard: Boolean = false | ||
} | ||
|
||
class Stay(override val cards: Cards) : DealerState { | ||
override val canReceiveMoreCard: Boolean = false | ||
} | ||
|
||
class BlackJack(override val cards: Cards) : DealerState { | ||
override val canReceiveMoreCard: Boolean = false | ||
} | ||
} | ||
|
||
fun DealerState(cards: Cards): DealerState { | ||
val result = cards.result() | ||
return when { | ||
result > Cards.BLACKJACK_POINT -> DealerState.Bust(cards) | ||
result == Cards.BLACKJACK_POINT -> DealerState.BlackJack(cards) | ||
result > Dealer.DEALER_MAX_POINT -> DealerState.Stay(cards) | ||
else -> DealerState.Hit(cards) | ||
} | ||
} | ||
peter-shlee marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package domain.player | ||
|
||
import domain.card.Card | ||
import domain.card.Cards | ||
|
||
open class Player(val name: String, initialState: PlayerState = PlayerState.Hit(Cards())) { | ||
|
||
private var state: PlayerState = initialState | ||
|
||
val cards: Cards | ||
get() = state.cards | ||
|
||
val result: Int | ||
get() = state.cards.result() | ||
|
||
val isBust: Boolean | ||
get() = state is PlayerState.Bust | ||
|
||
val isHit: Boolean | ||
get() = state is PlayerState.Hit | ||
|
||
open val canReceiveMoreCard: Boolean | ||
get() = state.canReceiveMoreCard | ||
|
||
fun hit(card: Card) { | ||
val capturedState = state | ||
if (capturedState is PlayerState.Hit) { | ||
state = capturedState.hit(card) | ||
} | ||
} | ||
|
||
fun stay() { | ||
state = PlayerState.Stay(state.cards) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package domain.player | ||
|
||
import domain.card.Card | ||
import domain.card.Cards | ||
|
||
sealed interface PlayerState { | ||
val cards: Cards | ||
|
||
val canReceiveMoreCard: Boolean | ||
|
||
class Hit(override val cards: Cards) : PlayerState { | ||
fun hit(card: Card): PlayerState { | ||
cards.add(card) | ||
return PlayerState(cards) | ||
} | ||
|
||
override val canReceiveMoreCard: Boolean = true | ||
} | ||
|
||
class Bust(override val cards: Cards) : PlayerState { | ||
override val canReceiveMoreCard: Boolean = false | ||
} | ||
|
||
class Stay(override val cards: Cards) : PlayerState { | ||
override val canReceiveMoreCard: Boolean = false | ||
} | ||
|
||
class BlackJack(override val cards: Cards) : PlayerState { | ||
override val canReceiveMoreCard: Boolean = false | ||
} | ||
} | ||
|
||
fun PlayerState(cards: Cards): PlayerState { | ||
val result = cards.result() | ||
return when { | ||
result == Cards.BLACKJACK_POINT -> PlayerState.BlackJack(cards) | ||
result < Cards.BLACKJACK_POINT -> PlayerState.Hit(cards) | ||
else -> PlayerState.Bust(cards) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package domain.player | ||
|
||
import domain.card.CardDeck | ||
|
||
data class Players(val list: List<Player>) { | ||
|
||
fun hit(cardDeck: CardDeck) { | ||
list.forEach { | ||
it.hit(cardDeck.pop()) | ||
} | ||
} | ||
|
||
fun playersCanReceiveMoreCard(): Players { | ||
return Players( | ||
list.filter { | ||
it.canReceiveMoreCard | ||
} | ||
) | ||
} | ||
|
||
fun noMoreHit(): Boolean { | ||
return list.none { it.isHit } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
모든 플레이어의 턴이 종료된 후에 딜러가 카드를 뽑아야 할 것 같습니다.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
블랙잭 룰을 잘못 이해하고 있었네요.. 감사합니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
딜러의 카드는 마지막 단계에서만 오픈되어야 할 것 같아요.
