From e504ca06e3189abbc771add4cd25bef7f5dec83a Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 5 Mar 2024 14:00:05 +0900 Subject: [PATCH 001/100] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=20=EC=82=AC=ED=95=AD=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000000..84afc684a5 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,34 @@ +# 기능 요구 사항 + +* [ ] 참여자 이름 입력 받는다. + * [ ] 참여자는 쉼표(,)로 구분한다. + * [ ] null 이나 공백인 경우 예외가 발생한다. + * [ ] 쉼표로 시작시 예외가 발생한다. ex) ,pobi,jason + * [ ] 쉼표로 끝날시 예외가 발생한다. ex) pobi,jason, + * [ ] 쉼표가 연속으로 올시 예외가 발생한다. ex) pobi,,jason + * [ ] 참여자 이름이 중복시 예외가 발생한다. +* [ ] 총 참여자의 수는 2이상 8이하여야 한다. + +
+ +* [ ] 딜러가 참여자마다 2장 카드 분배한다. + * [ ] 카드는 총 6벌을 둔다. (52 * 6) +* [ ] 딜러의 카드를 출력한다. (1장) +* [ ] 참여자의 카드를 출력한다. (2장) + +
+ +* [ ] 참여자는 hit(y) / stay(n)를 선택한다. + * [ ] y, n 가 아닐시 예외가 발생한다. +* [ ] 현재 가지고 있는 카드를 출력한다. + +
+ +* [ ] 딜러의 카드의 합을 계산한다. + * [ ] 17 이상이 될때까지 카드를 받는다. + +
+ +* [ ] 모든 참여자의 카드의 합을 계산한다. +* [ ] 최종 승패를 확인한다. +* [ ] 게임 결과를 출력한다. From 3ed10c0941218ff63b68033c087c63b9f345d932 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 14:34:51 +0900 Subject: [PATCH 002/100] =?UTF-8?q?test:=20=EC=B9=B4=EB=93=9C=20=ED=95=A9?= =?UTF-8?q?=20=EA=B5=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/Card.java | 26 ++++++++++++++++++ src/test/java/domain/CardCalculator.java | 7 +++++ src/test/java/domain/CardCalculatorTest.java | 23 ++++++++++++++++ src/test/java/domain/CardNumber.java | 28 ++++++++++++++++++++ src/test/java/domain/CardShape.java | 14 ++++++++++ 5 files changed, 98 insertions(+) create mode 100644 src/test/java/domain/Card.java create mode 100644 src/test/java/domain/CardCalculator.java create mode 100644 src/test/java/domain/CardCalculatorTest.java create mode 100644 src/test/java/domain/CardNumber.java create mode 100644 src/test/java/domain/CardShape.java diff --git a/src/test/java/domain/Card.java b/src/test/java/domain/Card.java new file mode 100644 index 0000000000..0d7c6101b6 --- /dev/null +++ b/src/test/java/domain/Card.java @@ -0,0 +1,26 @@ +package domain; + +import java.util.Objects; + +public class Card { + private final CardNumber cardNumber; + private final CardShape cardShape; + + public Card(final CardNumber cardNumber, final CardShape cardShape) { + this.cardNumber = cardNumber; + this.cardShape = cardShape; + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Card card = (Card) o; + return cardNumber == card.cardNumber && cardShape == card.cardShape; + } + + @Override + public int hashCode() { + return Objects.hash(cardNumber, cardShape); + } +} diff --git a/src/test/java/domain/CardCalculator.java b/src/test/java/domain/CardCalculator.java new file mode 100644 index 0000000000..1f87791154 --- /dev/null +++ b/src/test/java/domain/CardCalculator.java @@ -0,0 +1,7 @@ +package domain; + +public class CardCalculator { + public int sum(final Card card1, final Card card2, final Card card3) { + return 21; + } +} diff --git a/src/test/java/domain/CardCalculatorTest.java b/src/test/java/domain/CardCalculatorTest.java new file mode 100644 index 0000000000..7d128dbd54 --- /dev/null +++ b/src/test/java/domain/CardCalculatorTest.java @@ -0,0 +1,23 @@ +package domain; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class CardCalculatorTest { + @DisplayName("카드의 합을 구한다.") + @Test + void sum() { + // given + CardCalculator cardCalculator = new CardCalculator(); + Card card1 = new Card(CardNumber.TWO, CardShape.HEART); + Card card2 = new Card(CardNumber.EIGHT, CardShape.SPADE); + Card card3 = new Card(CardNumber.ACE, CardShape.CLOVER); + + // when + int result = cardCalculator.sum(card1, card2, card3); + + // then + Assertions.assertThat(result).isEqualTo(21); + } +} diff --git a/src/test/java/domain/CardNumber.java b/src/test/java/domain/CardNumber.java new file mode 100644 index 0000000000..522268d564 --- /dev/null +++ b/src/test/java/domain/CardNumber.java @@ -0,0 +1,28 @@ +package domain; + +//TODO Number말고 더 좋은 클래스명짓기 +public enum CardNumber { + + ACE("A", 1), + ONE("1", 1), + TWO("2", 2), + THREE("3", 3), + FOUR("4", 4), + FIVE("5", 5), + SIX("6", 6), + SEVEN("7", 7), + EIGHT("8", 8), + NINE("9", 9), + JACK("J", 10), + QUEEN("Q", 10), + KING("K", 10); + + + private final String name; + private final int value; + + CardNumber(final String name, final int value) { + this.name = name; + this.value = value; + } +} diff --git a/src/test/java/domain/CardShape.java b/src/test/java/domain/CardShape.java new file mode 100644 index 0000000000..63ab7b7240 --- /dev/null +++ b/src/test/java/domain/CardShape.java @@ -0,0 +1,14 @@ +package domain; + +public enum CardShape { + SPADE("스페이드"), + HEART("하트"), + CLOVER("클로버"), + DIAMOND("다이아몬드"); + + private final String name; + + CardShape(final String name) { + this.name = name; + } +} From 42a28a9ce23559c02d0b6e6e7b244e635bac73c2 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 5 Mar 2024 14:58:35 +0900 Subject: [PATCH 003/100] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=ED=95=A9?= =?UTF-8?q?=20=EA=B5=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/Card.java | 4 +++ src/test/java/domain/CardCalculator.java | 8 +++-- src/test/java/domain/CardCalculatorTest.java | 38 +++++++++++++++----- src/test/java/domain/CardNumber.java | 6 +++- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/test/java/domain/Card.java b/src/test/java/domain/Card.java index 0d7c6101b6..d291b79349 100644 --- a/src/test/java/domain/Card.java +++ b/src/test/java/domain/Card.java @@ -11,6 +11,10 @@ public Card(final CardNumber cardNumber, final CardShape cardShape) { this.cardShape = cardShape; } + public int getCardNumber() { + return cardNumber.getValue(); + } + @Override public boolean equals(final Object o) { if (this == o) return true; diff --git a/src/test/java/domain/CardCalculator.java b/src/test/java/domain/CardCalculator.java index 1f87791154..518140a935 100644 --- a/src/test/java/domain/CardCalculator.java +++ b/src/test/java/domain/CardCalculator.java @@ -1,7 +1,11 @@ package domain; +import java.util.List; + public class CardCalculator { - public int sum(final Card card1, final Card card2, final Card card3) { - return 21; + public int sum(final List cards) { + return cards.stream() + .mapToInt(Card::getCardNumber) + .sum(); } } diff --git a/src/test/java/domain/CardCalculatorTest.java b/src/test/java/domain/CardCalculatorTest.java index 7d128dbd54..6511a644a8 100644 --- a/src/test/java/domain/CardCalculatorTest.java +++ b/src/test/java/domain/CardCalculatorTest.java @@ -1,23 +1,43 @@ package domain; +import java.util.List; +import java.util.stream.Stream; import org.assertj.core.api.Assertions; 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; -public class CardCalculatorTest { +class CardCalculatorTest { @DisplayName("카드의 합을 구한다.") - @Test - void sum() { + @ParameterizedTest + @MethodSource("sumParameterProvider") + void sum(List cards, int expected) { // given CardCalculator cardCalculator = new CardCalculator(); - Card card1 = new Card(CardNumber.TWO, CardShape.HEART); - Card card2 = new Card(CardNumber.EIGHT, CardShape.SPADE); - Card card3 = new Card(CardNumber.ACE, CardShape.CLOVER); // when - int result = cardCalculator.sum(card1, card2, card3); + int result = cardCalculator.sum(cards); // then - Assertions.assertThat(result).isEqualTo(21); + Assertions.assertThat(result).isEqualTo(expected); + } + + + static Stream sumParameterProvider() { + return Stream.of( + Arguments.of( + List.of(new Card(CardNumber.TWO, CardShape.HEART), + new Card(CardNumber.EIGHT, CardShape.SPADE), + new Card(CardNumber.JACK, CardShape.CLOVER)), + 20 + ), + Arguments.of( + List.of(new Card(CardNumber.THREE, CardShape.DIAMOND), + new Card(CardNumber.NINE, CardShape.CLOVER), + new Card(CardNumber.NINE, CardShape.CLOVER)), + 21 + ) + ); } } diff --git a/src/test/java/domain/CardNumber.java b/src/test/java/domain/CardNumber.java index 522268d564..b39f3bfed5 100644 --- a/src/test/java/domain/CardNumber.java +++ b/src/test/java/domain/CardNumber.java @@ -3,7 +3,7 @@ //TODO Number말고 더 좋은 클래스명짓기 public enum CardNumber { - ACE("A", 1), + ACE("A", 1), // TODO: 1 혹은 11 ONE("1", 1), TWO("2", 2), THREE("3", 3), @@ -25,4 +25,8 @@ public enum CardNumber { this.name = name; this.value = value; } + + public int getValue() { + return value; + } } From 8972ef51f2781a935e5f2f826260218244160802 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 15:06:51 +0900 Subject: [PATCH 004/100] =?UTF-8?q?chore:=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/{test => main}/java/domain/Card.java | 0 src/{test => main}/java/domain/CardCalculator.java | 0 src/{test => main}/java/domain/CardNumber.java | 0 src/{test => main}/java/domain/CardShape.java | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename src/{test => main}/java/domain/Card.java (100%) rename src/{test => main}/java/domain/CardCalculator.java (100%) rename src/{test => main}/java/domain/CardNumber.java (100%) rename src/{test => main}/java/domain/CardShape.java (100%) diff --git a/src/test/java/domain/Card.java b/src/main/java/domain/Card.java similarity index 100% rename from src/test/java/domain/Card.java rename to src/main/java/domain/Card.java diff --git a/src/test/java/domain/CardCalculator.java b/src/main/java/domain/CardCalculator.java similarity index 100% rename from src/test/java/domain/CardCalculator.java rename to src/main/java/domain/CardCalculator.java diff --git a/src/test/java/domain/CardNumber.java b/src/main/java/domain/CardNumber.java similarity index 100% rename from src/test/java/domain/CardNumber.java rename to src/main/java/domain/CardNumber.java diff --git a/src/test/java/domain/CardShape.java b/src/main/java/domain/CardShape.java similarity index 100% rename from src/test/java/domain/CardShape.java rename to src/main/java/domain/CardShape.java From 3a7d4d414ce4f8aaf56079fe8b55d063cb61cd2d Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 15:11:42 +0900 Subject: [PATCH 005/100] =?UTF-8?q?feat:=20=EC=B0=B8=EC=97=AC=EC=9E=90=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EA=B8=B0=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 --- src/main/java/domain/Player.java | 9 +++++++++ src/test/java/domain/PlayerTest.java | 14 ++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/main/java/domain/Player.java create mode 100644 src/test/java/domain/PlayerTest.java diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java new file mode 100644 index 0000000000..03a0a58c29 --- /dev/null +++ b/src/main/java/domain/Player.java @@ -0,0 +1,9 @@ +package domain; + +public class Player { + private final String name; + + public Player(final String name) { + this.name = name; + } +} diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java new file mode 100644 index 0000000000..1cc1529fb7 --- /dev/null +++ b/src/test/java/domain/PlayerTest.java @@ -0,0 +1,14 @@ +package domain; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class PlayerTest { + @DisplayName("이름으로 참여자를 생성한다.") + @Test + void createPlayerWithName() { + Assertions.assertThatCode(() -> new Player("pobi")) + .doesNotThrowAnyException(); + } +} From d719d08327de1d4e811d2bee1d6388a983791bb0 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 15:18:18 +0900 Subject: [PATCH 006/100] =?UTF-8?q?feat:=20null=20=EC=9D=B4=EB=82=98=20?= =?UTF-8?q?=EA=B3=B5=EB=B0=B1=EC=9D=B8=20=EA=B2=BD=EC=9A=B0=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EA=B0=80=20=EB=B0=9C=EC=83=9D=20=EA=B8=B0=EB=8A=A5=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 --- docs/README.md | 2 +- src/main/java/domain/Player.java | 18 ++++++++++++++++++ src/test/java/domain/PlayerTest.java | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 84afc684a5..b9c70153af 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ * [ ] 참여자 이름 입력 받는다. * [ ] 참여자는 쉼표(,)로 구분한다. - * [ ] null 이나 공백인 경우 예외가 발생한다. + * [X] null 이나 공백인 경우 예외가 발생한다. * [ ] 쉼표로 시작시 예외가 발생한다. ex) ,pobi,jason * [ ] 쉼표로 끝날시 예외가 발생한다. ex) pobi,jason, * [ ] 쉼표가 연속으로 올시 예외가 발생한다. ex) pobi,,jason diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 03a0a58c29..f317188600 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -4,6 +4,24 @@ public class Player { private final String name; public Player(final String name) { + validate(name); this.name = name; } + + private void validate(final String name) { + validateNull(name); + validateBlank(name); + } + + private void validateNull(final String name) { + if (name == null) { + throw new IllegalArgumentException("[ERROR] 유효하지 않은 이름입니다."); + } + } + + private void validateBlank(final String name) { + if (name.isBlank()) { + throw new IllegalArgumentException("[ERROR] 이름에 공백을 입력할 수 없습니다."); + } + } } diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index 1cc1529fb7..8f4bfcbda1 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -3,6 +3,9 @@ import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; public class PlayerTest { @DisplayName("이름으로 참여자를 생성한다.") @@ -11,4 +14,13 @@ void createPlayerWithName() { Assertions.assertThatCode(() -> new Player("pobi")) .doesNotThrowAnyException(); } + + @DisplayName("null이나 공백을 입력하면 예외를 발생시킨다.") + @ParameterizedTest + @ValueSource(strings = {"", " ", " "}) + @NullSource + void nullOrBlankInputThrowException(String name) { + Assertions.assertThatThrownBy(() -> new Player(name)) + .isInstanceOf(IllegalArgumentException.class); + } } From a83c06d9629213c1b549cdec94d998962ca1106c Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 5 Mar 2024 15:29:37 +0900 Subject: [PATCH 007/100] =?UTF-8?q?feat:=20=EC=B0=B8=EC=97=AC=EC=9E=90=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=9D=B4=20=EC=A4=91=EB=B3=B5=EC=8B=9C=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D=20=EA=B8=B0=EB=8A=A5=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 --- docs/README.md | 2 +- src/main/java/domain/Player.java | 22 +++++++++++++++++++-- src/main/java/domain/Players.java | 28 +++++++++++++++++++++++++++ src/test/java/domain/PlayersTest.java | 20 +++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/main/java/domain/Players.java create mode 100644 src/test/java/domain/PlayersTest.java diff --git a/docs/README.md b/docs/README.md index b9c70153af..2cfe2d5286 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,7 @@ * [ ] 쉼표로 시작시 예외가 발생한다. ex) ,pobi,jason * [ ] 쉼표로 끝날시 예외가 발생한다. ex) pobi,jason, * [ ] 쉼표가 연속으로 올시 예외가 발생한다. ex) pobi,,jason - * [ ] 참여자 이름이 중복시 예외가 발생한다. + * [x] 참여자 이름이 중복시 예외가 발생한다. * [ ] 총 참여자의 수는 2이상 8이하여야 한다.
diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index f317188600..8cde19098d 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -1,5 +1,7 @@ package domain; +import java.util.Objects; + public class Player { private final String name; @@ -15,13 +17,29 @@ private void validate(final String name) { private void validateNull(final String name) { if (name == null) { - throw new IllegalArgumentException("[ERROR] 유효하지 않은 이름입니다."); + throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 이름입니다."); } } private void validateBlank(final String name) { if (name.isBlank()) { - throw new IllegalArgumentException("[ERROR] 이름에 공백을 입력할 수 없습니다."); + throw new IllegalArgumentException("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); + } + } + + @Override + public boolean equals(final Object target) { + if (this == target) { + return true; } + if (!(target instanceof Player player)) { + return true; + } + return Objects.equals(name, player.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); } } diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java new file mode 100644 index 0000000000..ee6b93b532 --- /dev/null +++ b/src/main/java/domain/Players.java @@ -0,0 +1,28 @@ +package domain; + +import java.util.List; +import java.util.Set; + +public class Players { + + private final List names; + + private Players(final List names) { + this.names = names; + } + + public static Players from(final List names) { + List players = names.stream() + .map(Player::new) + .toList(); + validateDuplicate(players); + return new Players(players); + } + + private static void validateDuplicate(final List players) { + if (players.size() != Set.copyOf(players).size()) { + throw new IllegalArgumentException("[ERROR] 참여자 이름은 중복될 수 없습니다."); + } + } + +} diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java new file mode 100644 index 0000000000..0012cfcf2d --- /dev/null +++ b/src/test/java/domain/PlayersTest.java @@ -0,0 +1,20 @@ +package domain; + +import java.util.List; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class PlayersTest { + + @Test + @DisplayName("참여자 이름 중복시 예외가 발생한다.") + void duplicatePlayerName() { + //given + List names = List.of("redy", "redy"); + + //when & then + Assertions.assertThatThrownBy(() -> Players.from(names)) + .isInstanceOf(IllegalArgumentException.class); + } +} From 3a1cd4894385eff12c6d83310fc5e4d2808a350a Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 15:47:24 +0900 Subject: [PATCH 008/100] =?UTF-8?q?feat:=20=EC=B4=9D=20=EC=B0=B8=EC=97=AC?= =?UTF-8?q?=EC=9E=90=EC=9D=98=20=EC=88=98=EB=8A=94=202=EC=9D=B4=EC=83=81?= =?UTF-8?q?=208=EC=9D=B4=ED=95=98=EA=B0=80=20=EC=95=84=EB=8B=90=EC=8B=9C?= =?UTF-8?q?=20=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D=20=EA=B8=B0=EB=8A=A5?= =?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 --- docs/README.md | 4 +- src/main/java/domain/Players.java | 15 ++++++- src/test/java/domain/CardCalculatorTest.java | 6 +-- src/test/java/domain/PlayersTest.java | 47 +++++++++++++++++++- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/docs/README.md b/docs/README.md index 2cfe2d5286..408b6cc199 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,12 +2,12 @@ * [ ] 참여자 이름 입력 받는다. * [ ] 참여자는 쉼표(,)로 구분한다. - * [X] null 이나 공백인 경우 예외가 발생한다. + * [x] null 이나 공백인 경우 예외가 발생한다. * [ ] 쉼표로 시작시 예외가 발생한다. ex) ,pobi,jason * [ ] 쉼표로 끝날시 예외가 발생한다. ex) pobi,jason, * [ ] 쉼표가 연속으로 올시 예외가 발생한다. ex) pobi,,jason * [x] 참여자 이름이 중복시 예외가 발생한다. -* [ ] 총 참여자의 수는 2이상 8이하여야 한다. +* [x] 총 참여자의 수는 2이상 8이하여야 한다.
diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index ee6b93b532..e5bba4f9ad 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -4,6 +4,8 @@ import java.util.Set; public class Players { + private static final int MIN_SIZE = 2; + private static final int MAX_SIZE = 8; private final List names; @@ -15,10 +17,21 @@ public static Players from(final List names) { List players = names.stream() .map(Player::new) .toList(); - validateDuplicate(players); + validate(players); return new Players(players); } + private static void validate(final List players) { + validateSize(players); + validateDuplicate(players); + } + + private static void validateSize(final List players) { + if (players.size() < MIN_SIZE || MAX_SIZE < players.size()) { + throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 수입니다."); + } + } + private static void validateDuplicate(final List players) { if (players.size() != Set.copyOf(players).size()) { throw new IllegalArgumentException("[ERROR] 참여자 이름은 중복될 수 없습니다."); diff --git a/src/test/java/domain/CardCalculatorTest.java b/src/test/java/domain/CardCalculatorTest.java index 6511a644a8..18ef3b2a15 100644 --- a/src/test/java/domain/CardCalculatorTest.java +++ b/src/test/java/domain/CardCalculatorTest.java @@ -12,12 +12,12 @@ class CardCalculatorTest { @DisplayName("카드의 합을 구한다.") @ParameterizedTest @MethodSource("sumParameterProvider") - void sum(List cards, int expected) { + void sum(final List cards, final int expected) { // given - CardCalculator cardCalculator = new CardCalculator(); + final CardCalculator cardCalculator = new CardCalculator(); // when - int result = cardCalculator.sum(cards); + final int result = cardCalculator.sum(cards); // then Assertions.assertThat(result).isEqualTo(expected); diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index 0012cfcf2d..15d199c942 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -1,9 +1,14 @@ package domain; import java.util.List; +import java.util.stream.Stream; + import org.assertj.core.api.Assertions; 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; class PlayersTest { @@ -15,6 +20,46 @@ void duplicatePlayerName() { //when & then Assertions.assertThatThrownBy(() -> Players.from(names)) - .isInstanceOf(IllegalArgumentException.class); + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 참여자 이름은 중복될 수 없습니다."); + } + + @DisplayName("총 참여자 수가 2이상 8이하이면 참여자를 생성한다.") + @ParameterizedTest + @MethodSource("validPlayersSizeParameterProvider") + void validPlayersSize(final List names) { + Assertions.assertThatCode(() -> Players.from(names)) + .doesNotThrowAnyException(); + } + + @DisplayName("총 참여자 수는 2이상 8이하가 아니면 예외가 발생한다.") + @ParameterizedTest + @MethodSource("invalidPlayersSizeParameterProvider") + void invalidPlayersSize(final List names) { + Assertions.assertThatThrownBy(() -> Players.from(names)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 유효하지 않은 참여자 수입니다."); + } + + static Stream validPlayersSizeParameterProvider() { + return Stream.of( + Arguments.of( + List.of("pobi", "jason") + ), + Arguments.of( + List.of("1", "2", "3", "4", "5", "6", "7", "8") + ) + ); + } + + static Stream invalidPlayersSizeParameterProvider() { + return Stream.of( + Arguments.of( + List.of("pobi") + ), + Arguments.of( + List.of("1", "2", "3", "4", "5", "6", "7", "8", "9") + ) + ); } } From acf668012b9df9a879d32ea90707457170141d00 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 5 Mar 2024 16:07:16 +0900 Subject: [PATCH 009/100] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=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/domain/Packet.java | 25 +++++++++++++++++++++++++ src/test/java/domain/PacketTest.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/domain/Packet.java create mode 100644 src/test/java/domain/PacketTest.java diff --git a/src/main/java/domain/Packet.java b/src/main/java/domain/Packet.java new file mode 100644 index 0000000000..3c57fe6b33 --- /dev/null +++ b/src/main/java/domain/Packet.java @@ -0,0 +1,25 @@ +package domain; + +import java.util.ArrayList; +import java.util.List; + +public class Packet { //TODO: 더 좋은 이름이 있다면 날 설득해줘 + + private final List cards; + + public Packet() { + this.cards = new ArrayList<>(); + } + + public Packet(final List cards) { + this.cards = cards; + } + + public void add(final Card card) { + cards.add(card); + } + + public int size() { + return cards.size(); + } +} diff --git a/src/test/java/domain/PacketTest.java b/src/test/java/domain/PacketTest.java new file mode 100644 index 0000000000..197b63f48c --- /dev/null +++ b/src/test/java/domain/PacketTest.java @@ -0,0 +1,28 @@ +package domain; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class PacketTest { + + @Test + @DisplayName("카드를 가지고 있는 객체를 생성한다.") + void createPacket() { + Assertions.assertThatCode(Packet::new) + .doesNotThrowAnyException(); + } + + @Test + @DisplayName("카드를 추가한다.") + void addCard() { + //given + final Packet packet = new Packet(); + + //when + packet.add(new Card(CardNumber.EIGHT, CardShape.CLOVER)); + + //then + Assertions.assertThat(packet.size()).isEqualTo(1); + } +} From 68fa5481cf8b5d3fc4061b60214b1f5387d0bfea Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 16:22:04 +0900 Subject: [PATCH 010/100] =?UTF-8?q?feat:=20Player=EA=B0=80=20Packet?= =?UTF-8?q?=EC=9D=84=20=ED=95=84=EB=93=9C=EB=A1=9C=20=EA=B0=96=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=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/domain/Packet.java | 8 ++++---- src/main/java/domain/Player.java | 4 +++- src/main/java/domain/Players.java | 6 ++++-- src/test/java/domain/PacketTest.java | 6 ++++-- src/test/java/domain/PlayerTest.java | 4 ++-- src/test/java/domain/PlayersTest.java | 2 +- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/domain/Packet.java b/src/main/java/domain/Packet.java index 3c57fe6b33..19bfcbb649 100644 --- a/src/main/java/domain/Packet.java +++ b/src/main/java/domain/Packet.java @@ -7,12 +7,12 @@ public class Packet { //TODO: 더 좋은 이름이 있다면 날 설득해줘 private final List cards; - public Packet() { - this.cards = new ArrayList<>(); + private Packet(final List cards) { + this.cards = cards; } - public Packet(final List cards) { - this.cards = cards; + public static Packet createEmptyPacket() { + return new Packet(new ArrayList<>()); } public void add(final Card card) { diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 8cde19098d..fcf4eda271 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -4,10 +4,12 @@ public class Player { private final String name; + private final Packet packet; - public Player(final String name) { + public Player(final String name, final Packet packet) { validate(name); this.name = name; + this.packet = packet; } private void validate(final String name) { diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index e5bba4f9ad..224c308649 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -1,9 +1,11 @@ package domain; +import java.util.ArrayList; import java.util.List; import java.util.Set; public class Players { + private static final int MIN_SIZE = 2; private static final int MAX_SIZE = 8; @@ -15,10 +17,10 @@ private Players(final List names) { public static Players from(final List names) { List players = names.stream() - .map(Player::new) + .map(name -> new Player(name, Packet.createEmptyPacket())) .toList(); validate(players); - return new Players(players); + return new Players(players); //TODO 이름바꾸기 } private static void validate(final List players) { diff --git a/src/test/java/domain/PacketTest.java b/src/test/java/domain/PacketTest.java index 197b63f48c..845b3ebbdf 100644 --- a/src/test/java/domain/PacketTest.java +++ b/src/test/java/domain/PacketTest.java @@ -4,12 +4,14 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.ArrayList; + class PacketTest { @Test @DisplayName("카드를 가지고 있는 객체를 생성한다.") void createPacket() { - Assertions.assertThatCode(Packet::new) + Assertions.assertThatCode(() -> Packet.createEmptyPacket()) .doesNotThrowAnyException(); } @@ -17,7 +19,7 @@ void createPacket() { @DisplayName("카드를 추가한다.") void addCard() { //given - final Packet packet = new Packet(); + final Packet packet = Packet.createEmptyPacket(); //when packet.add(new Card(CardNumber.EIGHT, CardShape.CLOVER)); diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index 8f4bfcbda1..59263dc00a 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -11,7 +11,7 @@ public class PlayerTest { @DisplayName("이름으로 참여자를 생성한다.") @Test void createPlayerWithName() { - Assertions.assertThatCode(() -> new Player("pobi")) + Assertions.assertThatCode(() -> new Player("pobi", Packet.createEmptyPacket())) .doesNotThrowAnyException(); } @@ -20,7 +20,7 @@ void createPlayerWithName() { @ValueSource(strings = {"", " ", " "}) @NullSource void nullOrBlankInputThrowException(String name) { - Assertions.assertThatThrownBy(() -> new Player(name)) + Assertions.assertThatThrownBy(() -> new Player(name, Packet.createEmptyPacket())) .isInstanceOf(IllegalArgumentException.class); } } diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index 15d199c942..617f85ddd2 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -16,7 +16,7 @@ class PlayersTest { @DisplayName("참여자 이름 중복시 예외가 발생한다.") void duplicatePlayerName() { //given - List names = List.of("redy", "redy"); + final List names = List.of("redy", "redy"); //when & then Assertions.assertThatThrownBy(() -> Players.from(names)) From fbf514fbec24e87f60bdf1a73fc964cb22a13c5c Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 17:10:42 +0900 Subject: [PATCH 011/100] =?UTF-8?q?feat:=20CardDeck=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=20=EA=B8=B0=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 --- src/main/java/domain/CardDeck.java | 36 ++++++++++++++++++++++++++ src/test/java/domain/CardDeckTest.java | 17 ++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/domain/CardDeck.java create mode 100644 src/test/java/domain/CardDeckTest.java diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java new file mode 100644 index 0000000000..1e9778774b --- /dev/null +++ b/src/main/java/domain/CardDeck.java @@ -0,0 +1,36 @@ +package domain; + +import java.util.ArrayList; +import java.util.List; + +public class CardDeck { + private static final int DECK_SIZE = 6; + + private List cards; + + private CardDeck(final List cards) { + this.cards = cards; + } + + public static CardDeck generate() { + List cards = new ArrayList<>(); + + for (int i = 0; i < DECK_SIZE; i++) { + generateOneCardDeck(cards); + } + + return new CardDeck(cards); + } + + private static void generateOneCardDeck(final List cards) { //TODO 인덴트 줄이기 + for (CardShape cardShape : CardShape.values()) { + for (CardNumber cardNumber : CardNumber.values()) { + cards.add(new Card(cardNumber, cardShape)); + } + } + } + + public int size() { + return cards.size(); + } +} diff --git a/src/test/java/domain/CardDeckTest.java b/src/test/java/domain/CardDeckTest.java new file mode 100644 index 0000000000..33294600de --- /dev/null +++ b/src/test/java/domain/CardDeckTest.java @@ -0,0 +1,17 @@ +package domain; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class CardDeckTest { + @DisplayName("카드를 52 * 6 만큼 생성한다.") + @Test + void generate() { + // given & when + CardDeck cardDeck = CardDeck.generate(); + + // then + Assertions.assertThat(cardDeck.size()).isEqualTo(52 * 6); + } +} From 6017614cf2f98490aa517e8526906a8fce8ebf8f Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 5 Mar 2024 17:35:56 +0900 Subject: [PATCH 012/100] =?UTF-8?q?test:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EC=97=90=EA=B2=8C=20=EC=B9=B4=EB=93=9C=202=EC=9E=A5?= =?UTF-8?q?=EC=9D=84=20=EB=82=98=EB=88=A0=EC=A3=BC=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 --- docs/README.md | 2 +- src/main/java/domain/CardDeck.java | 5 +++++ src/main/java/domain/Dealer.java | 17 +++++++++++++++++ src/main/java/domain/Player.java | 4 ++++ src/main/java/domain/Players.java | 4 +++- src/test/java/domain/DealerTest.java | 28 ++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/main/java/domain/Dealer.java create mode 100644 src/test/java/domain/DealerTest.java diff --git a/docs/README.md b/docs/README.md index 408b6cc199..88e9b454e8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,7 @@
* [ ] 딜러가 참여자마다 2장 카드 분배한다. - * [ ] 카드는 총 6벌을 둔다. (52 * 6) + * [x] 카드는 총 6벌을 둔다. (52 * 6) * [ ] 딜러의 카드를 출력한다. (1장) * [ ] 참여자의 카드를 출력한다. (2장) diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java index 1e9778774b..7820a46c20 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/CardDeck.java @@ -1,6 +1,7 @@ package domain; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class CardDeck { @@ -33,4 +34,8 @@ private static void generateOneCardDeck(final List cards) { //TODO 인덴 public int size() { return cards.size(); } + + public void shuffle() { + Collections.shuffle(cards); + } } diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java new file mode 100644 index 0000000000..a295be66f7 --- /dev/null +++ b/src/main/java/domain/Dealer.java @@ -0,0 +1,17 @@ +package domain; + +public class Dealer { + + private final Players players; + private final CardDeck cardDeck; + + public Dealer(final Players players, final CardDeck cardDeck) { + this.players = players; + this.cardDeck = cardDeck; + } + + public void startDeal() { + cardDeck.shuffle(); + + } +} diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index fcf4eda271..4ec5b880fd 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -44,4 +44,8 @@ public boolean equals(final Object target) { public int hashCode() { return Objects.hash(name); } + + public int getPacketSize() { + return 2; + } } diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index 224c308649..bf84e5e28e 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -1,6 +1,5 @@ package domain; -import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -40,4 +39,7 @@ private static void validateDuplicate(final List players) { } } + public List getNames() { + return names; + } } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java new file mode 100644 index 0000000000..69adb88a41 --- /dev/null +++ b/src/test/java/domain/DealerTest.java @@ -0,0 +1,28 @@ +package domain; + +import java.util.List; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class DealerTest { + + @Test + @DisplayName("플레이어에게 카드 2장을 나눠준다.") + void dealCards() { + //given + final Players players = Players.from(List.of("레디", "제제")); + final CardDeck cardDeck = CardDeck.generate(); + final Dealer dealer = new Dealer(players, cardDeck); + + //when + dealer.startDeal(); + List names = players.getNames(); + + //then + boolean allMatch = names.stream() + .allMatch(player -> player.getPacketSize() == 2); + + Assertions.assertThat(allMatch).isTrue(); + } +} From ac2aa2268b7971acd4d91167e3158328d92c5d13 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 17:47:02 +0900 Subject: [PATCH 013/100] =?UTF-8?q?refactor:=20=EB=A6=AC=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EC=8A=A4=ED=83=9D=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/CardDeck.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java index 7820a46c20..bb884fc9bc 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/CardDeck.java @@ -1,34 +1,37 @@ package domain; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; +import java.util.Stack; public class CardDeck { private static final int DECK_SIZE = 6; - private List cards; + private Stack cards; - private CardDeck(final List cards) { + public CardDeck(final Stack cards) { this.cards = cards; } public static CardDeck generate() { - List cards = new ArrayList<>(); + Stack result = new Stack<>(); for (int i = 0; i < DECK_SIZE; i++) { - generateOneCardDeck(cards); + result.addAll(generateOneCardDeck()); } - return new CardDeck(cards); + return new CardDeck(result); } - private static void generateOneCardDeck(final List cards) { //TODO 인덴트 줄이기 + private static Stack generateOneCardDeck() { //TODO 인덴트 줄이기 + Stack cards = new Stack<>(); + for (CardShape cardShape : CardShape.values()) { for (CardNumber cardNumber : CardNumber.values()) { cards.add(new Card(cardNumber, cardShape)); } } + + return cards; } public int size() { From aa6549c6e2d2383adcd9ef0b166949c68899fb4e Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 18:04:30 +0900 Subject: [PATCH 014/100] =?UTF-8?q?feat:=20=EC=B0=B8=EC=97=AC=EC=9E=90?= =?UTF-8?q?=EC=97=90=EA=B2=8C=20=EC=B9=B4=EB=93=9C=EB=A5=BC=202=EC=9E=A5?= =?UTF-8?q?=EC=94=A9=20=EB=B6=84=EB=B0=B0=ED=95=98=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 --- docs/README.md | 2 +- src/main/java/domain/CardDeck.java | 4 ++++ src/main/java/domain/Dealer.java | 4 ++++ src/main/java/domain/Player.java | 12 ++++++++---- src/main/java/domain/Players.java | 5 +++++ src/test/java/domain/DealerTest.java | 6 +----- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/docs/README.md b/docs/README.md index 88e9b454e8..8f12d1a0e0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -11,7 +11,7 @@
-* [ ] 딜러가 참여자마다 2장 카드 분배한다. +* [ ] 딜러가 카드 2장을 분해하다. * [x] 카드는 총 6벌을 둔다. (52 * 6) * [ ] 딜러의 카드를 출력한다. (1장) * [ ] 참여자의 카드를 출력한다. (2장) diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java index bb884fc9bc..0728425a5d 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/CardDeck.java @@ -34,6 +34,10 @@ private static Stack generateOneCardDeck() { //TODO 인덴트 줄이기 return cards; } + public Card pop() { + return cards.pop(); + } + public int size() { return cards.size(); } diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index a295be66f7..793062d734 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -2,6 +2,7 @@ public class Dealer { + private static final int INIT_CARD_NUMBER = 2; //TODO 이름 생각해보기 private final Players players; private final CardDeck cardDeck; @@ -13,5 +14,8 @@ public Dealer(final Players players, final CardDeck cardDeck) { public void startDeal() { cardDeck.shuffle(); + for (int i = 0; i < INIT_CARD_NUMBER; i++) { + players.forEach(player -> player.add(cardDeck.pop())); + } } } diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 4ec5b880fd..59ba1d4deb 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -12,6 +12,10 @@ public Player(final String name, final Packet packet) { this.packet = packet; } + public void add(final Card card) { + packet.add(card); + } + private void validate(final String name) { validateNull(name); validateBlank(name); @@ -29,6 +33,10 @@ private void validateBlank(final String name) { } } + public int getPacketSize() { + return packet.size(); + } + @Override public boolean equals(final Object target) { if (this == target) { @@ -44,8 +52,4 @@ public boolean equals(final Object target) { public int hashCode() { return Objects.hash(name); } - - public int getPacketSize() { - return 2; - } } diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index bf84e5e28e..ad53f618ea 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Set; +import java.util.function.Consumer; public class Players { @@ -22,6 +23,10 @@ public static Players from(final List names) { return new Players(players); //TODO 이름바꾸기 } + public void forEach(Consumer action) { + names.forEach(action); + } + private static void validate(final List players) { validateSize(players); validateDuplicate(players); diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 69adb88a41..b9786f3589 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -17,12 +17,8 @@ void dealCards() { //when dealer.startDeal(); - List names = players.getNames(); //then - boolean allMatch = names.stream() - .allMatch(player -> player.getPacketSize() == 2); - - Assertions.assertThat(allMatch).isTrue(); + Assertions.assertThat(players.getNames()).allMatch(player -> player.getPacketSize() == 2); } } From 9450f3bccb39d9c9debdd7db93e22b6f2413ea60 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 5 Mar 2024 18:07:14 +0900 Subject: [PATCH 015/100] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=EA=B0=80=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=202=EC=9E=A5=EC=9D=84=20=EB=B6=84=ED=95=B4?= =?UTF-8?q?=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/domain/Dealer.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 8f12d1a0e0..efd6223972 100644 --- a/docs/README.md +++ b/docs/README.md @@ -11,7 +11,7 @@
-* [ ] 딜러가 카드 2장을 분해하다. +* [x] 딜러가 카드 2장을 분해하다. * [x] 카드는 총 6벌을 둔다. (52 * 6) * [ ] 딜러의 카드를 출력한다. (1장) * [ ] 참여자의 카드를 출력한다. (2장) diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 793062d734..d402867fac 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -3,12 +3,15 @@ public class Dealer { private static final int INIT_CARD_NUMBER = 2; //TODO 이름 생각해보기 + private final Players players; private final CardDeck cardDeck; + private final Packet packet; public Dealer(final Players players, final CardDeck cardDeck) { this.players = players; this.cardDeck = cardDeck; + this.packet = Packet.createEmptyPacket(); } public void startDeal() { @@ -16,6 +19,7 @@ public void startDeal() { for (int i = 0; i < INIT_CARD_NUMBER; i++) { players.forEach(player -> player.add(cardDeck.pop())); + packet.add(cardDeck.pop()); } } } From b533c9492c8010b3cfa4ee2257d3b9856ff29d4b Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Tue, 5 Mar 2024 19:59:03 +0900 Subject: [PATCH 016/100] =?UTF-8?q?refactor:=20=EC=8A=A4=ED=83=9D=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EB=8D=B1=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/CardDeck.java | 22 +++++++++++----------- src/main/java/domain/Dealer.java | 2 -- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java index 0728425a5d..16f520a970 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/CardDeck.java @@ -1,29 +1,33 @@ package domain; +import java.util.ArrayDeque; +import java.util.ArrayList; import java.util.Collections; -import java.util.Stack; +import java.util.Deque; +import java.util.List; public class CardDeck { private static final int DECK_SIZE = 6; - private Stack cards; + private final Deque cards; - public CardDeck(final Stack cards) { + public CardDeck(final Deque cards) { this.cards = cards; } public static CardDeck generate() { - Stack result = new Stack<>(); + final List result = new ArrayList<>(); for (int i = 0; i < DECK_SIZE; i++) { result.addAll(generateOneCardDeck()); } - return new CardDeck(result); + Collections.shuffle(result); + return new CardDeck(new ArrayDeque<>(result)); } - private static Stack generateOneCardDeck() { //TODO 인덴트 줄이기 - Stack cards = new Stack<>(); + private static List generateOneCardDeck() { //TODO 인덴트 줄이기 + final List cards = new ArrayList<>(); for (CardShape cardShape : CardShape.values()) { for (CardNumber cardNumber : CardNumber.values()) { @@ -41,8 +45,4 @@ public Card pop() { public int size() { return cards.size(); } - - public void shuffle() { - Collections.shuffle(cards); - } } diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index d402867fac..55dd547dce 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -15,8 +15,6 @@ public Dealer(final Players players, final CardDeck cardDeck) { } public void startDeal() { - cardDeck.shuffle(); - for (int i = 0; i < INIT_CARD_NUMBER; i++) { players.forEach(player -> player.add(cardDeck.pop())); packet.add(cardDeck.pop()); From b458bdfc3f1faed012807a71179c85b1a7230807 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 6 Mar 2024 14:14:44 +0900 Subject: [PATCH 017/100] =?UTF-8?q?fix:=20CardNumber=20ONE=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0,=20TEN=EC=9C=BC=EB=A1=9C=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/domain/CardNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/domain/CardNumber.java b/src/main/java/domain/CardNumber.java index b39f3bfed5..e709b5d94e 100644 --- a/src/main/java/domain/CardNumber.java +++ b/src/main/java/domain/CardNumber.java @@ -4,7 +4,6 @@ public enum CardNumber { ACE("A", 1), // TODO: 1 혹은 11 - ONE("1", 1), TWO("2", 2), THREE("3", 3), FOUR("4", 4), @@ -13,6 +12,7 @@ public enum CardNumber { SEVEN("7", 7), EIGHT("8", 8), NINE("9", 9), + TEN("10", 10), JACK("J", 10), QUEEN("Q", 10), KING("K", 10); From 63ec5fb2b72cf1bba25bfff4c5731a9c2b793c0c Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 6 Mar 2024 14:37:52 +0900 Subject: [PATCH 018/100] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=EB=82=B4?= =?UTF-8?q?=EC=9D=98=20ACE=20=EA=B0=80=20=ED=8F=AC=ED=95=A8=EB=90=9C=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=ED=95=A9=EC=9D=B4=2021=EC=9D=B4=ED=95=98?= =?UTF-8?q?=EC=9D=B4=EB=A9=B4=20=EC=97=90=EC=9D=B4=EC=8A=A4=EB=8A=94=2011?= =?UTF-8?q?=EB=A1=9C=20=EA=B3=84=EC=82=B0=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 5 +++- src/main/java/domain/Card.java | 12 ++++++-- src/main/java/domain/CardCalculator.java | 15 +++++++++- src/main/java/domain/CardNumber.java | 4 +++ src/test/java/domain/CardCalculatorTest.java | 31 ++++++++++++++++++++ 5 files changed, 63 insertions(+), 4 deletions(-) diff --git a/docs/README.md b/docs/README.md index efd6223972..5247bec9b7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,7 +25,10 @@
* [ ] 딜러의 카드의 합을 계산한다. - * [ ] 17 이상이 될때까지 카드를 받는다. +* [x] 카드 내의 ACE 가 포함된 경우 + * ACE: 11 + * 11로 했을 때 카드의 합이 21을 초과한 경우 1로 계산 +* [ ] 17 이상이 될때까지 카드를 받는다.
diff --git a/src/main/java/domain/Card.java b/src/main/java/domain/Card.java index d291b79349..78f51cb120 100644 --- a/src/main/java/domain/Card.java +++ b/src/main/java/domain/Card.java @@ -15,10 +15,18 @@ public int getCardNumber() { return cardNumber.getValue(); } + public boolean isAce() { + return cardNumber.isAce(); + } + @Override public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } final Card card = (Card) o; return cardNumber == card.cardNumber && cardShape == card.cardShape; } diff --git a/src/main/java/domain/CardCalculator.java b/src/main/java/domain/CardCalculator.java index 518140a935..33c8d9a49c 100644 --- a/src/main/java/domain/CardCalculator.java +++ b/src/main/java/domain/CardCalculator.java @@ -3,9 +3,22 @@ import java.util.List; public class CardCalculator { + + private static final int BLACK_JACK = 21; + + public int sum(final List cards) { - return cards.stream() + int total = cards.stream() .mapToInt(Card::getCardNumber) .sum(); + if (total <= BLACK_JACK && containAce(cards)) { + return total + 10; + } + return total; + } + + private boolean containAce(final List cards) { + return cards.stream() + .anyMatch(Card::isAce); } } diff --git a/src/main/java/domain/CardNumber.java b/src/main/java/domain/CardNumber.java index e709b5d94e..d1822e469b 100644 --- a/src/main/java/domain/CardNumber.java +++ b/src/main/java/domain/CardNumber.java @@ -26,6 +26,10 @@ public enum CardNumber { this.value = value; } + public boolean isAce() { + return this == ACE; + } + public int getValue() { return value; } diff --git a/src/test/java/domain/CardCalculatorTest.java b/src/test/java/domain/CardCalculatorTest.java index 18ef3b2a15..4d81833098 100644 --- a/src/test/java/domain/CardCalculatorTest.java +++ b/src/test/java/domain/CardCalculatorTest.java @@ -23,6 +23,22 @@ void sum(final List cards, final int expected) { Assertions.assertThat(result).isEqualTo(expected); } + //TODO: 디스플레이네임 수정 필요, 좀더 명확하고, 이해하기 쉽게 + // 코드 중복이 발생한다. + @DisplayName("에이스를 포함한 카드의 합이 21이하인 경우 에이스는 11로 계산한다.") + @ParameterizedTest + @MethodSource("sumAce11ParameterProvider") + void sumAce11(final List cards, final int expected) { + // given + final CardCalculator cardCalculator = new CardCalculator(); + + // when + final int result = cardCalculator.sum(cards); + + // then + Assertions.assertThat(result).isEqualTo(expected); + } + static Stream sumParameterProvider() { return Stream.of( @@ -40,4 +56,19 @@ static Stream sumParameterProvider() { ) ); } + + static Stream sumAce11ParameterProvider() { + return Stream.of( + Arguments.of( + List.of(new Card(CardNumber.ACE, CardShape.HEART), + new Card(CardNumber.EIGHT, CardShape.SPADE), + new Card(CardNumber.TWO, CardShape.CLOVER)), + 21), + Arguments.of(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), + new Card(CardNumber.TWO, CardShape.CLOVER), + new Card(CardNumber.FOUR, CardShape.CLOVER), + new Card(CardNumber.TWO, CardShape.CLOVER)), + 19) + ); + } } From 50e902d7b1d0b15bafa19474e116dd7202c0b0e8 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Wed, 6 Mar 2024 15:06:53 +0900 Subject: [PATCH 019/100] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=20=ED=95=A9?= =?UTF-8?q?=20=EA=B3=84=EC=82=B0=EC=8B=9C=20ACE=20=EA=B3=A0=EB=A0=A4?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/domain/CardCalculator.java | 24 ------ src/main/java/domain/Packet.java | 21 ++++- src/test/java/domain/CardCalculatorTest.java | 74 ----------------- src/test/java/domain/PacketTest.java | 84 +++++++++++++++++++- 5 files changed, 103 insertions(+), 102 deletions(-) delete mode 100644 src/main/java/domain/CardCalculator.java delete mode 100644 src/test/java/domain/CardCalculatorTest.java diff --git a/docs/README.md b/docs/README.md index 5247bec9b7..71943e4068 100644 --- a/docs/README.md +++ b/docs/README.md @@ -32,6 +32,6 @@
-* [ ] 모든 참여자의 카드의 합을 계산한다. +* [x] 모든 참여자의 카드의 합을 계산한다. * [ ] 최종 승패를 확인한다. * [ ] 게임 결과를 출력한다. diff --git a/src/main/java/domain/CardCalculator.java b/src/main/java/domain/CardCalculator.java deleted file mode 100644 index 33c8d9a49c..0000000000 --- a/src/main/java/domain/CardCalculator.java +++ /dev/null @@ -1,24 +0,0 @@ -package domain; - -import java.util.List; - -public class CardCalculator { - - private static final int BLACK_JACK = 21; - - - public int sum(final List cards) { - int total = cards.stream() - .mapToInt(Card::getCardNumber) - .sum(); - if (total <= BLACK_JACK && containAce(cards)) { - return total + 10; - } - return total; - } - - private boolean containAce(final List cards) { - return cards.stream() - .anyMatch(Card::isAce); - } -} diff --git a/src/main/java/domain/Packet.java b/src/main/java/domain/Packet.java index 19bfcbb649..4477c91171 100644 --- a/src/main/java/domain/Packet.java +++ b/src/main/java/domain/Packet.java @@ -5,9 +5,11 @@ public class Packet { //TODO: 더 좋은 이름이 있다면 날 설득해줘 + private static final int BLACK_JACK = 21; + private final List cards; - private Packet(final List cards) { + public Packet(final List cards) { //TODO validation this.cards = cards; } @@ -15,10 +17,27 @@ public static Packet createEmptyPacket() { return new Packet(new ArrayList<>()); } + public int sum() { + int total = cards.stream() + .mapToInt(Card::getCardNumber) + .sum(); + + if (hasAce() && total + 10 <= BLACK_JACK) { //TODO 메서드분리 + return total + 10; + } + + return total; + } + public void add(final Card card) { cards.add(card); } + private boolean hasAce() { + return cards.stream() + .anyMatch(Card::isAce); + } + public int size() { return cards.size(); } diff --git a/src/test/java/domain/CardCalculatorTest.java b/src/test/java/domain/CardCalculatorTest.java deleted file mode 100644 index 4d81833098..0000000000 --- a/src/test/java/domain/CardCalculatorTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package domain; - -import java.util.List; -import java.util.stream.Stream; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -class CardCalculatorTest { - @DisplayName("카드의 합을 구한다.") - @ParameterizedTest - @MethodSource("sumParameterProvider") - void sum(final List cards, final int expected) { - // given - final CardCalculator cardCalculator = new CardCalculator(); - - // when - final int result = cardCalculator.sum(cards); - - // then - Assertions.assertThat(result).isEqualTo(expected); - } - - //TODO: 디스플레이네임 수정 필요, 좀더 명확하고, 이해하기 쉽게 - // 코드 중복이 발생한다. - @DisplayName("에이스를 포함한 카드의 합이 21이하인 경우 에이스는 11로 계산한다.") - @ParameterizedTest - @MethodSource("sumAce11ParameterProvider") - void sumAce11(final List cards, final int expected) { - // given - final CardCalculator cardCalculator = new CardCalculator(); - - // when - final int result = cardCalculator.sum(cards); - - // then - Assertions.assertThat(result).isEqualTo(expected); - } - - - static Stream sumParameterProvider() { - return Stream.of( - Arguments.of( - List.of(new Card(CardNumber.TWO, CardShape.HEART), - new Card(CardNumber.EIGHT, CardShape.SPADE), - new Card(CardNumber.JACK, CardShape.CLOVER)), - 20 - ), - Arguments.of( - List.of(new Card(CardNumber.THREE, CardShape.DIAMOND), - new Card(CardNumber.NINE, CardShape.CLOVER), - new Card(CardNumber.NINE, CardShape.CLOVER)), - 21 - ) - ); - } - - static Stream sumAce11ParameterProvider() { - return Stream.of( - Arguments.of( - List.of(new Card(CardNumber.ACE, CardShape.HEART), - new Card(CardNumber.EIGHT, CardShape.SPADE), - new Card(CardNumber.TWO, CardShape.CLOVER)), - 21), - Arguments.of(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), - new Card(CardNumber.TWO, CardShape.CLOVER), - new Card(CardNumber.FOUR, CardShape.CLOVER), - new Card(CardNumber.TWO, CardShape.CLOVER)), - 19) - ); - } -} diff --git a/src/test/java/domain/PacketTest.java b/src/test/java/domain/PacketTest.java index 845b3ebbdf..22aa99132d 100644 --- a/src/test/java/domain/PacketTest.java +++ b/src/test/java/domain/PacketTest.java @@ -3,15 +3,19 @@ import org.assertj.core.api.Assertions; 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.ArrayList; +import java.util.List; +import java.util.stream.Stream; class PacketTest { @Test @DisplayName("카드를 가지고 있는 객체를 생성한다.") void createPacket() { - Assertions.assertThatCode(() -> Packet.createEmptyPacket()) + Assertions.assertThatCode(Packet::createEmptyPacket) .doesNotThrowAnyException(); } @@ -27,4 +31,80 @@ void addCard() { //then Assertions.assertThat(packet.size()).isEqualTo(1); } + + @DisplayName("카드의 합을 구한다.") + @ParameterizedTest + @MethodSource("sumParameterProvider") + void sum(final Packet packet, final int expected) { + // given & when + final int result = packet.sum(); + + // then + Assertions.assertThat(result).isEqualTo(expected); + } + + //TODO: 디스플레이네임 수정 필요, 좀더 명확하고, 이해하기 쉽게 + // 코드 중복이 발생한다. + @DisplayName("에이스를 11로 계산한다.") + @ParameterizedTest + @MethodSource("sumAce11ParameterProvider") + void sumAce11(final Packet packet, final int expected) { + // given & when + final int result = packet.sum(); + + // then + Assertions.assertThat(result).isEqualTo(expected); + } + + @DisplayName("에이스를 1로 계산한다.") + @ParameterizedTest + @MethodSource("sumAce1ParameterProvider") + void sumAce1(final Packet packet, final int expected) { + // given & when + final int result = packet.sum(); + + // then + Assertions.assertThat(result).isEqualTo(expected); + } + + static Stream sumParameterProvider() { + return Stream.of( + Arguments.of(new Packet(List.of(new Card(CardNumber.TWO, CardShape.HEART), + new Card(CardNumber.EIGHT, CardShape.SPADE), + new Card(CardNumber.JACK, CardShape.CLOVER))), + 20), + Arguments.of(new Packet(List.of(new Card(CardNumber.THREE, CardShape.DIAMOND), + new Card(CardNumber.NINE, CardShape.CLOVER), + new Card(CardNumber.NINE, CardShape.CLOVER))), + 21) + ); + } + + static Stream sumAce11ParameterProvider() { + return Stream.of( + Arguments.of(new Packet(List.of(new Card(CardNumber.ACE, CardShape.HEART), + new Card(CardNumber.EIGHT, CardShape.SPADE), + new Card(CardNumber.TWO, CardShape.CLOVER))), + 21), + Arguments.of(new Packet(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), + new Card(CardNumber.TWO, CardShape.CLOVER), + new Card(CardNumber.FOUR, CardShape.CLOVER), + new Card(CardNumber.TWO, CardShape.CLOVER))), + 19) + ); + } + + static Stream sumAce1ParameterProvider() { + return Stream.of( + Arguments.of(new Packet(List.of(new Card(CardNumber.ACE, CardShape.HEART), + new Card(CardNumber.NINE, CardShape.SPADE), + new Card(CardNumber.NINE, CardShape.CLOVER))), + 19), + Arguments.of(new Packet(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), + new Card(CardNumber.EIGHT, CardShape.CLOVER), + new Card(CardNumber.FIVE, CardShape.CLOVER), + new Card(CardNumber.SIX, CardShape.CLOVER))), + 20) + ); + } } From 52dcb843eec25345d8e5b3532bae12c07ec63f18 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 6 Mar 2024 15:11:12 +0900 Subject: [PATCH 020/100] =?UTF-8?q?refactor:=20Packet=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=9D=B4=EB=A6=84=EC=9D=84=20Hands=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Dealer.java | 6 ++-- .../java/domain/{Packet.java => Hands.java} | 8 ++--- src/main/java/domain/Player.java | 10 +++--- src/main/java/domain/Players.java | 2 +- .../{PacketTest.java => HandsTest.java} | 34 +++++++++---------- src/test/java/domain/PlayerTest.java | 4 +-- 6 files changed, 32 insertions(+), 32 deletions(-) rename src/main/java/domain/{Packet.java => Hands.java} (75%) rename src/test/java/domain/{PacketTest.java => HandsTest.java} (72%) diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 55dd547dce..3ab24c303d 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -6,18 +6,18 @@ public class Dealer { private final Players players; private final CardDeck cardDeck; - private final Packet packet; + private final Hands hands; public Dealer(final Players players, final CardDeck cardDeck) { this.players = players; this.cardDeck = cardDeck; - this.packet = Packet.createEmptyPacket(); + this.hands = Hands.createEmptyPacket(); } public void startDeal() { for (int i = 0; i < INIT_CARD_NUMBER; i++) { players.forEach(player -> player.add(cardDeck.pop())); - packet.add(cardDeck.pop()); + hands.add(cardDeck.pop()); } } } diff --git a/src/main/java/domain/Packet.java b/src/main/java/domain/Hands.java similarity index 75% rename from src/main/java/domain/Packet.java rename to src/main/java/domain/Hands.java index 4477c91171..3c0428d473 100644 --- a/src/main/java/domain/Packet.java +++ b/src/main/java/domain/Hands.java @@ -3,18 +3,18 @@ import java.util.ArrayList; import java.util.List; -public class Packet { //TODO: 더 좋은 이름이 있다면 날 설득해줘 +public class Hands { //TODO: 더 좋은 이름이 있다면 날 설득해줘 private static final int BLACK_JACK = 21; private final List cards; - public Packet(final List cards) { //TODO validation + public Hands(final List cards) { //TODO validation this.cards = cards; } - public static Packet createEmptyPacket() { - return new Packet(new ArrayList<>()); + public static Hands createEmptyPacket() { + return new Hands(new ArrayList<>()); } public int sum() { diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 59ba1d4deb..5ed2406521 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -4,16 +4,16 @@ public class Player { private final String name; - private final Packet packet; + private final Hands hands; - public Player(final String name, final Packet packet) { + public Player(final String name, final Hands hands) { validate(name); this.name = name; - this.packet = packet; + this.hands = hands; } public void add(final Card card) { - packet.add(card); + hands.add(card); } private void validate(final String name) { @@ -34,7 +34,7 @@ private void validateBlank(final String name) { } public int getPacketSize() { - return packet.size(); + return hands.size(); } @Override diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index ad53f618ea..042d9e2778 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -17,7 +17,7 @@ private Players(final List names) { public static Players from(final List names) { List players = names.stream() - .map(name -> new Player(name, Packet.createEmptyPacket())) + .map(name -> new Player(name, Hands.createEmptyPacket())) .toList(); validate(players); return new Players(players); //TODO 이름바꾸기 diff --git a/src/test/java/domain/PacketTest.java b/src/test/java/domain/HandsTest.java similarity index 72% rename from src/test/java/domain/PacketTest.java rename to src/test/java/domain/HandsTest.java index 22aa99132d..7fa88cad08 100644 --- a/src/test/java/domain/PacketTest.java +++ b/src/test/java/domain/HandsTest.java @@ -10,12 +10,12 @@ import java.util.List; import java.util.stream.Stream; -class PacketTest { +class HandsTest { @Test @DisplayName("카드를 가지고 있는 객체를 생성한다.") void createPacket() { - Assertions.assertThatCode(Packet::createEmptyPacket) + Assertions.assertThatCode(Hands::createEmptyPacket) .doesNotThrowAnyException(); } @@ -23,21 +23,21 @@ void createPacket() { @DisplayName("카드를 추가한다.") void addCard() { //given - final Packet packet = Packet.createEmptyPacket(); + final Hands hands = Hands.createEmptyPacket(); //when - packet.add(new Card(CardNumber.EIGHT, CardShape.CLOVER)); + hands.add(new Card(CardNumber.EIGHT, CardShape.CLOVER)); //then - Assertions.assertThat(packet.size()).isEqualTo(1); + Assertions.assertThat(hands.size()).isEqualTo(1); } @DisplayName("카드의 합을 구한다.") @ParameterizedTest @MethodSource("sumParameterProvider") - void sum(final Packet packet, final int expected) { + void sum(final Hands hands, final int expected) { // given & when - final int result = packet.sum(); + final int result = hands.sum(); // then Assertions.assertThat(result).isEqualTo(expected); @@ -48,9 +48,9 @@ void sum(final Packet packet, final int expected) { @DisplayName("에이스를 11로 계산한다.") @ParameterizedTest @MethodSource("sumAce11ParameterProvider") - void sumAce11(final Packet packet, final int expected) { + void sumAce11(final Hands hands, final int expected) { // given & when - final int result = packet.sum(); + final int result = hands.sum(); // then Assertions.assertThat(result).isEqualTo(expected); @@ -59,9 +59,9 @@ void sumAce11(final Packet packet, final int expected) { @DisplayName("에이스를 1로 계산한다.") @ParameterizedTest @MethodSource("sumAce1ParameterProvider") - void sumAce1(final Packet packet, final int expected) { + void sumAce1(final Hands hands, final int expected) { // given & when - final int result = packet.sum(); + final int result = hands.sum(); // then Assertions.assertThat(result).isEqualTo(expected); @@ -69,11 +69,11 @@ void sumAce1(final Packet packet, final int expected) { static Stream sumParameterProvider() { return Stream.of( - Arguments.of(new Packet(List.of(new Card(CardNumber.TWO, CardShape.HEART), + Arguments.of(new Hands(List.of(new Card(CardNumber.TWO, CardShape.HEART), new Card(CardNumber.EIGHT, CardShape.SPADE), new Card(CardNumber.JACK, CardShape.CLOVER))), 20), - Arguments.of(new Packet(List.of(new Card(CardNumber.THREE, CardShape.DIAMOND), + Arguments.of(new Hands(List.of(new Card(CardNumber.THREE, CardShape.DIAMOND), new Card(CardNumber.NINE, CardShape.CLOVER), new Card(CardNumber.NINE, CardShape.CLOVER))), 21) @@ -82,11 +82,11 @@ static Stream sumParameterProvider() { static Stream sumAce11ParameterProvider() { return Stream.of( - Arguments.of(new Packet(List.of(new Card(CardNumber.ACE, CardShape.HEART), + Arguments.of(new Hands(List.of(new Card(CardNumber.ACE, CardShape.HEART), new Card(CardNumber.EIGHT, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.CLOVER))), 21), - Arguments.of(new Packet(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), + Arguments.of(new Hands(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), new Card(CardNumber.TWO, CardShape.CLOVER), new Card(CardNumber.FOUR, CardShape.CLOVER), new Card(CardNumber.TWO, CardShape.CLOVER))), @@ -96,11 +96,11 @@ static Stream sumAce11ParameterProvider() { static Stream sumAce1ParameterProvider() { return Stream.of( - Arguments.of(new Packet(List.of(new Card(CardNumber.ACE, CardShape.HEART), + Arguments.of(new Hands(List.of(new Card(CardNumber.ACE, CardShape.HEART), new Card(CardNumber.NINE, CardShape.SPADE), new Card(CardNumber.NINE, CardShape.CLOVER))), 19), - Arguments.of(new Packet(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), + Arguments.of(new Hands(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), new Card(CardNumber.EIGHT, CardShape.CLOVER), new Card(CardNumber.FIVE, CardShape.CLOVER), new Card(CardNumber.SIX, CardShape.CLOVER))), diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index 59263dc00a..d8ae5066f2 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -11,7 +11,7 @@ public class PlayerTest { @DisplayName("이름으로 참여자를 생성한다.") @Test void createPlayerWithName() { - Assertions.assertThatCode(() -> new Player("pobi", Packet.createEmptyPacket())) + Assertions.assertThatCode(() -> new Player("pobi", Hands.createEmptyPacket())) .doesNotThrowAnyException(); } @@ -20,7 +20,7 @@ void createPlayerWithName() { @ValueSource(strings = {"", " ", " "}) @NullSource void nullOrBlankInputThrowException(String name) { - Assertions.assertThatThrownBy(() -> new Player(name, Packet.createEmptyPacket())) + Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyPacket())) .isInstanceOf(IllegalArgumentException.class); } } From ba0dd837f1b71418ac72446a1c63f5f8556b3938 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 6 Mar 2024 15:21:43 +0900 Subject: [PATCH 021/100] =?UTF-8?q?feat:=20=EC=B0=B8=EC=97=AC=EC=9E=90?= =?UTF-8?q?=EC=9D=98=20=EB=8C=80=EB=8B=B5=EC=9D=B4=20y,=20n=20=EA=B0=80=20?= =?UTF-8?q?=EC=95=84=EB=8B=90=EC=8B=9C=20=EC=98=88=EC=99=B8=EA=B0=80=20?= =?UTF-8?q?=EB=B0=9C=EC=83=9D=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/domain/Answer.java | 22 ++++++++++++++++++++++ src/test/java/domain/AnswerTest.java | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/main/java/domain/Answer.java create mode 100644 src/test/java/domain/AnswerTest.java diff --git a/docs/README.md b/docs/README.md index 71943e4068..f41a0e264a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -19,7 +19,7 @@
* [ ] 참여자는 hit(y) / stay(n)를 선택한다. - * [ ] y, n 가 아닐시 예외가 발생한다. + * [x] y, n 가 아닐시 예외가 발생한다. * [ ] 현재 가지고 있는 카드를 출력한다.
diff --git a/src/main/java/domain/Answer.java b/src/main/java/domain/Answer.java new file mode 100644 index 0000000000..d8c855b9bb --- /dev/null +++ b/src/main/java/domain/Answer.java @@ -0,0 +1,22 @@ +package domain; + +import java.util.Arrays; + +public enum Answer { + HIT("y"), + STAY("n"); + + private final String value; + + Answer(final String value) { + this.value = value; + } + + + public static Answer from(final String value) { + return Arrays.stream(Answer.values()) + .filter(answer -> answer.value.equals(value)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("[ERROR] 유효하지 않은 대답입니다.")); + } +} diff --git a/src/test/java/domain/AnswerTest.java b/src/test/java/domain/AnswerTest.java new file mode 100644 index 0000000000..4e57d3fd75 --- /dev/null +++ b/src/test/java/domain/AnswerTest.java @@ -0,0 +1,19 @@ +package domain; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class AnswerTest { + + @ParameterizedTest + @DisplayName("y혹은 n이 아닐시 예외가 발생한다.") + @ValueSource(strings = {"Y", "nn", "aa"}) + void invalidAnswer(String value) { + Assertions.assertThatThrownBy(() -> Answer.from(value)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 유효하지 않은 대답입니다."); + } + +} From 56eaa06be33bd320d058c66c6acda0389531621e Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Wed, 6 Mar 2024 15:50:20 +0900 Subject: [PATCH 022/100] =?UTF-8?q?feat:=20=EC=B0=B8=EC=97=AC=EC=9E=90=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=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 --- docs/README.md | 11 +++-- src/main/java/application/Application.java | 16 +++++++ .../java/controller/BlackJackController.java | 16 +++++++ src/main/java/domain/Players.java | 1 + src/main/java/view/InputView.java | 47 +++++++++++++++++++ 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 src/main/java/application/Application.java create mode 100644 src/main/java/controller/BlackJackController.java create mode 100644 src/main/java/view/InputView.java diff --git a/docs/README.md b/docs/README.md index f41a0e264a..38a9221907 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,11 +1,12 @@ # 기능 요구 사항 -* [ ] 참여자 이름 입력 받는다. - * [ ] 참여자는 쉼표(,)로 구분한다. +* [x] 참여자 이름 입력 받는다. + * [x] 양 끝 공백을 제거한다. + * [x] 참여자는 쉼표(,)로 구분한다. * [x] null 이나 공백인 경우 예외가 발생한다. - * [ ] 쉼표로 시작시 예외가 발생한다. ex) ,pobi,jason - * [ ] 쉼표로 끝날시 예외가 발생한다. ex) pobi,jason, - * [ ] 쉼표가 연속으로 올시 예외가 발생한다. ex) pobi,,jason + * [x] 쉼표로 시작시 예외가 발생한다. ex) ,pobi,jason + * [x] 쉼표로 끝날시 예외가 발생한다. ex) pobi,jason, + * [x] 쉼표가 연속으로 올시 예외가 발생한다. ex) pobi,,jason * [x] 참여자 이름이 중복시 예외가 발생한다. * [x] 총 참여자의 수는 2이상 8이하여야 한다. diff --git a/src/main/java/application/Application.java b/src/main/java/application/Application.java new file mode 100644 index 0000000000..71f1892132 --- /dev/null +++ b/src/main/java/application/Application.java @@ -0,0 +1,16 @@ +package application; + +import controller.BlackJackController; +import view.InputView; + +import java.util.Scanner; + +public class Application { + public static void main(String[] args) { + final Scanner scanner = new Scanner(System.in); + final InputView inputView = new InputView(scanner); + + final BlackJackController blackJackController = new BlackJackController(inputView); + blackJackController.run(); + } +} diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java new file mode 100644 index 0000000000..0686755a0d --- /dev/null +++ b/src/main/java/controller/BlackJackController.java @@ -0,0 +1,16 @@ +package controller; + +import domain.Players; +import view.InputView; + +public class BlackJackController { + private final InputView inputView; + + public BlackJackController(final InputView inputView) { + this.inputView = inputView; + } + + public void run() { + Players players = Players.from(inputView.readPlayerNames()); + } +} diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index 042d9e2778..e5c4f7190b 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -17,6 +17,7 @@ private Players(final List names) { public static Players from(final List names) { List players = names.stream() + .map(String::trim) .map(name -> new Player(name, Hands.createEmptyPacket())) .toList(); validate(players); diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 0000000000..a9198ed92e --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,47 @@ +package view; + +import java.util.List; +import java.util.Scanner; + +public class InputView { + + private static final String NAME_SEPARATOR = ","; + + private final Scanner scanner; + + public InputView(final Scanner scanner) { + this.scanner = scanner; + } + + public List readPlayerNames() { + System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); + String rawNames = scanner.nextLine().trim(); + validateBlank(rawNames); + validateSeparators(rawNames); + List players = List.of(rawNames.split(NAME_SEPARATOR)); + System.out.println(); + return players; + } + + private void validateBlank(final String rawNames) { + if (rawNames == null || rawNames.isBlank()) { + throw new IllegalArgumentException("[ERROR] 유효하지 않은 입력입니다."); + } + } + + private void validateSeparators(final String rawNames) { + if (isInvalidSeparator(rawNames)) { + throw new IllegalArgumentException("[ERROR] 유효하지 않은 구분자입니다."); + } + } + + private boolean isInvalidSeparator(final String rawNames) { + if (rawNames.startsWith(NAME_SEPARATOR)) { + return true; + } + if (rawNames.endsWith(NAME_SEPARATOR)) { + return true; + } + return rawNames.contains(NAME_SEPARATOR.repeat(2)); + } +} From ee7777745daed8706ad8d5bad3782f8da55013ea Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 6 Mar 2024 16:44:32 +0900 Subject: [PATCH 023/100] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EC=9D=98=20=EC=8B=9C=EC=9E=91=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=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 --- src/main/java/application/Application.java | 8 ++--- .../java/controller/BlackJackController.java | 14 +++++++-- src/main/java/domain/Card.java | 5 +++ src/main/java/domain/CardNumber.java | 4 +++ src/main/java/domain/CardShape.java | 4 +++ src/main/java/domain/Hands.java | 4 +++ src/main/java/domain/Player.java | 13 ++++++++ src/main/java/domain/PlayerHandsDto.java | 27 ++++++++++++++++ src/main/java/view/OutputView.java | 31 +++++++++++++++++++ 9 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 src/main/java/domain/PlayerHandsDto.java create mode 100644 src/main/java/view/OutputView.java diff --git a/src/main/java/application/Application.java b/src/main/java/application/Application.java index 71f1892132..5c5998ec53 100644 --- a/src/main/java/application/Application.java +++ b/src/main/java/application/Application.java @@ -1,16 +1,16 @@ package application; import controller.BlackJackController; -import view.InputView; - import java.util.Scanner; +import view.InputView; +import view.OutputView; public class Application { public static void main(String[] args) { final Scanner scanner = new Scanner(System.in); final InputView inputView = new InputView(scanner); - - final BlackJackController blackJackController = new BlackJackController(inputView); + final OutputView outputView = new OutputView(); + final BlackJackController blackJackController = new BlackJackController(inputView, outputView); blackJackController.run(); } } diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 0686755a0d..7bbbb2ad6d 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -1,16 +1,26 @@ package controller; +import domain.CardDeck; +import domain.Dealer; +import domain.PlayerHandsDto; import domain.Players; import view.InputView; +import view.OutputView; public class BlackJackController { private final InputView inputView; + private final OutputView outputView; - public BlackJackController(final InputView inputView) { + public BlackJackController(final InputView inputView, final OutputView outputView) { this.inputView = inputView; + this.outputView = outputView; } public void run() { - Players players = Players.from(inputView.readPlayerNames()); + final Players players = Players.from(inputView.readPlayerNames()); + final CardDeck cardDeck = CardDeck.generate(); + final Dealer dealer = new Dealer(players, cardDeck); + dealer.startDeal(); + outputView.printStartDeal(PlayerHandsDto.from(players)); } } diff --git a/src/main/java/domain/Card.java b/src/main/java/domain/Card.java index 78f51cb120..4b9af07027 100644 --- a/src/main/java/domain/Card.java +++ b/src/main/java/domain/Card.java @@ -35,4 +35,9 @@ public boolean equals(final Object o) { public int hashCode() { return Objects.hash(cardNumber, cardShape); } + + @Override + public String toString() { + return cardNumber.getName() + cardShape.getName(); + } } diff --git a/src/main/java/domain/CardNumber.java b/src/main/java/domain/CardNumber.java index d1822e469b..e02011686e 100644 --- a/src/main/java/domain/CardNumber.java +++ b/src/main/java/domain/CardNumber.java @@ -33,4 +33,8 @@ public boolean isAce() { public int getValue() { return value; } + + public String getName() { + return name; + } } diff --git a/src/main/java/domain/CardShape.java b/src/main/java/domain/CardShape.java index 63ab7b7240..8c1dd4e41e 100644 --- a/src/main/java/domain/CardShape.java +++ b/src/main/java/domain/CardShape.java @@ -11,4 +11,8 @@ public enum CardShape { CardShape(final String name) { this.name = name; } + + public String getName() { + return name; + } } diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 3c0428d473..ff848510fc 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -41,4 +41,8 @@ private boolean hasAce() { public int size() { return cards.size(); } + + public List getCards() { + return cards; + } } diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 5ed2406521..646ed8281c 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -1,5 +1,6 @@ package domain; +import java.util.List; import java.util.Objects; public class Player { @@ -33,10 +34,22 @@ private void validateBlank(final String name) { } } + //TODO: 메서드 handsSize 수정 public int getPacketSize() { return hands.size(); } + public String getName() { + return name; + } + + public List getCards() { + return hands.getCards() + .stream() + .map(Card::toString) + .toList(); + } + @Override public boolean equals(final Object target) { if (this == target) { diff --git a/src/main/java/domain/PlayerHandsDto.java b/src/main/java/domain/PlayerHandsDto.java new file mode 100644 index 0000000000..9ee0806f5c --- /dev/null +++ b/src/main/java/domain/PlayerHandsDto.java @@ -0,0 +1,27 @@ +package domain; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class PlayerHandsDto { + + private final Map> playerHands; + + public PlayerHandsDto(final Map> playerHands) { + this.playerHands = playerHands; + } + + public static PlayerHandsDto from(final Players players) { + Map> result = new LinkedHashMap<>(); + for (Player player : players.getNames()) { + result.put(player.getName(), player.getCards()); + } + return new PlayerHandsDto(result); + } + + public Map> getPlayerHands() { + return playerHands; + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 0000000000..4b35e7698d --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,31 @@ +package view; + +import domain.PlayerHandsDto; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +public class OutputView { + + private final String FORM = "%s카드: %s"; + + public void printStartDeal(final PlayerHandsDto playerHandsDto) { + final Set playerNames = playerHandsDto.getPlayerHands().keySet(); + System.out.println("딜러와 " + format(playerNames) + " 에게 2장을 나누었습니다."); + System.out.println("딜러: "); // TODO: 딜러 카드 + Map> playerHands = playerHandsDto.getPlayerHands(); + for (Entry> stringListEntry : playerHands.entrySet()) { + System.out.printf(FORM, stringListEntry.getKey(), format(stringListEntry.getValue())); + System.out.println(); + } + } + + private String format(final Set playerNames) { + return String.join(", ", playerNames); + } + + private String format(final List playerNames) { + return String.join(", ", playerNames); + } +} From a5255e3e30fee3845157d2e89e3f24b3246933e3 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Wed, 6 Mar 2024 17:02:55 +0900 Subject: [PATCH 024/100] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=20=EC=B6=9C=EB=A0=A5=ED=95=98=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 --- .../java/controller/BlackJackController.java | 5 +++-- src/main/java/domain/Dealer.java | 9 ++++++++ src/main/java/dto/DealerHandsDto.java | 22 +++++++++++++++++++ .../java/{domain => dto}/PlayerHandsDto.java | 6 +++-- src/main/java/view/OutputView.java | 8 ++++--- 5 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 src/main/java/dto/DealerHandsDto.java rename src/main/java/{domain => dto}/PlayerHandsDto.java (92%) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 7bbbb2ad6d..430f7fa6de 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -2,7 +2,8 @@ import domain.CardDeck; import domain.Dealer; -import domain.PlayerHandsDto; +import dto.DealerHandsDto; +import dto.PlayerHandsDto; import domain.Players; import view.InputView; import view.OutputView; @@ -21,6 +22,6 @@ public void run() { final CardDeck cardDeck = CardDeck.generate(); final Dealer dealer = new Dealer(players, cardDeck); dealer.startDeal(); - outputView.printStartDeal(PlayerHandsDto.from(players)); + outputView.printStartDeal(DealerHandsDto.from(dealer), PlayerHandsDto.from(players)); } } diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 3ab24c303d..865229489d 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -1,5 +1,7 @@ package domain; +import java.util.List; + public class Dealer { private static final int INIT_CARD_NUMBER = 2; //TODO 이름 생각해보기 @@ -20,4 +22,11 @@ public void startDeal() { hands.add(cardDeck.pop()); } } + + public List getCards() { + return hands.getCards() + .stream() + .map(Card::toString) + .toList(); + } } diff --git a/src/main/java/dto/DealerHandsDto.java b/src/main/java/dto/DealerHandsDto.java new file mode 100644 index 0000000000..04fc88fce7 --- /dev/null +++ b/src/main/java/dto/DealerHandsDto.java @@ -0,0 +1,22 @@ +package dto; + +import domain.Dealer; + +import java.util.List; + +public class DealerHandsDto { + private final String displayedCard; + + private DealerHandsDto(final String displayedCard) { + this.displayedCard = displayedCard; + } + + public static DealerHandsDto from(final Dealer dealer) { + List cards = dealer.getCards(); + return new DealerHandsDto(cards.get(0)); + } + + public String getDisplayedCard() { + return displayedCard; + } +} diff --git a/src/main/java/domain/PlayerHandsDto.java b/src/main/java/dto/PlayerHandsDto.java similarity index 92% rename from src/main/java/domain/PlayerHandsDto.java rename to src/main/java/dto/PlayerHandsDto.java index 9ee0806f5c..7d82527163 100644 --- a/src/main/java/domain/PlayerHandsDto.java +++ b/src/main/java/dto/PlayerHandsDto.java @@ -1,6 +1,8 @@ -package domain; +package dto; + +import domain.Player; +import domain.Players; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 4b35e7698d..b13ea448b7 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,6 +1,7 @@ package view; -import domain.PlayerHandsDto; +import dto.DealerHandsDto; +import dto.PlayerHandsDto; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -10,10 +11,11 @@ public class OutputView { private final String FORM = "%s카드: %s"; - public void printStartDeal(final PlayerHandsDto playerHandsDto) { + public void printStartDeal(final DealerHandsDto dealerHandsDto, final PlayerHandsDto playerHandsDto) { + final String dealerCard = dealerHandsDto.getDisplayedCard(); final Set playerNames = playerHandsDto.getPlayerHands().keySet(); System.out.println("딜러와 " + format(playerNames) + " 에게 2장을 나누었습니다."); - System.out.println("딜러: "); // TODO: 딜러 카드 + System.out.println("딜러: " + dealerCard); Map> playerHands = playerHandsDto.getPlayerHands(); for (Entry> stringListEntry : playerHands.entrySet()) { System.out.printf(FORM, stringListEntry.getKey(), format(stringListEntry.getValue())); From 9575be9b5509c09f53a2fd2b3130a49c3bca5afe Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Wed, 6 Mar 2024 17:04:30 +0900 Subject: [PATCH 025/100] =?UTF-8?q?docs:=20=EC=99=84=EB=A3=8C=ED=95=9C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B2=B4=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 38a9221907..1afadf5a69 100644 --- a/docs/README.md +++ b/docs/README.md @@ -14,8 +14,8 @@ * [x] 딜러가 카드 2장을 분해하다. * [x] 카드는 총 6벌을 둔다. (52 * 6) -* [ ] 딜러의 카드를 출력한다. (1장) -* [ ] 참여자의 카드를 출력한다. (2장) +* [x] 딜러의 카드를 출력한다. (1장) +* [x] 참여자의 카드를 출력한다. (2장)
From e4d43694e7699137ad0ab21f1021c58b120a9d67 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 6 Mar 2024 18:07:36 +0900 Subject: [PATCH 026/100] =?UTF-8?q?feat:=20answer=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=9D=BC=20=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EA=B8=B0=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 --- .../java/controller/BlackJackController.java | 1 + src/main/java/domain/Dealer.java | 15 ++++++++++++ src/main/java/domain/Player.java | 2 ++ src/main/java/domain/Players.java | 2 +- src/main/java/view/InputView.java | 8 +++++++ src/test/java/domain/DealerTest.java | 24 +++++++++++++++++++ src/test/java/domain/PlayerTest.java | 5 +++- 7 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 430f7fa6de..68f7f310eb 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -23,5 +23,6 @@ public void run() { final Dealer dealer = new Dealer(players, cardDeck); dealer.startDeal(); outputView.printStartDeal(DealerHandsDto.from(dealer), PlayerHandsDto.from(players)); + dealer.play(inputView); } } diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 865229489d..ae7e269c83 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -1,6 +1,7 @@ package domain; import java.util.List; +import view.InputView; public class Dealer { @@ -29,4 +30,18 @@ public List getCards() { .map(Card::toString) .toList(); } + + public void play(final InputView inputView) { + List names = players.getNames(); + for (Player player : names) { + hitOrStay(inputView.readAnswer(player.getName()), player); + } + } + + public void hitOrStay(final String value, final Player player) { + Answer answer = Answer.from(value); + if (Answer.HIT == answer) { + player.add(cardDeck.pop()); + } + } } diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 646ed8281c..7523764ef1 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -17,6 +17,8 @@ public void add(final Card card) { hands.add(card); } + //TODO 메서드 명 변경 + private void validate(final String name) { validateNull(name); validateBlank(name); diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index e5c4f7190b..67ff2d50d4 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -11,7 +11,7 @@ public class Players { private final List names; - private Players(final List names) { + public Players(final List names) { this.names = names; } diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index a9198ed92e..8c9d34360e 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -23,6 +23,14 @@ public List readPlayerNames() { return players; } + public String readAnswer(String name) { + System.out.printf("%s는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)", name); + System.out.println(); + String rawAnswer = scanner.nextLine().trim(); + validateBlank(rawAnswer); + return rawAnswer; + } + private void validateBlank(final String rawNames) { if (rawNames == null || rawNames.isBlank()) { throw new IllegalArgumentException("[ERROR] 유효하지 않은 입력입니다."); diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index b9786f3589..53362807d3 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -1,9 +1,11 @@ package domain; import java.util.List; +import java.util.Scanner; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import view.InputView; class DealerTest { @@ -21,4 +23,26 @@ void dealCards() { //then Assertions.assertThat(players.getNames()).allMatch(player -> player.getPacketSize() == 2); } + + @Test + @DisplayName("플레이어의 답변이 y라면 카드를 한장 추가한다.") + void addOneCard() { + //given + final Player redddy = new Player("레디", Hands.createEmptyPacket()); + final Player zeze = new Player("제제", Hands.createEmptyPacket()); + + final Players players = new Players(List.of(redddy, zeze)); + + final CardDeck cardDeck = CardDeck.generate(); + final Dealer dealer = new Dealer(players, cardDeck); + dealer.startDeal(); + + //when + dealer.hitOrStay("y", redddy); + dealer.hitOrStay("n", zeze); + + //then + Assertions.assertThat(redddy.getPacketSize()).isEqualTo(3); + Assertions.assertThat(zeze.getPacketSize()).isEqualTo(2); + } } diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index d8ae5066f2..e2f48da2a4 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -1,13 +1,16 @@ package domain; +import java.util.List; +import java.util.Scanner; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; +import view.InputView; -public class PlayerTest { +class PlayerTest { @DisplayName("이름으로 참여자를 생성한다.") @Test void createPlayerWithName() { From 51f5aeaf2cbf98619f804320e1f832974db9b7bf Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Wed, 6 Mar 2024 19:26:08 +0900 Subject: [PATCH 027/100] =?UTF-8?q?refactor:=20Dealer=20=ED=95=84=EB=93=9C?= =?UTF-8?q?=EC=97=90=EC=84=9C=20Players=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/BlackJackController.java | 12 ++++++--- src/main/java/domain/Dealer.java | 27 ++++++------------- src/test/java/domain/DealerTest.java | 15 +++++------ 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 68f7f310eb..2f9ea149ad 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -1,7 +1,9 @@ package controller; +import domain.Answer; import domain.CardDeck; import domain.Dealer; +import domain.Player; import dto.DealerHandsDto; import dto.PlayerHandsDto; import domain.Players; @@ -20,9 +22,13 @@ public BlackJackController(final InputView inputView, final OutputView outputVie public void run() { final Players players = Players.from(inputView.readPlayerNames()); final CardDeck cardDeck = CardDeck.generate(); - final Dealer dealer = new Dealer(players, cardDeck); - dealer.startDeal(); + final Dealer dealer = new Dealer(cardDeck); + + dealer.startDeal(players); outputView.printStartDeal(DealerHandsDto.from(dealer), PlayerHandsDto.from(players)); - dealer.play(inputView); + + for (Player player : players.getNames()) { + dealer.deal(player, Answer.from(inputView.readAnswer(player.getName()))); + } } } diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index ae7e269c83..25397446a0 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -1,47 +1,36 @@ package domain; import java.util.List; -import view.InputView; public class Dealer { private static final int INIT_CARD_NUMBER = 2; //TODO 이름 생각해보기 - private final Players players; private final CardDeck cardDeck; private final Hands hands; - public Dealer(final Players players, final CardDeck cardDeck) { - this.players = players; + public Dealer(final CardDeck cardDeck) { this.cardDeck = cardDeck; this.hands = Hands.createEmptyPacket(); } - public void startDeal() { + public void startDeal(final Players players) { for (int i = 0; i < INIT_CARD_NUMBER; i++) { players.forEach(player -> player.add(cardDeck.pop())); hands.add(cardDeck.pop()); } } + public void deal(final Player player, final Answer answer) { + if (Answer.HIT == answer) { + player.add(cardDeck.pop()); + } + } + public List getCards() { return hands.getCards() .stream() .map(Card::toString) .toList(); } - - public void play(final InputView inputView) { - List names = players.getNames(); - for (Player player : names) { - hitOrStay(inputView.readAnswer(player.getName()), player); - } - } - - public void hitOrStay(final String value, final Player player) { - Answer answer = Answer.from(value); - if (Answer.HIT == answer) { - player.add(cardDeck.pop()); - } - } } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 53362807d3..60b00176ef 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -1,11 +1,10 @@ package domain; import java.util.List; -import java.util.Scanner; + import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import view.InputView; class DealerTest { @@ -15,10 +14,10 @@ void dealCards() { //given final Players players = Players.from(List.of("레디", "제제")); final CardDeck cardDeck = CardDeck.generate(); - final Dealer dealer = new Dealer(players, cardDeck); + final Dealer dealer = new Dealer(cardDeck); //when - dealer.startDeal(); + dealer.startDeal(players); //then Assertions.assertThat(players.getNames()).allMatch(player -> player.getPacketSize() == 2); @@ -34,12 +33,12 @@ void addOneCard() { final Players players = new Players(List.of(redddy, zeze)); final CardDeck cardDeck = CardDeck.generate(); - final Dealer dealer = new Dealer(players, cardDeck); - dealer.startDeal(); + final Dealer dealer = new Dealer(cardDeck); + dealer.startDeal(players); //when - dealer.hitOrStay("y", redddy); - dealer.hitOrStay("n", zeze); + dealer.deal(redddy, Answer.HIT); + dealer.deal(zeze, Answer.STAY); //then Assertions.assertThat(redddy.getPacketSize()).isEqualTo(3); From 3a0ddb4500faffde3e97fd1eed68492edbc10c36 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Wed, 6 Mar 2024 19:45:28 +0900 Subject: [PATCH 028/100] =?UTF-8?q?feat:=20=EC=B6=94=EA=B0=80=EB=90=9C=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=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 --- .../java/controller/BlackJackController.java | 12 +++++-- src/main/java/dto/PlayerDto.java | 27 ++++++++++++++ src/main/java/dto/PlayerHandsDto.java | 29 --------------- src/main/java/dto/PlayersDto.java | 35 +++++++++++++++++++ src/main/java/view/OutputView.java | 23 +++++++----- 5 files changed, 86 insertions(+), 40 deletions(-) create mode 100644 src/main/java/dto/PlayerDto.java delete mode 100644 src/main/java/dto/PlayerHandsDto.java create mode 100644 src/main/java/dto/PlayersDto.java diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 2f9ea149ad..ef5e801f24 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -5,7 +5,8 @@ import domain.Dealer; import domain.Player; import dto.DealerHandsDto; -import dto.PlayerHandsDto; +import dto.PlayerDto; +import dto.PlayersDto; import domain.Players; import view.InputView; import view.OutputView; @@ -25,10 +26,15 @@ public void run() { final Dealer dealer = new Dealer(cardDeck); dealer.startDeal(players); - outputView.printStartDeal(DealerHandsDto.from(dealer), PlayerHandsDto.from(players)); + outputView.printStartDeal(DealerHandsDto.from(dealer), PlayersDto.from(players)); for (Player player : players.getNames()) { - dealer.deal(player, Answer.from(inputView.readAnswer(player.getName()))); + Answer answer = Answer.from(inputView.readAnswer(player.getName())); + dealer.deal(player, answer); + + if (Answer.HIT.equals(answer)) { + outputView.printHands(PlayerDto.from(player)); + } } } } diff --git a/src/main/java/dto/PlayerDto.java b/src/main/java/dto/PlayerDto.java new file mode 100644 index 0000000000..fde206fdad --- /dev/null +++ b/src/main/java/dto/PlayerDto.java @@ -0,0 +1,27 @@ +package dto; + +import domain.Player; + +import java.util.List; + +public class PlayerDto { + private final String name; + private final List cards; + + private PlayerDto(final String name, final List cards) { + this.name = name; + this.cards = cards; + } + + public static PlayerDto from(final Player player) { + return new PlayerDto(player.getName(), player.getCards()); + } + + public String getName() { + return name; + } + + public List getCards() { + return cards; + } +} diff --git a/src/main/java/dto/PlayerHandsDto.java b/src/main/java/dto/PlayerHandsDto.java deleted file mode 100644 index 7d82527163..0000000000 --- a/src/main/java/dto/PlayerHandsDto.java +++ /dev/null @@ -1,29 +0,0 @@ -package dto; - -import domain.Player; -import domain.Players; - -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -public class PlayerHandsDto { - - private final Map> playerHands; - - public PlayerHandsDto(final Map> playerHands) { - this.playerHands = playerHands; - } - - public static PlayerHandsDto from(final Players players) { - Map> result = new LinkedHashMap<>(); - for (Player player : players.getNames()) { - result.put(player.getName(), player.getCards()); - } - return new PlayerHandsDto(result); - } - - public Map> getPlayerHands() { - return playerHands; - } -} diff --git a/src/main/java/dto/PlayersDto.java b/src/main/java/dto/PlayersDto.java new file mode 100644 index 0000000000..1be30cb341 --- /dev/null +++ b/src/main/java/dto/PlayersDto.java @@ -0,0 +1,35 @@ +package dto; + +import domain.Player; +import domain.Players; + +import java.util.ArrayList; +import java.util.List; + +public class PlayersDto { + + private final List players; + + private PlayersDto(final List players) { + this.players = players; + } + + public static PlayersDto from(final Players players) { + List result = new ArrayList<>(); + for (Player player : players.getNames()) { + result.add(PlayerDto.from(player)); + } + + return new PlayersDto(result); + } + + public List getNames() { + return players.stream() + .map(PlayerDto::getName) + .toList(); + } + + public List getPlayers() { + return players; + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index b13ea448b7..4a243e716e 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,26 +1,33 @@ package view; import dto.DealerHandsDto; -import dto.PlayerHandsDto; +import dto.PlayerDto; +import dto.PlayersDto; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import java.util.Set; public class OutputView { private final String FORM = "%s카드: %s"; - public void printStartDeal(final DealerHandsDto dealerHandsDto, final PlayerHandsDto playerHandsDto) { + public void printStartDeal(final DealerHandsDto dealerHandsDto, final PlayersDto playersDto) { final String dealerCard = dealerHandsDto.getDisplayedCard(); - final Set playerNames = playerHandsDto.getPlayerHands().keySet(); + + final List playerNames = playersDto.getNames(); System.out.println("딜러와 " + format(playerNames) + " 에게 2장을 나누었습니다."); + System.out.println("딜러: " + dealerCard); - Map> playerHands = playerHandsDto.getPlayerHands(); - for (Entry> stringListEntry : playerHands.entrySet()) { - System.out.printf(FORM, stringListEntry.getKey(), format(stringListEntry.getValue())); + + for (PlayerDto playerDto : playersDto.getPlayers()) { + System.out.printf(FORM, playerDto.getName(), format(playerDto.getCards())); System.out.println(); } + System.out.println(); + } + + public void printHands(final PlayerDto playerDto) { + System.out.printf(FORM, playerDto.getName(), format(playerDto.getCards())); + System.out.println(); } private String format(final Set playerNames) { From 9b1e7a4aea4dde7c64e7eb1fee05eb7a895a119a Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 6 Mar 2024 19:46:12 +0900 Subject: [PATCH 029/100] =?UTF-8?q?style:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/view/OutputView.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 4a243e716e..5100401f96 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -4,7 +4,6 @@ import dto.PlayerDto; import dto.PlayersDto; import java.util.List; -import java.util.Set; public class OutputView { @@ -30,10 +29,6 @@ public void printHands(final PlayerDto playerDto) { System.out.println(); } - private String format(final Set playerNames) { - return String.join(", ", playerNames); - } - private String format(final List playerNames) { return String.join(", ", playerNames); } From 546c4e9d36cb58470b72dfb3e7ca4f5d160ddfa4 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 6 Mar 2024 20:02:14 +0900 Subject: [PATCH 030/100] =?UTF-8?q?feat:=20=EC=B0=B8=EC=97=AC=EC=9E=90?= =?UTF-8?q?=EC=9D=98=20=EB=8C=80=EB=8B=B5=EC=9D=B4=20y=EC=9D=BC=EC=8B=9C?= =?UTF-8?q?=20=ED=95=9C=EB=B2=88=20=EB=8D=94=20deal=20=EA=B8=B0=EB=8A=A5?= =?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 --- .../java/controller/BlackJackController.java | 16 ++++++++++------ src/main/java/domain/Dealer.java | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index ef5e801f24..89580f0389 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -4,10 +4,10 @@ import domain.CardDeck; import domain.Dealer; import domain.Player; +import domain.Players; import dto.DealerHandsDto; import dto.PlayerDto; import dto.PlayersDto; -import domain.Players; import view.InputView; import view.OutputView; @@ -29,12 +29,16 @@ public void run() { outputView.printStartDeal(DealerHandsDto.from(dealer), PlayersDto.from(players)); for (Player player : players.getNames()) { - Answer answer = Answer.from(inputView.readAnswer(player.getName())); - dealer.deal(player, answer); + deal(player, dealer); + } + } - if (Answer.HIT.equals(answer)) { - outputView.printHands(PlayerDto.from(player)); - } + private void deal(final Player player, final Dealer dealer) { + Answer answer = Answer.HIT; + while (Answer.HIT.equals(answer)) { + answer = Answer.from(inputView.readAnswer(player.getName())); + dealer.deal(player, answer); + outputView.printHands(PlayerDto.from(player)); } } } diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 25397446a0..7d65d65197 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -22,7 +22,7 @@ public void startDeal(final Players players) { } public void deal(final Player player, final Answer answer) { - if (Answer.HIT == answer) { + if (Answer.HIT.equals(answer)) { player.add(cardDeck.pop()); } } From 9fd7e9c348e67ea34c8d1171c294ddb2921e5eea Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Wed, 6 Mar 2024 20:16:26 +0900 Subject: [PATCH 031/100] =?UTF-8?q?feat:=20=EB=B3=80=EA=B2=BD=EB=90=9C=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=EA=B0=80=20=EC=9E=88=EC=9D=84=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=20=EC=B6=9C=EB=A0=A5=ED=95=98=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 --- docs/README.md | 4 ++-- src/main/java/controller/BlackJackController.java | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 1afadf5a69..c4588bac3d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -19,9 +19,9 @@
-* [ ] 참여자는 hit(y) / stay(n)를 선택한다. +* [x] 참여자는 hit(y) / stay(n)를 선택한다. * [x] y, n 가 아닐시 예외가 발생한다. -* [ ] 현재 가지고 있는 카드를 출력한다. +* [x] 현재 가지고 있는 카드를 출력한다.
diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 89580f0389..2609803548 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -34,11 +34,21 @@ public void run() { } private void deal(final Player player, final Dealer dealer) { + boolean changed = false; Answer answer = Answer.HIT; + while (Answer.HIT.equals(answer)) { answer = Answer.from(inputView.readAnswer(player.getName())); dealer.deal(player, answer); - outputView.printHands(PlayerDto.from(player)); + + if (handsChanged(changed, answer)) { + outputView.printHands(PlayerDto.from(player)); + } + changed = true; } } + + private boolean handsChanged(final boolean changed, final Answer answer) { + return (Answer.STAY.equals(answer) && !changed) || Answer.HIT.equals(answer); + } } From 3c2ce482191a88af837c987fff2154060b06b23c Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 6 Mar 2024 20:30:43 +0900 Subject: [PATCH 032/100] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=EC=9D=98=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=EA=B0=80=2017=20=EC=9D=B4=EC=83=81=EC=9D=B4?= =?UTF-8?q?=20=EB=90=A0=EB=95=8C=EA=B9=8C=EC=A7=80=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EB=B0=9B=EB=8A=94=20=EA=B8=B0=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 --- docs/README.md | 2 +- src/main/java/domain/Dealer.java | 10 ++++++++++ src/main/java/view/OutputView.java | 5 +++++ src/test/java/domain/DealerTest.java | 15 ++++++++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index c4588bac3d..962b678787 100644 --- a/docs/README.md +++ b/docs/README.md @@ -29,7 +29,7 @@ * [x] 카드 내의 ACE 가 포함된 경우 * ACE: 11 * 11로 했을 때 카드의 합이 21을 초과한 경우 1로 계산 -* [ ] 17 이상이 될때까지 카드를 받는다. +* [x] 17 이상이 될때까지 카드를 받는다.
diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 7d65d65197..e2aa921d71 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -27,10 +27,20 @@ public void deal(final Player player, final Answer answer) { } } + public void deal() { + while (hands.sum() <= 16) { + hands.add(cardDeck.pop()); + } + } + public List getCards() { return hands.getCards() .stream() .map(Card::toString) .toList(); } + + public int getTotalCardSum() { + return hands.sum(); + } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 5100401f96..ee2efd737d 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,5 +1,6 @@ package view; +import domain.Dealer; import dto.DealerHandsDto; import dto.PlayerDto; import dto.PlayersDto; @@ -29,6 +30,10 @@ public void printHands(final PlayerDto playerDto) { System.out.println(); } + public void printDealer(final Dealer dealer) { + + } + private String format(final List playerNames) { return String.join(", ", playerNames); } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 60b00176ef..c659c5a5f7 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -1,7 +1,6 @@ package domain; import java.util.List; - import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -44,4 +43,18 @@ void addOneCard() { Assertions.assertThat(redddy.getPacketSize()).isEqualTo(3); Assertions.assertThat(zeze.getPacketSize()).isEqualTo(2); } + + @Test + @DisplayName("딜러의 카드 합이 16이하인 경우 카드를 추가한다.") + void addDealerCard() { + //given + final CardDeck cardDeck = CardDeck.generate(); + final Dealer dealer = new Dealer(cardDeck); + + //when + dealer.deal(); + + //then + Assertions.assertThat(dealer.getTotalCardSum()).isGreaterThan(16); + } } From eb08c75b56948db7b46e7d82937228d1d5720ca8 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Wed, 6 Mar 2024 21:10:19 +0900 Subject: [PATCH 033/100] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=EA=B0=80=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EB=B0=9B=EC=9D=84=20=EB=95=8C=20?= =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=B6=9C=EB=A0=A5=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 --- docs/README.md | 2 +- src/main/java/controller/BlackJackController.java | 5 +++++ src/main/java/domain/Dealer.java | 4 +--- src/main/java/view/OutputView.java | 4 ++-- src/test/java/domain/DealerTest.java | 14 -------------- 5 files changed, 9 insertions(+), 20 deletions(-) diff --git a/docs/README.md b/docs/README.md index 962b678787..5f8e3cbf8d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,7 +25,7 @@
-* [ ] 딜러의 카드의 합을 계산한다. +* [x] 딜러의 카드의 합을 계산한다. * [x] 카드 내의 ACE 가 포함된 경우 * ACE: 11 * 11로 했을 때 카드의 합이 21을 초과한 경우 1로 계산 diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 2609803548..64aca4424c 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -31,6 +31,11 @@ public void run() { for (Player player : players.getNames()) { deal(player, dealer); } + + while (dealer.getTotalCardSum() <= 16) { //TODO 테스트 코드 고민 + dealer.deal(); + outputView.printDealerCard(); + } } private void deal(final Player player, final Dealer dealer) { diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index e2aa921d71..3606f02ff9 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -28,9 +28,7 @@ public void deal(final Player player, final Answer answer) { } public void deal() { - while (hands.sum() <= 16) { - hands.add(cardDeck.pop()); - } + hands.add(cardDeck.pop()); } public List getCards() { diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index ee2efd737d..0a99352ee6 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -30,8 +30,8 @@ public void printHands(final PlayerDto playerDto) { System.out.println(); } - public void printDealer(final Dealer dealer) { - + public void printDealerCard() { + System.out.println("딜러는 16이하라 한장의 카드를 더 받았습니다."); //TODO 메서드 변경 } private String format(final List playerNames) { diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index c659c5a5f7..a529e6d4ae 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -43,18 +43,4 @@ void addOneCard() { Assertions.assertThat(redddy.getPacketSize()).isEqualTo(3); Assertions.assertThat(zeze.getPacketSize()).isEqualTo(2); } - - @Test - @DisplayName("딜러의 카드 합이 16이하인 경우 카드를 추가한다.") - void addDealerCard() { - //given - final CardDeck cardDeck = CardDeck.generate(); - final Dealer dealer = new Dealer(cardDeck); - - //when - dealer.deal(); - - //then - Assertions.assertThat(dealer.getTotalCardSum()).isGreaterThan(16); - } } From a1f0915246703b7ea7f36c18ee5a985464687142 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 10:59:30 +0900 Subject: [PATCH 034/100] =?UTF-8?q?feat:=20=EB=AA=A8=EB=93=A0=20=EC=B0=B8?= =?UTF-8?q?=EC=97=AC=EC=9E=90=EC=9D=98=20=EC=B9=B4=EB=93=9C=EC=9D=98=20?= =?UTF-8?q?=ED=95=A9=EC=9D=84=20=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=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 --- docs/README.md | 1 + src/main/java/controller/BlackJackController.java | 2 ++ src/main/java/domain/Dealer.java | 6 ++++++ src/main/java/domain/Player.java | 4 ++++ src/main/java/dto/PlayerDto.java | 15 +++++++++++++-- src/main/java/dto/PlayersDto.java | 7 +++++++ src/main/java/view/OutputView.java | 9 +++++++++ 7 files changed, 42 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 5f8e3cbf8d..c582e33ac4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -34,5 +34,6 @@
* [x] 모든 참여자의 카드의 합을 계산한다. +* [x] 모든 참여자의 카드의 합을 출력한다. * [ ] 최종 승패를 확인한다. * [ ] 게임 결과를 출력한다. diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 64aca4424c..284a540e42 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -36,6 +36,8 @@ public void run() { dealer.deal(); outputView.printDealerCard(); } + + outputView.printHandsResult(PlayersDto.from(dealer, players)); } private void deal(final Player player, final Dealer dealer) { diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 3606f02ff9..0590f03bcf 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -4,6 +4,7 @@ public class Dealer { + private static final String NAME = "딜러"; private static final int INIT_CARD_NUMBER = 2; //TODO 이름 생각해보기 private final CardDeck cardDeck; @@ -41,4 +42,9 @@ public List getCards() { public int getTotalCardSum() { return hands.sum(); } + + public String getName() { + return NAME; + } + } diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 7523764ef1..91970992c0 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -52,6 +52,10 @@ public List getCards() { .toList(); } + public int getTotalSum() { + return hands.sum(); + } + @Override public boolean equals(final Object target) { if (this == target) { diff --git a/src/main/java/dto/PlayerDto.java b/src/main/java/dto/PlayerDto.java index fde206fdad..fda6d2c405 100644 --- a/src/main/java/dto/PlayerDto.java +++ b/src/main/java/dto/PlayerDto.java @@ -1,5 +1,6 @@ package dto; +import domain.Dealer; import domain.Player; import java.util.List; @@ -7,14 +8,20 @@ public class PlayerDto { private final String name; private final List cards; + private final int totalSum; - private PlayerDto(final String name, final List cards) { + public PlayerDto(final String name, final List cards, final int totalSum) { this.name = name; this.cards = cards; + this.totalSum = totalSum; } public static PlayerDto from(final Player player) { - return new PlayerDto(player.getName(), player.getCards()); + return new PlayerDto(player.getName(), player.getCards(), player.getTotalSum()); + } + + public static PlayerDto from(final Dealer dealer) { + return new PlayerDto(dealer.getName(), dealer.getCards(), dealer.getTotalCardSum()); } public String getName() { @@ -24,4 +31,8 @@ public String getName() { public List getCards() { return cards; } + + public int getTotalSum() { + return totalSum; + } } diff --git a/src/main/java/dto/PlayersDto.java b/src/main/java/dto/PlayersDto.java index 1be30cb341..5b8c34a72b 100644 --- a/src/main/java/dto/PlayersDto.java +++ b/src/main/java/dto/PlayersDto.java @@ -1,5 +1,6 @@ package dto; +import domain.Dealer; import domain.Player; import domain.Players; @@ -14,6 +15,12 @@ private PlayersDto(final List players) { this.players = players; } + public static PlayersDto from(final Dealer dealer, final Players players) { // TODO 추상화 고려 + PlayersDto playersDto = from(players); + playersDto.players.add(0, PlayerDto.from(dealer)); + return playersDto; + } + public static PlayersDto from(final Players players) { List result = new ArrayList<>(); for (Player player : players.getNames()) { diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 0a99352ee6..7488048145 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -9,6 +9,7 @@ public class OutputView { private final String FORM = "%s카드: %s"; + private final String TOTAL_SUM_FORM = "%s 카드: %s - 결과: %d"; public void printStartDeal(final DealerHandsDto dealerHandsDto, final PlayersDto playersDto) { final String dealerCard = dealerHandsDto.getDisplayedCard(); @@ -32,6 +33,14 @@ public void printHands(final PlayerDto playerDto) { public void printDealerCard() { System.out.println("딜러는 16이하라 한장의 카드를 더 받았습니다."); //TODO 메서드 변경 + System.out.println(); + } + + public void printHandsResult(final PlayersDto playersDto) { + for (PlayerDto playerDto : playersDto.getPlayers()) { + System.out.printf(TOTAL_SUM_FORM, playerDto.getName(), format(playerDto.getCards()), playerDto.getTotalSum() ); + System.out.println(); + } } private String format(final List playerNames) { From 35955932aa8369849f4783cf7c932e506c32ba6f Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 11:43:40 +0900 Subject: [PATCH 035/100] =?UTF-8?q?feat:=20hands=EC=9D=98=20=EC=8A=B9?= =?UTF-8?q?=ED=8C=A8=20=ED=8C=90=EB=8B=A8=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Hands.java | 5 +++ src/test/java/domain/HandsTest.java | 51 +++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index ff848510fc..4266da73e8 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -29,6 +29,11 @@ public int sum() { return total; } + //TODO: 메서드 명 수정 , blackjack 을 넘는지도 확인하고 있음 + public boolean isWin(final Hands target) { + return this.sum() >= target.sum() && this.sum() <= BLACK_JACK; + } + public void add(final Card card) { cards.add(card); } diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index 7fa88cad08..366ff3dbdf 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -1,5 +1,7 @@ package domain; +import java.util.List; +import java.util.stream.Stream; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -7,9 +9,6 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import java.util.List; -import java.util.stream.Stream; - class HandsTest { @Test @@ -67,6 +66,52 @@ void sumAce1(final Hands hands, final int expected) { Assertions.assertThat(result).isEqualTo(expected); } + @Test + @DisplayName("카드 합이 21이하이면서 승리한다.") + void isWin() { + //given + final Hands sum20 = new Hands( + List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), + new Card(CardNumber.ACE, CardShape.SPADE))); + + final Hands sum21 = new Hands( + List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + + //when && then + Assertions.assertThat(sum21.isWin(sum20)).isTrue(); + } + + + @Test + @DisplayName("카드 합이 21이하이면서 패배한다.") + void isLose() { + final Hands sum20 = new Hands( + List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), + new Card(CardNumber.ACE, CardShape.SPADE))); + + final Hands sum21 = new Hands( + List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + + //when && then + Assertions.assertThat(sum20.isWin(sum21)).isFalse(); + } + + @Test + @DisplayName("카드 합이 21초과이면 패배한다.") + void isLoseWhenCardSumGreater21() { + //given + final Hands sum18 = new Hands( + List.of(new Card(CardNumber.SEVEN, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + + final Hands sum22 = new Hands( + List.of(new Card(CardNumber.NINE, CardShape.HEART), new Card(CardNumber.EIGHT, CardShape.SPADE), + new Card(CardNumber.FIVE, CardShape.HEART))); + + //when && then + Assertions.assertThat(sum22.isWin(sum18)).isFalse(); + } + + static Stream sumParameterProvider() { return Stream.of( Arguments.of(new Hands(List.of(new Card(CardNumber.TWO, CardShape.HEART), From e26d9885e09dec23f68a2bcdf8a6a52f1cb624e7 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 11:50:59 +0900 Subject: [PATCH 036/100] =?UTF-8?q?feat:=20=EC=B0=B8=EC=97=AC=EC=9E=90?= =?UTF-8?q?=EC=9D=98=20=EC=8A=B9=ED=8C=A8=20=ED=99=95=EC=9D=B8=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 --- docs/README.md | 3 ++- src/main/java/domain/Dealer.java | 9 +++++++ src/main/java/domain/Game.java | 31 +++++++++++++++++++++++ src/main/java/domain/Player.java | 4 +++ src/main/java/domain/Result.java | 14 +++++++++++ src/test/java/domain/GameTest.java | 37 ++++++++++++++++++++++++++++ src/test/java/domain/PlayerTest.java | 3 --- 7 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 src/main/java/domain/Game.java create mode 100644 src/main/java/domain/Result.java create mode 100644 src/test/java/domain/GameTest.java diff --git a/docs/README.md b/docs/README.md index 5f8e3cbf8d..e040ae4f98 100644 --- a/docs/README.md +++ b/docs/README.md @@ -34,5 +34,6 @@
* [x] 모든 참여자의 카드의 합을 계산한다. -* [ ] 최종 승패를 확인한다. +* [ ] 딜러의 승패를 확인한다. +* [x] 참여자의 승패를 확인한다. * [ ] 게임 결과를 출력한다. diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 3606f02ff9..cbe754b55c 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -14,6 +14,11 @@ public Dealer(final CardDeck cardDeck) { this.hands = Hands.createEmptyPacket(); } + public Dealer(final CardDeck cardDeck, final Hands hands) { + this.cardDeck = cardDeck; + this.hands = hands; + } + public void startDeal(final Players players) { for (int i = 0; i < INIT_CARD_NUMBER; i++) { players.forEach(player -> player.add(cardDeck.pop())); @@ -41,4 +46,8 @@ public List getCards() { public int getTotalCardSum() { return hands.sum(); } + + public Hands getHands() { + return hands; + } } diff --git a/src/main/java/domain/Game.java b/src/main/java/domain/Game.java new file mode 100644 index 0000000000..23f79b05d4 --- /dev/null +++ b/src/main/java/domain/Game.java @@ -0,0 +1,31 @@ +package domain; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class Game { + + private final Dealer dealer; + private final Players players; + + public Game(final Dealer dealer, final Players players) { + this.dealer = dealer; + this.players = players; + } + + public Map getResult() { + return calculate(); + } + + private Map calculate() { + final Map map = new LinkedHashMap<>(); + for (Player player : players.getNames()) { // TODO 인덴트 줄이기 + if (player.isWin(dealer)) { + map.put(player, Result.WIN); + continue; + } + map.put(player, Result.LOSE); + } + return map; + } +} diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 7523764ef1..d566506260 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -17,6 +17,10 @@ public void add(final Card card) { hands.add(card); } + public boolean isWin(final Dealer dealer) { + return this.hands.isWin(dealer.getHands()); + } + //TODO 메서드 명 변경 private void validate(final String name) { diff --git a/src/main/java/domain/Result.java b/src/main/java/domain/Result.java new file mode 100644 index 0000000000..9d808e9613 --- /dev/null +++ b/src/main/java/domain/Result.java @@ -0,0 +1,14 @@ +package domain; + +public enum Result { + + WIN("승"), + LOSE("패"), + ; + + private final String value; + + Result(final String value) { + this.value = value; + } +} diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java new file mode 100644 index 0000000000..9c9ffc9a6e --- /dev/null +++ b/src/test/java/domain/GameTest.java @@ -0,0 +1,37 @@ +package domain; + +import java.util.List; +import java.util.Map; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class GameTest { + + @Test + @DisplayName("딜러의 승패를 판단한다.") + void checkDealerWin() { + //given + + Hands sum18 = new Hands(List.of(new Card(CardNumber.EIGHT, CardShape.CLOVER), + new Card(CardNumber.TEN, CardShape.DIAMOND))); // 18 + Hands sum21 = new Hands( + List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + + Hands sum20 = new Hands( + List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), + new Card(CardNumber.ACE, CardShape.SPADE))); + + Player loser = new Player("레디", sum18); + Player winner = new Player("제제", sum21); + + Players players = new Players(List.of(loser, winner)); + Dealer dealer = new Dealer(CardDeck.generate(), sum20); + + Game game = new Game(dealer, players); + + //when & then + Map expected = Map.of(loser, Result.LOSE, winner, Result.WIN); + Assertions.assertThat(game.getResult()).isEqualTo(expected); + } +} diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index e2f48da2a4..33238c85b6 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -1,14 +1,11 @@ package domain; -import java.util.List; -import java.util.Scanner; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; -import view.InputView; class PlayerTest { @DisplayName("이름으로 참여자를 생성한다.") From bbd54df4bb537276f419a9f3e78bcbc49b90856a Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 11:51:55 +0900 Subject: [PATCH 037/100] =?UTF-8?q?style:=20'=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4'=EB=A5=BC=20'=EC=B0=B8=EC=97=AC=EC=9E=90'=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/DealerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index a529e6d4ae..4b103ab947 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -8,7 +8,7 @@ class DealerTest { @Test - @DisplayName("플레이어에게 카드 2장을 나눠준다.") + @DisplayName("참여자에게 카드 2장을 나눠준다.") void dealCards() { //given final Players players = Players.from(List.of("레디", "제제")); @@ -23,7 +23,7 @@ void dealCards() { } @Test - @DisplayName("플레이어의 답변이 y라면 카드를 한장 추가한다.") + @DisplayName("참여자의 답변이 y라면 카드를 한장 추가한다.") void addOneCard() { //given final Player redddy = new Player("레디", Hands.createEmptyPacket()); From ef45b28563bdf964214ad6b0a77c700e22f7da3a Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 13:08:35 +0900 Subject: [PATCH 038/100] =?UTF-8?q?fix:=20=EC=BB=B4=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/domain/Dealer.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 686789f142..e040ae4f98 100644 --- a/docs/README.md +++ b/docs/README.md @@ -34,6 +34,6 @@
* [x] 모든 참여자의 카드의 합을 계산한다. -* [x] 딜러의 승패를 확인한다. +* [ ] 딜러의 승패를 확인한다. * [x] 참여자의 승패를 확인한다. * [ ] 게임 결과를 출력한다. diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 3606f02ff9..5680b903e1 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -3,6 +3,7 @@ import java.util.List; public class Dealer { + private static final String NAME = "딜러"; private static final int INIT_CARD_NUMBER = 2; //TODO 이름 생각해보기 @@ -14,6 +15,11 @@ public Dealer(final CardDeck cardDeck) { this.hands = Hands.createEmptyPacket(); } + public Dealer(final CardDeck cardDeck, final Hands hands) { + this.cardDeck = cardDeck; + this.hands = hands; + } + public void startDeal(final Players players) { for (int i = 0; i < INIT_CARD_NUMBER; i++) { players.forEach(player -> player.add(cardDeck.pop())); @@ -41,4 +47,12 @@ public List getCards() { public int getTotalCardSum() { return hands.sum(); } + + public Hands getHands() { + return hands; + } + + public String getName() { + return NAME; + } } From 8f1e9b86314e5b4aaeb1bdbdd2fbc52933058f53 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 13:29:56 +0900 Subject: [PATCH 039/100] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=EC=9D=98=20?= =?UTF-8?q?=EC=8A=B9=ED=8C=A8=EB=A5=BC=20=ED=99=95=EC=9D=B8=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 --- docs/README.md | 2 +- src/main/java/domain/Game.java | 13 +++++++++++ src/main/java/domain/Result.java | 7 ++++++ src/test/java/domain/GameTest.java | 35 +++++++++++++++++++++++++++--- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/docs/README.md b/docs/README.md index e040ae4f98..686789f142 100644 --- a/docs/README.md +++ b/docs/README.md @@ -34,6 +34,6 @@
* [x] 모든 참여자의 카드의 합을 계산한다. -* [ ] 딜러의 승패를 확인한다. +* [x] 딜러의 승패를 확인한다. * [x] 참여자의 승패를 확인한다. * [ ] 게임 결과를 출력한다. diff --git a/src/main/java/domain/Game.java b/src/main/java/domain/Game.java index 23f79b05d4..3f713b0f03 100644 --- a/src/main/java/domain/Game.java +++ b/src/main/java/domain/Game.java @@ -1,5 +1,7 @@ package domain; +import java.util.EnumMap; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -17,6 +19,17 @@ public Map getResult() { return calculate(); } + public Map getDealerResult() { + Map dealerResult = new EnumMap<>(Result.class); + + for (Result value : getResult().values()) { + Result reversed = value.reverse(); + Integer orDefault = dealerResult.getOrDefault(reversed, 0); + dealerResult.put(reversed, orDefault + 1); + } + return dealerResult; + } + private Map calculate() { final Map map = new LinkedHashMap<>(); for (Player player : players.getNames()) { // TODO 인덴트 줄이기 diff --git a/src/main/java/domain/Result.java b/src/main/java/domain/Result.java index 9d808e9613..247013c7b5 100644 --- a/src/main/java/domain/Result.java +++ b/src/main/java/domain/Result.java @@ -11,4 +11,11 @@ public enum Result { Result(final String value) { this.value = value; } + + public Result reverse() { + if (Result.WIN.equals(this)) { + return LOSE; + } + return WIN; + } } diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index 9c9ffc9a6e..658a327b7f 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -9,10 +9,9 @@ class GameTest { @Test - @DisplayName("딜러의 승패를 판단한다.") - void checkDealerWin() { + @DisplayName("참여자의 승패를 판단한다.") + void playerResult() { //given - Hands sum18 = new Hands(List.of(new Card(CardNumber.EIGHT, CardShape.CLOVER), new Card(CardNumber.TEN, CardShape.DIAMOND))); // 18 Hands sum21 = new Hands( @@ -34,4 +33,34 @@ void checkDealerWin() { Map expected = Map.of(loser, Result.LOSE, winner, Result.WIN); Assertions.assertThat(game.getResult()).isEqualTo(expected); } + + @DisplayName("딜러의 승패를 판단한다.") + @Test + void dealerResult() { + // given + Hands sum18 = new Hands(List.of(new Card(CardNumber.EIGHT, CardShape.CLOVER), + new Card(CardNumber.TEN, CardShape.DIAMOND))); // 18 + + Hands sum21 = new Hands( + List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + + Hands sum20 = new Hands( + List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), + new Card(CardNumber.ACE, CardShape.SPADE))); + + Player loser1 = new Player("레디", sum18); + Player loser2 = new Player("포비", sum18); + Player winner = new Player("제제", sum21); + + Players players = new Players(List.of(loser1, loser2, winner)); + Dealer dealer = new Dealer(CardDeck.generate(), sum20); + + Game game = new Game(dealer, players); + + // when + Map expected = Map.of(Result.WIN, 2, Result.LOSE, 1); + + // then + Assertions.assertThat(game.getDealerResult()).isEqualTo(expected); + } } From 755f99af8cf0e25020282a9bf06bf35f0ffbff0d Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 13:39:19 +0900 Subject: [PATCH 040/100] =?UTF-8?q?docs:=20=EA=B5=AC=ED=98=84=ED=95=B4?= =?UTF-8?q?=EC=95=BC=20=ED=95=A0=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9=EB=A1=9D?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 1 + src/main/java/domain/Result.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 686789f142..dbca8fafcd 100644 --- a/docs/README.md +++ b/docs/README.md @@ -22,6 +22,7 @@ * [x] 참여자는 hit(y) / stay(n)를 선택한다. * [x] y, n 가 아닐시 예외가 발생한다. * [x] 현재 가지고 있는 카드를 출력한다. +* [ ] 카드 합이 21 초과시 버스트 메시지를 출력한다.
diff --git a/src/main/java/domain/Result.java b/src/main/java/domain/Result.java index 247013c7b5..56b736bbb3 100644 --- a/src/main/java/domain/Result.java +++ b/src/main/java/domain/Result.java @@ -1,6 +1,6 @@ package domain; -public enum Result { +public enum Result { //TODO 무승부 WIN("승"), LOSE("패"), From d8f2ad4b3371593810448c199eb635d59d164608 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 13:51:35 +0900 Subject: [PATCH 041/100] =?UTF-8?q?feat:=20=EC=B5=9C=EC=A2=85=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=ED=95=98=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 --- docs/README.md | 2 +- .../java/controller/BlackJackController.java | 3 +++ src/main/java/domain/Result.java | 4 +++ src/main/java/view/OutputView.java | 25 ++++++++++++++++++- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index dbca8fafcd..7d68362038 100644 --- a/docs/README.md +++ b/docs/README.md @@ -37,4 +37,4 @@ * [x] 모든 참여자의 카드의 합을 계산한다. * [x] 딜러의 승패를 확인한다. * [x] 참여자의 승패를 확인한다. -* [ ] 게임 결과를 출력한다. +* [x] 게임 결과를 출력한다. diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 284a540e42..e1c5672ce8 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -3,6 +3,7 @@ import domain.Answer; import domain.CardDeck; import domain.Dealer; +import domain.Game; import domain.Player; import domain.Players; import dto.DealerHandsDto; @@ -24,6 +25,7 @@ public void run() { final Players players = Players.from(inputView.readPlayerNames()); final CardDeck cardDeck = CardDeck.generate(); final Dealer dealer = new Dealer(cardDeck); + final Game game = new Game(dealer, players); dealer.startDeal(players); outputView.printStartDeal(DealerHandsDto.from(dealer), PlayersDto.from(players)); @@ -38,6 +40,7 @@ public void run() { } outputView.printHandsResult(PlayersDto.from(dealer, players)); + outputView.printGameResult(game.getDealerResult(), game.getResult()); } private void deal(final Player player, final Dealer dealer) { diff --git a/src/main/java/domain/Result.java b/src/main/java/domain/Result.java index 56b736bbb3..73f8c3e64f 100644 --- a/src/main/java/domain/Result.java +++ b/src/main/java/domain/Result.java @@ -18,4 +18,8 @@ public Result reverse() { } return WIN; } + + public String getValue() { + return value; + } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 7488048145..588b52fb2e 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,15 +1,19 @@ package view; import domain.Dealer; +import domain.Player; +import domain.Result; import dto.DealerHandsDto; import dto.PlayerDto; import dto.PlayersDto; import java.util.List; +import java.util.Map; public class OutputView { private final String FORM = "%s카드: %s"; private final String TOTAL_SUM_FORM = "%s 카드: %s - 결과: %d"; + private final String RESULT_FORM = "%s: %s"; public void printStartDeal(final DealerHandsDto dealerHandsDto, final PlayersDto playersDto) { final String dealerCard = dealerHandsDto.getDisplayedCard(); @@ -38,11 +42,30 @@ public void printDealerCard() { public void printHandsResult(final PlayersDto playersDto) { for (PlayerDto playerDto : playersDto.getPlayers()) { - System.out.printf(TOTAL_SUM_FORM, playerDto.getName(), format(playerDto.getCards()), playerDto.getTotalSum() ); + System.out.printf(TOTAL_SUM_FORM, playerDto.getName(), format(playerDto.getCards()), playerDto.getTotalSum()); System.out.println(); } } + public void printGameResult(final Map dealerResult, final Map playerResult) { + System.out.println("## 최종결과"); + System.out.printf(RESULT_FORM, "딜러", format(dealerResult)); + System.out.println(); + for (Map.Entry entry : playerResult.entrySet()) { + System.out.printf(RESULT_FORM, entry.getKey().getName(), entry.getValue().getValue()); + System.out.println(); + } + } + + private String format(final Map dealerResult) { + StringBuilder stringBuilder = new StringBuilder(); + for (Map.Entry entry : dealerResult.entrySet()) { + stringBuilder.append(String.valueOf(entry.getValue()) + entry.getKey().getValue() + " "); + } + + return stringBuilder.toString(); + } + private String format(final List playerNames) { return String.join(", ", playerNames); } From 4f080a19941d0dfe92972a49c4ea3b07f88ac0ab Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 14:57:52 +0900 Subject: [PATCH 042/100] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90?= =?UTF-8?q?=EC=9D=98=20=EC=B9=B4=EB=93=9C=EC=9D=98=20=ED=95=A9=EC=9D=B4=20?= =?UTF-8?q?21=EC=9D=84=20=EC=B4=88=EA=B3=BC=ED=95=98=EB=A9=B4=20BUST=20?= =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=B6=9C=EB=A0=A5=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 --- docs/README.md | 2 +- src/main/java/controller/BlackJackController.java | 4 ++++ src/main/java/domain/Hands.java | 5 +++++ src/main/java/domain/Player.java | 4 ++++ src/main/java/view/OutputView.java | 7 ++++++- 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 7d68362038..a8c7261f8b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -22,7 +22,7 @@ * [x] 참여자는 hit(y) / stay(n)를 선택한다. * [x] y, n 가 아닐시 예외가 발생한다. * [x] 현재 가지고 있는 카드를 출력한다. -* [ ] 카드 합이 21 초과시 버스트 메시지를 출력한다. +* [x] 카드 합이 21 초과시 버스트 메시지를 출력한다.
diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index e1c5672ce8..5a7afdd790 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -54,6 +54,10 @@ private void deal(final Player player, final Dealer dealer) { if (handsChanged(changed, answer)) { outputView.printHands(PlayerDto.from(player)); } + if (player.isBust()) { + outputView.printBustMessage(); + break; + } changed = true; } } diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 4266da73e8..1aa4737868 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -38,6 +38,10 @@ public void add(final Card card) { cards.add(card); } + public boolean isBust() { + return sum() > BLACK_JACK; + } + private boolean hasAce() { return cards.stream() .anyMatch(Card::isAce); @@ -50,4 +54,5 @@ public int size() { public List getCards() { return cards; } + } diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 894b91238f..82b64e5de8 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -56,6 +56,10 @@ public List getCards() { .toList(); } + public boolean isBust() { + return hands.isBust(); + } + public int getTotalSum() { return hands.sum(); } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 588b52fb2e..e070755107 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -60,7 +60,7 @@ public void printGameResult(final Map dealerResult, final Map

dealerResult) { StringBuilder stringBuilder = new StringBuilder(); for (Map.Entry entry : dealerResult.entrySet()) { - stringBuilder.append(String.valueOf(entry.getValue()) + entry.getKey().getValue() + " "); + stringBuilder.append(entry.getValue() + entry.getKey().getValue() + " "); } return stringBuilder.toString(); @@ -69,4 +69,9 @@ private String format(final Map dealerResult) { private String format(final List playerNames) { return String.join(", ", playerNames); } + + public void printBustMessage() { + System.out.println("BUST"); + System.out.println(); + } } From f6438d3663114e6b851ce25c143949a18b362364 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 15:14:40 +0900 Subject: [PATCH 043/100] =?UTF-8?q?feat:=20=EB=AA=A8=EB=93=A0=20=EC=B0=B8?= =?UTF-8?q?=EC=97=AC=EC=9E=90=EA=B0=80=20=EB=B2=84=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=9D=BC=EB=A9=B4=20=EB=94=9C=EB=9F=AC=EB=8A=94=20=EB=94=9C?= =?UTF-8?q?=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EA=B8=B0=EB=8A=A5=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 --- .../java/controller/BlackJackController.java | 6 +++ src/main/java/domain/Players.java | 5 +++ src/test/java/domain/PlayersTest.java | 40 ++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 5a7afdd790..0fe2c5409b 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -34,6 +34,12 @@ public void run() { deal(player, dealer); } + if (players.isAllBust()) { + outputView.printHandsResult(PlayersDto.from(dealer, players)); + outputView.printGameResult(game.getDealerResult(), game.getResult()); + return; + } + while (dealer.getTotalCardSum() <= 16) { //TODO 테스트 코드 고민 dealer.deal(); outputView.printDealerCard(); diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index 67ff2d50d4..e4a4c02fc6 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -28,6 +28,11 @@ public void forEach(Consumer action) { names.forEach(action); } + public boolean isAllBust() { + return names.stream() + .allMatch(Player::isBust); + } + private static void validate(final List players) { validateSize(players); validateDuplicate(players); diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index 617f85ddd2..5d11bca012 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -2,7 +2,6 @@ import java.util.List; import java.util.stream.Stream; - import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -41,6 +40,45 @@ void invalidPlayersSize(final List names) { .hasMessage("[ERROR] 유효하지 않은 참여자 수입니다."); } + @Test + @DisplayName("참가자 중 버스트 되지 않은 참가자가 있다면 False를 반환한다.") + void isAllBustFalse() { + //given + Hands bustHands = new Hands( + List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.TEN, CardShape.SPADE), + new Card(CardNumber.TEN, CardShape.HEART))); + + Hands noBustHands = new Hands( + List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.TEN, CardShape.SPADE))); + + Player player1 = new Player("레디", bustHands); + Player player2 = new Player("제제", noBustHands); + Players players = new Players(List.of(player1, player2)); + + //when && then + Assertions.assertThat(players.isAllBust()).isFalse(); + } + + @Test + @DisplayName("모든 참가자가 버스트되면 True를 반환한다.") + void isAllBustTrue() { + //given + Hands bustHands = new Hands( + List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.TEN, CardShape.SPADE), + new Card(CardNumber.TEN, CardShape.HEART))); + + Player player1 = new Player("레디", bustHands); + Player player2 = new Player("제제", bustHands); + Player player3 = new Player("수달", bustHands); + Player player4 = new Player("피케이", bustHands); + + Players players = new Players(List.of(player1, player2, player3, player4)); + + //when && then + Assertions.assertThat(players.isAllBust()).isTrue(); + } + + static Stream validPlayersSizeParameterProvider() { return Stream.of( Arguments.of( From 649014aa8853d9dab329fb579067a886aaf75701 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 15:42:08 +0900 Subject: [PATCH 044/100] =?UTF-8?q?feat:=20=EB=AC=B4=EC=8A=B9=EB=B6=80=20?= =?UTF-8?q?=EA=B8=B0=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 --- docs/README.md | 4 +-- src/main/java/domain/Game.java | 7 +---- src/main/java/domain/Hands.java | 13 +++++++--- src/main/java/domain/Player.java | 4 +-- src/main/java/domain/Result.java | 6 ++++- src/test/java/domain/GameTest.java | 16 +++++++----- src/test/java/domain/HandsTest.java | 40 +++++++++++++++++++++++++---- 7 files changed, 63 insertions(+), 27 deletions(-) diff --git a/docs/README.md b/docs/README.md index a8c7261f8b..58b230e429 100644 --- a/docs/README.md +++ b/docs/README.md @@ -35,6 +35,6 @@
* [x] 모든 참여자의 카드의 합을 계산한다. -* [x] 딜러의 승패를 확인한다. -* [x] 참여자의 승패를 확인한다. +* [x] 딜러의 승패무를 확인한다. +* [x] 참여자의 승패무를 확인한다. * [x] 게임 결과를 출력한다. diff --git a/src/main/java/domain/Game.java b/src/main/java/domain/Game.java index 3f713b0f03..35ac065753 100644 --- a/src/main/java/domain/Game.java +++ b/src/main/java/domain/Game.java @@ -1,7 +1,6 @@ package domain; import java.util.EnumMap; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -33,11 +32,7 @@ public Map getDealerResult() { private Map calculate() { final Map map = new LinkedHashMap<>(); for (Player player : players.getNames()) { // TODO 인덴트 줄이기 - if (player.isWin(dealer)) { - map.put(player, Result.WIN); - continue; - } - map.put(player, Result.LOSE); + map.put(player, player.isWin(dealer)); } return map; } diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 1aa4737868..b6c0fc15b0 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -29,9 +29,15 @@ public int sum() { return total; } - //TODO: 메서드 명 수정 , blackjack 을 넘는지도 확인하고 있음 - public boolean isWin(final Hands target) { - return this.sum() >= target.sum() && this.sum() <= BLACK_JACK; + public Result calculateResult(final Hands target) { + if (this.sum() >= target.sum() && this.sum() <= BLACK_JACK) { + if (this.size() == target.size()) { + return Result.TIE; + } + + return Result.WIN; + } + return Result.LOSE; } public void add(final Card card) { @@ -54,5 +60,4 @@ public int size() { public List getCards() { return cards; } - } diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index 82b64e5de8..efe87158c0 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -17,8 +17,8 @@ public void add(final Card card) { hands.add(card); } - public boolean isWin(final Dealer dealer) { - return this.hands.isWin(dealer.getHands()); + public Result isWin(final Dealer dealer) { + return this.hands.calculateResult(dealer.getHands()); } //TODO 메서드 명 변경 diff --git a/src/main/java/domain/Result.java b/src/main/java/domain/Result.java index 73f8c3e64f..8cf7ce2d29 100644 --- a/src/main/java/domain/Result.java +++ b/src/main/java/domain/Result.java @@ -4,6 +4,7 @@ public enum Result { //TODO 무승부 WIN("승"), LOSE("패"), + TIE("무") ; private final String value; @@ -16,7 +17,10 @@ public Result reverse() { if (Result.WIN.equals(this)) { return LOSE; } - return WIN; + if (Result.LOSE.equals(this)) { + return WIN; + } + return TIE; } public String getValue() { diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index 658a327b7f..0a5908cd3f 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -9,7 +9,7 @@ class GameTest { @Test - @DisplayName("참여자의 승패를 판단한다.") + @DisplayName("참여자의 승패무를 판단한다.") void playerResult() { //given Hands sum18 = new Hands(List.of(new Card(CardNumber.EIGHT, CardShape.CLOVER), @@ -23,18 +23,19 @@ void playerResult() { Player loser = new Player("레디", sum18); Player winner = new Player("제제", sum21); + Player tier = new Player("수달", sum20); - Players players = new Players(List.of(loser, winner)); + Players players = new Players(List.of(loser, winner, tier)); Dealer dealer = new Dealer(CardDeck.generate(), sum20); Game game = new Game(dealer, players); //when & then - Map expected = Map.of(loser, Result.LOSE, winner, Result.WIN); + Map expected = Map.of(loser, Result.LOSE, winner, Result.WIN, tier, Result.TIE); Assertions.assertThat(game.getResult()).isEqualTo(expected); } - @DisplayName("딜러의 승패를 판단한다.") + @DisplayName("딜러의 승패무를 판단한다.") @Test void dealerResult() { // given @@ -49,16 +50,17 @@ void dealerResult() { new Card(CardNumber.ACE, CardShape.SPADE))); Player loser1 = new Player("레디", sum18); - Player loser2 = new Player("포비", sum18); + Player loser2 = new Player("피케이", sum18); Player winner = new Player("제제", sum21); + Player tier = new Player("브라운", sum20); - Players players = new Players(List.of(loser1, loser2, winner)); + Players players = new Players(List.of(loser1, loser2, winner, tier)); Dealer dealer = new Dealer(CardDeck.generate(), sum20); Game game = new Game(dealer, players); // when - Map expected = Map.of(Result.WIN, 2, Result.LOSE, 1); + Map expected = Map.of(Result.WIN, 2, Result.LOSE, 1, Result.TIE, 1); // then Assertions.assertThat(game.getDealerResult()).isEqualTo(expected); diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index 366ff3dbdf..d1f965a859 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -66,8 +66,37 @@ void sumAce1(final Hands hands, final int expected) { Assertions.assertThat(result).isEqualTo(expected); } + @DisplayName("카드 합이 같고 카드 갯수가 같으면 무승부이다.") @Test - @DisplayName("카드 합이 21이하이면서 승리한다.") + void isTie() { + // given + final Hands sum1 = new Hands( + List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), + new Card(CardNumber.ACE, CardShape.SPADE))); + final Hands sum2 = new Hands( + List.of(new Card(CardNumber.FOUR, CardShape.SPADE), new Card(CardNumber.FIVE, CardShape.SPADE), + new Card(CardNumber.ACE, CardShape.SPADE))); + + // when && then + Assertions.assertThat(sum1.calculateResult(sum2)).isEqualTo(Result.TIE); + } + + @DisplayName("카드 합이 같은데 카드 갯수가 더 적으면 승리이다.") + @Test + void isWinBySize() { + // given + final Hands sum20Size2 = new Hands( + List.of(new Card(CardNumber.NINE, CardShape.SPADE), new Card(CardNumber.ACE, CardShape.SPADE))); + final Hands sum20Size3 = new Hands( + List.of(new Card(CardNumber.FOUR, CardShape.SPADE), new Card(CardNumber.FIVE, CardShape.SPADE), + new Card(CardNumber.ACE, CardShape.SPADE))); + + // when && then + Assertions.assertThat(sum20Size2.calculateResult(sum20Size3)).isEqualTo(Result.WIN); + } + + @Test + @DisplayName("카드 합이 21이하이면서 승리한다.") //TODO 이름 변경 void isWin() { //given final Hands sum20 = new Hands( @@ -75,10 +104,11 @@ void isWin() { new Card(CardNumber.ACE, CardShape.SPADE))); final Hands sum21 = new Hands( - List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(CardNumber.JACK, CardShape.HEART), + new Card(CardNumber.ACE, CardShape.SPADE))); //when && then - Assertions.assertThat(sum21.isWin(sum20)).isTrue(); + Assertions.assertThat(sum21.calculateResult(sum20)).isEqualTo(Result.WIN); } @@ -93,7 +123,7 @@ void isLose() { List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); //when && then - Assertions.assertThat(sum20.isWin(sum21)).isFalse(); + Assertions.assertThat(sum20.calculateResult(sum21)).isEqualTo(Result.LOSE); } @Test @@ -108,7 +138,7 @@ void isLoseWhenCardSumGreater21() { new Card(CardNumber.FIVE, CardShape.HEART))); //when && then - Assertions.assertThat(sum22.isWin(sum18)).isFalse(); + Assertions.assertThat(sum22.calculateResult(sum18)).isEqualTo(Result.LOSE); } From 7593784496bfe5900bed361416a3385499de105c Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 16:08:06 +0900 Subject: [PATCH 045/100] =?UTF-8?q?fix:=20=EB=94=9C=EB=9F=AC=EA=B0=80=20?= =?UTF-8?q?=EB=B2=84=EC=8A=A4=ED=8A=B8=EC=9D=BC=EB=95=8C=20=EC=B0=B8?= =?UTF-8?q?=EC=97=AC=EC=9E=90=EA=B0=80=20=EB=B2=84=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EA=B0=80=20=EC=95=84=EB=8B=88=EB=A9=B4=20WIN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Hands.java | 4 ++ src/test/java/domain/GameTest.java | 80 +++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index b6c0fc15b0..9b7e7274e8 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -37,6 +37,10 @@ public Result calculateResult(final Hands target) { return Result.WIN; } + + if (!this.isBust() && target.isBust()) { + return Result.WIN; + } return Result.LOSE; } diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index 0a5908cd3f..92135e2487 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -1,5 +1,22 @@ package domain; +import static domain.CardNumber.ACE; +import static domain.CardNumber.EIGHT; +import static domain.CardNumber.FIVE; +import static domain.CardNumber.FOUR; +import static domain.CardNumber.JACK; +import static domain.CardNumber.KING; +import static domain.CardNumber.QUEEN; +import static domain.CardNumber.SEVEN; +import static domain.CardNumber.TEN; +import static domain.CardNumber.THREE; +import static domain.CardNumber.TWO; +import static domain.CardShape.CLOVER; +import static domain.CardShape.DIAMOND; +import static domain.CardShape.HEART; +import static domain.CardShape.SPADE; +import static domain.Result.*; + import java.util.List; import java.util.Map; import org.assertj.core.api.Assertions; @@ -12,14 +29,14 @@ class GameTest { @DisplayName("참여자의 승패무를 판단한다.") void playerResult() { //given - Hands sum18 = new Hands(List.of(new Card(CardNumber.EIGHT, CardShape.CLOVER), - new Card(CardNumber.TEN, CardShape.DIAMOND))); // 18 + Hands sum18 = new Hands(List.of(new Card(EIGHT, CLOVER), + new Card(TEN, DIAMOND))); // 18 Hands sum21 = new Hands( - List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); Hands sum20 = new Hands( - List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), - new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), + new Card(ACE, SPADE))); Player loser = new Player("레디", sum18); Player winner = new Player("제제", sum21); @@ -31,7 +48,7 @@ void playerResult() { Game game = new Game(dealer, players); //when & then - Map expected = Map.of(loser, Result.LOSE, winner, Result.WIN, tier, Result.TIE); + Map expected = Map.of(loser, LOSE, winner, WIN, tier, TIE); Assertions.assertThat(game.getResult()).isEqualTo(expected); } @@ -39,15 +56,15 @@ void playerResult() { @Test void dealerResult() { // given - Hands sum18 = new Hands(List.of(new Card(CardNumber.EIGHT, CardShape.CLOVER), - new Card(CardNumber.TEN, CardShape.DIAMOND))); // 18 + Hands sum18 = new Hands(List.of(new Card(EIGHT, CLOVER), + new Card(TEN, DIAMOND))); // 18 Hands sum21 = new Hands( - List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); Hands sum20 = new Hands( - List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), - new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), + new Card(ACE, SPADE))); Player loser1 = new Player("레디", sum18); Player loser2 = new Player("피케이", sum18); @@ -60,9 +77,48 @@ void dealerResult() { Game game = new Game(dealer, players); // when - Map expected = Map.of(Result.WIN, 2, Result.LOSE, 1, Result.TIE, 1); + Map expected = Map.of(WIN, 2, LOSE, 1, TIE, 1); // then Assertions.assertThat(game.getDealerResult()).isEqualTo(expected); } + + @Test + @DisplayName("딜러가 버스트일때 참여자가 버스트가 아니면 WIN") + void all() { + //given + Hands dealerBustHands = new Hands( + List.of(new Card(EIGHT, DIAMOND), new Card(TWO, DIAMOND), new Card(TWO, DIAMOND), + new Card(JACK, CLOVER))); + Dealer bustDealer = new Dealer(CardDeck.generate(), dealerBustHands); + + Hands winner1PlayerHandsSum19 = new Hands( + List.of(new Card(FIVE, DIAMOND), new Card(THREE, CLOVER), new Card(SEVEN, HEART), + new Card(FOUR, DIAMOND)) + ); + + Player winner1 = new Player("레디", winner1PlayerHandsSum19); + + Hands winner2PlayerHandsSum20 = new Hands( + List.of(new Card(QUEEN, CLOVER), new Card(KING, SPADE)) + ); + Player winner2 = new Player("브라운", winner2PlayerHandsSum20); + + Hands loserPlayerHandsSum22 = new Hands( + List.of(new Card(SEVEN, CLOVER), new Card(QUEEN, HEART), new Card(FIVE, SPADE)) + ); + Player loser = new Player("제제", loserPlayerHandsSum22); + + Players players = new Players(List.of(winner1, winner2, loser)); + + //when + Game game = new Game(bustDealer, players); + + Map expectedPlayerResult = Map.of(winner1, WIN, winner2, WIN, loser, LOSE); + Map expectedDealerResult = Map.of(WIN, 1, LOSE, 2); + + //then + Assertions.assertThat(game.getResult()).isEqualTo(expectedPlayerResult); + Assertions.assertThat(game.getDealerResult()).isEqualTo(expectedDealerResult); + } } From cbee98a24105787e446ad72399b0d7c7cf378acd Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 16:14:49 +0900 Subject: [PATCH 046/100] =?UTF-8?q?feat:=20=EB=B8=94=EB=9E=99=EC=9E=AD=20?= =?UTF-8?q?=EC=8B=9C=20=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=ED=9B=84=20=EC=B9=B4=EB=93=9C=20=EB=8D=94=20=EB=B0=9B=EC=A7=80?= =?UTF-8?q?=20=EC=95=8A=EB=8A=94=20=EA=B8=B0=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 --- src/main/java/controller/BlackJackController.java | 5 +++++ src/main/java/domain/Hands.java | 4 ++++ src/main/java/domain/Player.java | 4 ++++ src/main/java/view/OutputView.java | 5 +++++ 4 files changed, 18 insertions(+) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 0fe2c5409b..591ab45169 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -65,6 +65,11 @@ private void deal(final Player player, final Dealer dealer) { break; } changed = true; + + if (player.isBlackJack()) { + outputView.printBlackJack(); + break; + } } } diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 9b7e7274e8..8a746c86f1 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -52,6 +52,10 @@ public boolean isBust() { return sum() > BLACK_JACK; } + public boolean isBlackJack() { + return sum() == BLACK_JACK; + } + private boolean hasAce() { return cards.stream() .anyMatch(Card::isAce); diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index efe87158c0..cece8a5702 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -79,4 +79,8 @@ public boolean equals(final Object target) { public int hashCode() { return Objects.hash(name); } + + public boolean isBlackJack() { + return hands.isBlackJack(); + } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index e070755107..28b6df72f3 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -74,4 +74,9 @@ public void printBustMessage() { System.out.println("BUST"); System.out.println(); } + + public void printBlackJack() { + System.out.println("BLACK JACK!!!"); + System.out.println(); + } } From 0909ae6b65e2808cc159169139cce16df985cdb8 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 16:30:33 +0900 Subject: [PATCH 047/100] =?UTF-8?q?docs:=20=EB=B8=94=EB=9E=99=EC=9E=AD=20?= =?UTF-8?q?=EA=B7=9C=EC=B9=99=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/README.md b/docs/README.md index 58b230e429..3c67b7b5ba 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,3 +1,24 @@ +# 블랙잭 규칙 + +## 참여자 + +**승** + +* (딜러 카드 합 < 참여자 카드 합) && 참여자가 버스트가 아닌 경우 +* (딜러 카드 합 == 참여자 카드 합) && (딜러 카드 수 > 참여자 카드 수) && 참여자가 버스트가 아닌 경우 +* 딜러가 버스트인 경우 && 참여자가 버스트가 아닌 경우 + +**무** + +* (딜러 카드 합 == 참여자 카드 합) && (딜러 카드 수 == 참여자 카드 수) && 참여자가 버스트가 아닌 경우 + +**패** + +* 참여자가 버스트인 경우 +* (딜러 카드 합 > 참여자 카드 합) + +
+ # 기능 요구 사항 * [x] 참여자 이름 입력 받는다. From b188bdf616addd6f85bf1b7251ff97e862b38d50 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 16:41:46 +0900 Subject: [PATCH 048/100] =?UTF-8?q?refactor:=20'CardNumber'=EB=A5=BC=20'Ra?= =?UTF-8?q?nk'=EB=A1=9C=20=EB=B3=80=EA=B2=BD,=20'CardShape'=EB=A5=BC=20'Sh?= =?UTF-8?q?ape'=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20card=20?= =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Card.java | 43 ---------- src/main/java/domain/CardDeck.java | 10 ++- src/main/java/domain/Dealer.java | 2 + src/main/java/domain/Hands.java | 2 + src/main/java/domain/Player.java | 2 + src/main/java/domain/card/Card.java | 43 ++++++++++ .../{CardNumber.java => card/Rank.java} | 10 +-- .../{CardShape.java => card/Shape.java} | 6 +- src/test/java/domain/GameTest.java | 32 ++++---- src/test/java/domain/HandsTest.java | 80 ++++++++++--------- src/test/java/domain/PlayersTest.java | 14 ++-- 11 files changed, 131 insertions(+), 113 deletions(-) delete mode 100644 src/main/java/domain/Card.java create mode 100644 src/main/java/domain/card/Card.java rename src/main/java/domain/{CardNumber.java => card/Rank.java} (74%) rename src/main/java/domain/{CardShape.java => card/Shape.java} (75%) diff --git a/src/main/java/domain/Card.java b/src/main/java/domain/Card.java deleted file mode 100644 index 4b9af07027..0000000000 --- a/src/main/java/domain/Card.java +++ /dev/null @@ -1,43 +0,0 @@ -package domain; - -import java.util.Objects; - -public class Card { - private final CardNumber cardNumber; - private final CardShape cardShape; - - public Card(final CardNumber cardNumber, final CardShape cardShape) { - this.cardNumber = cardNumber; - this.cardShape = cardShape; - } - - public int getCardNumber() { - return cardNumber.getValue(); - } - - public boolean isAce() { - return cardNumber.isAce(); - } - - @Override - public boolean equals(final Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - final Card card = (Card) o; - return cardNumber == card.cardNumber && cardShape == card.cardShape; - } - - @Override - public int hashCode() { - return Objects.hash(cardNumber, cardShape); - } - - @Override - public String toString() { - return cardNumber.getName() + cardShape.getName(); - } -} diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java index 16f520a970..de3506fe2b 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/CardDeck.java @@ -1,5 +1,9 @@ package domain; +import domain.card.Card; +import domain.card.Rank; +import domain.card.Shape; + import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; @@ -29,9 +33,9 @@ public static CardDeck generate() { private static List generateOneCardDeck() { //TODO 인덴트 줄이기 final List cards = new ArrayList<>(); - for (CardShape cardShape : CardShape.values()) { - for (CardNumber cardNumber : CardNumber.values()) { - cards.add(new Card(cardNumber, cardShape)); + for (Shape shape : Shape.values()) { + for (Rank rank : Rank.values()) { + cards.add(new Card(rank, shape)); } } diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 5680b903e1..4acbdc1c03 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -1,5 +1,7 @@ package domain; +import domain.card.Card; + import java.util.List; public class Dealer { diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 8a746c86f1..debda9826d 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -1,5 +1,7 @@ package domain; +import domain.card.Card; + import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index cece8a5702..ee3d23c5a5 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -1,5 +1,7 @@ package domain; +import domain.card.Card; + import java.util.List; import java.util.Objects; diff --git a/src/main/java/domain/card/Card.java b/src/main/java/domain/card/Card.java new file mode 100644 index 0000000000..1daf7dd284 --- /dev/null +++ b/src/main/java/domain/card/Card.java @@ -0,0 +1,43 @@ +package domain.card; + +import java.util.Objects; + +public class Card { + private final Rank rank; + private final Shape shape; + + public Card(final Rank rank, final Shape shape) { + this.rank = rank; + this.shape = shape; + } + + public int getCardNumber() { + return rank.getValue(); + } + + public boolean isAce() { + return rank.isAce(); + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final Card card = (Card) o; + return rank == card.rank && shape == card.shape; + } + + @Override + public int hashCode() { + return Objects.hash(rank, shape); + } + + @Override + public String toString() { + return rank.getName() + shape.getName(); + } +} diff --git a/src/main/java/domain/CardNumber.java b/src/main/java/domain/card/Rank.java similarity index 74% rename from src/main/java/domain/CardNumber.java rename to src/main/java/domain/card/Rank.java index e02011686e..aa92ef3465 100644 --- a/src/main/java/domain/CardNumber.java +++ b/src/main/java/domain/card/Rank.java @@ -1,9 +1,8 @@ -package domain; +package domain.card; -//TODO Number말고 더 좋은 클래스명짓기 -public enum CardNumber { +public enum Rank { - ACE("A", 1), // TODO: 1 혹은 11 + ACE("A", 1), TWO("2", 2), THREE("3", 3), FOUR("4", 4), @@ -17,11 +16,10 @@ public enum CardNumber { QUEEN("Q", 10), KING("K", 10); - private final String name; private final int value; - CardNumber(final String name, final int value) { + Rank(final String name, final int value) { this.name = name; this.value = value; } diff --git a/src/main/java/domain/CardShape.java b/src/main/java/domain/card/Shape.java similarity index 75% rename from src/main/java/domain/CardShape.java rename to src/main/java/domain/card/Shape.java index 8c1dd4e41e..400464c239 100644 --- a/src/main/java/domain/CardShape.java +++ b/src/main/java/domain/card/Shape.java @@ -1,6 +1,6 @@ -package domain; +package domain.card; -public enum CardShape { +public enum Shape { SPADE("스페이드"), HEART("하트"), CLOVER("클로버"), @@ -8,7 +8,7 @@ public enum CardShape { private final String name; - CardShape(final String name) { + Shape(final String name) { this.name = name; } diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index 92135e2487..55bb1430be 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -1,24 +1,26 @@ package domain; -import static domain.CardNumber.ACE; -import static domain.CardNumber.EIGHT; -import static domain.CardNumber.FIVE; -import static domain.CardNumber.FOUR; -import static domain.CardNumber.JACK; -import static domain.CardNumber.KING; -import static domain.CardNumber.QUEEN; -import static domain.CardNumber.SEVEN; -import static domain.CardNumber.TEN; -import static domain.CardNumber.THREE; -import static domain.CardNumber.TWO; -import static domain.CardShape.CLOVER; -import static domain.CardShape.DIAMOND; -import static domain.CardShape.HEART; -import static domain.CardShape.SPADE; +import static domain.card.Rank.ACE; +import static domain.card.Rank.EIGHT; +import static domain.card.Rank.FIVE; +import static domain.card.Rank.FOUR; +import static domain.card.Rank.JACK; +import static domain.card.Rank.KING; +import static domain.card.Rank.QUEEN; +import static domain.card.Rank.SEVEN; +import static domain.card.Rank.TEN; +import static domain.card.Rank.THREE; +import static domain.card.Rank.TWO; +import static domain.card.Shape.CLOVER; +import static domain.card.Shape.DIAMOND; +import static domain.card.Shape.HEART; +import static domain.card.Shape.SPADE; import static domain.Result.*; import java.util.List; import java.util.Map; + +import domain.card.Card; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index d1f965a859..a550484638 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -2,6 +2,10 @@ import java.util.List; import java.util.stream.Stream; + +import domain.card.Card; +import domain.card.Rank; +import domain.card.Shape; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -25,7 +29,7 @@ void addCard() { final Hands hands = Hands.createEmptyPacket(); //when - hands.add(new Card(CardNumber.EIGHT, CardShape.CLOVER)); + hands.add(new Card(Rank.EIGHT, Shape.CLOVER)); //then Assertions.assertThat(hands.size()).isEqualTo(1); @@ -71,11 +75,11 @@ void sumAce1(final Hands hands, final int expected) { void isTie() { // given final Hands sum1 = new Hands( - List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), - new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(Rank.SEVEN, Shape.SPADE), new Card(Rank.TWO, Shape.SPADE), + new Card(Rank.ACE, Shape.SPADE))); final Hands sum2 = new Hands( - List.of(new Card(CardNumber.FOUR, CardShape.SPADE), new Card(CardNumber.FIVE, CardShape.SPADE), - new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(Rank.FOUR, Shape.SPADE), new Card(Rank.FIVE, Shape.SPADE), + new Card(Rank.ACE, Shape.SPADE))); // when && then Assertions.assertThat(sum1.calculateResult(sum2)).isEqualTo(Result.TIE); @@ -86,10 +90,10 @@ void isTie() { void isWinBySize() { // given final Hands sum20Size2 = new Hands( - List.of(new Card(CardNumber.NINE, CardShape.SPADE), new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(Rank.NINE, Shape.SPADE), new Card(Rank.ACE, Shape.SPADE))); final Hands sum20Size3 = new Hands( - List.of(new Card(CardNumber.FOUR, CardShape.SPADE), new Card(CardNumber.FIVE, CardShape.SPADE), - new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(Rank.FOUR, Shape.SPADE), new Card(Rank.FIVE, Shape.SPADE), + new Card(Rank.ACE, Shape.SPADE))); // when && then Assertions.assertThat(sum20Size2.calculateResult(sum20Size3)).isEqualTo(Result.WIN); @@ -100,12 +104,12 @@ void isWinBySize() { void isWin() { //given final Hands sum20 = new Hands( - List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), - new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(Rank.SEVEN, Shape.SPADE), new Card(Rank.TWO, Shape.SPADE), + new Card(Rank.ACE, Shape.SPADE))); final Hands sum21 = new Hands( - List.of(new Card(CardNumber.JACK, CardShape.HEART), - new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(Rank.JACK, Shape.HEART), + new Card(Rank.ACE, Shape.SPADE))); //when && then Assertions.assertThat(sum21.calculateResult(sum20)).isEqualTo(Result.WIN); @@ -116,11 +120,11 @@ void isWin() { @DisplayName("카드 합이 21이하이면서 패배한다.") void isLose() { final Hands sum20 = new Hands( - List.of(new Card(CardNumber.SEVEN, CardShape.SPADE), new Card(CardNumber.TWO, CardShape.SPADE), - new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(Rank.SEVEN, Shape.SPADE), new Card(Rank.TWO, Shape.SPADE), + new Card(Rank.ACE, Shape.SPADE))); final Hands sum21 = new Hands( - List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.ACE, Shape.SPADE))); //when && then Assertions.assertThat(sum20.calculateResult(sum21)).isEqualTo(Result.LOSE); @@ -131,11 +135,11 @@ void isLose() { void isLoseWhenCardSumGreater21() { //given final Hands sum18 = new Hands( - List.of(new Card(CardNumber.SEVEN, CardShape.HEART), new Card(CardNumber.ACE, CardShape.SPADE))); + List.of(new Card(Rank.SEVEN, Shape.HEART), new Card(Rank.ACE, Shape.SPADE))); final Hands sum22 = new Hands( - List.of(new Card(CardNumber.NINE, CardShape.HEART), new Card(CardNumber.EIGHT, CardShape.SPADE), - new Card(CardNumber.FIVE, CardShape.HEART))); + List.of(new Card(Rank.NINE, Shape.HEART), new Card(Rank.EIGHT, Shape.SPADE), + new Card(Rank.FIVE, Shape.HEART))); //when && then Assertions.assertThat(sum22.calculateResult(sum18)).isEqualTo(Result.LOSE); @@ -144,41 +148,41 @@ void isLoseWhenCardSumGreater21() { static Stream sumParameterProvider() { return Stream.of( - Arguments.of(new Hands(List.of(new Card(CardNumber.TWO, CardShape.HEART), - new Card(CardNumber.EIGHT, CardShape.SPADE), - new Card(CardNumber.JACK, CardShape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(Rank.TWO, Shape.HEART), + new Card(Rank.EIGHT, Shape.SPADE), + new Card(Rank.JACK, Shape.CLOVER))), 20), - Arguments.of(new Hands(List.of(new Card(CardNumber.THREE, CardShape.DIAMOND), - new Card(CardNumber.NINE, CardShape.CLOVER), - new Card(CardNumber.NINE, CardShape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(Rank.THREE, Shape.DIAMOND), + new Card(Rank.NINE, Shape.CLOVER), + new Card(Rank.NINE, Shape.CLOVER))), 21) ); } static Stream sumAce11ParameterProvider() { return Stream.of( - Arguments.of(new Hands(List.of(new Card(CardNumber.ACE, CardShape.HEART), - new Card(CardNumber.EIGHT, CardShape.SPADE), - new Card(CardNumber.TWO, CardShape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(Rank.ACE, Shape.HEART), + new Card(Rank.EIGHT, Shape.SPADE), + new Card(Rank.TWO, Shape.CLOVER))), 21), - Arguments.of(new Hands(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), - new Card(CardNumber.TWO, CardShape.CLOVER), - new Card(CardNumber.FOUR, CardShape.CLOVER), - new Card(CardNumber.TWO, CardShape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(Rank.ACE, Shape.DIAMOND), + new Card(Rank.TWO, Shape.CLOVER), + new Card(Rank.FOUR, Shape.CLOVER), + new Card(Rank.TWO, Shape.CLOVER))), 19) ); } static Stream sumAce1ParameterProvider() { return Stream.of( - Arguments.of(new Hands(List.of(new Card(CardNumber.ACE, CardShape.HEART), - new Card(CardNumber.NINE, CardShape.SPADE), - new Card(CardNumber.NINE, CardShape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(Rank.ACE, Shape.HEART), + new Card(Rank.NINE, Shape.SPADE), + new Card(Rank.NINE, Shape.CLOVER))), 19), - Arguments.of(new Hands(List.of(new Card(CardNumber.ACE, CardShape.DIAMOND), - new Card(CardNumber.EIGHT, CardShape.CLOVER), - new Card(CardNumber.FIVE, CardShape.CLOVER), - new Card(CardNumber.SIX, CardShape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(Rank.ACE, Shape.DIAMOND), + new Card(Rank.EIGHT, Shape.CLOVER), + new Card(Rank.FIVE, Shape.CLOVER), + new Card(Rank.SIX, Shape.CLOVER))), 20) ); } diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index 5d11bca012..a6af634a6a 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -2,6 +2,10 @@ import java.util.List; import java.util.stream.Stream; + +import domain.card.Card; +import domain.card.Rank; +import domain.card.Shape; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -45,11 +49,11 @@ void invalidPlayersSize(final List names) { void isAllBustFalse() { //given Hands bustHands = new Hands( - List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.TEN, CardShape.SPADE), - new Card(CardNumber.TEN, CardShape.HEART))); + List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE), + new Card(Rank.TEN, Shape.HEART))); Hands noBustHands = new Hands( - List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.TEN, CardShape.SPADE))); + List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE))); Player player1 = new Player("레디", bustHands); Player player2 = new Player("제제", noBustHands); @@ -64,8 +68,8 @@ void isAllBustFalse() { void isAllBustTrue() { //given Hands bustHands = new Hands( - List.of(new Card(CardNumber.JACK, CardShape.HEART), new Card(CardNumber.TEN, CardShape.SPADE), - new Card(CardNumber.TEN, CardShape.HEART))); + List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE), + new Card(Rank.TEN, Shape.HEART))); Player player1 = new Player("레디", bustHands); Player player2 = new Player("제제", bustHands); From 62493d6617f89526e2ac6d85cfd68c83b512002c Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 16:49:16 +0900 Subject: [PATCH 049/100] =?UTF-8?q?refactor:=20players=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EB=A9=94=EC=84=9C=EB=93=9C=EC=9D=98=20=ED=8C=8C?= =?UTF-8?q?=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EB=B0=8F=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/BlackJackController.java | 2 +- src/main/java/domain/Hands.java | 3 +-- src/main/java/domain/Players.java | 16 ++++++++++------ src/main/java/view/InputView.java | 6 +++--- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 591ab45169..0fccd94510 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -22,7 +22,7 @@ public BlackJackController(final InputView inputView, final OutputView outputVie } public void run() { - final Players players = Players.from(inputView.readPlayerNames()); + final Players players = Players.from(inputView.readNames()); final CardDeck cardDeck = CardDeck.generate(); final Dealer dealer = new Dealer(cardDeck); final Game game = new Game(dealer, players); diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index debda9826d..1b34943e89 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -1,11 +1,10 @@ package domain; import domain.card.Card; - import java.util.ArrayList; import java.util.List; -public class Hands { //TODO: 더 좋은 이름이 있다면 날 설득해줘 +public class Hands { private static final int BLACK_JACK = 21; diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index e4a4c02fc6..9ee6e0f5f4 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -16,12 +16,9 @@ public Players(final List names) { } public static Players from(final List names) { - List players = names.stream() - .map(String::trim) - .map(name -> new Player(name, Hands.createEmptyPacket())) - .toList(); - validate(players); - return new Players(players); //TODO 이름바꾸기 + List playerNames = mapToPlayers(names); + validate(playerNames); + return new Players(playerNames); } public void forEach(Consumer action) { @@ -33,6 +30,13 @@ public boolean isAllBust() { .allMatch(Player::isBust); } + private static List mapToPlayers(final List names) { + return names.stream() + .map(String::trim) + .map(name -> new Player(name, Hands.createEmptyPacket())) + .toList(); + } + private static void validate(final List players) { validateSize(players); validateDuplicate(players); diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 8c9d34360e..5beb67c19c 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -13,14 +13,14 @@ public InputView(final Scanner scanner) { this.scanner = scanner; } - public List readPlayerNames() { + public List readNames() { System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); String rawNames = scanner.nextLine().trim(); validateBlank(rawNames); validateSeparators(rawNames); - List players = List.of(rawNames.split(NAME_SEPARATOR)); + List names = List.of(rawNames.split(NAME_SEPARATOR)); System.out.println(); - return players; + return names; } public String readAnswer(String name) { From 4df9b2633013928d49900392afc5e194cd2f8357 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 17:01:52 +0900 Subject: [PATCH 050/100] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20=EB=B0=8F=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/BlackJackController.java | 6 +++--- src/main/java/domain/Game.java | 21 ++++++++----------- src/main/java/domain/Player.java | 6 ++---- src/main/java/domain/Players.java | 2 +- src/main/java/domain/Result.java | 2 +- src/main/java/dto/PlayersDto.java | 2 +- src/test/java/domain/DealerTest.java | 2 +- src/test/java/domain/GameTest.java | 4 ++-- 8 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 0fccd94510..98ecee4c77 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -30,13 +30,13 @@ public void run() { dealer.startDeal(players); outputView.printStartDeal(DealerHandsDto.from(dealer), PlayersDto.from(players)); - for (Player player : players.getNames()) { + for (Player player : players.getPlayers()) { deal(player, dealer); } if (players.isAllBust()) { outputView.printHandsResult(PlayersDto.from(dealer, players)); - outputView.printGameResult(game.getDealerResult(), game.getResult()); + outputView.printGameResult(game.getDealerResult(), game.getPlayersResult()); return; } @@ -46,7 +46,7 @@ public void run() { } outputView.printHandsResult(PlayersDto.from(dealer, players)); - outputView.printGameResult(game.getDealerResult(), game.getResult()); + outputView.printGameResult(game.getDealerResult(), game.getPlayersResult()); } private void deal(final Player player, final Dealer dealer) { diff --git a/src/main/java/domain/Game.java b/src/main/java/domain/Game.java index 35ac065753..e2e136e7ca 100644 --- a/src/main/java/domain/Game.java +++ b/src/main/java/domain/Game.java @@ -5,7 +5,7 @@ import java.util.Map; public class Game { - + //TODO Map 필드 고려 private final Dealer dealer; private final Players players; @@ -14,26 +14,23 @@ public Game(final Dealer dealer, final Players players) { this.players = players; } - public Map getResult() { - return calculate(); + public Map getPlayersResult() { + final Map playerResult = new LinkedHashMap<>(); + + for (Player player : players.getPlayers()) { //TODO getPlayers() 메서드명 고려 + playerResult.put(player, player.calculateResult(dealer)); + } + return playerResult; } public Map getDealerResult() { Map dealerResult = new EnumMap<>(Result.class); - for (Result value : getResult().values()) { + for (Result value : getPlayersResult().values()) { Result reversed = value.reverse(); Integer orDefault = dealerResult.getOrDefault(reversed, 0); dealerResult.put(reversed, orDefault + 1); } return dealerResult; } - - private Map calculate() { - final Map map = new LinkedHashMap<>(); - for (Player player : players.getNames()) { // TODO 인덴트 줄이기 - map.put(player, player.isWin(dealer)); - } - return map; - } } diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index ee3d23c5a5..b9262a833f 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -16,15 +16,13 @@ public Player(final String name, final Hands hands) { } public void add(final Card card) { - hands.add(card); + this.hands.add(card); } - public Result isWin(final Dealer dealer) { + public Result calculateResult(final Dealer dealer) { return this.hands.calculateResult(dealer.getHands()); } - //TODO 메서드 명 변경 - private void validate(final String name) { validateNull(name); validateBlank(name); diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index 9ee6e0f5f4..423c907e7d 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -54,7 +54,7 @@ private static void validateDuplicate(final List players) { } } - public List getNames() { + public List getPlayers() { return names; } } diff --git a/src/main/java/domain/Result.java b/src/main/java/domain/Result.java index 8cf7ce2d29..425fb48112 100644 --- a/src/main/java/domain/Result.java +++ b/src/main/java/domain/Result.java @@ -1,6 +1,6 @@ package domain; -public enum Result { //TODO 무승부 +public enum Result { WIN("승"), LOSE("패"), diff --git a/src/main/java/dto/PlayersDto.java b/src/main/java/dto/PlayersDto.java index 5b8c34a72b..0f5e9f3748 100644 --- a/src/main/java/dto/PlayersDto.java +++ b/src/main/java/dto/PlayersDto.java @@ -23,7 +23,7 @@ public static PlayersDto from(final Dealer dealer, final Players players) { // T public static PlayersDto from(final Players players) { List result = new ArrayList<>(); - for (Player player : players.getNames()) { + for (Player player : players.getPlayers()) { result.add(PlayerDto.from(player)); } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 4b103ab947..6a70e78eb4 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -19,7 +19,7 @@ void dealCards() { dealer.startDeal(players); //then - Assertions.assertThat(players.getNames()).allMatch(player -> player.getPacketSize() == 2); + Assertions.assertThat(players.getPlayers()).allMatch(player -> player.getPacketSize() == 2); } @Test diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index 55bb1430be..bde2e36b06 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -51,7 +51,7 @@ void playerResult() { //when & then Map expected = Map.of(loser, LOSE, winner, WIN, tier, TIE); - Assertions.assertThat(game.getResult()).isEqualTo(expected); + Assertions.assertThat(game.getPlayersResult()).isEqualTo(expected); } @DisplayName("딜러의 승패무를 판단한다.") @@ -120,7 +120,7 @@ void all() { Map expectedDealerResult = Map.of(WIN, 1, LOSE, 2); //then - Assertions.assertThat(game.getResult()).isEqualTo(expectedPlayerResult); + Assertions.assertThat(game.getPlayersResult()).isEqualTo(expectedPlayerResult); Assertions.assertThat(game.getDealerResult()).isEqualTo(expectedDealerResult); } } From 7b0d3d77b3d21f52643c39ef2a587d6cee31d09b Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 17:10:46 +0900 Subject: [PATCH 051/100] =?UTF-8?q?refactor:=20Player=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EB=B0=8F=20?= =?UTF-8?q?=EC=88=9C=EC=84=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Player.java | 57 ++++++++++++++-------------- src/main/java/dto/PlayerDto.java | 2 +- src/test/java/domain/DealerTest.java | 6 +-- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java index b9262a833f..88c1e97ef5 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Player.java @@ -16,52 +16,55 @@ public Player(final String name, final Hands hands) { } public void add(final Card card) { - this.hands.add(card); + hands.add(card); } public Result calculateResult(final Dealer dealer) { - return this.hands.calculateResult(dealer.getHands()); + return hands.calculateResult(dealer.getHands()); } - private void validate(final String name) { - validateNull(name); - validateBlank(name); + //TODO 메서드가 조잡해요 + public boolean isBust() { + return hands.isBust(); } - private void validateNull(final String name) { - if (name == null) { - throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 이름입니다."); - } + public int handsSum() { + return hands.sum(); } - private void validateBlank(final String name) { - if (name.isBlank()) { - throw new IllegalArgumentException("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); - } + public int handsSize() { + return hands.size(); } - //TODO: 메서드 handsSize 수정 - public int getPacketSize() { - return hands.size(); + public boolean isBlackJack() { + return hands.isBlackJack(); + } + + public List getCardNames() { + return hands.getCards().stream() + .map(Card::toString) + .toList(); } public String getName() { return name; } - public List getCards() { - return hands.getCards() - .stream() - .map(Card::toString) - .toList(); + private void validate(final String name) { + validateNull(name); + validateBlank(name); } - public boolean isBust() { - return hands.isBust(); + private void validateNull(final String name) { + if (name == null) { + throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 이름입니다."); + } } - public int getTotalSum() { - return hands.sum(); + private void validateBlank(final String name) { + if (name.isBlank()) { + throw new IllegalArgumentException("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); + } } @Override @@ -79,8 +82,4 @@ public boolean equals(final Object target) { public int hashCode() { return Objects.hash(name); } - - public boolean isBlackJack() { - return hands.isBlackJack(); - } } diff --git a/src/main/java/dto/PlayerDto.java b/src/main/java/dto/PlayerDto.java index fda6d2c405..9e03ea5f9d 100644 --- a/src/main/java/dto/PlayerDto.java +++ b/src/main/java/dto/PlayerDto.java @@ -17,7 +17,7 @@ public PlayerDto(final String name, final List cards, final int totalSum } public static PlayerDto from(final Player player) { - return new PlayerDto(player.getName(), player.getCards(), player.getTotalSum()); + return new PlayerDto(player.getName(), player.getCardNames(), player.handsSum()); } public static PlayerDto from(final Dealer dealer) { diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 6a70e78eb4..000af54716 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -19,7 +19,7 @@ void dealCards() { dealer.startDeal(players); //then - Assertions.assertThat(players.getPlayers()).allMatch(player -> player.getPacketSize() == 2); + Assertions.assertThat(players.getPlayers()).allMatch(player -> player.handsSize() == 2); } @Test @@ -40,7 +40,7 @@ void addOneCard() { dealer.deal(zeze, Answer.STAY); //then - Assertions.assertThat(redddy.getPacketSize()).isEqualTo(3); - Assertions.assertThat(zeze.getPacketSize()).isEqualTo(2); + Assertions.assertThat(redddy.handsSize()).isEqualTo(3); + Assertions.assertThat(zeze.handsSize()).isEqualTo(2); } } From 75d216bee31b14b98bd458e861d9ff2b4d7d6075 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 17:23:53 +0900 Subject: [PATCH 052/100] =?UTF-8?q?refactor:=20CardDeck=EC=9D=98=20generat?= =?UTF-8?q?e=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=8A=A4=ED=8A=B8=EB=A6=BC=20?= =?UTF-8?q?=ED=99=9C=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/CardDeck.java | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java index de3506fe2b..135729eacb 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/CardDeck.java @@ -3,9 +3,9 @@ import domain.card.Card; import domain.card.Rank; import domain.card.Shape; - import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.List; @@ -19,27 +19,29 @@ public CardDeck(final Deque cards) { this.cards = cards; } + //TODO 모르는 사람이 봐도 이해할 수 있도록 수정해보기 + // DECK_SIZE 파라미터로 받아오는 거 고려 public static CardDeck generate() { - final List result = new ArrayList<>(); + final List deck = new ArrayList<>(); for (int i = 0; i < DECK_SIZE; i++) { - result.addAll(generateOneCardDeck()); + deck.addAll(generateOneCardDeck()); } - Collections.shuffle(result); - return new CardDeck(new ArrayDeque<>(result)); + Collections.shuffle(deck); + return new CardDeck(new ArrayDeque<>(deck)); } - private static List generateOneCardDeck() { //TODO 인덴트 줄이기 - final List cards = new ArrayList<>(); - - for (Shape shape : Shape.values()) { - for (Rank rank : Rank.values()) { - cards.add(new Card(rank, shape)); - } - } + private static List generateOneCardDeck() { + return Arrays.stream(Shape.values()) + .flatMap(shape -> matching(shape).stream()) + .toList(); + } - return cards; + private static List matching(final Shape shape) { + return Arrays.stream(Rank.values()) + .map(rank -> new Card(rank, shape)) + .toList(); } public Card pop() { From 72b3f459e96edf545a4abfd87202892e859b97a1 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Thu, 7 Mar 2024 17:26:10 +0900 Subject: [PATCH 053/100] =?UTF-8?q?refactor:=20Dealer=20=EC=B4=88=EA=B8=B0?= =?UTF-8?q?=20=EC=B9=B4=EB=93=9C=20=EB=84=98=EB=B2=84=20=EC=83=81=EC=88=98?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Dealer.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 4acbdc1c03..6cba258813 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -1,13 +1,11 @@ package domain; import domain.card.Card; - import java.util.List; public class Dealer { private static final String NAME = "딜러"; - - private static final int INIT_CARD_NUMBER = 2; //TODO 이름 생각해보기 + private static final int INIT_HANDS_SIZE = 2; private final CardDeck cardDeck; private final Hands hands; @@ -23,7 +21,7 @@ public Dealer(final CardDeck cardDeck, final Hands hands) { } public void startDeal(final Players players) { - for (int i = 0; i < INIT_CARD_NUMBER; i++) { + for (int i = 0; i < INIT_HANDS_SIZE; i++) { players.forEach(player -> player.add(cardDeck.pop())); hands.add(cardDeck.pop()); } From 62382484786a1f22c253794aa39558f601f8ae86 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Thu, 7 Mar 2024 17:55:57 +0900 Subject: [PATCH 054/100] =?UTF-8?q?refactor:=20Dealer=EA=B0=80=20Participa?= =?UTF-8?q?nt=20=EC=83=81=EC=86=8D=EB=B0=9B=EB=8F=84=EB=A1=9D=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/controller/BlackJackController.java | 18 +++++----- src/main/java/domain/Dealer.java | 28 +++++---------- src/main/java/domain/Game.java | 8 ++--- .../domain/{Player.java => Participant.java} | 12 ++++--- src/main/java/domain/Players.java | 34 +++++++++---------- src/main/java/dto/DealerHandsDto.java | 5 +-- src/main/java/dto/PlayerDto.java | 8 ++--- src/main/java/dto/PlayersDto.java | 6 ++-- src/main/java/view/OutputView.java | 7 ++-- src/test/java/domain/DealerTest.java | 4 +-- src/test/java/domain/GameTest.java | 24 ++++++------- .../{PlayerTest.java => ParticipantTest.java} | 6 ++-- src/test/java/domain/PlayersTest.java | 16 ++++----- 13 files changed, 85 insertions(+), 91 deletions(-) rename src/main/java/domain/{Player.java => Participant.java} (86%) rename src/test/java/domain/{PlayerTest.java => ParticipantTest.java} (77%) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 98ecee4c77..d0682028a4 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -4,7 +4,7 @@ import domain.CardDeck; import domain.Dealer; import domain.Game; -import domain.Player; +import domain.Participant; import domain.Players; import dto.DealerHandsDto; import dto.PlayerDto; @@ -30,8 +30,8 @@ public void run() { dealer.startDeal(players); outputView.printStartDeal(DealerHandsDto.from(dealer), PlayersDto.from(players)); - for (Player player : players.getPlayers()) { - deal(player, dealer); + for (Participant participant : players.getPlayers()) { + deal(participant, dealer); } if (players.isAllBust()) { @@ -49,24 +49,24 @@ public void run() { outputView.printGameResult(game.getDealerResult(), game.getPlayersResult()); } - private void deal(final Player player, final Dealer dealer) { + private void deal(final Participant participant, final Dealer dealer) { boolean changed = false; Answer answer = Answer.HIT; while (Answer.HIT.equals(answer)) { - answer = Answer.from(inputView.readAnswer(player.getName())); - dealer.deal(player, answer); + answer = Answer.from(inputView.readAnswer(participant.getName())); + dealer.deal(participant, answer); if (handsChanged(changed, answer)) { - outputView.printHands(PlayerDto.from(player)); + outputView.printHands(PlayerDto.from(participant)); } - if (player.isBust()) { + if (participant.isBust()) { outputView.printBustMessage(); break; } changed = true; - if (player.isBlackJack()) { + if (participant.isBlackJack()) { outputView.printBlackJack(); break; } diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/Dealer.java index 6cba258813..2f51110b1f 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/Dealer.java @@ -1,55 +1,45 @@ package domain; -import domain.card.Card; -import java.util.List; +public class Dealer extends Participant { -public class Dealer { private static final String NAME = "딜러"; private static final int INIT_HANDS_SIZE = 2; private final CardDeck cardDeck; - private final Hands hands; public Dealer(final CardDeck cardDeck) { + super(NAME, Hands.createEmptyPacket()); this.cardDeck = cardDeck; - this.hands = Hands.createEmptyPacket(); } public Dealer(final CardDeck cardDeck, final Hands hands) { + super(NAME, hands); this.cardDeck = cardDeck; - this.hands = hands; } public void startDeal(final Players players) { for (int i = 0; i < INIT_HANDS_SIZE; i++) { players.forEach(player -> player.add(cardDeck.pop())); - hands.add(cardDeck.pop()); + super.add(cardDeck.pop()); } } - public void deal(final Player player, final Answer answer) { + public void deal(final Participant participant, final Answer answer) { if (Answer.HIT.equals(answer)) { - player.add(cardDeck.pop()); + participant.add(cardDeck.pop()); } } public void deal() { - hands.add(cardDeck.pop()); - } - - public List getCards() { - return hands.getCards() - .stream() - .map(Card::toString) - .toList(); + super.add(cardDeck.pop()); } public int getTotalCardSum() { - return hands.sum(); + return super.handsSum(); } public Hands getHands() { - return hands; + return super.getHands(); } public String getName() { diff --git a/src/main/java/domain/Game.java b/src/main/java/domain/Game.java index e2e136e7ca..dd4d5ba366 100644 --- a/src/main/java/domain/Game.java +++ b/src/main/java/domain/Game.java @@ -14,11 +14,11 @@ public Game(final Dealer dealer, final Players players) { this.players = players; } - public Map getPlayersResult() { - final Map playerResult = new LinkedHashMap<>(); + public Map getPlayersResult() { + final Map playerResult = new LinkedHashMap<>(); - for (Player player : players.getPlayers()) { //TODO getPlayers() 메서드명 고려 - playerResult.put(player, player.calculateResult(dealer)); + for (Participant participant : players.getPlayers()) { //TODO getPlayers() 메서드명 고려 + playerResult.put(participant, participant.calculateResult(dealer)); } return playerResult; } diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Participant.java similarity index 86% rename from src/main/java/domain/Player.java rename to src/main/java/domain/Participant.java index 88c1e97ef5..318175358e 100644 --- a/src/main/java/domain/Player.java +++ b/src/main/java/domain/Participant.java @@ -5,11 +5,11 @@ import java.util.List; import java.util.Objects; -public class Player { +public class Participant { private final String name; private final Hands hands; - public Player(final String name, final Hands hands) { + public Participant(final String name, final Hands hands) { validate(name); this.name = name; this.hands = hands; @@ -50,6 +50,10 @@ public String getName() { return name; } + public Hands getHands() { + return hands; + } + private void validate(final String name) { validateNull(name); validateBlank(name); @@ -72,10 +76,10 @@ public boolean equals(final Object target) { if (this == target) { return true; } - if (!(target instanceof Player player)) { + if (!(target instanceof Participant participant)) { return true; } - return Objects.equals(name, player.name); + return Objects.equals(name, participant.name); } @Override diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java index 423c907e7d..d890ab12f0 100644 --- a/src/main/java/domain/Players.java +++ b/src/main/java/domain/Players.java @@ -9,52 +9,52 @@ public class Players { private static final int MIN_SIZE = 2; private static final int MAX_SIZE = 8; - private final List names; + private final List names; - public Players(final List names) { + public Players(final List names) { this.names = names; } public static Players from(final List names) { - List playerNames = mapToPlayers(names); - validate(playerNames); - return new Players(playerNames); + List participantNames = mapToPlayers(names); + validate(participantNames); + return new Players(participantNames); } - public void forEach(Consumer action) { + public void forEach(Consumer action) { names.forEach(action); } public boolean isAllBust() { return names.stream() - .allMatch(Player::isBust); + .allMatch(Participant::isBust); } - private static List mapToPlayers(final List names) { + private static List mapToPlayers(final List names) { return names.stream() .map(String::trim) - .map(name -> new Player(name, Hands.createEmptyPacket())) + .map(name -> new Participant(name, Hands.createEmptyPacket())) .toList(); } - private static void validate(final List players) { - validateSize(players); - validateDuplicate(players); + private static void validate(final List participants) { + validateSize(participants); + validateDuplicate(participants); } - private static void validateSize(final List players) { - if (players.size() < MIN_SIZE || MAX_SIZE < players.size()) { + private static void validateSize(final List participants) { + if (participants.size() < MIN_SIZE || MAX_SIZE < participants.size()) { throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 수입니다."); } } - private static void validateDuplicate(final List players) { - if (players.size() != Set.copyOf(players).size()) { + private static void validateDuplicate(final List participants) { + if (participants.size() != Set.copyOf(participants).size()) { throw new IllegalArgumentException("[ERROR] 참여자 이름은 중복될 수 없습니다."); } } - public List getPlayers() { + public List getPlayers() { return names; } } diff --git a/src/main/java/dto/DealerHandsDto.java b/src/main/java/dto/DealerHandsDto.java index 04fc88fce7..bbf7d12a09 100644 --- a/src/main/java/dto/DealerHandsDto.java +++ b/src/main/java/dto/DealerHandsDto.java @@ -1,6 +1,7 @@ package dto; import domain.Dealer; +import domain.Participant; import java.util.List; @@ -11,8 +12,8 @@ private DealerHandsDto(final String displayedCard) { this.displayedCard = displayedCard; } - public static DealerHandsDto from(final Dealer dealer) { - List cards = dealer.getCards(); + public static DealerHandsDto from(final Participant dealer) { + List cards = dealer.getCardNames(); return new DealerHandsDto(cards.get(0)); } diff --git a/src/main/java/dto/PlayerDto.java b/src/main/java/dto/PlayerDto.java index 9e03ea5f9d..083c819ac4 100644 --- a/src/main/java/dto/PlayerDto.java +++ b/src/main/java/dto/PlayerDto.java @@ -1,7 +1,7 @@ package dto; import domain.Dealer; -import domain.Player; +import domain.Participant; import java.util.List; @@ -16,12 +16,12 @@ public PlayerDto(final String name, final List cards, final int totalSum this.totalSum = totalSum; } - public static PlayerDto from(final Player player) { - return new PlayerDto(player.getName(), player.getCardNames(), player.handsSum()); + public static PlayerDto from(final Participant participant) { + return new PlayerDto(participant.getName(), participant.getCardNames(), participant.handsSum()); } public static PlayerDto from(final Dealer dealer) { - return new PlayerDto(dealer.getName(), dealer.getCards(), dealer.getTotalCardSum()); + return new PlayerDto(dealer.getName(), dealer.getCardNames(), dealer.getTotalCardSum()); } public String getName() { diff --git a/src/main/java/dto/PlayersDto.java b/src/main/java/dto/PlayersDto.java index 0f5e9f3748..23e9b3c323 100644 --- a/src/main/java/dto/PlayersDto.java +++ b/src/main/java/dto/PlayersDto.java @@ -1,7 +1,7 @@ package dto; import domain.Dealer; -import domain.Player; +import domain.Participant; import domain.Players; import java.util.ArrayList; @@ -23,8 +23,8 @@ public static PlayersDto from(final Dealer dealer, final Players players) { // T public static PlayersDto from(final Players players) { List result = new ArrayList<>(); - for (Player player : players.getPlayers()) { - result.add(PlayerDto.from(player)); + for (Participant participant : players.getPlayers()) { + result.add(PlayerDto.from(participant)); } return new PlayersDto(result); diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 28b6df72f3..4ad159072e 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,7 +1,6 @@ package view; -import domain.Dealer; -import domain.Player; +import domain.Participant; import domain.Result; import dto.DealerHandsDto; import dto.PlayerDto; @@ -47,11 +46,11 @@ public void printHandsResult(final PlayersDto playersDto) { } } - public void printGameResult(final Map dealerResult, final Map playerResult) { + public void printGameResult(final Map dealerResult, final Map playerResult) { System.out.println("## 최종결과"); System.out.printf(RESULT_FORM, "딜러", format(dealerResult)); System.out.println(); - for (Map.Entry entry : playerResult.entrySet()) { + for (Map.Entry entry : playerResult.entrySet()) { System.out.printf(RESULT_FORM, entry.getKey().getName(), entry.getValue().getValue()); System.out.println(); } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 000af54716..2c956f97ed 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -26,8 +26,8 @@ void dealCards() { @DisplayName("참여자의 답변이 y라면 카드를 한장 추가한다.") void addOneCard() { //given - final Player redddy = new Player("레디", Hands.createEmptyPacket()); - final Player zeze = new Player("제제", Hands.createEmptyPacket()); + final Participant redddy = new Participant("레디", Hands.createEmptyPacket()); + final Participant zeze = new Participant("제제", Hands.createEmptyPacket()); final Players players = new Players(List.of(redddy, zeze)); diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index bde2e36b06..82d369b8e3 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -40,9 +40,9 @@ void playerResult() { List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), new Card(ACE, SPADE))); - Player loser = new Player("레디", sum18); - Player winner = new Player("제제", sum21); - Player tier = new Player("수달", sum20); + Participant loser = new Participant("레디", sum18); + Participant winner = new Participant("제제", sum21); + Participant tier = new Participant("수달", sum20); Players players = new Players(List.of(loser, winner, tier)); Dealer dealer = new Dealer(CardDeck.generate(), sum20); @@ -50,7 +50,7 @@ void playerResult() { Game game = new Game(dealer, players); //when & then - Map expected = Map.of(loser, LOSE, winner, WIN, tier, TIE); + Map expected = Map.of(loser, LOSE, winner, WIN, tier, TIE); Assertions.assertThat(game.getPlayersResult()).isEqualTo(expected); } @@ -68,10 +68,10 @@ void dealerResult() { List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), new Card(ACE, SPADE))); - Player loser1 = new Player("레디", sum18); - Player loser2 = new Player("피케이", sum18); - Player winner = new Player("제제", sum21); - Player tier = new Player("브라운", sum20); + Participant loser1 = new Participant("레디", sum18); + Participant loser2 = new Participant("피케이", sum18); + Participant winner = new Participant("제제", sum21); + Participant tier = new Participant("브라운", sum20); Players players = new Players(List.of(loser1, loser2, winner, tier)); Dealer dealer = new Dealer(CardDeck.generate(), sum20); @@ -99,24 +99,24 @@ void all() { new Card(FOUR, DIAMOND)) ); - Player winner1 = new Player("레디", winner1PlayerHandsSum19); + Participant winner1 = new Participant("레디", winner1PlayerHandsSum19); Hands winner2PlayerHandsSum20 = new Hands( List.of(new Card(QUEEN, CLOVER), new Card(KING, SPADE)) ); - Player winner2 = new Player("브라운", winner2PlayerHandsSum20); + Participant winner2 = new Participant("브라운", winner2PlayerHandsSum20); Hands loserPlayerHandsSum22 = new Hands( List.of(new Card(SEVEN, CLOVER), new Card(QUEEN, HEART), new Card(FIVE, SPADE)) ); - Player loser = new Player("제제", loserPlayerHandsSum22); + Participant loser = new Participant("제제", loserPlayerHandsSum22); Players players = new Players(List.of(winner1, winner2, loser)); //when Game game = new Game(bustDealer, players); - Map expectedPlayerResult = Map.of(winner1, WIN, winner2, WIN, loser, LOSE); + Map expectedPlayerResult = Map.of(winner1, WIN, winner2, WIN, loser, LOSE); Map expectedDealerResult = Map.of(WIN, 1, LOSE, 2); //then diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/ParticipantTest.java similarity index 77% rename from src/test/java/domain/PlayerTest.java rename to src/test/java/domain/ParticipantTest.java index 33238c85b6..2f6fb03538 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/ParticipantTest.java @@ -7,11 +7,11 @@ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; -class PlayerTest { +class ParticipantTest { @DisplayName("이름으로 참여자를 생성한다.") @Test void createPlayerWithName() { - Assertions.assertThatCode(() -> new Player("pobi", Hands.createEmptyPacket())) + Assertions.assertThatCode(() -> new Participant("pobi", Hands.createEmptyPacket())) .doesNotThrowAnyException(); } @@ -20,7 +20,7 @@ void createPlayerWithName() { @ValueSource(strings = {"", " ", " "}) @NullSource void nullOrBlankInputThrowException(String name) { - Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyPacket())) + Assertions.assertThatThrownBy(() -> new Participant(name, Hands.createEmptyPacket())) .isInstanceOf(IllegalArgumentException.class); } } diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index a6af634a6a..595c579616 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -55,9 +55,9 @@ void isAllBustFalse() { Hands noBustHands = new Hands( List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE))); - Player player1 = new Player("레디", bustHands); - Player player2 = new Player("제제", noBustHands); - Players players = new Players(List.of(player1, player2)); + Participant participant1 = new Participant("레디", bustHands); + Participant participant2 = new Participant("제제", noBustHands); + Players players = new Players(List.of(participant1, participant2)); //when && then Assertions.assertThat(players.isAllBust()).isFalse(); @@ -71,12 +71,12 @@ void isAllBustTrue() { List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE), new Card(Rank.TEN, Shape.HEART))); - Player player1 = new Player("레디", bustHands); - Player player2 = new Player("제제", bustHands); - Player player3 = new Player("수달", bustHands); - Player player4 = new Player("피케이", bustHands); + Participant participant1 = new Participant("레디", bustHands); + Participant participant2 = new Participant("제제", bustHands); + Participant participant3 = new Participant("수달", bustHands); + Participant participant4 = new Participant("피케이", bustHands); - Players players = new Players(List.of(player1, player2, player3, player4)); + Players players = new Players(List.of(participant1, participant2, participant3, participant4)); //when && then Assertions.assertThat(players.isAllBust()).isTrue(); From 5a87d7c11dfd72d1d62e0e7ab8faccb5bbb74a20 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 12:09:16 +0900 Subject: [PATCH 055/100] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=20=EC=82=AC=ED=95=AD=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 3c67b7b5ba..675fc68227 100644 --- a/docs/README.md +++ b/docs/README.md @@ -33,7 +33,7 @@
-* [x] 딜러가 카드 2장을 분해하다. +* [x] 딜러가 카드 2장을 분배하다. * [x] 카드는 총 6벌을 둔다. (52 * 6) * [x] 딜러의 카드를 출력한다. (1장) * [x] 참여자의 카드를 출력한다. (2장) @@ -43,6 +43,7 @@ * [x] 참여자는 hit(y) / stay(n)를 선택한다. * [x] y, n 가 아닐시 예외가 발생한다. * [x] 현재 가지고 있는 카드를 출력한다. +* [x] 카드 합이 블랙잭인 경우 블랙잭 메시지를 출력한다. * [x] 카드 합이 21 초과시 버스트 메시지를 출력한다.
From 72a3a20bbd218a537fcb0441dd1702698b9a1d56 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 12:25:42 +0900 Subject: [PATCH 056/100] =?UTF-8?q?fix:=20=EC=B9=B4=EB=93=9C=EC=9D=98=20?= =?UTF-8?q?=EC=82=AC=EC=9D=B4=EC=A6=88=EA=B0=80=20=EA=B0=99=EC=9D=84=20?= =?UTF-8?q?=EB=95=8C=2021=EC=97=90=20=EA=B0=80=EA=B9=8C=EC=9A=B4=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=ED=95=A9=EC=9D=B4=20=EC=9D=B4=EA=B8=B0?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=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/domain/Hands.java | 2 +- src/test/java/domain/HandsTest.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 1b34943e89..aba5624dd4 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -32,7 +32,7 @@ public int sum() { public Result calculateResult(final Hands target) { if (this.sum() >= target.sum() && this.sum() <= BLACK_JACK) { - if (this.size() == target.size()) { + if (this.size() == target.size() && this.sum() == target.sum()) { return Result.TIE; } diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index a550484638..f15e6d9c8c 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -146,6 +146,21 @@ void isLoseWhenCardSumGreater21() { } + @Test + @DisplayName("blackjack이 이긴다.") + void isWinBlackJack() { + //given + final Hands blackJack = new Hands( + List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.ACE, Shape.SPADE))); + + final Hands sum19 = new Hands( + List.of(new Card(Rank.NINE, Shape.HEART), new Card(Rank.TEN, Shape.SPADE))); + + //when && then + Assertions.assertThat(blackJack.calculateResult(sum19)).isEqualTo(Result.WIN); + Assertions.assertThat(sum19.calculateResult(blackJack)).isEqualTo(Result.LOSE); + } + static Stream sumParameterProvider() { return Stream.of( Arguments.of(new Hands(List.of(new Card(Rank.TWO, Shape.HEART), From 9414fb4a4130f2f68e539e7c1cffc64a129c4578 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Fri, 8 Mar 2024 13:46:57 +0900 Subject: [PATCH 057/100] =?UTF-8?q?refactor:=20Participant=20=EC=B6=94?= =?UTF-8?q?=EC=83=81=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 --- .../java/controller/BlackJackController.java | 24 +++---- src/main/java/domain/Game.java | 12 ++-- src/main/java/domain/Hands.java | 20 ++++++ src/main/java/domain/Players.java | 60 ------------------ .../java/domain/{ => participant}/Dealer.java | 22 +++---- .../domain/{ => participant}/Participant.java | 36 ++++------- src/main/java/domain/participant/Player.java | 28 +++++++++ src/main/java/domain/participant/Players.java | 62 +++++++++++++++++++ src/main/java/dto/DealerHandsDto.java | 3 +- src/main/java/dto/PlayerDto.java | 10 +-- src/main/java/dto/PlayersDto.java | 10 +-- src/main/java/view/OutputView.java | 7 ++- src/test/java/domain/DealerTest.java | 10 ++- src/test/java/domain/GameTest.java | 47 +++++++------- .../{ParticipantTest.java => PlayerTest.java} | 7 ++- src/test/java/domain/PlayersTest.java | 24 +++---- 16 files changed, 213 insertions(+), 169 deletions(-) delete mode 100644 src/main/java/domain/Players.java rename src/main/java/domain/{ => participant}/Dealer.java (70%) rename src/main/java/domain/{ => participant}/Participant.java (58%) create mode 100644 src/main/java/domain/participant/Player.java create mode 100644 src/main/java/domain/participant/Players.java rename src/test/java/domain/{ParticipantTest.java => PlayerTest.java} (76%) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index d0682028a4..9aeaf48d42 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -2,10 +2,10 @@ import domain.Answer; import domain.CardDeck; -import domain.Dealer; import domain.Game; -import domain.Participant; -import domain.Players; +import domain.participant.Dealer; +import domain.participant.Player; +import domain.participant.Players; import dto.DealerHandsDto; import dto.PlayerDto; import dto.PlayersDto; @@ -30,8 +30,8 @@ public void run() { dealer.startDeal(players); outputView.printStartDeal(DealerHandsDto.from(dealer), PlayersDto.from(players)); - for (Participant participant : players.getPlayers()) { - deal(participant, dealer); + for (Player player : players.getPlayers()) { + deal(player, dealer); } if (players.isAllBust()) { @@ -40,7 +40,7 @@ public void run() { return; } - while (dealer.getTotalCardSum() <= 16) { //TODO 테스트 코드 고민 + while (dealer.handsSum() <= 16) { //TODO 테스트 코드 고민 dealer.deal(); outputView.printDealerCard(); } @@ -49,24 +49,24 @@ public void run() { outputView.printGameResult(game.getDealerResult(), game.getPlayersResult()); } - private void deal(final Participant participant, final Dealer dealer) { + private void deal(final Player player, final Dealer dealer) { boolean changed = false; Answer answer = Answer.HIT; while (Answer.HIT.equals(answer)) { - answer = Answer.from(inputView.readAnswer(participant.getName())); - dealer.deal(participant, answer); + answer = Answer.from(inputView.readAnswer(player.getName())); + dealer.deal(player, answer); if (handsChanged(changed, answer)) { - outputView.printHands(PlayerDto.from(participant)); + outputView.printHands(PlayerDto.from(player)); } - if (participant.isBust()) { + if (player.isBust()) { outputView.printBustMessage(); break; } changed = true; - if (participant.isBlackJack()) { + if (player.isBlackJack()) { outputView.printBlackJack(); break; } diff --git a/src/main/java/domain/Game.java b/src/main/java/domain/Game.java index dd4d5ba366..a65ac67bb5 100644 --- a/src/main/java/domain/Game.java +++ b/src/main/java/domain/Game.java @@ -1,5 +1,9 @@ package domain; +import domain.participant.Dealer; +import domain.participant.Player; +import domain.participant.Players; + import java.util.EnumMap; import java.util.LinkedHashMap; import java.util.Map; @@ -14,11 +18,11 @@ public Game(final Dealer dealer, final Players players) { this.players = players; } - public Map getPlayersResult() { - final Map playerResult = new LinkedHashMap<>(); + public Map getPlayersResult() { + final Map playerResult = new LinkedHashMap<>(); - for (Participant participant : players.getPlayers()) { //TODO getPlayers() 메서드명 고려 - playerResult.put(participant, participant.calculateResult(dealer)); + for (Player player : players.getPlayers()) { //TODO getPlayers() 메서드명 고려 + playerResult.put(player, player.calculateResult(dealer)); } return playerResult; } diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index aba5624dd4..45fb2e3d7d 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -1,8 +1,10 @@ package domain; import domain.card.Card; + import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class Hands { @@ -69,4 +71,22 @@ public int size() { public List getCards() { return cards; } + + @Override + public boolean equals(final Object target) { + if (this == target) { + return true; + } + + if (!(target instanceof Hands hands)) { + return false; + } + + return Objects.equals(cards, hands.cards); + } + + @Override + public int hashCode() { + return Objects.hash(cards); + } } diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java deleted file mode 100644 index d890ab12f0..0000000000 --- a/src/main/java/domain/Players.java +++ /dev/null @@ -1,60 +0,0 @@ -package domain; - -import java.util.List; -import java.util.Set; -import java.util.function.Consumer; - -public class Players { - - private static final int MIN_SIZE = 2; - private static final int MAX_SIZE = 8; - - private final List names; - - public Players(final List names) { - this.names = names; - } - - public static Players from(final List names) { - List participantNames = mapToPlayers(names); - validate(participantNames); - return new Players(participantNames); - } - - public void forEach(Consumer action) { - names.forEach(action); - } - - public boolean isAllBust() { - return names.stream() - .allMatch(Participant::isBust); - } - - private static List mapToPlayers(final List names) { - return names.stream() - .map(String::trim) - .map(name -> new Participant(name, Hands.createEmptyPacket())) - .toList(); - } - - private static void validate(final List participants) { - validateSize(participants); - validateDuplicate(participants); - } - - private static void validateSize(final List participants) { - if (participants.size() < MIN_SIZE || MAX_SIZE < participants.size()) { - throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 수입니다."); - } - } - - private static void validateDuplicate(final List participants) { - if (participants.size() != Set.copyOf(participants).size()) { - throw new IllegalArgumentException("[ERROR] 참여자 이름은 중복될 수 없습니다."); - } - } - - public List getPlayers() { - return names; - } -} diff --git a/src/main/java/domain/Dealer.java b/src/main/java/domain/participant/Dealer.java similarity index 70% rename from src/main/java/domain/Dealer.java rename to src/main/java/domain/participant/Dealer.java index 2f51110b1f..0d455269e2 100644 --- a/src/main/java/domain/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -1,4 +1,8 @@ -package domain; +package domain.participant; + +import domain.Answer; +import domain.CardDeck; +import domain.Hands; public class Dealer extends Participant { @@ -24,25 +28,13 @@ public void startDeal(final Players players) { } } - public void deal(final Participant participant, final Answer answer) { + public void deal(final Player player, final Answer answer) { if (Answer.HIT.equals(answer)) { - participant.add(cardDeck.pop()); + player.add(cardDeck.pop()); } } public void deal() { super.add(cardDeck.pop()); } - - public int getTotalCardSum() { - return super.handsSum(); - } - - public Hands getHands() { - return super.getHands(); - } - - public String getName() { - return NAME; - } } diff --git a/src/main/java/domain/Participant.java b/src/main/java/domain/participant/Participant.java similarity index 58% rename from src/main/java/domain/Participant.java rename to src/main/java/domain/participant/Participant.java index 318175358e..d4230511d6 100644 --- a/src/main/java/domain/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -1,16 +1,17 @@ -package domain; +package domain.participant; +import domain.Hands; +import domain.Result; import domain.card.Card; import java.util.List; import java.util.Objects; -public class Participant { +public abstract class Participant { private final String name; private final Hands hands; public Participant(final String name, final Hands hands) { - validate(name); this.name = name; this.hands = hands; } @@ -19,8 +20,8 @@ public void add(final Card card) { hands.add(card); } - public Result calculateResult(final Dealer dealer) { - return hands.calculateResult(dealer.getHands()); + public Result calculateResult(final Participant participant) { + return hands.calculateResult(participant.getHands()); } //TODO 메서드가 조잡해요 @@ -54,36 +55,21 @@ public Hands getHands() { return hands; } - private void validate(final String name) { - validateNull(name); - validateBlank(name); - } - - private void validateNull(final String name) { - if (name == null) { - throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 이름입니다."); - } - } - - private void validateBlank(final String name) { - if (name.isBlank()) { - throw new IllegalArgumentException("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); - } - } - @Override public boolean equals(final Object target) { if (this == target) { return true; } + if (!(target instanceof Participant participant)) { - return true; + return false; } - return Objects.equals(name, participant.name); + + return Objects.equals(name, participant.name) && Objects.equals(hands, participant.hands); } @Override public int hashCode() { - return Objects.hash(name); + return Objects.hash(name, hands); } } diff --git a/src/main/java/domain/participant/Player.java b/src/main/java/domain/participant/Player.java new file mode 100644 index 0000000000..4ddbe76ca6 --- /dev/null +++ b/src/main/java/domain/participant/Player.java @@ -0,0 +1,28 @@ +package domain.participant; + +import domain.Hands; + +public class Player extends Participant { + + public Player(final String name, final Hands hands) { + super(name, hands); + validate(name); + } + + private void validate(final String name) { + validateNull(name); + validateBlank(name); + } + + private void validateNull(final String name) { + if (name == null) { + throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 이름입니다."); + } + } + + private void validateBlank(final String name) { + if (name.isBlank()) { + throw new IllegalArgumentException("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); + } + } +} diff --git a/src/main/java/domain/participant/Players.java b/src/main/java/domain/participant/Players.java new file mode 100644 index 0000000000..16166c54df --- /dev/null +++ b/src/main/java/domain/participant/Players.java @@ -0,0 +1,62 @@ +package domain.participant; + +import domain.Hands; + +import java.util.List; +import java.util.Set; +import java.util.function.Consumer; + +public class Players { + + private static final int MIN_SIZE = 2; + private static final int MAX_SIZE = 8; + + private final List names; + + public Players(final List names) { + this.names = names; + } + + public static Players from(final List names) { + List playerNames = mapToPlayers(names); + validate(playerNames); + return new Players(playerNames); + } + + public void forEach(Consumer action) { + names.forEach(action); + } + + public boolean isAllBust() { + return names.stream() + .allMatch(Player::isBust); + } + + private static List mapToPlayers(final List names) { + return names.stream() + .map(String::trim) + .map(name -> new Player(name, Hands.createEmptyPacket())) + .toList(); + } + + private static void validate(final List players) { + validateSize(players); + validateDuplicate(players); + } + + private static void validateSize(final List players) { + if (players.size() < MIN_SIZE || MAX_SIZE < players.size()) { + throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 수입니다."); + } + } + + private static void validateDuplicate(final List players) { + if (players.size() != Set.copyOf(players).size()) { + throw new IllegalArgumentException("[ERROR] 참여자 이름은 중복될 수 없습니다."); + } + } + + public List getPlayers() { + return names; + } +} diff --git a/src/main/java/dto/DealerHandsDto.java b/src/main/java/dto/DealerHandsDto.java index bbf7d12a09..428383c4b9 100644 --- a/src/main/java/dto/DealerHandsDto.java +++ b/src/main/java/dto/DealerHandsDto.java @@ -1,7 +1,6 @@ package dto; -import domain.Dealer; -import domain.Participant; +import domain.participant.Participant; import java.util.List; diff --git a/src/main/java/dto/PlayerDto.java b/src/main/java/dto/PlayerDto.java index 083c819ac4..f81d335d7d 100644 --- a/src/main/java/dto/PlayerDto.java +++ b/src/main/java/dto/PlayerDto.java @@ -1,7 +1,7 @@ package dto; -import domain.Dealer; -import domain.Participant; +import domain.participant.Dealer; +import domain.participant.Player; import java.util.List; @@ -16,12 +16,12 @@ public PlayerDto(final String name, final List cards, final int totalSum this.totalSum = totalSum; } - public static PlayerDto from(final Participant participant) { - return new PlayerDto(participant.getName(), participant.getCardNames(), participant.handsSum()); + public static PlayerDto from(final Player player) { + return new PlayerDto(player.getName(), player.getCardNames(), player.handsSum()); } public static PlayerDto from(final Dealer dealer) { - return new PlayerDto(dealer.getName(), dealer.getCardNames(), dealer.getTotalCardSum()); + return new PlayerDto(dealer.getName(), dealer.getCardNames(), dealer.handsSum()); } public String getName() { diff --git a/src/main/java/dto/PlayersDto.java b/src/main/java/dto/PlayersDto.java index 23e9b3c323..21a68f75e3 100644 --- a/src/main/java/dto/PlayersDto.java +++ b/src/main/java/dto/PlayersDto.java @@ -1,8 +1,8 @@ package dto; -import domain.Dealer; -import domain.Participant; -import domain.Players; +import domain.participant.Dealer; +import domain.participant.Player; +import domain.participant.Players; import java.util.ArrayList; import java.util.List; @@ -23,8 +23,8 @@ public static PlayersDto from(final Dealer dealer, final Players players) { // T public static PlayersDto from(final Players players) { List result = new ArrayList<>(); - for (Participant participant : players.getPlayers()) { - result.add(PlayerDto.from(participant)); + for (Player player : players.getPlayers()) { + result.add(PlayerDto.from(player)); } return new PlayersDto(result); diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 4ad159072e..d7cb8017b2 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,10 +1,11 @@ package view; -import domain.Participant; import domain.Result; +import domain.participant.Player; import dto.DealerHandsDto; import dto.PlayerDto; import dto.PlayersDto; + import java.util.List; import java.util.Map; @@ -46,11 +47,11 @@ public void printHandsResult(final PlayersDto playersDto) { } } - public void printGameResult(final Map dealerResult, final Map playerResult) { + public void printGameResult(final Map dealerResult, final Map playerResult) { System.out.println("## 최종결과"); System.out.printf(RESULT_FORM, "딜러", format(dealerResult)); System.out.println(); - for (Map.Entry entry : playerResult.entrySet()) { + for (Map.Entry entry : playerResult.entrySet()) { System.out.printf(RESULT_FORM, entry.getKey().getName(), entry.getValue().getValue()); System.out.println(); } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 2c956f97ed..731c77d2c3 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -1,10 +1,14 @@ package domain; -import java.util.List; +import domain.participant.Dealer; +import domain.participant.Player; +import domain.participant.Players; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.List; + class DealerTest { @Test @@ -26,8 +30,8 @@ void dealCards() { @DisplayName("참여자의 답변이 y라면 카드를 한장 추가한다.") void addOneCard() { //given - final Participant redddy = new Participant("레디", Hands.createEmptyPacket()); - final Participant zeze = new Participant("제제", Hands.createEmptyPacket()); + final Player redddy = new Player("레디", Hands.createEmptyPacket()); + final Player zeze = new Player("제제", Hands.createEmptyPacket()); final Players players = new Players(List.of(redddy, zeze)); diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index 82d369b8e3..32d8959af6 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -1,5 +1,19 @@ package domain; +import domain.card.Card; +import domain.participant.Dealer; +import domain.participant.Player; +import domain.participant.Players; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +import static domain.Result.LOSE; +import static domain.Result.TIE; +import static domain.Result.WIN; import static domain.card.Rank.ACE; import static domain.card.Rank.EIGHT; import static domain.card.Rank.FIVE; @@ -15,15 +29,6 @@ import static domain.card.Shape.DIAMOND; import static domain.card.Shape.HEART; import static domain.card.Shape.SPADE; -import static domain.Result.*; - -import java.util.List; -import java.util.Map; - -import domain.card.Card; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; class GameTest { @@ -40,9 +45,9 @@ void playerResult() { List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), new Card(ACE, SPADE))); - Participant loser = new Participant("레디", sum18); - Participant winner = new Participant("제제", sum21); - Participant tier = new Participant("수달", sum20); + Player loser = new Player("레디", sum18); + Player winner = new Player("제제", sum21); + Player tier = new Player("수달", sum20); Players players = new Players(List.of(loser, winner, tier)); Dealer dealer = new Dealer(CardDeck.generate(), sum20); @@ -50,7 +55,7 @@ void playerResult() { Game game = new Game(dealer, players); //when & then - Map expected = Map.of(loser, LOSE, winner, WIN, tier, TIE); + Map expected = Map.of(loser, LOSE, winner, WIN, tier, TIE); Assertions.assertThat(game.getPlayersResult()).isEqualTo(expected); } @@ -68,10 +73,10 @@ void dealerResult() { List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), new Card(ACE, SPADE))); - Participant loser1 = new Participant("레디", sum18); - Participant loser2 = new Participant("피케이", sum18); - Participant winner = new Participant("제제", sum21); - Participant tier = new Participant("브라운", sum20); + Player loser1 = new Player("레디", sum18); + Player loser2 = new Player("피케이", sum18); + Player winner = new Player("제제", sum21); + Player tier = new Player("브라운", sum20); Players players = new Players(List.of(loser1, loser2, winner, tier)); Dealer dealer = new Dealer(CardDeck.generate(), sum20); @@ -99,24 +104,24 @@ void all() { new Card(FOUR, DIAMOND)) ); - Participant winner1 = new Participant("레디", winner1PlayerHandsSum19); + Player winner1 = new Player("레디", winner1PlayerHandsSum19); Hands winner2PlayerHandsSum20 = new Hands( List.of(new Card(QUEEN, CLOVER), new Card(KING, SPADE)) ); - Participant winner2 = new Participant("브라운", winner2PlayerHandsSum20); + Player winner2 = new Player("브라운", winner2PlayerHandsSum20); Hands loserPlayerHandsSum22 = new Hands( List.of(new Card(SEVEN, CLOVER), new Card(QUEEN, HEART), new Card(FIVE, SPADE)) ); - Participant loser = new Participant("제제", loserPlayerHandsSum22); + Player loser = new Player("제제", loserPlayerHandsSum22); Players players = new Players(List.of(winner1, winner2, loser)); //when Game game = new Game(bustDealer, players); - Map expectedPlayerResult = Map.of(winner1, WIN, winner2, WIN, loser, LOSE); + Map expectedPlayerResult = Map.of(winner1, WIN, winner2, WIN, loser, LOSE); Map expectedDealerResult = Map.of(WIN, 1, LOSE, 2); //then diff --git a/src/test/java/domain/ParticipantTest.java b/src/test/java/domain/PlayerTest.java similarity index 76% rename from src/test/java/domain/ParticipantTest.java rename to src/test/java/domain/PlayerTest.java index 2f6fb03538..d3751f986b 100644 --- a/src/test/java/domain/ParticipantTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -1,5 +1,6 @@ package domain; +import domain.participant.Player; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -7,11 +8,11 @@ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; -class ParticipantTest { +class PlayerTest { @DisplayName("이름으로 참여자를 생성한다.") @Test void createPlayerWithName() { - Assertions.assertThatCode(() -> new Participant("pobi", Hands.createEmptyPacket())) + Assertions.assertThatCode(() -> new Player("pobi", Hands.createEmptyPacket())) .doesNotThrowAnyException(); } @@ -20,7 +21,7 @@ void createPlayerWithName() { @ValueSource(strings = {"", " ", " "}) @NullSource void nullOrBlankInputThrowException(String name) { - Assertions.assertThatThrownBy(() -> new Participant(name, Hands.createEmptyPacket())) + Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyPacket())) .isInstanceOf(IllegalArgumentException.class); } } diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index 595c579616..3c41e205aa 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -1,11 +1,10 @@ package domain; -import java.util.List; -import java.util.stream.Stream; - import domain.card.Card; import domain.card.Rank; import domain.card.Shape; +import domain.participant.Player; +import domain.participant.Players; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -13,6 +12,9 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import java.util.List; +import java.util.stream.Stream; + class PlayersTest { @Test @@ -55,9 +57,9 @@ void isAllBustFalse() { Hands noBustHands = new Hands( List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE))); - Participant participant1 = new Participant("레디", bustHands); - Participant participant2 = new Participant("제제", noBustHands); - Players players = new Players(List.of(participant1, participant2)); + Player player1 = new Player("레디", bustHands); + Player player2 = new Player("제제", noBustHands); + Players players = new Players(List.of(player1, player2)); //when && then Assertions.assertThat(players.isAllBust()).isFalse(); @@ -71,12 +73,12 @@ void isAllBustTrue() { List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE), new Card(Rank.TEN, Shape.HEART))); - Participant participant1 = new Participant("레디", bustHands); - Participant participant2 = new Participant("제제", bustHands); - Participant participant3 = new Participant("수달", bustHands); - Participant participant4 = new Participant("피케이", bustHands); + Player player1 = new Player("레디", bustHands); + Player player2 = new Player("제제", bustHands); + Player player3 = new Player("수달", bustHands); + Player player4 = new Player("피케이", bustHands); - Players players = new Players(List.of(participant1, participant2, participant3, participant4)); + Players players = new Players(List.of(player1, player2, player3, player4)); //when && then Assertions.assertThat(players.isAllBust()).isTrue(); From 2d2154ecfb98c9464b8983cb946e0ea23d3d630f Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 14:41:46 +0900 Subject: [PATCH 058/100] =?UTF-8?q?refactor:=20Dto=EC=9D=98=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EC=A0=95=EC=A0=81?= =?UTF-8?q?=20=ED=8C=A9=ED=84=B0=EB=A6=AC=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/BlackJackController.java | 12 +++--- src/main/java/dto/ParticipantDto.java | 32 ++++++++++++++ src/main/java/dto/ParticipantsDto.java | 42 +++++++++++++++++++ src/main/java/dto/PlayerDto.java | 38 ----------------- src/main/java/dto/PlayersDto.java | 42 ------------------- src/main/java/view/OutputView.java | 22 +++++----- 6 files changed, 91 insertions(+), 97 deletions(-) create mode 100644 src/main/java/dto/ParticipantDto.java create mode 100644 src/main/java/dto/ParticipantsDto.java delete mode 100644 src/main/java/dto/PlayerDto.java delete mode 100644 src/main/java/dto/PlayersDto.java diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 9aeaf48d42..f1452ce1f6 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -7,8 +7,8 @@ import domain.participant.Player; import domain.participant.Players; import dto.DealerHandsDto; -import dto.PlayerDto; -import dto.PlayersDto; +import dto.ParticipantDto; +import dto.ParticipantsDto; import view.InputView; import view.OutputView; @@ -28,14 +28,14 @@ public void run() { final Game game = new Game(dealer, players); dealer.startDeal(players); - outputView.printStartDeal(DealerHandsDto.from(dealer), PlayersDto.from(players)); + outputView.printStartDeal(DealerHandsDto.from(dealer), ParticipantsDto.of(players)); for (Player player : players.getPlayers()) { deal(player, dealer); } if (players.isAllBust()) { - outputView.printHandsResult(PlayersDto.from(dealer, players)); + outputView.printHandsResult(ParticipantsDto.of(dealer, players)); outputView.printGameResult(game.getDealerResult(), game.getPlayersResult()); return; } @@ -45,7 +45,7 @@ public void run() { outputView.printDealerCard(); } - outputView.printHandsResult(PlayersDto.from(dealer, players)); + outputView.printHandsResult(ParticipantsDto.of(dealer, players)); outputView.printGameResult(game.getDealerResult(), game.getPlayersResult()); } @@ -58,7 +58,7 @@ private void deal(final Player player, final Dealer dealer) { dealer.deal(player, answer); if (handsChanged(changed, answer)) { - outputView.printHands(PlayerDto.from(player)); + outputView.printHands(ParticipantDto.from(player)); } if (player.isBust()) { outputView.printBustMessage(); diff --git a/src/main/java/dto/ParticipantDto.java b/src/main/java/dto/ParticipantDto.java new file mode 100644 index 0000000000..c230e5d94d --- /dev/null +++ b/src/main/java/dto/ParticipantDto.java @@ -0,0 +1,32 @@ +package dto; + +import domain.participant.Participant; +import java.util.List; + +public class ParticipantDto { + private final String name; + private final List cards; + private final int totalSum; + + public ParticipantDto(final String name, final List cards, final int totalSum) { + this.name = name; + this.cards = cards; + this.totalSum = totalSum; + } + + public static ParticipantDto from(final Participant player) { + return new ParticipantDto(player.getName(), player.getCardNames(), player.handsSum()); + } + + public String getName() { + return name; + } + + public List getCards() { + return cards; + } + + public int getTotalSum() { + return totalSum; + } +} diff --git a/src/main/java/dto/ParticipantsDto.java b/src/main/java/dto/ParticipantsDto.java new file mode 100644 index 0000000000..7218c9557a --- /dev/null +++ b/src/main/java/dto/ParticipantsDto.java @@ -0,0 +1,42 @@ +package dto; + +import domain.participant.Participant; +import domain.participant.Players; +import java.util.ArrayList; +import java.util.List; + +public class ParticipantsDto { // TODO: parit dto + + private final List players; + + private ParticipantsDto(final List players) { + this.players = players; + } + + public static ParticipantsDto of(final Participant dealer, final Players players) { + List result = new ArrayList<>(); + result.add(ParticipantDto.from(dealer)); + for (Participant player : players.getPlayers()) { + result.add(ParticipantDto.from(player)); + } + return new ParticipantsDto(result); + } + + public static ParticipantsDto of(final Players players) { + List result = new ArrayList<>(); + for (Participant player : players.getPlayers()) { + result.add(ParticipantDto.from(player)); + } + return new ParticipantsDto(result); + } + + public List getNames() { + return players.stream() + .map(ParticipantDto::getName) + .toList(); + } + + public List getPlayers() { + return players; + } +} diff --git a/src/main/java/dto/PlayerDto.java b/src/main/java/dto/PlayerDto.java deleted file mode 100644 index f81d335d7d..0000000000 --- a/src/main/java/dto/PlayerDto.java +++ /dev/null @@ -1,38 +0,0 @@ -package dto; - -import domain.participant.Dealer; -import domain.participant.Player; - -import java.util.List; - -public class PlayerDto { - private final String name; - private final List cards; - private final int totalSum; - - public PlayerDto(final String name, final List cards, final int totalSum) { - this.name = name; - this.cards = cards; - this.totalSum = totalSum; - } - - public static PlayerDto from(final Player player) { - return new PlayerDto(player.getName(), player.getCardNames(), player.handsSum()); - } - - public static PlayerDto from(final Dealer dealer) { - return new PlayerDto(dealer.getName(), dealer.getCardNames(), dealer.handsSum()); - } - - public String getName() { - return name; - } - - public List getCards() { - return cards; - } - - public int getTotalSum() { - return totalSum; - } -} diff --git a/src/main/java/dto/PlayersDto.java b/src/main/java/dto/PlayersDto.java deleted file mode 100644 index 21a68f75e3..0000000000 --- a/src/main/java/dto/PlayersDto.java +++ /dev/null @@ -1,42 +0,0 @@ -package dto; - -import domain.participant.Dealer; -import domain.participant.Player; -import domain.participant.Players; - -import java.util.ArrayList; -import java.util.List; - -public class PlayersDto { - - private final List players; - - private PlayersDto(final List players) { - this.players = players; - } - - public static PlayersDto from(final Dealer dealer, final Players players) { // TODO 추상화 고려 - PlayersDto playersDto = from(players); - playersDto.players.add(0, PlayerDto.from(dealer)); - return playersDto; - } - - public static PlayersDto from(final Players players) { - List result = new ArrayList<>(); - for (Player player : players.getPlayers()) { - result.add(PlayerDto.from(player)); - } - - return new PlayersDto(result); - } - - public List getNames() { - return players.stream() - .map(PlayerDto::getName) - .toList(); - } - - public List getPlayers() { - return players; - } -} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index d7cb8017b2..cf7a3a7fe4 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -3,8 +3,8 @@ import domain.Result; import domain.participant.Player; import dto.DealerHandsDto; -import dto.PlayerDto; -import dto.PlayersDto; +import dto.ParticipantDto; +import dto.ParticipantsDto; import java.util.List; import java.util.Map; @@ -15,23 +15,23 @@ public class OutputView { private final String TOTAL_SUM_FORM = "%s 카드: %s - 결과: %d"; private final String RESULT_FORM = "%s: %s"; - public void printStartDeal(final DealerHandsDto dealerHandsDto, final PlayersDto playersDto) { + public void printStartDeal(final DealerHandsDto dealerHandsDto, final ParticipantsDto participantsDto) { final String dealerCard = dealerHandsDto.getDisplayedCard(); - final List playerNames = playersDto.getNames(); + final List playerNames = participantsDto.getNames(); System.out.println("딜러와 " + format(playerNames) + " 에게 2장을 나누었습니다."); System.out.println("딜러: " + dealerCard); - for (PlayerDto playerDto : playersDto.getPlayers()) { - System.out.printf(FORM, playerDto.getName(), format(playerDto.getCards())); + for (ParticipantDto participantDto : participantsDto.getPlayers()) { + System.out.printf(FORM, participantDto.getName(), format(participantDto.getCards())); System.out.println(); } System.out.println(); } - public void printHands(final PlayerDto playerDto) { - System.out.printf(FORM, playerDto.getName(), format(playerDto.getCards())); + public void printHands(final ParticipantDto participantDto) { + System.out.printf(FORM, participantDto.getName(), format(participantDto.getCards())); System.out.println(); } @@ -40,9 +40,9 @@ public void printDealerCard() { System.out.println(); } - public void printHandsResult(final PlayersDto playersDto) { - for (PlayerDto playerDto : playersDto.getPlayers()) { - System.out.printf(TOTAL_SUM_FORM, playerDto.getName(), format(playerDto.getCards()), playerDto.getTotalSum()); + public void printHandsResult(final ParticipantsDto participantsDto) { + for (ParticipantDto participantDto : participantsDto.getPlayers()) { + System.out.printf(TOTAL_SUM_FORM, participantDto.getName(), format(participantDto.getCards()), participantDto.getTotalSum()); System.out.println(); } } From 356674b1e97ec9d4185b903bc1b3b15167683134 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 14:50:19 +0900 Subject: [PATCH 059/100] =?UTF-8?q?refactor:=20getCardNames=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=8F=99=EC=9E=91=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=EC=9D=84=20hands=20=EC=95=88=EC=9C=BC=EB=A1=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Hands.java | 7 ++++--- src/main/java/domain/card/Card.java | 9 ++++----- src/main/java/domain/participant/Participant.java | 5 +---- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 45fb2e3d7d..88ebecb8d5 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -1,7 +1,6 @@ package domain; import domain.card.Card; - import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -68,8 +67,10 @@ public int size() { return cards.size(); } - public List getCards() { - return cards; + public List getCards() { + return cards.stream() + .map(Card::toString) + .toList(); } @Override diff --git a/src/main/java/domain/card/Card.java b/src/main/java/domain/card/Card.java index 1daf7dd284..a9fec7d5e2 100644 --- a/src/main/java/domain/card/Card.java +++ b/src/main/java/domain/card/Card.java @@ -20,15 +20,14 @@ public boolean isAce() { } @Override - public boolean equals(final Object o) { - if (this == o) { + public boolean equals(final Object target) { + if (this == target) { return true; } - if (o == null || getClass() != o.getClass()) { + if (!(target instanceof Card card)) { return false; } - final Card card = (Card) o; - return rank == card.rank && shape == card.shape; + return Objects.equals(rank, card.rank) && Objects.equals(shape, card.shape); } @Override diff --git a/src/main/java/domain/participant/Participant.java b/src/main/java/domain/participant/Participant.java index d4230511d6..14e603f1fa 100644 --- a/src/main/java/domain/participant/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -3,7 +3,6 @@ import domain.Hands; import domain.Result; import domain.card.Card; - import java.util.List; import java.util.Objects; @@ -42,9 +41,7 @@ public boolean isBlackJack() { } public List getCardNames() { - return hands.getCards().stream() - .map(Card::toString) - .toList(); + return hands.getCards(); } public String getName() { From da599d26550e6d1424f15845ea502e95ba411a3c Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 14:54:00 +0900 Subject: [PATCH 060/100] =?UTF-8?q?refactor:=20Participant=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EC=9E=90=20=EC=A0=91=EA=B7=BC=EC=A0=9C=ED=95=9C?= =?UTF-8?q?=EC=9E=90=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/participant/Participant.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/domain/participant/Participant.java b/src/main/java/domain/participant/Participant.java index 14e603f1fa..713a2a5615 100644 --- a/src/main/java/domain/participant/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -10,7 +10,7 @@ public abstract class Participant { private final String name; private final Hands hands; - public Participant(final String name, final Hands hands) { + protected Participant(final String name, final Hands hands) { this.name = name; this.hands = hands; } @@ -19,15 +19,15 @@ public void add(final Card card) { hands.add(card); } - public Result calculateResult(final Participant participant) { - return hands.calculateResult(participant.getHands()); - } - //TODO 메서드가 조잡해요 public boolean isBust() { return hands.isBust(); } + public boolean isBlackJack() { + return hands.isBlackJack(); + } + public int handsSum() { return hands.sum(); } @@ -36,8 +36,8 @@ public int handsSize() { return hands.size(); } - public boolean isBlackJack() { - return hands.isBlackJack(); + public Result calculateResult(final Participant participant) { + return hands.calculateResult(participant.getHands()); } public List getCardNames() { From 019808493fc05e04416def9fed920e463239d480 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 15:29:20 +0900 Subject: [PATCH 061/100] =?UTF-8?q?refactor:=20Dealer=20=EC=9D=98=20deal?= =?UTF-8?q?=EC=9D=84=20Dealer=20=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/BlackJackController.java | 4 ++-- src/main/java/domain/Hands.java | 4 ++-- src/main/java/domain/participant/Dealer.java | 10 ++++++++-- src/test/java/domain/DealerTest.java | 19 +++++++++++++++++-- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index f1452ce1f6..171a9fa3e9 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -40,8 +40,8 @@ public void run() { return; } - while (dealer.handsSum() <= 16) { //TODO 테스트 코드 고민 - dealer.deal(); + int turn = dealer.turn(); + for (int i = 0; i < turn; i++) { outputView.printDealerCard(); } diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 88ebecb8d5..3c158449fd 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -11,8 +11,8 @@ public class Hands { private final List cards; - public Hands(final List cards) { //TODO validation - this.cards = cards; + public Hands(final List cards) { + this.cards = new ArrayList<>(cards); } public static Hands createEmptyPacket() { diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 0d455269e2..36846aefbc 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -8,6 +8,7 @@ public class Dealer extends Participant { private static final String NAME = "딜러"; private static final int INIT_HANDS_SIZE = 2; + private static final int MIN_HANDS_SUM = 16; private final CardDeck cardDeck; @@ -34,7 +35,12 @@ public void deal(final Player player, final Answer answer) { } } - public void deal() { - super.add(cardDeck.pop()); + public int turn() { + int result = 0; + while (handsSum() <= MIN_HANDS_SUM) { + super.add(cardDeck.pop()); + result++; + } + return result; } } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 731c77d2c3..3fee5de215 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -1,14 +1,18 @@ package domain; +import static domain.card.Rank.FIVE; +import static domain.card.Shape.HEART; +import static domain.card.Shape.SPADE; + +import domain.card.Card; import domain.participant.Dealer; import domain.participant.Player; import domain.participant.Players; +import java.util.List; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.List; - class DealerTest { @Test @@ -47,4 +51,15 @@ void addOneCard() { Assertions.assertThat(redddy.handsSize()).isEqualTo(3); Assertions.assertThat(zeze.handsSize()).isEqualTo(2); } + + @Test + @DisplayName("딜러의 카드의 합이 17이상이 될때까지 카드를 추가한다.") + void dealerTurn() { + //given + final Hands sum10 = new Hands(List.of(new Card(FIVE, SPADE), new Card(FIVE, HEART))); + final Dealer dealer = new Dealer(CardDeck.generate(), sum10); + + //when && then + Assertions.assertThat(dealer.turn()).isPositive(); + } } From 781f8e8db42a17d782fd73e5ecb2c60a6709918c Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Fri, 8 Mar 2024 16:44:14 +0900 Subject: [PATCH 062/100] =?UTF-8?q?refactor:=20controller=20=EC=9D=B8?= =?UTF-8?q?=EB=8D=B4=ED=8A=B8=20=EC=A4=84=EC=9D=B4=EA=B8=B0=20=EB=B0=8F=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 21 ++--- .../java/controller/BlackJackController.java | 79 ++++++++++++------- src/main/java/domain/participant/Dealer.java | 11 +-- .../java/domain/participant/Participant.java | 7 ++ src/main/java/view/OutputView.java | 4 +- src/test/java/domain/DealerTest.java | 39 ++++++--- 6 files changed, 104 insertions(+), 57 deletions(-) diff --git a/docs/README.md b/docs/README.md index 675fc68227..de4c45d7af 100644 --- a/docs/README.md +++ b/docs/README.md @@ -22,27 +22,28 @@ # 기능 요구 사항 * [x] 참여자 이름 입력 받는다. - * [x] 양 끝 공백을 제거한다. - * [x] 참여자는 쉼표(,)로 구분한다. - * [x] null 이나 공백인 경우 예외가 발생한다. - * [x] 쉼표로 시작시 예외가 발생한다. ex) ,pobi,jason - * [x] 쉼표로 끝날시 예외가 발생한다. ex) pobi,jason, - * [x] 쉼표가 연속으로 올시 예외가 발생한다. ex) pobi,,jason - * [x] 참여자 이름이 중복시 예외가 발생한다. + * [x] 양 끝 공백을 제거한다. + * [x] 참여자는 쉼표(,)로 구분한다. + * [x] null 이나 공백인 경우 예외가 발생한다. + * [x] 쉼표로 시작시 예외가 발생한다. ex) ,pobi,jason + * [x] 쉼표로 끝날시 예외가 발생한다. ex) pobi,jason, + * [x] 쉼표가 연속으로 올시 예외가 발생한다. ex) pobi,,jason + * [x] 참여자 이름이 중복시 예외가 발생한다. * [x] 총 참여자의 수는 2이상 8이하여야 한다.
* [x] 딜러가 카드 2장을 분배하다. - * [x] 카드는 총 6벌을 둔다. (52 * 6) + * [x] 카드는 총 6벌을 둔다. (52 * 6) * [x] 딜러의 카드를 출력한다. (1장) * [x] 참여자의 카드를 출력한다. (2장)
* [x] 참여자는 hit(y) / stay(n)를 선택한다. - * [x] y, n 가 아닐시 예외가 발생한다. -* [x] 현재 가지고 있는 카드를 출력한다. + * [x] y, n 가 아닐시 예외가 발생한다. +* [x] 참여자가 hit을 하는 경우 현재 가지고 있는 카드를 출력한다. +* [x] 참여자가 hit을 한 적 없이 stay를 하는 경우 현재 가지고 있는 카드를 출력한다. * [x] 카드 합이 블랙잭인 경우 블랙잭 메시지를 출력한다. * [x] 카드 합이 21 초과시 버스트 메시지를 출력한다. diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 171a9fa3e9..e3971aacd8 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -27,53 +27,72 @@ public void run() { final Dealer dealer = new Dealer(cardDeck); final Game game = new Game(dealer, players); - dealer.startDeal(players); - outputView.printStartDeal(DealerHandsDto.from(dealer), ParticipantsDto.of(players)); + initHands(players, dealer); + dealWithPlayers(players, dealer); - for (Player player : players.getPlayers()) { - deal(player, dealer); + if (!players.isAllBust()) { + dealer.deal(); + printDealerTurnMessage(dealer.countAddedHands()); } + printFinalResult(players, dealer, game); + } - if (players.isAllBust()) { - outputView.printHandsResult(ParticipantsDto.of(dealer, players)); - outputView.printGameResult(game.getDealerResult(), game.getPlayersResult()); - return; + private void printDealerTurnMessage(final int turn) { + for (int i = 0; i < turn; i++) { + outputView.printDealerTurnMessage(); } + } - int turn = dealer.turn(); - for (int i = 0; i < turn; i++) { - outputView.printDealerCard(); + private void dealWithPlayers(final Players players, final Dealer dealer) { + for (Player player : players.getPlayers()) { + deal(player, dealer); } + } + private void initHands(final Players players, final Dealer dealer) { + dealer.initHands(players); + outputView.printStartDeal(DealerHandsDto.from(dealer), ParticipantsDto.of(players)); + } + + private void printFinalResult(final Players players, final Dealer dealer, final Game game) { outputView.printHandsResult(ParticipantsDto.of(dealer, players)); outputView.printGameResult(game.getDealerResult(), game.getPlayersResult()); } private void deal(final Player player, final Dealer dealer) { - boolean changed = false; - Answer answer = Answer.HIT; + boolean handsChanged = false; + boolean turnEnded = false; - while (Answer.HIT.equals(answer)) { - answer = Answer.from(inputView.readAnswer(player.getName())); + while (!turnEnded) { + Answer answer = Answer.from(inputView.readAnswer(player.getName())); dealer.deal(player, answer); - if (handsChanged(changed, answer)) { - outputView.printHands(ParticipantDto.from(player)); - } - if (player.isBust()) { - outputView.printBustMessage(); - break; - } - changed = true; - - if (player.isBlackJack()) { - outputView.printBlackJack(); - break; - } + printHandsIfRequired(player, handsChanged, answer); + + handsChanged = true; + turnEnded = isTurnEnded(player, answer); + } + } + + private void printHandsIfRequired(final Player player, final boolean handsChanged, final Answer answer) { + if (shouldShowHands(handsChanged, answer)) { + outputView.printHands(ParticipantDto.from(player)); + } + } + + private boolean isTurnEnded(final Player player, final Answer answer) { + if (player.isBust()) { + outputView.printBust(); + return true; + } + if (player.isBlackJack()) { + outputView.printBlackJack(); + return true; } + return Answer.STAY.equals(answer); } - private boolean handsChanged(final boolean changed, final Answer answer) { - return (Answer.STAY.equals(answer) && !changed) || Answer.HIT.equals(answer); + private boolean shouldShowHands(final boolean handsChanged, final Answer answer) { + return (Answer.STAY.equals(answer) && !handsChanged) || Answer.HIT.equals(answer); } } diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 36846aefbc..325477b2b1 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -22,7 +22,7 @@ public Dealer(final CardDeck cardDeck, final Hands hands) { this.cardDeck = cardDeck; } - public void startDeal(final Players players) { + public void initHands(final Players players) { for (int i = 0; i < INIT_HANDS_SIZE; i++) { players.forEach(player -> player.add(cardDeck.pop())); super.add(cardDeck.pop()); @@ -35,12 +35,13 @@ public void deal(final Player player, final Answer answer) { } } - public int turn() { - int result = 0; + public void deal() { while (handsSum() <= MIN_HANDS_SUM) { super.add(cardDeck.pop()); - result++; } - return result; + } + + public int countAddedHands() { + return handsSize() - INIT_HANDS_SIZE; } } diff --git a/src/main/java/domain/participant/Participant.java b/src/main/java/domain/participant/Participant.java index 713a2a5615..e0b5289310 100644 --- a/src/main/java/domain/participant/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -3,10 +3,13 @@ import domain.Hands; import domain.Result; import domain.card.Card; + import java.util.List; import java.util.Objects; public abstract class Participant { + + private static final int INIT_HANDS_SIZE = 2; private final String name; private final Hands hands; @@ -28,6 +31,10 @@ public boolean isBlackJack() { return hands.isBlackJack(); } + public boolean isHandsChanged() { + return handsSize() != INIT_HANDS_SIZE; + } + public int handsSum() { return hands.sum(); } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index cf7a3a7fe4..65ee8d77cc 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -35,7 +35,7 @@ public void printHands(final ParticipantDto participantDto) { System.out.println(); } - public void printDealerCard() { + public void printDealerTurnMessage() { System.out.println("딜러는 16이하라 한장의 카드를 더 받았습니다."); //TODO 메서드 변경 System.out.println(); } @@ -70,7 +70,7 @@ private String format(final List playerNames) { return String.join(", ", playerNames); } - public void printBustMessage() { + public void printBust() { System.out.println("BUST"); System.out.println(); } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 3fee5de215..c607402a8b 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -1,18 +1,20 @@ package domain; -import static domain.card.Rank.FIVE; -import static domain.card.Shape.HEART; -import static domain.card.Shape.SPADE; - import domain.card.Card; import domain.participant.Dealer; import domain.participant.Player; import domain.participant.Players; -import java.util.List; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.List; + +import static domain.card.Rank.FIVE; +import static domain.card.Rank.NINE; +import static domain.card.Shape.HEART; +import static domain.card.Shape.SPADE; + class DealerTest { @Test @@ -24,7 +26,7 @@ void dealCards() { final Dealer dealer = new Dealer(cardDeck); //when - dealer.startDeal(players); + dealer.initHands(players); //then Assertions.assertThat(players.getPlayers()).allMatch(player -> player.handsSize() == 2); @@ -41,7 +43,7 @@ void addOneCard() { final CardDeck cardDeck = CardDeck.generate(); final Dealer dealer = new Dealer(cardDeck); - dealer.startDeal(players); + dealer.initHands(players); //when dealer.deal(redddy, Answer.HIT); @@ -54,12 +56,29 @@ void addOneCard() { @Test @DisplayName("딜러의 카드의 합이 17이상이 될때까지 카드를 추가한다.") - void dealerTurn() { + void dealerDeal() { //given final Hands sum10 = new Hands(List.of(new Card(FIVE, SPADE), new Card(FIVE, HEART))); final Dealer dealer = new Dealer(CardDeck.generate(), sum10); - //when && then - Assertions.assertThat(dealer.turn()).isPositive(); + //when + dealer.deal(); + + //then + Assertions.assertThat(dealer.countAddedHands()).isPositive(); + } + + @DisplayName("딜러의 카드의 합이 17이상이라면 카드를 추가하지 않는다") + @Test + void dealerNoDeal() { + //given + final Hands sum18 = new Hands(List.of(new Card(NINE, SPADE), new Card(NINE, HEART))); + final Dealer dealer = new Dealer(CardDeck.generate(), sum18); + + //when + dealer.deal(); + + //then + Assertions.assertThat(dealer.countAddedHands()).isZero(); } } From 162927e70cb11243ad7f47cf4088bb5a71c4303b Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 16:46:51 +0900 Subject: [PATCH 063/100] =?UTF-8?q?refactor:=20cardDeckTest=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=A0=91=EA=B7=BC=EC=A0=9C=ED=95=9C?= =?UTF-8?q?=EC=9E=90=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/CardDeckTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/domain/CardDeckTest.java b/src/test/java/domain/CardDeckTest.java index 33294600de..29b4479f21 100644 --- a/src/test/java/domain/CardDeckTest.java +++ b/src/test/java/domain/CardDeckTest.java @@ -4,14 +4,14 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -public class CardDeckTest { +class CardDeckTest { @DisplayName("카드를 52 * 6 만큼 생성한다.") @Test void generate() { - // given & when + // given CardDeck cardDeck = CardDeck.generate(); - // then + // when && then Assertions.assertThat(cardDeck.size()).isEqualTo(52 * 6); } } From 02211ea4c6823849d82fdea9c36334843c6db6ca Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Fri, 8 Mar 2024 16:51:08 +0900 Subject: [PATCH 064/100] =?UTF-8?q?refactor:=20null=EA=B3=BC=20blank=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/PlayerTest.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index d3751f986b..51718f1aee 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -16,12 +16,21 @@ void createPlayerWithName() { .doesNotThrowAnyException(); } - @DisplayName("null이나 공백을 입력하면 예외를 발생시킨다.") + @DisplayName("공백을 입력하면 예외를 발생시킨다.") @ParameterizedTest @ValueSource(strings = {"", " ", " "}) + void BlankInputThrowException(String name) { + Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyPacket())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); + } + + @DisplayName("null을 입력하면 예외를 발생시킨다.") + @ParameterizedTest @NullSource - void nullOrBlankInputThrowException(String name) { + void nullInputThrowException(String name) { Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyPacket())) - .isInstanceOf(IllegalArgumentException.class); + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 유효하지 않은 참여자 이름입니다."); } } From 2d96eecde94af1fbff25e795ff4ce2d273690432 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Fri, 8 Mar 2024 16:54:33 +0900 Subject: [PATCH 065/100] =?UTF-8?q?refactor:=20=EC=A4=91=EB=B3=B5=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/PlayersTest.java | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index 3c41e205aa..f23bef6e3b 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -17,6 +17,14 @@ class PlayersTest { + final Hands bustHands = new Hands( + List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE), + new Card(Rank.TEN, Shape.HEART))); + + final Hands noBustHands = new Hands( + List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE))); + + @Test @DisplayName("참여자 이름 중복시 예외가 발생한다.") void duplicatePlayerName() { @@ -50,16 +58,9 @@ void invalidPlayersSize(final List names) { @DisplayName("참가자 중 버스트 되지 않은 참가자가 있다면 False를 반환한다.") void isAllBustFalse() { //given - Hands bustHands = new Hands( - List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE), - new Card(Rank.TEN, Shape.HEART))); - - Hands noBustHands = new Hands( - List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE))); - - Player player1 = new Player("레디", bustHands); - Player player2 = new Player("제제", noBustHands); - Players players = new Players(List.of(player1, player2)); + final Player player1 = new Player("레디", bustHands); + final Player player2 = new Player("제제", noBustHands); + final Players players = new Players(List.of(player1, player2)); //when && then Assertions.assertThat(players.isAllBust()).isFalse(); @@ -69,10 +70,6 @@ void isAllBustFalse() { @DisplayName("모든 참가자가 버스트되면 True를 반환한다.") void isAllBustTrue() { //given - Hands bustHands = new Hands( - List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE), - new Card(Rank.TEN, Shape.HEART))); - Player player1 = new Player("레디", bustHands); Player player2 = new Player("제제", bustHands); Player player3 = new Player("수달", bustHands); From 32b1496f0b63e9a225f5dcc2016ea2d1df345be1 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Fri, 8 Mar 2024 16:56:05 +0900 Subject: [PATCH 066/100] =?UTF-8?q?style:=20static=20=EB=B3=80=EC=88=98=20?= =?UTF-8?q?import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/PlayersTest.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index f23bef6e3b..83adb52be4 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -1,8 +1,6 @@ package domain; import domain.card.Card; -import domain.card.Rank; -import domain.card.Shape; import domain.participant.Player; import domain.participant.Players; import org.assertj.core.api.Assertions; @@ -15,14 +13,19 @@ import java.util.List; import java.util.stream.Stream; +import static domain.card.Rank.JACK; +import static domain.card.Rank.TEN; +import static domain.card.Shape.HEART; +import static domain.card.Shape.SPADE; + class PlayersTest { final Hands bustHands = new Hands( - List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE), - new Card(Rank.TEN, Shape.HEART))); + List.of(new Card(JACK, HEART), new Card(TEN, SPADE), + new Card(TEN, HEART))); final Hands noBustHands = new Hands( - List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.TEN, Shape.SPADE))); + List.of(new Card(JACK, HEART), new Card(TEN, SPADE))); @Test From d0c04515857375087466970cc9f1a266166f843e Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 17:00:04 +0900 Subject: [PATCH 067/100] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=EB=82=B4=EC=97=90=20=EC=A4=91=EB=B3=B5?= =?UTF-8?q?=EB=90=98=EB=8A=94=20=EB=B3=80=EC=88=98=EB=A5=BC=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=EB=A1=9C=20=EC=84=A0=EC=96=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/GameTest.java | 77 ++++++++---------------------- 1 file changed, 21 insertions(+), 56 deletions(-) diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index 32d8959af6..adeddbb279 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -1,50 +1,42 @@ package domain; -import domain.card.Card; -import domain.participant.Dealer; -import domain.participant.Player; -import domain.participant.Players; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; -import java.util.Map; - import static domain.Result.LOSE; import static domain.Result.TIE; import static domain.Result.WIN; import static domain.card.Rank.ACE; import static domain.card.Rank.EIGHT; -import static domain.card.Rank.FIVE; -import static domain.card.Rank.FOUR; import static domain.card.Rank.JACK; -import static domain.card.Rank.KING; -import static domain.card.Rank.QUEEN; import static domain.card.Rank.SEVEN; import static domain.card.Rank.TEN; -import static domain.card.Rank.THREE; import static domain.card.Rank.TWO; import static domain.card.Shape.CLOVER; import static domain.card.Shape.DIAMOND; import static domain.card.Shape.HEART; import static domain.card.Shape.SPADE; +import domain.card.Card; +import domain.participant.Dealer; +import domain.participant.Player; +import domain.participant.Players; +import java.util.List; +import java.util.Map; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + class GameTest { + final Hands sum18 = new Hands(List.of(new Card(EIGHT, CLOVER), new Card(TEN, DIAMOND))); + final Hands sum20 = new Hands(List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), new Card(ACE, SPADE))); + final Hands sum21 = new Hands(List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); + final Hands bustHands = new Hands(List.of(new Card(EIGHT, DIAMOND), new Card(TWO, DIAMOND), + new Card(TWO, DIAMOND), new Card(JACK, CLOVER))); + + @Test @DisplayName("참여자의 승패무를 판단한다.") void playerResult() { //given - Hands sum18 = new Hands(List.of(new Card(EIGHT, CLOVER), - new Card(TEN, DIAMOND))); // 18 - Hands sum21 = new Hands( - List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); - - Hands sum20 = new Hands( - List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), - new Card(ACE, SPADE))); - Player loser = new Player("레디", sum18); Player winner = new Player("제제", sum21); Player tier = new Player("수달", sum20); @@ -63,16 +55,6 @@ void playerResult() { @Test void dealerResult() { // given - Hands sum18 = new Hands(List.of(new Card(EIGHT, CLOVER), - new Card(TEN, DIAMOND))); // 18 - - Hands sum21 = new Hands( - List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); - - Hands sum20 = new Hands( - List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), - new Card(ACE, SPADE))); - Player loser1 = new Player("레디", sum18); Player loser2 = new Player("피케이", sum18); Player winner = new Player("제제", sum21); @@ -94,27 +76,10 @@ void dealerResult() { @DisplayName("딜러가 버스트일때 참여자가 버스트가 아니면 WIN") void all() { //given - Hands dealerBustHands = new Hands( - List.of(new Card(EIGHT, DIAMOND), new Card(TWO, DIAMOND), new Card(TWO, DIAMOND), - new Card(JACK, CLOVER))); - Dealer bustDealer = new Dealer(CardDeck.generate(), dealerBustHands); - - Hands winner1PlayerHandsSum19 = new Hands( - List.of(new Card(FIVE, DIAMOND), new Card(THREE, CLOVER), new Card(SEVEN, HEART), - new Card(FOUR, DIAMOND)) - ); - - Player winner1 = new Player("레디", winner1PlayerHandsSum19); - - Hands winner2PlayerHandsSum20 = new Hands( - List.of(new Card(QUEEN, CLOVER), new Card(KING, SPADE)) - ); - Player winner2 = new Player("브라운", winner2PlayerHandsSum20); - - Hands loserPlayerHandsSum22 = new Hands( - List.of(new Card(SEVEN, CLOVER), new Card(QUEEN, HEART), new Card(FIVE, SPADE)) - ); - Player loser = new Player("제제", loserPlayerHandsSum22); + Dealer bustDealer = new Dealer(CardDeck.generate(), bustHands); + Player winner1 = new Player("레디", sum18); + Player winner2 = new Player("브라운", sum20); + Player loser = new Player("제제", bustHands); Players players = new Players(List.of(winner1, winner2, loser)); From 23bdf6967d27dfce30ddde95eab90f683dc6ff35 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 17:06:17 +0900 Subject: [PATCH 068/100] =?UTF-8?q?refactor:=20outputView=20FORM=20?= =?UTF-8?q?=EC=83=81=EC=88=98static=20=EC=9C=BC=EB=A1=9C=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 --- src/main/java/view/OutputView.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 65ee8d77cc..19a6a63882 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -5,15 +5,14 @@ import dto.DealerHandsDto; import dto.ParticipantDto; import dto.ParticipantsDto; - import java.util.List; import java.util.Map; public class OutputView { - private final String FORM = "%s카드: %s"; - private final String TOTAL_SUM_FORM = "%s 카드: %s - 결과: %d"; - private final String RESULT_FORM = "%s: %s"; + private static final String FORM = "%s카드: %s"; + private static final String TOTAL_SUM_FORM = "%s 카드: %s - 결과: %d"; + private static final String RESULT_FORM = "%s: %s"; public void printStartDeal(final DealerHandsDto dealerHandsDto, final ParticipantsDto participantsDto) { final String dealerCard = dealerHandsDto.getDisplayedCard(); @@ -36,13 +35,14 @@ public void printHands(final ParticipantDto participantDto) { } public void printDealerTurnMessage() { - System.out.println("딜러는 16이하라 한장의 카드를 더 받았습니다."); //TODO 메서드 변경 + System.out.println("딜러는 16이하라 한장의 카드를 더 받았습니다."); System.out.println(); } public void printHandsResult(final ParticipantsDto participantsDto) { for (ParticipantDto participantDto : participantsDto.getPlayers()) { - System.out.printf(TOTAL_SUM_FORM, participantDto.getName(), format(participantDto.getCards()), participantDto.getTotalSum()); + System.out.printf(TOTAL_SUM_FORM, participantDto.getName(), format(participantDto.getCards()), + participantDto.getTotalSum()); System.out.println(); } } @@ -60,7 +60,7 @@ public void printGameResult(final Map dealerResult, final Map

dealerResult) { StringBuilder stringBuilder = new StringBuilder(); for (Map.Entry entry : dealerResult.entrySet()) { - stringBuilder.append(entry.getValue() + entry.getKey().getValue() + " "); + stringBuilder.append(entry.getValue()).append(entry.getKey().getValue()).append(" "); } return stringBuilder.toString(); From 4e2a246b5efc923d83c85c2bb73b2171fd411568 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 17:07:32 +0900 Subject: [PATCH 069/100] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=ED=95=84=EB=93=9C=20=EB=B0=8F?= =?UTF-8?q?=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/participant/Participant.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/domain/participant/Participant.java b/src/main/java/domain/participant/Participant.java index e0b5289310..33389107b2 100644 --- a/src/main/java/domain/participant/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -3,13 +3,11 @@ import domain.Hands; import domain.Result; import domain.card.Card; - import java.util.List; import java.util.Objects; public abstract class Participant { - private static final int INIT_HANDS_SIZE = 2; private final String name; private final Hands hands; @@ -22,7 +20,6 @@ public void add(final Card card) { hands.add(card); } - //TODO 메서드가 조잡해요 public boolean isBust() { return hands.isBust(); } @@ -31,10 +28,6 @@ public boolean isBlackJack() { return hands.isBlackJack(); } - public boolean isHandsChanged() { - return handsSize() != INIT_HANDS_SIZE; - } - public int handsSum() { return hands.sum(); } @@ -64,11 +57,9 @@ public boolean equals(final Object target) { if (this == target) { return true; } - if (!(target instanceof Participant participant)) { return false; } - return Objects.equals(name, participant.name) && Objects.equals(hands, participant.hands); } From 4ab6363c402d25279e64f9985eb66318496456b2 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 17:12:22 +0900 Subject: [PATCH 070/100] =?UTF-8?q?style:=20=EC=BD=94=EB=93=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Answer.java | 1 - src/main/java/domain/CardDeck.java | 17 ++++++++--------- src/main/java/domain/Game.java | 5 ++--- src/main/java/domain/Result.java | 3 +-- src/main/java/domain/participant/Players.java | 1 - src/main/java/dto/DealerHandsDto.java | 1 - 6 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/java/domain/Answer.java b/src/main/java/domain/Answer.java index d8c855b9bb..2204468090 100644 --- a/src/main/java/domain/Answer.java +++ b/src/main/java/domain/Answer.java @@ -12,7 +12,6 @@ public enum Answer { this.value = value; } - public static Answer from(final String value) { return Arrays.stream(Answer.values()) .filter(answer -> answer.value.equals(value)) diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java index 135729eacb..70cf9106b1 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/CardDeck.java @@ -19,8 +19,6 @@ public CardDeck(final Deque cards) { this.cards = cards; } - //TODO 모르는 사람이 봐도 이해할 수 있도록 수정해보기 - // DECK_SIZE 파라미터로 받아오는 거 고려 public static CardDeck generate() { final List deck = new ArrayList<>(); @@ -32,6 +30,14 @@ public static CardDeck generate() { return new CardDeck(new ArrayDeque<>(deck)); } + public Card pop() { + return cards.pop(); + } + + public int size() { + return cards.size(); + } + private static List generateOneCardDeck() { return Arrays.stream(Shape.values()) .flatMap(shape -> matching(shape).stream()) @@ -44,11 +50,4 @@ private static List matching(final Shape shape) { .toList(); } - public Card pop() { - return cards.pop(); - } - - public int size() { - return cards.size(); - } } diff --git a/src/main/java/domain/Game.java b/src/main/java/domain/Game.java index a65ac67bb5..3e17a298ef 100644 --- a/src/main/java/domain/Game.java +++ b/src/main/java/domain/Game.java @@ -3,13 +3,12 @@ import domain.participant.Dealer; import domain.participant.Player; import domain.participant.Players; - import java.util.EnumMap; import java.util.LinkedHashMap; import java.util.Map; public class Game { - //TODO Map 필드 고려 + private final Dealer dealer; private final Players players; @@ -21,7 +20,7 @@ public Game(final Dealer dealer, final Players players) { public Map getPlayersResult() { final Map playerResult = new LinkedHashMap<>(); - for (Player player : players.getPlayers()) { //TODO getPlayers() 메서드명 고려 + for (Player player : players.getPlayers()) { playerResult.put(player, player.calculateResult(dealer)); } return playerResult; diff --git a/src/main/java/domain/Result.java b/src/main/java/domain/Result.java index 425fb48112..d1f02ad4d4 100644 --- a/src/main/java/domain/Result.java +++ b/src/main/java/domain/Result.java @@ -4,8 +4,7 @@ public enum Result { WIN("승"), LOSE("패"), - TIE("무") - ; + TIE("무"); private final String value; diff --git a/src/main/java/domain/participant/Players.java b/src/main/java/domain/participant/Players.java index 16166c54df..e667a17bc1 100644 --- a/src/main/java/domain/participant/Players.java +++ b/src/main/java/domain/participant/Players.java @@ -1,7 +1,6 @@ package domain.participant; import domain.Hands; - import java.util.List; import java.util.Set; import java.util.function.Consumer; diff --git a/src/main/java/dto/DealerHandsDto.java b/src/main/java/dto/DealerHandsDto.java index 428383c4b9..821b146887 100644 --- a/src/main/java/dto/DealerHandsDto.java +++ b/src/main/java/dto/DealerHandsDto.java @@ -1,7 +1,6 @@ package dto; import domain.participant.Participant; - import java.util.List; public class DealerHandsDto { From 987be12f761f70bb75caee2532e7e33db31282ca Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Fri, 8 Mar 2024 17:18:24 +0900 Subject: [PATCH 071/100] =?UTF-8?q?refactor:=20HandsTest=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=EB=90=98=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/HandsTest.java | 167 +++++++++++----------------- 1 file changed, 62 insertions(+), 105 deletions(-) diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index f15e6d9c8c..748595addd 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -1,11 +1,6 @@ package domain; -import java.util.List; -import java.util.stream.Stream; - import domain.card.Card; -import domain.card.Rank; -import domain.card.Shape; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -13,8 +8,39 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import java.util.List; +import java.util.stream.Stream; + +import static domain.card.Rank.ACE; +import static domain.card.Rank.EIGHT; +import static domain.card.Rank.FIVE; +import static domain.card.Rank.FOUR; +import static domain.card.Rank.JACK; +import static domain.card.Rank.NINE; +import static domain.card.Rank.SEVEN; +import static domain.card.Rank.SIX; +import static domain.card.Rank.TWO; +import static domain.card.Shape.CLOVER; +import static domain.card.Shape.DIAMOND; +import static domain.card.Shape.HEART; +import static domain.card.Shape.SPADE; + class HandsTest { + final Hands sum20Size2 = new Hands( + List.of(new Card(NINE, SPADE), new Card(ACE, SPADE))); + final Hands sum20Size3 = new Hands( + List.of(new Card(FOUR, SPADE), new Card(FIVE, SPADE), + new Card(ACE, SPADE))); + final Hands sum21Size2 = new Hands( + List.of(new Card(JACK, HEART), + new Card(ACE, SPADE))); + final Hands sum22Size3 = new Hands( + List.of(new Card(NINE, HEART), new Card(EIGHT, SPADE), + new Card(FIVE, HEART))); + final Hands blackJack = new Hands( + List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); + @Test @DisplayName("카드를 가지고 있는 객체를 생성한다.") void createPacket() { @@ -29,25 +55,18 @@ void addCard() { final Hands hands = Hands.createEmptyPacket(); //when - hands.add(new Card(Rank.EIGHT, Shape.CLOVER)); + hands.add(new Card(EIGHT, CLOVER)); //then Assertions.assertThat(hands.size()).isEqualTo(1); } @DisplayName("카드의 합을 구한다.") - @ParameterizedTest - @MethodSource("sumParameterProvider") - void sum(final Hands hands, final int expected) { - // given & when - final int result = hands.sum(); - - // then - Assertions.assertThat(result).isEqualTo(expected); + @Test + void sum() { + Assertions.assertThat(sum20Size2.sum()).isEqualTo(20); } - //TODO: 디스플레이네임 수정 필요, 좀더 명확하고, 이해하기 쉽게 - // 코드 중복이 발생한다. @DisplayName("에이스를 11로 계산한다.") @ParameterizedTest @MethodSource("sumAce11ParameterProvider") @@ -74,130 +93,68 @@ void sumAce1(final Hands hands, final int expected) { @Test void isTie() { // given - final Hands sum1 = new Hands( - List.of(new Card(Rank.SEVEN, Shape.SPADE), new Card(Rank.TWO, Shape.SPADE), - new Card(Rank.ACE, Shape.SPADE))); - final Hands sum2 = new Hands( - List.of(new Card(Rank.FOUR, Shape.SPADE), new Card(Rank.FIVE, Shape.SPADE), - new Card(Rank.ACE, Shape.SPADE))); + final Hands dealerSum10Size3 = new Hands( + List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), + new Card(ACE, SPADE))); + final Hands playerSum10Size3 = new Hands( + List.of(new Card(FOUR, SPADE), new Card(FIVE, SPADE), + new Card(ACE, SPADE))); // when && then - Assertions.assertThat(sum1.calculateResult(sum2)).isEqualTo(Result.TIE); + Assertions.assertThat(dealerSum10Size3.calculateResult(playerSum10Size3)).isEqualTo(Result.TIE); } @DisplayName("카드 합이 같은데 카드 갯수가 더 적으면 승리이다.") @Test void isWinBySize() { - // given - final Hands sum20Size2 = new Hands( - List.of(new Card(Rank.NINE, Shape.SPADE), new Card(Rank.ACE, Shape.SPADE))); - final Hands sum20Size3 = new Hands( - List.of(new Card(Rank.FOUR, Shape.SPADE), new Card(Rank.FIVE, Shape.SPADE), - new Card(Rank.ACE, Shape.SPADE))); - - // when && then Assertions.assertThat(sum20Size2.calculateResult(sum20Size3)).isEqualTo(Result.WIN); } @Test - @DisplayName("카드 합이 21이하이면서 승리한다.") //TODO 이름 변경 + @DisplayName("카드 합이 21이하이면서 21에 가까운 카드가 승리한다.") void isWin() { - //given - final Hands sum20 = new Hands( - List.of(new Card(Rank.SEVEN, Shape.SPADE), new Card(Rank.TWO, Shape.SPADE), - new Card(Rank.ACE, Shape.SPADE))); - - final Hands sum21 = new Hands( - List.of(new Card(Rank.JACK, Shape.HEART), - new Card(Rank.ACE, Shape.SPADE))); - - //when && then - Assertions.assertThat(sum21.calculateResult(sum20)).isEqualTo(Result.WIN); - } - - - @Test - @DisplayName("카드 합이 21이하이면서 패배한다.") - void isLose() { - final Hands sum20 = new Hands( - List.of(new Card(Rank.SEVEN, Shape.SPADE), new Card(Rank.TWO, Shape.SPADE), - new Card(Rank.ACE, Shape.SPADE))); - - final Hands sum21 = new Hands( - List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.ACE, Shape.SPADE))); - - //when && then - Assertions.assertThat(sum20.calculateResult(sum21)).isEqualTo(Result.LOSE); + Assertions.assertThat(sum21Size2.calculateResult(sum20Size2)).isEqualTo(Result.WIN); + Assertions.assertThat(sum20Size2.calculateResult(sum21Size2)).isEqualTo(Result.LOSE); } @Test @DisplayName("카드 합이 21초과이면 패배한다.") void isLoseWhenCardSumGreater21() { - //given - final Hands sum18 = new Hands( - List.of(new Card(Rank.SEVEN, Shape.HEART), new Card(Rank.ACE, Shape.SPADE))); - - final Hands sum22 = new Hands( - List.of(new Card(Rank.NINE, Shape.HEART), new Card(Rank.EIGHT, Shape.SPADE), - new Card(Rank.FIVE, Shape.HEART))); - - //when && then - Assertions.assertThat(sum22.calculateResult(sum18)).isEqualTo(Result.LOSE); + Assertions.assertThat(sum22Size3.calculateResult(sum20Size2)).isEqualTo(Result.LOSE); } @Test @DisplayName("blackjack이 이긴다.") void isWinBlackJack() { - //given - final Hands blackJack = new Hands( - List.of(new Card(Rank.JACK, Shape.HEART), new Card(Rank.ACE, Shape.SPADE))); - - final Hands sum19 = new Hands( - List.of(new Card(Rank.NINE, Shape.HEART), new Card(Rank.TEN, Shape.SPADE))); - - //when && then - Assertions.assertThat(blackJack.calculateResult(sum19)).isEqualTo(Result.WIN); - Assertions.assertThat(sum19.calculateResult(blackJack)).isEqualTo(Result.LOSE); - } - - static Stream sumParameterProvider() { - return Stream.of( - Arguments.of(new Hands(List.of(new Card(Rank.TWO, Shape.HEART), - new Card(Rank.EIGHT, Shape.SPADE), - new Card(Rank.JACK, Shape.CLOVER))), - 20), - Arguments.of(new Hands(List.of(new Card(Rank.THREE, Shape.DIAMOND), - new Card(Rank.NINE, Shape.CLOVER), - new Card(Rank.NINE, Shape.CLOVER))), - 21) - ); + Assertions.assertThat(blackJack.calculateResult(sum20Size2)).isEqualTo(Result.WIN); + Assertions.assertThat(sum20Size2.calculateResult(blackJack)).isEqualTo(Result.LOSE); } static Stream sumAce11ParameterProvider() { return Stream.of( - Arguments.of(new Hands(List.of(new Card(Rank.ACE, Shape.HEART), - new Card(Rank.EIGHT, Shape.SPADE), - new Card(Rank.TWO, Shape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(ACE, HEART), + new Card(EIGHT, SPADE), + new Card(TWO, CLOVER))), 21), - Arguments.of(new Hands(List.of(new Card(Rank.ACE, Shape.DIAMOND), - new Card(Rank.TWO, Shape.CLOVER), - new Card(Rank.FOUR, Shape.CLOVER), - new Card(Rank.TWO, Shape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(ACE, DIAMOND), + new Card(TWO, CLOVER), + new Card(FOUR, CLOVER), + new Card(TWO, CLOVER))), 19) ); } static Stream sumAce1ParameterProvider() { return Stream.of( - Arguments.of(new Hands(List.of(new Card(Rank.ACE, Shape.HEART), - new Card(Rank.NINE, Shape.SPADE), - new Card(Rank.NINE, Shape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(ACE, HEART), + new Card(NINE, SPADE), + new Card(NINE, CLOVER))), 19), - Arguments.of(new Hands(List.of(new Card(Rank.ACE, Shape.DIAMOND), - new Card(Rank.EIGHT, Shape.CLOVER), - new Card(Rank.FIVE, Shape.CLOVER), - new Card(Rank.SIX, Shape.CLOVER))), + Arguments.of(new Hands(List.of(new Card(ACE, DIAMOND), + new Card(EIGHT, CLOVER), + new Card(FIVE, CLOVER), + new Card(SIX, CLOVER))), 20) ); } From 88665a873fa2650debe21b49557a2a5428f8015d Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 17:20:17 +0900 Subject: [PATCH 072/100] =?UTF-8?q?refactor:=20ParticipantDto=20=EB=A0=88?= =?UTF-8?q?=EC=BD=94=EB=93=9C=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/dto/ParticipantDto.java | 23 +---------------------- src/main/java/dto/ParticipantsDto.java | 4 ++-- src/main/java/view/OutputView.java | 8 ++++---- 3 files changed, 7 insertions(+), 28 deletions(-) diff --git a/src/main/java/dto/ParticipantDto.java b/src/main/java/dto/ParticipantDto.java index c230e5d94d..795bae6b57 100644 --- a/src/main/java/dto/ParticipantDto.java +++ b/src/main/java/dto/ParticipantDto.java @@ -3,30 +3,9 @@ import domain.participant.Participant; import java.util.List; -public class ParticipantDto { - private final String name; - private final List cards; - private final int totalSum; - - public ParticipantDto(final String name, final List cards, final int totalSum) { - this.name = name; - this.cards = cards; - this.totalSum = totalSum; - } +public record ParticipantDto(String name, List cards, int totalSum) { public static ParticipantDto from(final Participant player) { return new ParticipantDto(player.getName(), player.getCardNames(), player.handsSum()); } - - public String getName() { - return name; - } - - public List getCards() { - return cards; - } - - public int getTotalSum() { - return totalSum; - } } diff --git a/src/main/java/dto/ParticipantsDto.java b/src/main/java/dto/ParticipantsDto.java index 7218c9557a..c7a2984c4a 100644 --- a/src/main/java/dto/ParticipantsDto.java +++ b/src/main/java/dto/ParticipantsDto.java @@ -5,7 +5,7 @@ import java.util.ArrayList; import java.util.List; -public class ParticipantsDto { // TODO: parit dto +public class ParticipantsDto { private final List players; @@ -32,7 +32,7 @@ public static ParticipantsDto of(final Players players) { public List getNames() { return players.stream() - .map(ParticipantDto::getName) + .map(ParticipantDto::name) .toList(); } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 19a6a63882..8388bc65ed 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -23,14 +23,14 @@ public void printStartDeal(final DealerHandsDto dealerHandsDto, final Participan System.out.println("딜러: " + dealerCard); for (ParticipantDto participantDto : participantsDto.getPlayers()) { - System.out.printf(FORM, participantDto.getName(), format(participantDto.getCards())); + System.out.printf(FORM, participantDto.name(), format(participantDto.cards())); System.out.println(); } System.out.println(); } public void printHands(final ParticipantDto participantDto) { - System.out.printf(FORM, participantDto.getName(), format(participantDto.getCards())); + System.out.printf(FORM, participantDto.name(), format(participantDto.cards())); System.out.println(); } @@ -41,8 +41,8 @@ public void printDealerTurnMessage() { public void printHandsResult(final ParticipantsDto participantsDto) { for (ParticipantDto participantDto : participantsDto.getPlayers()) { - System.out.printf(TOTAL_SUM_FORM, participantDto.getName(), format(participantDto.getCards()), - participantDto.getTotalSum()); + System.out.printf(TOTAL_SUM_FORM, participantDto.name(), format(participantDto.cards()), + participantDto.totalSum()); System.out.println(); } } From 7977057f2eb08d1c2e7b5704789f54e0533462e2 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Fri, 8 Mar 2024 17:27:03 +0900 Subject: [PATCH 073/100] =?UTF-8?q?refactor:=20Hands=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=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/domain/Hands.java | 46 +++++++++++++++++------------- src/test/java/domain/GameTest.java | 21 +++++++------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 3c158449fd..0564a5d922 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -1,6 +1,7 @@ package domain; import domain.card.Card; + import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -8,6 +9,7 @@ public class Hands { private static final int BLACK_JACK = 21; + public static final int EXTRA_ACE_VALUE = 10; private final List cards; @@ -24,26 +26,7 @@ public int sum() { .mapToInt(Card::getCardNumber) .sum(); - if (hasAce() && total + 10 <= BLACK_JACK) { //TODO 메서드분리 - return total + 10; - } - - return total; - } - - public Result calculateResult(final Hands target) { - if (this.sum() >= target.sum() && this.sum() <= BLACK_JACK) { - if (this.size() == target.size() && this.sum() == target.sum()) { - return Result.TIE; - } - - return Result.WIN; - } - - if (!this.isBust() && target.isBust()) { - return Result.WIN; - } - return Result.LOSE; + return calculateTotalByAce(total); } public void add(final Card card) { @@ -73,6 +56,29 @@ public List getCards() { .toList(); } + public Result calculateResult(final Hands target) { + if (this.sum() >= target.sum() && this.sum() <= BLACK_JACK) { + if (this.size() == target.size() && this.sum() == target.sum()) { + return Result.TIE; + } + + return Result.WIN; + } + + if (!this.isBust() && target.isBust()) { + return Result.WIN; + } + return Result.LOSE; + } + + private int calculateTotalByAce(final int total) { + if (hasAce() && total + EXTRA_ACE_VALUE <= BLACK_JACK) { + return total + EXTRA_ACE_VALUE; + } + + return total; + } + @Override public boolean equals(final Object target) { if (this == target) { diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index adeddbb279..394eff3601 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -1,5 +1,16 @@ package domain; +import domain.card.Card; +import domain.participant.Dealer; +import domain.participant.Player; +import domain.participant.Players; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + import static domain.Result.LOSE; import static domain.Result.TIE; import static domain.Result.WIN; @@ -14,16 +25,6 @@ import static domain.card.Shape.HEART; import static domain.card.Shape.SPADE; -import domain.card.Card; -import domain.participant.Dealer; -import domain.participant.Player; -import domain.participant.Players; -import java.util.List; -import java.util.Map; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - class GameTest { final Hands sum18 = new Hands(List.of(new Card(EIGHT, CLOVER), new Card(TEN, DIAMOND))); From 995d8649e769c9d08ff85c23859c9191689412c0 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 8 Mar 2024 17:33:51 +0900 Subject: [PATCH 074/100] =?UTF-8?q?refactor:=20calculateResult=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=9D=B8=EB=8D=B4=ED=8A=B8=20=EC=A4=84?= =?UTF-8?q?=EC=9D=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Hands.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 0564a5d922..1f4fddc587 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -1,7 +1,6 @@ package domain; import domain.card.Card; - import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -57,15 +56,13 @@ public List getCards() { } public Result calculateResult(final Hands target) { - if (this.sum() >= target.sum() && this.sum() <= BLACK_JACK) { - if (this.size() == target.size() && this.sum() == target.sum()) { - return Result.TIE; - } - + if (!this.isBust() && target.isBust()) { return Result.WIN; } - - if (!this.isBust() && target.isBust()) { + if (this.sum() == target.sum() && this.size() == target.size() && !this.isBust()) { + return Result.TIE; + } + if (this.sum() >= target.sum() && this.sum() <= BLACK_JACK) { return Result.WIN; } return Result.LOSE; From 2d822e9b1bdc657bd9991a73d867887445c4e73e Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 13:52:14 +0900 Subject: [PATCH 075/100] =?UTF-8?q?docs:=20=EB=A6=AC=ED=8C=A9=ED=84=B0?= =?UTF-8?q?=EB=A7=81=20=EB=AA=A9=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/README.md b/docs/README.md index de4c45d7af..5602ad332f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -61,3 +61,26 @@ * [x] 딜러의 승패무를 확인한다. * [x] 참여자의 승패무를 확인한다. * [x] 게임 결과를 출력한다. + +--- + +## 리팩터링 목록 + +* [ ] 추상 클래스 활용 +* [ ] 카드가 비어진 경우 예외 발생 +* [ ] createEmptyPacket 메서드명 수정 +* [ ] 블랙잭인경우 바로 게임 종료 +* [ ] model 안에 있는 view를 어떻게 해야할까 + * Rank, Shape +* [ ] 플레이어의 이름 딜러 불가 +* [ ] Name 객체 포장하기 +* [ ] CardDeck 스토리지 유닛으로 변경 +* [ ] Result 함수형 프로그래밍 사용 +* [ ] TestFixture 사용 +* [ ] Answer 필드값 및 stream 제거 +* [ ] 패키지 정리 +* [ ] 메서드 컨벤션 작성 및 확인 +* [ ] final 컨벤션 통일 +* [ ] CardDeck 생성자 닫기 +* [ ] CardDeck 구조 변경 +* [ ] Game 메서드 변경 Dto 변경 From 405ac6b35e5c68c3303d48ea6603c3a9281d53fa Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 13:53:13 +0900 Subject: [PATCH 076/100] =?UTF-8?q?refactor:=20createEmptyPacket=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20createEmptyHands?= =?UTF-8?q?=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/domain/Hands.java | 2 +- src/main/java/domain/participant/Dealer.java | 2 +- src/main/java/domain/participant/Players.java | 2 +- src/test/java/domain/DealerTest.java | 4 ++-- src/test/java/domain/HandsTest.java | 4 ++-- src/test/java/domain/PlayerTest.java | 6 +++--- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/README.md b/docs/README.md index 5602ad332f..07da087b89 100644 --- a/docs/README.md +++ b/docs/README.md @@ -68,7 +68,7 @@ * [ ] 추상 클래스 활용 * [ ] 카드가 비어진 경우 예외 발생 -* [ ] createEmptyPacket 메서드명 수정 +* [x] createEmptyPacket 메서드명 수정 * [ ] 블랙잭인경우 바로 게임 종료 * [ ] model 안에 있는 view를 어떻게 해야할까 * Rank, Shape diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 1f4fddc587..4b4a78cc8d 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -16,7 +16,7 @@ public Hands(final List cards) { this.cards = new ArrayList<>(cards); } - public static Hands createEmptyPacket() { + public static Hands createEmptyHands() { return new Hands(new ArrayList<>()); } diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 325477b2b1..6de11e3c0a 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -13,7 +13,7 @@ public class Dealer extends Participant { private final CardDeck cardDeck; public Dealer(final CardDeck cardDeck) { - super(NAME, Hands.createEmptyPacket()); + super(NAME, Hands.createEmptyHands()); this.cardDeck = cardDeck; } diff --git a/src/main/java/domain/participant/Players.java b/src/main/java/domain/participant/Players.java index e667a17bc1..4c456de94f 100644 --- a/src/main/java/domain/participant/Players.java +++ b/src/main/java/domain/participant/Players.java @@ -34,7 +34,7 @@ public boolean isAllBust() { private static List mapToPlayers(final List names) { return names.stream() .map(String::trim) - .map(name -> new Player(name, Hands.createEmptyPacket())) + .map(name -> new Player(name, Hands.createEmptyHands())) .toList(); } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index c607402a8b..d8902b4e55 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -36,8 +36,8 @@ void dealCards() { @DisplayName("참여자의 답변이 y라면 카드를 한장 추가한다.") void addOneCard() { //given - final Player redddy = new Player("레디", Hands.createEmptyPacket()); - final Player zeze = new Player("제제", Hands.createEmptyPacket()); + final Player redddy = new Player("레디", Hands.createEmptyHands()); + final Player zeze = new Player("제제", Hands.createEmptyHands()); final Players players = new Players(List.of(redddy, zeze)); diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index 748595addd..3c28f2b4c0 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -44,7 +44,7 @@ class HandsTest { @Test @DisplayName("카드를 가지고 있는 객체를 생성한다.") void createPacket() { - Assertions.assertThatCode(Hands::createEmptyPacket) + Assertions.assertThatCode(Hands::createEmptyHands) .doesNotThrowAnyException(); } @@ -52,7 +52,7 @@ void createPacket() { @DisplayName("카드를 추가한다.") void addCard() { //given - final Hands hands = Hands.createEmptyPacket(); + final Hands hands = Hands.createEmptyHands(); //when hands.add(new Card(EIGHT, CLOVER)); diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index 51718f1aee..dbb06c8cc2 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -12,7 +12,7 @@ class PlayerTest { @DisplayName("이름으로 참여자를 생성한다.") @Test void createPlayerWithName() { - Assertions.assertThatCode(() -> new Player("pobi", Hands.createEmptyPacket())) + Assertions.assertThatCode(() -> new Player("pobi", Hands.createEmptyHands())) .doesNotThrowAnyException(); } @@ -20,7 +20,7 @@ void createPlayerWithName() { @ParameterizedTest @ValueSource(strings = {"", " ", " "}) void BlankInputThrowException(String name) { - Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyPacket())) + Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyHands())) .isInstanceOf(IllegalArgumentException.class) .hasMessage("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); } @@ -29,7 +29,7 @@ void BlankInputThrowException(String name) { @ParameterizedTest @NullSource void nullInputThrowException(String name) { - Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyPacket())) + Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyHands())) .isInstanceOf(IllegalArgumentException.class) .hasMessage("[ERROR] 유효하지 않은 참여자 이름입니다."); } From dfaae333e7ac93911d48e50843cb7a30b4706945 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 14:00:58 +0900 Subject: [PATCH 077/100] =?UTF-8?q?feat:=20=EC=B9=B4=EB=93=9C=EB=8D=B1?= =?UTF-8?q?=EC=9D=B4=20=EB=B9=84=EC=97=88=EC=9D=84=EB=95=8C=20=EA=BA=BC?= =?UTF-8?q?=EB=82=B8=20=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- src/main/java/domain/CardDeck.java | 6 ++++-- src/test/java/domain/CardDeckTest.java | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/docs/README.md b/docs/README.md index 07da087b89..29a28247b5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -67,7 +67,7 @@ ## 리팩터링 목록 * [ ] 추상 클래스 활용 -* [ ] 카드가 비어진 경우 예외 발생 +* [x] 카드덱이 비었을때 꺼낸 경우 예외 발생 * [x] createEmptyPacket 메서드명 수정 * [ ] 블랙잭인경우 바로 게임 종료 * [ ] model 안에 있는 view를 어떻게 해야할까 @@ -81,6 +81,6 @@ * [ ] 패키지 정리 * [ ] 메서드 컨벤션 작성 및 확인 * [ ] final 컨벤션 통일 -* [ ] CardDeck 생성자 닫기 +* [x] CardDeck 생성자 닫기 * [ ] CardDeck 구조 변경 * [ ] Game 메서드 변경 Dto 변경 diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java index 70cf9106b1..9f9e36e7d1 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/CardDeck.java @@ -15,7 +15,7 @@ public class CardDeck { private final Deque cards; - public CardDeck(final Deque cards) { + private CardDeck(final Deque cards) { this.cards = cards; } @@ -31,6 +31,9 @@ public static CardDeck generate() { } public Card pop() { + if (cards.isEmpty()) { + throw new IllegalStateException("[ERROR] 카드덱이 비어있습니다."); + } return cards.pop(); } @@ -49,5 +52,4 @@ private static List matching(final Shape shape) { .map(rank -> new Card(rank, shape)) .toList(); } - } diff --git a/src/test/java/domain/CardDeckTest.java b/src/test/java/domain/CardDeckTest.java index 29b4479f21..8bd192aa6c 100644 --- a/src/test/java/domain/CardDeckTest.java +++ b/src/test/java/domain/CardDeckTest.java @@ -14,4 +14,24 @@ void generate() { // when && then Assertions.assertThat(cardDeck.size()).isEqualTo(52 * 6); } + + + @Test + @DisplayName("카드가 없는데 카드를 뽑을 경우 예외가 발생한다.") + void pop() { + //given + CardDeck cardDeck = CardDeck.generate(); + + //when + int cardSize = 52 * 6; + while (cardSize > 0) { + cardDeck.pop(); + cardSize--; + } + + //then + Assertions.assertThatThrownBy(cardDeck::pop) + .isInstanceOf(IllegalStateException.class) + .hasMessage("[ERROR] 카드덱이 비어있습니다."); + } } From aaf03abe7206960a8c10a45eefff8dc1a4b97218 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Mon, 11 Mar 2024 14:26:34 +0900 Subject: [PATCH 078/100] =?UTF-8?q?refactor:=20Name=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=20=ED=8F=AC=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 +-- src/main/java/domain/participant/Dealer.java | 4 +-- src/main/java/domain/participant/Name.java | 31 +++++++++++++++++++ .../java/domain/participant/Participant.java | 7 +++-- src/main/java/domain/participant/Player.java | 20 +----------- src/main/java/domain/participant/Players.java | 22 ++++++------- src/test/java/domain/DealerTest.java | 5 +-- src/test/java/domain/GameTest.java | 21 +++++++------ src/test/java/domain/NameTest.java | 29 +++++++++++++++++ src/test/java/domain/PlayerTest.java | 24 ++------------ src/test/java/domain/PlayersTest.java | 13 ++++---- 11 files changed, 103 insertions(+), 77 deletions(-) create mode 100644 src/main/java/domain/participant/Name.java create mode 100644 src/test/java/domain/NameTest.java diff --git a/docs/README.md b/docs/README.md index 29a28247b5..0e727cb9a1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -71,9 +71,9 @@ * [x] createEmptyPacket 메서드명 수정 * [ ] 블랙잭인경우 바로 게임 종료 * [ ] model 안에 있는 view를 어떻게 해야할까 - * Rank, Shape + * Rank, Shape * [ ] 플레이어의 이름 딜러 불가 -* [ ] Name 객체 포장하기 +* [x] Name 객체 포장하기 * [ ] CardDeck 스토리지 유닛으로 변경 * [ ] Result 함수형 프로그래밍 사용 * [ ] TestFixture 사용 diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 6de11e3c0a..a0276f8d8c 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -13,12 +13,12 @@ public class Dealer extends Participant { private final CardDeck cardDeck; public Dealer(final CardDeck cardDeck) { - super(NAME, Hands.createEmptyHands()); + super(new Name(NAME), Hands.createEmptyHands()); this.cardDeck = cardDeck; } public Dealer(final CardDeck cardDeck, final Hands hands) { - super(NAME, hands); + super(new Name(NAME), hands); this.cardDeck = cardDeck; } diff --git a/src/main/java/domain/participant/Name.java b/src/main/java/domain/participant/Name.java new file mode 100644 index 0000000000..558917c277 --- /dev/null +++ b/src/main/java/domain/participant/Name.java @@ -0,0 +1,31 @@ +package domain.participant; + +public class Name { + private final String value; + + public Name(final String value) { + validate(value); + this.value = value; + } + + public String getValue() { + return value; + } + + private void validate(final String name) { + validateNull(name); + validateBlank(name); + } + + private void validateNull(final String name) { + if (name == null) { + throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 이름입니다."); + } + } + + private void validateBlank(final String name) { + if (name.isBlank()) { + throw new IllegalArgumentException("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); + } + } +} diff --git a/src/main/java/domain/participant/Participant.java b/src/main/java/domain/participant/Participant.java index 33389107b2..6939ecb6c6 100644 --- a/src/main/java/domain/participant/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -3,15 +3,16 @@ import domain.Hands; import domain.Result; import domain.card.Card; + import java.util.List; import java.util.Objects; public abstract class Participant { - private final String name; + private final Name name; private final Hands hands; - protected Participant(final String name, final Hands hands) { + protected Participant(final Name name, final Hands hands) { this.name = name; this.hands = hands; } @@ -45,7 +46,7 @@ public List getCardNames() { } public String getName() { - return name; + return name.getValue(); } public Hands getHands() { diff --git a/src/main/java/domain/participant/Player.java b/src/main/java/domain/participant/Player.java index 4ddbe76ca6..0c2bc54f1a 100644 --- a/src/main/java/domain/participant/Player.java +++ b/src/main/java/domain/participant/Player.java @@ -4,25 +4,7 @@ public class Player extends Participant { - public Player(final String name, final Hands hands) { + public Player(final Name name, final Hands hands) { super(name, hands); - validate(name); - } - - private void validate(final String name) { - validateNull(name); - validateBlank(name); - } - - private void validateNull(final String name) { - if (name == null) { - throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 이름입니다."); - } - } - - private void validateBlank(final String name) { - if (name.isBlank()) { - throw new IllegalArgumentException("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); - } } } diff --git a/src/main/java/domain/participant/Players.java b/src/main/java/domain/participant/Players.java index 4c456de94f..da1f717d97 100644 --- a/src/main/java/domain/participant/Players.java +++ b/src/main/java/domain/participant/Players.java @@ -1,6 +1,7 @@ package domain.participant; import domain.Hands; + import java.util.List; import java.util.Set; import java.util.function.Consumer; @@ -17,9 +18,8 @@ public Players(final List names) { } public static Players from(final List names) { - List playerNames = mapToPlayers(names); - validate(playerNames); - return new Players(playerNames); + validate(names); + return new Players(mapToPlayers(names)); } public void forEach(Consumer action) { @@ -34,23 +34,23 @@ public boolean isAllBust() { private static List mapToPlayers(final List names) { return names.stream() .map(String::trim) - .map(name -> new Player(name, Hands.createEmptyHands())) + .map(name -> new Player(new Name(name), Hands.createEmptyHands())) .toList(); } - private static void validate(final List players) { - validateSize(players); - validateDuplicate(players); + private static void validate(final List names) { + validateSize(names); + validateDuplicate(names); } - private static void validateSize(final List players) { - if (players.size() < MIN_SIZE || MAX_SIZE < players.size()) { + private static void validateSize(final List names) { + if (names.size() < MIN_SIZE || MAX_SIZE < names.size()) { throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 수입니다."); } } - private static void validateDuplicate(final List players) { - if (players.size() != Set.copyOf(players).size()) { + private static void validateDuplicate(final List names) { + if (names.size() != Set.copyOf(names).size()) { throw new IllegalArgumentException("[ERROR] 참여자 이름은 중복될 수 없습니다."); } } diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index d8902b4e55..a35f299af0 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -2,6 +2,7 @@ import domain.card.Card; import domain.participant.Dealer; +import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; import org.assertj.core.api.Assertions; @@ -36,8 +37,8 @@ void dealCards() { @DisplayName("참여자의 답변이 y라면 카드를 한장 추가한다.") void addOneCard() { //given - final Player redddy = new Player("레디", Hands.createEmptyHands()); - final Player zeze = new Player("제제", Hands.createEmptyHands()); + final Player redddy = new Player(new Name("레디"), Hands.createEmptyHands()); + final Player zeze = new Player(new Name("제제"), Hands.createEmptyHands()); final Players players = new Players(List.of(redddy, zeze)); diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java index 394eff3601..03d97de311 100644 --- a/src/test/java/domain/GameTest.java +++ b/src/test/java/domain/GameTest.java @@ -2,6 +2,7 @@ import domain.card.Card; import domain.participant.Dealer; +import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; import org.assertj.core.api.Assertions; @@ -38,9 +39,9 @@ class GameTest { @DisplayName("참여자의 승패무를 판단한다.") void playerResult() { //given - Player loser = new Player("레디", sum18); - Player winner = new Player("제제", sum21); - Player tier = new Player("수달", sum20); + Player loser = new Player(new Name("레디"), sum18); + Player winner = new Player(new Name("제제"), sum21); + Player tier = new Player(new Name("수달"), sum20); Players players = new Players(List.of(loser, winner, tier)); Dealer dealer = new Dealer(CardDeck.generate(), sum20); @@ -56,10 +57,10 @@ void playerResult() { @Test void dealerResult() { // given - Player loser1 = new Player("레디", sum18); - Player loser2 = new Player("피케이", sum18); - Player winner = new Player("제제", sum21); - Player tier = new Player("브라운", sum20); + Player loser1 = new Player(new Name("레디"), sum18); + Player loser2 = new Player(new Name("피케이"), sum18); + Player winner = new Player(new Name("제제"), sum21); + Player tier = new Player(new Name("브라운"), sum20); Players players = new Players(List.of(loser1, loser2, winner, tier)); Dealer dealer = new Dealer(CardDeck.generate(), sum20); @@ -78,9 +79,9 @@ void dealerResult() { void all() { //given Dealer bustDealer = new Dealer(CardDeck.generate(), bustHands); - Player winner1 = new Player("레디", sum18); - Player winner2 = new Player("브라운", sum20); - Player loser = new Player("제제", bustHands); + Player winner1 = new Player(new Name("레디"), sum18); + Player winner2 = new Player(new Name("브라운"), sum20); + Player loser = new Player(new Name("제제"), bustHands); Players players = new Players(List.of(winner1, winner2, loser)); diff --git a/src/test/java/domain/NameTest.java b/src/test/java/domain/NameTest.java new file mode 100644 index 0000000000..52eacd4b2c --- /dev/null +++ b/src/test/java/domain/NameTest.java @@ -0,0 +1,29 @@ +package domain; + +import domain.participant.Name; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; + +class NameTest { + + @DisplayName("공백을 입력하면 예외를 발생시킨다.") + @ParameterizedTest + @ValueSource(strings = {"", " ", " "}) + void BlankInputThrowException(String value) { + Assertions.assertThatThrownBy(() -> new Name(value)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); + } + + @DisplayName("null을 입력하면 예외를 발생시킨다.") + @ParameterizedTest + @NullSource + void nullInputThrowException(String value) { + Assertions.assertThatThrownBy(() -> new Name(value)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 유효하지 않은 참여자 이름입니다."); + } +} diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index dbb06c8cc2..2f3c86f004 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -1,36 +1,16 @@ package domain; +import domain.participant.Name; import domain.participant.Player; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.NullSource; -import org.junit.jupiter.params.provider.ValueSource; class PlayerTest { @DisplayName("이름으로 참여자를 생성한다.") @Test void createPlayerWithName() { - Assertions.assertThatCode(() -> new Player("pobi", Hands.createEmptyHands())) + Assertions.assertThatCode(() -> new Player(new Name("pobi"), Hands.createEmptyHands())) .doesNotThrowAnyException(); } - - @DisplayName("공백을 입력하면 예외를 발생시킨다.") - @ParameterizedTest - @ValueSource(strings = {"", " ", " "}) - void BlankInputThrowException(String name) { - Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyHands())) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); - } - - @DisplayName("null을 입력하면 예외를 발생시킨다.") - @ParameterizedTest - @NullSource - void nullInputThrowException(String name) { - Assertions.assertThatThrownBy(() -> new Player(name, Hands.createEmptyHands())) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("[ERROR] 유효하지 않은 참여자 이름입니다."); - } } diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index 83adb52be4..b3bd1b02f5 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -1,6 +1,7 @@ package domain; import domain.card.Card; +import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; import org.assertj.core.api.Assertions; @@ -61,8 +62,8 @@ void invalidPlayersSize(final List names) { @DisplayName("참가자 중 버스트 되지 않은 참가자가 있다면 False를 반환한다.") void isAllBustFalse() { //given - final Player player1 = new Player("레디", bustHands); - final Player player2 = new Player("제제", noBustHands); + final Player player1 = new Player(new Name("레디"), bustHands); + final Player player2 = new Player(new Name("제제"), noBustHands); final Players players = new Players(List.of(player1, player2)); //when && then @@ -73,10 +74,10 @@ void isAllBustFalse() { @DisplayName("모든 참가자가 버스트되면 True를 반환한다.") void isAllBustTrue() { //given - Player player1 = new Player("레디", bustHands); - Player player2 = new Player("제제", bustHands); - Player player3 = new Player("수달", bustHands); - Player player4 = new Player("피케이", bustHands); + Player player1 = new Player(new Name("레디"), bustHands); + Player player2 = new Player(new Name("제제"), bustHands); + Player player3 = new Player(new Name("수달"), bustHands); + Player player4 = new Player(new Name("피케이"), bustHands); Players players = new Players(List.of(player1, player2, player3, player4)); From 017e6a00e3d3a43976c387d957eac165253329e7 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 17:27:23 +0900 Subject: [PATCH 079/100] =?UTF-8?q?refactor:=20=EC=8A=B9=ED=8C=A8=EB=AC=B4?= =?UTF-8?q?=20=ED=8C=90=EB=8B=A8=EC=9D=84=20Result=EC=9D=98=20=EC=B1=85?= =?UTF-8?q?=EC=9E=84=EC=9C=BC=EB=A1=9C=20=EC=9D=B4=EB=8F=99=20(=EC=9E=AC?= =?UTF-8?q?=EB=AF=B8=EC=9E=88=EA=B2=8C=20BiPredicate=20=ED=99=9C=EC=9A=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Game.java | 4 +- src/main/java/domain/Hands.java | 13 +---- src/main/java/domain/Result.java | 34 +++++++++++-- src/test/java/domain/HandsTest.java | 76 ++++------------------------ src/test/java/domain/ResultTest.java | 76 ++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 84 deletions(-) create mode 100644 src/test/java/domain/ResultTest.java diff --git a/src/main/java/domain/Game.java b/src/main/java/domain/Game.java index 3e17a298ef..7e550cf606 100644 --- a/src/main/java/domain/Game.java +++ b/src/main/java/domain/Game.java @@ -28,11 +28,9 @@ public Map getPlayersResult() { public Map getDealerResult() { Map dealerResult = new EnumMap<>(Result.class); - for (Result value : getPlayersResult().values()) { Result reversed = value.reverse(); - Integer orDefault = dealerResult.getOrDefault(reversed, 0); - dealerResult.put(reversed, orDefault + 1); + dealerResult.put(reversed, dealerResult.getOrDefault(reversed, 0) + 1); } return dealerResult; } diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 4b4a78cc8d..f25c1b2ccb 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -8,7 +8,7 @@ public class Hands { private static final int BLACK_JACK = 21; - public static final int EXTRA_ACE_VALUE = 10; + private static final int EXTRA_ACE_VALUE = 10; private final List cards; @@ -56,16 +56,7 @@ public List getCards() { } public Result calculateResult(final Hands target) { - if (!this.isBust() && target.isBust()) { - return Result.WIN; - } - if (this.sum() == target.sum() && this.size() == target.size() && !this.isBust()) { - return Result.TIE; - } - if (this.sum() >= target.sum() && this.sum() <= BLACK_JACK) { - return Result.WIN; - } - return Result.LOSE; + return Result.calculateOf(this, target); } private int calculateTotalByAce(final int total) { diff --git a/src/main/java/domain/Result.java b/src/main/java/domain/Result.java index d1f02ad4d4..944f905757 100644 --- a/src/main/java/domain/Result.java +++ b/src/main/java/domain/Result.java @@ -1,15 +1,34 @@ package domain; +import java.util.Arrays; +import java.util.function.BiPredicate; + public enum Result { - WIN("승"), - LOSE("패"), - TIE("무"); + WIN("승", Result::winningCondition), + TIE("무", Result::tieCondition), + LOSE("패", Result::loseCondition); private final String value; + private final BiPredicate condition; - Result(final String value) { + Result(final String value, final BiPredicate condition) { this.value = value; + this.condition = condition; + } + + private static boolean winningCondition(final Hands hands, final Hands target) { + return (!hands.isBust() && target.isBust()) + || (hands.sum() > target.sum() && !hands.isBust()) + || (hands.sum() == target.sum() && hands.size() < target.size() && !hands.isBust()); + } + + private static boolean tieCondition(final Hands hands, final Hands target) { + return hands.sum() == target.sum() && hands.size() == target.size() && !hands.isBust(); + } + + private static boolean loseCondition(final Hands hands, final Hands target) { + return hands.isBust() || hands.sum() < target.sum() || !target.isBust(); } public Result reverse() { @@ -22,6 +41,13 @@ public Result reverse() { return TIE; } + public static Result calculateOf(final Hands hands, final Hands target) { + return Arrays.stream(Result.values()) + .filter(result -> result.condition.test(hands, target)) + .findFirst() + .orElseThrow(); + } + public String getValue() { return value; } diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index 3c28f2b4c0..c9b2e8406c 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -1,23 +1,10 @@ package domain; -import domain.card.Card; -import org.assertj.core.api.Assertions; -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.List; -import java.util.stream.Stream; - import static domain.card.Rank.ACE; import static domain.card.Rank.EIGHT; import static domain.card.Rank.FIVE; import static domain.card.Rank.FOUR; -import static domain.card.Rank.JACK; import static domain.card.Rank.NINE; -import static domain.card.Rank.SEVEN; import static domain.card.Rank.SIX; import static domain.card.Rank.TWO; import static domain.card.Shape.CLOVER; @@ -25,21 +12,20 @@ import static domain.card.Shape.HEART; import static domain.card.Shape.SPADE; +import domain.card.Card; +import java.util.List; +import java.util.stream.Stream; +import org.assertj.core.api.Assertions; +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; + class HandsTest { final Hands sum20Size2 = new Hands( List.of(new Card(NINE, SPADE), new Card(ACE, SPADE))); - final Hands sum20Size3 = new Hands( - List.of(new Card(FOUR, SPADE), new Card(FIVE, SPADE), - new Card(ACE, SPADE))); - final Hands sum21Size2 = new Hands( - List.of(new Card(JACK, HEART), - new Card(ACE, SPADE))); - final Hands sum22Size3 = new Hands( - List.of(new Card(NINE, HEART), new Card(EIGHT, SPADE), - new Card(FIVE, HEART))); - final Hands blackJack = new Hands( - List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); @Test @DisplayName("카드를 가지고 있는 객체를 생성한다.") @@ -89,48 +75,6 @@ void sumAce1(final Hands hands, final int expected) { Assertions.assertThat(result).isEqualTo(expected); } - @DisplayName("카드 합이 같고 카드 갯수가 같으면 무승부이다.") - @Test - void isTie() { - // given - final Hands dealerSum10Size3 = new Hands( - List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), - new Card(ACE, SPADE))); - final Hands playerSum10Size3 = new Hands( - List.of(new Card(FOUR, SPADE), new Card(FIVE, SPADE), - new Card(ACE, SPADE))); - - // when && then - Assertions.assertThat(dealerSum10Size3.calculateResult(playerSum10Size3)).isEqualTo(Result.TIE); - } - - @DisplayName("카드 합이 같은데 카드 갯수가 더 적으면 승리이다.") - @Test - void isWinBySize() { - Assertions.assertThat(sum20Size2.calculateResult(sum20Size3)).isEqualTo(Result.WIN); - } - - @Test - @DisplayName("카드 합이 21이하이면서 21에 가까운 카드가 승리한다.") - void isWin() { - Assertions.assertThat(sum21Size2.calculateResult(sum20Size2)).isEqualTo(Result.WIN); - Assertions.assertThat(sum20Size2.calculateResult(sum21Size2)).isEqualTo(Result.LOSE); - } - - @Test - @DisplayName("카드 합이 21초과이면 패배한다.") - void isLoseWhenCardSumGreater21() { - Assertions.assertThat(sum22Size3.calculateResult(sum20Size2)).isEqualTo(Result.LOSE); - } - - - @Test - @DisplayName("blackjack이 이긴다.") - void isWinBlackJack() { - Assertions.assertThat(blackJack.calculateResult(sum20Size2)).isEqualTo(Result.WIN); - Assertions.assertThat(sum20Size2.calculateResult(blackJack)).isEqualTo(Result.LOSE); - } - static Stream sumAce11ParameterProvider() { return Stream.of( Arguments.of(new Hands(List.of(new Card(ACE, HEART), diff --git a/src/test/java/domain/ResultTest.java b/src/test/java/domain/ResultTest.java new file mode 100644 index 0000000000..669202bcb4 --- /dev/null +++ b/src/test/java/domain/ResultTest.java @@ -0,0 +1,76 @@ +package domain; + +import static domain.card.Rank.ACE; +import static domain.card.Rank.EIGHT; +import static domain.card.Rank.FIVE; +import static domain.card.Rank.FOUR; +import static domain.card.Rank.JACK; +import static domain.card.Rank.NINE; +import static domain.card.Rank.SEVEN; +import static domain.card.Rank.TWO; +import static domain.card.Shape.HEART; +import static domain.card.Shape.SPADE; + +import domain.card.Card; +import java.util.List; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class ResultTest { + + final Hands sum20Size2 = new Hands( + List.of(new Card(NINE, SPADE), new Card(ACE, SPADE))); + final Hands sum20Size3 = new Hands( + List.of(new Card(FOUR, SPADE), new Card(FIVE, SPADE), + new Card(ACE, SPADE))); + final Hands sum21Size2 = new Hands( + List.of(new Card(JACK, HEART), + new Card(ACE, SPADE))); + final Hands sum22Size3 = new Hands( + List.of(new Card(NINE, HEART), new Card(EIGHT, SPADE), + new Card(FIVE, HEART))); + final Hands blackJack = new Hands( + List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); + + @DisplayName("카드 합이 같고 카드 갯수가 같으면 무승부이다.") + @Test + void isTie() { + // given + final Hands dealerSum10Size3 = new Hands( + List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), + new Card(ACE, SPADE))); + final Hands playerSum10Size3 = new Hands( + List.of(new Card(FOUR, SPADE), new Card(FIVE, SPADE), + new Card(ACE, SPADE))); + + // when && then + Assertions.assertThat(dealerSum10Size3.calculateResult(playerSum10Size3)).isEqualTo(Result.TIE); + } + + @DisplayName("카드 합이 같은데 카드 갯수가 더 적으면 승리이다.") + @Test + void isWinBySize() { + Assertions.assertThat(sum20Size2.calculateResult(sum20Size3)).isEqualTo(Result.WIN); + } + + @Test + @DisplayName("카드 합이 21이하이면서 21에 가까운 카드가 승리한다.") + void isWin() { + Assertions.assertThat(sum21Size2.calculateResult(sum20Size2)).isEqualTo(Result.WIN); + Assertions.assertThat(sum20Size2.calculateResult(sum21Size2)).isEqualTo(Result.LOSE); + } + + @Test + @DisplayName("카드 합이 21초과이면 패배한다.") + void isLoseWhenCardSumGreater21() { + Assertions.assertThat(sum22Size3.calculateResult(sum20Size2)).isEqualTo(Result.LOSE); + } + + @Test + @DisplayName("blackjack이 이긴다.") + void isWinBlackJack() { + Assertions.assertThat(blackJack.calculateResult(sum20Size2)).isEqualTo(Result.WIN); + Assertions.assertThat(sum20Size2.calculateResult(blackJack)).isEqualTo(Result.LOSE); + } +} From 36094c89bc5ad6035541fff7b3fe3bc9f347714b Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Mon, 11 Mar 2024 19:36:27 +0900 Subject: [PATCH 080/100] =?UTF-8?q?refactor:=20Game=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=82=AD=EC=A0=9C=20=EB=B0=8F=20=EC=B1=85=EC=9E=84?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/BlackJackController.java | 12 +++--- src/main/java/domain/Game.java | 37 ------------------- src/main/java/domain/Hands.java | 2 + src/main/java/domain/participant/Dealer.java | 14 +++++++ .../java/domain/participant/Participant.java | 2 +- src/main/java/domain/participant/Players.java | 11 ++++++ src/main/java/domain/{ => result}/Result.java | 4 +- src/main/java/view/OutputView.java | 3 +- 8 files changed, 38 insertions(+), 47 deletions(-) delete mode 100644 src/main/java/domain/Game.java rename src/main/java/domain/{ => result}/Result.java (97%) diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index e3971aacd8..835f4ac1cf 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -2,7 +2,6 @@ import domain.Answer; import domain.CardDeck; -import domain.Game; import domain.participant.Dealer; import domain.participant.Player; import domain.participant.Players; @@ -23,9 +22,7 @@ public BlackJackController(final InputView inputView, final OutputView outputVie public void run() { final Players players = Players.from(inputView.readNames()); - final CardDeck cardDeck = CardDeck.generate(); - final Dealer dealer = new Dealer(cardDeck); - final Game game = new Game(dealer, players); + final Dealer dealer = new Dealer(CardDeck.generate()); initHands(players, dealer); dealWithPlayers(players, dealer); @@ -34,7 +31,8 @@ public void run() { dealer.deal(); printDealerTurnMessage(dealer.countAddedHands()); } - printFinalResult(players, dealer, game); + + printFinalResult(players, dealer); } private void printDealerTurnMessage(final int turn) { @@ -54,9 +52,9 @@ private void initHands(final Players players, final Dealer dealer) { outputView.printStartDeal(DealerHandsDto.from(dealer), ParticipantsDto.of(players)); } - private void printFinalResult(final Players players, final Dealer dealer, final Game game) { + private void printFinalResult(final Players players, final Dealer dealer) { outputView.printHandsResult(ParticipantsDto.of(dealer, players)); - outputView.printGameResult(game.getDealerResult(), game.getPlayersResult()); + outputView.printGameResult(dealer.getDealerResult(players), players.getPlayersResult(dealer)); } private void deal(final Player player, final Dealer dealer) { diff --git a/src/main/java/domain/Game.java b/src/main/java/domain/Game.java deleted file mode 100644 index 7e550cf606..0000000000 --- a/src/main/java/domain/Game.java +++ /dev/null @@ -1,37 +0,0 @@ -package domain; - -import domain.participant.Dealer; -import domain.participant.Player; -import domain.participant.Players; -import java.util.EnumMap; -import java.util.LinkedHashMap; -import java.util.Map; - -public class Game { - - private final Dealer dealer; - private final Players players; - - public Game(final Dealer dealer, final Players players) { - this.dealer = dealer; - this.players = players; - } - - public Map getPlayersResult() { - final Map playerResult = new LinkedHashMap<>(); - - for (Player player : players.getPlayers()) { - playerResult.put(player, player.calculateResult(dealer)); - } - return playerResult; - } - - public Map getDealerResult() { - Map dealerResult = new EnumMap<>(Result.class); - for (Result value : getPlayersResult().values()) { - Result reversed = value.reverse(); - dealerResult.put(reversed, dealerResult.getOrDefault(reversed, 0) + 1); - } - return dealerResult; - } -} diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index f25c1b2ccb..13087af791 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -1,6 +1,8 @@ package domain; import domain.card.Card; +import domain.result.Result; + import java.util.ArrayList; import java.util.List; import java.util.Objects; diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index a0276f8d8c..86a5549683 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -3,6 +3,10 @@ import domain.Answer; import domain.CardDeck; import domain.Hands; +import domain.result.Result; + +import java.util.EnumMap; +import java.util.Map; public class Dealer extends Participant { @@ -44,4 +48,14 @@ public void deal() { public int countAddedHands() { return handsSize() - INIT_HANDS_SIZE; } + + public Map getDealerResult(final Players players) { + Map dealerResult = new EnumMap<>(Result.class); + + for (Result value : players.getPlayersResult(this).values()) { + Result reversed = value.reverse(); + dealerResult.put(reversed, dealerResult.getOrDefault(reversed, 0) + 1); + } + return dealerResult; + } } diff --git a/src/main/java/domain/participant/Participant.java b/src/main/java/domain/participant/Participant.java index 6939ecb6c6..d6a9fc33bc 100644 --- a/src/main/java/domain/participant/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -1,8 +1,8 @@ package domain.participant; import domain.Hands; -import domain.Result; import domain.card.Card; +import domain.result.Result; import java.util.List; import java.util.Objects; diff --git a/src/main/java/domain/participant/Players.java b/src/main/java/domain/participant/Players.java index da1f717d97..9b14b4098b 100644 --- a/src/main/java/domain/participant/Players.java +++ b/src/main/java/domain/participant/Players.java @@ -1,10 +1,13 @@ package domain.participant; import domain.Hands; +import domain.result.Result; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.function.Consumer; +import java.util.stream.Collectors; public class Players { @@ -31,6 +34,14 @@ public boolean isAllBust() { .allMatch(Player::isBust); } + public Map getPlayersResult(final Dealer dealer) { + return names.stream() + .collect(Collectors.toMap( + player -> player, + player -> player.calculateResult(dealer) + )); + } + private static List mapToPlayers(final List names) { return names.stream() .map(String::trim) diff --git a/src/main/java/domain/Result.java b/src/main/java/domain/result/Result.java similarity index 97% rename from src/main/java/domain/Result.java rename to src/main/java/domain/result/Result.java index 944f905757..d6b8d846d2 100644 --- a/src/main/java/domain/Result.java +++ b/src/main/java/domain/result/Result.java @@ -1,4 +1,6 @@ -package domain; +package domain.result; + +import domain.Hands; import java.util.Arrays; import java.util.function.BiPredicate; diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 8388bc65ed..137b41c66b 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,10 +1,11 @@ package view; -import domain.Result; import domain.participant.Player; +import domain.result.Result; import dto.DealerHandsDto; import dto.ParticipantDto; import dto.ParticipantsDto; + import java.util.List; import java.util.Map; From ad35da366c314d8854c7b966ee05aa2f58fe60c9 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Mon, 11 Mar 2024 19:36:43 +0900 Subject: [PATCH 081/100] =?UTF-8?q?refactor:=20TestFixture=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 --- src/test/java/domain/AnswerTest.java | 1 - src/test/java/domain/CardDeckTest.java | 2 +- src/test/java/domain/DealerTest.java | 39 +++++++-- src/test/java/domain/GameTest.java | 98 ---------------------- src/test/java/domain/HandsTest.java | 49 ++++------- src/test/java/domain/HandsTestFixture.java | 41 +++++++++ src/test/java/domain/PlayersTest.java | 60 ++++++++++--- src/test/java/domain/ResultTest.java | 49 +++-------- 8 files changed, 144 insertions(+), 195 deletions(-) delete mode 100644 src/test/java/domain/GameTest.java create mode 100644 src/test/java/domain/HandsTestFixture.java diff --git a/src/test/java/domain/AnswerTest.java b/src/test/java/domain/AnswerTest.java index 4e57d3fd75..9b6f41a7c0 100644 --- a/src/test/java/domain/AnswerTest.java +++ b/src/test/java/domain/AnswerTest.java @@ -15,5 +15,4 @@ void invalidAnswer(String value) { .isInstanceOf(IllegalArgumentException.class) .hasMessage("[ERROR] 유효하지 않은 대답입니다."); } - } diff --git a/src/test/java/domain/CardDeckTest.java b/src/test/java/domain/CardDeckTest.java index 8bd192aa6c..0f373818b1 100644 --- a/src/test/java/domain/CardDeckTest.java +++ b/src/test/java/domain/CardDeckTest.java @@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test; class CardDeckTest { + @DisplayName("카드를 52 * 6 만큼 생성한다.") @Test void generate() { @@ -15,7 +16,6 @@ void generate() { Assertions.assertThat(cardDeck.size()).isEqualTo(52 * 6); } - @Test @DisplayName("카드가 없는데 카드를 뽑을 경우 예외가 발생한다.") void pop() { diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index a35f299af0..999a4230cf 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -1,20 +1,24 @@ package domain; -import domain.card.Card; import domain.participant.Dealer; import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; +import domain.result.Result; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.List; +import java.util.Map; -import static domain.card.Rank.FIVE; -import static domain.card.Rank.NINE; -import static domain.card.Shape.HEART; -import static domain.card.Shape.SPADE; +import static domain.HandsTestFixture.sum10Size2; +import static domain.HandsTestFixture.sum18Size2; +import static domain.HandsTestFixture.sum20Size3; +import static domain.HandsTestFixture.sum21Size2; +import static domain.result.Result.LOSE; +import static domain.result.Result.TIE; +import static domain.result.Result.WIN; class DealerTest { @@ -59,8 +63,7 @@ void addOneCard() { @DisplayName("딜러의 카드의 합이 17이상이 될때까지 카드를 추가한다.") void dealerDeal() { //given - final Hands sum10 = new Hands(List.of(new Card(FIVE, SPADE), new Card(FIVE, HEART))); - final Dealer dealer = new Dealer(CardDeck.generate(), sum10); + final Dealer dealer = new Dealer(CardDeck.generate(), sum10Size2); //when dealer.deal(); @@ -73,8 +76,7 @@ void dealerDeal() { @Test void dealerNoDeal() { //given - final Hands sum18 = new Hands(List.of(new Card(NINE, SPADE), new Card(NINE, HEART))); - final Dealer dealer = new Dealer(CardDeck.generate(), sum18); + final Dealer dealer = new Dealer(CardDeck.generate(), sum18Size2); //when dealer.deal(); @@ -82,4 +84,23 @@ void dealerNoDeal() { //then Assertions.assertThat(dealer.countAddedHands()).isZero(); } + + @DisplayName("딜러의 승패무를 판단한다.") + @Test + void dealerResult() { + // given + Player loser1 = new Player(new Name("레디"), sum18Size2); + Player loser2 = new Player(new Name("피케이"), sum18Size2); + Player winner = new Player(new Name("제제"), sum21Size2); + Player tier = new Player(new Name("브라운"), sum20Size3); + + Players players = new Players(List.of(loser1, loser2, winner, tier)); + Dealer dealer = new Dealer(CardDeck.generate(), sum20Size3); + + // when + Map expected = Map.of(WIN, 2, LOSE, 1, TIE, 1); + + // then + Assertions.assertThat(dealer.getDealerResult(players)).isEqualTo(expected); + } } diff --git a/src/test/java/domain/GameTest.java b/src/test/java/domain/GameTest.java deleted file mode 100644 index 03d97de311..0000000000 --- a/src/test/java/domain/GameTest.java +++ /dev/null @@ -1,98 +0,0 @@ -package domain; - -import domain.card.Card; -import domain.participant.Dealer; -import domain.participant.Name; -import domain.participant.Player; -import domain.participant.Players; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; -import java.util.Map; - -import static domain.Result.LOSE; -import static domain.Result.TIE; -import static domain.Result.WIN; -import static domain.card.Rank.ACE; -import static domain.card.Rank.EIGHT; -import static domain.card.Rank.JACK; -import static domain.card.Rank.SEVEN; -import static domain.card.Rank.TEN; -import static domain.card.Rank.TWO; -import static domain.card.Shape.CLOVER; -import static domain.card.Shape.DIAMOND; -import static domain.card.Shape.HEART; -import static domain.card.Shape.SPADE; - -class GameTest { - - final Hands sum18 = new Hands(List.of(new Card(EIGHT, CLOVER), new Card(TEN, DIAMOND))); - final Hands sum20 = new Hands(List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), new Card(ACE, SPADE))); - final Hands sum21 = new Hands(List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); - final Hands bustHands = new Hands(List.of(new Card(EIGHT, DIAMOND), new Card(TWO, DIAMOND), - new Card(TWO, DIAMOND), new Card(JACK, CLOVER))); - - - @Test - @DisplayName("참여자의 승패무를 판단한다.") - void playerResult() { - //given - Player loser = new Player(new Name("레디"), sum18); - Player winner = new Player(new Name("제제"), sum21); - Player tier = new Player(new Name("수달"), sum20); - - Players players = new Players(List.of(loser, winner, tier)); - Dealer dealer = new Dealer(CardDeck.generate(), sum20); - - Game game = new Game(dealer, players); - - //when & then - Map expected = Map.of(loser, LOSE, winner, WIN, tier, TIE); - Assertions.assertThat(game.getPlayersResult()).isEqualTo(expected); - } - - @DisplayName("딜러의 승패무를 판단한다.") - @Test - void dealerResult() { - // given - Player loser1 = new Player(new Name("레디"), sum18); - Player loser2 = new Player(new Name("피케이"), sum18); - Player winner = new Player(new Name("제제"), sum21); - Player tier = new Player(new Name("브라운"), sum20); - - Players players = new Players(List.of(loser1, loser2, winner, tier)); - Dealer dealer = new Dealer(CardDeck.generate(), sum20); - - Game game = new Game(dealer, players); - - // when - Map expected = Map.of(WIN, 2, LOSE, 1, TIE, 1); - - // then - Assertions.assertThat(game.getDealerResult()).isEqualTo(expected); - } - - @Test - @DisplayName("딜러가 버스트일때 참여자가 버스트가 아니면 WIN") - void all() { - //given - Dealer bustDealer = new Dealer(CardDeck.generate(), bustHands); - Player winner1 = new Player(new Name("레디"), sum18); - Player winner2 = new Player(new Name("브라운"), sum20); - Player loser = new Player(new Name("제제"), bustHands); - - Players players = new Players(List.of(winner1, winner2, loser)); - - //when - Game game = new Game(bustDealer, players); - - Map expectedPlayerResult = Map.of(winner1, WIN, winner2, WIN, loser, LOSE); - Map expectedDealerResult = Map.of(WIN, 1, LOSE, 2); - - //then - Assertions.assertThat(game.getPlayersResult()).isEqualTo(expectedPlayerResult); - Assertions.assertThat(game.getDealerResult()).isEqualTo(expectedDealerResult); - } -} diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index c9b2e8406c..948c902545 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -1,20 +1,6 @@ package domain; -import static domain.card.Rank.ACE; -import static domain.card.Rank.EIGHT; -import static domain.card.Rank.FIVE; -import static domain.card.Rank.FOUR; -import static domain.card.Rank.NINE; -import static domain.card.Rank.SIX; -import static domain.card.Rank.TWO; -import static domain.card.Shape.CLOVER; -import static domain.card.Shape.DIAMOND; -import static domain.card.Shape.HEART; -import static domain.card.Shape.SPADE; - import domain.card.Card; -import java.util.List; -import java.util.stream.Stream; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -22,10 +8,17 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -class HandsTest { +import java.util.stream.Stream; - final Hands sum20Size2 = new Hands( - List.of(new Card(NINE, SPADE), new Card(ACE, SPADE))); +import static domain.HandsTestFixture.sum19Size3Ace1; +import static domain.HandsTestFixture.sum19Size4Ace11; +import static domain.HandsTestFixture.sum20Size2; +import static domain.HandsTestFixture.sum20Size3Ace1; +import static domain.HandsTestFixture.sum21Size3Ace11; +import static domain.card.Rank.EIGHT; +import static domain.card.Shape.CLOVER; + +class HandsTest { @Test @DisplayName("카드를 가지고 있는 객체를 생성한다.") @@ -77,29 +70,15 @@ void sumAce1(final Hands hands, final int expected) { static Stream sumAce11ParameterProvider() { return Stream.of( - Arguments.of(new Hands(List.of(new Card(ACE, HEART), - new Card(EIGHT, SPADE), - new Card(TWO, CLOVER))), - 21), - Arguments.of(new Hands(List.of(new Card(ACE, DIAMOND), - new Card(TWO, CLOVER), - new Card(FOUR, CLOVER), - new Card(TWO, CLOVER))), - 19) + Arguments.of(sum21Size3Ace11, 21), + Arguments.of(sum19Size4Ace11, 19) ); } static Stream sumAce1ParameterProvider() { return Stream.of( - Arguments.of(new Hands(List.of(new Card(ACE, HEART), - new Card(NINE, SPADE), - new Card(NINE, CLOVER))), - 19), - Arguments.of(new Hands(List.of(new Card(ACE, DIAMOND), - new Card(EIGHT, CLOVER), - new Card(FIVE, CLOVER), - new Card(SIX, CLOVER))), - 20) + Arguments.of(sum19Size3Ace1, 19), + Arguments.of(sum20Size3Ace1, 20) ); } } diff --git a/src/test/java/domain/HandsTestFixture.java b/src/test/java/domain/HandsTestFixture.java new file mode 100644 index 0000000000..c89b3616ca --- /dev/null +++ b/src/test/java/domain/HandsTestFixture.java @@ -0,0 +1,41 @@ +package domain; + +import domain.card.Card; + +import java.util.List; + +import static domain.card.Rank.ACE; +import static domain.card.Rank.EIGHT; +import static domain.card.Rank.FIVE; +import static domain.card.Rank.FOUR; +import static domain.card.Rank.JACK; +import static domain.card.Rank.KING; +import static domain.card.Rank.NINE; +import static domain.card.Rank.QUEEN; +import static domain.card.Rank.SEVEN; +import static domain.card.Rank.SIX; +import static domain.card.Rank.TEN; +import static domain.card.Rank.THREE; +import static domain.card.Rank.TWO; +import static domain.card.Shape.CLOVER; +import static domain.card.Shape.DIAMOND; +import static domain.card.Shape.HEART; +import static domain.card.Shape.SPADE; + +class HandsTestFixture { + + static final Hands sum10Size2 = new Hands(List.of(new Card(FIVE, SPADE), new Card(FIVE, HEART))); + static final Hands sum17Size3One = new Hands(List.of(new Card(SEVEN, SPADE), new Card(FOUR, SPADE), new Card(SIX, SPADE))); + static final Hands sum17Size3Two = new Hands(List.of(new Card(TEN, SPADE), new Card(THREE, SPADE), new Card(FOUR, SPADE))); + static final Hands sum18Size2 = new Hands(List.of(new Card(EIGHT, CLOVER), new Card(TEN, DIAMOND))); + static final Hands sum19Size4Ace11 = new Hands(List.of(new Card(ACE, DIAMOND), new Card(TWO, CLOVER), new Card(FOUR, CLOVER), new Card(TWO, CLOVER))); + static final Hands sum19Size3Ace1 = new Hands(List.of(new Card(ACE, HEART), new Card(NINE, SPADE), new Card(NINE, CLOVER))); + static final Hands sum20Size2 = new Hands(List.of(new Card(NINE, SPADE), new Card(ACE, SPADE))); + static final Hands sum20Size3 = new Hands(List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), new Card(ACE, SPADE))); + static final Hands sum20Size3Ace1 = new Hands(List.of(new Card(ACE, DIAMOND), new Card(EIGHT, CLOVER), new Card(FIVE, CLOVER), new Card(SIX, CLOVER))); + static final Hands sum21Size2 = new Hands(List.of(new Card(QUEEN, HEART), new Card(ACE, SPADE))); + static final Hands sum21Size3Ace11 = new Hands(List.of(new Card(ACE, HEART), new Card(EIGHT, SPADE), new Card(TWO, CLOVER))); + static final Hands bustHands = new Hands(List.of(new Card(EIGHT, DIAMOND), new Card(TWO, DIAMOND), new Card(TWO, DIAMOND), new Card(KING, CLOVER))); + static final Hands noBustHands = new Hands(List.of(new Card(JACK, HEART), new Card(TEN, SPADE))); + static final Hands blackJack = new Hands(List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); +} diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index b3bd1b02f5..077a582928 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -1,9 +1,10 @@ package domain; -import domain.card.Card; +import domain.participant.Dealer; import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; +import domain.result.Result; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,23 +13,21 @@ import org.junit.jupiter.params.provider.MethodSource; import java.util.List; +import java.util.Map; import java.util.stream.Stream; -import static domain.card.Rank.JACK; -import static domain.card.Rank.TEN; -import static domain.card.Shape.HEART; -import static domain.card.Shape.SPADE; +import static domain.HandsTestFixture.bustHands; +import static domain.HandsTestFixture.noBustHands; +import static domain.HandsTestFixture.sum18Size2; +import static domain.HandsTestFixture.sum20Size2; +import static domain.HandsTestFixture.sum20Size3; +import static domain.HandsTestFixture.sum21Size2; +import static domain.result.Result.LOSE; +import static domain.result.Result.TIE; +import static domain.result.Result.WIN; class PlayersTest { - final Hands bustHands = new Hands( - List.of(new Card(JACK, HEART), new Card(TEN, SPADE), - new Card(TEN, HEART))); - - final Hands noBustHands = new Hands( - List.of(new Card(JACK, HEART), new Card(TEN, SPADE))); - - @Test @DisplayName("참여자 이름 중복시 예외가 발생한다.") void duplicatePlayerName() { @@ -85,6 +84,41 @@ void isAllBustTrue() { Assertions.assertThat(players.isAllBust()).isTrue(); } + @Test + @DisplayName("참여자의 승패무를 판단한다.") + void playerResult() { + //given + Player loser = new Player(new Name("레디"), sum18Size2); + Player winner = new Player(new Name("제제"), sum21Size2); + Player tier = new Player(new Name("수달"), sum20Size3); + + Players players = new Players(List.of(loser, winner, tier)); + Dealer dealer = new Dealer(CardDeck.generate(), sum20Size3); + + //when & then + Map expected = Map.of(loser, LOSE, winner, WIN, tier, TIE); + Assertions.assertThat(players.getPlayersResult(dealer)).isEqualTo(expected); + } + + @Test + @DisplayName("딜러가 버스트일때 참여자가 버스트가 아니면 WIN") + void all() { + //given + Dealer bustDealer = new Dealer(CardDeck.generate(), bustHands); + Player winner1 = new Player(new Name("레디"), sum18Size2); + Player winner2 = new Player(new Name("브라운"), sum20Size2); + Player loser = new Player(new Name("제제"), bustHands); + + Players players = new Players(List.of(winner1, winner2, loser)); + + //when + Map expectedPlayerResult = Map.of(winner1, WIN, winner2, WIN, loser, LOSE); + Map expectedDealerResult = Map.of(WIN, 1, LOSE, 2); + + //then + Assertions.assertThat(players.getPlayersResult(bustDealer)).isEqualTo(expectedPlayerResult); + Assertions.assertThat(bustDealer.getDealerResult(players)).isEqualTo(expectedDealerResult); + } static Stream validPlayersSizeParameterProvider() { return Stream.of( diff --git a/src/test/java/domain/ResultTest.java b/src/test/java/domain/ResultTest.java index 669202bcb4..c0bed919c9 100644 --- a/src/test/java/domain/ResultTest.java +++ b/src/test/java/domain/ResultTest.java @@ -1,51 +1,24 @@ package domain; -import static domain.card.Rank.ACE; -import static domain.card.Rank.EIGHT; -import static domain.card.Rank.FIVE; -import static domain.card.Rank.FOUR; -import static domain.card.Rank.JACK; -import static domain.card.Rank.NINE; -import static domain.card.Rank.SEVEN; -import static domain.card.Rank.TWO; -import static domain.card.Shape.HEART; -import static domain.card.Shape.SPADE; - -import domain.card.Card; -import java.util.List; +import domain.result.Result; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -class ResultTest { +import static domain.HandsTestFixture.blackJack; +import static domain.HandsTestFixture.bustHands; +import static domain.HandsTestFixture.sum17Size3One; +import static domain.HandsTestFixture.sum17Size3Two; +import static domain.HandsTestFixture.sum20Size2; +import static domain.HandsTestFixture.sum20Size3; +import static domain.HandsTestFixture.sum21Size2; - final Hands sum20Size2 = new Hands( - List.of(new Card(NINE, SPADE), new Card(ACE, SPADE))); - final Hands sum20Size3 = new Hands( - List.of(new Card(FOUR, SPADE), new Card(FIVE, SPADE), - new Card(ACE, SPADE))); - final Hands sum21Size2 = new Hands( - List.of(new Card(JACK, HEART), - new Card(ACE, SPADE))); - final Hands sum22Size3 = new Hands( - List.of(new Card(NINE, HEART), new Card(EIGHT, SPADE), - new Card(FIVE, HEART))); - final Hands blackJack = new Hands( - List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); +class ResultTest { @DisplayName("카드 합이 같고 카드 갯수가 같으면 무승부이다.") @Test void isTie() { - // given - final Hands dealerSum10Size3 = new Hands( - List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), - new Card(ACE, SPADE))); - final Hands playerSum10Size3 = new Hands( - List.of(new Card(FOUR, SPADE), new Card(FIVE, SPADE), - new Card(ACE, SPADE))); - - // when && then - Assertions.assertThat(dealerSum10Size3.calculateResult(playerSum10Size3)).isEqualTo(Result.TIE); + Assertions.assertThat(sum17Size3One.calculateResult(sum17Size3Two)).isEqualTo(Result.TIE); } @DisplayName("카드 합이 같은데 카드 갯수가 더 적으면 승리이다.") @@ -64,7 +37,7 @@ void isWin() { @Test @DisplayName("카드 합이 21초과이면 패배한다.") void isLoseWhenCardSumGreater21() { - Assertions.assertThat(sum22Size3.calculateResult(sum20Size2)).isEqualTo(Result.LOSE); + Assertions.assertThat(bustHands.calculateResult(sum20Size2)).isEqualTo(Result.LOSE); } @Test From 5e142b0919fb5e9e5b77ada1fc94ba1c24998be6 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 19:40:53 +0900 Subject: [PATCH 082/100] =?UTF-8?q?style:=20import=20=EB=AC=B8=20=EC=88=9C?= =?UTF-8?q?=EC=84=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Hands.java | 1 - src/main/java/domain/participant/Dealer.java | 1 - .../java/domain/participant/Participant.java | 1 - src/main/java/domain/participant/Players.java | 1 - src/main/java/domain/result/Result.java | 1 - src/main/java/view/OutputView.java | 1 - src/test/java/domain/DealerTest.java | 21 ++++++------- src/test/java/domain/HandsTest.java | 19 ++++++------ src/test/java/domain/HandsTestFixture.java | 31 ++++++++++++------- src/test/java/domain/PlayersTest.java | 27 ++++++++-------- src/test/java/domain/ResultTest.java | 10 +++--- 11 files changed, 56 insertions(+), 58 deletions(-) diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/Hands.java index 13087af791..8f23fbf9df 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/Hands.java @@ -2,7 +2,6 @@ import domain.card.Card; import domain.result.Result; - import java.util.ArrayList; import java.util.List; import java.util.Objects; diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 86a5549683..e81387f2da 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -4,7 +4,6 @@ import domain.CardDeck; import domain.Hands; import domain.result.Result; - import java.util.EnumMap; import java.util.Map; diff --git a/src/main/java/domain/participant/Participant.java b/src/main/java/domain/participant/Participant.java index d6a9fc33bc..a1e2354893 100644 --- a/src/main/java/domain/participant/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -3,7 +3,6 @@ import domain.Hands; import domain.card.Card; import domain.result.Result; - import java.util.List; import java.util.Objects; diff --git a/src/main/java/domain/participant/Players.java b/src/main/java/domain/participant/Players.java index 9b14b4098b..de81d676af 100644 --- a/src/main/java/domain/participant/Players.java +++ b/src/main/java/domain/participant/Players.java @@ -2,7 +2,6 @@ import domain.Hands; import domain.result.Result; - import java.util.List; import java.util.Map; import java.util.Set; diff --git a/src/main/java/domain/result/Result.java b/src/main/java/domain/result/Result.java index d6b8d846d2..1a1babe02d 100644 --- a/src/main/java/domain/result/Result.java +++ b/src/main/java/domain/result/Result.java @@ -1,7 +1,6 @@ package domain.result; import domain.Hands; - import java.util.Arrays; import java.util.function.BiPredicate; diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 137b41c66b..3bad54f0a6 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -5,7 +5,6 @@ import dto.DealerHandsDto; import dto.ParticipantDto; import dto.ParticipantsDto; - import java.util.List; import java.util.Map; diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 999a4230cf..f6ae009417 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -1,25 +1,24 @@ package domain; +import static domain.HandsTestFixture.sum10Size2; +import static domain.HandsTestFixture.sum18Size2; +import static domain.HandsTestFixture.sum20Size3; +import static domain.HandsTestFixture.sum21Size2; +import static domain.result.Result.LOSE; +import static domain.result.Result.TIE; +import static domain.result.Result.WIN; + import domain.participant.Dealer; import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; import domain.result.Result; +import java.util.List; +import java.util.Map; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.List; -import java.util.Map; - -import static domain.HandsTestFixture.sum10Size2; -import static domain.HandsTestFixture.sum18Size2; -import static domain.HandsTestFixture.sum20Size3; -import static domain.HandsTestFixture.sum21Size2; -import static domain.result.Result.LOSE; -import static domain.result.Result.TIE; -import static domain.result.Result.WIN; - class DealerTest { @Test diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index 948c902545..91617944d8 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -1,15 +1,5 @@ package domain; -import domain.card.Card; -import org.assertj.core.api.Assertions; -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.stream.Stream; - import static domain.HandsTestFixture.sum19Size3Ace1; import static domain.HandsTestFixture.sum19Size4Ace11; import static domain.HandsTestFixture.sum20Size2; @@ -18,6 +8,15 @@ import static domain.card.Rank.EIGHT; import static domain.card.Shape.CLOVER; +import domain.card.Card; +import java.util.stream.Stream; +import org.assertj.core.api.Assertions; +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; + class HandsTest { @Test diff --git a/src/test/java/domain/HandsTestFixture.java b/src/test/java/domain/HandsTestFixture.java index c89b3616ca..167e3039ba 100644 --- a/src/test/java/domain/HandsTestFixture.java +++ b/src/test/java/domain/HandsTestFixture.java @@ -1,9 +1,5 @@ package domain; -import domain.card.Card; - -import java.util.List; - import static domain.card.Rank.ACE; import static domain.card.Rank.EIGHT; import static domain.card.Rank.FIVE; @@ -22,20 +18,31 @@ import static domain.card.Shape.HEART; import static domain.card.Shape.SPADE; +import domain.card.Card; +import java.util.List; + class HandsTestFixture { static final Hands sum10Size2 = new Hands(List.of(new Card(FIVE, SPADE), new Card(FIVE, HEART))); - static final Hands sum17Size3One = new Hands(List.of(new Card(SEVEN, SPADE), new Card(FOUR, SPADE), new Card(SIX, SPADE))); - static final Hands sum17Size3Two = new Hands(List.of(new Card(TEN, SPADE), new Card(THREE, SPADE), new Card(FOUR, SPADE))); + static final Hands sum17Size3One = new Hands( + List.of(new Card(SEVEN, SPADE), new Card(FOUR, SPADE), new Card(SIX, SPADE))); + static final Hands sum17Size3Two = new Hands( + List.of(new Card(TEN, SPADE), new Card(THREE, SPADE), new Card(FOUR, SPADE))); static final Hands sum18Size2 = new Hands(List.of(new Card(EIGHT, CLOVER), new Card(TEN, DIAMOND))); - static final Hands sum19Size4Ace11 = new Hands(List.of(new Card(ACE, DIAMOND), new Card(TWO, CLOVER), new Card(FOUR, CLOVER), new Card(TWO, CLOVER))); - static final Hands sum19Size3Ace1 = new Hands(List.of(new Card(ACE, HEART), new Card(NINE, SPADE), new Card(NINE, CLOVER))); + static final Hands sum19Size4Ace11 = new Hands( + List.of(new Card(ACE, DIAMOND), new Card(TWO, CLOVER), new Card(FOUR, CLOVER), new Card(TWO, CLOVER))); + static final Hands sum19Size3Ace1 = new Hands( + List.of(new Card(ACE, HEART), new Card(NINE, SPADE), new Card(NINE, CLOVER))); static final Hands sum20Size2 = new Hands(List.of(new Card(NINE, SPADE), new Card(ACE, SPADE))); - static final Hands sum20Size3 = new Hands(List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), new Card(ACE, SPADE))); - static final Hands sum20Size3Ace1 = new Hands(List.of(new Card(ACE, DIAMOND), new Card(EIGHT, CLOVER), new Card(FIVE, CLOVER), new Card(SIX, CLOVER))); + static final Hands sum20Size3 = new Hands( + List.of(new Card(SEVEN, SPADE), new Card(TWO, SPADE), new Card(ACE, SPADE))); + static final Hands sum20Size3Ace1 = new Hands( + List.of(new Card(ACE, DIAMOND), new Card(EIGHT, CLOVER), new Card(FIVE, CLOVER), new Card(SIX, CLOVER))); static final Hands sum21Size2 = new Hands(List.of(new Card(QUEEN, HEART), new Card(ACE, SPADE))); - static final Hands sum21Size3Ace11 = new Hands(List.of(new Card(ACE, HEART), new Card(EIGHT, SPADE), new Card(TWO, CLOVER))); - static final Hands bustHands = new Hands(List.of(new Card(EIGHT, DIAMOND), new Card(TWO, DIAMOND), new Card(TWO, DIAMOND), new Card(KING, CLOVER))); + static final Hands sum21Size3Ace11 = new Hands( + List.of(new Card(ACE, HEART), new Card(EIGHT, SPADE), new Card(TWO, CLOVER))); + static final Hands bustHands = new Hands( + List.of(new Card(EIGHT, DIAMOND), new Card(TWO, DIAMOND), new Card(TWO, DIAMOND), new Card(KING, CLOVER))); static final Hands noBustHands = new Hands(List.of(new Card(JACK, HEART), new Card(TEN, SPADE))); static final Hands blackJack = new Hands(List.of(new Card(JACK, HEART), new Card(ACE, SPADE))); } diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index 077a582928..d83d6239ef 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -1,10 +1,23 @@ package domain; +import static domain.HandsTestFixture.bustHands; +import static domain.HandsTestFixture.noBustHands; +import static domain.HandsTestFixture.sum18Size2; +import static domain.HandsTestFixture.sum20Size2; +import static domain.HandsTestFixture.sum20Size3; +import static domain.HandsTestFixture.sum21Size2; +import static domain.result.Result.LOSE; +import static domain.result.Result.TIE; +import static domain.result.Result.WIN; + import domain.participant.Dealer; import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; import domain.result.Result; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,20 +25,6 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import java.util.List; -import java.util.Map; -import java.util.stream.Stream; - -import static domain.HandsTestFixture.bustHands; -import static domain.HandsTestFixture.noBustHands; -import static domain.HandsTestFixture.sum18Size2; -import static domain.HandsTestFixture.sum20Size2; -import static domain.HandsTestFixture.sum20Size3; -import static domain.HandsTestFixture.sum21Size2; -import static domain.result.Result.LOSE; -import static domain.result.Result.TIE; -import static domain.result.Result.WIN; - class PlayersTest { @Test diff --git a/src/test/java/domain/ResultTest.java b/src/test/java/domain/ResultTest.java index c0bed919c9..1223bb30c4 100644 --- a/src/test/java/domain/ResultTest.java +++ b/src/test/java/domain/ResultTest.java @@ -1,10 +1,5 @@ package domain; -import domain.result.Result; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - import static domain.HandsTestFixture.blackJack; import static domain.HandsTestFixture.bustHands; import static domain.HandsTestFixture.sum17Size3One; @@ -13,6 +8,11 @@ import static domain.HandsTestFixture.sum20Size3; import static domain.HandsTestFixture.sum21Size2; +import domain.result.Result; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + class ResultTest { @DisplayName("카드 합이 같고 카드 갯수가 같으면 무승부이다.") From cc6a91ff0a5f035ef085184dc586eede05978f34 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 19:48:15 +0900 Subject: [PATCH 083/100] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=B3=80=EC=88=98=EB=AA=85=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/DealerTest.java | 16 +++++++++------- src/test/java/domain/PlayersTest.java | 10 +++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index f6ae009417..7b1cd06e99 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -40,22 +40,22 @@ void dealCards() { @DisplayName("참여자의 답변이 y라면 카드를 한장 추가한다.") void addOneCard() { //given - final Player redddy = new Player(new Name("레디"), Hands.createEmptyHands()); - final Player zeze = new Player(new Name("제제"), Hands.createEmptyHands()); + final Player hitPlayer = new Player(new Name("레디"), Hands.createEmptyHands()); + final Player stayPlayer = new Player(new Name("제제"), Hands.createEmptyHands()); - final Players players = new Players(List.of(redddy, zeze)); + final Players players = new Players(List.of(hitPlayer, stayPlayer)); final CardDeck cardDeck = CardDeck.generate(); final Dealer dealer = new Dealer(cardDeck); dealer.initHands(players); //when - dealer.deal(redddy, Answer.HIT); - dealer.deal(zeze, Answer.STAY); + dealer.deal(hitPlayer, Answer.HIT); + dealer.deal(stayPlayer, Answer.STAY); //then - Assertions.assertThat(redddy.handsSize()).isEqualTo(3); - Assertions.assertThat(zeze.handsSize()).isEqualTo(2); + Assertions.assertThat(hitPlayer.handsSize()).isEqualTo(3); + Assertions.assertThat(stayPlayer.handsSize()).isEqualTo(2); } @Test @@ -69,6 +69,7 @@ void dealerDeal() { //then Assertions.assertThat(dealer.countAddedHands()).isPositive(); + Assertions.assertThat(dealer.handsSum()).isGreaterThanOrEqualTo(17); } @DisplayName("딜러의 카드의 합이 17이상이라면 카드를 추가하지 않는다") @@ -82,6 +83,7 @@ void dealerNoDeal() { //then Assertions.assertThat(dealer.countAddedHands()).isZero(); + Assertions.assertThat(dealer.handsSum()).isGreaterThanOrEqualTo(17); } @DisplayName("딜러의 승패무를 판단한다.") diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index d83d6239ef..bcb648188c 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -57,19 +57,19 @@ void invalidPlayersSize(final List names) { } @Test - @DisplayName("참가자 중 버스트 되지 않은 참가자가 있다면 False를 반환한다.") + @DisplayName("참가자 중 버스트 되지 않은 참가자가 있다면 isAllBust가 False를 반환한다.") void isAllBustFalse() { //given - final Player player1 = new Player(new Name("레디"), bustHands); - final Player player2 = new Player(new Name("제제"), noBustHands); - final Players players = new Players(List.of(player1, player2)); + final Player bustPlayer = new Player(new Name("레디"), bustHands); + final Player noBustPlayer = new Player(new Name("제제"), noBustHands); + final Players players = new Players(List.of(bustPlayer, noBustPlayer)); //when && then Assertions.assertThat(players.isAllBust()).isFalse(); } @Test - @DisplayName("모든 참가자가 버스트되면 True를 반환한다.") + @DisplayName("모든 참가자가 버스트되면 isAllBust가 True를 반환한다.") void isAllBustTrue() { //given Player player1 = new Player(new Name("레디"), bustHands); From d5564b4e75a1ec7b9ec5efc5320d812740190c29 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 19:59:12 +0900 Subject: [PATCH 084/100] =?UTF-8?q?fix:=20LinkedHashMap=EC=9C=BC=EB=A1=9C?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD=ED=95=98=EC=97=AC=20=EC=B0=B8=EC=97=AC?= =?UTF-8?q?=EC=9E=90=20=EC=88=9C=EC=84=9C=20=EC=9C=A0=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/participant/Players.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/domain/participant/Players.java b/src/main/java/domain/participant/Players.java index de81d676af..f655da0235 100644 --- a/src/main/java/domain/participant/Players.java +++ b/src/main/java/domain/participant/Players.java @@ -2,11 +2,11 @@ import domain.Hands; import domain.result.Result; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Consumer; -import java.util.stream.Collectors; public class Players { @@ -34,11 +34,11 @@ public boolean isAllBust() { } public Map getPlayersResult(final Dealer dealer) { - return names.stream() - .collect(Collectors.toMap( - player -> player, - player -> player.calculateResult(dealer) - )); + final Map result = new LinkedHashMap<>(); + for (Player name : names) { + result.put(name, name.calculateResult(dealer)); + } + return result; } private static List mapToPlayers(final List names) { From 2b02cdb4560625c09d37fceb815e7e022a0f35ad Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 20:07:41 +0900 Subject: [PATCH 085/100] =?UTF-8?q?refactor:=20System.out.println=20?= =?UTF-8?q?=EB=8C=80=EC=8B=A0=20%n=EA=B3=BC=20System.lineSeparator=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/participant/Dealer.java | 4 +-- src/main/java/view/OutputView.java | 32 +++++++++----------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index e81387f2da..82236874f2 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -9,9 +9,9 @@ public class Dealer extends Participant { + public static final int INIT_HANDS_SIZE = 2; + public static final int MIN_HANDS_SUM = 16; private static final String NAME = "딜러"; - private static final int INIT_HANDS_SIZE = 2; - private static final int MIN_HANDS_SUM = 16; private final CardDeck cardDeck; diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 3bad54f0a6..0c37e10aed 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,5 +1,8 @@ package view; +import static domain.participant.Dealer.INIT_HANDS_SIZE; +import static domain.participant.Dealer.MIN_HANDS_SUM; + import domain.participant.Player; import domain.result.Result; import dto.DealerHandsDto; @@ -10,50 +13,45 @@ public class OutputView { - private static final String FORM = "%s카드: %s"; - private static final String TOTAL_SUM_FORM = "%s 카드: %s - 결과: %d"; - private static final String RESULT_FORM = "%s: %s"; + private static final String LINE = System.lineSeparator(); + private static final String FORM = "%s카드: %s%n"; + private static final String TOTAL_SUM_FORM = "%s 카드: %s - 결과: %d%n"; + private static final String RESULT_FORM = "%s: %s%n"; public void printStartDeal(final DealerHandsDto dealerHandsDto, final ParticipantsDto participantsDto) { final String dealerCard = dealerHandsDto.getDisplayedCard(); final List playerNames = participantsDto.getNames(); - System.out.println("딜러와 " + format(playerNames) + " 에게 2장을 나누었습니다."); - - System.out.println("딜러: " + dealerCard); + System.out.printf("딜러와 %s 에게 %d장을 나누었습니다.%n", format(playerNames), INIT_HANDS_SIZE); + System.out.printf("딜러: %s%n", dealerCard); for (ParticipantDto participantDto : participantsDto.getPlayers()) { System.out.printf(FORM, participantDto.name(), format(participantDto.cards())); - System.out.println(); } - System.out.println(); + System.out.print(LINE); } public void printHands(final ParticipantDto participantDto) { System.out.printf(FORM, participantDto.name(), format(participantDto.cards())); - System.out.println(); } public void printDealerTurnMessage() { - System.out.println("딜러는 16이하라 한장의 카드를 더 받았습니다."); - System.out.println(); + System.out.printf("딜러는 %d이하라 한장의 카드를 더 받았습니다.%n%n", MIN_HANDS_SUM); } public void printHandsResult(final ParticipantsDto participantsDto) { for (ParticipantDto participantDto : participantsDto.getPlayers()) { System.out.printf(TOTAL_SUM_FORM, participantDto.name(), format(participantDto.cards()), participantDto.totalSum()); - System.out.println(); } + System.out.print(LINE); } public void printGameResult(final Map dealerResult, final Map playerResult) { System.out.println("## 최종결과"); System.out.printf(RESULT_FORM, "딜러", format(dealerResult)); - System.out.println(); for (Map.Entry entry : playerResult.entrySet()) { System.out.printf(RESULT_FORM, entry.getKey().getName(), entry.getValue().getValue()); - System.out.println(); } } @@ -71,12 +69,10 @@ private String format(final List playerNames) { } public void printBust() { - System.out.println("BUST"); - System.out.println(); + System.out.printf("BUST%n"); } public void printBlackJack() { - System.out.println("BLACK JACK!!!"); - System.out.println(); + System.out.printf("BLACK JACK!!!%n"); } } From b23548c78de39a11e20b150377754986a6f2d583 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 20:08:44 +0900 Subject: [PATCH 086/100] =?UTF-8?q?docs:=20=EB=A6=AC=ED=8C=A9=ED=84=B0?= =?UTF-8?q?=EB=A7=81=20=EB=AA=A9=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 0e727cb9a1..4163a2dc16 100644 --- a/docs/README.md +++ b/docs/README.md @@ -75,12 +75,11 @@ * [ ] 플레이어의 이름 딜러 불가 * [x] Name 객체 포장하기 * [ ] CardDeck 스토리지 유닛으로 변경 -* [ ] Result 함수형 프로그래밍 사용 -* [ ] TestFixture 사용 +* [x] Result 함수형 프로그래밍 사용 +* [x] TestFixture 사용 * [ ] Answer 필드값 및 stream 제거 * [ ] 패키지 정리 * [ ] 메서드 컨벤션 작성 및 확인 * [ ] final 컨벤션 통일 * [x] CardDeck 생성자 닫기 * [ ] CardDeck 구조 변경 -* [ ] Game 메서드 변경 Dto 변경 From 8e95a70518e8c20a7b9f2d501867cbf4b308af56 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 20:15:08 +0900 Subject: [PATCH 087/100] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90?= =?UTF-8?q?=EC=9D=98=20=EC=9D=B4=EB=A6=84=EC=97=90=20=EB=94=9C=EB=9F=AC?= =?UTF-8?q?=EA=B0=80=20=EC=82=AC=EC=9A=A9=EB=90=A0=20=EC=8B=9C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EB=B0=9C=EC=83=9D=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/domain/participant/Dealer.java | 2 +- src/main/java/domain/participant/Player.java | 8 +++++++- src/test/java/domain/PlayerTest.java | 11 +++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 4163a2dc16..ff0415a76a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -72,7 +72,7 @@ * [ ] 블랙잭인경우 바로 게임 종료 * [ ] model 안에 있는 view를 어떻게 해야할까 * Rank, Shape -* [ ] 플레이어의 이름 딜러 불가 +* [x] 참가자의 이름 딜러 불가 * [x] Name 객체 포장하기 * [ ] CardDeck 스토리지 유닛으로 변경 * [x] Result 함수형 프로그래밍 사용 diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 82236874f2..40ac9a23c7 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -11,7 +11,7 @@ public class Dealer extends Participant { public static final int INIT_HANDS_SIZE = 2; public static final int MIN_HANDS_SUM = 16; - private static final String NAME = "딜러"; + public static final String NAME = "딜러"; private final CardDeck cardDeck; diff --git a/src/main/java/domain/participant/Player.java b/src/main/java/domain/participant/Player.java index 0c2bc54f1a..0cd041caf7 100644 --- a/src/main/java/domain/participant/Player.java +++ b/src/main/java/domain/participant/Player.java @@ -3,8 +3,14 @@ import domain.Hands; public class Player extends Participant { - public Player(final Name name, final Hands hands) { super(name, hands); + validate(name); + } + + private void validate(final Name name) { + if (Dealer.NAME.equals(name.getValue())) { + throw new IllegalArgumentException("[ERROR] 사용할 수 없는 이름입니다."); + } } } diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index 2f3c86f004..759a3cdee5 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -13,4 +13,15 @@ void createPlayerWithName() { Assertions.assertThatCode(() -> new Player(new Name("pobi"), Hands.createEmptyHands())) .doesNotThrowAnyException(); } + + @Test + @DisplayName("참여자 이름이 딜러이면 예외가 발생한다.") + void validateName() { + final Name name = new Name("딜러"); + final Hands hands = Hands.createEmptyHands(); + + Assertions.assertThatThrownBy(() -> new Player(name, hands)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 사용할 수 없는 이름입니다."); + } } From 2a8dfdcad656434aa0933dceb65723bfc9309005 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Mon, 11 Mar 2024 20:34:58 +0900 Subject: [PATCH 088/100] =?UTF-8?q?refactor:=20isHit=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 1 - src/main/java/controller/BlackJackController.java | 4 ++-- src/main/java/domain/Answer.java | 4 ++++ src/main/java/domain/participant/Dealer.java | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/README.md b/docs/README.md index ff0415a76a..c3d09910a3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -77,7 +77,6 @@ * [ ] CardDeck 스토리지 유닛으로 변경 * [x] Result 함수형 프로그래밍 사용 * [x] TestFixture 사용 -* [ ] Answer 필드값 및 stream 제거 * [ ] 패키지 정리 * [ ] 메서드 컨벤션 작성 및 확인 * [ ] final 컨벤션 통일 diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 835f4ac1cf..d1e1c91e5a 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -87,10 +87,10 @@ private boolean isTurnEnded(final Player player, final Answer answer) { outputView.printBlackJack(); return true; } - return Answer.STAY.equals(answer); + return !answer.isHit(); } private boolean shouldShowHands(final boolean handsChanged, final Answer answer) { - return (Answer.STAY.equals(answer) && !handsChanged) || Answer.HIT.equals(answer); + return answer.isHit() || !handsChanged; } } diff --git a/src/main/java/domain/Answer.java b/src/main/java/domain/Answer.java index 2204468090..1fcc62f84a 100644 --- a/src/main/java/domain/Answer.java +++ b/src/main/java/domain/Answer.java @@ -18,4 +18,8 @@ public static Answer from(final String value) { .findFirst() .orElseThrow(() -> new IllegalArgumentException("[ERROR] 유효하지 않은 대답입니다.")); } + + public boolean isHit() { + return HIT.equals(this); + } } diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 40ac9a23c7..a8498bc6e6 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -4,6 +4,7 @@ import domain.CardDeck; import domain.Hands; import domain.result.Result; + import java.util.EnumMap; import java.util.Map; @@ -33,7 +34,7 @@ public void initHands(final Players players) { } public void deal(final Player player, final Answer answer) { - if (Answer.HIT.equals(answer)) { + if (answer.isHit()) { player.add(cardDeck.pop()); } } From 3de3d0b21ea7a0b6813f939de0f46cddf9240ae9 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Mon, 11 Mar 2024 21:01:41 +0900 Subject: [PATCH 089/100] =?UTF-8?q?docs:=20=EB=A6=AC=ED=8C=A9=ED=84=B0?= =?UTF-8?q?=EB=A7=81=20=EB=AA=A9=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/docs/README.md b/docs/README.md index c3d09910a3..8c83ffa8eb 100644 --- a/docs/README.md +++ b/docs/README.md @@ -66,19 +66,15 @@ ## 리팩터링 목록 -* [ ] 추상 클래스 활용 +~~추상 클래스 활용~~ + * [x] 카드덱이 비었을때 꺼낸 경우 예외 발생 * [x] createEmptyPacket 메서드명 수정 -* [ ] 블랙잭인경우 바로 게임 종료 -* [ ] model 안에 있는 view를 어떻게 해야할까 - * Rank, Shape * [x] 참가자의 이름 딜러 불가 * [x] Name 객체 포장하기 -* [ ] CardDeck 스토리지 유닛으로 변경 * [x] Result 함수형 프로그래밍 사용 * [x] TestFixture 사용 * [ ] 패키지 정리 * [ ] 메서드 컨벤션 작성 및 확인 * [ ] final 컨벤션 통일 * [x] CardDeck 생성자 닫기 -* [ ] CardDeck 구조 변경 From 21ec566b9dd82c404fc25f310d1547b943139973 Mon Sep 17 00:00:00 2001 From: jinwoo22 Date: Mon, 11 Mar 2024 21:07:58 +0900 Subject: [PATCH 090/100] =?UTF-8?q?style:=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=9C=84=EC=B9=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Answer.java | 1 + src/main/java/domain/CardDeck.java | 1 + src/main/java/domain/card/Card.java | 9 ++++--- src/main/java/domain/card/Shape.java | 1 + src/main/java/domain/participant/Name.java | 9 ++++--- src/main/java/domain/participant/Player.java | 1 + src/main/java/domain/result/Result.java | 28 ++++++++++---------- src/main/java/dto/DealerHandsDto.java | 1 + 8 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/main/java/domain/Answer.java b/src/main/java/domain/Answer.java index 1fcc62f84a..885164c3d2 100644 --- a/src/main/java/domain/Answer.java +++ b/src/main/java/domain/Answer.java @@ -3,6 +3,7 @@ import java.util.Arrays; public enum Answer { + HIT("y"), STAY("n"); diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/CardDeck.java index 9f9e36e7d1..8c3b6ded4e 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/CardDeck.java @@ -11,6 +11,7 @@ import java.util.List; public class CardDeck { + private static final int DECK_SIZE = 6; private final Deque cards; diff --git a/src/main/java/domain/card/Card.java b/src/main/java/domain/card/Card.java index a9fec7d5e2..254770fc9d 100644 --- a/src/main/java/domain/card/Card.java +++ b/src/main/java/domain/card/Card.java @@ -3,6 +3,7 @@ import java.util.Objects; public class Card { + private final Rank rank; private final Shape shape; @@ -11,14 +12,14 @@ public Card(final Rank rank, final Shape shape) { this.shape = shape; } - public int getCardNumber() { - return rank.getValue(); - } - public boolean isAce() { return rank.isAce(); } + public int getCardNumber() { + return rank.getValue(); + } + @Override public boolean equals(final Object target) { if (this == target) { diff --git a/src/main/java/domain/card/Shape.java b/src/main/java/domain/card/Shape.java index 400464c239..093d73cfa8 100644 --- a/src/main/java/domain/card/Shape.java +++ b/src/main/java/domain/card/Shape.java @@ -1,6 +1,7 @@ package domain.card; public enum Shape { + SPADE("스페이드"), HEART("하트"), CLOVER("클로버"), diff --git a/src/main/java/domain/participant/Name.java b/src/main/java/domain/participant/Name.java index 558917c277..312caacf16 100644 --- a/src/main/java/domain/participant/Name.java +++ b/src/main/java/domain/participant/Name.java @@ -1,6 +1,7 @@ package domain.participant; public class Name { + private final String value; public Name(final String value) { @@ -8,10 +9,6 @@ public Name(final String value) { this.value = value; } - public String getValue() { - return value; - } - private void validate(final String name) { validateNull(name); validateBlank(name); @@ -28,4 +25,8 @@ private void validateBlank(final String name) { throw new IllegalArgumentException("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); } } + + public String getValue() { + return value; + } } diff --git a/src/main/java/domain/participant/Player.java b/src/main/java/domain/participant/Player.java index 0cd041caf7..c5dc795898 100644 --- a/src/main/java/domain/participant/Player.java +++ b/src/main/java/domain/participant/Player.java @@ -3,6 +3,7 @@ import domain.Hands; public class Player extends Participant { + public Player(final Name name, final Hands hands) { super(name, hands); validate(name); diff --git a/src/main/java/domain/result/Result.java b/src/main/java/domain/result/Result.java index 1a1babe02d..01e7878400 100644 --- a/src/main/java/domain/result/Result.java +++ b/src/main/java/domain/result/Result.java @@ -18,20 +18,6 @@ public enum Result { this.condition = condition; } - private static boolean winningCondition(final Hands hands, final Hands target) { - return (!hands.isBust() && target.isBust()) - || (hands.sum() > target.sum() && !hands.isBust()) - || (hands.sum() == target.sum() && hands.size() < target.size() && !hands.isBust()); - } - - private static boolean tieCondition(final Hands hands, final Hands target) { - return hands.sum() == target.sum() && hands.size() == target.size() && !hands.isBust(); - } - - private static boolean loseCondition(final Hands hands, final Hands target) { - return hands.isBust() || hands.sum() < target.sum() || !target.isBust(); - } - public Result reverse() { if (Result.WIN.equals(this)) { return LOSE; @@ -49,6 +35,20 @@ public static Result calculateOf(final Hands hands, final Hands target) { .orElseThrow(); } + private static boolean winningCondition(final Hands hands, final Hands target) { + return (!hands.isBust() && target.isBust()) + || (hands.sum() > target.sum() && !hands.isBust()) + || (hands.sum() == target.sum() && hands.size() < target.size() && !hands.isBust()); + } + + private static boolean tieCondition(final Hands hands, final Hands target) { + return hands.sum() == target.sum() && hands.size() == target.size() && !hands.isBust(); + } + + private static boolean loseCondition(final Hands hands, final Hands target) { + return hands.isBust() || hands.sum() < target.sum() || !target.isBust(); + } + public String getValue() { return value; } diff --git a/src/main/java/dto/DealerHandsDto.java b/src/main/java/dto/DealerHandsDto.java index 821b146887..dae3867715 100644 --- a/src/main/java/dto/DealerHandsDto.java +++ b/src/main/java/dto/DealerHandsDto.java @@ -4,6 +4,7 @@ import java.util.List; public class DealerHandsDto { + private final String displayedCard; private DealerHandsDto(final String displayedCard) { From f2d25ece7e508d3714b8a37728f42cf877fb2994 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 21:17:10 +0900 Subject: [PATCH 091/100] =?UTF-8?q?refactor:=20controller=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/participant/Dealer.java | 9 ++++++--- src/main/java/view/OutputView.java | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index a8498bc6e6..c0009c69ca 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -4,14 +4,13 @@ import domain.CardDeck; import domain.Hands; import domain.result.Result; - import java.util.EnumMap; import java.util.Map; public class Dealer extends Participant { public static final int INIT_HANDS_SIZE = 2; - public static final int MIN_HANDS_SUM = 16; + public static final int THRESHOLD = 16; public static final String NAME = "딜러"; private final CardDeck cardDeck; @@ -40,7 +39,7 @@ public void deal(final Player player, final Answer answer) { } public void deal() { - while (handsSum() <= MIN_HANDS_SUM) { + while (isUnderThreshold()) { super.add(cardDeck.pop()); } } @@ -58,4 +57,8 @@ public Map getDealerResult(final Players players) { } return dealerResult; } + + private boolean isUnderThreshold() { + return handsSum() <= THRESHOLD; + } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 0c37e10aed..90a51caede 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,7 +1,7 @@ package view; import static domain.participant.Dealer.INIT_HANDS_SIZE; -import static domain.participant.Dealer.MIN_HANDS_SUM; +import static domain.participant.Dealer.THRESHOLD; import domain.participant.Player; import domain.result.Result; @@ -36,7 +36,7 @@ public void printHands(final ParticipantDto participantDto) { } public void printDealerTurnMessage() { - System.out.printf("딜러는 %d이하라 한장의 카드를 더 받았습니다.%n%n", MIN_HANDS_SUM); + System.out.printf("딜러는 %d이하라 한장의 카드를 더 받았습니다.%n%n", THRESHOLD); } public void printHandsResult(final ParticipantsDto participantsDto) { From 96cdc3b8fbe6644e63db214c37c7e27641d13b24 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Mon, 11 Mar 2024 21:18:37 +0900 Subject: [PATCH 092/100] =?UTF-8?q?chore:=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 6 +++--- src/main/java/controller/BlackJackController.java | 2 +- src/main/java/domain/{result => }/Result.java | 4 ++-- src/main/java/domain/{ => card}/CardDeck.java | 5 +---- src/main/java/domain/participant/Dealer.java | 5 ++--- src/main/java/domain/{ => participant}/Hands.java | 4 ++-- src/main/java/domain/participant/Participant.java | 3 +-- src/main/java/domain/participant/Player.java | 2 -- src/main/java/domain/participant/Players.java | 3 +-- src/main/java/view/OutputView.java | 2 +- src/test/java/domain/CardDeckTest.java | 1 + src/test/java/domain/DealerTest.java | 9 +++++---- src/test/java/domain/HandsTest.java | 1 + src/test/java/domain/HandsTestFixture.java | 1 + src/test/java/domain/PlayerTest.java | 1 + src/test/java/domain/PlayersTest.java | 8 ++++---- src/test/java/domain/ResultTest.java | 1 - 17 files changed, 27 insertions(+), 31 deletions(-) rename src/main/java/domain/{result => }/Result.java (97%) rename src/main/java/domain/{ => card}/CardDeck.java (93%) rename src/main/java/domain/{ => participant}/Hands.java (97%) diff --git a/docs/README.md b/docs/README.md index 8c83ffa8eb..f875fee7da 100644 --- a/docs/README.md +++ b/docs/README.md @@ -74,7 +74,7 @@ * [x] Name 객체 포장하기 * [x] Result 함수형 프로그래밍 사용 * [x] TestFixture 사용 -* [ ] 패키지 정리 -* [ ] 메서드 컨벤션 작성 및 확인 -* [ ] final 컨벤션 통일 +* [x] 패키지 정리 +* [x] 메서드 컨벤션 작성 및 확인 +* [x] final 컨벤션 통일 * [x] CardDeck 생성자 닫기 diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index d1e1c91e5a..f2fbb18c59 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -1,7 +1,7 @@ package controller; import domain.Answer; -import domain.CardDeck; +import domain.card.CardDeck; import domain.participant.Dealer; import domain.participant.Player; import domain.participant.Players; diff --git a/src/main/java/domain/result/Result.java b/src/main/java/domain/Result.java similarity index 97% rename from src/main/java/domain/result/Result.java rename to src/main/java/domain/Result.java index 01e7878400..15cdb94994 100644 --- a/src/main/java/domain/result/Result.java +++ b/src/main/java/domain/Result.java @@ -1,6 +1,6 @@ -package domain.result; +package domain; -import domain.Hands; +import domain.participant.Hands; import java.util.Arrays; import java.util.function.BiPredicate; diff --git a/src/main/java/domain/CardDeck.java b/src/main/java/domain/card/CardDeck.java similarity index 93% rename from src/main/java/domain/CardDeck.java rename to src/main/java/domain/card/CardDeck.java index 8c3b6ded4e..6d1423e231 100644 --- a/src/main/java/domain/CardDeck.java +++ b/src/main/java/domain/card/CardDeck.java @@ -1,8 +1,5 @@ -package domain; +package domain.card; -import domain.card.Card; -import domain.card.Rank; -import domain.card.Shape; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index c0009c69ca..5ead0cec28 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -1,9 +1,8 @@ package domain.participant; import domain.Answer; -import domain.CardDeck; -import domain.Hands; -import domain.result.Result; +import domain.card.CardDeck; +import domain.Result; import java.util.EnumMap; import java.util.Map; diff --git a/src/main/java/domain/Hands.java b/src/main/java/domain/participant/Hands.java similarity index 97% rename from src/main/java/domain/Hands.java rename to src/main/java/domain/participant/Hands.java index 8f23fbf9df..42e2f78027 100644 --- a/src/main/java/domain/Hands.java +++ b/src/main/java/domain/participant/Hands.java @@ -1,7 +1,7 @@ -package domain; +package domain.participant; import domain.card.Card; -import domain.result.Result; +import domain.Result; import java.util.ArrayList; import java.util.List; import java.util.Objects; diff --git a/src/main/java/domain/participant/Participant.java b/src/main/java/domain/participant/Participant.java index a1e2354893..f58029de41 100644 --- a/src/main/java/domain/participant/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -1,8 +1,7 @@ package domain.participant; -import domain.Hands; import domain.card.Card; -import domain.result.Result; +import domain.Result; import java.util.List; import java.util.Objects; diff --git a/src/main/java/domain/participant/Player.java b/src/main/java/domain/participant/Player.java index c5dc795898..7491b785bd 100644 --- a/src/main/java/domain/participant/Player.java +++ b/src/main/java/domain/participant/Player.java @@ -1,7 +1,5 @@ package domain.participant; -import domain.Hands; - public class Player extends Participant { public Player(final Name name, final Hands hands) { diff --git a/src/main/java/domain/participant/Players.java b/src/main/java/domain/participant/Players.java index f655da0235..0075625da4 100644 --- a/src/main/java/domain/participant/Players.java +++ b/src/main/java/domain/participant/Players.java @@ -1,7 +1,6 @@ package domain.participant; -import domain.Hands; -import domain.result.Result; +import domain.Result; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 90a51caede..d4550f673c 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -4,7 +4,7 @@ import static domain.participant.Dealer.THRESHOLD; import domain.participant.Player; -import domain.result.Result; +import domain.Result; import dto.DealerHandsDto; import dto.ParticipantDto; import dto.ParticipantsDto; diff --git a/src/test/java/domain/CardDeckTest.java b/src/test/java/domain/CardDeckTest.java index 0f373818b1..90629be835 100644 --- a/src/test/java/domain/CardDeckTest.java +++ b/src/test/java/domain/CardDeckTest.java @@ -1,5 +1,6 @@ package domain; +import domain.card.CardDeck; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index 7b1cd06e99..d38c6534e6 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -4,15 +4,16 @@ import static domain.HandsTestFixture.sum18Size2; import static domain.HandsTestFixture.sum20Size3; import static domain.HandsTestFixture.sum21Size2; -import static domain.result.Result.LOSE; -import static domain.result.Result.TIE; -import static domain.result.Result.WIN; +import static domain.Result.LOSE; +import static domain.Result.TIE; +import static domain.Result.WIN; +import domain.card.CardDeck; import domain.participant.Dealer; +import domain.participant.Hands; import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; -import domain.result.Result; import java.util.List; import java.util.Map; import org.assertj.core.api.Assertions; diff --git a/src/test/java/domain/HandsTest.java b/src/test/java/domain/HandsTest.java index 91617944d8..8c5bb396d8 100644 --- a/src/test/java/domain/HandsTest.java +++ b/src/test/java/domain/HandsTest.java @@ -9,6 +9,7 @@ import static domain.card.Shape.CLOVER; import domain.card.Card; +import domain.participant.Hands; import java.util.stream.Stream; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; diff --git a/src/test/java/domain/HandsTestFixture.java b/src/test/java/domain/HandsTestFixture.java index 167e3039ba..bb1adb416d 100644 --- a/src/test/java/domain/HandsTestFixture.java +++ b/src/test/java/domain/HandsTestFixture.java @@ -19,6 +19,7 @@ import static domain.card.Shape.SPADE; import domain.card.Card; +import domain.participant.Hands; import java.util.List; class HandsTestFixture { diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index 759a3cdee5..8c4895ef99 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -1,5 +1,6 @@ package domain; +import domain.participant.Hands; import domain.participant.Name; import domain.participant.Player; import org.assertj.core.api.Assertions; diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index bcb648188c..cba3712341 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -6,15 +6,15 @@ import static domain.HandsTestFixture.sum20Size2; import static domain.HandsTestFixture.sum20Size3; import static domain.HandsTestFixture.sum21Size2; -import static domain.result.Result.LOSE; -import static domain.result.Result.TIE; -import static domain.result.Result.WIN; +import static domain.Result.LOSE; +import static domain.Result.TIE; +import static domain.Result.WIN; +import domain.card.CardDeck; import domain.participant.Dealer; import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; -import domain.result.Result; import java.util.List; import java.util.Map; import java.util.stream.Stream; diff --git a/src/test/java/domain/ResultTest.java b/src/test/java/domain/ResultTest.java index 1223bb30c4..01ad8e26a1 100644 --- a/src/test/java/domain/ResultTest.java +++ b/src/test/java/domain/ResultTest.java @@ -8,7 +8,6 @@ import static domain.HandsTestFixture.sum20Size3; import static domain.HandsTestFixture.sum21Size2; -import domain.result.Result; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; From fec95ee98e7f968a0e7e8b20d73a61f7eee5586f Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 12 Mar 2024 15:16:59 +0900 Subject: [PATCH 093/100] =?UTF-8?q?docs:=20=EB=A6=AC=ED=8C=A9=ED=84=B0?= =?UTF-8?q?=EB=A7=81=20=EB=AA=A9=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/README.md b/docs/README.md index f875fee7da..642a69c3d6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -78,3 +78,15 @@ * [x] 메서드 컨벤션 작성 및 확인 * [x] final 컨벤션 통일 * [x] CardDeck 생성자 닫기 + +
+ +* [ ] 예외 시 재입력 기능 +* [ ] 예외 메시지 상수화 +* [ ] CardDeck 웨어하우스로 바라보기 +* [ ] 컨트롤러 메서드 수정하기 +* [ ] Participant 추상 클래스 대신 클래스로 변경하기 +* [ ] model 과 view 분리하기 + * Result + * Rank + * Shape From 41b1df7364fbd74a1c706e47efdc9294372aebfa Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 12 Mar 2024 15:49:34 +0900 Subject: [PATCH 094/100] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=20=EC=8B=9C?= =?UTF-8?q?=20=EC=9E=AC=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/application/Application.java | 4 +- .../java/controller/BlackJackController.java | 13 ++--- src/main/java/controller/InputController.java | 54 +++++++++++++++++++ src/main/java/view/OutputView.java | 7 ++- 5 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 src/main/java/controller/InputController.java diff --git a/docs/README.md b/docs/README.md index 642a69c3d6..1fc1f16a33 100644 --- a/docs/README.md +++ b/docs/README.md @@ -81,7 +81,7 @@
-* [ ] 예외 시 재입력 기능 +* [x] 예외 시 재입력 기능 * [ ] 예외 메시지 상수화 * [ ] CardDeck 웨어하우스로 바라보기 * [ ] 컨트롤러 메서드 수정하기 diff --git a/src/main/java/application/Application.java b/src/main/java/application/Application.java index 5c5998ec53..2d98d55bfc 100644 --- a/src/main/java/application/Application.java +++ b/src/main/java/application/Application.java @@ -1,6 +1,7 @@ package application; import controller.BlackJackController; +import controller.InputController; import java.util.Scanner; import view.InputView; import view.OutputView; @@ -10,7 +11,8 @@ public static void main(String[] args) { final Scanner scanner = new Scanner(System.in); final InputView inputView = new InputView(scanner); final OutputView outputView = new OutputView(); - final BlackJackController blackJackController = new BlackJackController(inputView, outputView); + final InputController inputController = new InputController(inputView, outputView); + final BlackJackController blackJackController = new BlackJackController(inputController, outputView); blackJackController.run(); } } diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index f2fbb18c59..36624f4009 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -8,20 +8,21 @@ import dto.DealerHandsDto; import dto.ParticipantDto; import dto.ParticipantsDto; -import view.InputView; import view.OutputView; public class BlackJackController { - private final InputView inputView; + + private final InputController inputController; private final OutputView outputView; - public BlackJackController(final InputView inputView, final OutputView outputView) { - this.inputView = inputView; + + public BlackJackController(final InputController inputController, final OutputView outputView) { + this.inputController = inputController; this.outputView = outputView; } public void run() { - final Players players = Players.from(inputView.readNames()); + final Players players = inputController.getPlayers(); final Dealer dealer = new Dealer(CardDeck.generate()); initHands(players, dealer); @@ -62,7 +63,7 @@ private void deal(final Player player, final Dealer dealer) { boolean turnEnded = false; while (!turnEnded) { - Answer answer = Answer.from(inputView.readAnswer(player.getName())); + Answer answer = inputController.getAnswer(player.getName()); dealer.deal(player, answer); printHandsIfRequired(player, handsChanged, answer); diff --git a/src/main/java/controller/InputController.java b/src/main/java/controller/InputController.java new file mode 100644 index 0000000000..208ba53db8 --- /dev/null +++ b/src/main/java/controller/InputController.java @@ -0,0 +1,54 @@ +package controller; + +import domain.Answer; +import domain.participant.Players; +import java.util.List; +import view.InputView; +import view.OutputView; + +public class InputController { + + private final InputView inputView; + private final OutputView outputView; + + public InputController(final InputView inputView, final OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public Players getPlayers() { + Players players; + do { + players = readPlayers(); + } while (players == null); + return players; + } + + public Answer getAnswer(String name) { + Answer answer; + do { + answer = readAnswer(name); + } while (answer == null); + return answer; + } + + private Players readPlayers() { + try { + List rawNames = inputView.readNames(); + return Players.from(rawNames); + } catch (IllegalArgumentException exception) { + outputView.printException(exception); + return null; + } + } + + private Answer readAnswer(String name) { + try { + String value = inputView.readAnswer(name); + return Answer.from(value); + } catch (IllegalArgumentException exception) { + outputView.printException(exception); + return null; + } + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index d4550f673c..2eab6087c2 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -3,8 +3,8 @@ import static domain.participant.Dealer.INIT_HANDS_SIZE; import static domain.participant.Dealer.THRESHOLD; -import domain.participant.Player; import domain.Result; +import domain.participant.Player; import dto.DealerHandsDto; import dto.ParticipantDto; import dto.ParticipantsDto; @@ -18,6 +18,7 @@ public class OutputView { private static final String TOTAL_SUM_FORM = "%s 카드: %s - 결과: %d%n"; private static final String RESULT_FORM = "%s: %s%n"; + public void printStartDeal(final DealerHandsDto dealerHandsDto, final ParticipantsDto participantsDto) { final String dealerCard = dealerHandsDto.getDisplayedCard(); @@ -75,4 +76,8 @@ public void printBust() { public void printBlackJack() { System.out.printf("BLACK JACK!!!%n"); } + + public void printException(final Throwable exception) { + System.out.println(exception.getMessage()); + } } From 4347c8c3f8ed0aa45fb5b84d93f7764eb02da53a Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 12 Mar 2024 19:23:17 +0900 Subject: [PATCH 095/100] =?UTF-8?q?feat:=20=EC=BB=A4=EC=8A=A4=ED=85=80=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 3 +- src/main/java/constants/ErrorCode.java | 15 ++++++ src/main/java/controller/InputController.java | 9 ++-- src/main/java/domain/Answer.java | 4 +- src/main/java/domain/card/CardDeck.java | 4 +- src/main/java/domain/participant/Name.java | 7 ++- src/main/java/domain/participant/Player.java | 5 +- src/main/java/domain/participant/Players.java | 7 ++- src/main/java/exception/CustomException.java | 16 ++++++ .../DuplicatePlayerNameException.java | 10 ++++ .../exception/InvalidCommandException.java | 10 ++++ .../java/exception/InvalidInputException.java | 10 ++++ .../java/exception/InvalidPlayerName.java | 10 ++++ .../InvalidPlayersSizeException.java | 10 ++++ .../exception/InvalidSeparatorException.java | 10 ++++ .../MessageDoesNotExistException.java | 10 ++++ .../java/exception/NoMoreCardException.java | 10 ++++ .../ReservedPlayerNameException.java | 10 ++++ src/main/java/view/InputView.java | 7 ++- src/main/java/view/OutputView.java | 7 ++- .../java/view/message/ErrorCodeMessage.java | 49 +++++++++++++++++++ src/test/java/domain/AnswerTest.java | 4 +- src/test/java/domain/CardDeckTest.java | 4 +- src/test/java/domain/NameTest.java | 7 ++- src/test/java/domain/PlayerTest.java | 5 +- src/test/java/domain/PlayersTest.java | 8 +-- 26 files changed, 221 insertions(+), 30 deletions(-) create mode 100644 src/main/java/constants/ErrorCode.java create mode 100644 src/main/java/exception/CustomException.java create mode 100644 src/main/java/exception/DuplicatePlayerNameException.java create mode 100644 src/main/java/exception/InvalidCommandException.java create mode 100644 src/main/java/exception/InvalidInputException.java create mode 100644 src/main/java/exception/InvalidPlayerName.java create mode 100644 src/main/java/exception/InvalidPlayersSizeException.java create mode 100644 src/main/java/exception/InvalidSeparatorException.java create mode 100644 src/main/java/exception/MessageDoesNotExistException.java create mode 100644 src/main/java/exception/NoMoreCardException.java create mode 100644 src/main/java/exception/ReservedPlayerNameException.java create mode 100644 src/main/java/view/message/ErrorCodeMessage.java diff --git a/docs/README.md b/docs/README.md index 1fc1f16a33..205b599645 100644 --- a/docs/README.md +++ b/docs/README.md @@ -82,7 +82,8 @@
* [x] 예외 시 재입력 기능 -* [ ] 예외 메시지 상수화 +* [x] 예외 메시지 상수화 + * 커스텀 예외 구현 * [ ] CardDeck 웨어하우스로 바라보기 * [ ] 컨트롤러 메서드 수정하기 * [ ] Participant 추상 클래스 대신 클래스로 변경하기 diff --git a/src/main/java/constants/ErrorCode.java b/src/main/java/constants/ErrorCode.java new file mode 100644 index 0000000000..d6278bf1c5 --- /dev/null +++ b/src/main/java/constants/ErrorCode.java @@ -0,0 +1,15 @@ +package constants; + +public enum ErrorCode { + + NOT_EXIST_MESSAGE, + INVALID_SEPARATOR, + INVALID_INPUT, + INVALID_SIZE, + DUPLICATE_NAME, + RESERVED_NAME, + BLANK_VALUE, + EMPTY_CARD, + INVALID_COMMAND, + ; +} diff --git a/src/main/java/controller/InputController.java b/src/main/java/controller/InputController.java index 208ba53db8..4f9bb5df96 100644 --- a/src/main/java/controller/InputController.java +++ b/src/main/java/controller/InputController.java @@ -2,6 +2,7 @@ import domain.Answer; import domain.participant.Players; +import exception.CustomException; import java.util.List; import view.InputView; import view.OutputView; @@ -36,8 +37,8 @@ private Players readPlayers() { try { List rawNames = inputView.readNames(); return Players.from(rawNames); - } catch (IllegalArgumentException exception) { - outputView.printException(exception); + } catch (CustomException exception) { + outputView.printException(exception.getErrorCode()); return null; } } @@ -46,8 +47,8 @@ private Answer readAnswer(String name) { try { String value = inputView.readAnswer(name); return Answer.from(value); - } catch (IllegalArgumentException exception) { - outputView.printException(exception); + } catch (CustomException exception) { + outputView.printException(exception.getErrorCode()); return null; } } diff --git a/src/main/java/domain/Answer.java b/src/main/java/domain/Answer.java index 885164c3d2..dc53bfc33a 100644 --- a/src/main/java/domain/Answer.java +++ b/src/main/java/domain/Answer.java @@ -1,5 +1,7 @@ package domain; +import constants.ErrorCode; +import exception.InvalidCommandException; import java.util.Arrays; public enum Answer { @@ -17,7 +19,7 @@ public static Answer from(final String value) { return Arrays.stream(Answer.values()) .filter(answer -> answer.value.equals(value)) .findFirst() - .orElseThrow(() -> new IllegalArgumentException("[ERROR] 유효하지 않은 대답입니다.")); + .orElseThrow(() -> new InvalidCommandException(ErrorCode.INVALID_COMMAND)); } public boolean isHit() { diff --git a/src/main/java/domain/card/CardDeck.java b/src/main/java/domain/card/CardDeck.java index 6d1423e231..25ce09adff 100644 --- a/src/main/java/domain/card/CardDeck.java +++ b/src/main/java/domain/card/CardDeck.java @@ -1,5 +1,7 @@ package domain.card; +import constants.ErrorCode; +import exception.NoMoreCardException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -30,7 +32,7 @@ public static CardDeck generate() { public Card pop() { if (cards.isEmpty()) { - throw new IllegalStateException("[ERROR] 카드덱이 비어있습니다."); + throw new NoMoreCardException(ErrorCode.EMPTY_CARD); } return cards.pop(); } diff --git a/src/main/java/domain/participant/Name.java b/src/main/java/domain/participant/Name.java index 312caacf16..692e2ad538 100644 --- a/src/main/java/domain/participant/Name.java +++ b/src/main/java/domain/participant/Name.java @@ -1,5 +1,8 @@ package domain.participant; +import constants.ErrorCode; +import exception.InvalidPlayerName; + public class Name { private final String value; @@ -16,13 +19,13 @@ private void validate(final String name) { private void validateNull(final String name) { if (name == null) { - throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 이름입니다."); + throw new InvalidPlayerName(ErrorCode.BLANK_VALUE); } } private void validateBlank(final String name) { if (name.isBlank()) { - throw new IllegalArgumentException("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); + throw new InvalidPlayerName(ErrorCode.BLANK_VALUE); } } diff --git a/src/main/java/domain/participant/Player.java b/src/main/java/domain/participant/Player.java index 7491b785bd..7a45bef100 100644 --- a/src/main/java/domain/participant/Player.java +++ b/src/main/java/domain/participant/Player.java @@ -1,5 +1,8 @@ package domain.participant; +import constants.ErrorCode; +import exception.ReservedPlayerNameException; + public class Player extends Participant { public Player(final Name name, final Hands hands) { @@ -9,7 +12,7 @@ public Player(final Name name, final Hands hands) { private void validate(final Name name) { if (Dealer.NAME.equals(name.getValue())) { - throw new IllegalArgumentException("[ERROR] 사용할 수 없는 이름입니다."); + throw new ReservedPlayerNameException(ErrorCode.RESERVED_NAME); } } } diff --git a/src/main/java/domain/participant/Players.java b/src/main/java/domain/participant/Players.java index 0075625da4..b007d830b9 100644 --- a/src/main/java/domain/participant/Players.java +++ b/src/main/java/domain/participant/Players.java @@ -1,6 +1,9 @@ package domain.participant; +import constants.ErrorCode; import domain.Result; +import exception.DuplicatePlayerNameException; +import exception.InvalidPlayersSizeException; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -54,13 +57,13 @@ private static void validate(final List names) { private static void validateSize(final List names) { if (names.size() < MIN_SIZE || MAX_SIZE < names.size()) { - throw new IllegalArgumentException("[ERROR] 유효하지 않은 참여자 수입니다."); + throw new InvalidPlayersSizeException(ErrorCode.INVALID_SIZE); } } private static void validateDuplicate(final List names) { if (names.size() != Set.copyOf(names).size()) { - throw new IllegalArgumentException("[ERROR] 참여자 이름은 중복될 수 없습니다."); + throw new DuplicatePlayerNameException(ErrorCode.DUPLICATE_NAME); } } diff --git a/src/main/java/exception/CustomException.java b/src/main/java/exception/CustomException.java new file mode 100644 index 0000000000..fa189d08d1 --- /dev/null +++ b/src/main/java/exception/CustomException.java @@ -0,0 +1,16 @@ +package exception; + +import constants.ErrorCode; + +public class CustomException extends RuntimeException { + + private final ErrorCode errorCode; + + public CustomException(final ErrorCode errorCode) { + this.errorCode = errorCode; + } + + public ErrorCode getErrorCode() { + return errorCode; + } +} diff --git a/src/main/java/exception/DuplicatePlayerNameException.java b/src/main/java/exception/DuplicatePlayerNameException.java new file mode 100644 index 0000000000..8eb7bb2b5a --- /dev/null +++ b/src/main/java/exception/DuplicatePlayerNameException.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class DuplicatePlayerNameException extends CustomException { + + public DuplicatePlayerNameException(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/exception/InvalidCommandException.java b/src/main/java/exception/InvalidCommandException.java new file mode 100644 index 0000000000..ea970cc741 --- /dev/null +++ b/src/main/java/exception/InvalidCommandException.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class InvalidCommandException extends CustomException { + + public InvalidCommandException(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/exception/InvalidInputException.java b/src/main/java/exception/InvalidInputException.java new file mode 100644 index 0000000000..1ade7a5b23 --- /dev/null +++ b/src/main/java/exception/InvalidInputException.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class InvalidInputException extends CustomException { + + public InvalidInputException(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/exception/InvalidPlayerName.java b/src/main/java/exception/InvalidPlayerName.java new file mode 100644 index 0000000000..a5ae9971b0 --- /dev/null +++ b/src/main/java/exception/InvalidPlayerName.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class InvalidPlayerName extends CustomException{ + + public InvalidPlayerName(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/exception/InvalidPlayersSizeException.java b/src/main/java/exception/InvalidPlayersSizeException.java new file mode 100644 index 0000000000..7242843584 --- /dev/null +++ b/src/main/java/exception/InvalidPlayersSizeException.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class InvalidPlayersSizeException extends CustomException{ + + public InvalidPlayersSizeException(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/exception/InvalidSeparatorException.java b/src/main/java/exception/InvalidSeparatorException.java new file mode 100644 index 0000000000..76538c156d --- /dev/null +++ b/src/main/java/exception/InvalidSeparatorException.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class InvalidSeparatorException extends CustomException{ + + public InvalidSeparatorException(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/exception/MessageDoesNotExistException.java b/src/main/java/exception/MessageDoesNotExistException.java new file mode 100644 index 0000000000..74251064ca --- /dev/null +++ b/src/main/java/exception/MessageDoesNotExistException.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class MessageDoesNotExistException extends CustomException{ + + public MessageDoesNotExistException(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/exception/NoMoreCardException.java b/src/main/java/exception/NoMoreCardException.java new file mode 100644 index 0000000000..25551e0a6b --- /dev/null +++ b/src/main/java/exception/NoMoreCardException.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class NoMoreCardException extends CustomException{ + + public NoMoreCardException(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/exception/ReservedPlayerNameException.java b/src/main/java/exception/ReservedPlayerNameException.java new file mode 100644 index 0000000000..836ba97541 --- /dev/null +++ b/src/main/java/exception/ReservedPlayerNameException.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class ReservedPlayerNameException extends CustomException { + + public ReservedPlayerNameException(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 5beb67c19c..c432099732 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -1,5 +1,8 @@ package view; +import constants.ErrorCode; +import exception.InvalidInputException; +import exception.InvalidSeparatorException; import java.util.List; import java.util.Scanner; @@ -33,13 +36,13 @@ public String readAnswer(String name) { private void validateBlank(final String rawNames) { if (rawNames == null || rawNames.isBlank()) { - throw new IllegalArgumentException("[ERROR] 유효하지 않은 입력입니다."); + throw new InvalidInputException(ErrorCode.INVALID_INPUT); } } private void validateSeparators(final String rawNames) { if (isInvalidSeparator(rawNames)) { - throw new IllegalArgumentException("[ERROR] 유효하지 않은 구분자입니다."); + throw new InvalidSeparatorException(ErrorCode.INVALID_SEPARATOR); } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 2eab6087c2..02e14fb8ec 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -3,6 +3,7 @@ import static domain.participant.Dealer.INIT_HANDS_SIZE; import static domain.participant.Dealer.THRESHOLD; +import constants.ErrorCode; import domain.Result; import domain.participant.Player; import dto.DealerHandsDto; @@ -10,6 +11,7 @@ import dto.ParticipantsDto; import java.util.List; import java.util.Map; +import view.message.ErrorCodeMessage; public class OutputView { @@ -17,6 +19,7 @@ public class OutputView { private static final String FORM = "%s카드: %s%n"; private static final String TOTAL_SUM_FORM = "%s 카드: %s - 결과: %d%n"; private static final String RESULT_FORM = "%s: %s%n"; + private static final String ERROR_FORM = "[ERROR] %s%n"; public void printStartDeal(final DealerHandsDto dealerHandsDto, final ParticipantsDto participantsDto) { @@ -77,7 +80,7 @@ public void printBlackJack() { System.out.printf("BLACK JACK!!!%n"); } - public void printException(final Throwable exception) { - System.out.println(exception.getMessage()); + public void printException(final ErrorCode errorCode) { + System.out.printf(ERROR_FORM, ErrorCodeMessage.from(errorCode).getMessage()); } } diff --git a/src/main/java/view/message/ErrorCodeMessage.java b/src/main/java/view/message/ErrorCodeMessage.java new file mode 100644 index 0000000000..507dc908f7 --- /dev/null +++ b/src/main/java/view/message/ErrorCodeMessage.java @@ -0,0 +1,49 @@ +package view.message; + +import constants.ErrorCode; +import exception.MessageDoesNotExistException; +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +public enum ErrorCodeMessage { + + NOT_EXIST_MESSAGE(ErrorCode.NOT_EXIST_MESSAGE, "해당 메시지가 없습니다."), + INVALID_SEPARATOR(ErrorCode.INVALID_SEPARATOR, "유효하지 않은 구분자입니다."), + INVALID_INPUT(ErrorCode.INVALID_INPUT, "유효하지 않은 입력입니다."), + INVALID_SIZE(ErrorCode.INVALID_SIZE, "유효하지 않은 참여자 수입니다."), + DUPLICATE_NAME(ErrorCode.DUPLICATE_NAME, "중복된 이름은 사용할 수 없습니다."), + RESERVED_NAME(ErrorCode.RESERVED_NAME, "이름은 딜러일 수 없습니다."), + BLANK_VALUE(ErrorCode.BLANK_VALUE, "이름은 공백일 수 없습니다."), + EMPTY_CARD(ErrorCode.EMPTY_CARD, "뽑을 수 있는 카드가 없습니다."), + INVALID_COMMAND(ErrorCode.INVALID_COMMAND, "y 또는 n을 입력해주세요"), + ; + + private static final Map SUIT_MESSAGE = Arrays.stream(values()) + .collect(Collectors.toMap(ErrorCodeMessage::getCode, Function.identity())); + + + private final ErrorCode errorCode; + private final String message; + + ErrorCodeMessage(final ErrorCode errorCode, final String message) { + this.errorCode = errorCode; + this.message = message; + } + + public static ErrorCodeMessage from(ErrorCode errorCode) { + if (SUIT_MESSAGE.containsKey(errorCode)) { + return SUIT_MESSAGE.get(errorCode); + } + throw new MessageDoesNotExistException(ErrorCode.NOT_EXIST_MESSAGE); + } + + private ErrorCode getCode() { + return errorCode; + } + + public String getMessage() { + return message; + } +} diff --git a/src/test/java/domain/AnswerTest.java b/src/test/java/domain/AnswerTest.java index 9b6f41a7c0..ea61387a17 100644 --- a/src/test/java/domain/AnswerTest.java +++ b/src/test/java/domain/AnswerTest.java @@ -1,5 +1,6 @@ package domain; +import exception.InvalidCommandException; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; @@ -12,7 +13,6 @@ class AnswerTest { @ValueSource(strings = {"Y", "nn", "aa"}) void invalidAnswer(String value) { Assertions.assertThatThrownBy(() -> Answer.from(value)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("[ERROR] 유효하지 않은 대답입니다."); + .isInstanceOf(InvalidCommandException.class); } } diff --git a/src/test/java/domain/CardDeckTest.java b/src/test/java/domain/CardDeckTest.java index 90629be835..0e88addbeb 100644 --- a/src/test/java/domain/CardDeckTest.java +++ b/src/test/java/domain/CardDeckTest.java @@ -1,6 +1,7 @@ package domain; import domain.card.CardDeck; +import exception.NoMoreCardException; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -32,7 +33,6 @@ void pop() { //then Assertions.assertThatThrownBy(cardDeck::pop) - .isInstanceOf(IllegalStateException.class) - .hasMessage("[ERROR] 카드덱이 비어있습니다."); + .isInstanceOf(NoMoreCardException.class); } } diff --git a/src/test/java/domain/NameTest.java b/src/test/java/domain/NameTest.java index 52eacd4b2c..32dce8a7e9 100644 --- a/src/test/java/domain/NameTest.java +++ b/src/test/java/domain/NameTest.java @@ -1,6 +1,7 @@ package domain; import domain.participant.Name; +import exception.InvalidPlayerName; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; @@ -14,8 +15,7 @@ class NameTest { @ValueSource(strings = {"", " ", " "}) void BlankInputThrowException(String value) { Assertions.assertThatThrownBy(() -> new Name(value)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("[ERROR] 참여자 이름에 공백을 입력할 수 없습니다."); + .isInstanceOf(InvalidPlayerName.class); } @DisplayName("null을 입력하면 예외를 발생시킨다.") @@ -23,7 +23,6 @@ void BlankInputThrowException(String value) { @NullSource void nullInputThrowException(String value) { Assertions.assertThatThrownBy(() -> new Name(value)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("[ERROR] 유효하지 않은 참여자 이름입니다."); + .isInstanceOf(InvalidPlayerName.class); } } diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index 8c4895ef99..b64563452c 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -3,6 +3,8 @@ import domain.participant.Hands; import domain.participant.Name; import domain.participant.Player; +import exception.InvalidPlayerName; +import exception.ReservedPlayerNameException; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -22,7 +24,6 @@ void validateName() { final Hands hands = Hands.createEmptyHands(); Assertions.assertThatThrownBy(() -> new Player(name, hands)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("[ERROR] 사용할 수 없는 이름입니다."); + .isInstanceOf(ReservedPlayerNameException.class); } } diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index cba3712341..c55c24ce2b 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -15,6 +15,8 @@ import domain.participant.Name; import domain.participant.Player; import domain.participant.Players; +import exception.DuplicatePlayerNameException; +import exception.InvalidPlayersSizeException; import java.util.List; import java.util.Map; import java.util.stream.Stream; @@ -35,8 +37,7 @@ void duplicatePlayerName() { //when & then Assertions.assertThatThrownBy(() -> Players.from(names)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("[ERROR] 참여자 이름은 중복될 수 없습니다."); + .isInstanceOf(DuplicatePlayerNameException.class); } @DisplayName("총 참여자 수가 2이상 8이하이면 참여자를 생성한다.") @@ -52,8 +53,7 @@ void validPlayersSize(final List names) { @MethodSource("invalidPlayersSizeParameterProvider") void invalidPlayersSize(final List names) { Assertions.assertThatThrownBy(() -> Players.from(names)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("[ERROR] 유효하지 않은 참여자 수입니다."); + .isInstanceOf(InvalidPlayersSizeException.class); } @Test From 1713750c2b8900aaaab6225159d50aea8dab8096 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 12 Mar 2024 20:33:03 +0900 Subject: [PATCH 096/100] =?UTF-8?q?refactor:=20Participant=20=EC=B6=94?= =?UTF-8?q?=EC=83=81=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 3 ++- src/main/java/controller/BlackJackController.java | 14 +++++--------- src/main/java/domain/participant/Dealer.java | 7 ++++--- src/main/java/domain/participant/Hands.java | 2 +- src/main/java/domain/participant/Participant.java | 5 ++++- src/main/java/domain/participant/Player.java | 5 +++++ src/main/java/view/OutputView.java | 8 ++++++++ 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/docs/README.md b/docs/README.md index 205b599645..946d17d8a9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -86,7 +86,8 @@ * 커스텀 예외 구현 * [ ] CardDeck 웨어하우스로 바라보기 * [ ] 컨트롤러 메서드 수정하기 -* [ ] Participant 추상 클래스 대신 클래스로 변경하기 +* [x] ~~Participant 추상 클래스 대신 클래스로 변경하기~~ + * 추상 클래스를 유지하고 추상 메서드 추가 * [ ] model 과 view 분리하기 * Result * Rank diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 36624f4009..214d9e4fdc 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -63,7 +63,7 @@ private void deal(final Player player, final Dealer dealer) { boolean turnEnded = false; while (!turnEnded) { - Answer answer = inputController.getAnswer(player.getName()); + final Answer answer = inputController.getAnswer(player.getName()); dealer.deal(player, answer); printHandsIfRequired(player, handsChanged, answer); @@ -80,15 +80,11 @@ private void printHandsIfRequired(final Player player, final boolean handsChange } private boolean isTurnEnded(final Player player, final Answer answer) { - if (player.isBust()) { - outputView.printBust(); - return true; + if (player.canDeal()) { + return !answer.isHit(); } - if (player.isBlackJack()) { - outputView.printBlackJack(); - return true; - } - return !answer.isHit(); + outputView.printDealEndMessage(player.isBust()); + return true; } private boolean shouldShowHands(final boolean handsChanged, final Answer answer) { diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 5ead0cec28..41a25d55b4 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -1,8 +1,8 @@ package domain.participant; import domain.Answer; -import domain.card.CardDeck; import domain.Result; +import domain.card.CardDeck; import java.util.EnumMap; import java.util.Map; @@ -38,7 +38,7 @@ public void deal(final Player player, final Answer answer) { } public void deal() { - while (isUnderThreshold()) { + while (canDeal()) { super.add(cardDeck.pop()); } } @@ -57,7 +57,8 @@ public Map getDealerResult(final Players players) { return dealerResult; } - private boolean isUnderThreshold() { + @Override + public boolean canDeal() { return handsSum() <= THRESHOLD; } } diff --git a/src/main/java/domain/participant/Hands.java b/src/main/java/domain/participant/Hands.java index 42e2f78027..102d2966e8 100644 --- a/src/main/java/domain/participant/Hands.java +++ b/src/main/java/domain/participant/Hands.java @@ -8,7 +8,7 @@ public class Hands { - private static final int BLACK_JACK = 21; + public static final int BLACK_JACK = 21; private static final int EXTRA_ACE_VALUE = 10; private final List cards; diff --git a/src/main/java/domain/participant/Participant.java b/src/main/java/domain/participant/Participant.java index f58029de41..0bfa97b988 100644 --- a/src/main/java/domain/participant/Participant.java +++ b/src/main/java/domain/participant/Participant.java @@ -1,7 +1,7 @@ package domain.participant; -import domain.card.Card; import domain.Result; +import domain.card.Card; import java.util.List; import java.util.Objects; @@ -15,6 +15,8 @@ protected Participant(final Name name, final Hands hands) { this.hands = hands; } + public abstract boolean canDeal(); + public void add(final Card card) { hands.add(card); } @@ -23,6 +25,7 @@ public boolean isBust() { return hands.isBust(); } + //TODO 시작시에 블랙잭일 때 쓸거에용 public boolean isBlackJack() { return hands.isBlackJack(); } diff --git a/src/main/java/domain/participant/Player.java b/src/main/java/domain/participant/Player.java index 7a45bef100..2c8a8c6da6 100644 --- a/src/main/java/domain/participant/Player.java +++ b/src/main/java/domain/participant/Player.java @@ -15,4 +15,9 @@ private void validate(final Name name) { throw new ReservedPlayerNameException(ErrorCode.RESERVED_NAME); } } + + @Override + public boolean canDeal() { + return handsSum() <= Hands.BLACK_JACK; + } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 02e14fb8ec..87c607de6b 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -83,4 +83,12 @@ public void printBlackJack() { public void printException(final ErrorCode errorCode) { System.out.printf(ERROR_FORM, ErrorCodeMessage.from(errorCode).getMessage()); } + + public void printDealEndMessage(final boolean isBust) { + if (isBust) { + printBust(); + return; + } + printBlackJack(); + } } From 167957a525f17677e35cebee9f140ffd4f2e6b21 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 12 Mar 2024 20:44:39 +0900 Subject: [PATCH 097/100] =?UTF-8?q?refactor:=20InvalidPlayerName=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/participant/Name.java | 6 +++--- src/main/java/exception/InvalidPlayerName.java | 10 ---------- .../java/exception/InvalidPlayerNameException.java | 10 ++++++++++ src/test/java/domain/NameTest.java | 6 +++--- src/test/java/domain/PlayerTest.java | 1 - 5 files changed, 16 insertions(+), 17 deletions(-) delete mode 100644 src/main/java/exception/InvalidPlayerName.java create mode 100644 src/main/java/exception/InvalidPlayerNameException.java diff --git a/src/main/java/domain/participant/Name.java b/src/main/java/domain/participant/Name.java index 692e2ad538..cf9d7a4aa8 100644 --- a/src/main/java/domain/participant/Name.java +++ b/src/main/java/domain/participant/Name.java @@ -1,7 +1,7 @@ package domain.participant; import constants.ErrorCode; -import exception.InvalidPlayerName; +import exception.InvalidPlayerNameException; public class Name { @@ -19,13 +19,13 @@ private void validate(final String name) { private void validateNull(final String name) { if (name == null) { - throw new InvalidPlayerName(ErrorCode.BLANK_VALUE); + throw new InvalidPlayerNameException(ErrorCode.BLANK_VALUE); } } private void validateBlank(final String name) { if (name.isBlank()) { - throw new InvalidPlayerName(ErrorCode.BLANK_VALUE); + throw new InvalidPlayerNameException(ErrorCode.BLANK_VALUE); } } diff --git a/src/main/java/exception/InvalidPlayerName.java b/src/main/java/exception/InvalidPlayerName.java deleted file mode 100644 index a5ae9971b0..0000000000 --- a/src/main/java/exception/InvalidPlayerName.java +++ /dev/null @@ -1,10 +0,0 @@ -package exception; - -import constants.ErrorCode; - -public class InvalidPlayerName extends CustomException{ - - public InvalidPlayerName(final ErrorCode errorCode) { - super(errorCode); - } -} diff --git a/src/main/java/exception/InvalidPlayerNameException.java b/src/main/java/exception/InvalidPlayerNameException.java new file mode 100644 index 0000000000..2bf217ea37 --- /dev/null +++ b/src/main/java/exception/InvalidPlayerNameException.java @@ -0,0 +1,10 @@ +package exception; + +import constants.ErrorCode; + +public class InvalidPlayerNameException extends CustomException{ + + public InvalidPlayerNameException(final ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/test/java/domain/NameTest.java b/src/test/java/domain/NameTest.java index 32dce8a7e9..401a38f783 100644 --- a/src/test/java/domain/NameTest.java +++ b/src/test/java/domain/NameTest.java @@ -1,7 +1,7 @@ package domain; import domain.participant.Name; -import exception.InvalidPlayerName; +import exception.InvalidPlayerNameException; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; @@ -15,7 +15,7 @@ class NameTest { @ValueSource(strings = {"", " ", " "}) void BlankInputThrowException(String value) { Assertions.assertThatThrownBy(() -> new Name(value)) - .isInstanceOf(InvalidPlayerName.class); + .isInstanceOf(InvalidPlayerNameException.class); } @DisplayName("null을 입력하면 예외를 발생시킨다.") @@ -23,6 +23,6 @@ void BlankInputThrowException(String value) { @NullSource void nullInputThrowException(String value) { Assertions.assertThatThrownBy(() -> new Name(value)) - .isInstanceOf(InvalidPlayerName.class); + .isInstanceOf(InvalidPlayerNameException.class); } } diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java index b64563452c..850c1599ec 100644 --- a/src/test/java/domain/PlayerTest.java +++ b/src/test/java/domain/PlayerTest.java @@ -3,7 +3,6 @@ import domain.participant.Hands; import domain.participant.Name; import domain.participant.Player; -import exception.InvalidPlayerName; import exception.ReservedPlayerNameException; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; From e9374bc3f9d1ae2cf2f3d4358d90984cd68bc79a Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 12 Mar 2024 21:47:31 +0900 Subject: [PATCH 098/100] =?UTF-8?q?refactor:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EC=B1=85=EC=9E=84=EC=9D=84=20Card?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EC=BA=90=EC=8B=9C=ED=95=98=EC=97=AC=20=EA=B0=80?= =?UTF-8?q?=EC=A0=B8=EC=98=A4=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/BlackJackController.java | 3 +-- src/main/java/domain/card/Card.java | 20 +++++++++++++++++ src/main/java/domain/card/CardDeck.java | 22 +++---------------- src/main/java/domain/participant/Dealer.java | 15 +++++++------ src/main/java/domain/participant/Name.java | 17 ++++++++++++++ src/main/java/domain/participant/Player.java | 4 +++- src/test/java/domain/CardDeckTest.java | 6 +++-- src/test/java/domain/CardTest.java | 20 +++++++++++++++++ src/test/java/domain/DealerTest.java | 13 +++++------ src/test/java/domain/PlayersTest.java | 5 ++--- 10 files changed, 83 insertions(+), 42 deletions(-) create mode 100644 src/test/java/domain/CardTest.java diff --git a/src/main/java/controller/BlackJackController.java b/src/main/java/controller/BlackJackController.java index 214d9e4fdc..e44b17ed96 100644 --- a/src/main/java/controller/BlackJackController.java +++ b/src/main/java/controller/BlackJackController.java @@ -1,7 +1,6 @@ package controller; import domain.Answer; -import domain.card.CardDeck; import domain.participant.Dealer; import domain.participant.Player; import domain.participant.Players; @@ -23,7 +22,7 @@ public BlackJackController(final InputController inputController, final OutputVi public void run() { final Players players = inputController.getPlayers(); - final Dealer dealer = new Dealer(CardDeck.generate()); + final Dealer dealer = new Dealer(); initHands(players, dealer); dealWithPlayers(players, dealer); diff --git a/src/main/java/domain/card/Card.java b/src/main/java/domain/card/Card.java index 254770fc9d..f32cdd138f 100644 --- a/src/main/java/domain/card/Card.java +++ b/src/main/java/domain/card/Card.java @@ -1,9 +1,19 @@ package domain.card; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; public class Card { + private static final List CACHE = new ArrayList<>(); + + static { + for (Shape shape : Shape.values()) { + generateCard(shape); + } + } + private final Rank rank; private final Shape shape; @@ -12,6 +22,16 @@ public Card(final Rank rank, final Shape shape) { this.shape = shape; } + private static void generateCard(final Shape shape) { + for (Rank rank : Rank.values()) { + CACHE.add(new Card(rank, shape)); + } + } + + public static List values() { + return List.copyOf(CACHE); + } + public boolean isAce() { return rank.isAce(); } diff --git a/src/main/java/domain/card/CardDeck.java b/src/main/java/domain/card/CardDeck.java index 25ce09adff..ff0e4440b7 100644 --- a/src/main/java/domain/card/CardDeck.java +++ b/src/main/java/domain/card/CardDeck.java @@ -4,28 +4,24 @@ import exception.NoMoreCardException; import java.util.ArrayDeque; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.List; public class CardDeck { - private static final int DECK_SIZE = 6; - private final Deque cards; private CardDeck(final Deque cards) { this.cards = cards; } - public static CardDeck generate() { + public static CardDeck generate(int size) { final List deck = new ArrayList<>(); - for (int i = 0; i < DECK_SIZE; i++) { - deck.addAll(generateOneCardDeck()); + for (int i = 0; i < size; i++) { + deck.addAll(Card.values()); } - Collections.shuffle(deck); return new CardDeck(new ArrayDeque<>(deck)); } @@ -40,16 +36,4 @@ public Card pop() { public int size() { return cards.size(); } - - private static List generateOneCardDeck() { - return Arrays.stream(Shape.values()) - .flatMap(shape -> matching(shape).stream()) - .toList(); - } - - private static List matching(final Shape shape) { - return Arrays.stream(Rank.values()) - .map(rank -> new Card(rank, shape)) - .toList(); - } } diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 41a25d55b4..7e32955290 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -10,18 +10,19 @@ public class Dealer extends Participant { public static final int INIT_HANDS_SIZE = 2; public static final int THRESHOLD = 16; - public static final String NAME = "딜러"; + public static final int DECK_SIZE = 6; + public static final Name DEALER_NAME = new Name("딜러"); private final CardDeck cardDeck; - public Dealer(final CardDeck cardDeck) { - super(new Name(NAME), Hands.createEmptyHands()); - this.cardDeck = cardDeck; + public Dealer() { + super(DEALER_NAME, Hands.createEmptyHands()); + this.cardDeck = CardDeck.generate(DECK_SIZE); } - public Dealer(final CardDeck cardDeck, final Hands hands) { - super(new Name(NAME), hands); - this.cardDeck = cardDeck; + public Dealer(final Hands hands) { + super(DEALER_NAME, hands); + this.cardDeck = CardDeck.generate(DECK_SIZE); } public void initHands(final Players players) { diff --git a/src/main/java/domain/participant/Name.java b/src/main/java/domain/participant/Name.java index cf9d7a4aa8..ffb95b64db 100644 --- a/src/main/java/domain/participant/Name.java +++ b/src/main/java/domain/participant/Name.java @@ -2,6 +2,7 @@ import constants.ErrorCode; import exception.InvalidPlayerNameException; +import java.util.Objects; public class Name { @@ -32,4 +33,20 @@ private void validateBlank(final String name) { public String getValue() { return value; } + + @Override + public boolean equals(final Object target) { + if (this == target) { + return true; + } + if (!(target instanceof Name name)) { + return false; + } + return Objects.equals(value, name.value); + } + + @Override + public int hashCode() { + return Objects.hashCode(value); + } } diff --git a/src/main/java/domain/participant/Player.java b/src/main/java/domain/participant/Player.java index 2c8a8c6da6..4674d6e99e 100644 --- a/src/main/java/domain/participant/Player.java +++ b/src/main/java/domain/participant/Player.java @@ -1,5 +1,7 @@ package domain.participant; +import static domain.participant.Dealer.DEALER_NAME; + import constants.ErrorCode; import exception.ReservedPlayerNameException; @@ -11,7 +13,7 @@ public Player(final Name name, final Hands hands) { } private void validate(final Name name) { - if (Dealer.NAME.equals(name.getValue())) { + if (DEALER_NAME.equals(name)) { throw new ReservedPlayerNameException(ErrorCode.RESERVED_NAME); } } diff --git a/src/test/java/domain/CardDeckTest.java b/src/test/java/domain/CardDeckTest.java index 0e88addbeb..00ebc0758d 100644 --- a/src/test/java/domain/CardDeckTest.java +++ b/src/test/java/domain/CardDeckTest.java @@ -1,5 +1,7 @@ package domain; +import static domain.participant.Dealer.DECK_SIZE; + import domain.card.CardDeck; import exception.NoMoreCardException; import org.assertj.core.api.Assertions; @@ -12,7 +14,7 @@ class CardDeckTest { @Test void generate() { // given - CardDeck cardDeck = CardDeck.generate(); + CardDeck cardDeck = CardDeck.generate(DECK_SIZE); // when && then Assertions.assertThat(cardDeck.size()).isEqualTo(52 * 6); @@ -22,7 +24,7 @@ void generate() { @DisplayName("카드가 없는데 카드를 뽑을 경우 예외가 발생한다.") void pop() { //given - CardDeck cardDeck = CardDeck.generate(); + CardDeck cardDeck = CardDeck.generate(DECK_SIZE); //when int cardSize = 52 * 6; diff --git a/src/test/java/domain/CardTest.java b/src/test/java/domain/CardTest.java new file mode 100644 index 0000000000..39c5667e1d --- /dev/null +++ b/src/test/java/domain/CardTest.java @@ -0,0 +1,20 @@ +package domain; + +import domain.card.Card; +import java.util.List; +import java.util.Set; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class CardTest { + + @Test + @DisplayName("중복없는 52장의 카드를 생성한다.") + void generate() { + List values = Card.values(); + + Assertions.assertThat(values).hasSize(52); + Assertions.assertThat(Set.copyOf(values)).hasSize(52); + } +} diff --git a/src/test/java/domain/DealerTest.java b/src/test/java/domain/DealerTest.java index d38c6534e6..fcec8af224 100644 --- a/src/test/java/domain/DealerTest.java +++ b/src/test/java/domain/DealerTest.java @@ -8,7 +8,6 @@ import static domain.Result.TIE; import static domain.Result.WIN; -import domain.card.CardDeck; import domain.participant.Dealer; import domain.participant.Hands; import domain.participant.Name; @@ -27,8 +26,7 @@ class DealerTest { void dealCards() { //given final Players players = Players.from(List.of("레디", "제제")); - final CardDeck cardDeck = CardDeck.generate(); - final Dealer dealer = new Dealer(cardDeck); + final Dealer dealer = new Dealer(); //when dealer.initHands(players); @@ -46,8 +44,7 @@ void addOneCard() { final Players players = new Players(List.of(hitPlayer, stayPlayer)); - final CardDeck cardDeck = CardDeck.generate(); - final Dealer dealer = new Dealer(cardDeck); + final Dealer dealer = new Dealer(); dealer.initHands(players); //when @@ -63,7 +60,7 @@ void addOneCard() { @DisplayName("딜러의 카드의 합이 17이상이 될때까지 카드를 추가한다.") void dealerDeal() { //given - final Dealer dealer = new Dealer(CardDeck.generate(), sum10Size2); + final Dealer dealer = new Dealer(sum10Size2); //when dealer.deal(); @@ -77,7 +74,7 @@ void dealerDeal() { @Test void dealerNoDeal() { //given - final Dealer dealer = new Dealer(CardDeck.generate(), sum18Size2); + final Dealer dealer = new Dealer(sum18Size2); //when dealer.deal(); @@ -97,7 +94,7 @@ void dealerResult() { Player tier = new Player(new Name("브라운"), sum20Size3); Players players = new Players(List.of(loser1, loser2, winner, tier)); - Dealer dealer = new Dealer(CardDeck.generate(), sum20Size3); + Dealer dealer = new Dealer(sum20Size3); // when Map expected = Map.of(WIN, 2, LOSE, 1, TIE, 1); diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java index c55c24ce2b..f038cb7a75 100644 --- a/src/test/java/domain/PlayersTest.java +++ b/src/test/java/domain/PlayersTest.java @@ -10,7 +10,6 @@ import static domain.Result.TIE; import static domain.Result.WIN; -import domain.card.CardDeck; import domain.participant.Dealer; import domain.participant.Name; import domain.participant.Player; @@ -92,7 +91,7 @@ void playerResult() { Player tier = new Player(new Name("수달"), sum20Size3); Players players = new Players(List.of(loser, winner, tier)); - Dealer dealer = new Dealer(CardDeck.generate(), sum20Size3); + Dealer dealer = new Dealer(sum20Size3); //when & then Map expected = Map.of(loser, LOSE, winner, WIN, tier, TIE); @@ -103,7 +102,7 @@ void playerResult() { @DisplayName("딜러가 버스트일때 참여자가 버스트가 아니면 WIN") void all() { //given - Dealer bustDealer = new Dealer(CardDeck.generate(), bustHands); + Dealer bustDealer = new Dealer(bustHands); Player winner1 = new Player(new Name("레디"), sum18Size2); Player winner2 = new Player(new Name("브라운"), sum20Size2); Player loser = new Player(new Name("제제"), bustHands); From 19b71c6256fe6fc93ca162c18f77b1cf3ffb27a1 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 12 Mar 2024 22:00:22 +0900 Subject: [PATCH 099/100] =?UTF-8?q?docs:=20=EB=A6=AC=ED=8C=A9=ED=84=B0?= =?UTF-8?q?=EB=A7=81=20=EB=AA=A9=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/docs/README.md b/docs/README.md index 946d17d8a9..b819b8e049 100644 --- a/docs/README.md +++ b/docs/README.md @@ -84,11 +84,7 @@ * [x] 예외 시 재입력 기능 * [x] 예외 메시지 상수화 * 커스텀 예외 구현 -* [ ] CardDeck 웨어하우스로 바라보기 -* [ ] 컨트롤러 메서드 수정하기 +* [x] CardDeck 웨어하우스로 바라보기 +* [ ] ~~컨트롤러 메서드 수정하기~~ * [x] ~~Participant 추상 클래스 대신 클래스로 변경하기~~ * 추상 클래스를 유지하고 추상 메서드 추가 -* [ ] model 과 view 분리하기 - * Result - * Rank - * Shape From 80e13e45dec4889d3ec2df0cfb777cd92e4d3b73 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Tue, 12 Mar 2024 22:13:20 +0900 Subject: [PATCH 100/100] =?UTF-8?q?refactor:=20cardDeck=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=B1=85=EC=9E=84=20=EC=9C=84=EC=B9=98=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 --- src/main/java/domain/card/CardDeck.java | 4 ++-- src/main/java/domain/participant/Dealer.java | 18 ++++++++++++++++-- .../java/domain/{ => card}/CardDeckTest.java | 7 +++---- 3 files changed, 21 insertions(+), 8 deletions(-) rename src/test/java/domain/{ => card}/CardDeckTest.java (86%) diff --git a/src/main/java/domain/card/CardDeck.java b/src/main/java/domain/card/CardDeck.java index ff0e4440b7..90432e4fa5 100644 --- a/src/main/java/domain/card/CardDeck.java +++ b/src/main/java/domain/card/CardDeck.java @@ -12,11 +12,11 @@ public class CardDeck { private final Deque cards; - private CardDeck(final Deque cards) { + public CardDeck(final Deque cards) { this.cards = cards; } - public static CardDeck generate(int size) { + protected static CardDeck generate(int size) { final List deck = new ArrayList<>(); for (int i = 0; i < size; i++) { diff --git a/src/main/java/domain/participant/Dealer.java b/src/main/java/domain/participant/Dealer.java index 7e32955290..08cc130792 100644 --- a/src/main/java/domain/participant/Dealer.java +++ b/src/main/java/domain/participant/Dealer.java @@ -2,8 +2,13 @@ import domain.Answer; import domain.Result; +import domain.card.Card; import domain.card.CardDeck; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; import java.util.EnumMap; +import java.util.List; import java.util.Map; public class Dealer extends Participant { @@ -17,12 +22,21 @@ public class Dealer extends Participant { public Dealer() { super(DEALER_NAME, Hands.createEmptyHands()); - this.cardDeck = CardDeck.generate(DECK_SIZE); + this.cardDeck = new CardDeck(generate()); } public Dealer(final Hands hands) { super(DEALER_NAME, hands); - this.cardDeck = CardDeck.generate(DECK_SIZE); + this.cardDeck = new CardDeck(generate()); + } + + private static ArrayDeque generate() { + final List deck = new ArrayList<>(); + for (int i = 0; i < DECK_SIZE; i++) { + deck.addAll(Card.values()); + } + Collections.shuffle(deck); + return new ArrayDeque<>(deck); } public void initHands(final Players players) { diff --git a/src/test/java/domain/CardDeckTest.java b/src/test/java/domain/card/CardDeckTest.java similarity index 86% rename from src/test/java/domain/CardDeckTest.java rename to src/test/java/domain/card/CardDeckTest.java index 00ebc0758d..a7bf0c60bd 100644 --- a/src/test/java/domain/CardDeckTest.java +++ b/src/test/java/domain/card/CardDeckTest.java @@ -1,8 +1,7 @@ -package domain; +package domain.card; import static domain.participant.Dealer.DECK_SIZE; -import domain.card.CardDeck; import exception.NoMoreCardException; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; @@ -24,10 +23,10 @@ void generate() { @DisplayName("카드가 없는데 카드를 뽑을 경우 예외가 발생한다.") void pop() { //given - CardDeck cardDeck = CardDeck.generate(DECK_SIZE); + CardDeck cardDeck = CardDeck.generate(1); //when - int cardSize = 52 * 6; + int cardSize = 52; while (cardSize > 0) { cardDeck.pop(); cardSize--;