From 3bd2a677a285f2e5d903d2498c35394dba11cca6 Mon Sep 17 00:00:00 2001 From: DD <48818566+fucct@users.noreply.github.com> Date: Mon, 23 Mar 2020 21:05:12 +0900 Subject: [PATCH] =?UTF-8?q?[=EB=94=94=EB=94=94]=20=EB=B8=94=EB=9E=99?= =?UTF-8?q?=EC=9E=AD=202=EB=8B=A8=EA=B3=84=20=EB=AF=B8=EC=85=98=20?= =?UTF-8?q?=EC=A0=9C=EC=B6=9C=ED=95=A9=EB=8B=88=EB=8B=A4.=20(#66)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: GameResult 설계 개선 * refactor: 전체적인 변수명과 기타 수정 * refactor: Result enum 클래스 변경 * refactor: User 테스트 방식 수정 및 테스트 용 생성자 구현 * refactor: Result 테스트 구현 * feat: Bet관련 테스트와 클래스 구현 * feat: Bet 입력 구현 * feat: Bet 입력 구현2 * refactor: GameResult 설계 수정 * feat: 수익 계산 구현 * refactor: user의 draw 구현 * refactor: User 생성 방식 변경 * refactor: 컨벤션에 맞게 리팩토링 * refactor: 컨벤션에 맞게 리팩토링 * refactor: bet 생성시 검증 추가 * refactor: 메소드 네이밍 컨벤션에 맞게 refactor * refactor: Result 람다식으로 수정, Bet 생성자 삭제 * refactor: 안쓰는 메서드 삭제, 컨벤션 리팩터 --- src/main/java/controller/BlackJackGame.java | 37 ++++--- .../java/exception/BetFormatException.java | 7 ++ .../java/exception/BetRangeException.java | 7 ++ .../java/exception/IllegalDrawException.java | 7 ++ src/main/java/model/Answer.java | 29 +++--- src/main/java/model/Bet.java | 55 +++++++++++ src/main/java/model/Card.java | 26 ++--- src/main/java/model/CardFactory.java | 18 ++-- src/main/java/model/CardHand.java | 34 +++++-- src/main/java/model/Dealer.java | 19 ++-- src/main/java/model/Deck.java | 32 ++---- src/main/java/model/GameResult.java | 32 ++---- src/main/java/model/Player.java | 25 ++++- src/main/java/model/PlayerNames.java | 6 +- src/main/java/model/Players.java | 12 ++- src/main/java/model/PlayersData.java | 21 ++++ src/main/java/model/Profit.java | 28 ++++++ src/main/java/model/Result.java | 81 +++++++--------- src/main/java/model/User.java | 54 ++++++++--- src/main/java/utils/StringUtils.java | 5 + src/main/java/view/InputView.java | 7 +- src/main/java/view/OutputView.java | 37 ++++--- src/test/java/model/AnswerTest.java | 5 +- src/test/java/model/BetTest.java | 27 ++++++ src/test/java/model/CardHandTest.java | 2 +- src/test/java/model/DeckTest.java | 13 ++- src/test/java/model/GameResultTest.java | 59 ++++++----- src/test/java/model/PlayerNamesTest.java | 16 --- src/test/java/model/ResultTest.java | 97 ++++++++++++------- src/test/java/model/StringUtilTest.java | 30 ++++++ src/test/java/model/UserTest.java | 80 +++++++++------ 31 files changed, 595 insertions(+), 313 deletions(-) create mode 100644 src/main/java/exception/BetFormatException.java create mode 100644 src/main/java/exception/BetRangeException.java create mode 100644 src/main/java/exception/IllegalDrawException.java create mode 100644 src/main/java/model/Bet.java create mode 100644 src/main/java/model/PlayersData.java create mode 100644 src/main/java/model/Profit.java create mode 100644 src/test/java/model/BetTest.java create mode 100644 src/test/java/model/StringUtilTest.java diff --git a/src/main/java/controller/BlackJackGame.java b/src/main/java/controller/BlackJackGame.java index cf470c2935..d8d800a762 100644 --- a/src/main/java/controller/BlackJackGame.java +++ b/src/main/java/controller/BlackJackGame.java @@ -4,27 +4,36 @@ import view.InputView; import view.OutputView; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + public class BlackJackGame { + public static final String COMMA = ","; public static final int ADDITIONAL_DRAW_COUNT = 1; public static final int INITIAL_DRAW_COUNT = 2; - public static final int HIT_BOUNDARY = 16; + public static final int DEALER_HIT_BOUNDARY = 16; public static final int BLACK_JACK_COUNT = 21; public static void play() { Deck deck = new Deck(CardFactory.createCardList()); PlayerNames playerNames = new PlayerNames(InputView.inputPlayerNames()); - Players players = new Players(playerNames, deck, INITIAL_DRAW_COUNT); - Dealer dealer = new Dealer(deck, INITIAL_DRAW_COUNT); - - OutputView.printInitialCards(players, dealer); - OutputView.printUsersCard(players, dealer); + PlayersData playersData = new PlayersData(makePlayersData(playerNames)); + Players players = new Players(playersData, deck); + Dealer dealer = new Dealer(deck); + OutputView.printInitialState(players, dealer); drawCardToPlayers(players, deck); hitOrStayForDealer(dealer, deck); - OutputView.printFinalCardHandResult(players, dealer); - GameResult gameResult = new GameResult(players, dealer); - gameResult.calculateResults(); - OutputView.printResult(gameResult); + OutputView.printResult(gameResult, players, dealer); + } + + private static Map makePlayersData(PlayerNames playerNames) { + Map playerData = new LinkedHashMap<>(); + for (String name : playerNames) { + playerData.put(name, new Bet(InputView.inputBet(name))); + } + return Collections.unmodifiableMap(playerData); } private static void drawCardToPlayers(final Players players, final Deck deck) { @@ -34,12 +43,12 @@ private static void drawCardToPlayers(final Players players, final Deck deck) { } private static void drawCardEachPlayer(Deck deck, Player player) { - while (!player.isBust()) { + while (player.isHitBound()) { Answer answer = Answer.getYesOrNoByValue(InputView.inputYesOrNo(player)); - if (!answer.getTrueOrFalse()) { + if (!answer.isYes()) { break; } - player.drawCard(deck, ADDITIONAL_DRAW_COUNT); + player.additionalDraw(deck); OutputView.printPlayerCard(player); } } @@ -47,7 +56,7 @@ private static void drawCardEachPlayer(Deck deck, Player player) { private static void hitOrStayForDealer(Dealer dealer, Deck deck) { if (dealer.isHitBound()) { OutputView.printDealerDraw(dealer); - dealer.drawCard(deck, ADDITIONAL_DRAW_COUNT); + dealer.additionalDraw(deck); } } } diff --git a/src/main/java/exception/BetFormatException.java b/src/main/java/exception/BetFormatException.java new file mode 100644 index 0000000000..bd9db9f423 --- /dev/null +++ b/src/main/java/exception/BetFormatException.java @@ -0,0 +1,7 @@ +package exception; + +public class BetFormatException extends RuntimeException { + public BetFormatException(String message) { + super(message); + } +} diff --git a/src/main/java/exception/BetRangeException.java b/src/main/java/exception/BetRangeException.java new file mode 100644 index 0000000000..a104916cd9 --- /dev/null +++ b/src/main/java/exception/BetRangeException.java @@ -0,0 +1,7 @@ +package exception; + +public class BetRangeException extends RuntimeException { + public BetRangeException(String message) { + super(message); + } +} diff --git a/src/main/java/exception/IllegalDrawException.java b/src/main/java/exception/IllegalDrawException.java new file mode 100644 index 0000000000..8da039f6aa --- /dev/null +++ b/src/main/java/exception/IllegalDrawException.java @@ -0,0 +1,7 @@ +package exception; + +public class IllegalDrawException extends RuntimeException { + public IllegalDrawException(String message) { + super(message); + } +} diff --git a/src/main/java/model/Answer.java b/src/main/java/model/Answer.java index dc908b1c35..72dafd3d8d 100644 --- a/src/main/java/model/Answer.java +++ b/src/main/java/model/Answer.java @@ -6,36 +6,29 @@ import java.util.Arrays; public enum Answer { - YES("y", true), - NO("n", false); + YES("y"), + NO("n"); - private String yesOrNo; - private boolean trueOrFalse; + private final String answer; - Answer(String yesOrNo, boolean trueOrFalse) { - this.yesOrNo = yesOrNo; - this.trueOrFalse = trueOrFalse; + Answer(String answer) { + this.answer = answer; } public static Answer getYesOrNoByValue(String input) { - validate(input); + StringUtils.validateString(input); return Arrays.stream(Answer.values()) - .filter(answer -> answer.isSameYesOrNo(input)) + .filter(answer -> answer.isSameAnswer(input)) .findAny() .orElseThrow(() -> new IllegalYesOrNoInputException(String.format("%s는 올바르지 않은 값입니다.", input))); } - private boolean isSameYesOrNo(final String input) { - return yesOrNo.equalsIgnoreCase(input); + private boolean isSameAnswer(final String input) { + return answer.equalsIgnoreCase(input); } - private static void validate(String input) { - StringUtils.validateNull(input); - StringUtils.validateEmpty(input); - } - - public boolean getTrueOrFalse() { - return trueOrFalse; + public boolean isYes() { + return this == YES; } } diff --git a/src/main/java/model/Bet.java b/src/main/java/model/Bet.java new file mode 100644 index 0000000000..aac9e23b6d --- /dev/null +++ b/src/main/java/model/Bet.java @@ -0,0 +1,55 @@ +package model; + +import exception.BetFormatException; +import exception.BetRangeException; +import utils.StringUtils; + +import java.util.Objects; + +public class Bet { + public static final double LOWER_BET_BOUND = 100; + + private final double bet; + + public Bet(String input) { + validate(input); + this.bet = Double.parseDouble(input); + } + + private void validate(String input) { + StringUtils.validateString(input); + validateFormat(input); + validateRange(input); + } + + private void validateFormat(String input) { + try { + Double.parseDouble(input); + } catch (NumberFormatException e) { + throw new BetFormatException("베팅금액은 실수만 입력 가능합니다."); + } + } + + private void validateRange(String input) { + if (Double.parseDouble(input) < LOWER_BET_BOUND) { + throw new BetRangeException("베팅금액은 100원 이상부터 입력 가능합니다."); + } + } + + public double multiplyBet(double ratio) { + return bet * ratio; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Bet bet1 = (Bet) o; + return Double.compare(bet1.bet, bet) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(bet); + } +} diff --git a/src/main/java/model/Card.java b/src/main/java/model/Card.java index 0e67e6478e..d951e35b06 100644 --- a/src/main/java/model/Card.java +++ b/src/main/java/model/Card.java @@ -2,7 +2,7 @@ import java.util.Objects; -public class Card { +public final class Card { private final Symbol symbol; private final Type type; @@ -15,13 +15,13 @@ public boolean isAce() { return symbol == Symbol.ACE; } + public int getScore() { + return symbol.getScore(); + } + @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Card card = (Card) o; - return symbol == card.symbol && - type == card.type; + public String toString() { + return symbol.toString() + type.toString(); } @Override @@ -30,11 +30,11 @@ public int hashCode() { } @Override - public String toString() { - return symbol.toString() + type.toString(); - } - - public int calculateScore() { - return symbol.getScore(); + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Card card = (Card) o; + return symbol == card.symbol && + type == card.type; } } diff --git a/src/main/java/model/CardFactory.java b/src/main/java/model/CardFactory.java index 5443996190..1c32d63e22 100644 --- a/src/main/java/model/CardFactory.java +++ b/src/main/java/model/CardFactory.java @@ -1,20 +1,22 @@ package model; import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class CardFactory { public static List createCardList() { - List cards = new ArrayList<>(); - for (Symbol symbol : Symbol.values()) { - createByType(cards, symbol); - } + List cards = Arrays + .stream(Type.values()) + .flatMap(CardFactory::mapToCard) + .collect(Collectors.toList()); return Collections.unmodifiableList(cards); } - private static void createByType(List cards, Symbol symbol) { - for (Type type : Type.values()) { - cards.add(new Card(symbol, type)); - } + private static Stream mapToCard(Type type) { + return Arrays + .stream(Symbol.values()) + .map(symbol -> new Card(symbol, type)); } } diff --git a/src/main/java/model/CardHand.java b/src/main/java/model/CardHand.java index 8e0631ef4a..d15a36d986 100644 --- a/src/main/java/model/CardHand.java +++ b/src/main/java/model/CardHand.java @@ -5,26 +5,32 @@ import java.util.Iterator; import java.util.List; +import static controller.BlackJackGame.BLACK_JACK_COUNT; +import static controller.BlackJackGame.INITIAL_DRAW_COUNT; + public class CardHand implements Iterable { - public static final int ADDITIONAL_ACE_SCORE = 10; - public static final int BLACK_JACK_SCORE = 21; + private static final int ADDITIONAL_ACE_SCORE = 10; - private List cards = new ArrayList<>(); + private final List cards = new ArrayList<>(); public void addCard(Card card) { cards.add(card); } - public int calculateScore() { - if (isAce()) { + public int getScore() { + if (hasAce()) { return calculateScoreWithAce(); } return calculateScoreWithNoAce(); } + public boolean isEmpty() { + return cards.size() == 0; + } + public int calculateScoreWithAce() { int score = calculateScoreWithNoAce(); - if (score + ADDITIONAL_ACE_SCORE > BLACK_JACK_SCORE) { + if (score + ADDITIONAL_ACE_SCORE > BLACK_JACK_COUNT) { return score; } return score + ADDITIONAL_ACE_SCORE; @@ -32,14 +38,26 @@ public int calculateScoreWithAce() { public int calculateScoreWithNoAce() { return cards.stream() - .mapToInt(Card::calculateScore) + .mapToInt(Card::getScore) .sum(); } - public boolean isAce() { + public boolean hasAce() { return cards.stream().anyMatch(Card::isAce); } + public boolean isBust() { + return getScore() > BLACK_JACK_COUNT; + } + + public boolean isBlackJack() { + return (getScore() == BLACK_JACK_COUNT) && (cards.size() == INITIAL_DRAW_COUNT); + } + + public boolean isLowerThanBlackJack() { + return getScore() < BLACK_JACK_COUNT; + } + public List getCards() { return Collections.unmodifiableList(cards); } diff --git a/src/main/java/model/Dealer.java b/src/main/java/model/Dealer.java index 21f15b997f..7745176a07 100644 --- a/src/main/java/model/Dealer.java +++ b/src/main/java/model/Dealer.java @@ -1,20 +1,27 @@ package model; -import static controller.BlackJackGame.HIT_BOUNDARY; +import java.util.List; + +import static controller.BlackJackGame.DEALER_HIT_BOUNDARY; public class Dealer extends User { - public static final int ZERO = 0; + private static final int FIRST = 0; public static final String DEALER_NAME = "딜러"; - public Dealer(Deck deck, int initialDrawCount) { - super(DEALER_NAME, deck, initialDrawCount); + public Dealer(Deck deck) { + super(DEALER_NAME, deck); + } + + public Dealer(List cards) { + super(DEALER_NAME, cards); } public String toStringCardHandFirst() { - return cardHand.getCards().get(ZERO).toString(); + return cardHand.getCards().get(FIRST).toString(); } + @Override public boolean isHitBound() { - return getScore() <= HIT_BOUNDARY; + return getScore() <= DEALER_HIT_BOUNDARY; } } diff --git a/src/main/java/model/Deck.java b/src/main/java/model/Deck.java index fa7dcfe01f..4f139a5837 100644 --- a/src/main/java/model/Deck.java +++ b/src/main/java/model/Deck.java @@ -6,40 +6,28 @@ import java.util.List; import java.util.Stack; +import static controller.BlackJackGame.ADDITIONAL_DRAW_COUNT; + public class Deck { - private Stack cards = new Stack<>(); + private final Stack cards = new Stack<>(); public Deck(List cards) { this.cards.addAll(cards); - shuffle(); - } - - public Deck(CardHand cardHand) { - this.cards.addAll(cardHand.getCards()); + Collections.shuffle(this.cards); } - private void shuffle() { - Collections.shuffle(cards); - } - - public CardHand draw(int count) { - validateSize(count); - CardHand cardHand = new CardHand(); - for (int i = 0; i < count; i++) { - Card card = cards.pop(); - cardHand.addCard(card); - } - return cardHand; + public Card draw() { + validateSize(); + return cards.pop(); } public int getCurrentDeckSize() { return cards.size(); } - private void validateSize(int count) { - if (getCurrentDeckSize() - count < 0) { - throw new EmptyDeckException(count + "장 이상 draw 할 카드가 존재하지 않습니다."); + private void validateSize() { + if (getCurrentDeckSize() - ADDITIONAL_DRAW_COUNT < 0) { + throw new EmptyDeckException("Deck에 카드가 존재하지 않습니다."); } } - } diff --git a/src/main/java/model/GameResult.java b/src/main/java/model/GameResult.java index 1ed28b1a0a..b69a8f1bbc 100644 --- a/src/main/java/model/GameResult.java +++ b/src/main/java/model/GameResult.java @@ -5,37 +5,23 @@ import java.util.Map; public class GameResult { - public static final int DEFAULT_RESULT = 0; - - private Players players; - private Dealer dealer; - private Map playerResult = new LinkedHashMap<>(); - private Map dealerResult = new LinkedHashMap<>(); + private Map playerResult = new LinkedHashMap<>(); public GameResult(final Players players, final Dealer dealer) { - this.players = players; - this.dealer = dealer; - for (Result result : Result.values()) { - dealerResult.put(result, DEFAULT_RESULT); - } - } - - public void calculateResults() { for (Player player : players) { Result result = Result.compete(dealer, player); - Result oppositeResult = Result.oppositeResult(result); - int count = dealerResult.get(oppositeResult); - - playerResult.put(player, result); - dealerResult.put(oppositeResult, count + 1); + playerResult.put(player, result.calculateProfit(player)); } } - public Map getPlayerResult() { - return Collections.unmodifiableMap(playerResult); + public Profit getDealerResult() { + return new Profit(playerResult.values() + .stream() + .mapToDouble(Profit::getProfit) + .sum() * -1); } - public Map getDealerResult() { - return Collections.unmodifiableMap(dealerResult); + public Map getPlayerResult() { + return Collections.unmodifiableMap(playerResult); } } diff --git a/src/main/java/model/Player.java b/src/main/java/model/Player.java index 409bba2342..67e79ecfb1 100644 --- a/src/main/java/model/Player.java +++ b/src/main/java/model/Player.java @@ -1,7 +1,28 @@ package model; +import java.util.List; + +import static model.Bet.LOWER_BET_BOUND; + public class Player extends User { - public Player(String name, Deck deck, int initialDrawCount) { - super(name, deck, initialDrawCount); + private final Bet bet; + + public Player(String name, Bet bet, Deck deck) { + super(name, deck); + this.bet = bet; + } + + public Player(String name, List cards) { + super(name, cards); + this.bet = new Bet(Double.toString(LOWER_BET_BOUND)); + } + + public double getMultiplyBet(double ratio) { + return bet.multiplyBet(ratio); + } + + @Override + public boolean isHitBound() { + return cardHand.isLowerThanBlackJack(); } } diff --git a/src/main/java/model/PlayerNames.java b/src/main/java/model/PlayerNames.java index b87ce6e046..cc40f72e1d 100644 --- a/src/main/java/model/PlayerNames.java +++ b/src/main/java/model/PlayerNames.java @@ -4,9 +4,9 @@ import java.util.*; -public class PlayerNames implements Iterable { - private static final String COMMA = ","; +import static controller.BlackJackGame.COMMA; +public class PlayerNames implements Iterable { private final List names; public PlayerNames(String input) { @@ -15,7 +15,7 @@ public PlayerNames(String input) { } private void validate(String input) { - StringUtils.validateNull(input); + StringUtils.validateString(input); validateSplit(input); } diff --git a/src/main/java/model/Players.java b/src/main/java/model/Players.java index cd21089496..cbb855c6fd 100644 --- a/src/main/java/model/Players.java +++ b/src/main/java/model/Players.java @@ -2,12 +2,14 @@ import java.util.*; +import static controller.BlackJackGame.COMMA; + public class Players implements Iterable { private final List players = new ArrayList<>(); - public Players(PlayerNames names, Deck deck, int initialDrawCount) { - for (String name : names) { - players.add(new Player(name, deck, initialDrawCount)); + public Players(PlayersData data, Deck deck) { + for (String name : data.getNames()) { + players.add(new Player(name, data.getBet(name), deck)); } } @@ -21,11 +23,11 @@ public String getNames() { for (Player player : players) { names.add(player.getName()); } - return String.join(",", names); + return String.join(COMMA, names); } @Override public Iterator iterator() { return players.iterator(); } -} +} \ No newline at end of file diff --git a/src/main/java/model/PlayersData.java b/src/main/java/model/PlayersData.java new file mode 100644 index 0000000000..93e1f06233 --- /dev/null +++ b/src/main/java/model/PlayersData.java @@ -0,0 +1,21 @@ +package model; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +public class PlayersData { + private final Map playersData = new LinkedHashMap<>(); + + public PlayersData(Map playersData) { + this.playersData.putAll(playersData); + } + + public Bet getBet(String name) { + return playersData.get(name); + } + + public Set getNames() { + return playersData.keySet(); + } +} diff --git a/src/main/java/model/Profit.java b/src/main/java/model/Profit.java new file mode 100644 index 0000000000..a434c95069 --- /dev/null +++ b/src/main/java/model/Profit.java @@ -0,0 +1,28 @@ +package model; + +import java.util.Objects; + +public class Profit { + private final double profit; + + public Profit(double profit) { + this.profit = profit; + } + + public double getProfit() { + return profit; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Profit profit1 = (Profit) o; + return Double.compare(profit1.profit, profit) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(profit); + } +} diff --git a/src/main/java/model/Result.java b/src/main/java/model/Result.java index 856612ad34..5693e8fbb5 100644 --- a/src/main/java/model/Result.java +++ b/src/main/java/model/Result.java @@ -3,63 +3,52 @@ import exception.IllegalResultException; import java.util.Arrays; +import java.util.function.BiFunction; public enum Result { - WIN("승", -1), - LOSE("패", 1), - DRAW("무", 0); + BLACKJACK(Constants.BLACK_JACK_RATIO, ((dealer, player) -> + !(dealer.isBlackJack()) && player.isBlackJack()) + ), + WIN(Constants.WIN_RATIO, (dealer, player) -> { + if (!player.isBust() && !player.isBlackJack()) { + return dealer.isBust() || dealer.isLowerThan(player); + } + return false; + }), + LOSE(Constants.LOSE_RATIO, (dealer, player) -> { + if (!player.isBlackJack()) { + return player.isBust() + || (!player.isBust() && !dealer.isBust() && player.isLowerThan(dealer)) + || (dealer.isBlackJack() && !player.isBlackJack()); + } + return false; + }), + DRAW(Constants.DRAW_RATIO, (dealer, player) -> dealer.isBlackJack() && player.isBlackJack() + || (!player.isBust() && !dealer.isBust() && player.isSameWith(dealer))); - String result; - int resultValue; + private final double ratio; + private final BiFunction function; - Result(String result, int resultValue) { - this.result = result; - this.resultValue = resultValue; + Result(double ratio, BiFunction function) { + this.ratio = ratio; + this.function = function; } - @Override - public String toString() { - return result; + public Profit calculateProfit(Player player) { + return new Profit(player.getMultiplyBet(ratio)); } public static Result compete(Dealer dealer, Player player) { - if (dealer.isBust() && player.isBust()) { - return DRAW; - } - if (dealer.isBust()) { - return WIN; - } - if (player.isBust()) { - return LOSE; - } - int compareValue = User.compare(dealer, player); - return Arrays.stream(Result.values()) - .filter(result -> result.isSameResult(compareValue)) - .findFirst() + return Arrays.stream(values()) + .filter(result -> result.function.apply(dealer, player)) + .findAny() .orElseThrow(() -> new IllegalResultException("올바른 비교 값이 아닙니다.")); } - private boolean isSameResult(int compareValue) { - return this.resultValue == compareValue; + private static class Constants { + public static final double BLACK_JACK_RATIO = 1.5; + public static final double WIN_RATIO = 1.0; + public static final double LOSE_RATIO = -1.0; + public static final double DRAW_RATIO = 0.0; } - - public static Result oppositeResult(Result result) { - if (isWin(result)) { - return LOSE; - } - if (isLose(result)) { - return WIN; - } - return DRAW; - } - - private static boolean isLose(final Result result) { - return result == LOSE; - } - - private static boolean isWin(final Result result) { - return result == WIN; - } - - } diff --git a/src/main/java/model/User.java b/src/main/java/model/User.java index 4a51f04fbb..04af5dccb5 100644 --- a/src/main/java/model/User.java +++ b/src/main/java/model/User.java @@ -1,23 +1,41 @@ package model; +import exception.IllegalDrawException; + import java.util.ArrayList; import java.util.List; import static controller.BlackJackGame.*; public abstract class User { - public static final String DELIMITER = ", "; - protected final String name; protected final CardHand cardHand; - public User(String name, Deck deck, int initialDrawCount) { + public User(String name, Deck deck) { + this.name = name; + this.cardHand = new CardHand(); + firstDraw(deck); + } + + public User(String name, List cards) { this.name = name; - this.cardHand = deck.draw(initialDrawCount); + this.cardHand = new CardHand(); + cards.forEach(cardHand::addCard); + } + + public void firstDraw(Deck deck) { + if (!cardHand.isEmpty()) { + throw new IllegalDrawException("2장 Draw는 패가 없는 경우에만 가능합니다."); + } + for (int i = 0; i < INITIAL_DRAW_COUNT; i++) { + cardHand.addCard(deck.draw()); + } } - public static int compare(final User dealer, final User player) { - return Integer.compare(dealer.getScore(), player.getScore()); + public void additionalDraw(Deck deck) { + for (int i = 0; i < ADDITIONAL_DRAW_COUNT; i++) { + cardHand.addCard(deck.draw()); + } } public String toStringCardHand() { @@ -26,24 +44,32 @@ public String toStringCardHand() { for (Card card : cardHand) { cardNames.add(card.toString()); } - return String.join(DELIMITER, cardNames); + return String.join(COMMA, cardNames); } - public void drawCard(Deck deck, int drawCount) { - for (Card drawCard : deck.draw(drawCount)) { - this.cardHand.addCard(drawCard); - } + public boolean isBust() { + return cardHand.isBust(); } - public boolean isBust() { - return getScore() > BLACK_JACK_COUNT; + public boolean isBlackJack() { + return cardHand.isBlackJack(); } + public abstract boolean isHitBound(); + public int getScore() { - return cardHand.calculateScore(); + return cardHand.getScore(); } public String getName() { return name; } + + public boolean isLowerThan(User other) { + return this.getScore() < other.getScore(); + } + + public boolean isSameWith(User other) { + return this.getScore() == other.getScore(); + } } diff --git a/src/main/java/utils/StringUtils.java b/src/main/java/utils/StringUtils.java index 858a9684d1..e99f763f00 100644 --- a/src/main/java/utils/StringUtils.java +++ b/src/main/java/utils/StringUtils.java @@ -6,6 +6,11 @@ public class StringUtils { + public static void validateString(String input) { + validateNull(input); + validateEmpty(input); + } + public static void validateEmpty(String input) { if (input.trim().isEmpty()) { throw new IllegalStringInputException("입력은 한 글자 이상이어야 합니다."); diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index e0978cd0a3..4bec14baa1 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -19,4 +19,9 @@ public static String inputYesOrNo(Player player) { "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); return scanner.nextLine(); } -} + + public static String inputBet(String name) { + System.out.println(NEW_LINE + name + "의 배팅 금액은?"); + return scanner.nextLine(); + } +} \ No newline at end of file diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index b39250f526..5e07b0afb8 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -5,18 +5,25 @@ import java.util.Map; import static controller.BlackJackGame.INITIAL_DRAW_COUNT; -import static controller.BlackJackGame.HIT_BOUNDARY; +import static controller.BlackJackGame.DEALER_HIT_BOUNDARY; import static model.Dealer.DEALER_NAME; public class OutputView { public static final String DELIMITER = ": "; public static final String NEW_LINE = "\n"; public static final String RESULT_STRING = " - 결과: "; + private static final String CARD = "카드"; + + + public static void printInitialState(Players players, Dealer dealer) { + printInitialCards(players, dealer); + printUsersCard(players, dealer); + } public static void printInitialCards(Players players, Dealer dealer) { StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("\n") + stringBuilder.append(NEW_LINE) .append(dealer.getName()) .append("와 ") .append(players.getNames()) @@ -32,7 +39,7 @@ public static void printUsersCard(Players players, Dealer dealer) { } private static void printDealerCard(Dealer dealer) { - System.out.print(NEW_LINE + dealer.getName() + DELIMITER + dealer.toStringCardHandFirst()); + System.out.print(NEW_LINE + dealer.getName() + CARD + DELIMITER + dealer.toStringCardHandFirst()); } private static void printPlayersCard(Players players) { @@ -43,7 +50,7 @@ private static void printPlayersCard(Players players) { } public static void printPlayerCard(User user) { - System.out.print(NEW_LINE + user.getName() + DELIMITER + user.toStringCardHand()); + System.out.print(NEW_LINE + user.getName() + CARD + DELIMITER + user.toStringCardHand()); } public static void printDealerDraw(Dealer dealer) { @@ -52,11 +59,16 @@ public static void printDealerDraw(Dealer dealer) { stringBuilder.append(NEW_LINE) .append(dealer.getName()) .append("는 ") - .append(HIT_BOUNDARY) + .append(DEALER_HIT_BOUNDARY) .append("이하라 한장의 카드를 더 받았습니다."); System.out.println(stringBuilder.toString()); } + public static void printResult(GameResult gameResult, Players players, Dealer dealer) { + printFinalCardHandResult(players, dealer); + printGameResult(gameResult); + } + public static void printFinalCardHandResult(final Players players, final Dealer dealer) { System.out.println(); printPlayerCard(dealer); @@ -67,27 +79,24 @@ public static void printFinalCardHandResult(final Players players, final Dealer } } - public static void printResult(final GameResult gameResult) { - System.out.println(NEW_LINE + NEW_LINE + "## 최종 승패"); + public static void printGameResult(final GameResult gameResult) { + System.out.println(NEW_LINE + NEW_LINE + "## 최종 수익"); printDealerResult(gameResult.getDealerResult()); printPlayersResult(gameResult.getPlayerResult()); } - public static void printDealerResult(final Map result) { + public static void printDealerResult(final Profit result) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(DEALER_NAME) .append(DELIMITER) - .append(result.get(Result.WIN)) - .append("승 ") - .append(result.get(Result.LOSE)) - .append("패"); + .append((int) result.getProfit()); System.out.println(stringBuilder.toString()); } - private static void printPlayersResult(final Map result) { + private static void printPlayersResult(final Map result) { for (Player player : result.keySet()) { System.out.print(player.getName() + DELIMITER); - System.out.println(result.get(player).toString()); + System.out.println((int) result.get(player).getProfit()); } } } diff --git a/src/test/java/model/AnswerTest.java b/src/test/java/model/AnswerTest.java index 287748ef6f..be7a9aae51 100644 --- a/src/test/java/model/AnswerTest.java +++ b/src/test/java/model/AnswerTest.java @@ -9,6 +9,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; public class AnswerTest { + @ParameterizedTest @DisplayName("입력값이 Y, y, N, n가 아닐 때 검증 테스트") @ValueSource(strings = {"q", ","}) @@ -21,13 +22,13 @@ void input_Validate_Test(String input) { @DisplayName("입력값이 N, n 일 때 테스트") @ValueSource(strings = {"N", "n"}) void input_No_Test(String input) { - assertThat(Answer.getYesOrNoByValue(input).getTrueOrFalse()).isFalse(); + assertThat(Answer.getYesOrNoByValue(input).isYes()).isFalse(); } @ParameterizedTest @DisplayName("입력값이 Y, y 일 때 테스트") @ValueSource(strings = {"Y", "y"}) void input_Yes_Test(String input) { - assertThat(Answer.getYesOrNoByValue(input).getTrueOrFalse()).isTrue(); + assertThat(Answer.getYesOrNoByValue(input).isYes()).isTrue(); } } diff --git a/src/test/java/model/BetTest.java b/src/test/java/model/BetTest.java new file mode 100644 index 0000000000..ee2b70c5cf --- /dev/null +++ b/src/test/java/model/BetTest.java @@ -0,0 +1,27 @@ +package model; + +import exception.BetFormatException; +import exception.BetRangeException; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class BetTest { + + @ParameterizedTest + @ValueSource(strings = {"99a", "안녕하세요", "-woowacourse"}) + void formatTest(String input) { + assertThatThrownBy(() -> new Bet(input)) + .isInstanceOf(BetFormatException.class) + .hasMessageMatching("베팅금액은 실수만 입력 가능합니다."); + } + + @ParameterizedTest + @ValueSource(strings = {"99", "0", "-1000"}) + void rangeTest(String input) { + assertThatThrownBy(() -> new Bet(input)) + .isInstanceOf(BetRangeException.class) + .hasMessageMatching("베팅금액은 100원 이상부터 입력 가능합니다."); + } +} diff --git a/src/test/java/model/CardHandTest.java b/src/test/java/model/CardHandTest.java index 79c70b671e..2858731402 100644 --- a/src/test/java/model/CardHandTest.java +++ b/src/test/java/model/CardHandTest.java @@ -12,7 +12,7 @@ public class CardHandTest { void isAce_Test() { CardHand cardHand = new CardHand(); cardHand.addCard(new Card(Symbol.ACE, Type.CLUB)); - assertThat(cardHand.isAce()).isTrue(); + assertThat(cardHand.hasAce()).isTrue(); } @Test diff --git a/src/test/java/model/DeckTest.java b/src/test/java/model/DeckTest.java index c4bb9771a2..d0063b242b 100644 --- a/src/test/java/model/DeckTest.java +++ b/src/test/java/model/DeckTest.java @@ -20,15 +20,20 @@ void init_deck() { @Test void drawTest() { - assertThatThrownBy(() -> deck.draw(DECK_SIZE + 1)) - .isInstanceOf(EmptyDeckException.class) - .hasMessageMatching("53장 이상 draw 할 카드가 존재하지 않습니다."); + assertThatThrownBy(() -> { + for (int i = 0; i < DECK_SIZE + 1; i++) { + deck.draw(); + } + }).isInstanceOf(EmptyDeckException.class) + .hasMessageMatching("Deck에 카드가 존재하지 않습니다."); } @ParameterizedTest @ValueSource(ints = {1, 2, 3, 4, 5}) void check_CardSize_Test(int count) { - deck.draw(count); + for (int i = 0; i < count; i++) { + deck.draw(); + } assertThat(deck.getCurrentDeckSize()).isEqualTo(DECK_SIZE - count); } } diff --git a/src/test/java/model/GameResultTest.java b/src/test/java/model/GameResultTest.java index 61cdf805c7..35834e206a 100644 --- a/src/test/java/model/GameResultTest.java +++ b/src/test/java/model/GameResultTest.java @@ -1,45 +1,44 @@ package model; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; -import static controller.BlackJackGame.INITIAL_DRAW_COUNT; import static model.UserTest.PLAYER_NAME; import static org.assertj.core.api.Assertions.assertThat; class GameResultTest { - CardHand cardHandWin1 = new CardHand(); - CardHand cardHandWin2 = new CardHand(); - CardHand cardHandLose = new CardHand(); - Deck deckWin1; - Deck deckWin2; - Deck deckLose; - List players = new ArrayList<>(); - Dealer dealer; + Player playerWin1 = new Player(PLAYER_NAME, Arrays.asList( + new Card(Symbol.KING, Type.CLUB), + new Card(Symbol.KING, Type.DIAMOND) + )); + Player playerWin2 = new Player(PLAYER_NAME, Arrays.asList( + new Card(Symbol.QUEEN, Type.CLUB), + new Card(Symbol.KING, Type.HEART) + )); + Player playerBlackJack = new Player(PLAYER_NAME, Arrays.asList( + new Card(Symbol.QUEEN, Type.CLUB), + new Card(Symbol.ACE, Type.HEART) + )); + Dealer dealerLose = new Dealer(Arrays.asList( + new Card(Symbol.TWO, Type.CLUB), + new Card(Symbol.TWO, Type.DIAMOND) + )); + Dealer dealerBlackJack = new Dealer(Arrays.asList( + new Card(Symbol.ACE, Type.CLUB), + new Card(Symbol.JACK, Type.DIAMOND) + )); + Players players = new Players(Arrays.asList(playerWin1, playerWin2, playerBlackJack)); - @BeforeEach - void init_field() { - cardHandWin1.addCard(new Card(Symbol.KING, Type.CLUB)); - cardHandWin1.addCard(new Card(Symbol.KING, Type.DIAMOND)); - cardHandWin2.addCard(new Card(Symbol.KING, Type.CLUB)); - cardHandWin2.addCard(new Card(Symbol.KING, Type.DIAMOND)); - cardHandLose.addCard(new Card(Symbol.TWO, Type.DIAMOND)); - cardHandLose.addCard(new Card(Symbol.TWO, Type.CLUB)); - deckWin1 = new Deck(cardHandWin1); - deckWin2 = new Deck(cardHandWin2); - deckLose = new Deck(cardHandLose); - players.add(new Player(PLAYER_NAME, deckWin1, INITIAL_DRAW_COUNT)); - players.add(new Player(PLAYER_NAME, deckWin2, INITIAL_DRAW_COUNT)); - dealer = new Dealer(deckLose, INITIAL_DRAW_COUNT); + @Test + void calculateDealerResultTest1() { + GameResult gameResult = new GameResult(players, dealerLose); + assertThat(gameResult.getDealerResult().getProfit()).isEqualTo(-350); } @Test - void gameResultTest() { - GameResult gameResult = new GameResult(new Players(players), dealer); - gameResult.calculateResults(); - assertThat(gameResult.getDealerResult().get(Result.LOSE)).isEqualTo(2); + void calculateDealerResultTest2() { + GameResult gameResult = new GameResult(players, dealerBlackJack); + assertThat(gameResult.getDealerResult().getProfit()).isEqualTo(200); } } \ No newline at end of file diff --git a/src/test/java/model/PlayerNamesTest.java b/src/test/java/model/PlayerNamesTest.java index 9f3448d3fe..719b8ee16f 100644 --- a/src/test/java/model/PlayerNamesTest.java +++ b/src/test/java/model/PlayerNamesTest.java @@ -11,22 +11,6 @@ public class PlayerNamesTest { - @Test - @DisplayName("입력으로 빈값이 들어올 때") - void input_Empty() { - assertThatThrownBy(() -> new PlayerNames(" ")) - .isInstanceOf(IllegalStringInputException.class) - .hasMessageMatching("입력은 한 글자 이상이어야 합니다."); - } - - @Test - @DisplayName("입력으로 null값이 들어올 때") - void input_Null() { - assertThatThrownBy(() -> new PlayerNames(null)) - .isInstanceOf(IllegalStringInputException.class) - .hasMessageMatching("Null 값은 입력하실 수 없습니다."); - } - @Test @DisplayName("쉼표만 있을 때") void split_test() { diff --git a/src/test/java/model/ResultTest.java b/src/test/java/model/ResultTest.java index 4eae5d39af..46cb4ca315 100644 --- a/src/test/java/model/ResultTest.java +++ b/src/test/java/model/ResultTest.java @@ -1,54 +1,79 @@ package model; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static controller.BlackJackGame.ADDITIONAL_DRAW_COUNT; -import static controller.BlackJackGame.INITIAL_DRAW_COUNT; +import java.util.Arrays; + import static model.UserTest.PLAYER_NAME; import static org.assertj.core.api.Assertions.assertThat; public class ResultTest { - CardHand bustHand1 = new CardHand(); - CardHand bustHand2 = new CardHand(); - CardHand notBustHand1 = new CardHand(); - Deck bustDeck1; - Deck bustDeck2; - Deck notBustDeck1; - - @BeforeEach - void init_field() { - bustHand1.addCard(new Card(Symbol.KING, Type.DIAMOND)); - bustHand1.addCard(new Card(Symbol.QUEEN, Type.DIAMOND)); - bustHand1.addCard(new Card(Symbol.JACK, Type.DIAMOND)); - bustHand2.addCard(new Card(Symbol.NINE, Type.DIAMOND)); - bustHand2.addCard(new Card(Symbol.KING, Type.DIAMOND)); - bustHand2.addCard(new Card(Symbol.KING, Type.DIAMOND)); - notBustHand1.addCard(new Card(Symbol.KING, Type.DIAMOND)); - notBustHand1.addCard(new Card(Symbol.JACK, Type.DIAMOND)); - - bustDeck1 = new Deck(bustHand1); - bustDeck2 = new Deck(bustHand2); - notBustDeck1 = new Deck(notBustHand1); + Player bustPlayer = new Player(PLAYER_NAME, Arrays.asList( + new Card(Symbol.KING, Type.DIAMOND), + new Card(Symbol.QUEEN, Type.DIAMOND), + new Card(Symbol.TWO, Type.DIAMOND) + )); + Player notBustPlayer = new Player(PLAYER_NAME, Arrays.asList( + new Card(Symbol.KING, Type.DIAMOND), + new Card(Symbol.QUEEN, Type.DIAMOND), + new Card(Symbol.ACE, Type.DIAMOND) + )); + Player blackJackPlayer = new Player(PLAYER_NAME, Arrays.asList( + new Card(Symbol.QUEEN, Type.CLUB), + new Card(Symbol.ACE, Type.HEART) + )); + Dealer bustDealer = new Dealer(Arrays.asList( + new Card(Symbol.KING, Type.DIAMOND), + new Card(Symbol.QUEEN, Type.DIAMOND), + new Card(Symbol.TWO, Type.DIAMOND) + )); + Dealer notBustDealer = new Dealer(Arrays.asList( + new Card(Symbol.KING, Type.DIAMOND), + new Card(Symbol.QUEEN, Type.DIAMOND), + new Card(Symbol.ACE, Type.DIAMOND) + )); + Dealer blackJackDealer = new Dealer(Arrays.asList( + new Card(Symbol.QUEEN, Type.CLUB), + new Card(Symbol.ACE, Type.HEART) + )); + + @Test + @DisplayName("플레이어만 첫 패가 21인 경우 BLACKJACK") + void blackJackTest() { + assertThat(Result.compete(notBustDealer, blackJackPlayer)).isEqualTo(Result.BLACKJACK); + assertThat(Result.compete(bustDealer, blackJackPlayer)).isEqualTo(Result.BLACKJACK); + } + @DisplayName("딜러와 플레이어 모두 21을 넘긴 경우 LOSE") @Test - @DisplayName("딜러와 플레이어 모두 21을 넘긴 경우 플레이 DRAW") - void both_isBust_Test() { - Player player = new Player(PLAYER_NAME, bustDeck1, INITIAL_DRAW_COUNT); - Dealer dealer = new Dealer(bustDeck2, INITIAL_DRAW_COUNT); - player.drawCard(bustDeck1, ADDITIONAL_DRAW_COUNT); - dealer.drawCard(bustDeck2, ADDITIONAL_DRAW_COUNT); - assertThat(Result.compete(dealer, player) == Result.DRAW).isTrue(); + void bothBustTest() { + assertThat(Result.compete(bustDealer, bustPlayer)).isEqualTo(Result.LOSE); } @Test @DisplayName("딜러만 21을 넘긴 경우 플레이어 WIN") - void Dealer_isBust_Test() { - Player player = new Player(PLAYER_NAME, notBustDeck1, INITIAL_DRAW_COUNT); - Dealer dealer = new Dealer(bustDeck1, INITIAL_DRAW_COUNT); - dealer.drawCard(bustDeck1, ADDITIONAL_DRAW_COUNT); - assertThat(Result.compete(dealer, player) == Result.WIN).isTrue(); + void dealerBustTest() { + assertThat(Result.compete(bustDealer, notBustPlayer)).isEqualTo(Result.WIN); + } + + @Test + @DisplayName("딜러와 플레이어 모두 21인 경우 DRAW") + void bothBlackJackTest() { + assertThat(Result.compete(blackJackDealer, blackJackPlayer)).isEqualTo(Result.DRAW); + } + + @Test + @DisplayName("딜러만 블랙잭인 경우 플레이어 Lose") + void dealerBlackJackTest() { + assertThat(Result.compete(blackJackDealer, notBustPlayer)).isEqualTo(Result.LOSE); + } + + @Test + @DisplayName("블랙잭 우승 시 수익 계산") + void playerBlackJackRevenueTest() { + Result result = Result.compete(notBustDealer, blackJackPlayer); + assertThat(result.calculateProfit(blackJackPlayer).getProfit()).isEqualTo(150); } } diff --git a/src/test/java/model/StringUtilTest.java b/src/test/java/model/StringUtilTest.java new file mode 100644 index 0000000000..e6bc46daf1 --- /dev/null +++ b/src/test/java/model/StringUtilTest.java @@ -0,0 +1,30 @@ +package model; + +import exception.IllegalStringInputException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import utils.StringUtils; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class StringUtilTest { + + @ParameterizedTest + @ValueSource(strings = {" ", ""}) + @DisplayName("입력으로 빈값이 들어올 때") + void input_Empty(String input) { + assertThatThrownBy(() -> StringUtils.validateEmpty(input)) + .isInstanceOf(IllegalStringInputException.class) + .hasMessageMatching("입력은 한 글자 이상이어야 합니다."); + } + + @Test + @DisplayName("입력으로 null값이 들어올 때") + void input_Null() { + assertThatThrownBy(() -> StringUtils.validateNull(null)) + .isInstanceOf(IllegalStringInputException.class) + .hasMessageMatching("Null 값은 입력하실 수 없습니다."); + } +} diff --git a/src/test/java/model/UserTest.java b/src/test/java/model/UserTest.java index ee44f21f5e..8dcb1c5e3e 100644 --- a/src/test/java/model/UserTest.java +++ b/src/test/java/model/UserTest.java @@ -1,50 +1,76 @@ package model; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static controller.BlackJackGame.ADDITIONAL_DRAW_COUNT; -import static controller.BlackJackGame.INITIAL_DRAW_COUNT; +import java.util.Arrays; + +import static model.Dealer.DEALER_NAME; import static org.assertj.core.api.Assertions.assertThat; public class UserTest { public static final String PLAYER_NAME = "DD"; - CardHand bustHand = new CardHand(); - CardHand notBustHand = new CardHand(); - Deck bustDeck; - Deck notBustDeck; - - @BeforeEach - void init_field() { - bustHand.addCard(new Card(Symbol.KING, Type.DIAMOND)); - bustHand.addCard(new Card(Symbol.QUEEN, Type.DIAMOND)); - bustHand.addCard(new Card(Symbol.JACK, Type.DIAMOND)); - notBustHand.addCard(new Card(Symbol.KING, Type.DIAMOND)); - notBustHand.addCard(new Card(Symbol.JACK, Type.DIAMOND)); - bustDeck = new Deck(bustHand); - notBustDeck = new Deck(notBustHand); - } + + private Player bustPlayer = new Player(PLAYER_NAME, Arrays.asList( + new Card(Symbol.KING, Type.DIAMOND), + new Card(Symbol.QUEEN, Type.DIAMOND), + new Card(Symbol.TWO, Type.DIAMOND) + )); + private Player notBustPlayer = new Player(PLAYER_NAME, Arrays.asList( + new Card(Symbol.KING, Type.DIAMOND), + new Card(Symbol.QUEEN, Type.DIAMOND) + )); + private Player blackJackPlayer = new Player(PLAYER_NAME, Arrays.asList( + new Card(Symbol.QUEEN, Type.CLUB), + new Card(Symbol.ACE, Type.HEART) + )); + private Dealer bustDealer = new Dealer(Arrays.asList( + new Card(Symbol.KING, Type.DIAMOND), + new Card(Symbol.QUEEN, Type.DIAMOND), + new Card(Symbol.TWO, Type.DIAMOND) + )); + private Dealer notBustDealer = new Dealer(Arrays.asList( + new Card(Symbol.KING, Type.DIAMOND), + new Card(Symbol.TWO, Type.DIAMOND) + )); @Test @DisplayName("딜러 이름 테스트") void dealer_Name() { - User dealer = new Dealer(notBustDeck, INITIAL_DRAW_COUNT); - assertThat(dealer.getName()).isEqualTo("딜러"); + assertThat(notBustDealer.getName()).isEqualTo(DEALER_NAME); } @Test @DisplayName("이름을 불러오는 지 테스트") void name_Test() { - Player player = new Player(PLAYER_NAME, notBustDeck, INITIAL_DRAW_COUNT); - assertThat(player.getName()).isEqualTo(PLAYER_NAME); + assertThat(notBustPlayer.getName()).isEqualTo(PLAYER_NAME); + } + + @Test + @DisplayName("bust 테스트") + void isBustTest() { + assertThat(bustDealer.isBust()).isTrue(); + assertThat(bustPlayer.isBust()).isTrue(); + assertThat(blackJackPlayer.isBust()).isFalse(); + assertThat(notBustPlayer.isBust()).isFalse(); + assertThat(notBustDealer.isBust()).isFalse(); + } + + @Test + @DisplayName("blackjack 테스트") + void isBlackJackTest() { + assertThat(notBustPlayer.isBlackJack()).isFalse(); + assertThat(blackJackPlayer.isBlackJack()).isTrue(); + assertThat(bustPlayer.isBlackJack()).isFalse(); } @Test - @DisplayName("21을 넘는 지") - void isBust_Player_Test() { - Player player = new Player(PLAYER_NAME, bustDeck, INITIAL_DRAW_COUNT); - player.drawCard(bustDeck, ADDITIONAL_DRAW_COUNT); - assertThat(player.isBust()).isTrue(); + @DisplayName("isHitBoundTest 테스트") + void isHitBoundTest() { + assertThat(notBustPlayer.isHitBound()).isTrue(); + assertThat(blackJackPlayer.isHitBound()).isFalse(); + assertThat(bustPlayer.isHitBound()).isFalse(); + assertThat(notBustDealer.isHitBound()).isTrue(); + assertThat(bustDealer.isHitBound()).isFalse(); } }