Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
b9c43d8
feat: 기능목록정의 추가
Feb 11, 2022
fcb9e17
feat: Car클래스 구현 및 필드, 메서드 정의
Feb 11, 2022
85d2e1d
feat: 리포트 생성 기능 추가
Feb 11, 2022
e857713
docs: 리드미 업데이트
Feb 11, 2022
442e4b1
style: 코드 정리
Feb 11, 2022
54702d2
feat: tripDistance 필드 상위 클래스에 정의
Feb 11, 2022
bbec84c
docs: update readme.md
hsj-96 Feb 14, 2022
4189d87
feat: getChargeQuantity 메서드 final로 변경
hsj-96 Feb 14, 2022
bba97a1
refactor: getChargeQuantity() 함수명 변경
hsj-96 Feb 14, 2022
55e623f
feat: Participnat 클래스 스켈레톤 생성
hsj-96 Feb 14, 2022
d68dff7
feat: 게임 로직 스켈레톤 구현
hsj-96 Feb 14, 2022
9d2dc0e
feat: 플레이어 리스트 일급 컬렉션 추가
hsj-96 Feb 14, 2022
1d9769b
refactor: drawAll() -> drawCardAll() 메서드 이름 변경
hsj-96 Feb 14, 2022
8edbe2a
feat: 참가자가 가지고 있는 카드들을 관리하는 일급컬렉션 추가
hsj-96 Feb 14, 2022
b6e6ec9
feat: 딜러, 플레이어 객체 생성
hsj-96 Feb 14, 2022
87a763a
update readme.md
hsj-96 Feb 14, 2022
30c85d0
refactor: Cards -> ParticipantCards 이름 변경
hsj-96 Feb 15, 2022
f1613ad
feat: Card, Deck 클래스 생성
hsj-96 Feb 15, 2022
d51daff
feat: deck 클래스 추가
Feb 16, 2022
8ef9212
refactor: 플레이어 deck에서 카드뽑기 구현
hscom96 Feb 16, 2022
e30e64a
feat: Card 클래스 getScore 계산 추가
hscom96 Feb 16, 2022
1cc0378
feat: 카드 총합 구하기 기능 추가
Feb 16, 2022
23a96d6
feat: 승자 구하기에서 max 점수 구하기 기능 추가
hscom96 Feb 16, 2022
bccb7a7
feat: controller 패키지 추가
hsj-96 Feb 17, 2022
f9c848f
feat: enum 을 public 으로 변경
hsj-96 Feb 17, 2022
75d1d93
feat: Deck 팩토리 메서드 생성
hsj-96 Feb 17, 2022
412aa73
feat: 승, 패 점수 계산 메서드 추가
hsj-96 Feb 17, 2022
fd874ab
feat: 카드 반환 메서드 추가, 점수계산 리팩터링
hsj-96 Feb 17, 2022
9458008
feat: 딜러, 참가자 모두를 관리하는 클래스
hsj-96 Feb 17, 2022
44e3e29
feat: GameController 수정
hsj-96 Feb 17, 2022
cb37623
feat: 사용자 입력 클래스 추가
hsj-96 Feb 17, 2022
e6fd564
feat: Test 추가
hsj-96 Feb 17, 2022
4a89e46
feat: 딜러를 제외한 참가자들 관리하는 클래스 추가
hsj-96 Feb 17, 2022
d175ab6
feat: 우승자 관리하는 클래스 추가
hsj-96 Feb 17, 2022
23ebdc6
feat: Card 클래스 수정
hsj-96 Feb 17, 2022
37ccf9c
feat: 게임 참가자 관련 로직 수정 및 추가
hsj-96 Feb 17, 2022
da2f554
feat: InputView 클래스 추가
hsj-96 Feb 17, 2022
a6159fb
feat: PrintView 클래스 추가
hsj-96 Feb 17, 2022
3a04921
feat: 게임 컨트롤러 추가
hsj-96 Feb 17, 2022
3771219
feat: main 함수 추가
hsj-96 Feb 17, 2022
3ed43ac
feat: Test 추가
hsj-96 Feb 17, 2022
63bcf1c
docs: update readme.md
hsj-96 Feb 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
# java-blackjack
# 연료 주입

## 기능 요구사항
우리 회사는 렌터카를 운영하고 있다. 현재 보유하고 있는 차량은 Sonata 2대, Avante 1대, K5 2대로 총 5대의 차량을 보유하고 있다. 고객이 인터넷으로부터 예약할 때 여행할 목적지의 대략적인 이동거리를 입력 받는다. 이 이동거리를 활용해 차량 별로 필요한 연료를 주입한다. 차량 별로 주입해야 할 연료량을 확인할 수 있는 보고서를 생성해야 한다.
각 차량별 연비는 다음과 같다.
* Sonata : 10km/리터
* Avante : 15km/리터
* K5 : 13km/리터

## 기능 목록

- [x] 회사를 생성한다. - RentCompany
- [x] 자동차를 생성한다. - RentCompany.addCar
- 종류 : Sonata, Avante, k5
- 개수 : Sonata 2대, Avante 1대, K5 2대
- [x] 주입해야할 연료량을 계산한다. - Car.getChargeQuantity()
- [x] 차량 별로 주입해야할 보고서를 생성한다. - RentCompany.generateReport()

# 블랙잭

## 기능 요구사항
블랙잭 게임을 변형한 프로그램을 구현한다. 블랙잭 게임은 딜러와 플레이어 중 카드의 합이 21 또는 21에 가장 가까운 숫자를 가지는 쪽이 이기는 게임이다.

카드의 숫자 계산은 카드 숫자를 기본으로 하며, 예외로 Ace는 1 또는 11로 계산할 수 있으며, King, Queen, Jack은 각각 10으로 계산한다.
게임을 시작하면 플레이어는 두 장의 카드를 지급 받으며, 두 장의 카드 숫자를 합쳐 21을 초과하지 않으면서 21에 가깝게 만들면 이긴다. 21을 넘지 않을 경우 원한다면 얼마든지 카드를 계속 뽑을 수 있다.
딜러는 처음에 받은 2장의 합계가 16이하이면 반드시 1장의 카드를 추가로 받아야 하고, 17점 이상이면 추가로 받을 수 없다.
게임을 완료한 후 각 플레이어별로 승패를 출력한다.

- [X] 딜러, 플레이어 (공통) - Participant
- 이름
- 보유 카드
- 승, 패 기록
- 첫번째 2장의 카드를 뽑음
- 이후 1장씩 카드를 뽑음
- 점수를 계산
- 보유하고 있는 카드 정보 반환
- [X] 딜러 - Dealer
- 딜러는 카드 점수가 16 이하이면 카드를 1장 더 받음
- [X] 플레이어 - Player
- 플레이어는 카드를 1장 더 받을지 안받을지 입력을 받아서 카드를 받음

- [X] 카드 - Card
- 종류 : ♦︎다이아, ♥하트, ♠스페이드, ♣클로버
- 숫자 : 1 ~ 9, A (= 1, 11), K (= 10), Q (= 10), J (= 10)
- 에이스 카드일 경우, 점수가 15 미만이면 에이스카드점수는 11점이 되고, 16이상이면 점수가 1점이 됨
- 카드 정보를 반환

- [X] 덱 - Deck
- 카드 52장을 생성하고 관리
- 생성시 미리 덱을 섞어서 반환
- 카드를 여러장 뽑음
- 카드를 한장 뽑음

- [X] 사용자가 가지고 있는 카드 묶음 - ParticipantsCards
- 가지고 있는 카드를 추가
- 카드의 점수를 계산
- 가지고 있는 카드의 정보를 반환

- [X] 게임 - GameController
- 게임 진행

- [X] 입력 - InputView
- 게임 참여자 입력 (쉼표로 구분)
- 카드를 더 받을지 입력 (y/n)
- 빈 입력을 허용하지 않음

- [X] 출력 - PrintView
- 뽑은 카드 결과 출력
- 게임 결과 출력
- 최종 승패 출력
10 changes: 10 additions & 0 deletions src/main/java/blackjack/BlackjackApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package blackjack;

import blackjack.controller.GameController;

public class BlackjackApplication {

public static void main(String[] args) {
GameController.start();
}
}
28 changes: 28 additions & 0 deletions src/main/java/blackjack/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package blackjack.controller;

import blackjack.domain.Dealer;
import blackjack.domain.Participants;
import blackjack.domain.Players;
import blackjack.view.InputView;
import blackjack.view.PrintView;
import java.util.List;

public class GameController {

public static void start() {
List<String> name = InputView.inputNames();

Players players = Players.from(name);
Dealer dealer = new Dealer("딜러");
Participants participants = Participants.from(players, dealer);

participants.drawCardMultiple(2);
PrintView.printFirstDrawCards(participants);

participants.drawCardContinue(1);
PrintView.printGameResult(participants);

participants.judgeScore();
PrintView.printWinLose(participants);
}
}
70 changes: 70 additions & 0 deletions src/main/java/blackjack/domain/Card.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package blackjack.domain;

import java.util.function.IntUnaryOperator;

public class Card {

private final CardType cardType;
private final CardValue cardValue;

Card(CardType cardType, CardValue cardValue) {
this.cardType = cardType;
this.cardValue = cardValue;
}

public int getScore(int currentScore) {
return this.cardValue.intUnaryOperator.applyAsInt(currentScore);
}

public CardValue getCardValue() {
return cardValue;
}

public String toString() {
return cardValue.number + cardType.name;
}

public enum CardType {
SPADE("스페이드"),
CLOVER("클로버"),
HEART("하트"),
DIAMOND("다이아몬드");

private final String name;

CardType(String name) {
this.name = name;
}
}

public enum CardValue {
ACE("A", currentScore -> currentScore < 15 ? 11 : 1),
TWO("2", currentScore -> 2),
THREE("3", currentScore -> 3),
FOUR("4", currentScore -> 4),
FIVE("5", currentScore -> 5),
SIX("6", currentScore -> 6),
SEVEN("7", currentScore -> 7),
EIGHT("8", currentScore -> 8),
NINE("9", currentScore -> 9),
TEN("10", currentScore -> 10),
QUEEN("Q", currentScore -> 10),
JACK("J", currentScore -> 10),
KING("K", currentScore -> 10);

private final String number;
private final IntUnaryOperator intUnaryOperator;

CardValue(String number, IntUnaryOperator intUnaryOperator) {
this.number = number;
this.intUnaryOperator = intUnaryOperator;
}

public boolean isEqualCardValue(Card card) {
return this.equals(card.getCardValue());
}
}
}



27 changes: 27 additions & 0 deletions src/main/java/blackjack/domain/Dealer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package blackjack.domain;

public class Dealer extends Participant {

public Dealer(String name) {
super(name);
}

@Override
public void drawCardMultiple(Deck deck, int number) {
cards.addCards(deck.drawMultiple(number));
}

@Override
public void drawCardContinue(Deck deck) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void drawCardContinue(Deck deck) {
public boolean drawCardContinue(Deck deck) {

이렇게 수정해 컨트롤러에 메시지를 던지는 방식으로 수정해보는 것은 어떨까요?
그리고 21점이 넘으면 카드를 받을 수 없는 조건이 빠져있습니다.

if (cards.sumCardScore() > 17) {
return;
}
System.out.println("딜러는 16이하라 한장의 카드를 더 받았습니다.");
Copy link

@joswlv joswlv Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앞선 미션에서 학습한 MVC패턴을 지켜보는 것은 어떨까요?

cards.addCards(deck.drawMultiple(1));
}

@Override
public String scoreResult() {
return name + " : " + winScore + "승" + loseScore + "패";
}
}
48 changes: 48 additions & 0 deletions src/main/java/blackjack/domain/Deck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package blackjack.domain;

import blackjack.domain.Card.CardType;
import blackjack.domain.Card.CardValue;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

public class Deck {

private final Deque<Card> deck = new ArrayDeque<>();

public Deck(List<CardType> cardTypes, List<CardValue> cardValues) {
for (CardType cardType : cardTypes) {
for (CardValue cardValue : cardValues) {
deck.add(new Card(cardType, cardValue));
}
}
}

public static Deck create() {
List<CardType> types = Arrays.asList(CardType.values());
Collections.shuffle(types);

List<CardValue> values = Arrays.asList(CardValue.values());
Collections.shuffle(values);

return new Deck(types, values);
}

public List<Card> drawMultiple(int num) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매번 카드를 생성해서 리턴하는 것보다 덱에서 카드를 뽑아 전달하는 방식으로 수정해 보는 것은 어떨까요?

실제 블랙잭 게임에서 52장의 Card를 덱으로 만들어 딜러와 플레이어에게 전달하는 방식으로 게임을 운영하는 것을 생각해 보면 구현에 도움이 될 것 같습니다.

List<Card> cards = new ArrayList<>();
for (int i = 0; i < num; i++) {
cards.add(draw());
}
return cards;
}

public Card draw() {
if (deck.size() == 0) {
throw new IllegalStateException("패가 더이상 존재하지 않습니다.");
}
return deck.pop();
}
}
49 changes: 49 additions & 0 deletions src/main/java/blackjack/domain/Participant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package blackjack.domain;

import java.util.ArrayList;

abstract public class Participant {

private static final String COLON = " : ";

protected final String name;
protected final ParticipantCards cards;
protected int winScore, loseScore;

public Participant(String name, ParticipantCards cards, int winCount, int loseCount) {
this.name = name;
this.cards = cards;
this.winScore = winCount;
this.loseScore = loseCount;
}

public Participant(String name) {
this(name, new ParticipantCards(new ArrayList<>()), 0, 0);
}

public final void judgeScore(int maxScore) {
if (sumCardScore() == maxScore) {
winScore++;
return;
}
loseScore++;
}

public final String getName() {
return name;
}

public final String holdingInfo() {
return name + COLON + cards.toString();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앞서 학습한 MVC패턴을 적용해보면 어떨까요?

}

public final int sumCardScore() {
return cards.sumCardScore();
}

public abstract void drawCardMultiple(Deck deck, int number);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public abstract void drawCardMultiple(Deck deck, int number);
public abstract void drawCardMultiple(Deck deck);

메서드 이름에서 행위가 표현됩니다. 굳이 카드 숫자를 받아 처리할 필요가 없을 것 같습니다.


public abstract void drawCardContinue(Deck deck);

public abstract String scoreResult();
}
42 changes: 42 additions & 0 deletions src/main/java/blackjack/domain/ParticipantCards.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package blackjack.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ParticipantCards {
Copy link

@joswlv joswlv Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

참가자의 카드 점수에 따른 상태값을 가지도록 수정해보는 것은 어떨까요?


private static final String COMMA = ", ";

private final List<Card> cards;

public ParticipantCards(List<Card> cards) {
this.cards = new ArrayList<>(cards);
}

public void addCards(List<Card> drawCards) {
cards.addAll(drawCards);
}

public int sumCardScore() {
return cards.stream()
.reduce(0, (x, y) -> x + y.getScore(x), Integer::sum);
}

public List<Card> getCards() {
return Collections.unmodifiableList(cards);
}

public String toString() {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < cards.size(); i++) {
sb.append(cards.get(i).toString());
if (i != cards.size() - 1) {
sb.append(COMMA);
}
}

return sb.toString();
}
}
Loading