From 115c412740e1f57a95dcae99142dd271404143bf Mon Sep 17 00:00:00 2001 From: leesewon00 Date: Mon, 26 Jun 2023 22:58:25 +0900 Subject: [PATCH 1/7] =?UTF-8?q?20230626=20=EA=B8=B0=EB=B3=B8=EB=8F=99?= =?UTF-8?q?=EC=9E=91=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/Application.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 7f1901b8b..ec5af24c1 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -3,5 +3,7 @@ public class Application { public static void main(String[] args) { //TODO: 숫자 야구 게임 구현 + Game game = new Game(); + game.start(); } } From f51401b5d11a140cb125f06cce1e380fab1ecee8 Mon Sep 17 00:00:00 2001 From: leesewon00 Date: Mon, 26 Jun 2023 22:59:19 +0900 Subject: [PATCH 2/7] =?UTF-8?q?20230626=20=EA=B8=B0=EB=B3=B8=EB=8F=99?= =?UTF-8?q?=EC=9E=91=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/Game.java | 136 +++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 src/main/java/baseball/Game.java diff --git a/src/main/java/baseball/Game.java b/src/main/java/baseball/Game.java new file mode 100644 index 000000000..add398b68 --- /dev/null +++ b/src/main/java/baseball/Game.java @@ -0,0 +1,136 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; + +public class Game { + + public void start(){ + + while(true){ + //랜덤 숫자 배열 생성 + int[] question = makeQuestion(); + + //3스트라이크면 중단 + boolean result = false; + while(!result){ + + /* + System.out.println("랜덤 숫자입니다:"); + for(int i = 0; i<3; i++){ + System.out.print(question[i]); + } + */ + + System.out.print("숫자를 입력해주세요 : "); + // 예외처리 필요 + int[] inputNumbers = inputNumbers(); + + // 스트라이크, 볼 체크 + String check = check(question, inputNumbers); + + // 결과처리 + result = printResult(check); + } + + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String str = Console.readLine(); + if(str.equals("2")){ + break; + } + } + + + + } + + public int[] makeQuestion(){ + int[] question = new int[3]; + for(int i = 0; i<3; i++){ + int n = Randoms.pickNumberInRange(1, 9); + question[i] = n; + } + + // 중복체크 필요 + boolean flag = duplicateCheck(question); + if(flag==true){ + return question; + }else{ + return makeQuestion(); + } + } + + public boolean duplicateCheck(int[]question){ + for(int i = 0; i<3; i++){ + for(int j = 0; j<3; j++){ + if(question[i]==question[j]&&i!=j){ + return false; + } + } + } + return true; + } + + public int[] inputNumbers(){ + int[] arr = new int[3]; + String str = Console.readLine(); + + if(str.length()==3){ + String[] split = str.split(""); + for(int i = 0; i<3; i++){ + int temp = Integer.parseInt(split[i]); + arr[i] = temp; + } + return arr; + }else{ + throw new IllegalArgumentException(); + } + + + + } + + public String check(int question[], int inputNumbers[]){ + + int strike = 0; + int ball = 0; + + // 존재여부 동일성, 위치 동일성 판별 + for(int i = 0; i<3; i++){ + for(int j = 0; j<3; j++){ + if(question[i]==inputNumbers[j]&&i==j){ + strike++; + } + else if(question[i]==inputNumbers[j]&&i!=j){ + ball++; + } + } + } + + // 결과를 어떻게 return 해줘야할까 + return Integer.toString(strike)+Integer.toString(ball); + } + + public boolean printResult(String check){ + String[] split = check.split(""); + + if(split[0].equals("3")){ + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + return true; + } + else if(split[0].equals("0")&&split[1].equals("0")){ + System.out.println("낫싱"); + return false; + }else if(split[0].equals("0")){ + System.out.println(split[1]+"볼"); + return false; + }else if(split[1].equals("0")){ + System.out.println(split[0]+"스트라이크"); + return false; + }else{ + System.out.println(split[1]+"볼 "+split[0]+"스트라이크"); + return false; + } + } +} From ce386fcb16770989234b41ffaa5f2c71a6c22893 Mon Sep 17 00:00:00 2001 From: leesewon00 Date: Mon, 26 Jun 2023 23:14:50 +0900 Subject: [PATCH 3/7] =?UTF-8?q?20230626=20=EA=B8=B0=EB=B3=B8=EB=8F=99?= =?UTF-8?q?=EC=9E=91=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/Game.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baseball/Game.java b/src/main/java/baseball/Game.java index add398b68..3c09717c6 100644 --- a/src/main/java/baseball/Game.java +++ b/src/main/java/baseball/Game.java @@ -90,7 +90,7 @@ public int[] inputNumbers(){ } - public String check(int question[], int inputNumbers[]){ + public String check(int[] question, int[] inputNumbers){ int strike = 0; int ball = 0; From 034032919e25e5db6974ea60f07622809f9df7ee Mon Sep 17 00:00:00 2001 From: leesewon00 Date: Thu, 29 Jun 2023 19:40:47 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=EC=9D=B4=EC=84=B8=EC=9B=90=20=EA=B3=BC?= =?UTF-8?q?=EC=A0=9C=20=EC=A0=9C=EC=B6=9C=ED=95=A9=EB=8B=88=EB=8B=A4(1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 2 + src/main/java/baseball/Game.java | 136 -------------------- src/test/java/baseball/ApplicationTest.java | 33 ++++- 3 files changed, 34 insertions(+), 137 deletions(-) delete mode 100644 src/main/java/baseball/Game.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index ec5af24c1..0d56b6417 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,5 +1,7 @@ package baseball; +import baseball.game.Game; + public class Application { public static void main(String[] args) { //TODO: 숫자 야구 게임 구현 diff --git a/src/main/java/baseball/Game.java b/src/main/java/baseball/Game.java deleted file mode 100644 index 3c09717c6..000000000 --- a/src/main/java/baseball/Game.java +++ /dev/null @@ -1,136 +0,0 @@ -package baseball; - -import camp.nextstep.edu.missionutils.Console; -import camp.nextstep.edu.missionutils.Randoms; - -public class Game { - - public void start(){ - - while(true){ - //랜덤 숫자 배열 생성 - int[] question = makeQuestion(); - - //3스트라이크면 중단 - boolean result = false; - while(!result){ - - /* - System.out.println("랜덤 숫자입니다:"); - for(int i = 0; i<3; i++){ - System.out.print(question[i]); - } - */ - - System.out.print("숫자를 입력해주세요 : "); - // 예외처리 필요 - int[] inputNumbers = inputNumbers(); - - // 스트라이크, 볼 체크 - String check = check(question, inputNumbers); - - // 결과처리 - result = printResult(check); - } - - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - String str = Console.readLine(); - if(str.equals("2")){ - break; - } - } - - - - } - - public int[] makeQuestion(){ - int[] question = new int[3]; - for(int i = 0; i<3; i++){ - int n = Randoms.pickNumberInRange(1, 9); - question[i] = n; - } - - // 중복체크 필요 - boolean flag = duplicateCheck(question); - if(flag==true){ - return question; - }else{ - return makeQuestion(); - } - } - - public boolean duplicateCheck(int[]question){ - for(int i = 0; i<3; i++){ - for(int j = 0; j<3; j++){ - if(question[i]==question[j]&&i!=j){ - return false; - } - } - } - return true; - } - - public int[] inputNumbers(){ - int[] arr = new int[3]; - String str = Console.readLine(); - - if(str.length()==3){ - String[] split = str.split(""); - for(int i = 0; i<3; i++){ - int temp = Integer.parseInt(split[i]); - arr[i] = temp; - } - return arr; - }else{ - throw new IllegalArgumentException(); - } - - - - } - - public String check(int[] question, int[] inputNumbers){ - - int strike = 0; - int ball = 0; - - // 존재여부 동일성, 위치 동일성 판별 - for(int i = 0; i<3; i++){ - for(int j = 0; j<3; j++){ - if(question[i]==inputNumbers[j]&&i==j){ - strike++; - } - else if(question[i]==inputNumbers[j]&&i!=j){ - ball++; - } - } - } - - // 결과를 어떻게 return 해줘야할까 - return Integer.toString(strike)+Integer.toString(ball); - } - - public boolean printResult(String check){ - String[] split = check.split(""); - - if(split[0].equals("3")){ - System.out.println("3스트라이크"); - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - return true; - } - else if(split[0].equals("0")&&split[1].equals("0")){ - System.out.println("낫싱"); - return false; - }else if(split[0].equals("0")){ - System.out.println(split[1]+"볼"); - return false; - }else if(split[1].equals("0")){ - System.out.println(split[0]+"스트라이크"); - return false; - }else{ - System.out.println(split[1]+"볼 "+split[0]+"스트라이크"); - return false; - } - } -} diff --git a/src/test/java/baseball/ApplicationTest.java b/src/test/java/baseball/ApplicationTest.java index 359aacfa4..057710c26 100644 --- a/src/test/java/baseball/ApplicationTest.java +++ b/src/test/java/baseball/ApplicationTest.java @@ -21,16 +21,47 @@ class ApplicationTest extends NsTest { ); } + @Test + void 모든_경우의_수() { + assertRandomNumberInRangeTest( + () -> { + run("456", + "416", "412", "312", + "136", "132", + "145", "124", "123", + "2" + ); + assertThat(output()).contains( + "낫싱", + "1볼", "2볼", "3볼", + "1볼 1스트라이크", "2볼 1스트라이크", + "1스트라이크", "2스트라이크", "3스트라이크", + "게임 종료"); + }, + 1, 2, 3 + ); + } + @Test void 예외_테스트() { assertSimpleTest(() -> assertThatThrownBy(() -> runException("1234")) .isInstanceOf(IllegalArgumentException.class) ); + + assertSimpleTest(() -> + assertThatThrownBy(() -> runException("023")) + .isInstanceOf(IllegalArgumentException.class) + ); + + assertSimpleTest(() -> + assertThatThrownBy(() -> runException("112")) + .isInstanceOf(IllegalArgumentException.class) + ); } @Override public void runMain() { Application.main(new String[]{}); } -} +} \ No newline at end of file From 6599c419d012c4962d858b08245f672b325eca10 Mon Sep 17 00:00:00 2001 From: leesewon00 Date: Thu, 29 Jun 2023 19:42:16 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=EC=9D=B4=EC=84=B8=EC=9B=90=20=EA=B3=BC?= =?UTF-8?q?=EC=A0=9C=20=EC=A0=9C=EC=B6=9C=ED=95=A9=EB=8B=88=EB=8B=A4(1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/game/Game.java | 128 ++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/main/java/baseball/game/Game.java diff --git a/src/main/java/baseball/game/Game.java b/src/main/java/baseball/game/Game.java new file mode 100644 index 000000000..597cd5afb --- /dev/null +++ b/src/main/java/baseball/game/Game.java @@ -0,0 +1,128 @@ +package baseball.game; + +import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; + +public class Game { + + public void start(){ + + while(true){ + //랜덤 숫자 배열 생성 + int[] question = makeQuestion(); + + //3스트라이크면 중단 + boolean result = false; + while(!result){ + + System.out.print("숫자를 입력해주세요 : "); + // 예외처리 필요 + int[] inputNumbers = inputNumbers(); + + // 스트라이크, 볼 체크 + String check = checkResult(question, inputNumbers); + + // 결과처리 + result = printResult(check); + } + + // restart or not + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String resume = Console.readLine(); + if(resume.equals("2")){ + break; + } + } + + + + } + + public int[] makeQuestion(){ + int[] question = new int[3]; + for(int i = 0; i<3; i++){ + int n = Randoms.pickNumberInRange(1, 9); + question[i] = n; + } + + // 중복체크 필요 + boolean flag = duplicateCheck(question); + if(flag){ + return question; + } + return makeQuestion(); + } + + public boolean duplicateCheck(int[]question){ + for(int i = 0; i<3; i++){ + for(int j = 0; j<3; j++){ + if(question[i]==question[j]&&i!=j){ + return false; + } + } + } + return true; + } + + public int[] inputNumbers(){ + int[] arr = new int[3]; + String str = Console.readLine(); + String[] split = str.split(""); + + if(str.length()==3){ + for(int i = 0; i<3; i++){ + int temp = Integer.parseInt(split[i]); + if(temp==0){ + throw new IllegalArgumentException(); + } + arr[i] = temp; + } + return arr; + } + throw new IllegalArgumentException(); + } + + public String checkResult(int[] question, int[] inputNumbers){ + + int strike = 0; + int ball = 0; + + // 존재여부 동일성, 위치 동일성 판별 + for(int i = 0; i<3; i++){ + for(int j = 0; j<3; j++){ + if(question[i]==inputNumbers[j]&&i==j){ + strike++; + } + else if(question[i]==inputNumbers[j]&&i!=j){ + ball++; + } + } + } + + // 결과를 어떻게 return 해줘야할까 + return Integer.toString(strike)+(ball); + } + + public boolean printResult(String check){ + String[] split = check.split(""); + + if(split[0].equals("3")){ + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + return true; + } + else if(split[0].equals("0")&&split[1].equals("0")){ + System.out.println("낫싱"); + return false; + }else if(split[0].equals("0")){ + System.out.println(split[1]+"볼"); + return false; + }else if(split[1].equals("0")){ + System.out.println(split[0]+"스트라이크"); + return false; + }else{ + System.out.println(split[1]+"볼 "+split[0]+"스트라이크"); + return false; + } + } +} \ No newline at end of file From 15b3db43a3efbc7c322d333e8ae74e1abe4b28a5 Mon Sep 17 00:00:00 2001 From: leesewon00 Date: Thu, 29 Jun 2023 19:49:18 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=EC=9D=B4=EC=84=B8=EC=9B=90=20=EA=B3=BC?= =?UTF-8?q?=EC=A0=9C=20=EC=A0=9C=EC=B6=9C=ED=95=A9=EB=8B=88=EB=8B=A4(1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/game/Game.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/game/Game.java b/src/main/java/baseball/game/Game.java index 597cd5afb..0e5d888e4 100644 --- a/src/main/java/baseball/game/Game.java +++ b/src/main/java/baseball/game/Game.java @@ -77,7 +77,12 @@ public int[] inputNumbers(){ } arr[i] = temp; } - return arr; + + boolean duplicate = duplicateCheck(arr); + if(duplicate){ + return arr; + } + throw new IllegalArgumentException(); } throw new IllegalArgumentException(); } From a5d3fcc8affe5182e605743cf32624f0fc4c2733 Mon Sep 17 00:00:00 2001 From: leesewon00 Date: Fri, 30 Jun 2023 21:02:40 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=EC=9D=B4=EC=84=B8=EC=9B=90=20=EA=B3=BC?= =?UTF-8?q?=EC=A0=9C=EC=88=98=EC=A0=95=EB=B3=B8(1)=20=EC=A0=9C=EC=B6=9C?= =?UTF-8?q?=ED=95=A9=EB=8B=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/game/Check.java | 41 ++++++++ src/main/java/baseball/game/Count.java | 24 +++++ src/main/java/baseball/game/Game.java | 127 +++++++------------------ src/main/java/baseball/game/Input.java | 40 ++++++++ src/main/java/baseball/game/Print.java | 23 +++++ 5 files changed, 162 insertions(+), 93 deletions(-) create mode 100644 src/main/java/baseball/game/Check.java create mode 100644 src/main/java/baseball/game/Count.java create mode 100644 src/main/java/baseball/game/Input.java create mode 100644 src/main/java/baseball/game/Print.java diff --git a/src/main/java/baseball/game/Check.java b/src/main/java/baseball/game/Check.java new file mode 100644 index 000000000..722822964 --- /dev/null +++ b/src/main/java/baseball/game/Check.java @@ -0,0 +1,41 @@ +package baseball.game; + +import java.util.ArrayList; +import java.util.Objects; + +public class Check { + public boolean duplicateCheck(ArrayList question){ + for(int i = 0; i question, ArrayList inputNumbers){ + // strike ball check + Count count = new Count(); + + // 존재여부 동일성, 위치 동일성 판별 + for(int i = 0; i question = makeQuestion(); //3스트라이크면 중단 boolean result = false; while(!result){ + // input + Input input = new Input(); System.out.print("숫자를 입력해주세요 : "); - // 예외처리 필요 - int[] inputNumbers = inputNumbers(); + ArrayList inputNumbers = input.getInputNumbers(); // 스트라이크, 볼 체크 - String check = checkResult(question, inputNumbers); + Check check = new Check(); + Count count = check.checkResult(question, inputNumbers); // 결과처리 - result = printResult(check); + Print print = new Print(); + String printResult = print.printResult(count); + System.out.println(printResult); + + // 종료조건 처리 + if(count.getStrike()==3){ + result = true; + } } // restart or not - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - String resume = Console.readLine(); - if(resume.equals("2")){ - break; + boolean gameResume = gameResume(); + if(gameResume){ + continue; } + break; } + } - - + public boolean gameResume(){ + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String resume = Console.readLine(); + if(resume.equals("2")){ + return false; + } + return true; } - public int[] makeQuestion(){ - int[] question = new int[3]; + public ArrayList makeQuestion(){ + ArrayList question = new ArrayList<>(); for(int i = 0; i<3; i++){ int n = Randoms.pickNumberInRange(1, 9); - question[i] = n; + question.add(n); } // 중복체크 필요 - boolean flag = duplicateCheck(question); - if(flag){ + Check check = new Check(); + boolean isDuplicated = check.duplicateCheck(question); + if(isDuplicated){ return question; } return makeQuestion(); } - public boolean duplicateCheck(int[]question){ - for(int i = 0; i<3; i++){ - for(int j = 0; j<3; j++){ - if(question[i]==question[j]&&i!=j){ - return false; - } - } - } - return true; - } - - public int[] inputNumbers(){ - int[] arr = new int[3]; - String str = Console.readLine(); - String[] split = str.split(""); - - if(str.length()==3){ - for(int i = 0; i<3; i++){ - int temp = Integer.parseInt(split[i]); - if(temp==0){ - throw new IllegalArgumentException(); - } - arr[i] = temp; - } - - boolean duplicate = duplicateCheck(arr); - if(duplicate){ - return arr; - } - throw new IllegalArgumentException(); - } - throw new IllegalArgumentException(); - } - - public String checkResult(int[] question, int[] inputNumbers){ - - int strike = 0; - int ball = 0; - - // 존재여부 동일성, 위치 동일성 판별 - for(int i = 0; i<3; i++){ - for(int j = 0; j<3; j++){ - if(question[i]==inputNumbers[j]&&i==j){ - strike++; - } - else if(question[i]==inputNumbers[j]&&i!=j){ - ball++; - } - } - } - - // 결과를 어떻게 return 해줘야할까 - return Integer.toString(strike)+(ball); - } - - public boolean printResult(String check){ - String[] split = check.split(""); - - if(split[0].equals("3")){ - System.out.println("3스트라이크"); - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - return true; - } - else if(split[0].equals("0")&&split[1].equals("0")){ - System.out.println("낫싱"); - return false; - }else if(split[0].equals("0")){ - System.out.println(split[1]+"볼"); - return false; - }else if(split[1].equals("0")){ - System.out.println(split[0]+"스트라이크"); - return false; - }else{ - System.out.println(split[1]+"볼 "+split[0]+"스트라이크"); - return false; - } - } } \ No newline at end of file diff --git a/src/main/java/baseball/game/Input.java b/src/main/java/baseball/game/Input.java new file mode 100644 index 000000000..a468d98e1 --- /dev/null +++ b/src/main/java/baseball/game/Input.java @@ -0,0 +1,40 @@ +package baseball.game; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.ArrayList; + +import static java.lang.Integer.*; + +public class Input { + public ArrayList getInputNumbers(){ + String str = Console.readLine(); + String[] split = str.split(""); + + // input check + if(str.length()!=3){ + throw new IllegalArgumentException(); + } + if(str.contains("0")){ + throw new IllegalArgumentException(); + } + + // arraylist 로 변환 + ArrayList arr = new ArrayList<>(); // return 해줄 것 + for(int i = 0; i<3; i++){ + int temp = parseInt(split[i]); + arr.add(temp); + } + + // 중복 check + Check check = new Check(); + boolean isDuplicate = check.duplicateCheck(arr); + + if(!isDuplicate){ + throw new IllegalArgumentException(); + } + + return arr; + } + +} diff --git a/src/main/java/baseball/game/Print.java b/src/main/java/baseball/game/Print.java new file mode 100644 index 000000000..b28214102 --- /dev/null +++ b/src/main/java/baseball/game/Print.java @@ -0,0 +1,23 @@ +package baseball.game; + +public class Print { + public String printResult(Count count){ + int ball = count.getBall(); + int strike = count.getStrike(); + String result = ""; + + if(strike==0&&ball==0){ + result += "낫싱"; + } + if(ball>0) { + result += ball + "볼 "; + } + if(strike>0){ + result += strike + "스트라이크"; + } + if(strike==3){ + result += "\n3개의 숫자를 모두 맞히셨습니다! 게임 종료"; + } + return result; + } +}