From ebb186acf31773c932a769bc185a4f7b75dda793 Mon Sep 17 00:00:00 2001 From: Taekyun Lee Date: Sat, 6 Jul 2024 15:19:16 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 78 ++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..69631029a9 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,83 @@ package baseball; +import camp.nextstep.edu.missionutils.Randoms; +import camp.nextstep.edu.missionutils.Console; + +import java.util.ArrayList; +import java.util.List; + public class Application { + + public static void Game(){ + while(true){ + System.out.println("숫자 야구 게임을 시작합니다."); + List computer = new ArrayList<>(); //랜덤 숫자 리스트 + while (computer.size() < 3) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!computer.contains(randomNumber)) { + computer.add(randomNumber); + } + } + while(true){ + System.out.println("숫자를 입력해주세요 : "); + String input = Console.readLine(); + int in = Integer.parseInt(input); + if (in < 0 || in > 999) { + throw new IllegalArgumentException(); + } + int[] num = new int[3]; //입력 숫자 리스트 + for(int i = 0; i < 3; i++){ + num[i] = input.charAt(i) - '0'; + } + + String result = Go(computer,num); + + System.out.println(result); + if(result.equals("3스트라이크")){ + break; + } + } + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String next = Console.readLine(); + if (next.equals("2")) { + break; + } + else if(!next.equals("1")){ + continue; + } + } + } + public static String Go(List computer, int[] input){ + int strike = 0; + int ball = 0; + for(int i = 0; i < 3; i++){ + for(int j = 0; j < 3; j++){ + if(computer.get(i).equals(input[j])){ + if(i == j){ + strike++; + } + else{ + ball++; + } + } + } + } + if(ball == 0 && strike == 0){ + return "낫싱"; + } + else if(strike == 0){ + return ball + "볼"; + } + else if(ball == 0){ + return strike + "스트라이크"; + } + else{ + return ball + "볼 " + strike + "스트라이크"; + } + + } public static void main(String[] args) { - // TODO: 프로그램 구현 + Game(); } } From f517d5cb933142e107c5405525f0e7f145fee94c Mon Sep 17 00:00:00 2001 From: Taekyun Lee Date: Sun, 7 Jul 2024 17:30:30 +0900 Subject: [PATCH 2/5] upload --- src/main/java/baseball/Application.java | 79 +------------------------ src/main/java/baseball/Computer.java | 22 +++++++ src/main/java/baseball/Game.java | 37 ++++++++++++ src/main/java/baseball/Go.java | 34 +++++++++++ src/main/java/baseball/Input.java | 21 +++++++ 5 files changed, 116 insertions(+), 77 deletions(-) create mode 100644 src/main/java/baseball/Computer.java create mode 100644 src/main/java/baseball/Game.java create mode 100644 src/main/java/baseball/Go.java create mode 100644 src/main/java/baseball/Input.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 69631029a9..ce723b4a39 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,83 +1,8 @@ package baseball; -import camp.nextstep.edu.missionutils.Randoms; -import camp.nextstep.edu.missionutils.Console; - -import java.util.ArrayList; -import java.util.List; - public class Application { - - public static void Game(){ - while(true){ - System.out.println("숫자 야구 게임을 시작합니다."); - List computer = new ArrayList<>(); //랜덤 숫자 리스트 - while (computer.size() < 3) { - int randomNumber = Randoms.pickNumberInRange(1, 9); - if (!computer.contains(randomNumber)) { - computer.add(randomNumber); - } - } - while(true){ - System.out.println("숫자를 입력해주세요 : "); - String input = Console.readLine(); - int in = Integer.parseInt(input); - if (in < 0 || in > 999) { - throw new IllegalArgumentException(); - } - int[] num = new int[3]; //입력 숫자 리스트 - for(int i = 0; i < 3; i++){ - num[i] = input.charAt(i) - '0'; - } - - String result = Go(computer,num); - - System.out.println(result); - if(result.equals("3스트라이크")){ - break; - } - } - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - String next = Console.readLine(); - if (next.equals("2")) { - break; - } - else if(!next.equals("1")){ - continue; - } - } - } - public static String Go(List computer, int[] input){ - int strike = 0; - int ball = 0; - for(int i = 0; i < 3; i++){ - for(int j = 0; j < 3; j++){ - if(computer.get(i).equals(input[j])){ - if(i == j){ - strike++; - } - else{ - ball++; - } - } - } - } - if(ball == 0 && strike == 0){ - return "낫싱"; - } - else if(strike == 0){ - return ball + "볼"; - } - else if(ball == 0){ - return strike + "스트라이크"; - } - else{ - return ball + "볼 " + strike + "스트라이크"; - } - - } public static void main(String[] args) { - Game(); + Game game = new Game(); + game.start(); } } diff --git a/src/main/java/baseball/Computer.java b/src/main/java/baseball/Computer.java new file mode 100644 index 0000000000..79949cd12e --- /dev/null +++ b/src/main/java/baseball/Computer.java @@ -0,0 +1,22 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class Computer { + private List com; + public void Random(){ + com = new ArrayList<>(); + while(com.size() < 3){ + int randomNumber = Randoms.pickNumberInRange(1,9); + if(!com.contains(randomNumber)){ + com.add(randomNumber); + } + } + } + public List getCom(){ + return com; + } +} diff --git a/src/main/java/baseball/Game.java b/src/main/java/baseball/Game.java new file mode 100644 index 0000000000..c21568a2fc --- /dev/null +++ b/src/main/java/baseball/Game.java @@ -0,0 +1,37 @@ +package baseball; + +public class Game { + private Computer computer; + private Input input; + private Go go; + public Game(){ + this.computer = new Computer(); + this.input = new Input(); + this.go = new Go(); + } + public void start(){ + while(true){ + System.out.println("숫자 야구 게임을 시작합니다."); + computer.Random(); + while(true){ + System.out.println("숫자를 입력해주세요 : "); + int[] player = input.getInput(); + String result = go.Check(computer.getCom(), player); + System.out.println(result); + + if(result.equals("3스트라이크")){ + break; + } + } + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String next = input.getUserInput(); + if (next.equals("2")) { + break; + } + else if(!next.equals("1")){ + continue; + } + } + } +} diff --git a/src/main/java/baseball/Go.java b/src/main/java/baseball/Go.java new file mode 100644 index 0000000000..d76e08c92e --- /dev/null +++ b/src/main/java/baseball/Go.java @@ -0,0 +1,34 @@ +package baseball; + +import java.util.List; + +public class Go { + public static String Check(List computer, int[] input){ + int strike = 0; + int ball = 0; + for(int i = 0; i < 3; i++){ + for(int j = 0; j < 3; j++){ + if(computer.get(i).equals(input[j])){ + if(i == j){ + strike++; + } + else{ + ball++; + } + } + } + } + if(ball == 0 && strike == 0){ + return "낫싱"; + } + else if(strike == 0){ + return ball + "볼"; + } + else if(ball == 0){ + return strike + "스트라이크"; + } + else{ + return ball + "볼 " + strike + "스트라이크"; + } + } +} diff --git a/src/main/java/baseball/Input.java b/src/main/java/baseball/Input.java new file mode 100644 index 0000000000..bd6fae9a52 --- /dev/null +++ b/src/main/java/baseball/Input.java @@ -0,0 +1,21 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Console; + +public class Input { + private int[] in = new int[3]; + public String getUserInput(){ + return Console.readLine(); + } + public int[] getInput(){ + String input = getUserInput(); + int a = Integer.parseInt(input); + if(a < 0 || a > 999){ + throw new IllegalArgumentException(); + } + for(int i = 0; i < 3; i++){ + in[i] = input.charAt(i)-'0'; + } + return in; + } +} From 17216592a76ba5883ea5886a4fdd229a95bafd03 Mon Sep 17 00:00:00 2001 From: taekyunlee Date: Mon, 8 Jul 2024 15:54:14 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactor=20:=20=EB=B3=80=EC=88=98=EB=AA=85?= =?UTF-8?q?=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/baseball/Computer.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/baseball/Computer.java b/src/main/java/baseball/Computer.java index 79949cd12e..d51593a867 100644 --- a/src/main/java/baseball/Computer.java +++ b/src/main/java/baseball/Computer.java @@ -6,17 +6,17 @@ import java.util.List; public class Computer { - private List com; + private List ComputerNumbers; public void Random(){ - com = new ArrayList<>(); - while(com.size() < 3){ + ComputerNumbers = new ArrayList<>(); + while(ComputerNumbers.size() < 3){ int randomNumber = Randoms.pickNumberInRange(1,9); - if(!com.contains(randomNumber)){ - com.add(randomNumber); + if(!ComputerNumbers.contains(randomNumber)){ + ComputerNumbers.add(randomNumber); } } } public List getCom(){ - return com; + return ComputerNumbers; } } From 6d07c15d16197192a30e0fd5ec4c5b160b1fa0fa Mon Sep 17 00:00:00 2001 From: taekyunlee Date: Mon, 8 Jul 2024 15:54:37 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat=20:=20newGame()=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=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/baseball/Game.java | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/baseball/Game.java b/src/main/java/baseball/Game.java index c21568a2fc..c72cfcddf3 100644 --- a/src/main/java/baseball/Game.java +++ b/src/main/java/baseball/Game.java @@ -9,20 +9,25 @@ public Game(){ this.input = new Input(); this.go = new Go(); } + public void newGame(){ + while(true){ + System.out.println("숫자를 입력해주세요 : "); + int[] player = input.getInput(); + String result = go.Check(computer.getCom(), player); + System.out.println(result); + + if(result.equals("3스트라이크")){ + break; + } + } + } public void start(){ while(true){ System.out.println("숫자 야구 게임을 시작합니다."); computer.Random(); - while(true){ - System.out.println("숫자를 입력해주세요 : "); - int[] player = input.getInput(); - String result = go.Check(computer.getCom(), player); - System.out.println(result); - if(result.equals("3스트라이크")){ - break; - } - } + newGame(); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); String next = input.getUserInput(); From a0fa8778f0b0e32ae3e957b6fa762e836c0f0353 Mon Sep 17 00:00:00 2001 From: taekyunlee Date: Mon, 8 Jul 2024 15:54:59 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=EB=B3=80=EC=88=98=EB=AA=85=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/baseball/Go.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baseball/Go.java b/src/main/java/baseball/Go.java index d76e08c92e..2b0fdde6c7 100644 --- a/src/main/java/baseball/Go.java +++ b/src/main/java/baseball/Go.java @@ -3,7 +3,7 @@ import java.util.List; public class Go { - public static String Check(List computer, int[] input){ + public String Check(List computer, int[] input){ int strike = 0; int ball = 0; for(int i = 0; i < 3; i++){