-
Notifications
You must be signed in to change notification settings - Fork 0
[1단계 - 블랙잭 게임 실행] 망쵸(김주환) 미션 제출합니다. #2
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 35 commits
6a4844d
34f668e
8163280
4d55c54
b180f62
5135ec3
49b5168
280ef53
14abcca
987e856
12bbf04
240e408
933b422
b42c767
dc3387f
043a26f
2ad912d
f252caa
cdb2713
710ec42
972dee8
4371415
8bb8fd2
35de854
2b06dd1
0f5681d
5d02370
6e32df2
4940a10
cbe46ac
0972560
ac428c6
8ba77c3
c29212d
c5555e5
a0e2241
c62821a
78e94fd
d1907c8
e23c9f5
944578d
c30e9e4
6de9287
a4df5f4
1b1d5a0
c205252
731dea0
adb40af
1b67385
6770804
f682149
0279a2e
e983f17
c2aafef
07d47c6
3670388
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 |
|---|---|---|
| @@ -1,7 +1,67 @@ | ||
| # java-blackjack | ||
| # 블랙잭 | ||
|
|
||
| 블랙잭 미션 저장소 | ||
|
|
||
| ## 우아한테크코스 코드리뷰 | ||
| ## 기능 요구 사항 | ||
|
|
||
| - [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md) | ||
| ### 플레이어 이름 | ||
|
|
||
| ### 플레이어 | ||
|
|
||
| - [X] 플레이어는 이름을 갖는다 | ||
| - [X] 플레이어는 카드들을 갖는다 | ||
| - [X] 플레이어는 카드를 저장한다 | ||
| - [X] 죽었는지 안 죽었는지 여부를 반환한다 (21을 초과하는지) | ||
|
|
||
|
|
||
| ### 카드 | ||
|
|
||
| - [X] 문자와 모양을 상태로 갖는다 | ||
|
|
||
|
|
||
| ### Hand(카드들) | ||
|
|
||
| - [X] 여러개의 카드를 갖는다 | ||
| - [X] 카드의 총합 계산한다 | ||
| - [X] 카드 추가한다 | ||
|
|
||
|
|
||
| ### 카드 게임 | ||
|
|
||
| - [X] 각 플레이어에게 카드를 2장씩 지급한다 | ||
| - [X] 플레이어마다 추가 지급한다 | ||
|
|
||
|
|
||
| ### 덱 | ||
|
|
||
| - [X] 모든 카드를 1장씩 갖고 있다. | ||
| - [X] 랜덤으로 카드를 뽑는다. | ||
|
|
||
|
|
||
| ### 게임 승패 결정 | ||
|
|
||
| - [X] 딜러와 모든 플레이어의 승패 여부를 결정한다. | ||
| - [X] 딜러와 플레이어 둘다 21을 초과할 경우, 플레이어가 패배한다. | ||
| - [X] 카드 합계가 딜러는 21 이하, 플레이어는 21 초과인 경우, 플레이어가 패배한다. | ||
| - [X] 카드 합계가 딜러는 21 초과, 플레이어는 21 이하인 경우, 플레이어가 승리한다. | ||
| - [X] 카드 합계가 딜러와 플레이어 모두 21 이하인 경우, 숫자가 큰 사람이 승리한다. | ||
| - [X] 카드 합계가 딜러와 플레이어 모두 21 이하이고 동일한 경우, 무승부다. | ||
|
|
||
|
|
||
| ### 인풋 뷰 | ||
|
|
||
| - [X] 참가자의 이름을 입력받는다. | ||
| - [X] 이름은 쉼표 기준으로 분리한다. | ||
| - [X] 카드 추가 여부를 입력받는다. | ||
| - [X] y 또는 n이 아닌 경우, 예외를 발생한다. | ||
| - [X] 사용자 카드 합이 21을 초과하면, 카드 추가 여부를 묻지 않는다. | ||
| - [X] 플레이어가 n을 입력할 때까지 카드 추가 여부를 묻는다. | ||
|
|
||
|
|
||
| ### 아웃풋 뷰 | ||
|
|
||
| - [X] 딜러와 플레이어 전원에게 초기에 분배한 카드 출력한다. | ||
| - [X] 딜러의 카드는 1장만 공개한다. | ||
| - [X] 플레이어가 보유한 모든 카드를 출력한다. | ||
| - [X] 딜러가 추가 카드를 발급 받았는지 여부 출력한다. | ||
| - [X] 보유한 모든 카드의 합을 출력한다. | ||
| - [X] 최종 승패를 출력한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package blackjack; | ||
|
|
||
| import blackjack.domain.cardgame.CardGame; | ||
| import blackjack.domain.cardgame.CardGameJudge; | ||
| import blackjack.domain.cardgame.CardGameResult; | ||
| import blackjack.domain.player.Dealer; | ||
| import blackjack.domain.player.Player; | ||
| import blackjack.view.InputView; | ||
| import blackjack.view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class BlackjackController { | ||
| public static void main(String[] args) { | ||
| final CardGame cardGame = new CardGame(); | ||
| final List<String> names = InputView.askPlayerNames(); | ||
| final Dealer dealer = new Dealer(); | ||
| final List<Player> players = names.stream() | ||
| .map(Player::new) | ||
| .toList(); | ||
|
|
||
| cardGame.initializeHand(dealer, players); | ||
| OutputView.printInitialHandOfEachPlayer(dealer, players); | ||
|
|
||
| for (final Player player : players) { | ||
| givePlayerMoreCardsIfWanted(cardGame, player); | ||
| } | ||
| giveDealerMoreCardsIfNeeded(cardGame, dealer); | ||
|
|
||
| printHandStatusOfEachPlayer(dealer, players); | ||
| printCardGameResult(dealer, players); | ||
| } | ||
|
|
||
| private static void givePlayerMoreCardsIfWanted(final CardGame cardGame, final Player player) { | ||
| final String playerName = player.getName(); | ||
| while (player.isAlive() && InputView.askForMoreCard(playerName)) { | ||
| cardGame.giveCard(player); | ||
| OutputView.printPlayerCard(player); | ||
| } | ||
| } | ||
|
|
||
| private static void giveDealerMoreCardsIfNeeded(final CardGame cardGame, final Dealer dealer) { | ||
| while (dealer.isMoreCardNeeded()) { | ||
| cardGame.giveCard(dealer); | ||
| OutputView.printDealerHitMessage(dealer); | ||
| } | ||
| } | ||
|
|
||
| private static void printHandStatusOfEachPlayer(final Dealer dealer, final List<Player> players) { | ||
| OutputView.printPlayerCardWithScore(dealer); | ||
| for (final Player player : players) { | ||
| OutputView.printPlayerCardWithScore(player); | ||
| } | ||
| } | ||
|
|
||
| private static void printCardGameResult(final Dealer dealer, final List<Player> players) { | ||
| final CardGameJudge cardGameJudge = new CardGameJudge(); | ||
| final CardGameResult cardGameResult = cardGameJudge.judge(dealer, players); | ||
| OutputView.printResult(cardGameResult); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public class Card { | ||
| private final CardNumber cardNumber; | ||
| private final CardShape cardShape; | ||
|
|
||
| public Card(final CardNumber cardNumber, final CardShape cardShape) { | ||
| this.cardNumber = cardNumber; | ||
| this.cardShape = cardShape; | ||
| } | ||
|
|
||
| public boolean isAceCard() { | ||
| return cardNumber == CardNumber.ACE; | ||
| } | ||
|
|
||
| public int getNumber() { | ||
| return cardNumber.getValue(); | ||
| } | ||
|
|
||
| public String getShape() { | ||
| return cardShape.getName(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public enum CardNumber { | ||
| ACE(1), | ||
| 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(final int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public enum CardShape { | ||
| SPADE("스페이드"), | ||
| HEART("하트"), | ||
| DIAMOND("다이아몬드"), | ||
| CLOVER("클로버"); | ||
|
||
|
|
||
| private final String name; | ||
|
|
||
| CardShape(final String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package blackjack.domain.cardgame; | ||
|
|
||
| import blackjack.domain.card.Card; | ||
| import blackjack.domain.card.CardNumber; | ||
| import blackjack.domain.card.CardShape; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Stack; | ||
|
|
||
| public class CardDeck { | ||
| private final Stack<Card> deck; | ||
|
||
|
|
||
| public CardDeck() { | ||
| final Stack<Card> deck = new Stack<>(); | ||
|
|
||
| for (final CardNumber cardNumber : CardNumber.values()) { | ||
| for (final CardShape cardShape : CardShape.values()) { | ||
| deck.push(new Card(cardNumber, cardShape)); | ||
| } | ||
|
||
| } | ||
|
|
||
| Collections.shuffle(deck); | ||
|
|
||
| this.deck = deck; | ||
| } | ||
|
|
||
| public Card draw() { | ||
| return deck.pop(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package blackjack.domain.cardgame; | ||
|
|
||
| import blackjack.domain.player.Dealer; | ||
| import blackjack.domain.player.Player; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class CardGame { | ||
| private final CardDeck cardDeck; | ||
|
|
||
| public CardGame() { | ||
| this.cardDeck = new CardDeck(); | ||
| } | ||
|
|
||
| public void giveCard(final Player player) { | ||
| player.addCard(cardDeck.draw()); | ||
| } | ||
|
|
||
| public void initializeHand(final Dealer dealer, final List<Player> players) { | ||
| giveCard(dealer); | ||
| giveCard(dealer); | ||
| giveTwoCardsEachPlayer(players); | ||
| } | ||
|
||
|
|
||
| private void giveTwoCardsEachPlayer(final List<Player> players) { | ||
|
||
| for (final Player player : players) { | ||
| giveCard(player); | ||
| giveCard(player); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package blackjack.domain.cardgame; | ||
|
|
||
| import blackjack.domain.player.Player; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class CardGameJudge { | ||
| public static final int BUST_THRESHOLD = 21; | ||
|
|
||
| public CardGameResult judge(final Player dealer, final List<Player> players) { | ||
| Map<Player, WinningStatus> result = new LinkedHashMap<>(); | ||
|
|
||
| for (final Player player : players) { | ||
| WinningStatus winningStatus = judge(dealer, player); | ||
| result.put(player, winningStatus); | ||
| } | ||
|
|
||
| return new CardGameResult(result); | ||
| } | ||
|
|
||
| private WinningStatus judge(final Player dealer, final Player player) { | ||
| int dealerScore = dealer.getScore(); | ||
| int playerScore = player.getScore(); | ||
|
|
||
| return doesPlayerWin(dealerScore, playerScore); | ||
| } | ||
|
|
||
| private WinningStatus doesPlayerWin(final int dealerScore, final int playerScore) { | ||
| if (playerScore > BUST_THRESHOLD) { | ||
| return WinningStatus.LOSE; | ||
| } | ||
| if (dealerScore > BUST_THRESHOLD) { | ||
| return WinningStatus.WIN; | ||
| } | ||
| if (dealerScore == playerScore) { | ||
| return WinningStatus.PUSH; | ||
| } | ||
| if (dealerScore < playerScore) { | ||
| return WinningStatus.WIN; | ||
| } | ||
| return WinningStatus.LOSE; | ||
| } | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package blackjack.domain.cardgame; | ||
|
|
||
| import blackjack.domain.player.Player; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| import static blackjack.domain.cardgame.WinningStatus.LOSE; | ||
| import static blackjack.domain.cardgame.WinningStatus.WIN; | ||
|
|
||
| public class CardGameResult { | ||
| private final Map<Player, WinningStatus> totalResult; | ||
|
|
||
| public CardGameResult(Map<Player, WinningStatus> totalResult) { | ||
| this.totalResult = totalResult; | ||
| } | ||
|
|
||
| public Map<Player, WinningStatus> getTotalResult() { | ||
| return Collections.unmodifiableMap(totalResult); | ||
| } | ||
|
|
||
| public int getDealerWinCount() { | ||
| return (int) totalResult.values() | ||
| .stream() | ||
| .filter(playerWinningStatus -> playerWinningStatus.equals(LOSE)) | ||
| .count(); | ||
| } | ||
|
|
||
| public int getDealerLoseCount() { | ||
| return (int) totalResult.values() | ||
| .stream() | ||
| .filter(status -> status.equals(WIN)) | ||
|
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. 위에 +딜러의 무승부 결과는 출력하지 않나요? |
||
| .count(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package blackjack.domain.cardgame; | ||
|
|
||
| public enum WinningStatus { | ||
| WIN("승"), | ||
| PUSH("무"), | ||
| LOSE("패"); | ||
|
|
||
| private final String value; | ||
|
|
||
| WinningStatus(final String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public String getValue() { | ||
| return value; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package blackjack.domain.player; | ||
|
|
||
| import blackjack.domain.card.Card; | ||
|
|
||
| public class Dealer extends Player { | ||
| private static final String NAME = "딜러"; | ||
|
||
| private static final int HIT_THRESHOLD = 16; | ||
|
|
||
| public Dealer() { | ||
| super(NAME); | ||
| } | ||
|
|
||
| public boolean isMoreCardNeeded() { | ||
| return this.hand.getSum() <= HIT_THRESHOLD; | ||
| } | ||
|
|
||
| public Card getFirstCard() { | ||
| try { | ||
| return hand.getAllCards().get(0); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| throw new RuntimeException("[ERROR] 딜러가 카드를 갖고 있지 않습니다."); | ||
| } | ||
| } | ||
| } | ||
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.
구글 자바 컨벤션에서는 non static import 사이에 엔터가 없는 게 규칙이더라구요~~~