-
Notifications
You must be signed in to change notification settings - Fork 74
[황인규] 연료 주입, blackjack (Step1) #41
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: sk11ingyuhwang
Are you sure you want to change the base?
Changes from 75 commits
cb6c3ad
c0947c3
527c83f
7b498fa
9f5362d
3498e4c
ebcb132
d8cb870
80dd03e
8cafc20
0687344
19286f8
d8cc419
cf06f9e
a06b01f
ebcaf92
f4c30c4
bfded44
e518287
8c65c2f
1fa56ff
e43b483
512b778
aeaab59
bbfecb5
9146c02
875efb0
165f03c
f1b626d
fd92fd7
25e555a
d45bbcf
da370f9
75ab3a5
17c3904
bea5c54
c571bc5
e79cfef
00f6f1d
dfa02ad
0646780
13cc902
5403ec2
6c4694f
72bb5f9
db82882
089ff30
181a0e4
d08b32a
f86f7d4
ec8a9c6
2e180aa
a7bb315
57fb0f5
7f6b559
db9372d
4f8dbcd
aa60018
1b5e63c
bf9ab2c
dccbe46
ce019ba
5558990
ffb0e10
d04be38
aca0ca1
7057c55
91ba5d6
c19ccae
76937c7
4b04243
15f0789
57b88c1
b56f972
dfa073e
c6ca5f2
b6d0f5b
5ddbcbc
b838084
2d4a365
b7c9f70
f691dd1
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 +1,25 @@ | ||
| # java-blackjack | ||
| # 연료 주입 | ||
|
|
||
| ## 기능요구사항 | ||
|
|
||
| - [x] 렌터카는 Sonata 2대, Avante 1대, K5 2대로 총 5대의 차량을 보유하고 있다. | ||
| - [x] 차량 별 연비 정보를 저장하고 있다. | ||
| - Sonata : 10km/리터 | ||
| - Avante : 15km/리터 | ||
| - K5 : 13km/리터) | ||
| - 차량 별 보유 대수를 저장하고 있다. | ||
| - [x] 고객은 여행할 목적지의 대략적인 이동거리를 입력 받는다. | ||
| - [x] 이 이동거리를 활용해 차량 별로 필요한 연료를 주입한다. | ||
| - [x] 차량 별로 주입해야 할 연료량을 확인할 수 있는 보고서를 생성한다. | ||
| - [x] 보고서를 출력한다. | ||
|
|
||
| # java-blackjack | ||
|
|
||
| - [x] 게임에 참여할 사람의 이름을 입력하세요. | ||
| - [x] 게임이 시작하면 참여한 사람과 딜러는 카드 2개씩을 받는다. | ||
| - [x] 카드 숫자를 기본으로 하며, 예외로 Ace는 1 또는 11로 계산할 수 있으며, King, Queen, Jack은 각각 10으로 계산한다. | ||
| - [x] 게임에 참여한 사람 순서대로 카드를 더 받을지 물어보고 카드를 제공한다. 21점을 넘지 않을 경우 원한다면 얼마든지 카드를 계속 뽑을 수 있다. 단, 여기서 카드의 합은 | ||
| 21점을 넘어가지 않는다. | ||
| - [x] 게임에 딜러도 기존에 받았던 카드들의 합이 16점 이하이면 한장의 카드를 더 받아야 한다. 17점 이상이면 추가로 받을 수 없다. | ||
| - [x] 블랙잭 게임은 딜러와 플레이어 중 카드의 합이 21점 또는 21점에 가장 가까운 숫자를 가지는 쪽이 이기는 게임이다. | ||
| - [x] 블랙잭 게임을 완료한 후 각 플레이어별로 승패를 출력한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package blackjack; | ||
|
|
||
| import blackjack.controller.Controller; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| Controller controller = new Controller(); | ||
| controller.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package blackjack.controller; | ||
|
|
||
| import blackjack.domain.Game; | ||
| import blackjack.domain.Winner; | ||
| import blackjack.domain.card.CardDeck; | ||
| import blackjack.domain.player.Player; | ||
| import blackjack.domain.state.Gameable; | ||
| import blackjack.domain.state.State; | ||
| import blackjack.view.InputView; | ||
| import blackjack.view.OutputView; | ||
|
|
||
| public class Controller { | ||
|
|
||
| private static final String BLANK = ""; | ||
| private static final String YES_MESSAGE = "y"; | ||
| private static final String NO_MESSAGE = "y"; | ||
|
|
||
| private static void initGame(Game game) { | ||
| OutputView.printStartMessage(game); | ||
| OutputView.printDealerCard(game.getDealer()); | ||
| OutputView.printPlayerCard(game.getPlayers()); | ||
| } | ||
|
|
||
| public void run() { | ||
| Game game = new Game(InputView.inputPlayers()); | ||
| initGame(game); | ||
| playGame(game); | ||
| finishGame(game); | ||
| } | ||
|
|
||
| private void playGame(Game game) { | ||
| game.getPlayers().forEach(this::receive); | ||
| if (game.giveCardToDealer()) { | ||
| OutputView.printMessageToGiveCardToDealer(); | ||
| } | ||
| } | ||
|
|
||
| private void receive(Player player) { | ||
| Gameable gameable = player.getCards(); | ||
| String yesOrNo = BLANK; | ||
| do { | ||
| yesOrNo = InputView.inputYesOrNo(player.getName()); | ||
| if (yesOrNo.equals(YES_MESSAGE)) { | ||
| gameable.addCard(CardDeck.pop()); | ||
| OutputView.printCurrentCardsState(player.getName(), player.getCards()); | ||
| gameable = gameable.judge(); | ||
| } | ||
| if (yesOrNo.equals(NO_MESSAGE)) { | ||
| gameable = new State(gameable.cards(), false); | ||
| } | ||
| } while (gameable.isEnd()); | ||
| } | ||
|
|
||
| private void finishGame(Game game) { | ||
| Winner winner = new Winner(game); | ||
|
|
||
| OutputView.printGameResults(game.getDealer(), game.getPlayers()); | ||
|
|
||
| OutputView.printGameWinOrLose(game.getDealer(), winner.calculateDealerGameResult()); | ||
| game.getPlayers().forEach( | ||
| player -> OutputView.printGameWinOrLose( | ||
| player, winner.calculatePlayerGameResult(player) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package blackjack.domain; | ||
|
|
||
| import blackjack.domain.card.CardDeck; | ||
| import blackjack.domain.player.Dealer; | ||
| import blackjack.domain.player.Player; | ||
| import blackjack.domain.state.State; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Game { | ||
|
|
||
| private static final int PASS_CARD_NUMBER = 2; | ||
| private static final String DEALER_NAME = "딜러"; | ||
| private final static int DEALER_THRESHOLD = 16; | ||
|
|
||
| private final List<Player> players; | ||
| private final Dealer dealer; | ||
|
|
||
| public Game(Dealer dealer, List<Player> players) { | ||
| this.players = players; | ||
| this.dealer = dealer; | ||
| } | ||
|
|
||
| public Game(List<String> playerNames) { | ||
| this(new Dealer(DEALER_NAME, handOutCards()), playerNames.stream() | ||
| .map(Game::createPlayer) | ||
| .collect(Collectors.toList())); | ||
| } | ||
|
|
||
| private static Player createPlayer(String name) { | ||
| return new Player(name, handOutCards()); | ||
| } | ||
|
|
||
| private static State handOutCards() { | ||
| return new State(CardDeck.pop(PASS_CARD_NUMBER), true); | ||
| } | ||
|
|
||
| public boolean giveCardToDealer() { | ||
| if (dealer.getCards().cards().sumScore() <= DEALER_THRESHOLD) { | ||
| dealer.getCards().addCard(CardDeck.pop()); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public int getTotalScoreOfPlayer(Player player) { | ||
| return player.getCards().cards().sumScore(); | ||
| } | ||
|
|
||
| public List<Integer> getScoresOfPlayers() { | ||
| return players.stream() | ||
| .mapToInt(this::getTotalScoreOfPlayer) | ||
| .boxed().collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public List<Player> getPlayers() { | ||
| return Collections.unmodifiableList(players); | ||
| } | ||
|
|
||
| public Dealer getDealer() { | ||
| return dealer; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package blackjack.domain; | ||
|
|
||
| import blackjack.domain.player.Player; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Winner { | ||
|
|
||
| private final int BLACKJACK = 21; | ||
| private final int NUMBER_ZERO = 0; | ||
| private final Game game; | ||
|
|
||
| public Winner(final Game game) { | ||
| this.game = game; | ||
| } | ||
|
|
||
| public List<Integer> calculateDealerGameResult() { | ||
| int dealerScore = game.getDealer().getCards().cards().sumScore(); | ||
|
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 calculateGameResult(dealerScore); | ||
| } | ||
|
|
||
| public List<Integer> calculatePlayerGameResult(final Player player) { | ||
| int playerScore = player.getCards().cards().sumScore(); | ||
| return calculateGameResult(playerScore); | ||
| } | ||
|
|
||
| private List<Integer> calculateGameResult(final int sourceScore) { | ||
| List<Integer> targetScores = game.getScoresOfPlayers().stream() | ||
| .map(this::convertZeroScore) | ||
| .collect(Collectors.toList()); | ||
| return Arrays.asList(countWin(sourceScore, targetScores), | ||
| countLose(sourceScore, targetScores)); | ||
| } | ||
|
|
||
| private int convertZeroScore(final int score) { | ||
| if (score > BLACKJACK) { | ||
| return NUMBER_ZERO; | ||
| } | ||
| return score; | ||
| } | ||
|
|
||
| private int countWin(final int sourceScore, final List<Integer> targetScores) { | ||
| if (sourceScore > BLACKJACK) { | ||
| return 0; | ||
| } | ||
| return Long.valueOf(targetScores.stream() | ||
| .filter(targetScore -> sourceScore > targetScore).count()).intValue(); | ||
| } | ||
|
|
||
| private int countLose(final int sourceScore, final List<Integer> targetScores) { | ||
|
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 (sourceScore > BLACKJACK) { | ||
| return targetScores.size(); | ||
| } | ||
| return Long.valueOf(targetScores.stream() | ||
| .filter(targetScore -> sourceScore < targetScore).count()).intValue(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public class Card { | ||
|
|
||
| private final CardNumber cardNumber; | ||
| private final CardPattern cardPattern; | ||
|
|
||
| public Card(final CardNumber cardNumber, final CardPattern cardPattern) { | ||
| this.cardNumber = cardNumber; | ||
| this.cardPattern = cardPattern; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return cardNumber.getName() + cardPattern.getName(); | ||
| } | ||
|
|
||
| public CardNumber getCardNumber() { | ||
| return cardNumber; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class CardDeck { | ||
|
|
||
| private static final int FIRST_INDEX = 0; | ||
| private static List<Card> cards = new ArrayList<>(); | ||
|
|
||
| static { | ||
| Arrays.stream(CardNumber.values()).forEach( | ||
| cardNumber -> Arrays.stream(CardPattern.values()).forEach( | ||
| cardPattern -> cards.add(new Card(cardNumber, cardPattern)) | ||
| ) | ||
| ); | ||
| Collections.shuffle(cards); | ||
| } | ||
|
Comment on lines
+13
to
+26
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 Cards pop(final int count) { | ||
| List<Card> newCards = new ArrayList<>(cards.subList(FIRST_INDEX, count)); | ||
| cards = cards.subList(count, cards.size() - count); | ||
| return new Cards(newCards); | ||
| } | ||
|
|
||
| public static Card pop() { | ||
| return cards.remove(FIRST_INDEX); | ||
| } | ||
|
|
||
| public static List<Card> getCards() { | ||
| return Collections.unmodifiableList(cards); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public enum CardNumber { | ||
| ACE("A", 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), | ||
| JACK("J", 10), | ||
| QUEEN("Q", 10), | ||
| KING("K", 10); | ||
|
|
||
| private final String name; | ||
| private final int score; | ||
|
|
||
| CardNumber(final String name, final int score) { | ||
| this.name = name; | ||
| this.score = score; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getScore() { | ||
| return score; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| public enum CardPattern { | ||
| DIAMOND("다이아몬드"), | ||
| CLOVER("클로버"), | ||
| HEART("하트"), | ||
| SPADE("스페이드"); | ||
|
|
||
| private final String name; | ||
|
|
||
| CardPattern(final String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package blackjack.domain.card; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
|
|
||
| public class Cards { | ||
|
|
||
| private final static int BLACKJACK = 21; | ||
| private final List<Card> cards; | ||
|
|
||
| public Cards(final List<Card> cards) { | ||
| this.cards = cards; | ||
| } | ||
|
|
||
| private static int getScoreToSum(final int score, final int compareScore) { | ||
| if ((compareScore == CardNumber.ACE.getScore()) && (score + CardNumber.ACE.getScore() | ||
| > BLACKJACK)) { | ||
| return 1; | ||
| } | ||
| return compareScore; | ||
| } | ||
|
|
||
| public void add(final Card card) { | ||
| cards.add(card); | ||
| } | ||
|
|
||
| public boolean sum() { | ||
| return sumScore() >= BLACKJACK; | ||
| } | ||
|
|
||
| public int sumScore() { | ||
| Comparator<Card> comparator = (a, b) -> b.getCardNumber().getScore() - a.getCardNumber() | ||
| .getScore(); | ||
|
|
||
| List<Card> newCards = new ArrayList<>(cards); | ||
| newCards.sort(comparator); | ||
|
|
||
| return newCards.stream() | ||
| .mapToInt(card -> card.getCardNumber().getScore()) | ||
| .reduce(0, (a, b) -> a + getScoreToSum(a, b)); | ||
| } | ||
|
|
||
| public List<Card> getCards() { | ||
| return cards; | ||
| } | ||
| } |

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.
Person에서 카드 추가 유무와 현재 카드 상태를 가지도록 수정해 보는 것은 어떨까요?
현실 세계에서는 참가자와 딜러 손으로 카드를 받고, 현재 카드 점수를 판단합니다.