-
Notifications
You must be signed in to change notification settings - Fork 0
Step2 놀러와요 누누의 step2 블랙잭에 #9
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 55 commits
dc2a806
aaf1160
86d3127
9bd63e2
092f1ab
bdd40b3
2931e75
3c963fe
886d8ae
acaf20f
adc2c80
36eb098
6e00bc0
077a5d4
2547f72
be4591d
f14ba4e
7be4015
b7f060a
6e3773c
618f993
9a4e3ab
27a8fed
7a8079d
fe7fb38
680486e
a6dd7ac
7cc4e18
35e55fb
0048059
359f18c
e3750dc
a6c01f0
0672801
0fa0ba9
e621337
8feda99
ed4540b
b4ddb0f
5a18897
5126cca
dd80fd6
6559e0a
30fad82
21d84e0
1dda7c0
2914060
7d16dcf
805def5
f677e7e
6df66b3
948dea8
1a1bef8
dbf6028
87d1212
68fa3ab
165c340
28d1fb8
40d04b2
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,95 @@ | ||
| package blackjack; | ||
|
|
||
| import blackjack.domain.card.Deck; | ||
| import blackjack.domain.card.ShuffledDeckFactory; | ||
| import blackjack.domain.participant.Participants; | ||
| import blackjack.domain.participant.Player; | ||
| import blackjack.domain.service.BlackJackRule; | ||
| import blackjack.view.DrawCommand; | ||
| import blackjack.view.InputView; | ||
| import blackjack.view.OutputView; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(final String[] args) { | ||
| final InputView inputView = new InputView(); | ||
| final OutputView outputView = new OutputView(); | ||
| final List<String> playerNames = inputPlayerNames(inputView); | ||
| final List<Integer> moneys = new ArrayList<>(); | ||
| for (final String playerName : playerNames) { | ||
| moneys.add(inputPlayerMoney(inputView, playerName)); | ||
| } | ||
| final Deck deck = new ShuffledDeckFactory().generate(); | ||
| final Participants participants = Participants.of(playerNames, moneys); | ||
| participants.distributeInitialCards(deck); | ||
| outputView.printInitialCards(participants.getDealerFirstCard(), participants.getPlayersCards()); | ||
|
|
||
| for (final String playerName : participants.getPlayerNames()) { | ||
| DrawCommand playerChoice = DrawCommand.DRAW; | ||
| while (participants.isPlayerDrawable(playerName) && playerChoice != DrawCommand.STAY) { | ||
| playerChoice = inputPlayerChoice(playerName, inputView); | ||
| if (playerChoice == DrawCommand.DRAW) { | ||
| participants.drawPlayerCard(playerName, deck.popCard()); | ||
| } | ||
| outputView.printCardStatusOfPlayer(playerName, participants.getPlayerCards(playerName)); | ||
| } | ||
| } | ||
| while (participants.isDealerDrawable()) { | ||
| participants.drawDealerCard(deck.popCard()); | ||
| outputView.printDealerCardDrawMessage(); | ||
| } | ||
|
|
||
| outputView.printFinalStatusOfDealer(participants.getDealerScore(), | ||
| participants.getDealerCards()); | ||
| outputView.printFinalStatusOfPlayers(participants.getPlayersCards(), | ||
| participants.getPlayersScores()); | ||
|
|
||
| final Map<String, Integer> playerMoney = new LinkedHashMap<>(); | ||
| for (final Player player : participants.getPlayers()) { | ||
| final int resultMoney = new BlackJackRule().calculatePlayerProfit(player, participants.getDealer()); | ||
| playerMoney.put(player.getName(), resultMoney); | ||
| } | ||
| outputView.printFinalMoney(playerMoney); | ||
| } | ||
|
|
||
| private static List<String> inputPlayerNames(final InputView inputView) { | ||
| return repeatUntilNoException(() -> { | ||
| final List<String> names = inputView.inputPlayerNames(); | ||
| Participants.validatePlayerNames(names); | ||
|
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. 좋은데요? |
||
| return names; | ||
| }, | ||
| inputView::printInputError); | ||
| } | ||
|
|
||
| private static int inputPlayerMoney(final InputView inputView, | ||
| final String playerName) { | ||
| return repeatUntilNoException(() -> { | ||
| final int amount = inputView.inputPlayerMoney(playerName); | ||
| Participants.validateBettingMoney(amount); | ||
| return amount; | ||
| }, | ||
| inputView::printInputError); | ||
| } | ||
|
|
||
| private static DrawCommand inputPlayerChoice(final String playerName, final InputView inputView) { | ||
| return repeatUntilNoException( | ||
| () -> inputView.inputCommand(playerName), inputView::printInputError); | ||
| } | ||
|
|
||
| private static <T> T repeatUntilNoException(final Supplier<T> supplier, | ||
| final Consumer<Exception> exceptionHandler) { | ||
| while (true) { | ||
| try { | ||
| return supplier.get(); | ||
| } catch (final IllegalArgumentException e) { | ||
| exceptionHandler.accept(e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package blackjack.domain.blackjack; | ||
|
|
||
| public enum ResultType { | ||
| BLACKJACK_WIN(1.5), | ||
| WIN(1), | ||
| TIE(0), | ||
| LOSE(-1), | ||
| BLACKJACK_LOSE(-1); | ||
|
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. 이 상태는 무엇을 의미할까요?
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. 그러게요 블랙잭으로 지는건가요? 뭔소리에요 도대체 리뷰를 이것도 못 하게 막았잖아요 |
||
|
|
||
| private final double playerProfit; | ||
|
|
||
| ResultType(final double playerProfit) { | ||
| this.playerProfit = playerProfit; | ||
| } | ||
|
|
||
| public double getPlayerProfit() { | ||
| return playerProfit; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public class Card { | ||
|
|
||
| private final Shape shape; | ||
| private final Symbol symbol; | ||
|
|
||
| public Card(final Shape shape, final Symbol symbol) { | ||
| this.shape = shape; | ||
| this.symbol = symbol; | ||
| } | ||
|
|
||
| boolean isAce() { | ||
| return symbol.isAce(); | ||
| } | ||
|
|
||
| int getScore() { | ||
| return symbol.getScore(); | ||
| } | ||
|
|
||
| public Symbol getSymbol() { | ||
| return symbol; | ||
| } | ||
|
|
||
| public Shape getShape() { | ||
| return shape; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| import blackjack.domain.card.dto.CardResponse; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class CardPocket { | ||
|
|
||
| private static final int BUST_SCORE = 21; | ||
| private static final int BLACKJACK_SCORE = 21; | ||
| private static final int VALUE_ACE = 10; | ||
|
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. 상수와 필드는 한 칸 띄어줘~
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. 으아 또 놓쳤네 고마워 |
||
|
|
||
| private final List<Card> cards; | ||
|
|
||
| private CardPocket(final List<Card> cards) { | ||
| validateCardPocket(cards); | ||
| this.cards = new ArrayList<>(cards); | ||
| } | ||
|
|
||
| public static CardPocket empty() { | ||
| return new CardPocket(new ArrayList<>()); | ||
| } | ||
|
|
||
| private void validateCardPocket(final List<Card> cards) { | ||
| if (cards == null) { | ||
| throw new IllegalArgumentException("카드에 null 값이 들어갈 수 없습니다"); | ||
|
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. 뷰에 보낼 메세지인데 null 이라고 하면 사용자는 뭐라 생각할까요?
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 void addCard(final Card card) { | ||
| cards.add(card); | ||
| calculateCurrentScore(); | ||
| } | ||
|
|
||
| private int calculateCurrentScore() { | ||
| final int countOfAce = countAce(); | ||
| int scoreOfCards = calculateMinimumScore(); | ||
| for (int i = 0; i < countOfAce; i++) { | ||
| scoreOfCards = calculateAceScore(scoreOfCards); | ||
|
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. 계산이 필요 없는 상황에서도 해당 메소드를 호출하는 경우가 있을 것 같은데 조건을 for문 혹은 while에 사용하여 불필요한 호출을 줄일 수 있는 방법이 있을 것 같네요! |
||
| } | ||
| return scoreOfCards; | ||
| } | ||
|
|
||
| public int calculateScore() { | ||
| return calculateCurrentScore(); | ||
| } | ||
|
|
||
| private int countAce() { | ||
| return (int) cards.stream() | ||
| .filter(Card::isAce) | ||
| .count(); | ||
| } | ||
|
|
||
| private int calculateMinimumScore() { | ||
| return cards.stream() | ||
| .mapToInt(Card::getScore) | ||
| .sum(); | ||
| } | ||
|
|
||
| private int calculateAceScore(final int score) { | ||
| if (score + VALUE_ACE > BUST_SCORE) { | ||
| return score; | ||
| } | ||
| return score + VALUE_ACE; | ||
| } | ||
|
|
||
| public List<CardResponse> getCards() { | ||
| return cards.stream() | ||
| .map(CardResponse::from) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public boolean isBlackJack() { | ||
| return cards.size() == 2 && calculateScore() == BLACKJACK_SCORE; | ||
|
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. 2는 상수로 빼도 될 것 같아
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. 아하 이것도 그렇네 고마워! |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| import java.util.Queue; | ||
|
|
||
| public class Deck { | ||
|
|
||
| private static final int NUMBER_OF_CARDS_IN_DECK = 52; | ||
|
|
||
| private final Queue<Card> cards; | ||
|
|
||
| Deck(final Queue<Card> cards) { | ||
| validateCards(cards); | ||
| this.cards = cards; | ||
| } | ||
|
|
||
| private void validateCards(final Queue<Card> cards) { | ||
| if (cards == null) { | ||
| throw new IllegalArgumentException("카드에 null 이 들어왔습니다"); | ||
|
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. 널 사랑해?
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. 나도 좋아! |
||
| } | ||
| if (cards.size() != NUMBER_OF_CARDS_IN_DECK) { | ||
| throw new IllegalArgumentException( | ||
| "카드 숫자는 " + NUMBER_OF_CARDS_IN_DECK + "장이어야 합니다 현재 :" + cards.size() + "장"); | ||
|
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. 에러 원인에 대한 상태표현 좋은데요?
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. that's cool |
||
| } | ||
| } | ||
|
|
||
| public Card popCard() { | ||
| if (cards.isEmpty()) { | ||
| throw new IllegalArgumentException("덱에 카드가 없습니다"); | ||
| } | ||
| return cards.remove(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public interface DeckFactory { | ||
|
|
||
| Deck generate(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public enum Shape { | ||
| HEART("하트"), | ||
| DIAMOND("다이아몬드"), | ||
| SPADE("스페이드"), | ||
| CLOVER("클로버"); | ||
| private final String name; | ||
|
Comment on lines
+7
to
+8
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. 띄어줘요 그리고 뷰에 대한 메세지를 1단계 때는 뺏었는데 다시 들어온 이유가 궁금해요
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. 이 부분은 안 띄워도 괜찮지 않나요? |
||
|
|
||
| Shape(final String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } | ||
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.
main 메소드가 길어서 전 안읽었는데 분리하는 것은 어때요? 분리 안한 이유가 궁금합니다
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.
ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
읽어주세요
#9 (comment)
여기서 볼 수 있는 것처럼 나름의 고민을 했는데, 그냥 분리 안 했어요