-
Notifications
You must be signed in to change notification settings - Fork 463
[1단계 - 블랙잭] 기론(김규철) 미션 제출합니다. #226
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
Changes from all commits
5aa725d
67807ba
ee1a9fe
b28bbe9
7161dc5
2ffc15c
6592e9d
6f32dec
ec380dd
65ea3ec
807fbf7
898beda
bd394f5
f7f5050
66f520b
ea9fa87
ac35b89
05827d6
8750ed4
0542747
002889e
bf87a2e
b39dc59
e4e796a
a64fdc1
3557611
f58dd8f
07a3b75
8c7ee51
e76aa89
8159fe2
afaf8e5
314393a
b3c44d5
ef0935d
0ac0ff6
8cae334
2393038
dbc2aa8
6e80286
da8abdd
3ed5fe1
c6a1387
b42b547
3f646fc
f7af5bb
5cb9d5a
d100e02
56ad261
d8586d9
ba13f67
d03126b
6ea48e5
2d334d3
2e80acc
83b7f5b
5d622a5
87a951e
4022d7d
79540cd
1268c7b
08781bb
3b9a5e5
51f79ec
178d91b
2eb9509
e8c5cc1
dc38f44
2d4c447
63786b2
860426a
10ec7f0
bee8566
ebe0b74
3e8852b
91dbac5
2a85843
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; | ||
|
|
||
| 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,59 @@ | ||
| package blackJack.controller; | ||
|
|
||
| import blackJack.domain.Card.CardFactory; | ||
| import blackJack.domain.User.Dealer; | ||
| import blackJack.domain.User.Player; | ||
| import blackJack.domain.User.Players; | ||
| import blackJack.dto.DealerResultDto; | ||
| import blackJack.dto.PlayerResultsDto; | ||
| import blackJack.dto.UserDto; | ||
| import blackJack.view.InputView; | ||
| import blackJack.view.OutputView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class BlackjackController { | ||
|
|
||
| public void run() { | ||
| CardFactory cardFactory = new CardFactory(); | ||
| List<String> inputPlayerNames = InputView.inputPlayerNames(); | ||
| Dealer dealer = new Dealer(cardFactory.initCards()); | ||
| Players players = Players.create(inputPlayerNames, cardFactory); | ||
| OutputView.printDrawMessage(inputPlayerNames); | ||
| OutputView.printTotalUserCards(convertToUserDtos(dealer, players)); | ||
|
|
||
| OutputView.printTotalResult(playGame(dealer, players, cardFactory)); | ||
|
|
||
| Map<String, String> statistics = players.getStatistics(dealer); | ||
| OutputView.printFinalResult(PlayerResultsDto.from(statistics), DealerResultDto.from(statistics)); | ||
| } | ||
|
|
||
| private List<UserDto> playGame(Dealer dealer, Players players, CardFactory cardFactory) { | ||
|
|
||
| players.getPlayers() | ||
| .forEach(player -> askOneMoreCard(player, cardFactory)); | ||
| while (dealer.hit(cardFactory)) { | ||
| OutputView.printAddDealerCard(); | ||
| } | ||
| return convertToUserDtos(dealer, players); | ||
|
|
||
| } | ||
|
|
||
| private void askOneMoreCard(Player player, CardFactory cardFactory) { | ||
| while (!player.isBust() && InputView.askOneMoreCard(player.getName())) { | ||
| player.hit(cardFactory); | ||
| OutputView.printPlayerCard(UserDto.from(player)); | ||
| } | ||
| } | ||
|
|
||
| private List<UserDto> convertToUserDtos(Dealer dealer, Players players) { | ||
| List<UserDto> userDtos = new ArrayList<>(); | ||
| userDtos.add(UserDto.from(dealer)); | ||
| players.getPlayers().stream() | ||
| .map(UserDto::from) | ||
| .forEach(userDtos::add); | ||
| return userDtos; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package blackJack.domain.Card; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import static blackJack.utils.ExeptionMessage.EMPTY_CARD; | ||
|
|
||
| public class Card { | ||
| private static final int TOTAL_CARDS_SIZE = 52; | ||
| private static final List<Card> CACHE = new ArrayList<>(TOTAL_CARDS_SIZE); | ||
|
|
||
| private final Shape shape; | ||
| private final Number number; | ||
|
|
||
| static { | ||
| Arrays.stream(Shape.values()) | ||
| .forEach(shape -> Arrays.stream(Number.values()) | ||
| .map(number -> new Card(shape, number)) | ||
| .forEach(CACHE::add)); | ||
| } | ||
|
Comment on lines
+17
to
+22
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(Shape shape, Number number) { | ||
| this.shape = shape; | ||
| this.number = number; | ||
| } | ||
|
|
||
| public static Card valueOf(Shape shape, Number number) { | ||
| return CACHE.stream().filter(card -> card.isSameShape(shape) && card.isSameNumber(number)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException(EMPTY_CARD)); | ||
| } | ||
|
Comment on lines
+29
to
+33
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 Shape getShape() { | ||
| return shape; | ||
| } | ||
|
|
||
| public Number getNumber() { | ||
| return number; | ||
| } | ||
|
|
||
| public boolean isAce() { | ||
| return number.isAce(); | ||
| } | ||
|
|
||
| private boolean isSameShape(Shape shape) { | ||
| return this.shape == shape; | ||
| } | ||
|
|
||
| private boolean isSameNumber(Number number) { | ||
| return this.number == number; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| Card card = (Card) o; | ||
| return shape == card.shape && number == card.number; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(shape, number); | ||
| } | ||
Gyuchool marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package blackJack.domain.Card; | ||
|
|
||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class CardFactory { | ||
|
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 static final int INIT_CARD_SIZE = 2; | ||
|
|
||
| private final Queue<Card> deck; | ||
|
|
||
| public CardFactory() { | ||
| List<Card> cards = createCards(); | ||
| Collections.shuffle(cards); | ||
| deck = new LinkedList<>(cards); | ||
| } | ||
|
|
||
| private List<Card> createCards() { | ||
| List<Card> cards = new ArrayList<>(); | ||
| Arrays.stream(Shape.values()) | ||
| .forEach(shape -> Arrays.stream(Number.values()) | ||
| .map(number -> Card.valueOf(shape, number)) | ||
| .forEach(cards::add)); | ||
| return cards; | ||
| } | ||
|
|
||
| public Cards initCards() { | ||
| List<Card> cards = IntStream.range(0, INIT_CARD_SIZE) | ||
| .mapToObj(i -> drawOneCard()) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return new Cards(cards); | ||
| } | ||
|
Comment on lines
+28
to
+34
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. 저는 collection을 구할때 stream을 유용하게 사용해서 웬만한 반복문은 통일성 있게 stream으로 바꾸려고 했었습니다! 또는 코드 길이가 길어지면 코드의 길이를 줄이기 또는 indent를 줄이기 위해 사용하기도 했습니다! forEach는 |
||
|
|
||
| public Card drawOneCard() { | ||
| return deck.poll(); | ||
| } | ||
|
|
||
| public int size() { | ||
| return deck.size(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package blackJack.domain.Card; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static blackJack.domain.Card.CardFactory.INIT_CARD_SIZE; | ||
|
|
||
| public class Cards { | ||
| private static final int BUST_LINE = 21; | ||
| private static final int EXTRA_SCORE = 10; | ||
|
|
||
| private final Set<Card> cards; | ||
|
|
||
| public Cards(List<Card> deck) { | ||
| this(new HashSet<>(deck)); | ||
| } | ||
|
|
||
| private Cards(Set<Card> deck) { | ||
| this.cards = new HashSet<>(deck); | ||
| } | ||
|
|
||
| public Set<Card> getCards() { | ||
| return new HashSet<>(cards); | ||
| } | ||
|
|
||
| public boolean add(Card card) { | ||
| return cards.add(card); | ||
| } | ||
|
Comment on lines
+27
to
+29
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 getScore() { | ||
| int score = cards.stream().mapToInt(card -> card.getNumber().getValue()).sum(); | ||
Gyuchool marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return addAceScore(score); | ||
| } | ||
|
|
||
| private int addAceScore(int score) { | ||
| long countAce = cards.stream().filter(Card::isAce).count(); | ||
|
|
||
| for (int i = 0; i < countAce; i++) { | ||
| score = calculateAceScore(score); | ||
| } | ||
| return score; | ||
| } | ||
|
|
||
| private int calculateAceScore(int score) { | ||
| if (score + EXTRA_SCORE <= BUST_LINE) { | ||
| score += EXTRA_SCORE; | ||
| } | ||
| return score; | ||
| } | ||
|
|
||
| public int size() { | ||
| return cards.size(); | ||
| } | ||
|
|
||
| public boolean isBlackJack() { | ||
| return cards.size() == INIT_CARD_SIZE && getScore() == BUST_LINE; | ||
| } | ||
|
|
||
| public boolean isBust() { | ||
| return getScore() > BUST_LINE; | ||
| } | ||
|
|
||
| public boolean isGreaterThan(Cards cards) { | ||
| return this.getScore() > cards.getScore(); | ||
| } | ||
|
|
||
| public boolean isSameScore(Cards cards) { | ||
| return !isBust() && this.getScore() == cards.getScore(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package blackJack.domain.Card; | ||
|
|
||
| public enum Number { | ||
| ACE("A", 1), | ||
| 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("J", 10), | ||
| QUEEN("Q", 10), | ||
| KING("K", 10); | ||
|
|
||
| private final String denomination; | ||
| private final int value; | ||
|
|
||
| Number(String denomination, int value) { | ||
| this.denomination = denomination; | ||
| this.value = value; | ||
| } | ||
|
|
||
| public String getDenomination() { | ||
| return denomination; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public boolean isAce() { | ||
| return this == ACE; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package blackJack.domain.Card; | ||
|
|
||
| public enum Shape { | ||
| HEART("하트"), | ||
| DIAMOND("다이아몬드"), | ||
| SPADE("스페이드"), | ||
| CLOVER("클로버"); | ||
|
|
||
| private final String shapeName; | ||
|
|
||
| Shape(String shapeName) { | ||
| this.shapeName = shapeName; | ||
| } | ||
|
|
||
| public String getShapeName() { | ||
| return shapeName; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package blackJack.domain; | ||
|
|
||
| import blackJack.domain.User.Dealer; | ||
| import blackJack.domain.User.Player; | ||
|
|
||
| public enum PlayerResult { | ||
| WIN("승"), | ||
| DRAW("무"), | ||
| LOSE("패"); | ||
|
|
||
| private final String value; | ||
|
|
||
| PlayerResult(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public String getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public static PlayerResult decision(Dealer dealer, Player player) { | ||
| if (isAllBlackJack(dealer, player) || dealer.isSameScore(player)) { | ||
| return PlayerResult.DRAW; | ||
| } | ||
| if (isLose(dealer, player)) { | ||
| return PlayerResult.WIN; | ||
| } | ||
|
Comment on lines
+25
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. isLose가 true면 Win을 반환하네요. 이 네이밍은 누구 기준일까요?
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 PlayerResult.LOSE; | ||
| } | ||
|
|
||
| private static boolean isLose(Dealer dealer, Player player) { | ||
| return player.isBlackJack() || dealer.isBust() || (player.isGreaterScoreThan(dealer) && !player.isBust()); | ||
| } | ||
|
|
||
| private static boolean isAllBlackJack(Dealer dealer, Player player) { | ||
| return dealer.isBlackJack() && player.isBlackJack(); | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.