-
Notifications
You must be signed in to change notification settings - Fork 0
[2단계 - 블랙잭 베팅] 커찬(이충안) 미션 제출합니다. #11
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
888b9e4
a2adac3
8315134
9ff2eb7
489c685
150cc56
a121d1a
106c036
0578c39
786608a
dac6b93
6146caf
5f7b0be
f2708af
979de37
7ed3ca0
124faa7
ad9ae1e
35d8986
a4e86a6
4cbebeb
c74a451
14c2d9b
cf300b7
2ddd8de
58e9925
51e5232
c707523
1ff93c7
25408c2
8e74520
8e13c47
69045b8
9d1e979
ddad41a
4a1481b
1d59566
329a6e8
795a5d3
4542815
243e8f8
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,12 @@ | ||
| package blackjack; | ||
|
|
||
| public class BlackJackApplication { | ||
|
|
||
| private BlackJackApplication() { | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| BlackJackGame blackJackGame = new BlackJackGame(); | ||
| blackJackGame.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package blackjack; | ||
|
|
||
| import blackjack.domain.card.Deck; | ||
| import blackjack.domain.money.Profit; | ||
| import blackjack.domain.participant.Dealer; | ||
| import blackjack.domain.participant.Player; | ||
| import blackjack.domain.participant.Players; | ||
| import blackjack.view.InputView; | ||
| import blackjack.view.OutputView; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class BlackJackGame { | ||
|
|
||
| private final InputView inputView = new InputView(); | ||
| private final OutputView outputView = new OutputView(); | ||
|
|
||
| public void run() { | ||
| Deck deck = Deck.createShuffledDeck(); | ||
| Dealer dealer = new Dealer(); | ||
| Players players = createPlayers(); | ||
|
|
||
| drawStartCards(dealer, players, deck); | ||
| play(players, dealer, deck); | ||
| printResult(dealer, players); | ||
| } | ||
|
|
||
| private Players createPlayers() { | ||
| List<String> names = inputView.inputPlayerNames(); | ||
| List<Player> players = names.stream() | ||
| .map(name -> Player.from(name, inputView.inputBetAmount(name))) | ||
| .toList(); | ||
| return new Players(players); | ||
| } | ||
|
|
||
| private void drawStartCards(Dealer dealer, Players players, Deck deck) { | ||
| dealer.drawStartCards(deck); | ||
| players.drawStartCards(deck); | ||
| outputView.printStartStatus(dealer, players); | ||
| } | ||
|
|
||
| private void play(Players players, Dealer dealer, Deck deck) { | ||
| for (Player player : players.getPlayers()) { | ||
| playTurn(player, deck); | ||
| } | ||
| playTurn(dealer, deck); | ||
| } | ||
|
|
||
| private void playTurn(Player player, Deck deck) { | ||
| while (player.isDrawable() && inputView.isPlayerWantDraw(player.getName())) { | ||
| player.add(deck.draw()); | ||
| outputView.printPlayerCards(player); | ||
| } | ||
| } | ||
|
|
||
|
Comment on lines
+49
to
+55
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. 저번에 얘기해줬던 리뷰어의 코멘트 반영했나 보네 👍 나는 개인적으로 provider느낌으로 아이디어가 번뜩인다고 생각하긴 했는데 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. 나와 페어도 번뜩이는 아이디어라고 생각해서 도입했는데, |
||
| private void playTurn(Dealer dealer, Deck deck) { | ||
| while (dealer.isDrawable()) { | ||
| outputView.printDealerDraw(); | ||
| dealer.add(deck.draw()); | ||
| } | ||
| } | ||
|
|
||
| private void printResult(Dealer dealer, Players players) { | ||
| outputView.printEndingStatus(dealer, players); | ||
|
|
||
| Profit dealerProfit = dealer.calculateDealerProfit(players); | ||
| Map<Player, Profit> playersProfit = dealer.calculatePlayersProfit(players); | ||
| outputView.printMatchResult(dealerProfit, playersProfit); | ||
| } | ||
| } | ||
|
Comment on lines
+63
to
+70
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. 솔직히 말하면, 그냥 생각없이 해버렸다는 게 맞을 것 같아요. 굳이 무의식을 분석해보자면,
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.StringJoiner; | ||
|
|
||
| public class Card { | ||
|
|
||
|
Comment on lines
+6
to
+7
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. record로 사용한다면 코드가 더 깔끔해질 것 같아요. |
||
| private final Value value; | ||
| private final Shape shape; | ||
|
|
||
| public Card(Value value, Shape shape) { | ||
| this.value = Objects.requireNonNull(value); | ||
| this.shape = Objects.requireNonNull(shape); | ||
| } | ||
|
|
||
| public int getMinScore() { | ||
| return value.getMinScore(); | ||
| } | ||
|
|
||
| public int getMaxScore() { | ||
| return value.getMaxScore(); | ||
| } | ||
|
|
||
| public boolean isAce() { | ||
| return value.isAce(); | ||
| } | ||
|
|
||
| public Value getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public Shape getShape() { | ||
| return shape; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object object) { | ||
| if (this == object) { | ||
| return true; | ||
| } | ||
| if (object == null || getClass() != object.getClass()) { | ||
| return false; | ||
| } | ||
| Card card = (Card) object; | ||
| return value == card.value && shape == card.shape; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(value, shape); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return new StringJoiner(", ", Card.class.getSimpleName() + "[", "]") | ||
| .add("value=" + value) | ||
| .add("shape=" + shape) | ||
| .toString(); | ||
| } | ||
|
Comment on lines
+53
to
+59
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. 디버깅용인가 🤔 toString은 디버깅 용으로 많이 사용한다고 하는데 어떤 부분에서 디버깅이 필요했는지, 테스트 코드만으로 방어할 수 없었는지 커찬의 생각 궁금하네 💭
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. 사실 toString()을 붙였던 시기는 2단계를 구현하다가 한 번 갈아엎기 전에 가시성이 떨어져서 붙였어 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Queue; | ||
|
|
||
| public class Deck { | ||
|
|
||
| private final Queue<Card> cards; | ||
|
|
||
| private Deck(List<Card> cards) { | ||
| this.cards = new LinkedList<>(cards); | ||
| } | ||
|
|
||
| public static Deck createShuffledDeck() { | ||
| List<Card> cards = Arrays.stream(Shape.values()) | ||
| .map(Deck::makeCards) | ||
| .flatMap(List::stream) | ||
| .collect(toList()); | ||
| Collections.shuffle(cards); | ||
| return new Deck(cards); | ||
| } | ||
|
|
||
| private static List<Card> makeCards(Shape shape) { | ||
| return Arrays.stream(Value.values()) | ||
| .map(value -> new Card(value, shape)) | ||
| .toList(); | ||
| } | ||
|
|
||
| public Card draw() { | ||
| if (cards.isEmpty()) { | ||
| throw new IllegalStateException("카드를 더 이상 뽑을 수 없습니다."); | ||
| } | ||
| return cards.poll(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Hand { | ||
|
|
||
| public static final int BLACKJACK_SCORE = 21; | ||
| private static final int BLACKJACK_SIZE = 2; | ||
|
|
||
| private final List<Card> cards; | ||
|
|
||
| public Hand(List<Card> cards) { | ||
| this.cards = List.copyOf(cards); | ||
| } | ||
|
|
||
| public Hand add(Card card) { | ||
| List<Card> newCards = new ArrayList<>(cards); | ||
| newCards.add(card); | ||
|
|
||
| return new Hand(newCards); | ||
| } | ||
|
Comment on lines
+17
to
+22
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. 처음에는 |
||
|
|
||
| public int calculateScore() { | ||
| int totalMinScore = getMinScore(); | ||
| int biggerScore = getBiggerScore(); | ||
|
|
||
| if (biggerScore > BLACKJACK_SCORE) { | ||
| return totalMinScore; | ||
| } | ||
| return biggerScore; | ||
| } | ||
|
|
||
| private int getMinScore() { | ||
| return cards.stream() | ||
| .mapToInt(Card::getMinScore) | ||
| .sum(); | ||
| } | ||
|
|
||
| private int getBiggerScore() { | ||
| int score = getMinScore(); | ||
| int differenceScore = cards.stream() | ||
| .filter(Card::isAce) | ||
| .mapToInt(this::calculateDifferenceScore) | ||
| .findAny() | ||
| .orElse(0); | ||
|
Comment on lines
+42
to
+46
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. 나는 오히려 반대. |
||
| return score + differenceScore; | ||
| } | ||
|
|
||
| private int calculateDifferenceScore(Card card) { | ||
| return card.getMaxScore() - card.getMinScore(); | ||
| } | ||
|
|
||
| public boolean isBusted() { | ||
| return calculateScore() > BLACKJACK_SCORE; | ||
| } | ||
|
|
||
| public boolean isBlackjack() { | ||
| return cards.size() == BLACKJACK_SIZE && calculateScore() == BLACKJACK_SCORE; | ||
| } | ||
|
|
||
| public List<Card> getCards() { | ||
| return cards; | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return cards.isEmpty(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public enum Shape { | ||
| SPADE, DIAMOND, HEART, CLOVER | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public enum Value { | ||
| ACE(1, 11), | ||
| TWO(2, 2), | ||
| THREE(3, 3), | ||
| FOUR(4, 4), | ||
| FIVE(5, 5), | ||
| SIX(6, 6), | ||
| SEVEN(7, 7), | ||
| EIGHT(8, 8), | ||
| NINE(9, 9), | ||
| TEN(10, 10), | ||
| JACK(10, 10), | ||
| QUEEN(10, 10), | ||
| KING(10, 10); | ||
|
|
||
| private final int minScore; | ||
| private final int maxScore; | ||
|
|
||
| Value(int minScore, int maxScore) { | ||
| this.minScore = minScore; | ||
| this.maxScore = maxScore; | ||
| } | ||
|
|
||
| public int getMinScore() { | ||
| return minScore; | ||
| } | ||
|
|
||
| public int getMaxScore() { | ||
| return maxScore; | ||
| } | ||
|
|
||
| public boolean isAce() { | ||
| return this == ACE; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package blackjack.domain.handrank; | ||
|
|
||
| import blackjack.domain.card.Hand; | ||
|
|
||
| public final class Blackjack implements HankRank { | ||
|
|
||
| @Override | ||
| public SingleMatchResult matchAtDealer(HankRank playerHandRank) { | ||
| if (playerHandRank.isBlackjack()) { | ||
| return SingleMatchResult.DRAW; | ||
| } | ||
| return SingleMatchResult.DEALER_WIN; | ||
| } | ||
|
|
||
| @Override | ||
| public int getScore() { | ||
| return Hand.BLACKJACK_SCORE; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isBlackjack() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isBust() { | ||
| return false; | ||
| } | ||
| } |
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.
페어였던 짱수에게 이 영광을 돌립니다 👍👍👍