-
Notifications
You must be signed in to change notification settings - Fork 0
[2단계 - 블랙잭 베팅] 재즈(함석명) 미션 제출합니다. #12
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
base: main
Are you sure you want to change the base?
Changes from all commits
d37008f
75656d1
3dce8a7
4c41d0f
cdf6f80
7b7d33f
e99482b
872cb10
cfcf2af
23a17b0
157d53a
feaa005
6753755
920b6cf
af934a5
661c788
0343057
19e55ee
c565111
6134b06
4384505
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package blackjack; | ||
|
|
||
| import blackjack.controller.BlackjackController; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| BlackjackController blackjackController = new BlackjackController(); | ||
| blackjackController.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package blackjack.controller; | ||
|
|
||
| import blackjack.domain.game.BlackjackGame; | ||
| import blackjack.domain.game.Money; | ||
| import blackjack.domain.gamer.Dealer; | ||
| import blackjack.domain.gamer.Player; | ||
| import blackjack.domain.gamer.Players; | ||
| import blackjack.dto.DealerInitialHandDto; | ||
| import blackjack.dto.HandDto; | ||
| import blackjack.dto.PlayerGameResultsDto; | ||
| import blackjack.dto.PlayerHandDto; | ||
| import blackjack.dto.PlayersHandDto; | ||
| import blackjack.view.InputView; | ||
| import blackjack.view.OutputView; | ||
| import blackjack.view.object.Command; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class BlackjackController { | ||
|
|
||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
| private final BlackjackGame blackjackGame; | ||
|
|
||
| public BlackjackController() { | ||
| this.inputView = new InputView(); | ||
| this.outputView = new OutputView(); | ||
| this.blackjackGame = new BlackjackGame(); | ||
| } | ||
|
|
||
| public void run() { | ||
| Players players = getPlayers(); | ||
| Dealer dealer = new Dealer(); | ||
| blackjackGame.distributeInitialHand(players, dealer); | ||
| blackjackGame.betPlayerMoney(receivePlayersBetMoney(players)); | ||
| printInitialHands(players, dealer); | ||
|
|
||
| distributeCardToPlayers(players); | ||
| blackjackGame.distributeCardToDealer(dealer); | ||
|
|
||
| printDealerCanReceiveCardMessage(dealer); | ||
| printAllGamerScores(dealer, players); | ||
| printResult(players, dealer); | ||
| } | ||
|
|
||
| private Players getPlayers() { | ||
| List<String> playerNames = inputView.receivePlayerNames(); | ||
| return new Players(playerNames); | ||
| } | ||
|
|
||
| private Map<Player, Money> receivePlayersBetMoney(Players players) { | ||
| Map<Player, Money> playerBetMoney = new HashMap<>(); | ||
| for (Player player : players.getPlayers()) { | ||
| int betMoney = inputView.receivePlayerMoney(player.getName().value()); | ||
| playerBetMoney.put(player, new Money(betMoney)); | ||
| } | ||
| return playerBetMoney; | ||
| } | ||
|
|
||
| private void printInitialHands(Players players, Dealer dealer) { | ||
| outputView.printInitialHands(DealerInitialHandDto.fromDealer(dealer), PlayersHandDto.fromPlayers(players)); | ||
| } | ||
|
|
||
| private void distributeCardToPlayers(Players players) { | ||
| for (Player player : players.getPlayers()) { | ||
| distributeCardToPlayer(player); | ||
| } | ||
| } | ||
|
|
||
| private void distributeCardToPlayer(Player player) { | ||
| while (player.canReceiveCard() && Command.isHit(getCommand(player))) { | ||
| blackjackGame.addCardToPlayer(player); | ||
| outputView.printPlayerHand(PlayerHandDto.fromPlayer(player)); | ||
| } | ||
| } | ||
|
|
||
| private Command getCommand(Player player) { | ||
| return inputView.receiveCommand(player.getName().value()); | ||
| } | ||
|
|
||
| private void printDealerCanReceiveCardMessage(Dealer dealer) { | ||
| if (dealer.canReceiveCard()) { | ||
| outputView.printDealerMessage(); | ||
| } | ||
| } | ||
|
|
||
| private void printAllGamerScores(Dealer dealer, Players players) { | ||
| outputView.printDealerHandScore(HandDto.fromHand(dealer.getHand())); | ||
| outputView.printPlayersHandScore(PlayersHandDto.fromPlayers(players)); | ||
| } | ||
|
|
||
| private void printResult(Players players, Dealer dealer) { | ||
| Money dealerIncome = blackjackGame.calculateDealerIncome(players, dealer); | ||
| PlayerGameResultsDto playerGameResultsDto = PlayerGameResultsDto.fromPlayerBetResults(blackjackGame.getStore()); | ||
| outputView.printResult(dealerIncome.value(), playerGameResultsDto); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Card { | ||
|
|
||
| private final CardShape cardShape; | ||
| private final CardNumber cardNumber; | ||
|
|
||
| public Card(CardShape cardShape, CardNumber cardNumber) { | ||
| this.cardShape = cardShape; | ||
| this.cardNumber = cardNumber; | ||
| } | ||
|
|
||
| public boolean isAce() { | ||
| return cardNumber == CardNumber.ACE; | ||
| } | ||
|
|
||
| public int getNumberValue() { | ||
| return cardNumber.getValue(); | ||
| } | ||
|
|
||
| public CardShape getCardShape() { | ||
| return cardShape; | ||
| } | ||
|
|
||
| public CardNumber getCardNumber() { | ||
| return cardNumber; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| Card card = (Card) o; | ||
| return cardShape == card.cardShape && cardNumber == card.cardNumber; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(cardShape, cardNumber); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public enum CardNumber { | ||
|
|
||
| ACE(11), | ||
| TWO(2), | ||
| THREE( 3), | ||
| FOUR(4), | ||
| FIVE(5), | ||
| SIX(6), | ||
| SEVEN(7), | ||
| EIGHT(8), | ||
| NINE(9), | ||
| TEN(10), | ||
| JACK(10), | ||
| QUEEN(10), | ||
| KING(10); | ||
|
|
||
| private final int value; | ||
|
|
||
| CardNumber(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public enum CardShape { | ||
|
|
||
| HEART, | ||
| CLOVER, | ||
| SPADE, | ||
| DIAMOND | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
|
|
||
| public class Deck { | ||
|
|
||
| private static final List<Card> CACHE = Arrays.stream(CardShape.values()) | ||
| .flatMap(cardShape -> Arrays.stream(CardNumber.values()) | ||
| .map(number -> new Card(cardShape, number))).toList(); | ||
|
|
||
| private final LinkedList<Card> cards; | ||
|
|
||
| public Deck() { | ||
| this.cards = new LinkedList<>(CACHE); | ||
| } | ||
|
|
||
| Deck(LinkedList<Card> cards) { | ||
| this.cards = cards; | ||
| } | ||
|
|
||
| public void shuffle() { | ||
| Collections.shuffle(cards); | ||
| } | ||
|
Comment on lines
+17
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 미리 섞여있는 덱을 주지않고, 순서대로 정렬된 덱을 만든 후에, 해당 덱을 받는 쪽에서 섞어 주는 이유가 있을까? public Deck() {
List<Card> cards = new LinkedList<>(CACHE);
Collections.shuffle(cards);
this.cards = cards;
}
Deck(LinkedList<Card> cards) {
this.cards = cards;
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
깊게 생각 안해봤었는데, |
||
|
|
||
| public Card draw() { | ||
| return cards.poll(); | ||
| } | ||
|
|
||
| public List<Card> getCards() { | ||
| return new ArrayList<>(cards); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package blackjack.domain.game; | ||
|
|
||
| import blackjack.domain.card.Deck; | ||
| import blackjack.domain.gamer.Dealer; | ||
| import blackjack.domain.gamer.GameResult; | ||
| import blackjack.domain.gamer.Player; | ||
| import blackjack.domain.gamer.Players; | ||
| import java.util.Map; | ||
|
|
||
| public class BlackjackGame { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 컨트롤러에서의 진행 로직을 최대한 |
||
|
|
||
| private final GameAccount gameAccount; | ||
| private final Deck deck; | ||
|
|
||
| public BlackjackGame() { | ||
| this.gameAccount = new GameAccount(); | ||
| this.deck = new Deck(); | ||
| } | ||
|
|
||
| public void distributeInitialHand(Players players, Dealer dealer) { | ||
| deck.shuffle(); | ||
| setUpInitialHands(players, dealer); | ||
| } | ||
|
|
||
|
|
||
| private void setUpInitialHands(Players players, Dealer dealer) { | ||
| players.initAllPlayersCard(deck); | ||
| dealer.initCard(deck); | ||
| } | ||
|
|
||
| public void betPlayerMoney(Map<Player, Money> playersBetMoney) { | ||
| for (Player player : playersBetMoney.keySet()) { | ||
| gameAccount.betMoney(player, playersBetMoney.get(player)); | ||
| } | ||
| } | ||
|
|
||
| public void addCardToPlayer(Player player) { | ||
| player.addCard(deck.draw()); | ||
| } | ||
|
|
||
| public void distributeCardToDealer(Dealer dealer) { | ||
| while (dealer.canReceiveCard()) { | ||
| dealer.addCard(deck.draw()); | ||
| } | ||
| } | ||
|
|
||
| public Money calculateDealerIncome(Players players, Dealer dealer) { | ||
| applyResultToBetMoney(players, dealer); | ||
| return gameAccount.calculateDealerIncome(); | ||
| } | ||
|
|
||
| private void applyResultToBetMoney(Players players, Dealer dealer) { | ||
| Map<Player, GameResult> playerGameResults = players.collectPlayerGameResults(dealer.getHandValue()); | ||
| gameAccount.applyGameResults(playerGameResults); | ||
| } | ||
|
|
||
| public Map<Player, Money> getStore() { | ||
| return gameAccount.getStore(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||||||||||||||||||||
| package blackjack.domain.game; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import blackjack.domain.gamer.GameResult; | ||||||||||||||||||||||||
| import blackjack.domain.gamer.Player; | ||||||||||||||||||||||||
| import java.util.LinkedHashMap; | ||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public class GameAccount { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private static final Map<Player, Money> store = new LinkedHashMap<>(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public void betMoney(Player player, Money money) { | ||||||||||||||||||||||||
| store.put(player, money); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public Money findMoney(Player player) { | ||||||||||||||||||||||||
| return store.get(player); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+8
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 커찬 말도 맞는 말이라고 생각해. 그런데 배팅 금액을 플레이어가 가지고 있게 되면, 플레이어의 역할이 너무 많아질 거라 생각해
그리고 Repository 개념의 Map을 선택한 이유를 커찬의 생각과 반대로 적용시켜보면
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앗 2번은 내가 일부러 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
나도 이 의견에 대해서 매우 공감해! 확실히 많이 무거워 질 것 같네...
그러면 Map이 단 하나만 존재하는게 아니라, |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public void applyGameResults(Map<Player, GameResult> gameResults) { | ||||||||||||||||||||||||
| for (Player player : gameResults.keySet()) { | ||||||||||||||||||||||||
| Money money = store.get(player); | ||||||||||||||||||||||||
| GameResult gameResult = gameResults.get(player); | ||||||||||||||||||||||||
| Money gameResultMoney = money.multipleRatio(gameResult.getRatio()); | ||||||||||||||||||||||||
| store.put(player, gameResultMoney); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public Money calculateDealerIncome() { | ||||||||||||||||||||||||
| int dealerIncome = 0; | ||||||||||||||||||||||||
| for (Money money : store.values()) { | ||||||||||||||||||||||||
| dealerIncome += money.value(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return new Money(-dealerIncome); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+29
to
+35
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
그리고 이렇게 고치면서 가장 좋은 효과를 볼 수 있는 부분은 따로 있다고 생각하는데 바로 dealerIncome이라는 불변하지 않은 변수를 사용하지 않아도 된다는 것! 상태가 변하게 되는 변수는 (맥락에 따라 다르겠지만) 시스템의 취약성과 직결된다고 생각해
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추가적으로 만약
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오오 리비 정말 좋은 의견인 것 같아 |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public Map<Player, Money> getStore() { | ||||||||||||||||||||||||
| return new LinkedHashMap<>(store); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public void clearStore() { | ||||||||||||||||||||||||
| store.clear(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
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.
덱 캐싱 👍🏻