-
Notifications
You must be signed in to change notification settings - Fork 0
[블랙잭 - 1단계] 아토(이혜린) 미션 제출합니다. #3
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 45 commits
143ebe7
21dae3a
1786e9f
76ef94b
3cd8593
f61ae03
be2c348
cc38fcc
959c905
4b04093
a10af0c
48dc12d
c305c41
7ceb8ba
9608caf
4ec8b0e
25353e0
9e6d1f3
9c16047
25088ec
de8d257
a1666f1
fc78bad
a86d395
e836cbe
3cbf5c0
efad4ef
6e0a373
86c80ac
3e0e9ee
46cc05c
4f27bbd
c245b03
70db6d4
cace848
a957752
d4da751
d888faa
5729a1d
1ca43df
ce53b0d
8969a9a
1a0d96e
4d35231
23b3015
0155159
1800705
76bed3c
33d8237
1f5d1ff
eb15bf0
cf27355
543e4a1
8c510c9
429555c
7622c5f
560a034
948d569
b3cc95c
b9c0665
5c8c80d
61950f8
a1b1c15
3e1c8f8
c99ed8e
c551c57
28dd3a1
dc16898
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,15 @@ | ||
| package blackjack; | ||
|
|
||
| import blackjack.game.BlackJackGame; | ||
| import blackjack.view.InputView; | ||
| import blackjack.view.OutputView; | ||
|
|
||
| public class BlackJackMain { | ||
|
|
||
| public static void main(String[] args) { | ||
| InputView inputView = new InputView(); | ||
| OutputView outputView = new OutputView(); | ||
| BlackJackGame blackJackGame = new BlackJackGame(inputView, outputView); | ||
| blackJackGame.play(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package blackjack.card; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Card { | ||
|
|
||
| private final Shape shape; | ||
| private final Number number; | ||
|
|
||
| public Card(Shape shape, Number number) { | ||
| this.shape = shape; | ||
| this.number = number; | ||
| } | ||
|
|
||
| public boolean isAce() { | ||
| return this.number == Number.ACE; | ||
| } | ||
|
|
||
| public int getScore() { | ||
| return number.getScore(); | ||
| } | ||
|
|
||
| public Shape getShape() { | ||
| return shape; | ||
| } | ||
|
|
||
| public Number getNumber() { | ||
| return 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); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package blackjack.card; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Queue; | ||
|
|
||
| public class Deck { | ||
|
|
||
| private final Queue<Card> cards; | ||
|
|
||
| Deck(List<Card> cards) { | ||
| this.cards = new LinkedList<>(cards); | ||
| } | ||
|
|
||
| public static Deck createShuffledFullDeck() { | ||
| List<Card> cards = new LinkedList<>(); | ||
| for (Shape shape : Shape.values()) { | ||
| cards.addAll(createNumberCardsOf(shape)); | ||
| } | ||
| Collections.shuffle(cards); | ||
| return new Deck(cards); | ||
| } | ||
|
|
||
| private static List<Card> createNumberCardsOf(Shape shape) { | ||
| List<Card> cards = new ArrayList<>(); | ||
| for (Number number : Number.values()) { | ||
| cards.add(new Card(shape, number)); | ||
| } | ||
| return cards; | ||
| } | ||
|
|
||
| public Card draw() { | ||
| if (cards.isEmpty()) { | ||
| throw new IllegalStateException("[ERROR] 덱이 비어있습니다."); | ||
| } | ||
| return cards.poll(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package blackjack.card; | ||
|
|
||
| public enum Number { | ||
|
||
|
|
||
| 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 score; | ||
|
|
||
| Number(int score) { | ||
| this.score = score; | ||
| } | ||
|
|
||
| int getScore() { | ||
| return this.score; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package blackjack.card; | ||
|
|
||
| public enum Shape { | ||
|
|
||
| HEART, | ||
| SPADE, | ||
| CLOVER, | ||
| DIAMOND | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package blackjack.game; | ||
|
|
||
| import blackjack.card.Deck; | ||
| import blackjack.player.Dealer; | ||
| import blackjack.player.Player; | ||
| import blackjack.player.Players; | ||
| import blackjack.view.InputView; | ||
| import blackjack.view.OutputView; | ||
| import java.util.List; | ||
|
|
||
| public class BlackJackGame { | ||
|
|
||
| public static final int BLACKJACK_MAX_SCORE = 21; | ||
|
||
|
|
||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
|
|
||
| public BlackJackGame(InputView inputView, OutputView outputView) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| } | ||
|
|
||
| public void play() { | ||
| Deck deck = Deck.createShuffledFullDeck(); | ||
| Dealer dealer = new Dealer(); | ||
|
|
||
| Players players = createPlayers(); | ||
| initGame(deck, dealer, players); | ||
| playersDrawMore(deck, players); | ||
| dealerDrawMore(deck, dealer); | ||
|
|
||
| showCardsWithScore(dealer, players); | ||
| showMatchResult(dealer, players); | ||
|
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 Players createPlayers() { | ||
| outputView.printNamesRequest(); | ||
| List<String> names = inputView.readNames(); | ||
| Players players = new Players(names); | ||
| outputView.printNewLine(); | ||
| return players; | ||
| } | ||
|
|
||
| private void initGame(Deck deck, Dealer dealer, Players players) { | ||
| players.initDrawCards(deck); | ||
| dealer.initDrawCards(deck); | ||
| outputView.printInitializeBlackJack(players.getNames()); | ||
| showInitCard(dealer, players); | ||
| } | ||
|
|
||
| private void showInitCard(Dealer dealer, Players players) { | ||
| outputView.printDealerFirstCard(dealer.getFirstCard()); | ||
|
|
||
| for (Player player : players.getPlayers()) { | ||
| outputView.printPlayerCards(player.getName(), player.getCards()); | ||
| } | ||
| outputView.printNewLine(); | ||
| } | ||
|
|
||
| private void playersDrawMore(Deck deck, Players players) { | ||
| for (Player player : players.getPlayers()) { | ||
| playerDrawMore(deck, player); | ||
| } | ||
| outputView.printNewLine(); | ||
| } | ||
|
|
||
| private void playerDrawMore(Deck deck, Player player) { | ||
| Command command = askPlayerToDrawMore(player); | ||
| if (command == Command.NO) { | ||
| return; | ||
| } | ||
| player.drawCard(deck); | ||
| outputView.printPlayerCards(player.getName(), player.getCards()); | ||
|
|
||
| if (player.hasDrawableScore()) { | ||
| playerDrawMore(deck, player); | ||
| } | ||
| } | ||
|
|
||
| private Command askPlayerToDrawMore(Player player) { | ||
| outputView.printDrawMoreCardRequest(player.getName()); | ||
| String input = inputView.readCommand(); | ||
| return Command.from(input); | ||
| } | ||
|
|
||
| private void dealerDrawMore(Deck deck, Dealer dealer) { | ||
| while (dealer.hasDrawableScore()) { | ||
| dealer.drawCard(deck); | ||
| outputView.printDealerDrawCard(); | ||
| outputView.printNewLine(); | ||
| } | ||
| } | ||
|
|
||
| private void showCardsWithScore(Dealer dealer, Players players) { | ||
| outputView.printDealerCardsWithScore(dealer.getCards(), dealer.getScore()); | ||
| for (Player player : players.getPlayers()) { | ||
| outputView.printPlayerCardsWithScore(player.getName(), player.getCards(), player.getScore()); | ||
| } | ||
| outputView.printNewLine(); | ||
| } | ||
|
|
||
| private void showMatchResult(Dealer dealer, Players players) { | ||
| MatchResults matchResults = calculateMatchResults(dealer, players); | ||
| outputView.printResultStart(); | ||
| showDealerResult(matchResults); | ||
| showPlayersResult(players, matchResults); | ||
| } | ||
|
|
||
| private MatchResults calculateMatchResults(Dealer dealer, Players players) { | ||
| MatchResults matchResults = new MatchResults(); | ||
| for (Player player : players.getPlayers()) { | ||
| matchResults.addResult(player.getName(), player.getScore(), dealer.getScore()); | ||
| } | ||
| return matchResults; | ||
| } | ||
|
|
||
| private void showDealerResult(MatchResults matchResults) { | ||
| outputView.printDealerResult( | ||
| matchResults.getResultCount(MatchResult.DEALER_WIN), | ||
| matchResults.getResultCount(MatchResult.TIE), | ||
| matchResults.getResultCount(MatchResult.PLAYER_WIN) | ||
| ); | ||
| } | ||
|
|
||
| private void showPlayersResult(Players players, MatchResults matchResults) { | ||
| for (Player player : players.getPlayers()) { | ||
| String playerName = player.getName(); | ||
| MatchResult result = matchResults.getResultByName(playerName); | ||
| outputView.printPlayerResult(playerName, result); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package blackjack.game; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public enum Command { | ||
| YES("y"), | ||
| NO("n"); | ||
|
|
||
| private final String value; | ||
|
|
||
| Command(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public static Command from(String value) { | ||
| return Arrays.stream(values()) | ||
| .filter(command -> command.value.equals(value)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException("[ERROR] 존재하지 않는 명령어입니다.")); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package blackjack.game; | ||
|
|
||
| public enum MatchResult { | ||
|
|
||
| DEALER_WIN, | ||
| PLAYER_WIN, | ||
| TIE; | ||
|
Comment on lines
+7
to
+9
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. 저는 WIN, LOSE 이런식으로 적은것 같은데 의미가 더 잘 전달되는 좋은이름이네요
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 static MatchResult chooseWinner(int playerScore, int dealerScore) { | ||
| if (isPlayerWinningCondition(playerScore, dealerScore)) { | ||
| return PLAYER_WIN; | ||
| } | ||
| if (isDealerWinningCondition(playerScore, dealerScore)) { | ||
| return DEALER_WIN; | ||
| } | ||
| return TIE; | ||
| } | ||
|
|
||
| private static boolean isPlayerWinningCondition(int playerScore, int dealerScore) { | ||
| if (isBurst(playerScore)) { | ||
| return false; | ||
| } | ||
| return isBurst(dealerScore) || playerScore > dealerScore; | ||
| } | ||
|
|
||
| private static boolean isDealerWinningCondition(int playerScore, int dealerScore) { | ||
| if (isBurst(playerScore)) { | ||
| return true; | ||
| } | ||
| return !isBurst(dealerScore) && dealerScore > playerScore; | ||
|
||
| } | ||
|
|
||
| private static boolean isBurst(int score) { | ||
| return score > BlackJackGame.BLACKJACK_MAX_SCORE; | ||
| } | ||
| } | ||
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.
옹 이렇게 페어와 지킬 컨벤션을 작성해두는것도 좋은 거 같네요!!