Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1258521
[1단계 - 블랙잭 게임 실행] 박스터 (한우석) 미션 제출합니다. (#407)
drunkenhw Mar 10, 2023
3e207f0
refactor: 패키지 이동
drunkenhw Mar 10, 2023
7c771a0
refactor: 메소드명 변경
drunkenhw Mar 10, 2023
3e398eb
refactor: score 계산하는 로직 수정
drunkenhw Mar 10, 2023
47c9b2e
feat: 무승부를 만드는 기능 추가
drunkenhw Mar 10, 2023
15a6617
refactor: 챰가자들의 bust 유무 메소드 추가
drunkenhw Mar 10, 2023
6247163
refactor: 무승부를 구하는 로직 수정
drunkenhw Mar 11, 2023
0c8b90e
refactor: 패키지명 변경
drunkenhw Mar 11, 2023
5c4dcab
feat: 첫번째 카드 오픈 기능 추가
drunkenhw Mar 11, 2023
52ef885
feat: Score 값 객체 추가
drunkenhw Mar 11, 2023
a1491d3
docs: 그래프 추가
drunkenhw Mar 11, 2023
5d78e14
refactor: 메소드 위치 수정
drunkenhw Mar 11, 2023
ee3b48b
refactor: 결과를 반환하는 로직 위치 이동
drunkenhw Mar 11, 2023
d923b0b
refactor: 변수명 변경
drunkenhw Mar 11, 2023
d4d08a8
docs: 기능 목록 추가
drunkenhw Mar 12, 2023
380de2e
feat: BettingMoney 생성
drunkenhw Mar 12, 2023
ebf4816
feat: BettingMoney 검증 추가
drunkenhw Mar 12, 2023
4d62340
feat: BettingTable 생성
drunkenhw Mar 12, 2023
ae75ab7
feat: 베팅 금액을 입력 받는 기능 추가
drunkenhw Mar 12, 2023
039cfc5
feat: 블랙잭 여부를 판단하는 기능 추가
drunkenhw Mar 12, 2023
6243221
feat: 수익률을 계산하는 기능 추가
drunkenhw Mar 12, 2023
c69b6b3
feat: 딜러의 수익금을 계산하는 기능 추가
drunkenhw Mar 12, 2023
e1e25c3
feat: 결과를 출력하는 기능 추가
drunkenhw Mar 12, 2023
7b053be
refactor: 오버라이딩 어노테이션 추가
drunkenhw Mar 12, 2023
e3f91e3
refactor: Money 상속 제거
drunkenhw Mar 13, 2023
5629a10
refactor: 사용하지 않는 메소드 제거
drunkenhw Mar 13, 2023
2224ee6
refactor: bettingTable을 불변으로 변경
drunkenhw Mar 13, 2023
a277ee8
refactor: score의 검증 추가
drunkenhw Mar 13, 2023
54c89db
refactor: bettingTable을 불변으로 변경
drunkenhw Mar 13, 2023
c6e78a8
refactor: bettingTable for loop로 변경
drunkenhw Mar 13, 2023
c02da94
refactor: 컬렉션 복사 방법 변경
drunkenhw Mar 13, 2023
ec5e66a
feat: 예외 처리 추가
drunkenhw Mar 13, 2023
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
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,69 @@
## 우아한테크코스 코드리뷰

- [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md)

---

```mermaid
graph TD
BlackjackController --> OutputView
BlackjackController --> InputView
BlackjackController --> BlackjackGame

BlackjackGame --> BettingTable
BlackjackGame --> Participants
BlackjackGame --> CardDeck

BettingTable --> BettingMoney
BettingTable --> Player

BettingMoney <-.-> Money

Participants --> Player
Participants --> Dealer

Player <-.-> Participant
Dealer <-.-> Participant

Participant --> Name
Participant --> PlayerCards

PlayerCards --> Card
CardDeck --> Card

Card --> CardSuit
Card --> CardNumber

```

### 기능 목록

## 1단계

- [x] 플레이어 이름을 입력 받는다
- [x] 이름은 공백 혹은 빈값만 들어올 수 없다
- [x] 중복되지 않는 카드 52개 생성한다
- [x] 딜러와 플레이어에게 카드를 나눠준다
- [x] 카드 한 장을 나눠준다
- [x] 처음에 딜러는 카드를 한 장만 공개한다
- [x] 딜러와 플레이어에게 나눠준 카드를 출력한다
- [x] 플레이어는 원한다면 카드를 계속 받을 수 있음
- [x] 딜러는 조건에 따라 카드를 받을 수 있음
- [x] 카드의 합을 통한 승패 판단
- [x] 카드의 합을 계산한다
- [x] 카드의 합이 21이하 중 가장 큰 값이 승리
- [x] 딜러는 16 이하라면 무조건 받음
- [x] 각 플레이어와 딜러의 카드 결과 출력
- [x] 각 플레이어와 딜러의 승패 결과 출력

## 2단계

- [x] 베팅 금액을 입력 받는 기능
- [x] 베팅 금액은 1000원 단위로 가능하다
- [x] 베팅 금액은 최대 10,000,000원까지 가능하다
- [x] 수익률을 계산하는 기능
- 플레이어가 블랙잭으로 이기면 1.5배를 받는다
- 블랙잭은 카드가 2장일 때 점수가 21이다
- 플레이어가 이기면 1배를 받는다
- 플레이어가 지면 -1배를 받는다
- [x] 수익률을 출력하는 기능
12 changes: 12 additions & 0 deletions src/main/java/blackjack/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package blackjack;

import blackjack.controller.BlackjackController;
import blackjack.view.InputView;
import blackjack.view.OutputView;

public class Application {
public static void main(String[] args) {
BlackjackController blackjackController = new BlackjackController(new InputView(), new OutputView());
blackjackController.run();
}
}
14 changes: 14 additions & 0 deletions src/main/java/blackjack/constants/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package blackjack.constants;

public enum ErrorCode {
DUPLICATE_NAME,
RESERVED_NAME,
NOT_EXIST_MESSAGE,
BLANK_NAME,
EMPTY_CARD,
INVALID_COMMAND,
INVALID_MONEY_UNIT,
INVALID_MONEY_BOUND,
INVALID_SCORE,
;
}
125 changes: 125 additions & 0 deletions src/main/java/blackjack/controller/BlackjackController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package blackjack.controller;

import blackjack.controller.exception.InvalidCommandException;
import blackjack.domain.card.exception.NoMoreCardException;
import blackjack.domain.exception.CustomException;
import blackjack.domain.game.BettingMoney;
import blackjack.domain.game.BettingResult;
import blackjack.domain.game.BlackjackGame;
import blackjack.domain.game.exception.InvalidMoneyValueException;
import blackjack.domain.user.Dealer;
import blackjack.domain.user.Participants;
import blackjack.domain.user.Player;
import blackjack.view.InputView;
import blackjack.view.OutputView;
import java.util.List;

public class BlackjackController {
private final InputView inputView;
private final OutputView outputView;

public BlackjackController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}

public void run() {
BlackjackGame blackjackGame = createBlackjackGame();
start(blackjackGame);
printResult(blackjackGame);
Comment on lines +27 to +29

Choose a reason for hiding this comment

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

너무 간결해서 좋은데요?

}

private BlackjackGame createBlackjackGame() {
Participants participants = createParticipants();
return new BlackjackGame(participants);
}
Comment on lines +32 to +35

Choose a reason for hiding this comment

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

BlackjackGame(List<String> playerNames) 같은 형태는 어떤가요?
바깥에서 생성할 이유가 없어보여요

Copy link
Member Author

Choose a reason for hiding this comment

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

맞는 말 입니다


private void start(BlackjackGame blackjackGame) {
try {
Participants participants = blackjackGame.getParticipants();
bet(participants.getPlayers(), blackjackGame);
dealOutInitCards(blackjackGame, participants);
play(participants, blackjackGame);
outputView.printCardResult(participants);
} catch (NoMoreCardException e) {
outputView.printError(e.getErrorCode());
}
}

private void bet(List<Player> players, BlackjackGame blackjackGame) {
for (Player player : players) {
BettingMoney money = createBettingMoney(player);
blackjackGame.bet(player, money);
}
}

private BettingMoney createBettingMoney(Player player) {
try {
int money = inputView.readBettingMoney(player);
return new BettingMoney(money);
} catch (InvalidMoneyValueException e) {
outputView.printError(e.getErrorCode());
return createBettingMoney(player);
}
}

private void dealOutInitCards(BlackjackGame blackjackGame, Participants participants) {
blackjackGame.dealOutCard();
outputView.printInitCards(participants);
}

private void printResult(BlackjackGame blackjackGame) {
BettingResult result = blackjackGame.getBettingResult();
outputView.printBettingResult(result);
}

private Participants createParticipants() {
try {
List<String> names = inputView.readNames();
return Participants.from(names);
} catch (CustomException e) {
outputView.printError(e.getErrorCode());
return createParticipants();
}
}
Comment on lines +76 to +84

Choose a reason for hiding this comment

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

재귀보다는 while 어떤가요?

Copy link
Member Author

Choose a reason for hiding this comment

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

그거 쓸 줄 몰라요.. 알려주세요


private void play(Participants participants, BlackjackGame blackjackGame) {
for (Player player : participants.getPlayers()) {
playPerPlayer(player, blackjackGame);
}
Dealer dealer = participants.getDealer();
playDealer(dealer, blackjackGame);
}

private void playPerPlayer(Player player, BlackjackGame blackjackGame) {
GameCommand command = GameCommand.PLAY;
while (player.isDrawable() && command.isPlay()) {
command = getGameCommand(player);
giveCard(player, blackjackGame, command);
outputView.printPlayerCards(player);
}
}

private GameCommand getGameCommand(Player player) {
try {
String inputCommand = inputView.readIsContinue(player.getName());
return GameCommand.from(inputCommand);
} catch (InvalidCommandException e) {
outputView.printError(e.getErrorCode());
return getGameCommand(player);
}
}

private void giveCard(Player player, BlackjackGame blackjackGame, GameCommand command) {
if (command.isPlay()) {
blackjackGame.drawCard(player);
}
}

private void playDealer(Dealer dealer, BlackjackGame blackjackGame) {
while (dealer.isDrawable()) {
outputView.printDealerState();
blackjackGame.drawCard(dealer);
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/blackjack/controller/GameCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package blackjack.controller;

import blackjack.constants.ErrorCode;
import blackjack.controller.exception.InvalidCommandException;

public enum GameCommand {
PLAY("y"),
STOP("n");

private final String command;

GameCommand(String command) {
this.command = command;
}

public static GameCommand from(String command) {
if (PLAY.command.equalsIgnoreCase(command)) {
return PLAY;
}
if (STOP.command.equalsIgnoreCase(command)) {

Choose a reason for hiding this comment

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

equalsIgnoreCase 좋은데요?

return STOP;
}
throw new InvalidCommandException(ErrorCode.INVALID_COMMAND);
}

public boolean isPlay() {
return this == PLAY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package blackjack.controller.exception;

import blackjack.constants.ErrorCode;
import blackjack.domain.exception.CustomException;

public class InvalidCommandException extends CustomException {

public InvalidCommandException(ErrorCode errorCode) {
super(errorCode);
}
}
67 changes: 67 additions & 0 deletions src/main/java/blackjack/domain/card/Card.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package blackjack.domain.card;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Card {

private static final List<Card> CACHE = new ArrayList<>();

static {
for (CardSuit suit : CardSuit.values()) {
generateCard(suit);
}
}

private final CardSuit cardSuit;
private final CardNumber cardNumber;

public Card(CardSuit cardSuit, CardNumber cardNumber) {
this.cardSuit = cardSuit;
this.cardNumber = cardNumber;
}
Comment on lines +20 to +23

Choose a reason for hiding this comment

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

new 로 생성할 수 있는데, 캐싱을 하는 의미가 없어보여요

Copy link
Member Author

Choose a reason for hiding this comment

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

죄송합니다

Copy link
Collaborator

Choose a reason for hiding this comment

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

ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
왜 의미가 없죠?

Copy link
Collaborator

Choose a reason for hiding this comment

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

생성자 막아놓고 정적 팩터리로 이미 만들어놓은거 반환해주면 될라나요?

Choose a reason for hiding this comment

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

그죠 그러면 되죠

Choose a reason for hiding this comment

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

캐싱 좋은데요?


private static void generateCard(CardSuit suit) {
for (CardNumber number : CardNumber.values()) {
CACHE.add(new Card(suit, number));
}
}

public static List<Card> values() {
return List.copyOf(CACHE);
}

public boolean isAce() {
return this.cardNumber.isAce();

Choose a reason for hiding this comment

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

this를 붙이는 기준이 있을까요?
아래에는 없네요?

Choose a reason for hiding this comment

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

페어탓 해명하세요

}

public int getValue() {
return cardNumber.getValue();
}

public CardSuit getSuit() {
return cardSuit;
}

public CardNumber getNumber() {
return cardNumber;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Card card = (Card) o;
return cardSuit == card.cardSuit && cardNumber == card.cardNumber;
}

@Override
public int hashCode() {
return Objects.hash(cardSuit, cardNumber);
}
}
35 changes: 35 additions & 0 deletions src/main/java/blackjack/domain/card/CardDeck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package blackjack.domain.card;

import blackjack.constants.ErrorCode;
import blackjack.domain.card.exception.NoMoreCardException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CardDeck {
private final List<Card> cards;
Comment on lines +9 to +10

Choose a reason for hiding this comment

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

전체적으로 한번 통일을 주면 좋을 것 같습니다~

클래스 선언 다음에 개행을 준다. vs 안준다

Suggested change
public class CardDeck {
private final List<Card> cards;
public class CardDeck {
private final List<Card> cards;


public CardDeck() {
this.cards = new ArrayList<>(Card.values());
shuffleCards();
}

public CardDeck(List<Card> cards) {
this.cards = cards;
}

private void shuffleCards() {
Collections.shuffle(this.cards);
}

public Card pick() {
if (cards.isEmpty()) {
throw new NoMoreCardException(ErrorCode.EMPTY_CARD);
}
return cards.remove(0);

Choose a reason for hiding this comment

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

remove(0)을 수행하면 내부적으로 배열을 앞으로 당기는 행위가 일어납니다.
remove(cards.size() -1)은 어떨까요?

}

public List<Card> getCards() {
return List.copyOf(cards);
}
}
Loading