-
Notifications
You must be signed in to change notification settings - Fork 75
[이현주] 연료 주입, blackjack (Step1) #38
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: hyoenju
Are you sure you want to change the base?
Changes from all 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
b83a693
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,24 @@ | ||
# 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,65 @@ | ||
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 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 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) | ||
) | ||
); | ||
} | ||
|
||
public void receive(Player player) { | ||
Gameable gameable = player.getCards(); | ||
String yesOrNo = ""; | ||
do { | ||
yesOrNo = InputView.inputYesOrNo(player.getName()); | ||
if (yesOrNo.equals("y")) { | ||
gameable.addCard(CardDeck.pop()); | ||
OutputView.printCurrentCardsState(player.getName(), player.getCards()); | ||
gameable = gameable.judge(); | ||
} | ||
if (yesOrNo.equals("n")) { | ||
gameable = new State(gameable.cards(), false); | ||
} | ||
} while (gameable.isEnd()); | ||
} | ||
|
||
|
||
} |
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; | ||
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 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) { | ||
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. 디미터 법칙을 위반하고 있네요. |
||
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,57 @@ | ||
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 Game game; | ||
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 Winner(final Game game) { | ||
this.game = game; | ||
} | ||
|
||
public List<Integer> calculateDealerGameResult() { | ||
int dealerScore = game.getDealer().getCards().cards().sumScore(); | ||
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 0; | ||
} | ||
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) { | ||
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); | ||
} | ||
|
||
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,46 @@ | ||
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 score1, final int score2) { | ||
if ((score2 == CardNumber.ACE.getScore()) && (score1 + CardNumber.ACE.getScore() > 21)) { | ||
return 1; | ||
} | ||
return score2; | ||
} | ||
|
||
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.
함수(또는 메서드)의 길이가 10라인을 넘어가지 않도록 구현한다.
요구사항에 맞도록 구현해보면 어떨까요?