From 6f7a604ea05bb6a1397b0b01d97e162cbc6a0fce Mon Sep 17 00:00:00 2001 From: JunBe Date: Sun, 13 Oct 2024 18:47:55 +0900 Subject: [PATCH 1/8] test --- src/main/java/baseball/Application.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..5b93188924 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -3,5 +3,6 @@ public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + System.out.println("test"); } } From c50605c629a3e0a8b1c8ded01326e6bd8c6ed75a Mon Sep 17 00:00:00 2001 From: JunBe Date: Mon, 14 Oct 2024 02:30:42 +0900 Subject: [PATCH 2/8] =?UTF-8?q?=EC=B4=88=EA=B8=B0=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 89 ++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 5b93188924..1a192379bd 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,8 +1,95 @@ package baseball; +import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 - System.out.println("test"); + List computer = getRandomNum(); //컴퓨터 수 임의지정 + System.out.println(computer); + List user = new ArrayList<>(); + //여기 를 계속 반복해서 출력해야함 + //기능 구현 + while (true) { + String input = Console.readLine(); + //잘못된 값 입력 시 예외 처리 + exception(input); + + for (int i = 0; i < 3; i++) { + user.add(input.charAt(i) - '0'); + } +// System.out.println(user); + int strike = 0; + int ball = 0; + for (int i = 0; i < 3; i++) { + if (user.get(i) == computer.get(i)) { + strike++; + } else if (user.contains(computer.get(i))) { + ball++; + } + } + if (strike == 3) { + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int selectNextGame = Integer.parseInt(Console.readLine()); + if (selectNextGame == 2) { + return; + }// 종료후 게임 종료 or 재시작 로직 구현 + computer = getRandomNum(); + } else if (strike == 0 && ball == 0) { + System.out.println("낫싱"); + } else if (strike > 0 && ball > 0) { + System.out.println(ball + "볼 " + strike + "스트라이크"); + } else if (strike > 0) { + System.out.println(strike + "스트라이크"); + } else if (ball > 0) { + System.out.println(ball + "볼"); + } + + for (int i = 0; i < 3; i++) { + user.remove(0); + } + } + + } + + private static void exception(String input) { + if (input.length() != 3) { + throw new IllegalArgumentException("3개의 값이 들어가야 합니다."); + } + + try { + int parseInt = Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("정수형 변환 불가"); + } + + for (int i = 0; i < input.length(); i++) { + for (int j = i + 1; j < input.length(); j++) { + if (input.charAt(i) == input.charAt(j)) { + throw new IllegalArgumentException("3개의 숫자는 모두 달라야 합니다."); + } + } + } + } + + private static List getRandomNum() { + List computer = new ArrayList<>(); + while (computer.size() < 3) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!computer.contains(randomNumber)) { + computer.add(randomNumber); + } + } + return computer; + } + + } From 585151e8b65584ecf054ad4234b60d4f25a78bca Mon Sep 17 00:00:00 2001 From: JunBe Date: Mon, 14 Oct 2024 21:13:15 +0900 Subject: [PATCH 3/8] =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC,=20=EC=A4=91=EB=B3=B5=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 109 ++++++++++++++---------- 1 file changed, 63 insertions(+), 46 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 1a192379bd..ff5fee917b 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -4,85 +4,102 @@ import camp.nextstep.edu.missionutils.Randoms; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class Application { + private static final int NUMBER_SIZE = 3; public static void main(String[] args) { // TODO: 프로그램 구현 List computer = getRandomNum(); //컴퓨터 수 임의지정 - System.out.println(computer); - List user = new ArrayList<>(); - //여기 를 계속 반복해서 출력해야함 - //기능 구현 + gameStart(computer); + } + + private static void gameStart(List computer) { while (true) { - String input = Console.readLine(); - //잘못된 값 입력 시 예외 처리 - exception(input); + List user = inputUser(); - for (int i = 0; i < 3; i++) { - user.add(input.charAt(i) - '0'); - } -// System.out.println(user); - int strike = 0; - int ball = 0; - for (int i = 0; i < 3; i++) { - if (user.get(i) == computer.get(i)) { - strike++; - } else if (user.contains(computer.get(i))) { - ball++; - } - } - if (strike == 3) { - System.out.println("3스트라이크"); - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - int selectNextGame = Integer.parseInt(Console.readLine()); - if (selectNextGame == 2) { - return; - }// 종료후 게임 종료 or 재시작 로직 구현 - computer = getRandomNum(); - } else if (strike == 0 && ball == 0) { - System.out.println("낫싱"); - } else if (strike > 0 && ball > 0) { - System.out.println(ball + "볼 " + strike + "스트라이크"); - } else if (strike > 0) { - System.out.println(strike + "스트라이크"); - } else if (ball > 0) { - System.out.println(ball + "볼"); - } + int[] result = calculateStrikeAndBall(computer, user); + computer = gameResult(result[0], result[1], computer); + if (computer == null) return; + } + } - for (int i = 0; i < 3; i++) { - user.remove(0); + private static int[] calculateStrikeAndBall(List computer,List user) { + int strike = 0; + int ball = 0; + for (int i = 0; i < NUMBER_SIZE; i++) { + if (user.get(i).equals(computer.get(i))) { + strike++; + } else if (user.contains(computer.get(i))) { + ball++; } } + return new int[]{strike,ball}; + } + private static List gameResult(int strike, int ball, List computer) { + if (strike == NUMBER_SIZE) { + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int selectNextGame = Integer.parseInt(Console.readLine()); + if (selectNextGame == 2) { + return null; + }// 종료후 게임 종료 or 재시작 로직 구현 + computer = getRandomNum(); + } else if (strike == 0 && ball == 0) { + System.out.println("낫싱"); + } else if (strike > 0 && ball > 0) { + System.out.println(ball + "볼 " + strike + "스트라이크"); + } else if (strike > 0) { + System.out.println(strike + "스트라이크"); + } else if (ball > 0) { + System.out.println(ball + "볼"); + } + return computer; + } + + private static List inputUser() { + List user = new ArrayList<>(); + String input = Console.readLine(); + exception(input); //잘못된 값 입력 시 예외 처리 + for (int i = 0; i < NUMBER_SIZE; i++) { + user.add(input.charAt(i) - '0'); + } + return user; } private static void exception(String input) { - if (input.length() != 3) { + if (input.length() != NUMBER_SIZE) { throw new IllegalArgumentException("3개의 값이 들어가야 합니다."); } try { - int parseInt = Integer.parseInt(input); + Integer.parseInt(input); } catch (NumberFormatException e) { throw new IllegalArgumentException("정수형 변환 불가"); } + if (hasDuplicatedDigits(input)) { + throw new IllegalArgumentException("3개의 숫자는 모두 달라야 합니다."); + } + + } + + private static boolean hasDuplicatedDigits(String input) { for (int i = 0; i < input.length(); i++) { for (int j = i + 1; j < input.length(); j++) { if (input.charAt(i) == input.charAt(j)) { - throw new IllegalArgumentException("3개의 숫자는 모두 달라야 합니다."); + return true; } } } - + return false; } private static List getRandomNum() { List computer = new ArrayList<>(); - while (computer.size() < 3) { + while (computer.size() < NUMBER_SIZE) { int randomNumber = Randoms.pickNumberInRange(1, 9); if (!computer.contains(randomNumber)) { computer.add(randomNumber); From 6fcbfcbf02262ed4485e69338614c724e90f35f8 Mon Sep 17 00:00:00 2001 From: JunBe Date: Fri, 18 Oct 2024 23:54:17 +0900 Subject: [PATCH 4/8] =?UTF-8?q?=EC=83=81=EC=88=98=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Constants.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/baseball/Constants.java diff --git a/src/main/java/baseball/Constants.java b/src/main/java/baseball/Constants.java new file mode 100644 index 0000000000..8aadc3c39e --- /dev/null +++ b/src/main/java/baseball/Constants.java @@ -0,0 +1,5 @@ +package baseball; + +public class Constants { + public static final int NUMBER_SIZE = 3; +} From c2c5506691692659c9f84e12e154de65b845cf10 Mon Sep 17 00:00:00 2001 From: JunBe Date: Fri, 18 Oct 2024 23:54:58 +0900 Subject: [PATCH 5/8] =?UTF-8?q?View=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/baseball/BaseballView.java | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/main/java/baseball/BaseballView.java diff --git a/src/main/java/baseball/BaseballView.java b/src/main/java/baseball/BaseballView.java new file mode 100644 index 0000000000..c63a210f54 --- /dev/null +++ b/src/main/java/baseball/BaseballView.java @@ -0,0 +1,39 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.ArrayList; +import java.util.List; + +public class BaseballView { + public String getUserInput() { + System.out.print("숫자를 입력해 주세요: "); + return Console.readLine(); + } + + public void printResult(int strike, int ball) { + if (strike == 0 && ball == 0) { + System.out.println("낫싱"); + } else if (strike > 0 && ball > 0) { + System.out.println(ball + "볼 " + strike + "스트라이크"); + } else if (strike > 0) { + System.out.println(strike + "스트라이크"); + } else if (ball > 0) { + System.out.println(ball + "볼"); + } + } + + public void printWinMessage() { + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + } + + public int askForRestart() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + return Integer.parseInt(Console.readLine()); //이것도 예외처리 해야함 + } + + public void printError(String message) { + System.out.println(message); + } +} From f1dca7613f76de8e349f6e5c498e6a39005ee8d2 Mon Sep 17 00:00:00 2001 From: JunBe Date: Fri, 18 Oct 2024 23:55:10 +0900 Subject: [PATCH 6/8] =?UTF-8?q?Model=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/baseball/BaseballModel.java | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/java/baseball/BaseballModel.java diff --git a/src/main/java/baseball/BaseballModel.java b/src/main/java/baseball/BaseballModel.java new file mode 100644 index 0000000000..cb90966372 --- /dev/null +++ b/src/main/java/baseball/BaseballModel.java @@ -0,0 +1,41 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.List; + +public class BaseballModel { + private List computer; + public BaseballModel() { + this.computer = generateRandomNum(); + } + + public List getNewComputerNum() { + return this.computer = generateRandomNum(); + } + + public int[] calculateStrikeAndBall(List user) { + int strike = 0; + int ball = 0; + for (int i = 0; i < Constants.NUMBER_SIZE; i++) { + if (user.get(i).equals(computer.get(i))) { + strike++; + } else if (user.contains(computer.get(i))) { + ball++; + } + } + return new int[]{strike, ball}; + } + + + public List generateRandomNum() { + List number = new ArrayList<>(); + while (number.size() < Constants.NUMBER_SIZE) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!number.contains(randomNumber)) { + number.add(randomNumber); + } + } + return number; + } +} From 718ce354c3f6a7da86fed869ce1b8f8b799e9f8c Mon Sep 17 00:00:00 2001 From: JunBe Date: Fri, 18 Oct 2024 23:55:19 +0900 Subject: [PATCH 7/8] =?UTF-8?q?Controller=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/BaseballController.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/baseball/BaseballController.java diff --git a/src/main/java/baseball/BaseballController.java b/src/main/java/baseball/BaseballController.java new file mode 100644 index 0000000000..b8efc76a9d --- /dev/null +++ b/src/main/java/baseball/BaseballController.java @@ -0,0 +1,70 @@ +package baseball; + +import java.util.ArrayList; +import java.util.List; + +public class BaseballController { + private final BaseballModel baseballModel; + private final BaseballView baseballView; + private final List user; + + public BaseballController() { + baseballModel = new BaseballModel(); + baseballView = new BaseballView(); + user = new ArrayList<>(); + } + + public void gameStart() { + System.out.println("숫자 야구 게임을 시작합니다."); + while (true) { + user.clear(); + String userInput = baseballView.getUserInput(); + exception(userInput); + for (int i = 0; i < Constants.NUMBER_SIZE; i++) { + user.add(userInput.charAt(i) - '0'); + } + int[] result = baseballModel.calculateStrikeAndBall(user); + if (result[0] == 3) { //게임 승리 + baseballView.printWinMessage(); + int restart = baseballView.askForRestart(); + if (restart == 1) { + baseballModel.getNewComputerNum(); + } else if (restart == 2) { + return; + } else { + throw new IllegalArgumentException("1,2만 입력하세요"); + } + } else { + baseballView.printResult(result[0], result[1]); + } + } + } + + private static void exception(String input) { + if (input.length() != Constants.NUMBER_SIZE) { + throw new IllegalArgumentException("3개의 값이 들어가야 합니다."); + } + + try { + Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("정수형 변환 불가"); + } + + if (hasDuplicatedDigits(input)) { + throw new IllegalArgumentException("3개의 숫자는 모두 달라야 합니다."); + } + + } + + private static boolean hasDuplicatedDigits(String input) { + for (int i = 0; i < input.length(); i++) { + for (int j = i + 1; j < input.length(); j++) { + if (input.charAt(i) == input.charAt(j)) { + return true; + } + } + } + return false; + } +} From ad817bcb31f56608175d22a63daf37ad6a114f4f Mon Sep 17 00:00:00 2001 From: JunBe Date: Fri, 18 Oct 2024 23:55:26 +0900 Subject: [PATCH 8/8] =?UTF-8?q?=EC=8B=9C=EC=9E=91=20=EB=A1=9C=EC=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 102 +----------------------- 1 file changed, 3 insertions(+), 99 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index ff5fee917b..1faf6f2a7e 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -7,106 +7,10 @@ import java.util.List; public class Application { - private static final int NUMBER_SIZE = 3; + public static void main(String[] args) { // TODO: 프로그램 구현 - List computer = getRandomNum(); //컴퓨터 수 임의지정 - gameStart(computer); - } - - private static void gameStart(List computer) { - while (true) { - List user = inputUser(); - - int[] result = calculateStrikeAndBall(computer, user); - computer = gameResult(result[0], result[1], computer); - if (computer == null) return; - } - } - - private static int[] calculateStrikeAndBall(List computer,List user) { - int strike = 0; - int ball = 0; - for (int i = 0; i < NUMBER_SIZE; i++) { - if (user.get(i).equals(computer.get(i))) { - strike++; - } else if (user.contains(computer.get(i))) { - ball++; - } - } - return new int[]{strike,ball}; + BaseballController baseballController = new BaseballController(); + baseballController.gameStart(); } - - private static List gameResult(int strike, int ball, List computer) { - if (strike == NUMBER_SIZE) { - System.out.println("3스트라이크"); - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - int selectNextGame = Integer.parseInt(Console.readLine()); - if (selectNextGame == 2) { - return null; - }// 종료후 게임 종료 or 재시작 로직 구현 - computer = getRandomNum(); - } else if (strike == 0 && ball == 0) { - System.out.println("낫싱"); - } else if (strike > 0 && ball > 0) { - System.out.println(ball + "볼 " + strike + "스트라이크"); - } else if (strike > 0) { - System.out.println(strike + "스트라이크"); - } else if (ball > 0) { - System.out.println(ball + "볼"); - } - return computer; - } - - private static List inputUser() { - List user = new ArrayList<>(); - String input = Console.readLine(); - exception(input); //잘못된 값 입력 시 예외 처리 - for (int i = 0; i < NUMBER_SIZE; i++) { - user.add(input.charAt(i) - '0'); - } - return user; - } - - private static void exception(String input) { - if (input.length() != NUMBER_SIZE) { - throw new IllegalArgumentException("3개의 값이 들어가야 합니다."); - } - - try { - Integer.parseInt(input); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("정수형 변환 불가"); - } - - if (hasDuplicatedDigits(input)) { - throw new IllegalArgumentException("3개의 숫자는 모두 달라야 합니다."); - } - - } - - private static boolean hasDuplicatedDigits(String input) { - for (int i = 0; i < input.length(); i++) { - for (int j = i + 1; j < input.length(); j++) { - if (input.charAt(i) == input.charAt(j)) { - return true; - } - } - } - return false; - } - - private static List getRandomNum() { - List computer = new ArrayList<>(); - while (computer.size() < NUMBER_SIZE) { - int randomNumber = Randoms.pickNumberInRange(1, 9); - if (!computer.contains(randomNumber)) { - computer.add(randomNumber); - } - } - return computer; - } - - }