From c214d87cc6eafe2ae63b7feeb7b4e7036f095a8a Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 09:30:16 +0900 Subject: [PATCH 01/18] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=20=EC=95=BC?= =?UTF-8?q?=EA=B5=AC=20=EA=B2=8C=EC=9E=84=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/BaseBall.java | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/BaseBall.java diff --git a/src/main/java/BaseBall.java b/src/main/java/BaseBall.java new file mode 100644 index 00000000..afb693e1 --- /dev/null +++ b/src/main/java/BaseBall.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/* +객체 지향 5대 원칙 SOLID +SRP(Single Responsibility Principle): 단일 책임 원칙 +OCP(Open Closed Principle): 개방 폐쇄 원칙 +LSP(Liskov Substitution Principle): 리스코프 치환 원칙 +ISP(Interface Segregation Principle): 인터페이스 분리 원칙 +DIP(Dependency Inversion Principle): 의존 역전 원칙 + */ + + +public class BaseBall { + + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + PlayBall play = new PlayBall(); + RandomBall random = new RandomBall(); + ValidBall validBall = new ValidBall(); + + + int number = random.makeRandomBall(); // 1 ~ 9 사이 서로 다른 수로 이루어진 세 자리 숫자 생성 + + + while (true) { + + System.out.println("숫자를 입력해 주세요 :"); + int answer = scanner.nextInt(); + validBall.valid(answer); + + List result = play.baseBall(number, answer); + + int ballCount = result.get(0); + int strikeCount = result.get(1); + + System.out.println("결과: " + ballCount + "볼, " + strikeCount + "스트라이크"); + if (strikeCount == 3) {System.out.println("정답입니다!"); break;} + + } + scanner.close(); + + } +} \ No newline at end of file From c253928eff4ceb5e1ccc128b43c0865d62ea6ce4 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 09:32:40 +0900 Subject: [PATCH 02/18] =?UTF-8?q?feat:=20Play=20Interface=20=EC=84=A4?= =?UTF-8?q?=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Play.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/java/Play.java diff --git a/src/main/java/Play.java b/src/main/java/Play.java new file mode 100644 index 00000000..af6269ab --- /dev/null +++ b/src/main/java/Play.java @@ -0,0 +1,6 @@ +import java.util.List; + +public interface Play { + + public List baseBall(int number, int answer); +} From 306cf197521d90993534e0864530fe851bfe2bf2 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 09:33:46 +0900 Subject: [PATCH 03/18] =?UTF-8?q?feat:=20Play=20Interface=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=ED=81=B4=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/PlayBall.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/PlayBall.java diff --git a/src/main/java/PlayBall.java b/src/main/java/PlayBall.java new file mode 100644 index 00000000..79c80d2a --- /dev/null +++ b/src/main/java/PlayBall.java @@ -0,0 +1,29 @@ +import java.util.ArrayList; +import java.util.List; + +public class PlayBall implements Play { + + @Override + public List baseBall(int number, int answer) { + List result = new ArrayList<>(); + String numberToStr = Integer.toString(number); + String answerToStr = Integer.toString(answer); + int ballCount = 0; + int strikeCount = 0; + + for (int i = 0; i < numberToStr.length(); i++) { + char numberChar = numberToStr.charAt(i); + for (int j = 0; j < answerToStr.length(); j++) { + char answerChar = answerToStr.charAt(j); + if (i == j && numberChar == answerChar) { + strikeCount += 1; + } else if (i != j && numberChar == answerChar) { + ballCount += 1; + } + } + } + result.add(ballCount); + result.add(strikeCount); + return result; + } +} From 38dd12766e28281fe424b674bf4090908af21972 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 09:34:17 +0900 Subject: [PATCH 04/18] =?UTF-8?q?feat:=20Random=20Interface=20=EC=84=A4?= =?UTF-8?q?=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Random.java | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/java/Random.java diff --git a/src/main/java/Random.java b/src/main/java/Random.java new file mode 100644 index 00000000..d3290218 --- /dev/null +++ b/src/main/java/Random.java @@ -0,0 +1,3 @@ +public interface Random { + public int makeRandomBall(); +} From 6ce268f2e0cdd96de9b87d35692f7ffc03b6375d Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 09:34:43 +0900 Subject: [PATCH 05/18] =?UTF-8?q?feat:=20Random=20Interface=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=ED=81=B4=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/RandomBall.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/RandomBall.java diff --git a/src/main/java/RandomBall.java b/src/main/java/RandomBall.java new file mode 100644 index 00000000..7026f9ca --- /dev/null +++ b/src/main/java/RandomBall.java @@ -0,0 +1,26 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class RandomBall implements Random{ + + @Override + public int makeRandomBall() { + List numbers = new ArrayList<>(); + + for (int i = 1; i <= 9; i++) { + numbers.add(i); + } + + Collections.shuffle(numbers); + StringBuilder randomNumberBuilder = new StringBuilder(); + + for (int i = 0; i < 3; i++) { + randomNumberBuilder.append(numbers.get(i)); + } + + int randomNumber = Integer.parseInt(randomNumberBuilder.toString()); + + return randomNumber; + } +} From 75e8a9dbc99326a6df74c09555b71d000b588d61 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 09:35:17 +0900 Subject: [PATCH 06/18] =?UTF-8?q?feat:=20Valid=20Interface=20=EC=84=A4?= =?UTF-8?q?=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Valid.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/Valid.java diff --git a/src/main/java/Valid.java b/src/main/java/Valid.java new file mode 100644 index 00000000..a58144f9 --- /dev/null +++ b/src/main/java/Valid.java @@ -0,0 +1,4 @@ +public interface Valid { + + public void valid(int number); +} From 1fc663d384bb574ad51c946e57ae047072909ee0 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 09:35:42 +0900 Subject: [PATCH 07/18] =?UTF-8?q?feat:=20Valid=20Interface=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=ED=81=B4=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ValidBall.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/ValidBall.java diff --git a/src/main/java/ValidBall.java b/src/main/java/ValidBall.java new file mode 100644 index 00000000..d9c6ab5c --- /dev/null +++ b/src/main/java/ValidBall.java @@ -0,0 +1,28 @@ +import java.util.HashSet; + +public class ValidBall implements Valid { + public ValidBall() { + super(); + } + + @Override + public void valid(int number) { + + HashSet set = new HashSet<>(); + + if (number < 111 && number > 999) { + throw new IllegalArgumentException("잘못된 범위의 입력입니다"); + } + + while (number > 0) { + int digit = number % 10; + if (!set.add(digit)) { + throw new IllegalArgumentException("입력된 숫자 중 중복되는 숫자가 있습니다."); + } + number /= 10; + } + + + + } +} From 06617fa856a920e09ac9cc03196a28371bb203bc Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 11:09:31 +0900 Subject: [PATCH 08/18] =?UTF-8?q?fix:=20=EB=B6=84=EA=B8=B0=EC=A0=90=20?= =?UTF-8?q?=EC=A1=B0=EA=B1=B4=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/ValidBall.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ValidBall.java b/src/main/java/ValidBall.java index d9c6ab5c..7387dde0 100644 --- a/src/main/java/ValidBall.java +++ b/src/main/java/ValidBall.java @@ -10,7 +10,7 @@ public void valid(int number) { HashSet set = new HashSet<>(); - if (number < 111 && number > 999) { + if ((number < 111) || (number > 999)) { throw new IllegalArgumentException("잘못된 범위의 입력입니다"); } From 42b2cb0b49b45ef965814f54ee2d86c19f02bd72 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 12:03:22 +0900 Subject: [PATCH 09/18] =?UTF-8?q?fix:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/BaseBall.java | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/BaseBall.java b/src/main/java/BaseBall.java index afb693e1..724afb62 100644 --- a/src/main/java/BaseBall.java +++ b/src/main/java/BaseBall.java @@ -15,15 +15,25 @@ public class BaseBall { public static void main(String[] args) { + baseball(); + while (true) { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + + Scanner scanner = new Scanner(System.in); + int answer = scanner.nextInt(); + if (answer == 1) {baseball();} + else if (answer == 2) {scanner.close(); break;} + } + } + + private static void baseball() { Scanner scanner = new Scanner(System.in); PlayBall play = new PlayBall(); RandomBall random = new RandomBall(); ValidBall validBall = new ValidBall(); - - int number = random.makeRandomBall(); // 1 ~ 9 사이 서로 다른 수로 이루어진 세 자리 숫자 생성 - + int number = random.makeRandomBall(); while (true) { @@ -36,11 +46,10 @@ public static void main(String[] args) { int ballCount = result.get(0); int strikeCount = result.get(1); - System.out.println("결과: " + ballCount + "볼, " + strikeCount + "스트라이크"); - if (strikeCount == 3) {System.out.println("정답입니다!"); break;} + if ((ballCount==0)&&(strikeCount==0)) {System.out.println("낫싱");} + else {System.out.println("결과: " + ballCount + "볼, " + strikeCount + "스트라이크");} + if (strikeCount == 3) {System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); break;} } - scanner.close(); - } } \ No newline at end of file From a7f338ed1a9f097ea2c9dd9462e395113fcec85b Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 29 Apr 2024 12:09:29 +0900 Subject: [PATCH 10/18] =?UTF-8?q?refactor:=20=EC=9D=98=EB=AF=B8=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EC=BD=94=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/ValidBall.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/ValidBall.java b/src/main/java/ValidBall.java index 7387dde0..8d62b1bb 100644 --- a/src/main/java/ValidBall.java +++ b/src/main/java/ValidBall.java @@ -1,9 +1,6 @@ import java.util.HashSet; public class ValidBall implements Valid { - public ValidBall() { - super(); - } @Override public void valid(int number) { @@ -21,8 +18,5 @@ public void valid(int number) { } number /= 10; } - - - } } From ba69f7948c3c95b6b64a45fd804c9b3c475345ae Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 6 May 2024 00:51:10 +0900 Subject: [PATCH 11/18] =?UTF-8?q?feat:=20=EC=8A=A4=ED=8A=B8=EB=9D=BC?= =?UTF-8?q?=EC=9D=B4=ED=81=AC=20or=20=EB=B3=BC=20=ED=8C=90=EB=B3=84=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/AutomaticBallStrikeSystem.java | 45 +++++++++++++++++++ .../java/{BaseBall.java => MajorLeague.java} | 24 ++++------ src/main/java/Play.java | 6 --- src/main/java/PlayBall.java | 29 ------------ src/main/java/Random.java | 3 -- src/main/java/Valid.java | 4 -- 6 files changed, 53 insertions(+), 58 deletions(-) create mode 100644 src/main/java/AutomaticBallStrikeSystem.java rename src/main/java/{BaseBall.java => MajorLeague.java} (68%) delete mode 100644 src/main/java/Play.java delete mode 100644 src/main/java/PlayBall.java delete mode 100644 src/main/java/Random.java delete mode 100644 src/main/java/Valid.java diff --git a/src/main/java/AutomaticBallStrikeSystem.java b/src/main/java/AutomaticBallStrikeSystem.java new file mode 100644 index 00000000..1bc4e632 --- /dev/null +++ b/src/main/java/AutomaticBallStrikeSystem.java @@ -0,0 +1,45 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; + +public class AutomaticBallStrikeSystem { + + public Result determine(int number, int answer) { + int strikeCount = determineStrike(number, answer); + int ballCount = determineBall(number, answer); + return new Result(strikeCount, ballCount); + } + + private int determineStrike(int number, int answer) { + int strike = 0; + while (number > 0) { + Integer unitsOfNumber = number % 10; + Integer unitsOfAnswer = answer % 10; + if (unitsOfNumber == unitsOfAnswer) { + strike += 1; + } + number /= 10; + answer /= 10; + } + return strike; + } + + private int determineBall(Integer number, Integer answer) { + int ball = 0; + String numToStr = number.toString(); + String ansToStr = answer.toString(); + + for (int i = 0; i < numToStr.length(); i++) { + char numChar = numToStr.charAt(i); + for (int j = 0; j < ansToStr.length(); j++) { + char ansChar = ansToStr.charAt(j); + if (i != j && numChar == ansChar) { + ball += 1; + } + } + } + return ball; + } +} + diff --git a/src/main/java/BaseBall.java b/src/main/java/MajorLeague.java similarity index 68% rename from src/main/java/BaseBall.java rename to src/main/java/MajorLeague.java index 724afb62..1c6ccef9 100644 --- a/src/main/java/BaseBall.java +++ b/src/main/java/MajorLeague.java @@ -1,3 +1,6 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -12,33 +15,22 @@ */ -public class BaseBall { +public class MajorLeague { - public static void main(String[] args) { - baseball(); - while (true) { - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - - Scanner scanner = new Scanner(System.in); - int answer = scanner.nextInt(); - if (answer == 1) {baseball();} - else if (answer == 2) {scanner.close(); break;} - } - } - - private static void baseball() { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static void playBaseBall() throws IOException { - Scanner scanner = new Scanner(System.in); PlayBall play = new PlayBall(); RandomBall random = new RandomBall(); ValidBall validBall = new ValidBall(); + int number = random.makeRandomBall(); while (true) { System.out.println("숫자를 입력해 주세요 :"); - int answer = scanner.nextInt(); + Integer answer = Integer.parseInt(br.readLine()); validBall.valid(answer); List result = play.baseBall(number, answer); diff --git a/src/main/java/Play.java b/src/main/java/Play.java deleted file mode 100644 index af6269ab..00000000 --- a/src/main/java/Play.java +++ /dev/null @@ -1,6 +0,0 @@ -import java.util.List; - -public interface Play { - - public List baseBall(int number, int answer); -} diff --git a/src/main/java/PlayBall.java b/src/main/java/PlayBall.java deleted file mode 100644 index 79c80d2a..00000000 --- a/src/main/java/PlayBall.java +++ /dev/null @@ -1,29 +0,0 @@ -import java.util.ArrayList; -import java.util.List; - -public class PlayBall implements Play { - - @Override - public List baseBall(int number, int answer) { - List result = new ArrayList<>(); - String numberToStr = Integer.toString(number); - String answerToStr = Integer.toString(answer); - int ballCount = 0; - int strikeCount = 0; - - for (int i = 0; i < numberToStr.length(); i++) { - char numberChar = numberToStr.charAt(i); - for (int j = 0; j < answerToStr.length(); j++) { - char answerChar = answerToStr.charAt(j); - if (i == j && numberChar == answerChar) { - strikeCount += 1; - } else if (i != j && numberChar == answerChar) { - ballCount += 1; - } - } - } - result.add(ballCount); - result.add(strikeCount); - return result; - } -} diff --git a/src/main/java/Random.java b/src/main/java/Random.java deleted file mode 100644 index d3290218..00000000 --- a/src/main/java/Random.java +++ /dev/null @@ -1,3 +0,0 @@ -public interface Random { - public int makeRandomBall(); -} diff --git a/src/main/java/Valid.java b/src/main/java/Valid.java deleted file mode 100644 index a58144f9..00000000 --- a/src/main/java/Valid.java +++ /dev/null @@ -1,4 +0,0 @@ -public interface Valid { - - public void valid(int number); -} From 4360cb929508342bb44e8fe0332592c1f8fdc04e Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 6 May 2024 00:52:21 +0900 Subject: [PATCH 12/18] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=20=EC=95=BC?= =?UTF-8?q?=EA=B5=AC=20=ED=94=8C=EB=A0=88=EC=9D=B4=20=EA=B3=BC=EC=A0=95=20?= =?UTF-8?q?=EB=B0=8F=20=EC=A2=85=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/MajorLeague.java | 71 +++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/src/main/java/MajorLeague.java b/src/main/java/MajorLeague.java index 1c6ccef9..4d7bc222 100644 --- a/src/main/java/MajorLeague.java +++ b/src/main/java/MajorLeague.java @@ -5,43 +5,68 @@ import java.util.List; import java.util.Scanner; -/* -객체 지향 5대 원칙 SOLID -SRP(Single Responsibility Principle): 단일 책임 원칙 -OCP(Open Closed Principle): 개방 폐쇄 원칙 -LSP(Liskov Substitution Principle): 리스코프 치환 원칙 -ISP(Interface Segregation Principle): 인터페이스 분리 원칙 -DIP(Dependency Inversion Principle): 의존 역전 원칙 - */ - - public class MajorLeague { - static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - static void playBaseBall() throws IOException { + static boolean playBaseBall() throws IOException { - PlayBall play = new PlayBall(); + AutomaticBallStrikeSystem ABS = new AutomaticBallStrikeSystem(); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); RandomBall random = new RandomBall(); ValidBall validBall = new ValidBall(); - - int number = random.makeRandomBall(); + System.out.println(number); while (true) { System.out.println("숫자를 입력해 주세요 :"); - Integer answer = Integer.parseInt(br.readLine()); - validBall.valid(answer); + Integer answer = getUserInput(); + validBall.validate(answer); - List result = play.baseBall(number, answer); + Result result = ABS.determine(number, answer); - int ballCount = result.get(0); - int strikeCount = result.get(1); + int ballCount = result.getBallCount(); + int strikeCount = result.getStrikeCount(); + referee(ballCount, strikeCount); + break; + } + return false; + } - if ((ballCount==0)&&(strikeCount==0)) {System.out.println("낫싱");} - else {System.out.println("결과: " + ballCount + "볼, " + strikeCount + "스트라이크");} + private static void referee(int ballCount, int strikeCount) throws IOException { + if ((ballCount == 0) && (strikeCount == 0)) { + System.out.println("낫싱"); + } else if ((ballCount != 0) && (strikeCount != 0)) { + System.out.println("결과: " + ballCount + "볼, " + strikeCount + "스트라이크"); + } + else if (strikeCount == 3) { + decideGameEnd(); + } + } - if (strikeCount == 3) {System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); break;} + private static boolean decideGameEnd() throws IOException { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + + Integer answer = getUserInput(); + if (answer.equals(1)) { + MajorLeague.playBaseBall(); + } else if (answer.equals(2)) { + return false; } + handleInvalidInput(); + return decideGameEnd(); + } + + + private static Integer getUserInput() throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + Integer answer = Integer.parseInt(br.readLine()); + return answer; } + + + private static void handleInvalidInput() { + System.out.println("잘못된 입력입니다. 다시 시도하세요."); + } + } \ No newline at end of file From 57e965d653c313b8fad9bc3a1762ef87c2bbdf03 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 6 May 2024 00:53:02 +0900 Subject: [PATCH 13/18] =?UTF-8?q?feat:=20=EC=BB=B4=ED=93=A8=ED=84=B0=20?= =?UTF-8?q?=EB=9E=9C=EB=8D=A4=20=EB=B3=BC=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/RandomBall.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/RandomBall.java b/src/main/java/RandomBall.java index 7026f9ca..1d307df5 100644 --- a/src/main/java/RandomBall.java +++ b/src/main/java/RandomBall.java @@ -2,9 +2,8 @@ import java.util.Collections; import java.util.List; -public class RandomBall implements Random{ +public class RandomBall { - @Override public int makeRandomBall() { List numbers = new ArrayList<>(); From 29134cdbe0629c5868c7f26d3e8a0b63ab67204b Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 6 May 2024 00:53:36 +0900 Subject: [PATCH 14/18] =?UTF-8?q?feat:=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=EB=B0=9B=EC=9D=80=20=EA=B3=B5=20=EC=9C=A0?= =?UTF-8?q?=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ValidBall.java | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/ValidBall.java b/src/main/java/ValidBall.java index 8d62b1bb..fcb010ed 100644 --- a/src/main/java/ValidBall.java +++ b/src/main/java/ValidBall.java @@ -1,22 +1,35 @@ import java.util.HashSet; -public class ValidBall implements Valid { +public class ValidBall { - @Override - public void valid(int number) { - - HashSet set = new HashSet<>(); + public void validate(int answer) { + validateInputType(answer); + validateRange(answer); + validateDuplication(answer); + } - if ((number < 111) || (number > 999)) { - throw new IllegalArgumentException("잘못된 범위의 입력입니다"); + // 아래 validateInputTpye 메서드가 의미가 있을까? MajorLeague에서 결국 인풋 받을 때 Integer.parseInt로 파싱하는데 문자열이 입력되면 + //결국 거기서 에러가 발생할텐데, 그럼 이건 테스트에 넣자 + private void validateInputType(T answer) { + if (!(answer instanceof Integer)) { + throw new IllegalArgumentException("잘못된 입력입니다. 다시 시도하세요."); } + } - while (number > 0) { - int digit = number % 10; + private void validateDuplication(int answer) { + HashSet set = new HashSet<>(); + while (answer > 0) { + int digit = answer % 10; if (!set.add(digit)) { throw new IllegalArgumentException("입력된 숫자 중 중복되는 숫자가 있습니다."); } - number /= 10; + answer /= 10; + } + } + + private void validateRange(int number) { + if ((number < 111) || (number > 999)) { + throw new IllegalArgumentException("잘못된 범위의 입력입니다"); } } } From 1462c35e7ef97b31f06273bfe91b4d5c55bb4699 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 6 May 2024 00:54:01 +0900 Subject: [PATCH 15/18] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=20=EC=95=BC?= =?UTF-8?q?=EA=B5=AC=20=EC=8B=A4=ED=96=89=EB=B6=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/BaseBallApplication.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/BaseBallApplication.java diff --git a/src/main/java/BaseBallApplication.java b/src/main/java/BaseBallApplication.java new file mode 100644 index 00000000..b66adcf5 --- /dev/null +++ b/src/main/java/BaseBallApplication.java @@ -0,0 +1,15 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Scanner; + +public class BaseBallApplication { + + public static void main(String[] args) throws IOException { + do { + if (!MajorLeague.playBaseBall()) { + break; + } + } while (true); + } +} \ No newline at end of file From 7c8b20a9d7b79c475821ba4c28f583a5f3580567 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 6 May 2024 00:54:49 +0900 Subject: [PATCH 16/18] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=20=EC=95=BC?= =?UTF-8?q?=EA=B5=AC=20=ED=8C=90=EB=8F=85=20=EA=B2=B0=EA=B3=BC=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Result.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/java/Result.java diff --git a/src/main/java/Result.java b/src/main/java/Result.java new file mode 100644 index 00000000..806bfdd6 --- /dev/null +++ b/src/main/java/Result.java @@ -0,0 +1,17 @@ +public class Result { + private Integer strikeCount; + private Integer ballCount; + + public Result(Integer strikeCount, Integer ballCount) { + this.strikeCount = strikeCount; + this.ballCount = ballCount; + } + + public Integer getStrikeCount() { + return strikeCount; + } + + public Integer getBallCount() { + return ballCount; + } +} From 255163cc80691dca5cc85769a0d779353d02dad0 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Mon, 6 May 2024 03:51:45 +0900 Subject: [PATCH 17/18] =?UTF-8?q?docs:=20=EC=88=AB=EC=9E=90=20=EC=95=BC?= =?UTF-8?q?=EA=B5=AC=20=EC=84=A4=EB=AA=85=EC=84=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d7e8aee..18801bd8 100644 --- a/README.md +++ b/README.md @@ -1 +1,25 @@ -# java-baseball-precourse \ No newline at end of file +## 구현 기능 + +### BaseBallApplication + +- 숫자 야구 시작 기능 + +### MajorLeague + +- 야구 게임의 전반적인 흐름 제어 기능 + +### AutomaticBallStrikeSystem + +- 볼(Strike/Ball) 판독 기능 + +### RandomBall + +- 컴퓨터의 랜덤 공 생성 기능 + +### Result + +- 판독된 볼의 리턴을 담당하는 클래스 + +### ValidBall + +- 사용자로부터 입력받은 숫자가 유효한지 확인하는 기능 From ad0637508c52119f8047de76039c656f185b1473 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Tue, 7 May 2024 21:33:57 +0900 Subject: [PATCH 18/18] =?UTF-8?q?fix:=20=EA=B8=B0=EB=8A=A5=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/BaseBallApplication.java | 2 +- src/main/java/MajorLeague.java | 13 +++++-------- src/main/java/ValidBall.java | 9 --------- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/main/java/BaseBallApplication.java b/src/main/java/BaseBallApplication.java index b66adcf5..48f94408 100644 --- a/src/main/java/BaseBallApplication.java +++ b/src/main/java/BaseBallApplication.java @@ -7,7 +7,7 @@ public class BaseBallApplication { public static void main(String[] args) throws IOException { do { - if (!MajorLeague.playBaseBall()) { + if (!MajorLeague.playBaseball()) { break; } } while (true); diff --git a/src/main/java/MajorLeague.java b/src/main/java/MajorLeague.java index 4d7bc222..3ee17ed5 100644 --- a/src/main/java/MajorLeague.java +++ b/src/main/java/MajorLeague.java @@ -7,14 +7,12 @@ public class MajorLeague { - static boolean playBaseBall() throws IOException { + static boolean playBaseball() throws IOException { AutomaticBallStrikeSystem ABS = new AutomaticBallStrikeSystem(); - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); RandomBall random = new RandomBall(); ValidBall validBall = new ValidBall(); int number = random.makeRandomBall(); - System.out.println(number); while (true) { @@ -27,9 +25,7 @@ static boolean playBaseBall() throws IOException { int ballCount = result.getBallCount(); int strikeCount = result.getStrikeCount(); referee(ballCount, strikeCount); - break; } - return false; } private static void referee(int ballCount, int strikeCount) throws IOException { @@ -48,9 +44,10 @@ private static boolean decideGameEnd() throws IOException { System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); Integer answer = getUserInput(); - if (answer.equals(1)) { - MajorLeague.playBaseBall(); - } else if (answer.equals(2)) { + if (answer == 1) { + MajorLeague.playBaseball(); + } + if (answer == 2) { return false; } handleInvalidInput(); diff --git a/src/main/java/ValidBall.java b/src/main/java/ValidBall.java index fcb010ed..f2577805 100644 --- a/src/main/java/ValidBall.java +++ b/src/main/java/ValidBall.java @@ -3,19 +3,10 @@ public class ValidBall { public void validate(int answer) { - validateInputType(answer); validateRange(answer); validateDuplication(answer); } - // 아래 validateInputTpye 메서드가 의미가 있을까? MajorLeague에서 결국 인풋 받을 때 Integer.parseInt로 파싱하는데 문자열이 입력되면 - //결국 거기서 에러가 발생할텐데, 그럼 이건 테스트에 넣자 - private void validateInputType(T answer) { - if (!(answer instanceof Integer)) { - throw new IllegalArgumentException("잘못된 입력입니다. 다시 시도하세요."); - } - } - private void validateDuplication(int answer) { HashSet set = new HashSet<>(); while (answer > 0) {