-
Notifications
You must be signed in to change notification settings - Fork 75
[김윤경] 연료주입, 블랙잭 (Step1) #44
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: fpal95
Are you sure you want to change the base?
Changes from all commits
6e7164a
dc3370c
02f2075
d0b0ed4
fe0aeaf
58d95c4
737b38b
0072001
d254e79
57ab33e
13f2df3
723a695
0c6149e
43e7536
99375ef
24d421f
4863c10
f3a00b4
ca9bc9b
a847574
cdc3af1
a1079a0
61e8084
9427d25
08557de
856da6c
60bae55
b9d844c
f37cbca
063732b
5e6fad3
380dc86
9abddc6
9c52e6a
f45209c
f5c4c7d
edecc81
610f916
45d6ee9
97461b0
17816e4
b7359f2
4d55fdb
e504b4d
8738a73
afe6bad
6f85082
381a22a
d6aa26a
3e1ba59
59e0c1f
5da9180
e5e664e
322d46c
c46df95
416d4da
277d667
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,78 @@ | ||
# java-blackjack | ||
# java-blackjack | ||
|
||
## 연료 주입 기능 목록 | ||
|
||
MotorVehicle (Interface) | ||
|
||
- 리터당 이동 거리를 반환 getDistancePerLiter | ||
- 여행할 거리를 반환 getTripDistance | ||
- 차종 이름을 반환 getName | ||
|
||
Car (abstract class) (implements MotorVehicle) | ||
|
||
- 거리에 따른 연료량을 계산 후 반환 getChargeQuantity | ||
- 멤버변수 distance | ||
|
||
Sonata, Avante, K5 (extends Car) | ||
|
||
- 생성자로 distance 받기 | ||
- private static final 로 연비를 가짐. (10km/리터, 15, 13) | ||
|
||
RentCompany | ||
|
||
- 차 List를 가짐. | ||
- private 생성자 | ||
- static create()을 통해 객체 생성 | ||
- addCar(Car) 를 통해 차 List에 저장 | ||
- generateReport 를 통해 report String 반환 | ||
|
||
## 블랙잭 | ||
|
||
[Card : Denomination, Shape] | ||
|
||
- [x] enum으로 1~10,J,Q,K를 구현한다. | ||
- [x] enum으로 하트,스페이드,다이아몬드,클로버를 구현한다. | ||
- [x] value와 shape를 합쳐 한 장의 카드를 생성한다. | ||
|
||
[Person] | ||
|
||
- [x] 모든 사람들은 카드 목록을 갖고 있다. | ||
- [x] 카드 1장을 인자로 전달 받아 카드 목록에 추가한다. | ||
|
||
[Player extends Person] | ||
|
||
- [x] 플레이어는 추가적으로 이름을 갖고 있다. | ||
- [x] 생성 시에 플레이어 이름만 인자로 전달 받아서 자기의 이름을 초기화한다. | ||
- [x] 전달 받은 1장의 카드를 자신의 카드 목록에 추가한다. (Person 기능) | ||
|
||
[Dealer extends Person] | ||
딜러가 카드를 섞어서 뽑는 역할. CardDeck으로 분리. | ||
|
||
- [x] 생성 시에 CardDeck을 초기화한다. | ||
- [x] 인자로 전달 받은 1장의 카드를 자신의 카드 목록에 추가한다. (Person 기능) | ||
- [x] 전달 인자로 받은 장수만큼 카드를 반환한다. | ||
|
||
[CardDeck] | ||
|
||
- [x] 전체 카드를 만들어서 리스트로 반환한다. (static) | ||
- [x] 인스턴스 생성 시, 만들어진 52장의 카드로 멤버변수를 초기화한다. | ||
- [x] shuffle한 뒤 전달인자로 받은 수만큼 카드를 뽑아서 반환한다. (제거하면서 반환한다.) | ||
|
||
[GameApplication] | ||
|
||
- [x] GameSetup의 Dealer와 List<Player>를 입력받고, 생성된 setup 내용을 ResultView에게 넘긴다. | ||
|
||
- [ ] 카드의 합이 21 이하인지 카드를 추가로 뽑을 것인지 질문한다. | ||
- [ ] 딜러에게 필요한 장수만큼 카드를 요구한다. | ||
- [ ] 반환 받은 카드를 통해 딜러와 플레이어들을 초기화한다. | ||
|
||
[InputView] | ||
|
||
-[x] 플레이어 이름을 입력받아서 쉼표 기준으로 분리한 List<String>을 반환한다. | ||
-[x] 플레이어에게 카드를 추가로 받을 것인지 입력받는다. | ||
|
||
[ResultView] | ||
|
||
-[x] 딜러와 플레이어 List<>를 전달 받아 이름과 카드 목록을 출력한다. | ||
-[x] 처음 시작 때 딜러는 1장의 카드만 출력한다. | ||
-[ ] 딜러와 플레이어의 최종 승패를 출력한다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package blackjack; | ||
|
||
import blackjack.controller.BlackJackGame; | ||
|
||
public class BlackjackApplication { | ||
|
||
public static void main(String[] args) { | ||
BlackJackGame blackJackGame = new BlackJackGame(); | ||
blackJackGame.play(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package blackjack.controller; | ||
|
||
import blackjack.domain.game.GameResult; | ||
import blackjack.domain.person.Dealer; | ||
import blackjack.domain.person.Player; | ||
import blackjack.view.InputView; | ||
import blackjack.view.ResultView; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class BlackJackGame { | ||
|
||
private final Dealer dealer; | ||
private final List<Player> players; | ||
|
||
public BlackJackGame() { | ||
dealer = new Dealer(); | ||
players = Player.createPlayers(InputView.getPlayerNames(), dealer); | ||
ResultView.printSetUpResult(dealer.getOpenedCard(), players); | ||
} | ||
|
||
public void play() { | ||
askPlayers(); | ||
giveDealerCard(); | ||
printGameResult(); | ||
} | ||
|
||
private void giveDealerCard() { | ||
if (dealer.canGetCard()) { | ||
dealer.addCard(dealer.getPickedCard()); | ||
ResultView.printDealerReceiveCard(); | ||
} | ||
} | ||
|
||
private void askPlayers() { | ||
for (Player player : players) { | ||
askPlayer(player); | ||
} | ||
} | ||
|
||
private void askPlayer(Player player) { | ||
while (player.canHit() && InputView.isHit(player)) { | ||
player.addCard(dealer.getPickedCard()); | ||
} | ||
} | ||
|
||
private void printGameResult() { | ||
ResultView.printDealerAndPlayerCardResult(dealer, players); | ||
ResultView.printGameResult(getGameResult()); | ||
} | ||
|
||
private GameResult getGameResult() { | ||
Map<String, String> playerResult = new HashMap<>(); | ||
int dealerWin = 0; | ||
int dealerSum = dealer.getSumOfCards(); | ||
|
||
for (Player player : players) { | ||
if (player.getSumOfCards() < dealerSum) { | ||
dealerWin++; | ||
playerResult.put(player.getName(), "패"); | ||
continue; | ||
} | ||
playerResult.put(player.getName(), "승"); | ||
} | ||
|
||
return new GameResult(playerResult, dealerWin, players.size() - dealerWin); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package blackjack.domain.card; | ||
|
||
public class Card { | ||
|
||
private final Shape shape; | ||
private final Denomination denomination; | ||
|
||
public Card(Shape shape, Denomination denomination) { | ||
this.shape = shape; | ||
this.denomination = denomination; | ||
} | ||
|
||
public Shape getShape() { | ||
return shape; | ||
} | ||
|
||
public Denomination getDenomination() { | ||
return denomination; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class CardDeck { | ||
|
||
public static final int FIRST_INDEX = 0; | ||
|
||
private List<Card> cards; | ||
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 CardDeck() { | ||
cards = createCardDeck(); | ||
} | ||
|
||
private List<Card> createCardDeck() { | ||
List<Card> cards = new ArrayList<>(); | ||
|
||
for (Shape shape : Shape.values()) { | ||
Arrays.stream(Denomination.values()) | ||
.map(denomination -> new Card(shape, denomination)) | ||
.forEach(cards::add); | ||
Comment on lines
+22
to
+24
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 cards; | ||
} | ||
|
||
public Card pickOneCard() { | ||
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. 카드가 비어있을 때에 대한 validation을 추가해보면 어떨까요? |
||
Collections.shuffle(cards); | ||
Card pickedCard = cards.get(FIRST_INDEX); | ||
cards.remove(FIRST_INDEX); | ||
return pickedCard; | ||
} | ||
|
||
public List<Card> getCards() { | ||
return new ArrayList<>(cards); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.List; | ||
|
||
public class CardsUtils { | ||
|
||
public static boolean isSumOfCardValueSmallerThanOrEquals(List<Card> cards, Integer target) { | ||
Integer sumOfValue = getSumOfValue(cards); | ||
return isSmallerThanOrEquals(sumOfValue, target); | ||
} | ||
|
||
private static Integer getSumOfValue(List<Card> cards) { | ||
return cards.stream() | ||
.mapToInt(card -> card.getDenomination().getValue()) | ||
.sum(); | ||
} | ||
|
||
private static boolean isSmallerThanOrEquals(Integer number, Integer target) { | ||
return number <= target; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package blackjack.domain.card; | ||
|
||
public enum Denomination { | ||
|
||
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 sign; | ||
private final int value; | ||
|
||
Denomination(String sign, int value) { | ||
this.sign = sign; | ||
this.value = value; | ||
} | ||
|
||
public String getSign() { | ||
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 sign; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package blackjack.domain.card; | ||
|
||
public enum Shape { | ||
|
||
HEART("하트"), | ||
DIAMOND("다이아몬드"), | ||
SPADE("스페이드"), | ||
CLUB("클로버"); | ||
|
||
private final String name; | ||
|
||
Shape(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package blackjack.domain.game; | ||
|
||
import java.util.Map; | ||
|
||
public class GameResult { | ||
|
||
private final Map<String, String> playerResult; | ||
private final int dealerWin; | ||
private final int dealerLose; | ||
|
||
public GameResult(Map<String, String> playerResult, int dealerWin, int dealerLose) { | ||
this.playerResult = playerResult; | ||
this.dealerWin = dealerWin; | ||
this.dealerLose = dealerLose; | ||
} | ||
|
||
public Map<String, String> getPlayerResult() { | ||
return playerResult; | ||
} | ||
|
||
public int getDealerWin() { | ||
return dealerWin; | ||
} | ||
|
||
public int getDealerLose() { | ||
return dealerLose; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package blackjack.domain.person; | ||
|
||
import blackjack.domain.card.Card; | ||
import blackjack.domain.card.CardDeck; | ||
import blackjack.domain.card.Denomination; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Dealer extends Person { | ||
|
||
private static final int FIRST_INDEX = 0; | ||
private static final int INITIAL_PICK_NUMBER = 2; | ||
private static final int DEALER_MAXIMUM_SUM = 16; | ||
|
||
private final CardDeck cardDeck = new CardDeck(); | ||
|
||
public Dealer() { | ||
this.cards.addAll(getInitialPickedCards()); | ||
} | ||
|
||
public List<Card> getInitialPickedCards() { | ||
List<Card> cards = new ArrayList<>(); | ||
for (int i = 0; i < INITIAL_PICK_NUMBER; i++) { | ||
cards.add(cardDeck.pickOneCard()); | ||
} | ||
return cards; | ||
} | ||
|
||
public Card getPickedCard() { | ||
return cardDeck.pickOneCard(); | ||
} | ||
|
||
public Card getOpenedCard() { | ||
return cards.get(FIRST_INDEX); | ||
} | ||
|
||
public int getSumOfCards() { | ||
return cards.stream() | ||
.mapToInt(card -> card.getDenomination().getValue()) | ||
.sum(); | ||
} | ||
|
||
@Override | ||
public void addCard(Card card) { | ||
cards.add(card); | ||
if (card.getDenomination().equals(Denomination.ACE) && sum < 10) { | ||
sum += 11; | ||
return; | ||
} | ||
sum += card.getDenomination().getValue(); | ||
} | ||
|
||
public boolean canGetCard() { | ||
return sum <= DEALER_MAXIMUM_SUM; | ||
} | ||
} |
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.
기능 구현 목록 작성 💯