From 3f6e3884f313b8840ae5ff95df6d4c7aff9101eb Mon Sep 17 00:00:00 2001 From: YebinK Date: Tue, 10 Mar 2020 13:56:25 +0900 Subject: [PATCH 01/19] =?UTF-8?q?[feature]=20=EC=97=B0=EB=A3=8C=20?= =?UTF-8?q?=EC=A3=BC=EC=9E=85=20=EC=8B=A4=EC=8A=B5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/empty.txt | 0 src/main/java/interfacepractice/Avante.java | 15 ++++++++++ src/main/java/interfacepractice/Car.java | 18 +++++++++++ src/main/java/interfacepractice/K5.java | 15 ++++++++++ .../java/interfacepractice/RentCompany.java | 30 +++++++++++++++++++ src/main/java/interfacepractice/Sonata.java | 15 ++++++++++ src/test/java/empty.txt | 0 .../interfacepractice/RentCompanyTest.java | 29 ++++++++++++++++++ 8 files changed, 122 insertions(+) delete mode 100644 src/main/java/empty.txt create mode 100644 src/main/java/interfacepractice/Avante.java create mode 100644 src/main/java/interfacepractice/Car.java create mode 100644 src/main/java/interfacepractice/K5.java create mode 100644 src/main/java/interfacepractice/RentCompany.java create mode 100644 src/main/java/interfacepractice/Sonata.java delete mode 100644 src/test/java/empty.txt create mode 100644 src/test/java/interfacepractice/RentCompanyTest.java diff --git a/src/main/java/empty.txt b/src/main/java/empty.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/main/java/interfacepractice/Avante.java b/src/main/java/interfacepractice/Avante.java new file mode 100644 index 0000000000..f8509a9430 --- /dev/null +++ b/src/main/java/interfacepractice/Avante.java @@ -0,0 +1,15 @@ +package interfacepractice; + +public class Avante extends Car { + + private static int FUEL_EFFICIENCY = 15; + private static String NAME = "Avante"; + + public Avante(int distance) { + super(FUEL_EFFICIENCY, distance); + } + + public String getName() { + return NAME; + } +} diff --git a/src/main/java/interfacepractice/Car.java b/src/main/java/interfacepractice/Car.java new file mode 100644 index 0000000000..3f1fd9520f --- /dev/null +++ b/src/main/java/interfacepractice/Car.java @@ -0,0 +1,18 @@ +package interfacepractice; + +public abstract class Car { + + private int fuelEfficiency; + private int distance; + + public Car(int fuelEfficiency, int distance) { + this.fuelEfficiency = fuelEfficiency; + this.distance = distance; + } + + public int getNeededFuel() { + return distance / fuelEfficiency; + } + + public abstract String getName(); +} diff --git a/src/main/java/interfacepractice/K5.java b/src/main/java/interfacepractice/K5.java new file mode 100644 index 0000000000..a81fce1ac0 --- /dev/null +++ b/src/main/java/interfacepractice/K5.java @@ -0,0 +1,15 @@ +package interfacepractice; + +public class K5 extends Car { + + private static int FUEL_EFFICIENCY = 13; + private static String NAME = "K5"; + + public K5(int distance) { + super(FUEL_EFFICIENCY, distance); + } + + public String getName() { + return NAME; + } +} diff --git a/src/main/java/interfacepractice/RentCompany.java b/src/main/java/interfacepractice/RentCompany.java new file mode 100644 index 0000000000..984d707aa7 --- /dev/null +++ b/src/main/java/interfacepractice/RentCompany.java @@ -0,0 +1,30 @@ +package interfacepractice; + +import java.util.ArrayList; +import java.util.List; + +public class RentCompany { + + public static final String NEWLINE = System.getProperty("line.separator"); + + private List cars = new ArrayList<>(); + + public static RentCompany create() { + return new RentCompany(); + } + + public void addCar(Car car) { + cars.add(car); + } + + public String generateReport() { + StringBuilder stringBuilder = new StringBuilder(); + for (Car car : cars) { + stringBuilder.append(car.getName()); + stringBuilder.append(" : "); + stringBuilder.append(car.getNeededFuel() + "리터"); + stringBuilder.append(NEWLINE); + } + return stringBuilder.toString(); + } +} diff --git a/src/main/java/interfacepractice/Sonata.java b/src/main/java/interfacepractice/Sonata.java new file mode 100644 index 0000000000..26b3759840 --- /dev/null +++ b/src/main/java/interfacepractice/Sonata.java @@ -0,0 +1,15 @@ +package interfacepractice; + +public class Sonata extends Car { + + private static int FUEL_EFFICIENCY = 10; + private static String NAME = "Sonata"; + + public Sonata(int distance) { + super(FUEL_EFFICIENCY, distance); + } + + public String getName() { + return NAME; + } +} diff --git a/src/test/java/empty.txt b/src/test/java/empty.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/test/java/interfacepractice/RentCompanyTest.java b/src/test/java/interfacepractice/RentCompanyTest.java new file mode 100644 index 0000000000..a3c7d9496c --- /dev/null +++ b/src/test/java/interfacepractice/RentCompanyTest.java @@ -0,0 +1,29 @@ +package interfacepractice; + +import org.junit.jupiter.api.Test; + +import static interfacepractice.RentCompany.NEWLINE; +import static org.assertj.core.api.Assertions.assertThat; + +public class RentCompanyTest { + + @Test + public void report() { + RentCompany company = RentCompany.create(); // factory method를 사용해 생성 + company.addCar(new Sonata(150)); + company.addCar(new K5(260)); + company.addCar(new Sonata(120)); + company.addCar(new Avante(300)); + company.addCar(new K5(390)); + + String report = company.generateReport(); + assertThat(report).isEqualTo( + "Sonata : 15리터" + NEWLINE + + "K5 : 20리터" + NEWLINE + + "Sonata : 12리터" + NEWLINE + + "Avante : 20리터" + NEWLINE + + "K5 : 30리터" + NEWLINE + ); + } + +} \ No newline at end of file From 31552df93300539a1ac7c05199fd998bb915eac3 Mon Sep 17 00:00:00 2001 From: YebinK Date: Tue, 10 Mar 2020 15:02:00 +0900 Subject: [PATCH 02/19] =?UTF-8?q?[docs]=20README.md=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3bcfc25784..4dc12c0eae 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,19 @@ # java-blackjack 블랙잭 게임 미션 저장소 -## 우아한테크코스 코드리뷰 -* [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md) \ No newline at end of file +## 도메인 기능 요구사항 +- [ ] 딜러가 게임 참가자들에게 카드를 2장씩 나눈다. +- [ ] 딜러와 플레이어들은 카드를 가질 수 있다. +- [ ] 각 플레이어들의 카드 합을 구한다. + - [ ] Ace는 1 또는 11로 계산하고, King, Queen, Jack은 10으로 계산한다. +- [ ] 플레이어들의 카드 합이 21을 넘는지 확인한다. +- [ ] 딜러의 카드 합이 16 이하인 경우 카드를 계속해서 더 받는다. + +## 입출력 기능 요구사항 +- [ ] 게임에 참여할 사람의 이름을 입력받는다. + - [ ] (예외) 공백이 입력될 경우 +- [ ] 딜러와 플레이어의 카드를 출력한다. + - [ ] 딜러는 2장 중 1장만 출력한다. +- [ ] 플레이어들을 순회하며 카드를 더 받을지 물어본다. +- [ ] 딜러와 플레이어들의 카드 목록과 합을 출력한다. +- [ ] 최종 승패를 출력한다. \ No newline at end of file From cd6dfb586755ef46868866b925850de26cd1ca5e Mon Sep 17 00:00:00 2001 From: "jaeju.jang" Date: Tue, 10 Mar 2020 17:44:04 +0900 Subject: [PATCH 03/19] =?UTF-8?q?[feature]=20card=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Card의 타입을 나타내는 CardType enum 추가 - Card의 심볼을 나타내는 CardSymbol enum 추가 --- src/main/java/blackjack/domain/card/Card.java | 17 ++++++++++ .../blackjack/domain/card/CardSymbol.java | 31 +++++++++++++++++++ .../java/blackjack/domain/card/CardType.java | 20 ++++++++++++ .../java/blackjack/domain/card/CardTest.java | 14 +++++++++ 4 files changed, 82 insertions(+) create mode 100644 src/main/java/blackjack/domain/card/Card.java create mode 100644 src/main/java/blackjack/domain/card/CardSymbol.java create mode 100644 src/main/java/blackjack/domain/card/CardType.java create mode 100644 src/test/java/blackjack/domain/card/CardTest.java diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java new file mode 100644 index 0000000000..dfb9553777 --- /dev/null +++ b/src/main/java/blackjack/domain/card/Card.java @@ -0,0 +1,17 @@ +package blackjack.domain.card; + +public class Card { + + private CardSymbol cardSymbol; + private CardType cardType; + + public Card(CardSymbol cardSymbol, CardType cardType) { + this.cardSymbol = cardSymbol; + this.cardType = cardType; + } + + @Override + public String toString() { + return cardSymbol.getCardSymbol() + cardType.getKoreanName(); + } +} diff --git a/src/main/java/blackjack/domain/card/CardSymbol.java b/src/main/java/blackjack/domain/card/CardSymbol.java new file mode 100644 index 0000000000..3c743b11d5 --- /dev/null +++ b/src/main/java/blackjack/domain/card/CardSymbol.java @@ -0,0 +1,31 @@ +package blackjack.domain.card; + +public enum CardSymbol { + + ACE(11, "A"), + 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(10, "J"), + QUEEN(10, "Q"), + KING(10, "K"); + + private final int carNumber; + private final String cardSymbol; + + CardSymbol(int cardnNumber, String cardSymbol) { + this.carNumber = cardnNumber; + this.cardSymbol = cardSymbol; + } + + public String getCardSymbol() { + return cardSymbol; + } + +} diff --git a/src/main/java/blackjack/domain/card/CardType.java b/src/main/java/blackjack/domain/card/CardType.java new file mode 100644 index 0000000000..df03c59d3b --- /dev/null +++ b/src/main/java/blackjack/domain/card/CardType.java @@ -0,0 +1,20 @@ +package blackjack.domain.card; + +public enum CardType { + + SPADE("스페이드"), + HEART("하트"), + CLOVER("클로버"), + DIAMOND("다이아몬드"); + + private final String koreanName; + + CardType(String koreanName) { + this.koreanName = koreanName; + } + + public String getKoreanName() { + return koreanName; + } + +} diff --git a/src/test/java/blackjack/domain/card/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java new file mode 100644 index 0000000000..94449db9a6 --- /dev/null +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -0,0 +1,14 @@ +package blackjack.domain.card; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CardTest { + + @Test + void cardTest() { + Card card = new Card(CardSymbol.ACE, CardType.SPADE); + assertThat(card.toString()).isEqualTo("A스페이드"); + } +} From 5963ee702b97eddaa7997dda1dffead3006713fe Mon Sep 17 00:00:00 2001 From: YebinK Date: Tue, 10 Mar 2020 17:54:41 +0900 Subject: [PATCH 04/19] =?UTF-8?q?[feature]=20CardFactory=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/domain/card/CardFactory.java | 19 +++++++++++++++++++ src/test/java/blackjack/domain/DeckTest.java | 5 +++++ .../domain/card/CardFactoryTest.java | 18 ++++++++++++++++++ .../java/blackjack/domain/card/CardTest.java | 2 ++ 4 files changed, 44 insertions(+) create mode 100644 src/main/java/blackjack/domain/card/CardFactory.java create mode 100644 src/test/java/blackjack/domain/DeckTest.java create mode 100644 src/test/java/blackjack/domain/card/CardFactoryTest.java diff --git a/src/main/java/blackjack/domain/card/CardFactory.java b/src/main/java/blackjack/domain/card/CardFactory.java new file mode 100644 index 0000000000..818e8ae076 --- /dev/null +++ b/src/main/java/blackjack/domain/card/CardFactory.java @@ -0,0 +1,19 @@ +package blackjack.domain.card; + +import java.util.ArrayList; +import java.util.List; + +public class CardFactory { + + public static List generate() { + + List cards = new ArrayList<>(); + + for (CardSymbol symbol : CardSymbol.values()) { + for (CardType type : CardType.values()) { + cards.add(new Card(symbol, type)); + } + } + return cards; + } +} diff --git a/src/test/java/blackjack/domain/DeckTest.java b/src/test/java/blackjack/domain/DeckTest.java new file mode 100644 index 0000000000..992283d20a --- /dev/null +++ b/src/test/java/blackjack/domain/DeckTest.java @@ -0,0 +1,5 @@ +package blackjack.domain; + +public class DeckTest { + +} diff --git a/src/test/java/blackjack/domain/card/CardFactoryTest.java b/src/test/java/blackjack/domain/card/CardFactoryTest.java new file mode 100644 index 0000000000..1632a98ac1 --- /dev/null +++ b/src/test/java/blackjack/domain/card/CardFactoryTest.java @@ -0,0 +1,18 @@ +package blackjack.domain.card; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CardFactoryTest { + + @Test + @DisplayName("카드 리스트 생성") + void generate() { + List cards = CardFactory.generate(); + assertThat(cards.size()).isEqualTo(52); + } +} diff --git a/src/test/java/blackjack/domain/card/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java index 94449db9a6..070c16b559 100644 --- a/src/test/java/blackjack/domain/card/CardTest.java +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -1,5 +1,6 @@ package blackjack.domain.card; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -7,6 +8,7 @@ public class CardTest { @Test + @DisplayName("카드 생성") void cardTest() { Card card = new Card(CardSymbol.ACE, CardType.SPADE); assertThat(card.toString()).isEqualTo("A스페이드"); From 42b2694c6020bf566e33b256425a3517033e03e3 Mon Sep 17 00:00:00 2001 From: YebinK Date: Tue, 10 Mar 2020 18:05:55 +0900 Subject: [PATCH 05/19] =?UTF-8?q?[feature]=20Deck=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/card/Deck.java | 26 +++++++++++++++++++ src/test/java/blackjack/domain/DeckTest.java | 17 ++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/main/java/blackjack/domain/card/Deck.java diff --git a/src/main/java/blackjack/domain/card/Deck.java b/src/main/java/blackjack/domain/card/Deck.java new file mode 100644 index 0000000000..736dc610c8 --- /dev/null +++ b/src/main/java/blackjack/domain/card/Deck.java @@ -0,0 +1,26 @@ +package blackjack.domain.card; + +import java.util.Collections; +import java.util.List; +import java.util.Stack; + +public class Deck { + + private final Stack cards = new Stack<>(); + + public Deck(List cards) { + + Collections.shuffle(cards); + this.cards.addAll(cards); + } + + public Card pick() { + + if (cards.empty()) { + throw new NullPointerException("카드를 모두 사용하셨습니다."); + } + return cards.pop(); + } + + +} diff --git a/src/test/java/blackjack/domain/DeckTest.java b/src/test/java/blackjack/domain/DeckTest.java index 992283d20a..74376756fb 100644 --- a/src/test/java/blackjack/domain/DeckTest.java +++ b/src/test/java/blackjack/domain/DeckTest.java @@ -1,5 +1,22 @@ package blackjack.domain; +import blackjack.domain.card.Card; +import blackjack.domain.card.CardFactory; +import blackjack.domain.card.Deck; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + public class DeckTest { + @Test + @DisplayName("카드 꺼내기") + void pick() { + List cards = CardFactory.generate(); + Deck deck = new Deck(cards); + assertThat(deck.pick()).isInstanceOf(Card.class); + } } From ab48de9eec17fdbd9429c1b387714f248fc39b2a Mon Sep 17 00:00:00 2001 From: "jaeju.jang" Date: Wed, 11 Mar 2020 13:22:16 +0900 Subject: [PATCH 06/19] =?UTF-8?q?[feature]=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=98=EA=B3=A0,=20?= =?UTF-8?q?=EA=B0=80=EC=A7=80=EA=B3=A0=20=EC=9E=88=EB=8A=94=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=EC=9D=98=20=ED=95=A9=EC=9D=84=20=EA=B5=AC=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ACE의 값이 1 또는 11로 계산하도록 구현 --- README.md | 4 +- src/main/java/blackjack/domain/card/Card.java | 8 +++ .../blackjack/domain/card/CardSymbol.java | 13 ++++- .../blackjack/domain/{card => deck}/Deck.java | 4 +- .../java/blackjack/domain/user/Player.java | 48 ++++++++++++++++ .../blackjack/domain/{ => deck}/DeckTest.java | 4 +- .../blackjack/domain/user/PlayerTest.java | 57 +++++++++++++++++++ 7 files changed, 130 insertions(+), 8 deletions(-) rename src/main/java/blackjack/domain/{card => deck}/Deck.java (87%) create mode 100644 src/main/java/blackjack/domain/user/Player.java rename src/test/java/blackjack/domain/{ => deck}/DeckTest.java (87%) create mode 100644 src/test/java/blackjack/domain/user/PlayerTest.java diff --git a/README.md b/README.md index 4dc12c0eae..89877db362 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ ## 도메인 기능 요구사항 - [ ] 딜러가 게임 참가자들에게 카드를 2장씩 나눈다. - [ ] 딜러와 플레이어들은 카드를 가질 수 있다. -- [ ] 각 플레이어들의 카드 합을 구한다. - - [ ] Ace는 1 또는 11로 계산하고, King, Queen, Jack은 10으로 계산한다. +- [x] 각 플레이어들의 카드 합을 구한다. + - [x] Ace는 1 또는 11로 계산하고, King, Queen, Jack은 10으로 계산한다. - [ ] 플레이어들의 카드 합이 21을 넘는지 확인한다. - [ ] 딜러의 카드 합이 16 이하인 경우 카드를 계속해서 더 받는다. diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index dfb9553777..45c9fb692b 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -14,4 +14,12 @@ public Card(CardSymbol cardSymbol, CardType cardType) { public String toString() { return cardSymbol.getCardSymbol() + cardType.getKoreanName(); } + + public int getNumber() { + return cardSymbol.getCardNumber(); + } + + public boolean isAce() { + return cardSymbol.isAce(); + } } diff --git a/src/main/java/blackjack/domain/card/CardSymbol.java b/src/main/java/blackjack/domain/card/CardSymbol.java index 3c743b11d5..7d1a0eaea2 100644 --- a/src/main/java/blackjack/domain/card/CardSymbol.java +++ b/src/main/java/blackjack/domain/card/CardSymbol.java @@ -16,11 +16,11 @@ public enum CardSymbol { QUEEN(10, "Q"), KING(10, "K"); - private final int carNumber; + private final int cardNumber; private final String cardSymbol; - CardSymbol(int cardnNumber, String cardSymbol) { - this.carNumber = cardnNumber; + CardSymbol(int cardNumber, String cardSymbol) { + this.cardNumber = cardNumber; this.cardSymbol = cardSymbol; } @@ -28,4 +28,11 @@ public String getCardSymbol() { return cardSymbol; } + public int getCardNumber() { + return cardNumber; + } + + public boolean isAce() { + return this == ACE; + } } diff --git a/src/main/java/blackjack/domain/card/Deck.java b/src/main/java/blackjack/domain/deck/Deck.java similarity index 87% rename from src/main/java/blackjack/domain/card/Deck.java rename to src/main/java/blackjack/domain/deck/Deck.java index 736dc610c8..aad4bb9936 100644 --- a/src/main/java/blackjack/domain/card/Deck.java +++ b/src/main/java/blackjack/domain/deck/Deck.java @@ -1,4 +1,6 @@ -package blackjack.domain.card; +package blackjack.domain.deck; + +import blackjack.domain.card.Card; import java.util.Collections; import java.util.List; diff --git a/src/main/java/blackjack/domain/user/Player.java b/src/main/java/blackjack/domain/user/Player.java new file mode 100644 index 0000000000..260001043b --- /dev/null +++ b/src/main/java/blackjack/domain/user/Player.java @@ -0,0 +1,48 @@ +package blackjack.domain.user; + +import blackjack.domain.card.Card; + +import java.util.ArrayList; +import java.util.List; + +public class Player { + + private static final int BURSTED_THRESHOLD = 21; + private static final int ACE_NUMBER_GAP = 10; + + private final List cards = new ArrayList<>(); + private final String name; + + public Player(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void add(Card card) { + cards.add(card); + } + + public int calculateSum() { + int result = sumAll(); + return subtractIfContainingAce(result); + } + + private int sumAll() { + return cards.stream() + .mapToInt(Card::getNumber) + .sum(); + } + + private int subtractIfContainingAce(int result) { + for (Card card : cards) { + if (result > BURSTED_THRESHOLD && card.isAce()) { + result -= ACE_NUMBER_GAP; + } + } + return result; + } + +} diff --git a/src/test/java/blackjack/domain/DeckTest.java b/src/test/java/blackjack/domain/deck/DeckTest.java similarity index 87% rename from src/test/java/blackjack/domain/DeckTest.java rename to src/test/java/blackjack/domain/deck/DeckTest.java index 74376756fb..7934c770fc 100644 --- a/src/test/java/blackjack/domain/DeckTest.java +++ b/src/test/java/blackjack/domain/deck/DeckTest.java @@ -1,8 +1,8 @@ -package blackjack.domain; +package blackjack.domain.deck; import blackjack.domain.card.Card; import blackjack.domain.card.CardFactory; -import blackjack.domain.card.Deck; +import blackjack.domain.deck.Deck; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/blackjack/domain/user/PlayerTest.java b/src/test/java/blackjack/domain/user/PlayerTest.java new file mode 100644 index 0000000000..7addd2b7ef --- /dev/null +++ b/src/test/java/blackjack/domain/user/PlayerTest.java @@ -0,0 +1,57 @@ +package blackjack.domain.user; + +import blackjack.domain.card.Card; +import blackjack.domain.card.CardSymbol; +import blackjack.domain.card.CardType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +class PlayerTest { + + private Player player; + + @BeforeEach + void setUp() { + player = new Player("엘리"); + } + + @Test + @DisplayName("플레이어는 이름을 입력받아 생성") + void player() { + assertThat(player.getName()).isEqualTo("엘리"); + } + + @ParameterizedTest + @MethodSource("createCards") + @DisplayName("플레이어는 가지고 있는 카드의 합을 구할 수 있다") + void calculateSum(List cards, int result) { + for (Card card : cards) { + player.add(card); + } + assertThat(player.calculateSum()).isEqualTo(result); + } + + private static Stream createCards() { + Card aceSpade = new Card(CardSymbol.ACE, CardType.SPADE); + Card fiveClover = new Card(CardSymbol.FIVE, CardType.CLOVER); + Card queenClover = new Card(CardSymbol.QUEEN, CardType.CLOVER); + Card kingClover = new Card(CardSymbol.KING, CardType.CLOVER); + List cardSumSixteen = Arrays.asList(aceSpade, fiveClover); + List cardSumTwentyOne = Arrays.asList(aceSpade, queenClover, kingClover); + List cardSumThirteen = Arrays.asList(aceSpade, aceSpade, aceSpade); + return Stream.of( + Arguments.of(cardSumSixteen, 16), + Arguments.of(cardSumTwentyOne, 21), + Arguments.of(cardSumThirteen, 13)); + } +} \ No newline at end of file From c848f7c220a97c2a55ff7276bd549871a85381c4 Mon Sep 17 00:00:00 2001 From: YebinK Date: Wed, 11 Mar 2020 13:53:39 +0900 Subject: [PATCH 07/19] =?UTF-8?q?[feature]=20Dealer=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dealer의 카드 합이 16 이상인지 체크하는 메소드 구현 --- .../java/blackjack/domain/user/Dealer.java | 43 +++++++++++++++++++ .../java/blackjack/domain/card/CardTest.java | 2 +- .../blackjack/domain/user/DealerTest.java | 30 +++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/domain/user/Dealer.java create mode 100644 src/test/java/blackjack/domain/user/DealerTest.java diff --git a/src/main/java/blackjack/domain/user/Dealer.java b/src/main/java/blackjack/domain/user/Dealer.java new file mode 100644 index 0000000000..2261ff01ca --- /dev/null +++ b/src/main/java/blackjack/domain/user/Dealer.java @@ -0,0 +1,43 @@ +package blackjack.domain.user; + +import blackjack.domain.card.Card; + +import java.util.ArrayList; +import java.util.List; + +public class Dealer { + + private static final int BURSTED_THRESHOLD = 21; + private static final int ACE_NUMBER_GAP = 10; + public static final int DRAW_THRESHOLD = 16; + + private final List cards = new ArrayList<>(); + + public void add(Card card) { + cards.add(card); + } + + public boolean shouldDrawCard() { + return calculateSum() <= DRAW_THRESHOLD; + } + + public int calculateSum() { + int result = sumAll(); + return subtractIfContainingAce(result); + } + + private int sumAll() { + return cards.stream() + .mapToInt(Card::getNumber) + .sum(); + } + + private int subtractIfContainingAce(int result) { + for (Card card : cards) { + if (result > BURSTED_THRESHOLD && card.isAce()) { + result -= ACE_NUMBER_GAP; + } + } + return result; + } +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/card/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java index 070c16b559..fc437ef961 100644 --- a/src/test/java/blackjack/domain/card/CardTest.java +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -13,4 +13,4 @@ void cardTest() { Card card = new Card(CardSymbol.ACE, CardType.SPADE); assertThat(card.toString()).isEqualTo("A스페이드"); } -} +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/user/DealerTest.java b/src/test/java/blackjack/domain/user/DealerTest.java new file mode 100644 index 0000000000..4f550f1122 --- /dev/null +++ b/src/test/java/blackjack/domain/user/DealerTest.java @@ -0,0 +1,30 @@ +package blackjack.domain.user; + +import blackjack.domain.card.Card; +import blackjack.domain.card.CardSymbol; +import blackjack.domain.card.CardType; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DealerTest { + + @Test + @DisplayName("딜러가 갖고 있는 카드의 합이 16 이하인지 확인") + void shouldReceiveCard1() { + Dealer dealer = new Dealer(); + dealer.add(new Card(CardSymbol.ACE, CardType.SPADE)); + assertThat(dealer.shouldDrawCard()).isTrue(); + } + + @Test + @DisplayName("딜러가 갖고 있는 카드의 합이 16 이하인지 확인") + void shouldReceiveCard2() { + Dealer dealer = new Dealer(); + dealer.add(new Card(CardSymbol.ACE, CardType.SPADE)); + dealer.add(new Card(CardSymbol.TEN, CardType.SPADE)); + dealer.add(new Card(CardSymbol.QUEEN, CardType.SPADE)); + assertThat(dealer.shouldDrawCard()).isFalse(); + } +} \ No newline at end of file From b9a9d51b189999fc05c9d9e9a50f731a55c73972 Mon Sep 17 00:00:00 2001 From: "jaeju.jang" Date: Wed, 11 Mar 2020 15:45:26 +0900 Subject: [PATCH 08/19] =?UTF-8?q?[feature/refactoring]=20Gamer=20=EC=B6=94?= =?UTF-8?q?=EC=83=81=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Dealer와 Player가 Gamer 상속 - 카드의 합을 구하는 기능을 CardCalculator로 이동 - Gamer에서 bust 됐는지 확인 --- README.md | 4 +- src/main/java/blackjack/domain/deck/Deck.java | 2 - .../java/blackjack/domain/gamer/Dealer.java | 16 ++++++ .../java/blackjack/domain/gamer/Gamer.java | 28 ++++++++++ .../java/blackjack/domain/gamer/Player.java | 14 +++++ .../blackjack/domain/rule/CardCalculator.java | 31 +++++++++++ .../java/blackjack/domain/user/Dealer.java | 43 --------------- .../java/blackjack/domain/user/Player.java | 48 ---------------- .../java/blackjack/domain/deck/DeckTest.java | 1 - .../blackjack/domain/gamer/DealerTest.java | 54 ++++++++++++++++++ .../blackjack/domain/gamer/PlayerTest.java | 55 +++++++++++++++++++ .../CardCalculatorTest.java} | 26 ++------- .../blackjack/domain/user/DealerTest.java | 30 ---------- 13 files changed, 204 insertions(+), 148 deletions(-) create mode 100644 src/main/java/blackjack/domain/gamer/Dealer.java create mode 100644 src/main/java/blackjack/domain/gamer/Gamer.java create mode 100644 src/main/java/blackjack/domain/gamer/Player.java create mode 100644 src/main/java/blackjack/domain/rule/CardCalculator.java delete mode 100644 src/main/java/blackjack/domain/user/Dealer.java delete mode 100644 src/main/java/blackjack/domain/user/Player.java create mode 100644 src/test/java/blackjack/domain/gamer/DealerTest.java create mode 100644 src/test/java/blackjack/domain/gamer/PlayerTest.java rename src/test/java/blackjack/domain/{user/PlayerTest.java => rule/CardCalculatorTest.java} (68%) delete mode 100644 src/test/java/blackjack/domain/user/DealerTest.java diff --git a/README.md b/README.md index 89877db362..a435c9deae 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,10 @@ ## 도메인 기능 요구사항 - [ ] 딜러가 게임 참가자들에게 카드를 2장씩 나눈다. -- [ ] 딜러와 플레이어들은 카드를 가질 수 있다. +- [x] 딜러와 플레이어들은 카드를 가질 수 있다. - [x] 각 플레이어들의 카드 합을 구한다. - [x] Ace는 1 또는 11로 계산하고, King, Queen, Jack은 10으로 계산한다. -- [ ] 플레이어들의 카드 합이 21을 넘는지 확인한다. +- [x] 플레이어들의 카드 합이 21을 넘는지 확인한다. - [ ] 딜러의 카드 합이 16 이하인 경우 카드를 계속해서 더 받는다. ## 입출력 기능 요구사항 diff --git a/src/main/java/blackjack/domain/deck/Deck.java b/src/main/java/blackjack/domain/deck/Deck.java index aad4bb9936..a95a69e3a2 100644 --- a/src/main/java/blackjack/domain/deck/Deck.java +++ b/src/main/java/blackjack/domain/deck/Deck.java @@ -11,13 +11,11 @@ public class Deck { private final Stack cards = new Stack<>(); public Deck(List cards) { - Collections.shuffle(cards); this.cards.addAll(cards); } public Card pick() { - if (cards.empty()) { throw new NullPointerException("카드를 모두 사용하셨습니다."); } diff --git a/src/main/java/blackjack/domain/gamer/Dealer.java b/src/main/java/blackjack/domain/gamer/Dealer.java new file mode 100644 index 0000000000..043e9bed20 --- /dev/null +++ b/src/main/java/blackjack/domain/gamer/Dealer.java @@ -0,0 +1,16 @@ +package blackjack.domain.gamer; + +public class Dealer extends Gamer { + + private static final String DEALER_NAME = "딜러"; + private static final int DRAW_THRESHOLD = 16; + + public boolean shouldDrawCard() { + return calculateSum() <= DRAW_THRESHOLD; + } + + @Override + public String getName() { + return DEALER_NAME; + } +} \ No newline at end of file diff --git a/src/main/java/blackjack/domain/gamer/Gamer.java b/src/main/java/blackjack/domain/gamer/Gamer.java new file mode 100644 index 0000000000..d2dd1424b2 --- /dev/null +++ b/src/main/java/blackjack/domain/gamer/Gamer.java @@ -0,0 +1,28 @@ +package blackjack.domain.gamer; + +import blackjack.domain.card.Card; +import blackjack.domain.rule.CardCalculator; + +import java.util.ArrayList; +import java.util.List; + +public abstract class Gamer { + + private List cards = new ArrayList<>(); + + public void add(Card card) { + cards.add(card); + } + + public boolean isBusted() { + return calculateSum() > CardCalculator.BUST_THRESHOLD; + } + + public int calculateSum() { + return CardCalculator.calculate(cards); + } + + + public abstract String getName(); + +} diff --git a/src/main/java/blackjack/domain/gamer/Player.java b/src/main/java/blackjack/domain/gamer/Player.java new file mode 100644 index 0000000000..a9e583afe4 --- /dev/null +++ b/src/main/java/blackjack/domain/gamer/Player.java @@ -0,0 +1,14 @@ +package blackjack.domain.gamer; + +public class Player extends Gamer { + + private final String name; + + public Player(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/src/main/java/blackjack/domain/rule/CardCalculator.java b/src/main/java/blackjack/domain/rule/CardCalculator.java new file mode 100644 index 0000000000..46c59207d5 --- /dev/null +++ b/src/main/java/blackjack/domain/rule/CardCalculator.java @@ -0,0 +1,31 @@ +package blackjack.domain.rule; + +import blackjack.domain.card.Card; + +import java.util.List; + +public class CardCalculator { + + public static final int BUST_THRESHOLD = 21; + private static final int ACE_NUMBER_GAP = 10; + + public static int calculate(List cards) { + int result = sumAll(cards); + return subtractIfContainingAce(cards, result); + } + + private static int sumAll(List cards) { + return cards.stream() + .mapToInt(Card::getNumber) + .sum(); + } + + private static int subtractIfContainingAce(List cards, int result) { + for (Card card : cards) { + if (result > BUST_THRESHOLD && card.isAce()) { + result -= ACE_NUMBER_GAP; + } + } + return result; + } +} diff --git a/src/main/java/blackjack/domain/user/Dealer.java b/src/main/java/blackjack/domain/user/Dealer.java deleted file mode 100644 index 2261ff01ca..0000000000 --- a/src/main/java/blackjack/domain/user/Dealer.java +++ /dev/null @@ -1,43 +0,0 @@ -package blackjack.domain.user; - -import blackjack.domain.card.Card; - -import java.util.ArrayList; -import java.util.List; - -public class Dealer { - - private static final int BURSTED_THRESHOLD = 21; - private static final int ACE_NUMBER_GAP = 10; - public static final int DRAW_THRESHOLD = 16; - - private final List cards = new ArrayList<>(); - - public void add(Card card) { - cards.add(card); - } - - public boolean shouldDrawCard() { - return calculateSum() <= DRAW_THRESHOLD; - } - - public int calculateSum() { - int result = sumAll(); - return subtractIfContainingAce(result); - } - - private int sumAll() { - return cards.stream() - .mapToInt(Card::getNumber) - .sum(); - } - - private int subtractIfContainingAce(int result) { - for (Card card : cards) { - if (result > BURSTED_THRESHOLD && card.isAce()) { - result -= ACE_NUMBER_GAP; - } - } - return result; - } -} \ No newline at end of file diff --git a/src/main/java/blackjack/domain/user/Player.java b/src/main/java/blackjack/domain/user/Player.java deleted file mode 100644 index 260001043b..0000000000 --- a/src/main/java/blackjack/domain/user/Player.java +++ /dev/null @@ -1,48 +0,0 @@ -package blackjack.domain.user; - -import blackjack.domain.card.Card; - -import java.util.ArrayList; -import java.util.List; - -public class Player { - - private static final int BURSTED_THRESHOLD = 21; - private static final int ACE_NUMBER_GAP = 10; - - private final List cards = new ArrayList<>(); - private final String name; - - public Player(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void add(Card card) { - cards.add(card); - } - - public int calculateSum() { - int result = sumAll(); - return subtractIfContainingAce(result); - } - - private int sumAll() { - return cards.stream() - .mapToInt(Card::getNumber) - .sum(); - } - - private int subtractIfContainingAce(int result) { - for (Card card : cards) { - if (result > BURSTED_THRESHOLD && card.isAce()) { - result -= ACE_NUMBER_GAP; - } - } - return result; - } - -} diff --git a/src/test/java/blackjack/domain/deck/DeckTest.java b/src/test/java/blackjack/domain/deck/DeckTest.java index 7934c770fc..8c49c93950 100644 --- a/src/test/java/blackjack/domain/deck/DeckTest.java +++ b/src/test/java/blackjack/domain/deck/DeckTest.java @@ -2,7 +2,6 @@ import blackjack.domain.card.Card; import blackjack.domain.card.CardFactory; -import blackjack.domain.deck.Deck; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/blackjack/domain/gamer/DealerTest.java b/src/test/java/blackjack/domain/gamer/DealerTest.java new file mode 100644 index 0000000000..ab0c9a95b0 --- /dev/null +++ b/src/test/java/blackjack/domain/gamer/DealerTest.java @@ -0,0 +1,54 @@ +package blackjack.domain.gamer; + +import blackjack.domain.card.Card; +import blackjack.domain.card.CardSymbol; +import blackjack.domain.card.CardType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DealerTest { + + private Dealer dealer; + + @BeforeEach + void setUp() { + dealer = new Dealer(); + } + + @Test + void getDealerName() { + assertThat(dealer.getName()).isEqualTo("딜러"); + } + + @ParameterizedTest + @MethodSource("createCards") + @DisplayName("딜러가 갖고 있는 카드의 합이 16 이하인지 확인") + void shouldReceiveCard1(List cards, boolean shouldDrawCard) { + for (Card card : cards) { + dealer.add(card); + } + assertThat(dealer.shouldDrawCard()).isEqualTo(shouldDrawCard); + } + + private static Stream createCards() { + Card aceSpade = new Card(CardSymbol.ACE, CardType.SPADE); + Card queenClover = new Card(CardSymbol.QUEEN, CardType.CLOVER); + Card kingClover = new Card(CardSymbol.KING, CardType.CLOVER); + List cardSumUnderSixteen = Arrays.asList(aceSpade); + List cardSumOverSixteen = Arrays.asList(aceSpade, queenClover, kingClover); + return Stream.of( + Arguments.of(cardSumUnderSixteen, true), + Arguments.of(cardSumOverSixteen, false)); + } + +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/gamer/PlayerTest.java b/src/test/java/blackjack/domain/gamer/PlayerTest.java new file mode 100644 index 0000000000..3079e4ba99 --- /dev/null +++ b/src/test/java/blackjack/domain/gamer/PlayerTest.java @@ -0,0 +1,55 @@ +package blackjack.domain.gamer; + +import blackjack.domain.card.Card; +import blackjack.domain.card.CardSymbol; +import blackjack.domain.card.CardType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +class PlayerTest { + + private Player player; + + @BeforeEach + void setUp() { + player = new Player("엘리"); + } + + @Test + @DisplayName("플레이어는 이름을 입력받아 생성") + void player() { + assertThat(player.getName()).isEqualTo("엘리"); + } + + @ParameterizedTest + @MethodSource("createCards") + @DisplayName("bust되었는지 확인") + void isBusted(List cards, boolean isBusted) { + for (Card card : cards) { + player.add(card); + } + + assertThat(player.isBusted()).isEqualTo(isBusted); + } + + private static Stream createCards() { + Card aceSpade = new Card(CardSymbol.ACE, CardType.SPADE); + Card queenClover = new Card(CardSymbol.QUEEN, CardType.CLOVER); + Card kingClover = new Card(CardSymbol.KING, CardType.CLOVER); + List notBustedCards = Arrays.asList(aceSpade, queenClover, kingClover); + List bustedCards = Arrays.asList(kingClover, kingClover, kingClover); + return Stream.of( + Arguments.of(notBustedCards, false), + Arguments.of(bustedCards, true)); + } +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/user/PlayerTest.java b/src/test/java/blackjack/domain/rule/CardCalculatorTest.java similarity index 68% rename from src/test/java/blackjack/domain/user/PlayerTest.java rename to src/test/java/blackjack/domain/rule/CardCalculatorTest.java index 7addd2b7ef..72156c900f 100644 --- a/src/test/java/blackjack/domain/user/PlayerTest.java +++ b/src/test/java/blackjack/domain/rule/CardCalculatorTest.java @@ -1,11 +1,9 @@ -package blackjack.domain.user; +package blackjack.domain.rule; import blackjack.domain.card.Card; import blackjack.domain.card.CardSymbol; import blackjack.domain.card.CardType; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -16,29 +14,13 @@ import static org.assertj.core.api.Assertions.assertThat; -class PlayerTest { - - private Player player; - - @BeforeEach - void setUp() { - player = new Player("엘리"); - } - - @Test - @DisplayName("플레이어는 이름을 입력받아 생성") - void player() { - assertThat(player.getName()).isEqualTo("엘리"); - } +class CardCalculatorTest { @ParameterizedTest @MethodSource("createCards") - @DisplayName("플레이어는 가지고 있는 카드의 합을 구할 수 있다") + @DisplayName("카드의 합을 구하는 테스트") void calculateSum(List cards, int result) { - for (Card card : cards) { - player.add(card); - } - assertThat(player.calculateSum()).isEqualTo(result); + assertThat(CardCalculator.calculate(cards)).isEqualTo(result); } private static Stream createCards() { diff --git a/src/test/java/blackjack/domain/user/DealerTest.java b/src/test/java/blackjack/domain/user/DealerTest.java deleted file mode 100644 index 4f550f1122..0000000000 --- a/src/test/java/blackjack/domain/user/DealerTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package blackjack.domain.user; - -import blackjack.domain.card.Card; -import blackjack.domain.card.CardSymbol; -import blackjack.domain.card.CardType; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class DealerTest { - - @Test - @DisplayName("딜러가 갖고 있는 카드의 합이 16 이하인지 확인") - void shouldReceiveCard1() { - Dealer dealer = new Dealer(); - dealer.add(new Card(CardSymbol.ACE, CardType.SPADE)); - assertThat(dealer.shouldDrawCard()).isTrue(); - } - - @Test - @DisplayName("딜러가 갖고 있는 카드의 합이 16 이하인지 확인") - void shouldReceiveCard2() { - Dealer dealer = new Dealer(); - dealer.add(new Card(CardSymbol.ACE, CardType.SPADE)); - dealer.add(new Card(CardSymbol.TEN, CardType.SPADE)); - dealer.add(new Card(CardSymbol.QUEEN, CardType.SPADE)); - assertThat(dealer.shouldDrawCard()).isFalse(); - } -} \ No newline at end of file From 2ba1665b4e4527e5e8b84d22ba655025718e1209 Mon Sep 17 00:00:00 2001 From: YebinK Date: Thu, 12 Mar 2020 10:35:12 +0900 Subject: [PATCH 09/19] =?UTF-8?q?[feature]=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=20=EC=9D=B4=EB=A6=84=20=EC=9E=85=EB=A0=A5,=20?= =?UTF-8?q?=EB=B6=84=EB=B0=B0=EB=90=9C=20=EC=B9=B4=EB=93=9C=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../java/blackjack/BlackJackApplication.java | 49 +++++++++++++++++++ .../java/blackjack/domain/gamer/Dealer.java | 5 ++ .../java/blackjack/domain/gamer/Gamer.java | 9 +++- .../java/blackjack/domain/gamer/Player.java | 2 + src/main/java/blackjack/view/InputView.java | 12 +++++ src/main/java/blackjack/view/OutputView.java | 28 +++++++++++ 7 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 src/main/java/blackjack/BlackJackApplication.java create mode 100644 src/main/java/blackjack/view/InputView.java create mode 100644 src/main/java/blackjack/view/OutputView.java diff --git a/README.md b/README.md index a435c9deae..a106ae666c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ 블랙잭 게임 미션 저장소 ## 도메인 기능 요구사항 -- [ ] 딜러가 게임 참가자들에게 카드를 2장씩 나눈다. +- [x] 딜러가 게임 참가자들에게 카드를 2장씩 나눈다. - [x] 딜러와 플레이어들은 카드를 가질 수 있다. - [x] 각 플레이어들의 카드 합을 구한다. - [x] Ace는 1 또는 11로 계산하고, King, Queen, Jack은 10으로 계산한다. diff --git a/src/main/java/blackjack/BlackJackApplication.java b/src/main/java/blackjack/BlackJackApplication.java new file mode 100644 index 0000000000..78cf52d156 --- /dev/null +++ b/src/main/java/blackjack/BlackJackApplication.java @@ -0,0 +1,49 @@ +package blackjack; + +import blackjack.domain.card.CardFactory; +import blackjack.domain.deck.Deck; +import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Player; +import blackjack.view.InputView; +import blackjack.view.OutputView; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class BlackJackApplication { + + public static void main(String[] args) { + Deck deck = new Deck(CardFactory.generate()); + Dealer dealer = new Dealer(); + List players = initializePlayers(InputView.askPlayerNames()); + + drawInitialCards(dealer, players, deck); + + OutputView.printInitialCards(dealer, players); + + while (dealer.shouldDrawCard()) { + dealer.add(deck.pick()); + } + + } + + private static void drawInitialCards(Dealer dealer, List players, Deck deck) { + for (int i = 0; i < 2; i++) { + drawCards(dealer, players, deck); + } + } + + private static void drawCards(Dealer dealer, List players, Deck deck) { + dealer.add(deck.pick()); + for (Player player : players) { + player.add(deck.pick()); + } + } + + private static List initializePlayers(String names) { + return Arrays.stream(names.split(",")) + .map(Player::new) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/blackjack/domain/gamer/Dealer.java b/src/main/java/blackjack/domain/gamer/Dealer.java index 043e9bed20..5f8ad16f06 100644 --- a/src/main/java/blackjack/domain/gamer/Dealer.java +++ b/src/main/java/blackjack/domain/gamer/Dealer.java @@ -4,6 +4,7 @@ public class Dealer extends Gamer { private static final String DEALER_NAME = "딜러"; private static final int DRAW_THRESHOLD = 16; + public static final int FIRST_CARD_INDEX = 0; public boolean shouldDrawCard() { return calculateSum() <= DRAW_THRESHOLD; @@ -13,4 +14,8 @@ public boolean shouldDrawCard() { public String getName() { return DEALER_NAME; } + + public String getInitialCardStatus() { + return cards.get(FIRST_CARD_INDEX).toString(); + } } \ No newline at end of file diff --git a/src/main/java/blackjack/domain/gamer/Gamer.java b/src/main/java/blackjack/domain/gamer/Gamer.java index d2dd1424b2..de290763bc 100644 --- a/src/main/java/blackjack/domain/gamer/Gamer.java +++ b/src/main/java/blackjack/domain/gamer/Gamer.java @@ -5,10 +5,11 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public abstract class Gamer { - private List cards = new ArrayList<>(); + protected List cards = new ArrayList<>(); public void add(Card card) { cards.add(card); @@ -22,7 +23,11 @@ public int calculateSum() { return CardCalculator.calculate(cards); } - public abstract String getName(); + public String getCardStatus() { + return cards.stream() + .map(Card::toString) + .collect(Collectors.joining(",")); + } } diff --git a/src/main/java/blackjack/domain/gamer/Player.java b/src/main/java/blackjack/domain/gamer/Player.java index a9e583afe4..81df5cdc96 100644 --- a/src/main/java/blackjack/domain/gamer/Player.java +++ b/src/main/java/blackjack/domain/gamer/Player.java @@ -11,4 +11,6 @@ public Player(String name) { public String getName() { return name; } + + } diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java new file mode 100644 index 0000000000..95d8cd4b9f --- /dev/null +++ b/src/main/java/blackjack/view/InputView.java @@ -0,0 +1,12 @@ +package blackjack.view; + +import java.util.Scanner; + +public class InputView { + private static final Scanner scanner = new Scanner(System.in); + + public static String askPlayerNames() { + System.out.println("게임에 참여할 사람의 이름을 입력하세요. (쉼표 기준으로 분리)"); + return scanner.nextLine(); + } +} diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java new file mode 100644 index 0000000000..9c7debccd9 --- /dev/null +++ b/src/main/java/blackjack/view/OutputView.java @@ -0,0 +1,28 @@ +package blackjack.view; + +import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Player; + +import java.util.List; +import java.util.stream.Collectors; + +public class OutputView { + + public static final String INITIAL_CARD_MESSAGE = "%s와 %s에게 2장의 카드를 나누었습니다."; + public static final String CARD_STATUS_FORMAT = "%s 카드 : %s"; + + public static void printInitialCards(Dealer dealer, List players) { + String names = parseName(players); + System.out.println(String.format(INITIAL_CARD_MESSAGE, dealer.getName(), names)); + System.out.println(String.format(CARD_STATUS_FORMAT, dealer.getName(), dealer.getInitialCardStatus())); + for (Player player : players) { + System.out.println(String.format(CARD_STATUS_FORMAT, player.getName(), player.getCardStatus())); + } + } + + private static String parseName(List players) { + List names = players.stream().map(Player::getName).collect(Collectors.toList()); + return String.join(",", names); + } +} + From bb8eeeebc111fd0d8f828b14e2c68e5d703b9d44 Mon Sep 17 00:00:00 2001 From: "jaeju.jang" Date: Thu, 12 Mar 2020 11:28:19 +0900 Subject: [PATCH 10/19] =?UTF-8?q?[feature]=20=EB=94=9C=EB=9F=AC=EC=99=80?= =?UTF-8?q?=20=ED=94=8C=EB=A0=88=EC=9D=B4=EC=96=B4=EA=B0=80=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EB=8D=94=20=EB=B0=9B=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 딜러가 16이하인 경우 카드를 계속해서 더 받음 - 플레이어는 bust되지 않은 경우 카드를 더 받을지 물어보고 카드를 받음 --- README.md | 10 +++--- .../java/blackjack/BlackJackApplication.java | 32 +++++++++++++++++-- .../blackjack/domain/rule/PlayerAnswer.java | 18 +++++++++++ src/main/java/blackjack/view/InputView.java | 14 ++++++-- src/main/java/blackjack/view/OutputView.java | 15 +++++++-- 5 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 src/main/java/blackjack/domain/rule/PlayerAnswer.java diff --git a/README.md b/README.md index a106ae666c..23ca1e8de7 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,13 @@ - [x] 각 플레이어들의 카드 합을 구한다. - [x] Ace는 1 또는 11로 계산하고, King, Queen, Jack은 10으로 계산한다. - [x] 플레이어들의 카드 합이 21을 넘는지 확인한다. -- [ ] 딜러의 카드 합이 16 이하인 경우 카드를 계속해서 더 받는다. +- [x] 딜러의 카드 합이 16 이하인 경우 카드를 계속해서 더 받는다. ## 입출력 기능 요구사항 -- [ ] 게임에 참여할 사람의 이름을 입력받는다. +- [x] 게임에 참여할 사람의 이름을 입력받는다. - [ ] (예외) 공백이 입력될 경우 -- [ ] 딜러와 플레이어의 카드를 출력한다. - - [ ] 딜러는 2장 중 1장만 출력한다. -- [ ] 플레이어들을 순회하며 카드를 더 받을지 물어본다. +- [x] 딜러와 플레이어의 카드를 출력한다. + - [x] 딜러는 2장 중 1장만 출력한다. +- [x] 플레이어들을 순회하며 카드를 더 받을지 물어본다. - [ ] 딜러와 플레이어들의 카드 목록과 합을 출력한다. - [ ] 최종 승패를 출력한다. \ No newline at end of file diff --git a/src/main/java/blackjack/BlackJackApplication.java b/src/main/java/blackjack/BlackJackApplication.java index 78cf52d156..293a829dbd 100644 --- a/src/main/java/blackjack/BlackJackApplication.java +++ b/src/main/java/blackjack/BlackJackApplication.java @@ -4,6 +4,7 @@ import blackjack.domain.deck.Deck; import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Player; +import blackjack.domain.rule.PlayerAnswer; import blackjack.view.InputView; import blackjack.view.OutputView; @@ -19,13 +20,40 @@ public static void main(String[] args) { List players = initializePlayers(InputView.askPlayerNames()); drawInitialCards(dealer, players, deck); - OutputView.printInitialCards(dealer, players); + drawMoreCard(dealer, players, deck); + + } - while (dealer.shouldDrawCard()) { + private static void drawMoreCard(Dealer dealer, List players, Deck deck) { + hitOrStandForPlayers(players, deck); + drawCardForDealer(dealer, deck); + } + + private static void drawCardForDealer(Dealer dealer, Deck deck) { + while(!dealer.shouldDrawCard()) { dealer.add(deck.pick()); + OutputView.printDealerDrewACard(); } + } + private static void hitOrStandForPlayers(List players, Deck deck) { + for (Player player : players) { + hitOrStand(player, deck); + } + } + + private static void hitOrStand(Player player, Deck deck) { + while(!player.isBusted()) { + PlayerAnswer answer = PlayerAnswer.of(InputView.askHitOrStand(player.getName())); + if (answer.isYes()) { + player.add(deck.pick()); + } + OutputView.printPlayerCard(player); + if (!answer.isYes()) { + break; + } + } } private static void drawInitialCards(Dealer dealer, List players, Deck deck) { diff --git a/src/main/java/blackjack/domain/rule/PlayerAnswer.java b/src/main/java/blackjack/domain/rule/PlayerAnswer.java new file mode 100644 index 0000000000..3d5d175731 --- /dev/null +++ b/src/main/java/blackjack/domain/rule/PlayerAnswer.java @@ -0,0 +1,18 @@ +package blackjack.domain.rule; + +import java.util.Arrays; + +public enum PlayerAnswer { + + Y, + N; + + public static PlayerAnswer of(String answer) { + return valueOf(answer.toUpperCase()); + } + + public boolean isYes() { + return this == Y; + } +} + diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index 95d8cd4b9f..0b4b88d9bb 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -3,10 +3,18 @@ import java.util.Scanner; public class InputView { - private static final Scanner scanner = new Scanner(System.in); + + private static final Scanner SCANNER = new Scanner(System.in); + private static final String ASK_PLAYER_NAMES_MESSAGE = "게임에 참여할 사람의 이름을 입력하세요. (쉼표 기준으로 분리)"; + private static final String ASK_HIT_OR_STAND_MESSAGE = "%s는 한 장의 카드를 더 받겠습니까?(에는 y, 아니오는 n)"; public static String askPlayerNames() { - System.out.println("게임에 참여할 사람의 이름을 입력하세요. (쉼표 기준으로 분리)"); - return scanner.nextLine(); + System.out.println(ASK_PLAYER_NAMES_MESSAGE); + return SCANNER.nextLine(); + } + + public static String askHitOrStand(String name) { + System.out.println(String.format(ASK_HIT_OR_STAND_MESSAGE, name)); + return SCANNER.nextLine(); } } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 9c7debccd9..a1693b9d6b 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -8,21 +8,30 @@ public class OutputView { - public static final String INITIAL_CARD_MESSAGE = "%s와 %s에게 2장의 카드를 나누었습니다."; - public static final String CARD_STATUS_FORMAT = "%s 카드 : %s"; + private static final String INITIAL_CARD_MESSAGE = "%s와 %s에게 2장의 카드를 나누었습니다."; + private static final String CARD_STATUS_FORMAT = "%s 카드 : %s"; + public static final String DEALER_DREW_A_CARD_MESSAGE = "딜러는 16이하라 1장의 카드를 더 받았습니다."; public static void printInitialCards(Dealer dealer, List players) { String names = parseName(players); System.out.println(String.format(INITIAL_CARD_MESSAGE, dealer.getName(), names)); System.out.println(String.format(CARD_STATUS_FORMAT, dealer.getName(), dealer.getInitialCardStatus())); for (Player player : players) { - System.out.println(String.format(CARD_STATUS_FORMAT, player.getName(), player.getCardStatus())); + printPlayerCard(player); } } + public static void printPlayerCard(Player player) { + System.out.println(String.format(CARD_STATUS_FORMAT, player.getName(), player.getCardStatus())); + } + private static String parseName(List players) { List names = players.stream().map(Player::getName).collect(Collectors.toList()); return String.join(",", names); } + + public static void printDealerDrewACard() { + System.out.println(DEALER_DREW_A_CARD_MESSAGE); + } } From a2c79773a0ea8de56e6d291562377eb96311050f Mon Sep 17 00:00:00 2001 From: YebinK Date: Thu, 12 Mar 2020 15:00:43 +0900 Subject: [PATCH 11/19] =?UTF-8?q?[feature]=20=EA=B2=B0=EA=B3=BC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 딜러와 플레이어들 간 승부 구하는 PlayerResultMatcher 클래스 구현 - 딜러/플레이어 각각 결과 출력 --- .../java/blackjack/BlackJackApplication.java | 7 ++- .../java/blackjack/domain/gamer/Gamer.java | 2 +- .../java/blackjack/domain/gamer/Player.java | 4 +- .../domain/result/BlackJackResult.java | 26 +++++++++ .../blackjack/domain/result/GamersResult.java | 26 +++++++++ .../domain/result/PlayerResultMatcher.java | 44 +++++++++++++++ src/main/java/blackjack/view/OutputView.java | 53 +++++++++++++++++-- .../result/PlayerResultMatcherTest.java | 29 ++++++++++ 8 files changed, 183 insertions(+), 8 deletions(-) create mode 100644 src/main/java/blackjack/domain/result/BlackJackResult.java create mode 100644 src/main/java/blackjack/domain/result/GamersResult.java create mode 100644 src/main/java/blackjack/domain/result/PlayerResultMatcher.java create mode 100644 src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java diff --git a/src/main/java/blackjack/BlackJackApplication.java b/src/main/java/blackjack/BlackJackApplication.java index 293a829dbd..3d52df69bd 100644 --- a/src/main/java/blackjack/BlackJackApplication.java +++ b/src/main/java/blackjack/BlackJackApplication.java @@ -4,6 +4,8 @@ import blackjack.domain.deck.Deck; import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Player; +import blackjack.domain.result.GamersResult; +import blackjack.domain.result.PlayerResultMatcher; import blackjack.domain.rule.PlayerAnswer; import blackjack.view.InputView; import blackjack.view.OutputView; @@ -23,6 +25,9 @@ public static void main(String[] args) { OutputView.printInitialCards(dealer, players); drawMoreCard(dealer, players, deck); + GamersResult gamersResult = PlayerResultMatcher.report(dealer, players); + OutputView.printGamerScore(dealer, players); + OutputView.printResult(gamersResult); } private static void drawMoreCard(Dealer dealer, List players, Deck deck) { @@ -31,7 +36,7 @@ private static void drawMoreCard(Dealer dealer, List players, Deck deck) } private static void drawCardForDealer(Dealer dealer, Deck deck) { - while(!dealer.shouldDrawCard()) { + while(dealer.shouldDrawCard()) { dealer.add(deck.pick()); OutputView.printDealerDrewACard(); } diff --git a/src/main/java/blackjack/domain/gamer/Gamer.java b/src/main/java/blackjack/domain/gamer/Gamer.java index de290763bc..6bee0b9806 100644 --- a/src/main/java/blackjack/domain/gamer/Gamer.java +++ b/src/main/java/blackjack/domain/gamer/Gamer.java @@ -28,6 +28,6 @@ public int calculateSum() { public String getCardStatus() { return cards.stream() .map(Card::toString) - .collect(Collectors.joining(",")); + .collect(Collectors.joining(", ")); } } diff --git a/src/main/java/blackjack/domain/gamer/Player.java b/src/main/java/blackjack/domain/gamer/Player.java index 81df5cdc96..3b2114d30a 100644 --- a/src/main/java/blackjack/domain/gamer/Player.java +++ b/src/main/java/blackjack/domain/gamer/Player.java @@ -11,6 +11,4 @@ public Player(String name) { public String getName() { return name; } - - -} +} \ No newline at end of file diff --git a/src/main/java/blackjack/domain/result/BlackJackResult.java b/src/main/java/blackjack/domain/result/BlackJackResult.java new file mode 100644 index 0000000000..37aea7e160 --- /dev/null +++ b/src/main/java/blackjack/domain/result/BlackJackResult.java @@ -0,0 +1,26 @@ +package blackjack.domain.result; + +public enum BlackJackResult { + + WIN("승"), + DRAW("무"), + LOSE("패"); + + private final String koreanName; + + BlackJackResult(String koreanName) { + this.koreanName = koreanName; + } + + public BlackJackResult reversed() { + if (this == WIN) + return LOSE; + if (this == LOSE) + return WIN; + return DRAW; + } + + public String getKoreanName() { + return koreanName; + } +} \ No newline at end of file diff --git a/src/main/java/blackjack/domain/result/GamersResult.java b/src/main/java/blackjack/domain/result/GamersResult.java new file mode 100644 index 0000000000..e1b8b895ae --- /dev/null +++ b/src/main/java/blackjack/domain/result/GamersResult.java @@ -0,0 +1,26 @@ +package blackjack.domain.result; + +import blackjack.domain.gamer.Player; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class GamersResult { + + private final Map dealerResult; + private final Map playersResult; + + public GamersResult(Map dealerResult, Map playersResult) { + this.dealerResult = Collections.unmodifiableMap(dealerResult); + this.playersResult = Collections.unmodifiableMap(playersResult); + } + + public Map getDealerResult() { + return dealerResult; + } + + public Map getPlayersResult() { + return playersResult; + } +} \ No newline at end of file diff --git a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java new file mode 100644 index 0000000000..9c958b5fbc --- /dev/null +++ b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java @@ -0,0 +1,44 @@ +package blackjack.domain.result; + +import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Player; + +import java.util.*; + +public class PlayerResultMatcher { + + public static GamersResult report(Dealer dealer, List players) { + EnumMap dealerResult = new EnumMap<>(BlackJackResult.class); + Arrays.stream(BlackJackResult.values()) + .forEach(blackJackResult -> dealerResult.put(blackJackResult, 0)); + + Map playersResult = new HashMap<>(); + + for (Player player : players) { + BlackJackResult result = match(dealer, player); + playersResult.put(player, result); + dealerResult.computeIfPresent(result.reversed(), (key, value) -> ++value); + } + + return new GamersResult(dealerResult, playersResult); + } + + public static BlackJackResult match(Dealer dealer, Player player) { + if (!player.isBusted()) { + if (!dealer.isBusted()) { + return findWinner(dealer, player); + } + return BlackJackResult.WIN; + } + return BlackJackResult.LOSE; + } + + private static BlackJackResult findWinner(Dealer dealer, Player player) { + if (dealer.calculateSum() < player.calculateSum()) + return BlackJackResult.WIN; + if (dealer.calculateSum() > player.calculateSum()) + return BlackJackResult.LOSE; + return BlackJackResult.DRAW; + } + +} diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index a1693b9d6b..ee431e08de 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,24 +1,33 @@ package blackjack.view; import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Gamer; import blackjack.domain.gamer.Player; +import blackjack.domain.result.BlackJackResult; +import blackjack.domain.result.GamersResult; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class OutputView { - private static final String INITIAL_CARD_MESSAGE = "%s와 %s에게 2장의 카드를 나누었습니다."; + private static final String INITIAL_CARD_FORMAT = "%s와 %s에게 2장의 카드를 나누었습니다."; private static final String CARD_STATUS_FORMAT = "%s 카드 : %s"; - public static final String DEALER_DREW_A_CARD_MESSAGE = "딜러는 16이하라 1장의 카드를 더 받았습니다."; + private static final String DEALER_DREW_A_CARD_MESSAGE = "딜러는 16이하라 1장의 카드를 더 받았습니다."; + private static final String CARD_RESULT_FORMAT = "%s 카드 : %s - 결과 : %d"; + private static final String RESULT_MESSAGE = "## 최종 승패"; + private static final String GAMERS_RESULT_FORMAT = "%s : %s"; public static void printInitialCards(Dealer dealer, List players) { String names = parseName(players); - System.out.println(String.format(INITIAL_CARD_MESSAGE, dealer.getName(), names)); + System.out.print(System.lineSeparator()); + System.out.println(String.format(INITIAL_CARD_FORMAT, dealer.getName(), names)); System.out.println(String.format(CARD_STATUS_FORMAT, dealer.getName(), dealer.getInitialCardStatus())); for (Player player : players) { printPlayerCard(player); } + System.out.print(System.lineSeparator()); } public static void printPlayerCard(Player player) { @@ -31,7 +40,45 @@ private static String parseName(List players) { } public static void printDealerDrewACard() { + System.out.print(System.lineSeparator()); System.out.println(DEALER_DREW_A_CARD_MESSAGE); } + + public static void printGamerScore(Dealer dealer, List players) { + System.out.println(System.lineSeparator()); + printCardResult(dealer); + for (Player player : players) { + printCardResult(player); + } + } + + private static void printCardResult(Gamer gamer) { + System.out.println(String.format(CARD_RESULT_FORMAT, gamer.getName(), gamer.getCardStatus(), gamer.calculateSum())); + } + + public static void printResult(GamersResult gamersResult) { + System.out.print(System.lineSeparator()); + System.out.println(RESULT_MESSAGE); + printDealerResult(gamersResult.getDealerResult()); + printPlayersResult(gamersResult.getPlayersResult()); + + } + + private static void printDealerResult(Map dealerResult) { + StringBuilder stringBuilder = new StringBuilder(); + for (Map.Entry entry : dealerResult.entrySet()) { + if (entry.getValue() == 0) { + continue; + } + stringBuilder.append(entry.getValue() + entry.getKey().getKoreanName()); + } + System.out.println(String.format(GAMERS_RESULT_FORMAT, "딜러", stringBuilder.toString())); + } + + private static void printPlayersResult(Map playersResult) { + for (Map.Entry entry : playersResult.entrySet()) { + System.out.println(String.format(GAMERS_RESULT_FORMAT, entry.getKey().getName(), entry.getValue().getKoreanName())); + } + } } diff --git a/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java b/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java new file mode 100644 index 0000000000..6ecec5499e --- /dev/null +++ b/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java @@ -0,0 +1,29 @@ +package blackjack.domain.result; + +import blackjack.domain.card.Card; +import blackjack.domain.card.CardSymbol; +import blackjack.domain.card.CardType; +import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Player; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class PlayerResultMatcherTest { + + @Test + void name() { + Dealer dealer = new Dealer(); + dealer.add(new Card(CardSymbol.SIX, CardType.SPADE)); + dealer.add(new Card(CardSymbol.TEN, CardType.CLOVER)); + dealer.add(new Card(CardSymbol.THREE, CardType.DIAMOND)); + + Player player = new Player("b"); + player.add(new Card(CardSymbol.FIVE, CardType.DIAMOND)); + player.add(new Card(CardSymbol.FOUR, CardType.HEART)); + + BlackJackResult blackJackResult = PlayerResultMatcher.match(dealer, player); + assertThat(blackJackResult).isEqualTo(BlackJackResult.LOSE); + } +} \ No newline at end of file From 2b15a6f40458fe1f5542943a8800b2f3845e3956 Mon Sep 17 00:00:00 2001 From: "jaeju.jang" Date: Thu, 12 Mar 2020 16:16:45 +0900 Subject: [PATCH 12/19] =?UTF-8?q?[refactoring]=20=EC=A0=84=EC=B2=B4=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=A8=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - List를 Hand 일급 컬렉션으로 생성 - List를 Players 일급 컬렉션 생성 --- README.md | 6 +- .../java/blackjack/BlackJackApplication.java | 73 +++++++++---------- src/main/java/blackjack/domain/card/Hand.java | 30 ++++++++ .../java/blackjack/domain/gamer/Dealer.java | 2 +- .../java/blackjack/domain/gamer/Gamer.java | 17 ++--- .../java/blackjack/domain/gamer/Player.java | 4 +- .../java/blackjack/domain/gamer/Players.java | 35 +++++++++ .../blackjack/domain/result/GamersResult.java | 1 - .../domain/result/PlayerResultMatcher.java | 11 +-- .../blackjack/domain/rule/PlayerAnswer.java | 2 - src/main/java/blackjack/view/OutputView.java | 54 +++++++------- .../blackjack/domain/gamer/DealerTest.java | 2 +- .../blackjack/domain/gamer/PlayerTest.java | 2 +- .../result/PlayerResultMatcherTest.java | 11 ++- 14 files changed, 153 insertions(+), 97 deletions(-) create mode 100644 src/main/java/blackjack/domain/card/Hand.java create mode 100644 src/main/java/blackjack/domain/gamer/Players.java diff --git a/README.md b/README.md index 23ca1e8de7..4965fcb8e5 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ ## 입출력 기능 요구사항 - [x] 게임에 참여할 사람의 이름을 입력받는다. - - [ ] (예외) 공백이 입력될 경우 + - [x] (예외) 공백이 입력될 경우 - [x] 딜러와 플레이어의 카드를 출력한다. - [x] 딜러는 2장 중 1장만 출력한다. - [x] 플레이어들을 순회하며 카드를 더 받을지 물어본다. -- [ ] 딜러와 플레이어들의 카드 목록과 합을 출력한다. -- [ ] 최종 승패를 출력한다. \ No newline at end of file +- [x] 딜러와 플레이어들의 카드 목록과 합을 출력한다. +- [x] 최종 승패를 출력한다. \ No newline at end of file diff --git a/src/main/java/blackjack/BlackJackApplication.java b/src/main/java/blackjack/BlackJackApplication.java index 3d52df69bd..648a151d43 100644 --- a/src/main/java/blackjack/BlackJackApplication.java +++ b/src/main/java/blackjack/BlackJackApplication.java @@ -4,79 +4,72 @@ import blackjack.domain.deck.Deck; import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Player; +import blackjack.domain.gamer.Players; import blackjack.domain.result.GamersResult; import blackjack.domain.result.PlayerResultMatcher; import blackjack.domain.rule.PlayerAnswer; import blackjack.view.InputView; import blackjack.view.OutputView; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - public class BlackJackApplication { + private static final int INITIAL_HAND_COUNT = 2; + public static void main(String[] args) { Deck deck = new Deck(CardFactory.generate()); Dealer dealer = new Dealer(); - List players = initializePlayers(InputView.askPlayerNames()); + Players players = Players.ofComma(InputView.askPlayerNames()); - drawInitialCards(dealer, players, deck); + initializeHand(dealer, players, deck); OutputView.printInitialCards(dealer, players); - drawMoreCard(dealer, players, deck); - GamersResult gamersResult = PlayerResultMatcher.report(dealer, players); + drawMoreCard(dealer, players, deck); OutputView.printGamerScore(dealer, players); - OutputView.printResult(gamersResult); - } - private static void drawMoreCard(Dealer dealer, List players, Deck deck) { - hitOrStandForPlayers(players, deck); - drawCardForDealer(dealer, deck); + GamersResult gamersResult = PlayerResultMatcher.report(dealer, players); + OutputView.printGamersResult(gamersResult); } - private static void drawCardForDealer(Dealer dealer, Deck deck) { - while(dealer.shouldDrawCard()) { - dealer.add(deck.pick()); - OutputView.printDealerDrewACard(); + private static void initializeHand(Dealer dealer, Players players, Deck deck) { + for (int i = 0; i < INITIAL_HAND_COUNT; i++) { + drawCard(dealer, players, deck); } } - private static void hitOrStandForPlayers(List players, Deck deck) { + private static void drawCard(Dealer dealer, Players players, Deck deck) { + dealer.draw(deck.pick()); for (Player player : players) { - hitOrStand(player, deck); + player.draw(deck.pick()); } } - private static void hitOrStand(Player player, Deck deck) { - while(!player.isBusted()) { - PlayerAnswer answer = PlayerAnswer.of(InputView.askHitOrStand(player.getName())); - if (answer.isYes()) { - player.add(deck.pick()); - } - OutputView.printPlayerCard(player); - if (!answer.isYes()) { - break; - } + private static void drawMoreCard(Dealer dealer, Players players, Deck deck) { + hitOrStandForPlayers(players, deck); + drawCardForDealer(dealer, deck); + } + + private static void hitOrStandForPlayers(Players players, Deck deck) { + for (Player player : players) { + hitOrStand(player, deck); } } - private static void drawInitialCards(Dealer dealer, List players, Deck deck) { - for (int i = 0; i < 2; i++) { - drawCards(dealer, players, deck); + private static void drawCardForDealer(Dealer dealer, Deck deck) { + while (dealer.shouldDrawCard()) { + dealer.draw(deck.pick()); + OutputView.printDealerDrewCard(); } } - private static void drawCards(Dealer dealer, List players, Deck deck) { - dealer.add(deck.pick()); - for (Player player : players) { - player.add(deck.pick()); + private static void hitOrStand(Player player, Deck deck) { + while (!player.isBusted() && wantMoreCard(player)) { + player.draw(deck.pick()); + OutputView.printPlayerHand(player); } } - private static List initializePlayers(String names) { - return Arrays.stream(names.split(",")) - .map(Player::new) - .collect(Collectors.toList()); + private static boolean wantMoreCard(Player player) { + PlayerAnswer answer = PlayerAnswer.of(InputView.askHitOrStand(player.getName())); + return answer.isYes(); } } diff --git a/src/main/java/blackjack/domain/card/Hand.java b/src/main/java/blackjack/domain/card/Hand.java new file mode 100644 index 0000000000..4fb1dca190 --- /dev/null +++ b/src/main/java/blackjack/domain/card/Hand.java @@ -0,0 +1,30 @@ +package blackjack.domain.card; + +import blackjack.domain.rule.CardCalculator; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class Hand { + + private final List hand = new ArrayList<>(); + + public void add(Card card) { + hand.add(card); + } + + public int calculateSum() { + return CardCalculator.calculate(hand); + } + + public String getCardStatus() { + return hand.stream() + .map(Card::toString) + .collect(Collectors.joining(", ")); + } + + public List getHand() { + return hand; + } +} diff --git a/src/main/java/blackjack/domain/gamer/Dealer.java b/src/main/java/blackjack/domain/gamer/Dealer.java index 5f8ad16f06..ae840ed742 100644 --- a/src/main/java/blackjack/domain/gamer/Dealer.java +++ b/src/main/java/blackjack/domain/gamer/Dealer.java @@ -16,6 +16,6 @@ public String getName() { } public String getInitialCardStatus() { - return cards.get(FIRST_CARD_INDEX).toString(); + return hand.getHand().get(FIRST_CARD_INDEX).toString(); } } \ No newline at end of file diff --git a/src/main/java/blackjack/domain/gamer/Gamer.java b/src/main/java/blackjack/domain/gamer/Gamer.java index 6bee0b9806..b9974f2676 100644 --- a/src/main/java/blackjack/domain/gamer/Gamer.java +++ b/src/main/java/blackjack/domain/gamer/Gamer.java @@ -1,18 +1,15 @@ package blackjack.domain.gamer; import blackjack.domain.card.Card; +import blackjack.domain.card.Hand; import blackjack.domain.rule.CardCalculator; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - public abstract class Gamer { - protected List cards = new ArrayList<>(); + protected Hand hand = new Hand(); - public void add(Card card) { - cards.add(card); + public void draw(Card card) { + hand.add(card); } public boolean isBusted() { @@ -20,14 +17,12 @@ public boolean isBusted() { } public int calculateSum() { - return CardCalculator.calculate(cards); + return hand.calculateSum(); } public abstract String getName(); public String getCardStatus() { - return cards.stream() - .map(Card::toString) - .collect(Collectors.joining(", ")); + return hand.getCardStatus(); } } diff --git a/src/main/java/blackjack/domain/gamer/Player.java b/src/main/java/blackjack/domain/gamer/Player.java index 3b2114d30a..b7b3fc80c2 100644 --- a/src/main/java/blackjack/domain/gamer/Player.java +++ b/src/main/java/blackjack/domain/gamer/Player.java @@ -1,11 +1,13 @@ package blackjack.domain.gamer; +import java.util.Objects; + public class Player extends Gamer { private final String name; public Player(String name) { - this.name = name; + this.name = Objects.requireNonNull(name); } public String getName() { diff --git a/src/main/java/blackjack/domain/gamer/Players.java b/src/main/java/blackjack/domain/gamer/Players.java new file mode 100644 index 0000000000..5f52c613ba --- /dev/null +++ b/src/main/java/blackjack/domain/gamer/Players.java @@ -0,0 +1,35 @@ +package blackjack.domain.gamer; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; + +public class Players implements Iterable { + + private static final String NAME_SPLITTER = ","; + + private List players; + + private Players(List players) { + this.players = players; + } + + public static Players ofComma(String names) { + List players = Arrays.stream(names.split(NAME_SPLITTER)) + .map(String::trim) + .map(Player::new) + .collect(Collectors.toList()); + return new Players(players); + } + + public String getNames() { + List names = players.stream().map(Player::getName).collect(Collectors.toList()); + return String.join(",", names); + } + + @Override + public Iterator iterator() { + return players.iterator(); + } +} diff --git a/src/main/java/blackjack/domain/result/GamersResult.java b/src/main/java/blackjack/domain/result/GamersResult.java index e1b8b895ae..24e9216dc8 100644 --- a/src/main/java/blackjack/domain/result/GamersResult.java +++ b/src/main/java/blackjack/domain/result/GamersResult.java @@ -3,7 +3,6 @@ import blackjack.domain.gamer.Player; import java.util.Collections; -import java.util.List; import java.util.Map; public class GamersResult { diff --git a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java index 9c958b5fbc..f19b4c3759 100644 --- a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java +++ b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java @@ -2,22 +2,23 @@ import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Player; +import blackjack.domain.gamer.Players; -import java.util.*; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; public class PlayerResultMatcher { - public static GamersResult report(Dealer dealer, List players) { + public static GamersResult report(Dealer dealer, Players players) { EnumMap dealerResult = new EnumMap<>(BlackJackResult.class); - Arrays.stream(BlackJackResult.values()) - .forEach(blackJackResult -> dealerResult.put(blackJackResult, 0)); - Map playersResult = new HashMap<>(); for (Player player : players) { BlackJackResult result = match(dealer, player); playersResult.put(player, result); dealerResult.computeIfPresent(result.reversed(), (key, value) -> ++value); + dealerResult.putIfAbsent(result.reversed(), 1); } return new GamersResult(dealerResult, playersResult); diff --git a/src/main/java/blackjack/domain/rule/PlayerAnswer.java b/src/main/java/blackjack/domain/rule/PlayerAnswer.java index 3d5d175731..bb89b62f14 100644 --- a/src/main/java/blackjack/domain/rule/PlayerAnswer.java +++ b/src/main/java/blackjack/domain/rule/PlayerAnswer.java @@ -1,7 +1,5 @@ package blackjack.domain.rule; -import java.util.Arrays; - public enum PlayerAnswer { Y, diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index ee431e08de..4ebb616e09 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -3,12 +3,11 @@ import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Gamer; import blackjack.domain.gamer.Player; +import blackjack.domain.gamer.Players; import blackjack.domain.result.BlackJackResult; import blackjack.domain.result.GamersResult; -import java.util.List; import java.util.Map; -import java.util.stream.Collectors; public class OutputView { @@ -19,32 +18,23 @@ public class OutputView { private static final String RESULT_MESSAGE = "## 최종 승패"; private static final String GAMERS_RESULT_FORMAT = "%s : %s"; - public static void printInitialCards(Dealer dealer, List players) { - String names = parseName(players); + public static void printInitialCards(Dealer dealer, Players players) { System.out.print(System.lineSeparator()); - System.out.println(String.format(INITIAL_CARD_FORMAT, dealer.getName(), names)); - System.out.println(String.format(CARD_STATUS_FORMAT, dealer.getName(), dealer.getInitialCardStatus())); - for (Player player : players) { - printPlayerCard(player); - } + System.out.println(String.format(INITIAL_CARD_FORMAT, dealer.getName(), players.getNames())); + printInitialHand(dealer, players); System.out.print(System.lineSeparator()); } - public static void printPlayerCard(Player player) { + public static void printPlayerHand(Player player) { System.out.println(String.format(CARD_STATUS_FORMAT, player.getName(), player.getCardStatus())); } - private static String parseName(List players) { - List names = players.stream().map(Player::getName).collect(Collectors.toList()); - return String.join(",", names); - } - - public static void printDealerDrewACard() { + public static void printDealerDrewCard() { System.out.print(System.lineSeparator()); System.out.println(DEALER_DREW_A_CARD_MESSAGE); } - public static void printGamerScore(Dealer dealer, List players) { + public static void printGamerScore(Dealer dealer, Players players) { System.out.println(System.lineSeparator()); printCardResult(dealer); for (Player player : players) { @@ -56,15 +46,35 @@ private static void printCardResult(Gamer gamer) { System.out.println(String.format(CARD_RESULT_FORMAT, gamer.getName(), gamer.getCardStatus(), gamer.calculateSum())); } - public static void printResult(GamersResult gamersResult) { + public static void printGamersResult(GamersResult gamersResult) { System.out.print(System.lineSeparator()); System.out.println(RESULT_MESSAGE); printDealerResult(gamersResult.getDealerResult()); printPlayersResult(gamersResult.getPlayersResult()); + } + private static void printInitialHand(Dealer dealer, Players players) { + printDealerInitialHand(dealer); + for (Player player : players) { + printPlayerHand(player); + } + } + + private static void printDealerInitialHand(Dealer dealer) { + System.out.println(String.format(CARD_STATUS_FORMAT, dealer.getName(), dealer.getInitialCardStatus())); } private static void printDealerResult(Map dealerResult) { + System.out.println(String.format(GAMERS_RESULT_FORMAT, "딜러", makeDealerResult(dealerResult))); + } + + private static void printPlayersResult(Map playersResult) { + for (Map.Entry entry : playersResult.entrySet()) { + System.out.println(String.format(GAMERS_RESULT_FORMAT, entry.getKey().getName(), entry.getValue().getKoreanName())); + } + } + + private static String makeDealerResult(Map dealerResult) { StringBuilder stringBuilder = new StringBuilder(); for (Map.Entry entry : dealerResult.entrySet()) { if (entry.getValue() == 0) { @@ -72,13 +82,7 @@ private static void printDealerResult(Map dealerResult } stringBuilder.append(entry.getValue() + entry.getKey().getKoreanName()); } - System.out.println(String.format(GAMERS_RESULT_FORMAT, "딜러", stringBuilder.toString())); - } - - private static void printPlayersResult(Map playersResult) { - for (Map.Entry entry : playersResult.entrySet()) { - System.out.println(String.format(GAMERS_RESULT_FORMAT, entry.getKey().getName(), entry.getValue().getKoreanName())); - } + return stringBuilder.toString(); } } diff --git a/src/test/java/blackjack/domain/gamer/DealerTest.java b/src/test/java/blackjack/domain/gamer/DealerTest.java index ab0c9a95b0..088bbc0779 100644 --- a/src/test/java/blackjack/domain/gamer/DealerTest.java +++ b/src/test/java/blackjack/domain/gamer/DealerTest.java @@ -35,7 +35,7 @@ void getDealerName() { @DisplayName("딜러가 갖고 있는 카드의 합이 16 이하인지 확인") void shouldReceiveCard1(List cards, boolean shouldDrawCard) { for (Card card : cards) { - dealer.add(card); + dealer.draw(card); } assertThat(dealer.shouldDrawCard()).isEqualTo(shouldDrawCard); } diff --git a/src/test/java/blackjack/domain/gamer/PlayerTest.java b/src/test/java/blackjack/domain/gamer/PlayerTest.java index 3079e4ba99..c2fb1adbc6 100644 --- a/src/test/java/blackjack/domain/gamer/PlayerTest.java +++ b/src/test/java/blackjack/domain/gamer/PlayerTest.java @@ -36,7 +36,7 @@ void player() { @DisplayName("bust되었는지 확인") void isBusted(List cards, boolean isBusted) { for (Card card : cards) { - player.add(card); + player.draw(card); } assertThat(player.isBusted()).isEqualTo(isBusted); diff --git a/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java b/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java index 6ecec5499e..f6e0c9b3cb 100644 --- a/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java +++ b/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java @@ -8,20 +8,19 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; class PlayerResultMatcherTest { @Test void name() { Dealer dealer = new Dealer(); - dealer.add(new Card(CardSymbol.SIX, CardType.SPADE)); - dealer.add(new Card(CardSymbol.TEN, CardType.CLOVER)); - dealer.add(new Card(CardSymbol.THREE, CardType.DIAMOND)); + dealer.draw(new Card(CardSymbol.SIX, CardType.SPADE)); + dealer.draw(new Card(CardSymbol.TEN, CardType.CLOVER)); + dealer.draw(new Card(CardSymbol.THREE, CardType.DIAMOND)); Player player = new Player("b"); - player.add(new Card(CardSymbol.FIVE, CardType.DIAMOND)); - player.add(new Card(CardSymbol.FOUR, CardType.HEART)); + player.draw(new Card(CardSymbol.FIVE, CardType.DIAMOND)); + player.draw(new Card(CardSymbol.FOUR, CardType.HEART)); BlackJackResult blackJackResult = PlayerResultMatcher.match(dealer, player); assertThat(blackJackResult).isEqualTo(BlackJackResult.LOSE); From 35ed54aea2ccfe82bab32138d3939c28df8b9bd6 Mon Sep 17 00:00:00 2001 From: YebinK Date: Thu, 12 Mar 2020 17:04:46 +0900 Subject: [PATCH 13/19] =?UTF-8?q?[refactoring]=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/deck/Deck.java | 2 -- .../java/blackjack/domain/gamer/Dealer.java | 4 +++ .../java/blackjack/domain/gamer/Gamer.java | 4 ++- .../java/blackjack/domain/gamer/Players.java | 2 +- .../domain/result/PlayerResultMatcher.java | 3 +- .../java/blackjack/domain/card/HandTest.java | 18 ++++++++++++ .../java/blackjack/domain/deck/DeckTest.java | 16 ++++++++++- .../blackjack/domain/gamer/DealerTest.java | 1 - .../blackjack/domain/gamer/PlayersTest.java | 17 +++++++++++ .../domain/result/BlackJackResultTest.java | 17 +++++++++++ .../result/PlayerResultMatcherTest.java | 28 ------------------- .../domain/rule/PlayerAnswerTest.java | 16 +++++++++++ 12 files changed, 92 insertions(+), 36 deletions(-) create mode 100644 src/test/java/blackjack/domain/card/HandTest.java create mode 100644 src/test/java/blackjack/domain/gamer/PlayersTest.java create mode 100644 src/test/java/blackjack/domain/result/BlackJackResultTest.java delete mode 100644 src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java create mode 100644 src/test/java/blackjack/domain/rule/PlayerAnswerTest.java diff --git a/src/main/java/blackjack/domain/deck/Deck.java b/src/main/java/blackjack/domain/deck/Deck.java index a95a69e3a2..570c4893e4 100644 --- a/src/main/java/blackjack/domain/deck/Deck.java +++ b/src/main/java/blackjack/domain/deck/Deck.java @@ -21,6 +21,4 @@ public Card pick() { } return cards.pop(); } - - } diff --git a/src/main/java/blackjack/domain/gamer/Dealer.java b/src/main/java/blackjack/domain/gamer/Dealer.java index ae840ed742..a5bc5a0d65 100644 --- a/src/main/java/blackjack/domain/gamer/Dealer.java +++ b/src/main/java/blackjack/domain/gamer/Dealer.java @@ -1,5 +1,9 @@ package blackjack.domain.gamer; +import blackjack.domain.card.Card; + +import java.util.List; + public class Dealer extends Gamer { private static final String DEALER_NAME = "딜러"; diff --git a/src/main/java/blackjack/domain/gamer/Gamer.java b/src/main/java/blackjack/domain/gamer/Gamer.java index b9974f2676..f56c881d08 100644 --- a/src/main/java/blackjack/domain/gamer/Gamer.java +++ b/src/main/java/blackjack/domain/gamer/Gamer.java @@ -4,6 +4,8 @@ import blackjack.domain.card.Hand; import blackjack.domain.rule.CardCalculator; +import java.util.List; + public abstract class Gamer { protected Hand hand = new Hand(); @@ -25,4 +27,4 @@ public int calculateSum() { public String getCardStatus() { return hand.getCardStatus(); } -} +} \ No newline at end of file diff --git a/src/main/java/blackjack/domain/gamer/Players.java b/src/main/java/blackjack/domain/gamer/Players.java index 5f52c613ba..8fa6f2ea53 100644 --- a/src/main/java/blackjack/domain/gamer/Players.java +++ b/src/main/java/blackjack/domain/gamer/Players.java @@ -25,7 +25,7 @@ public static Players ofComma(String names) { public String getNames() { List names = players.stream().map(Player::getName).collect(Collectors.toList()); - return String.join(",", names); + return String.join(", ", names); } @Override diff --git a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java index f19b4c3759..4b2393f0ef 100644 --- a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java +++ b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java @@ -24,7 +24,7 @@ public static GamersResult report(Dealer dealer, Players players) { return new GamersResult(dealerResult, playersResult); } - public static BlackJackResult match(Dealer dealer, Player player) { + private static BlackJackResult match(Dealer dealer, Player player) { if (!player.isBusted()) { if (!dealer.isBusted()) { return findWinner(dealer, player); @@ -41,5 +41,4 @@ private static BlackJackResult findWinner(Dealer dealer, Player player) { return BlackJackResult.LOSE; return BlackJackResult.DRAW; } - } diff --git a/src/test/java/blackjack/domain/card/HandTest.java b/src/test/java/blackjack/domain/card/HandTest.java new file mode 100644 index 0000000000..4a957b2a4c --- /dev/null +++ b/src/test/java/blackjack/domain/card/HandTest.java @@ -0,0 +1,18 @@ +package blackjack.domain.card; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class HandTest { + @Test + @DisplayName("카드 리스트 출력") + void getCardStatus() { + Hand hand = new Hand(); + hand.add(new Card(CardSymbol.ACE, CardType.SPADE)); + hand.add(new Card(CardSymbol.FIVE, CardType.HEART)); + + assertThat(hand.getCardStatus()).isEqualTo("A스페이드, 5하트"); + } +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/deck/DeckTest.java b/src/test/java/blackjack/domain/deck/DeckTest.java index 8c49c93950..d412995e29 100644 --- a/src/test/java/blackjack/domain/deck/DeckTest.java +++ b/src/test/java/blackjack/domain/deck/DeckTest.java @@ -8,6 +8,7 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class DeckTest { @@ -18,4 +19,17 @@ void pick() { Deck deck = new Deck(cards); assertThat(deck.pick()).isInstanceOf(Card.class); } -} + + @Test + @DisplayName("카드 꺼내기") + void pickThrowException() { + List cards = CardFactory.generate(); + Deck deck = new Deck(cards); + for (int i = 0; i < 52; i++) { + deck.pick(); + } + assertThatThrownBy(() -> deck.pick()) + .isInstanceOf(NullPointerException.class) + .hasMessage("카드를 모두 사용하셨습니다."); + } +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/gamer/DealerTest.java b/src/test/java/blackjack/domain/gamer/DealerTest.java index 088bbc0779..a4ba0bd4d1 100644 --- a/src/test/java/blackjack/domain/gamer/DealerTest.java +++ b/src/test/java/blackjack/domain/gamer/DealerTest.java @@ -50,5 +50,4 @@ private static Stream createCards() { Arguments.of(cardSumUnderSixteen, true), Arguments.of(cardSumOverSixteen, false)); } - } \ No newline at end of file diff --git a/src/test/java/blackjack/domain/gamer/PlayersTest.java b/src/test/java/blackjack/domain/gamer/PlayersTest.java new file mode 100644 index 0000000000..7d6c3c8afd --- /dev/null +++ b/src/test/java/blackjack/domain/gamer/PlayersTest.java @@ -0,0 +1,17 @@ +package blackjack.domain.gamer; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class PlayersTest { + @Test + @DisplayName("플레이어 이름 분리 테스트") + void split() { + String names = "pobi,jun,woni"; + Players players = Players.ofComma(names); + + assertThat(players.getNames()).isEqualTo("pobi, jun, woni"); + } +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/result/BlackJackResultTest.java b/src/test/java/blackjack/domain/result/BlackJackResultTest.java new file mode 100644 index 0000000000..ea4d0a5275 --- /dev/null +++ b/src/test/java/blackjack/domain/result/BlackJackResultTest.java @@ -0,0 +1,17 @@ +package blackjack.domain.result; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class BlackJackResultTest { + + @Test + @DisplayName("승/무/패의 반대를 반환하는 메소드") + void reversed() { + assertThat(BlackJackResult.LOSE.reversed()).isEqualTo(BlackJackResult.WIN); + assertThat(BlackJackResult.WIN.reversed()).isEqualTo(BlackJackResult.LOSE); + assertThat(BlackJackResult.DRAW.reversed()).isEqualTo(BlackJackResult.DRAW); + } +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java b/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java deleted file mode 100644 index f6e0c9b3cb..0000000000 --- a/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package blackjack.domain.result; - -import blackjack.domain.card.Card; -import blackjack.domain.card.CardSymbol; -import blackjack.domain.card.CardType; -import blackjack.domain.gamer.Dealer; -import blackjack.domain.gamer.Player; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -class PlayerResultMatcherTest { - - @Test - void name() { - Dealer dealer = new Dealer(); - dealer.draw(new Card(CardSymbol.SIX, CardType.SPADE)); - dealer.draw(new Card(CardSymbol.TEN, CardType.CLOVER)); - dealer.draw(new Card(CardSymbol.THREE, CardType.DIAMOND)); - - Player player = new Player("b"); - player.draw(new Card(CardSymbol.FIVE, CardType.DIAMOND)); - player.draw(new Card(CardSymbol.FOUR, CardType.HEART)); - - BlackJackResult blackJackResult = PlayerResultMatcher.match(dealer, player); - assertThat(blackJackResult).isEqualTo(BlackJackResult.LOSE); - } -} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/rule/PlayerAnswerTest.java b/src/test/java/blackjack/domain/rule/PlayerAnswerTest.java new file mode 100644 index 0000000000..57cbcd4d17 --- /dev/null +++ b/src/test/java/blackjack/domain/rule/PlayerAnswerTest.java @@ -0,0 +1,16 @@ +package blackjack.domain.rule; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class PlayerAnswerTest { + + @Test + @DisplayName("y와 n을 PlayerAnswer 객체로 포장") + void yesOrNo() { + assertThat(PlayerAnswer.of("y")).isEqualTo(PlayerAnswer.Y); + assertThat(PlayerAnswer.of("n")).isEqualTo(PlayerAnswer.N); + } +} \ No newline at end of file From e53ad994a050145218039a601eb801135172e4b8 Mon Sep 17 00:00:00 2001 From: "jaeju.jang" Date: Thu, 12 Mar 2020 17:27:13 +0900 Subject: [PATCH 14/19] =?UTF-8?q?[refactoring/feature]=20GamersResult?= =?UTF-8?q?=EB=A5=BC=20Dto=EB=A1=9C=20=EC=A0=84=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PlayerResultMatcher에 대한 test case 추가 - PlayerResultMatcher와 GamersResult의 역할 분리 --- .../java/blackjack/BlackJackApplication.java | 7 +- .../java/blackjack/domain/gamer/Dealer.java | 4 -- .../java/blackjack/domain/gamer/Gamer.java | 2 - .../blackjack/domain/result/GamersResult.java | 25 ------- .../domain/result/PlayerResultMatcher.java | 21 +----- .../java/blackjack/dto/GamersResultDto.java | 45 ++++++++++++ src/main/java/blackjack/view/OutputView.java | 8 +-- .../result/PlayerResultMatcherTest.java | 70 +++++++++++++++++++ 8 files changed, 123 insertions(+), 59 deletions(-) delete mode 100644 src/main/java/blackjack/domain/result/GamersResult.java create mode 100644 src/main/java/blackjack/dto/GamersResultDto.java create mode 100644 src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java diff --git a/src/main/java/blackjack/BlackJackApplication.java b/src/main/java/blackjack/BlackJackApplication.java index 648a151d43..b0033ce79b 100644 --- a/src/main/java/blackjack/BlackJackApplication.java +++ b/src/main/java/blackjack/BlackJackApplication.java @@ -5,9 +5,8 @@ import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Player; import blackjack.domain.gamer.Players; -import blackjack.domain.result.GamersResult; -import blackjack.domain.result.PlayerResultMatcher; import blackjack.domain.rule.PlayerAnswer; +import blackjack.dto.GamersResultDto; import blackjack.view.InputView; import blackjack.view.OutputView; @@ -26,8 +25,8 @@ public static void main(String[] args) { drawMoreCard(dealer, players, deck); OutputView.printGamerScore(dealer, players); - GamersResult gamersResult = PlayerResultMatcher.report(dealer, players); - OutputView.printGamersResult(gamersResult); + GamersResultDto gamersResultDto = GamersResultDto.of(dealer, players); + OutputView.printGamersResult(gamersResultDto); } private static void initializeHand(Dealer dealer, Players players, Deck deck) { diff --git a/src/main/java/blackjack/domain/gamer/Dealer.java b/src/main/java/blackjack/domain/gamer/Dealer.java index a5bc5a0d65..ae840ed742 100644 --- a/src/main/java/blackjack/domain/gamer/Dealer.java +++ b/src/main/java/blackjack/domain/gamer/Dealer.java @@ -1,9 +1,5 @@ package blackjack.domain.gamer; -import blackjack.domain.card.Card; - -import java.util.List; - public class Dealer extends Gamer { private static final String DEALER_NAME = "딜러"; diff --git a/src/main/java/blackjack/domain/gamer/Gamer.java b/src/main/java/blackjack/domain/gamer/Gamer.java index f56c881d08..66ec1703ff 100644 --- a/src/main/java/blackjack/domain/gamer/Gamer.java +++ b/src/main/java/blackjack/domain/gamer/Gamer.java @@ -4,8 +4,6 @@ import blackjack.domain.card.Hand; import blackjack.domain.rule.CardCalculator; -import java.util.List; - public abstract class Gamer { protected Hand hand = new Hand(); diff --git a/src/main/java/blackjack/domain/result/GamersResult.java b/src/main/java/blackjack/domain/result/GamersResult.java deleted file mode 100644 index 24e9216dc8..0000000000 --- a/src/main/java/blackjack/domain/result/GamersResult.java +++ /dev/null @@ -1,25 +0,0 @@ -package blackjack.domain.result; - -import blackjack.domain.gamer.Player; - -import java.util.Collections; -import java.util.Map; - -public class GamersResult { - - private final Map dealerResult; - private final Map playersResult; - - public GamersResult(Map dealerResult, Map playersResult) { - this.dealerResult = Collections.unmodifiableMap(dealerResult); - this.playersResult = Collections.unmodifiableMap(playersResult); - } - - public Map getDealerResult() { - return dealerResult; - } - - public Map getPlayersResult() { - return playersResult; - } -} \ No newline at end of file diff --git a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java index 4b2393f0ef..8161b1ef8c 100644 --- a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java +++ b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java @@ -2,29 +2,10 @@ import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Player; -import blackjack.domain.gamer.Players; - -import java.util.EnumMap; -import java.util.HashMap; -import java.util.Map; public class PlayerResultMatcher { - public static GamersResult report(Dealer dealer, Players players) { - EnumMap dealerResult = new EnumMap<>(BlackJackResult.class); - Map playersResult = new HashMap<>(); - - for (Player player : players) { - BlackJackResult result = match(dealer, player); - playersResult.put(player, result); - dealerResult.computeIfPresent(result.reversed(), (key, value) -> ++value); - dealerResult.putIfAbsent(result.reversed(), 1); - } - - return new GamersResult(dealerResult, playersResult); - } - - private static BlackJackResult match(Dealer dealer, Player player) { + public static BlackJackResult match(Dealer dealer, Player player) { if (!player.isBusted()) { if (!dealer.isBusted()) { return findWinner(dealer, player); diff --git a/src/main/java/blackjack/dto/GamersResultDto.java b/src/main/java/blackjack/dto/GamersResultDto.java new file mode 100644 index 0000000000..8bd7b32302 --- /dev/null +++ b/src/main/java/blackjack/dto/GamersResultDto.java @@ -0,0 +1,45 @@ +package blackjack.dto; + +import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Player; +import blackjack.domain.gamer.Players; +import blackjack.domain.result.BlackJackResult; +import blackjack.domain.result.PlayerResultMatcher; + +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; + +public class GamersResultDto { + + private final Map dealerResult; + private final Map playersResult; + + private GamersResultDto(Map dealerResult, Map playersResult) { + this.dealerResult = Collections.unmodifiableMap(dealerResult); + this.playersResult = Collections.unmodifiableMap(playersResult); + } + + public static GamersResultDto of(Dealer dealer, Players players) { + EnumMap dealerResult = new EnumMap<>(BlackJackResult.class); + Map playersResult = new HashMap<>(); + + for (Player player : players) { + BlackJackResult result = PlayerResultMatcher.match(dealer, player); + playersResult.put(player, result); + dealerResult.computeIfPresent(result.reversed(), (key, value) -> ++value); + dealerResult.putIfAbsent(result.reversed(), 1); + } + + return new GamersResultDto(dealerResult, playersResult); + } + + public Map getDealerResult() { + return dealerResult; + } + + public Map getPlayersResult() { + return playersResult; + } +} \ No newline at end of file diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 4ebb616e09..73dd61334e 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -5,7 +5,7 @@ import blackjack.domain.gamer.Player; import blackjack.domain.gamer.Players; import blackjack.domain.result.BlackJackResult; -import blackjack.domain.result.GamersResult; +import blackjack.dto.GamersResultDto; import java.util.Map; @@ -46,11 +46,11 @@ private static void printCardResult(Gamer gamer) { System.out.println(String.format(CARD_RESULT_FORMAT, gamer.getName(), gamer.getCardStatus(), gamer.calculateSum())); } - public static void printGamersResult(GamersResult gamersResult) { + public static void printGamersResult(GamersResultDto gamersResultDto) { System.out.print(System.lineSeparator()); System.out.println(RESULT_MESSAGE); - printDealerResult(gamersResult.getDealerResult()); - printPlayersResult(gamersResult.getPlayersResult()); + printDealerResult(gamersResultDto.getDealerResult()); + printPlayersResult(gamersResultDto.getPlayersResult()); } private static void printInitialHand(Dealer dealer, Players players) { diff --git a/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java b/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java new file mode 100644 index 0000000000..70aa3497f1 --- /dev/null +++ b/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java @@ -0,0 +1,70 @@ +package blackjack.domain.result; + +import blackjack.domain.card.Card; +import blackjack.domain.card.CardSymbol; +import blackjack.domain.card.CardType; +import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Gamer; +import blackjack.domain.gamer.Player; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class PlayerResultMatcherTest { + + private Dealer dealer; + private Player player; + + @BeforeEach + void setUp() { + dealer = new Dealer(); + player = new Player("pobi"); + } + + @Test + @DisplayName("player가 버스트 되면 무조건 패배") + void playerBusted() { + makeGamerBust(player); + assertThat(PlayerResultMatcher.match(dealer, player)).isEqualTo(BlackJackResult.LOSE); + } + + @Test + @DisplayName("player 버스트 x, dealer 버스트 o") + void dealerBusted() { + makeGamerBust(dealer); + assertThat(PlayerResultMatcher.match(dealer, player)).isEqualTo(BlackJackResult.WIN); + } + + @Test + @DisplayName("player 버스트 x, dealer 버스트 x, 플레이어 승") + void playerWin() { + dealer.draw(new Card(CardSymbol.KING, CardType.CLOVER)); + player.draw(new Card(CardSymbol.ACE, CardType.CLOVER)); + assertThat(PlayerResultMatcher.match(dealer, player)).isEqualTo(BlackJackResult.WIN); + } + + @Test + @DisplayName("player 버스트 x, dealer 버스트 x, 플레이어 무") + void playerDraw() { + dealer.draw(new Card(CardSymbol.KING, CardType.CLOVER)); + player.draw(new Card(CardSymbol.KING, CardType.DIAMOND)); + assertThat(PlayerResultMatcher.match(dealer, player)).isEqualTo(BlackJackResult.DRAW); + } + + @Test + @DisplayName("player 버스트 x, dealer 버스트 x, 플레이어 패") + void playerLose() { + dealer.draw(new Card(CardSymbol.ACE, CardType.CLOVER)); + player.draw(new Card(CardSymbol.KING, CardType.DIAMOND)); + assertThat(PlayerResultMatcher.match(dealer, player)).isEqualTo(BlackJackResult.LOSE); + } + + private void makeGamerBust(Gamer gamer) { + gamer.draw(new Card(CardSymbol.KING, CardType.SPADE)); + gamer.draw(new Card(CardSymbol.KING, CardType.CLOVER)); + gamer.draw(new Card(CardSymbol.KING, CardType.DIAMOND)); + } + +} \ No newline at end of file From 72ff8a219001b15ae0b1662a68b726c50c71b86c Mon Sep 17 00:00:00 2001 From: "jaeju.jang" Date: Thu, 12 Mar 2020 17:46:33 +0900 Subject: [PATCH 15/19] =?UTF-8?q?[refactoring]=20hand=EB=A5=BC=20=EC=B4=88?= =?UTF-8?q?=EA=B8=B0=ED=99=94=20=ED=95=98=EB=8A=94=20=EC=97=AD=ED=95=A0=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/BlackJackApplication.java | 18 ++------------ .../domain/rule/HandInitializer.java | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 src/main/java/blackjack/domain/rule/HandInitializer.java diff --git a/src/main/java/blackjack/BlackJackApplication.java b/src/main/java/blackjack/BlackJackApplication.java index b0033ce79b..6a78af7cca 100644 --- a/src/main/java/blackjack/BlackJackApplication.java +++ b/src/main/java/blackjack/BlackJackApplication.java @@ -5,6 +5,7 @@ import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Player; import blackjack.domain.gamer.Players; +import blackjack.domain.rule.HandInitializer; import blackjack.domain.rule.PlayerAnswer; import blackjack.dto.GamersResultDto; import blackjack.view.InputView; @@ -12,14 +13,12 @@ public class BlackJackApplication { - private static final int INITIAL_HAND_COUNT = 2; - public static void main(String[] args) { Deck deck = new Deck(CardFactory.generate()); Dealer dealer = new Dealer(); Players players = Players.ofComma(InputView.askPlayerNames()); - initializeHand(dealer, players, deck); + HandInitializer.initialize(dealer, players, deck); OutputView.printInitialCards(dealer, players); drawMoreCard(dealer, players, deck); @@ -29,19 +28,6 @@ public static void main(String[] args) { OutputView.printGamersResult(gamersResultDto); } - private static void initializeHand(Dealer dealer, Players players, Deck deck) { - for (int i = 0; i < INITIAL_HAND_COUNT; i++) { - drawCard(dealer, players, deck); - } - } - - private static void drawCard(Dealer dealer, Players players, Deck deck) { - dealer.draw(deck.pick()); - for (Player player : players) { - player.draw(deck.pick()); - } - } - private static void drawMoreCard(Dealer dealer, Players players, Deck deck) { hitOrStandForPlayers(players, deck); drawCardForDealer(dealer, deck); diff --git a/src/main/java/blackjack/domain/rule/HandInitializer.java b/src/main/java/blackjack/domain/rule/HandInitializer.java new file mode 100644 index 0000000000..0ef017376a --- /dev/null +++ b/src/main/java/blackjack/domain/rule/HandInitializer.java @@ -0,0 +1,24 @@ +package blackjack.domain.rule; + +import blackjack.domain.deck.Deck; +import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Player; +import blackjack.domain.gamer.Players; + +public class HandInitializer { + + private static final int INITIAL_HAND_COUNT = 2; + + public static void initialize(Dealer dealer, Players players, Deck deck) { + for (int i = 0; i < INITIAL_HAND_COUNT; i++) { + drawCard(dealer, players, deck); + } + } + + private static void drawCard(Dealer dealer, Players players, Deck deck) { + dealer.draw(deck.pick()); + for (Player player : players) { + player.draw(deck.pick()); + } + } +} From f49086d93accb23a04bd0c7c0bfa1b17a16838bf Mon Sep 17 00:00:00 2001 From: YebinK Date: Thu, 12 Mar 2020 17:56:58 +0900 Subject: [PATCH 16/19] =?UTF-8?q?[refactor]=20=EC=83=81=EC=88=98=20?= =?UTF-8?q?=EC=A0=91=EA=B7=BC=EC=9E=90=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/gamer/Dealer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/blackjack/domain/gamer/Dealer.java b/src/main/java/blackjack/domain/gamer/Dealer.java index ae840ed742..12e2bf52d1 100644 --- a/src/main/java/blackjack/domain/gamer/Dealer.java +++ b/src/main/java/blackjack/domain/gamer/Dealer.java @@ -4,7 +4,7 @@ public class Dealer extends Gamer { private static final String DEALER_NAME = "딜러"; private static final int DRAW_THRESHOLD = 16; - public static final int FIRST_CARD_INDEX = 0; + private static final int FIRST_CARD_INDEX = 0; public boolean shouldDrawCard() { return calculateSum() <= DRAW_THRESHOLD; From 15141e4778a4b4610f9e9e7cf410dd66ea701b73 Mon Sep 17 00:00:00 2001 From: YebinK Date: Sun, 15 Mar 2020 23:33:39 +0900 Subject: [PATCH 17/19] =?UTF-8?q?[refactoring]=20=EA=B2=8C=EC=9E=84=20?= =?UTF-8?q?=EC=A7=84=ED=96=89=20=EB=A1=9C=EC=A7=81=20BlackJackMaster=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EB=B6=84=EB=A6=AC=20=20?= =?UTF-8?q?=20=20=20-=20=EC=B6=9C=EB=A0=A5=EC=9D=84=20=EC=9C=84=ED=95=9C?= =?UTF-8?q?=20=EB=A9=94=EC=86=8C=EB=93=9C=EB=93=A4=20OutputView=EB=A1=9C?= =?UTF-8?q?=20=EC=9D=B4=EB=8F=99=20=20=20=20=20-=20GamersResultAssembler?= =?UTF-8?q?=EB=A1=9C=20GamersResultDto=20=EC=83=9D=EC=84=B1=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B5=AC=ED=98=84=20=20=20=20=20-=20=ED=94=8C?= =?UTF-8?q?=EB=A0=88=EC=9D=B4=EC=96=B4=20=EC=9D=B4=EB=A6=84=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EA=B0=92=20=EA=B2=80=EC=A6=9D/=ED=83=80=EC=9E=85=20?= =?UTF-8?q?=EB=B3=80=ED=99=98=ED=95=98=EB=8A=94=20NamesDto=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/BlackJackApplication.java | 45 +++----------- .../blackjack/domain/BlackJackMaster.java | 47 ++++++++++++++ src/main/java/blackjack/domain/card/Card.java | 16 +++-- .../blackjack/domain/card/CardFactory.java | 10 ++- .../blackjack/domain/card/CardSymbol.java | 8 +-- src/main/java/blackjack/domain/card/Hand.java | 12 ++-- src/main/java/blackjack/domain/deck/Deck.java | 6 +- .../java/blackjack/domain/gamer/Dealer.java | 13 ++-- .../java/blackjack/domain/gamer/Gamer.java | 8 ++- .../java/blackjack/domain/gamer/Players.java | 17 +++-- .../domain/result/PlayerResultMatcher.java | 18 +++--- .../blackjack/domain/rule/CardCalculator.java | 4 +- .../blackjack/dto/GamersResultAssembler.java | 28 +++++++++ .../java/blackjack/dto/GamersResultDto.java | 26 ++------ src/main/java/blackjack/dto/NamesDto.java | 28 +++++++++ src/main/java/blackjack/view/InputView.java | 15 +++-- src/main/java/blackjack/view/OutputView.java | 62 +++++++++---------- .../java/blackjack/domain/card/HandTest.java | 2 +- .../blackjack/domain/gamer/PlayersTest.java | 10 +-- 19 files changed, 221 insertions(+), 154 deletions(-) create mode 100644 src/main/java/blackjack/domain/BlackJackMaster.java create mode 100644 src/main/java/blackjack/dto/GamersResultAssembler.java create mode 100644 src/main/java/blackjack/dto/NamesDto.java diff --git a/src/main/java/blackjack/BlackJackApplication.java b/src/main/java/blackjack/BlackJackApplication.java index 6a78af7cca..00c6684faf 100644 --- a/src/main/java/blackjack/BlackJackApplication.java +++ b/src/main/java/blackjack/BlackJackApplication.java @@ -1,12 +1,11 @@ package blackjack; +import blackjack.domain.BlackJackMaster; import blackjack.domain.card.CardFactory; import blackjack.domain.deck.Deck; import blackjack.domain.gamer.Dealer; -import blackjack.domain.gamer.Player; import blackjack.domain.gamer.Players; -import blackjack.domain.rule.HandInitializer; -import blackjack.domain.rule.PlayerAnswer; +import blackjack.dto.GamersResultAssembler; import blackjack.dto.GamersResultDto; import blackjack.view.InputView; import blackjack.view.OutputView; @@ -16,45 +15,15 @@ public class BlackJackApplication { public static void main(String[] args) { Deck deck = new Deck(CardFactory.generate()); Dealer dealer = new Dealer(); - Players players = Players.ofComma(InputView.askPlayerNames()); + Players players = Players.of(InputView.askPlayerNames().get()); - HandInitializer.initialize(dealer, players, deck); + BlackJackMaster.initializeCard(dealer, players, deck); OutputView.printInitialCards(dealer, players); - drawMoreCard(dealer, players, deck); + BlackJackMaster.drawMoreCard(dealer, players, deck); OutputView.printGamerScore(dealer, players); - GamersResultDto gamersResultDto = GamersResultDto.of(dealer, players); + GamersResultDto gamersResultDto = GamersResultAssembler.of(dealer, players); OutputView.printGamersResult(gamersResultDto); } - - private static void drawMoreCard(Dealer dealer, Players players, Deck deck) { - hitOrStandForPlayers(players, deck); - drawCardForDealer(dealer, deck); - } - - private static void hitOrStandForPlayers(Players players, Deck deck) { - for (Player player : players) { - hitOrStand(player, deck); - } - } - - private static void drawCardForDealer(Dealer dealer, Deck deck) { - while (dealer.shouldDrawCard()) { - dealer.draw(deck.pick()); - OutputView.printDealerDrewCard(); - } - } - - private static void hitOrStand(Player player, Deck deck) { - while (!player.isBusted() && wantMoreCard(player)) { - player.draw(deck.pick()); - OutputView.printPlayerHand(player); - } - } - - private static boolean wantMoreCard(Player player) { - PlayerAnswer answer = PlayerAnswer.of(InputView.askHitOrStand(player.getName())); - return answer.isYes(); - } -} +} \ No newline at end of file diff --git a/src/main/java/blackjack/domain/BlackJackMaster.java b/src/main/java/blackjack/domain/BlackJackMaster.java new file mode 100644 index 0000000000..526a7d9423 --- /dev/null +++ b/src/main/java/blackjack/domain/BlackJackMaster.java @@ -0,0 +1,47 @@ +package blackjack.domain; + +import blackjack.domain.deck.Deck; +import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Player; +import blackjack.domain.gamer.Players; +import blackjack.domain.rule.HandInitializer; +import blackjack.domain.rule.PlayerAnswer; +import blackjack.view.InputView; +import blackjack.view.OutputView; + +public class BlackJackMaster { + + public static void initializeCard(Dealer dealer, Players players, Deck deck) { + HandInitializer.initialize(dealer, players, deck); + } + + public static void drawMoreCard(Dealer dealer, Players players, Deck deck) { + hitOrStandForPlayers(players, deck); + drawCardForDealer(dealer, deck); + } + + private static void hitOrStandForPlayers(Players players, Deck deck) { + for (Player player : players) { + hitOrStand(player, deck); + } + } + + private static void drawCardForDealer(Dealer dealer, Deck deck) { + while (dealer.shouldDrawCard()) { + dealer.draw(deck.pick()); + OutputView.printDealerDrewCard(); + } + } + + private static void hitOrStand(Player player, Deck deck) { + while (!player.isBusted() && wantMoreCard(player)) { + player.draw(deck.pick()); + OutputView.printPlayerHand(player); + } + } + + private static boolean wantMoreCard(Player player) { + PlayerAnswer answer = PlayerAnswer.of(InputView.askHitOrStand(player)); + return answer.isYes(); + } +} diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index 45c9fb692b..2552320e6c 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -10,16 +10,20 @@ public Card(CardSymbol cardSymbol, CardType cardType) { this.cardType = cardType; } - @Override - public String toString() { - return cardSymbol.getCardSymbol() + cardType.getKoreanName(); + public boolean isAce() { + return cardSymbol.isAce(); } - public int getNumber() { + public int getCardNumber() { return cardSymbol.getCardNumber(); } - public boolean isAce() { - return cardSymbol.isAce(); + public CardType getCardType() { + return cardType; + } + + @Override + public String toString() { + return cardSymbol.getCardSymbol() + cardType.getKoreanName(); } } diff --git a/src/main/java/blackjack/domain/card/CardFactory.java b/src/main/java/blackjack/domain/card/CardFactory.java index 818e8ae076..e407d85979 100644 --- a/src/main/java/blackjack/domain/card/CardFactory.java +++ b/src/main/java/blackjack/domain/card/CardFactory.java @@ -10,10 +10,14 @@ public static List generate() { List cards = new ArrayList<>(); for (CardSymbol symbol : CardSymbol.values()) { - for (CardType type : CardType.values()) { - cards.add(new Card(symbol, type)); - } + createByType(cards, symbol); } return cards; } + + private static void createByType(List cards, CardSymbol symbol) { + for (CardType type : CardType.values()) { + cards.add(new Card(symbol, type)); + } + } } diff --git a/src/main/java/blackjack/domain/card/CardSymbol.java b/src/main/java/blackjack/domain/card/CardSymbol.java index 7d1a0eaea2..3596a6d1b6 100644 --- a/src/main/java/blackjack/domain/card/CardSymbol.java +++ b/src/main/java/blackjack/domain/card/CardSymbol.java @@ -24,15 +24,15 @@ public enum CardSymbol { this.cardSymbol = cardSymbol; } - public String getCardSymbol() { - return cardSymbol; + public boolean isAce() { + return this == ACE; } public int getCardNumber() { return cardNumber; } - public boolean isAce() { - return this == ACE; + public String getCardSymbol() { + return cardSymbol; } } diff --git a/src/main/java/blackjack/domain/card/Hand.java b/src/main/java/blackjack/domain/card/Hand.java index 4fb1dca190..8c9755ae75 100644 --- a/src/main/java/blackjack/domain/card/Hand.java +++ b/src/main/java/blackjack/domain/card/Hand.java @@ -4,13 +4,15 @@ import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; public class Hand { private final List hand = new ArrayList<>(); public void add(Card card) { + if (card == null) { + throw new IllegalArgumentException("카드 리스트를 생성할 수 없습니다."); + } hand.add(card); } @@ -18,13 +20,7 @@ public int calculateSum() { return CardCalculator.calculate(hand); } - public String getCardStatus() { - return hand.stream() - .map(Card::toString) - .collect(Collectors.joining(", ")); - } - - public List getHand() { + public List getCardStatus() { return hand; } } diff --git a/src/main/java/blackjack/domain/deck/Deck.java b/src/main/java/blackjack/domain/deck/Deck.java index 570c4893e4..11695b9cb7 100644 --- a/src/main/java/blackjack/domain/deck/Deck.java +++ b/src/main/java/blackjack/domain/deck/Deck.java @@ -3,6 +3,7 @@ import blackjack.domain.card.Card; import java.util.Collections; +import java.util.EmptyStackException; import java.util.List; import java.util.Stack; @@ -11,13 +12,16 @@ public class Deck { private final Stack cards = new Stack<>(); public Deck(List cards) { + if (cards.isEmpty() || cards == null) { + throw new IllegalArgumentException("카드 덱을 생성할 수 없습니다."); + } Collections.shuffle(cards); this.cards.addAll(cards); } public Card pick() { if (cards.empty()) { - throw new NullPointerException("카드를 모두 사용하셨습니다."); + throw new EmptyStackException(); } return cards.pop(); } diff --git a/src/main/java/blackjack/domain/gamer/Dealer.java b/src/main/java/blackjack/domain/gamer/Dealer.java index 12e2bf52d1..12e0af35e6 100644 --- a/src/main/java/blackjack/domain/gamer/Dealer.java +++ b/src/main/java/blackjack/domain/gamer/Dealer.java @@ -1,21 +1,24 @@ package blackjack.domain.gamer; +import blackjack.domain.card.Card; + +import java.util.List; + public class Dealer extends Gamer { private static final String DEALER_NAME = "딜러"; private static final int DRAW_THRESHOLD = 16; - private static final int FIRST_CARD_INDEX = 0; public boolean shouldDrawCard() { return calculateSum() <= DRAW_THRESHOLD; } + public Card getOpenCard() { + return hand.getCardStatus().get(0); + } + @Override public String getName() { return DEALER_NAME; } - - public String getInitialCardStatus() { - return hand.getHand().get(FIRST_CARD_INDEX).toString(); - } } \ No newline at end of file diff --git a/src/main/java/blackjack/domain/gamer/Gamer.java b/src/main/java/blackjack/domain/gamer/Gamer.java index 66ec1703ff..323632e834 100644 --- a/src/main/java/blackjack/domain/gamer/Gamer.java +++ b/src/main/java/blackjack/domain/gamer/Gamer.java @@ -4,6 +4,8 @@ import blackjack.domain.card.Hand; import blackjack.domain.rule.CardCalculator; +import java.util.List; + public abstract class Gamer { protected Hand hand = new Hand(); @@ -20,9 +22,9 @@ public int calculateSum() { return hand.calculateSum(); } - public abstract String getName(); - - public String getCardStatus() { + public List getCardStatus() { return hand.getCardStatus(); } + + public abstract String getName(); } \ No newline at end of file diff --git a/src/main/java/blackjack/domain/gamer/Players.java b/src/main/java/blackjack/domain/gamer/Players.java index 8fa6f2ea53..958fc17174 100644 --- a/src/main/java/blackjack/domain/gamer/Players.java +++ b/src/main/java/blackjack/domain/gamer/Players.java @@ -1,31 +1,28 @@ package blackjack.domain.gamer; -import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; public class Players implements Iterable { - private static final String NAME_SPLITTER = ","; - - private List players; + private final List players; private Players(List players) { this.players = players; } - public static Players ofComma(String names) { - List players = Arrays.stream(names.split(NAME_SPLITTER)) - .map(String::trim) + public static Players of(List names) { + List players = names.stream() .map(Player::new) .collect(Collectors.toList()); return new Players(players); } - public String getNames() { - List names = players.stream().map(Player::getName).collect(Collectors.toList()); - return String.join(", ", names); + public List getNames() { + return players.stream() + .map(Player::getName) + .collect(Collectors.toList()); } @Override diff --git a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java index 8161b1ef8c..9bb27a8deb 100644 --- a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java +++ b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java @@ -6,20 +6,20 @@ public class PlayerResultMatcher { public static BlackJackResult match(Dealer dealer, Player player) { - if (!player.isBusted()) { - if (!dealer.isBusted()) { - return findWinner(dealer, player); - } + if (player.isBusted()) + return BlackJackResult.LOSE; + if (dealer.isBusted()) return BlackJackResult.WIN; - } - return BlackJackResult.LOSE; + return findWinner(dealer, player); } private static BlackJackResult findWinner(Dealer dealer, Player player) { - if (dealer.calculateSum() < player.calculateSum()) + if (dealer.calculateSum() < player.calculateSum()) { return BlackJackResult.WIN; - if (dealer.calculateSum() > player.calculateSum()) + } + if (dealer.calculateSum() > player.calculateSum()) { return BlackJackResult.LOSE; + } return BlackJackResult.DRAW; } -} +} \ No newline at end of file diff --git a/src/main/java/blackjack/domain/rule/CardCalculator.java b/src/main/java/blackjack/domain/rule/CardCalculator.java index 46c59207d5..f3c61c0208 100644 --- a/src/main/java/blackjack/domain/rule/CardCalculator.java +++ b/src/main/java/blackjack/domain/rule/CardCalculator.java @@ -16,7 +16,7 @@ public static int calculate(List cards) { private static int sumAll(List cards) { return cards.stream() - .mapToInt(Card::getNumber) + .mapToInt(Card::getCardNumber) .sum(); } @@ -28,4 +28,4 @@ private static int subtractIfContainingAce(List cards, int result) { } return result; } -} +} \ No newline at end of file diff --git a/src/main/java/blackjack/dto/GamersResultAssembler.java b/src/main/java/blackjack/dto/GamersResultAssembler.java new file mode 100644 index 0000000000..21069e32b8 --- /dev/null +++ b/src/main/java/blackjack/dto/GamersResultAssembler.java @@ -0,0 +1,28 @@ +package blackjack.dto; + +import blackjack.domain.gamer.Dealer; +import blackjack.domain.gamer.Player; +import blackjack.domain.gamer.Players; +import blackjack.domain.result.BlackJackResult; +import blackjack.domain.result.PlayerResultMatcher; + +import java.util.EnumMap; +import java.util.LinkedHashMap; +import java.util.Map; + +public class GamersResultAssembler { + + public static GamersResultDto of(Dealer dealer, Players players) { + EnumMap dealerResult = new EnumMap<>(BlackJackResult.class); + Map playersResult = new LinkedHashMap<>(); + + for (Player player : players) { + BlackJackResult result = PlayerResultMatcher.match(dealer, player); + playersResult.put(player, result); + dealerResult.computeIfPresent(result.reversed(), (key, value) -> ++value); + dealerResult.putIfAbsent(result.reversed(), 1); + } + + return new GamersResultDto(dealerResult, playersResult); + } +} diff --git a/src/main/java/blackjack/dto/GamersResultDto.java b/src/main/java/blackjack/dto/GamersResultDto.java index 8bd7b32302..322bdda33e 100644 --- a/src/main/java/blackjack/dto/GamersResultDto.java +++ b/src/main/java/blackjack/dto/GamersResultDto.java @@ -6,33 +6,19 @@ import blackjack.domain.result.BlackJackResult; import blackjack.domain.result.PlayerResultMatcher; -import java.util.Collections; import java.util.EnumMap; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.stream.Collectors; -public class GamersResultDto { +public final class GamersResultDto { private final Map dealerResult; private final Map playersResult; - private GamersResultDto(Map dealerResult, Map playersResult) { - this.dealerResult = Collections.unmodifiableMap(dealerResult); - this.playersResult = Collections.unmodifiableMap(playersResult); - } - - public static GamersResultDto of(Dealer dealer, Players players) { - EnumMap dealerResult = new EnumMap<>(BlackJackResult.class); - Map playersResult = new HashMap<>(); - - for (Player player : players) { - BlackJackResult result = PlayerResultMatcher.match(dealer, player); - playersResult.put(player, result); - dealerResult.computeIfPresent(result.reversed(), (key, value) -> ++value); - dealerResult.putIfAbsent(result.reversed(), 1); - } - - return new GamersResultDto(dealerResult, playersResult); + protected GamersResultDto(Map dealerResult, Map playersResult) { + this.dealerResult = dealerResult; + this.playersResult = playersResult; } public Map getDealerResult() { diff --git a/src/main/java/blackjack/dto/NamesDto.java b/src/main/java/blackjack/dto/NamesDto.java new file mode 100644 index 0000000000..c6e9946d05 --- /dev/null +++ b/src/main/java/blackjack/dto/NamesDto.java @@ -0,0 +1,28 @@ +package blackjack.dto; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class NamesDto { + private final List names; + + public NamesDto(String names) { + this.names = validate(names); + } + + private List validate(String names) { + List splittedNames = Arrays.stream(names.split(",")) + .map(String::trim) + .collect(Collectors.toList()); + + if (names == null || splittedNames.isEmpty()) + throw new NullPointerException("비어있는 값을 입력하셨습니다"); + + return splittedNames; + } + + public List get() { + return names; + } +} diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index 0b4b88d9bb..5804c1316f 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -1,20 +1,27 @@ package blackjack.view; +import blackjack.domain.gamer.Player; +import blackjack.dto.NamesDto; + +import java.util.Arrays; +import java.util.List; import java.util.Scanner; +import static java.util.stream.Collectors.toList; + public class InputView { private static final Scanner SCANNER = new Scanner(System.in); private static final String ASK_PLAYER_NAMES_MESSAGE = "게임에 참여할 사람의 이름을 입력하세요. (쉼표 기준으로 분리)"; private static final String ASK_HIT_OR_STAND_MESSAGE = "%s는 한 장의 카드를 더 받겠습니까?(에는 y, 아니오는 n)"; - public static String askPlayerNames() { + public static NamesDto askPlayerNames() { System.out.println(ASK_PLAYER_NAMES_MESSAGE); - return SCANNER.nextLine(); + return new NamesDto(SCANNER.nextLine()); } - public static String askHitOrStand(String name) { - System.out.println(String.format(ASK_HIT_OR_STAND_MESSAGE, name)); + public static String askHitOrStand(Player player) { + System.out.println(String.format(ASK_HIT_OR_STAND_MESSAGE, player.getName())); return SCANNER.nextLine(); } } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 73dd61334e..c7a18cc302 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,5 +1,6 @@ package blackjack.view; +import blackjack.domain.card.Card; import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Gamer; import blackjack.domain.gamer.Player; @@ -7,82 +8,77 @@ import blackjack.domain.result.BlackJackResult; import blackjack.dto.GamersResultDto; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public class OutputView { private static final String INITIAL_CARD_FORMAT = "%s와 %s에게 2장의 카드를 나누었습니다."; private static final String CARD_STATUS_FORMAT = "%s 카드 : %s"; private static final String DEALER_DREW_A_CARD_MESSAGE = "딜러는 16이하라 1장의 카드를 더 받았습니다."; - private static final String CARD_RESULT_FORMAT = "%s 카드 : %s - 결과 : %d"; + private static final String CARD_STATUS_AND_RESULT_FORMAT = "%s 카드 : %s - 결과 : %d"; private static final String RESULT_MESSAGE = "## 최종 승패"; private static final String GAMERS_RESULT_FORMAT = "%s : %s"; + private static final String CARD_FORMAT = "%s%s"; public static void printInitialCards(Dealer dealer, Players players) { System.out.print(System.lineSeparator()); - System.out.println(String.format(INITIAL_CARD_FORMAT, dealer.getName(), players.getNames())); + System.out.println(String.format(INITIAL_CARD_FORMAT, dealer.getName(), players.getNames().stream().collect(Collectors.joining(", ")))); printInitialHand(dealer, players); System.out.print(System.lineSeparator()); } public static void printPlayerHand(Player player) { - System.out.println(String.format(CARD_STATUS_FORMAT, player.getName(), player.getCardStatus())); + System.out.println(String.format(CARD_STATUS_FORMAT, player.getName(), makeHandResult(player.getCardStatus()))); } public static void printDealerDrewCard() { - System.out.print(System.lineSeparator()); - System.out.println(DEALER_DREW_A_CARD_MESSAGE); + System.out.print(System.lineSeparator() + DEALER_DREW_A_CARD_MESSAGE); } public static void printGamerScore(Dealer dealer, Players players) { System.out.println(System.lineSeparator()); - printCardResult(dealer); + printCardStatusAndResult(dealer); for (Player player : players) { - printCardResult(player); + printCardStatusAndResult(player); } } - private static void printCardResult(Gamer gamer) { - System.out.println(String.format(CARD_RESULT_FORMAT, gamer.getName(), gamer.getCardStatus(), gamer.calculateSum())); + private static void printCardStatusAndResult(Gamer gamer) { + System.out.println(String.format(CARD_STATUS_AND_RESULT_FORMAT, gamer.getName(), makeHandResult(gamer.getCardStatus()), gamer.calculateSum())); } public static void printGamersResult(GamersResultDto gamersResultDto) { - System.out.print(System.lineSeparator()); - System.out.println(RESULT_MESSAGE); - printDealerResult(gamersResultDto.getDealerResult()); - printPlayersResult(gamersResultDto.getPlayersResult()); + System.out.println(System.lineSeparator() + RESULT_MESSAGE); + System.out.println(String.format(GAMERS_RESULT_FORMAT, "딜러", makeDealerResult(gamersResultDto.getDealerResult()))); + System.out.println(makePlayersResult(gamersResultDto.getPlayersResult())); } private static void printInitialHand(Dealer dealer, Players players) { - printDealerInitialHand(dealer); + System.out.println(String.format(CARD_STATUS_FORMAT, dealer.getName(), dealer.getOpenCard().toString())); for (Player player : players) { printPlayerHand(player); } } - private static void printDealerInitialHand(Dealer dealer) { - System.out.println(String.format(CARD_STATUS_FORMAT, dealer.getName(), dealer.getInitialCardStatus())); - } - - private static void printDealerResult(Map dealerResult) { - System.out.println(String.format(GAMERS_RESULT_FORMAT, "딜러", makeDealerResult(dealerResult))); - } - - private static void printPlayersResult(Map playersResult) { - for (Map.Entry entry : playersResult.entrySet()) { - System.out.println(String.format(GAMERS_RESULT_FORMAT, entry.getKey().getName(), entry.getValue().getKoreanName())); - } + private static String makeHandResult(List cards) { + return cards.stream() + .map(card -> String.format(CARD_FORMAT, card.getCardNumber(), card.getCardType())) + .collect(Collectors.joining(", ")); } private static String makeDealerResult(Map dealerResult) { - StringBuilder stringBuilder = new StringBuilder(); + StringBuilder result = new StringBuilder(); for (Map.Entry entry : dealerResult.entrySet()) { - if (entry.getValue() == 0) { - continue; - } - stringBuilder.append(entry.getValue() + entry.getKey().getKoreanName()); + result.append(entry.getValue() + entry.getKey().getKoreanName()); } - return stringBuilder.toString(); + return result.toString(); } -} + private static String makePlayersResult(Map playersResult) { + return playersResult.entrySet().stream() + .map(entry -> String.format(GAMERS_RESULT_FORMAT, entry.getKey().getName(), entry.getValue().getKoreanName())) + .collect(Collectors.joining(System.lineSeparator())); + } +} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/card/HandTest.java b/src/test/java/blackjack/domain/card/HandTest.java index 4a957b2a4c..792fd92c1e 100644 --- a/src/test/java/blackjack/domain/card/HandTest.java +++ b/src/test/java/blackjack/domain/card/HandTest.java @@ -13,6 +13,6 @@ void getCardStatus() { hand.add(new Card(CardSymbol.ACE, CardType.SPADE)); hand.add(new Card(CardSymbol.FIVE, CardType.HEART)); - assertThat(hand.getCardStatus()).isEqualTo("A스페이드, 5하트"); + assertThat(hand.getCardStatus().size()).isEqualTo(2); } } \ No newline at end of file diff --git a/src/test/java/blackjack/domain/gamer/PlayersTest.java b/src/test/java/blackjack/domain/gamer/PlayersTest.java index 7d6c3c8afd..bee5bdd4a4 100644 --- a/src/test/java/blackjack/domain/gamer/PlayersTest.java +++ b/src/test/java/blackjack/domain/gamer/PlayersTest.java @@ -3,15 +3,11 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.List; + import static org.assertj.core.api.Assertions.assertThat; class PlayersTest { - @Test - @DisplayName("플레이어 이름 분리 테스트") - void split() { - String names = "pobi,jun,woni"; - Players players = Players.ofComma(names); - assertThat(players.getNames()).isEqualTo("pobi, jun, woni"); - } } \ No newline at end of file From 08d1fde1e7b4b0850a60b6f00fae11de2633d28e Mon Sep 17 00:00:00 2001 From: YebinK Date: Mon, 16 Mar 2020 13:42:11 +0900 Subject: [PATCH 18/19] =?UTF-8?q?[refactoring]=20BlackJackMaster=20->=20Bl?= =?UTF-8?q?ackJackController=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/BlackJackApplication.java | 5 ++--- ...JackMaster.java => BlackJackController.java} | 8 ++++---- src/main/java/blackjack/domain/card/Hand.java | 5 +++-- .../java/blackjack/domain/gamer/Dealer.java | 16 +++++++--------- src/main/java/blackjack/domain/gamer/Gamer.java | 15 ++++++++++++--- .../java/blackjack/domain/gamer/Player.java | 8 ++++++++ .../domain/result/BlackJackResult.java | 6 ++++-- .../domain/result/PlayerResultMatcher.java | 6 ++++-- src/main/java/blackjack/dto/NamesDto.java | 6 +++--- src/main/java/blackjack/view/InputView.java | 4 ---- src/main/java/blackjack/view/OutputView.java | 17 +++++++++-------- .../java/blackjack/domain/gamer/DealerTest.java | 8 +------- .../blackjack/domain/gamer/PlayersTest.java | 13 ------------- .../domain/result/PlayerResultMatcherTest.java | 6 +++--- 14 files changed, 60 insertions(+), 63 deletions(-) rename src/main/java/blackjack/{domain/BlackJackMaster.java => BlackJackController.java} (89%) delete mode 100644 src/test/java/blackjack/domain/gamer/PlayersTest.java diff --git a/src/main/java/blackjack/BlackJackApplication.java b/src/main/java/blackjack/BlackJackApplication.java index 00c6684faf..3ecbd79b31 100644 --- a/src/main/java/blackjack/BlackJackApplication.java +++ b/src/main/java/blackjack/BlackJackApplication.java @@ -1,6 +1,5 @@ package blackjack; -import blackjack.domain.BlackJackMaster; import blackjack.domain.card.CardFactory; import blackjack.domain.deck.Deck; import blackjack.domain.gamer.Dealer; @@ -17,10 +16,10 @@ public static void main(String[] args) { Dealer dealer = new Dealer(); Players players = Players.of(InputView.askPlayerNames().get()); - BlackJackMaster.initializeCard(dealer, players, deck); + BlackJackController.initializeCard(dealer, players, deck); OutputView.printInitialCards(dealer, players); - BlackJackMaster.drawMoreCard(dealer, players, deck); + BlackJackController.drawMoreCard(dealer, players, deck); OutputView.printGamerScore(dealer, players); GamersResultDto gamersResultDto = GamersResultAssembler.of(dealer, players); diff --git a/src/main/java/blackjack/domain/BlackJackMaster.java b/src/main/java/blackjack/BlackJackController.java similarity index 89% rename from src/main/java/blackjack/domain/BlackJackMaster.java rename to src/main/java/blackjack/BlackJackController.java index 526a7d9423..ec406f07ea 100644 --- a/src/main/java/blackjack/domain/BlackJackMaster.java +++ b/src/main/java/blackjack/BlackJackController.java @@ -1,4 +1,4 @@ -package blackjack.domain; +package blackjack; import blackjack.domain.deck.Deck; import blackjack.domain.gamer.Dealer; @@ -9,7 +9,7 @@ import blackjack.view.InputView; import blackjack.view.OutputView; -public class BlackJackMaster { +public class BlackJackController { public static void initializeCard(Dealer dealer, Players players, Deck deck) { HandInitializer.initialize(dealer, players, deck); @@ -27,14 +27,14 @@ private static void hitOrStandForPlayers(Players players, Deck deck) { } private static void drawCardForDealer(Dealer dealer, Deck deck) { - while (dealer.shouldDrawCard()) { + while (dealer.canDrawCard()) { dealer.draw(deck.pick()); OutputView.printDealerDrewCard(); } } private static void hitOrStand(Player player, Deck deck) { - while (!player.isBusted() && wantMoreCard(player)) { + while (player.canDrawCard() && wantMoreCard(player)) { player.draw(deck.pick()); OutputView.printPlayerHand(player); } diff --git a/src/main/java/blackjack/domain/card/Hand.java b/src/main/java/blackjack/domain/card/Hand.java index 8c9755ae75..22a5c7e39a 100644 --- a/src/main/java/blackjack/domain/card/Hand.java +++ b/src/main/java/blackjack/domain/card/Hand.java @@ -4,14 +4,15 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class Hand { private final List hand = new ArrayList<>(); public void add(Card card) { - if (card == null) { - throw new IllegalArgumentException("카드 리스트를 생성할 수 없습니다."); + if (Objects.isNull(card)) { + throw new IllegalArgumentException("카드를 추가할 수 없습니다."); } hand.add(card); } diff --git a/src/main/java/blackjack/domain/gamer/Dealer.java b/src/main/java/blackjack/domain/gamer/Dealer.java index 12e0af35e6..cc6e432e6e 100644 --- a/src/main/java/blackjack/domain/gamer/Dealer.java +++ b/src/main/java/blackjack/domain/gamer/Dealer.java @@ -2,23 +2,21 @@ import blackjack.domain.card.Card; -import java.util.List; - public class Dealer extends Gamer { - private static final String DEALER_NAME = "딜러"; private static final int DRAW_THRESHOLD = 16; - public boolean shouldDrawCard() { + @Override + public boolean canDrawCard() { return calculateSum() <= DRAW_THRESHOLD; } - public Card getOpenCard() { - return hand.getCardStatus().get(0); - } - @Override public String getName() { - return DEALER_NAME; + return "딜러"; + } + + public Card getOpenCard() { + return hand.getCardStatus().get(0); } } \ No newline at end of file diff --git a/src/main/java/blackjack/domain/gamer/Gamer.java b/src/main/java/blackjack/domain/gamer/Gamer.java index 323632e834..e4615eb9b9 100644 --- a/src/main/java/blackjack/domain/gamer/Gamer.java +++ b/src/main/java/blackjack/domain/gamer/Gamer.java @@ -6,12 +6,14 @@ import java.util.List; -public abstract class Gamer { +public abstract class Gamer implements Comparable { protected Hand hand = new Hand(); + private int score = 0; public void draw(Card card) { hand.add(card); + score = hand.calculateSum(); } public boolean isBusted() { @@ -22,9 +24,16 @@ public int calculateSum() { return hand.calculateSum(); } - public List getCardStatus() { + public abstract boolean canDrawCard(); + + public abstract String getName(); + + public List getHand() { return hand.getCardStatus(); } - public abstract String getName(); + @Override + public int compareTo(Gamer o) { + return Integer.compare(this.score, o.score); + } } \ No newline at end of file diff --git a/src/main/java/blackjack/domain/gamer/Player.java b/src/main/java/blackjack/domain/gamer/Player.java index b7b3fc80c2..911db08aa3 100644 --- a/src/main/java/blackjack/domain/gamer/Player.java +++ b/src/main/java/blackjack/domain/gamer/Player.java @@ -1,5 +1,7 @@ package blackjack.domain.gamer; +import blackjack.domain.rule.CardCalculator; + import java.util.Objects; public class Player extends Gamer { @@ -10,6 +12,12 @@ public Player(String name) { this.name = Objects.requireNonNull(name); } + @Override + public boolean canDrawCard() { + return calculateSum() <= CardCalculator.BUST_THRESHOLD; + } + + @Override public String getName() { return name; } diff --git a/src/main/java/blackjack/domain/result/BlackJackResult.java b/src/main/java/blackjack/domain/result/BlackJackResult.java index 37aea7e160..c7e4ac0eea 100644 --- a/src/main/java/blackjack/domain/result/BlackJackResult.java +++ b/src/main/java/blackjack/domain/result/BlackJackResult.java @@ -13,10 +13,12 @@ public enum BlackJackResult { } public BlackJackResult reversed() { - if (this == WIN) + if (this == WIN) { return LOSE; - if (this == LOSE) + } + if (this == LOSE) { return WIN; + } return DRAW; } diff --git a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java index 9bb27a8deb..2b052b468b 100644 --- a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java +++ b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java @@ -6,10 +6,12 @@ public class PlayerResultMatcher { public static BlackJackResult match(Dealer dealer, Player player) { - if (player.isBusted()) + if (player.isBusted()) { return BlackJackResult.LOSE; - if (dealer.isBusted()) + } + if (dealer.isBusted()) { return BlackJackResult.WIN; + } return findWinner(dealer, player); } diff --git a/src/main/java/blackjack/dto/NamesDto.java b/src/main/java/blackjack/dto/NamesDto.java index c6e9946d05..d877eec8df 100644 --- a/src/main/java/blackjack/dto/NamesDto.java +++ b/src/main/java/blackjack/dto/NamesDto.java @@ -12,9 +12,9 @@ public NamesDto(String names) { } private List validate(String names) { - List splittedNames = Arrays.stream(names.split(",")) - .map(String::trim) - .collect(Collectors.toList()); + List splittedNames = Arrays.stream(names.split(",")) + .map(String::trim) + .collect(Collectors.toList()); if (names == null || splittedNames.isEmpty()) throw new NullPointerException("비어있는 값을 입력하셨습니다"); diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index 5804c1316f..e764e93829 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -3,12 +3,8 @@ import blackjack.domain.gamer.Player; import blackjack.dto.NamesDto; -import java.util.Arrays; -import java.util.List; import java.util.Scanner; -import static java.util.stream.Collectors.toList; - public class InputView { private static final Scanner SCANNER = new Scanner(System.in); diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index c7a18cc302..7a33a118b3 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -14,23 +14,24 @@ public class OutputView { - private static final String INITIAL_CARD_FORMAT = "%s와 %s에게 2장의 카드를 나누었습니다."; + private static final String INITIAL_CARD_FORMAT = "딜러와 %s에게 2장의 카드를 나누었습니다."; private static final String CARD_STATUS_FORMAT = "%s 카드 : %s"; private static final String DEALER_DREW_A_CARD_MESSAGE = "딜러는 16이하라 1장의 카드를 더 받았습니다."; private static final String CARD_STATUS_AND_RESULT_FORMAT = "%s 카드 : %s - 결과 : %d"; private static final String RESULT_MESSAGE = "## 최종 승패"; private static final String GAMERS_RESULT_FORMAT = "%s : %s"; private static final String CARD_FORMAT = "%s%s"; + private static final String DELIMITER = ", "; public static void printInitialCards(Dealer dealer, Players players) { System.out.print(System.lineSeparator()); - System.out.println(String.format(INITIAL_CARD_FORMAT, dealer.getName(), players.getNames().stream().collect(Collectors.joining(", ")))); + System.out.println(String.format(INITIAL_CARD_FORMAT, players.getNames().stream().collect(Collectors.joining(DELIMITER)))); printInitialHand(dealer, players); System.out.print(System.lineSeparator()); } public static void printPlayerHand(Player player) { - System.out.println(String.format(CARD_STATUS_FORMAT, player.getName(), makeHandResult(player.getCardStatus()))); + System.out.println(String.format(CARD_STATUS_FORMAT, player.getName(), makeHandResult(player.getHand()))); } public static void printDealerDrewCard() { @@ -45,10 +46,6 @@ public static void printGamerScore(Dealer dealer, Players players) { } } - private static void printCardStatusAndResult(Gamer gamer) { - System.out.println(String.format(CARD_STATUS_AND_RESULT_FORMAT, gamer.getName(), makeHandResult(gamer.getCardStatus()), gamer.calculateSum())); - } - public static void printGamersResult(GamersResultDto gamersResultDto) { System.out.println(System.lineSeparator() + RESULT_MESSAGE); System.out.println(String.format(GAMERS_RESULT_FORMAT, "딜러", makeDealerResult(gamersResultDto.getDealerResult()))); @@ -62,10 +59,14 @@ private static void printInitialHand(Dealer dealer, Players players) { } } + private static void printCardStatusAndResult(Gamer gamer) { + System.out.println(String.format(CARD_STATUS_AND_RESULT_FORMAT, gamer.getName(), makeHandResult(gamer.getHand()), gamer.calculateSum())); + } + private static String makeHandResult(List cards) { return cards.stream() .map(card -> String.format(CARD_FORMAT, card.getCardNumber(), card.getCardType())) - .collect(Collectors.joining(", ")); + .collect(Collectors.joining(DELIMITER)); } private static String makeDealerResult(Map dealerResult) { diff --git a/src/test/java/blackjack/domain/gamer/DealerTest.java b/src/test/java/blackjack/domain/gamer/DealerTest.java index a4ba0bd4d1..df252c7c5f 100644 --- a/src/test/java/blackjack/domain/gamer/DealerTest.java +++ b/src/test/java/blackjack/domain/gamer/DealerTest.java @@ -5,7 +5,6 @@ import blackjack.domain.card.CardType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -25,11 +24,6 @@ void setUp() { dealer = new Dealer(); } - @Test - void getDealerName() { - assertThat(dealer.getName()).isEqualTo("딜러"); - } - @ParameterizedTest @MethodSource("createCards") @DisplayName("딜러가 갖고 있는 카드의 합이 16 이하인지 확인") @@ -37,7 +31,7 @@ void shouldReceiveCard1(List cards, boolean shouldDrawCard) { for (Card card : cards) { dealer.draw(card); } - assertThat(dealer.shouldDrawCard()).isEqualTo(shouldDrawCard); + assertThat(dealer.canDrawCard()).isEqualTo(shouldDrawCard); } private static Stream createCards() { diff --git a/src/test/java/blackjack/domain/gamer/PlayersTest.java b/src/test/java/blackjack/domain/gamer/PlayersTest.java deleted file mode 100644 index bee5bdd4a4..0000000000 --- a/src/test/java/blackjack/domain/gamer/PlayersTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package blackjack.domain.gamer; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - -class PlayersTest { - -} \ No newline at end of file diff --git a/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java b/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java index 70aa3497f1..5b1a603b10 100644 --- a/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java +++ b/src/test/java/blackjack/domain/result/PlayerResultMatcherTest.java @@ -26,14 +26,14 @@ void setUp() { @Test @DisplayName("player가 버스트 되면 무조건 패배") void playerBusted() { - makeGamerBust(player); + makeGamerBusted(player); assertThat(PlayerResultMatcher.match(dealer, player)).isEqualTo(BlackJackResult.LOSE); } @Test @DisplayName("player 버스트 x, dealer 버스트 o") void dealerBusted() { - makeGamerBust(dealer); + makeGamerBusted(dealer); assertThat(PlayerResultMatcher.match(dealer, player)).isEqualTo(BlackJackResult.WIN); } @@ -61,7 +61,7 @@ void playerLose() { assertThat(PlayerResultMatcher.match(dealer, player)).isEqualTo(BlackJackResult.LOSE); } - private void makeGamerBust(Gamer gamer) { + private void makeGamerBusted(Gamer gamer) { gamer.draw(new Card(CardSymbol.KING, CardType.SPADE)); gamer.draw(new Card(CardSymbol.KING, CardType.CLOVER)); gamer.draw(new Card(CardSymbol.KING, CardType.DIAMOND)); From 2eaa30df4ade0e7147e40423473512f4df214a8b Mon Sep 17 00:00:00 2001 From: YebinK Date: Mon, 16 Mar 2020 15:43:44 +0900 Subject: [PATCH 19/19] =?UTF-8?q?[refactoring]=20=EC=A0=90=EC=88=98=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EB=A1=9C=EC=A7=81=20Score=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/card/Hand.java | 6 +-- .../java/blackjack/domain/gamer/Dealer.java | 6 ++- .../java/blackjack/domain/gamer/Gamer.java | 17 +++---- .../java/blackjack/domain/gamer/Player.java | 6 ++- .../domain/result/PlayerResultMatcher.java | 10 +++- ...ardCalculator.java => HandCalculator.java} | 2 +- .../java/blackjack/domain/rule/Score.java | 51 +++++++++++++++++++ src/main/java/blackjack/view/OutputView.java | 4 +- ...latorTest.java => HandCalculatorTest.java} | 4 +- 9 files changed, 83 insertions(+), 23 deletions(-) rename src/main/java/blackjack/domain/rule/{CardCalculator.java => HandCalculator.java} (96%) create mode 100644 src/main/java/blackjack/domain/rule/Score.java rename src/test/java/blackjack/domain/rule/{CardCalculatorTest.java => HandCalculatorTest.java} (94%) diff --git a/src/main/java/blackjack/domain/card/Hand.java b/src/main/java/blackjack/domain/card/Hand.java index 22a5c7e39a..e58c186d92 100644 --- a/src/main/java/blackjack/domain/card/Hand.java +++ b/src/main/java/blackjack/domain/card/Hand.java @@ -1,6 +1,6 @@ package blackjack.domain.card; -import blackjack.domain.rule.CardCalculator; +import blackjack.domain.rule.Score; import java.util.ArrayList; import java.util.List; @@ -17,8 +17,8 @@ public void add(Card card) { hand.add(card); } - public int calculateSum() { - return CardCalculator.calculate(hand); + public Score getScore() { + return Score.calculateScore(hand); } public List getCardStatus() { diff --git a/src/main/java/blackjack/domain/gamer/Dealer.java b/src/main/java/blackjack/domain/gamer/Dealer.java index cc6e432e6e..ece1083ef0 100644 --- a/src/main/java/blackjack/domain/gamer/Dealer.java +++ b/src/main/java/blackjack/domain/gamer/Dealer.java @@ -1,6 +1,7 @@ package blackjack.domain.gamer; import blackjack.domain.card.Card; +import static blackjack.domain.rule.Score.SCORES; public class Dealer extends Gamer { @@ -8,7 +9,10 @@ public class Dealer extends Gamer { @Override public boolean canDrawCard() { - return calculateSum() <= DRAW_THRESHOLD; + if (handScore().compareTo(SCORES.get(DRAW_THRESHOLD)) > 0) { + return false; + } + return true; } @Override diff --git a/src/main/java/blackjack/domain/gamer/Gamer.java b/src/main/java/blackjack/domain/gamer/Gamer.java index e4615eb9b9..3353e1dc2b 100644 --- a/src/main/java/blackjack/domain/gamer/Gamer.java +++ b/src/main/java/blackjack/domain/gamer/Gamer.java @@ -2,26 +2,24 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Hand; -import blackjack.domain.rule.CardCalculator; +import blackjack.domain.rule.Score; import java.util.List; -public abstract class Gamer implements Comparable { +public abstract class Gamer { protected Hand hand = new Hand(); - private int score = 0; public void draw(Card card) { hand.add(card); - score = hand.calculateSum(); } public boolean isBusted() { - return calculateSum() > CardCalculator.BUST_THRESHOLD; + return handScore().isBusted(); } - public int calculateSum() { - return hand.calculateSum(); + public Score handScore() { + return hand.getScore(); } public abstract boolean canDrawCard(); @@ -32,8 +30,7 @@ public List getHand() { return hand.getCardStatus(); } - @Override - public int compareTo(Gamer o) { - return Integer.compare(this.score, o.score); + public int getHandScore() { + return handScore().getNumber(); } } \ No newline at end of file diff --git a/src/main/java/blackjack/domain/gamer/Player.java b/src/main/java/blackjack/domain/gamer/Player.java index 911db08aa3..176123bab7 100644 --- a/src/main/java/blackjack/domain/gamer/Player.java +++ b/src/main/java/blackjack/domain/gamer/Player.java @@ -1,6 +1,8 @@ package blackjack.domain.gamer; -import blackjack.domain.rule.CardCalculator; +import blackjack.domain.rule.HandCalculator; + +import static blackjack.domain.rule.Score.SCORES; import java.util.Objects; @@ -14,7 +16,7 @@ public Player(String name) { @Override public boolean canDrawCard() { - return calculateSum() <= CardCalculator.BUST_THRESHOLD; + return !handScore().isBusted(); } @Override diff --git a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java index 2b052b468b..f00521a3a7 100644 --- a/src/main/java/blackjack/domain/result/PlayerResultMatcher.java +++ b/src/main/java/blackjack/domain/result/PlayerResultMatcher.java @@ -2,6 +2,7 @@ import blackjack.domain.gamer.Dealer; import blackjack.domain.gamer.Player; +import blackjack.domain.rule.Score; public class PlayerResultMatcher { @@ -16,10 +17,15 @@ public static BlackJackResult match(Dealer dealer, Player player) { } private static BlackJackResult findWinner(Dealer dealer, Player player) { - if (dealer.calculateSum() < player.calculateSum()) { + Score dealerScore = dealer.handScore(); + Score playerScore = player.handScore(); + + int matchResult = playerScore.compareTo(dealerScore); + + if (matchResult > 0) { return BlackJackResult.WIN; } - if (dealer.calculateSum() > player.calculateSum()) { + if (matchResult < 0) { return BlackJackResult.LOSE; } return BlackJackResult.DRAW; diff --git a/src/main/java/blackjack/domain/rule/CardCalculator.java b/src/main/java/blackjack/domain/rule/HandCalculator.java similarity index 96% rename from src/main/java/blackjack/domain/rule/CardCalculator.java rename to src/main/java/blackjack/domain/rule/HandCalculator.java index f3c61c0208..f51734dcf8 100644 --- a/src/main/java/blackjack/domain/rule/CardCalculator.java +++ b/src/main/java/blackjack/domain/rule/HandCalculator.java @@ -4,7 +4,7 @@ import java.util.List; -public class CardCalculator { +public class HandCalculator { public static final int BUST_THRESHOLD = 21; private static final int ACE_NUMBER_GAP = 10; diff --git a/src/main/java/blackjack/domain/rule/Score.java b/src/main/java/blackjack/domain/rule/Score.java new file mode 100644 index 0000000000..df42cdc411 --- /dev/null +++ b/src/main/java/blackjack/domain/rule/Score.java @@ -0,0 +1,51 @@ +package blackjack.domain.rule; + +import blackjack.domain.card.Card; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Score implements Comparable { + + private static final int BUSTED = 0; + private static final int BUST_THRESHOLD = 21; + + public static final Map SCORES; + + private int score; + + static { + SCORES = new HashMap<>(); + + for (int i = 0; i <= BUST_THRESHOLD; i++) { + SCORES.put(i, new Score(i)); + } + } + + private Score(int score) { + this.score = score; + } + + public static Score calculateScore(List cards) { + int score = HandCalculator.calculate(cards); + + if (score > BUST_THRESHOLD) { + return SCORES.get(BUSTED); + } + return SCORES.get(score); + } + + public boolean isBusted() { + return score == BUSTED; + } + + public int getNumber() { + return score; + } + + @Override + public int compareTo(Score target) { + return Integer.compare(this.score, target.score); + } +} diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 7a33a118b3..c6ccb50276 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -60,12 +60,12 @@ private static void printInitialHand(Dealer dealer, Players players) { } private static void printCardStatusAndResult(Gamer gamer) { - System.out.println(String.format(CARD_STATUS_AND_RESULT_FORMAT, gamer.getName(), makeHandResult(gamer.getHand()), gamer.calculateSum())); + System.out.println(String.format(CARD_STATUS_AND_RESULT_FORMAT, gamer.getName(), makeHandResult(gamer.getHand()), gamer.getHandScore())); } private static String makeHandResult(List cards) { return cards.stream() - .map(card -> String.format(CARD_FORMAT, card.getCardNumber(), card.getCardType())) + .map(card -> String.format(CARD_FORMAT, card.getCardNumber(), card.getCardType().getKoreanName())) .collect(Collectors.joining(DELIMITER)); } diff --git a/src/test/java/blackjack/domain/rule/CardCalculatorTest.java b/src/test/java/blackjack/domain/rule/HandCalculatorTest.java similarity index 94% rename from src/test/java/blackjack/domain/rule/CardCalculatorTest.java rename to src/test/java/blackjack/domain/rule/HandCalculatorTest.java index 72156c900f..d0fc94ce5b 100644 --- a/src/test/java/blackjack/domain/rule/CardCalculatorTest.java +++ b/src/test/java/blackjack/domain/rule/HandCalculatorTest.java @@ -14,13 +14,13 @@ import static org.assertj.core.api.Assertions.assertThat; -class CardCalculatorTest { +class HandCalculatorTest { @ParameterizedTest @MethodSource("createCards") @DisplayName("카드의 합을 구하는 테스트") void calculateSum(List cards, int result) { - assertThat(CardCalculator.calculate(cards)).isEqualTo(result); + assertThat(HandCalculator.calculate(cards)).isEqualTo(result); } private static Stream createCards() {