From 448dc3099f2d1d250a86f2fac0e8a2a4088a0370 Mon Sep 17 00:00:00 2001 From: hyunj24 Date: Sun, 26 Jan 2025 14:55:49 +0900 Subject: [PATCH 1/3] first commit --- src/main/java/baseball/Application.java | 86 ++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..3d57054672 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,91 @@ package baseball; +import camp.nextstep.edu.missionutils.Console; + +import javax.swing.text.ElementIterator; +import java.util.Arrays; +import java.util.Random; +import java.util.Scanner; + public class Application { + static Scanner scanner = new Scanner(System.in); + + static int[] answer = new int[3]; + static Random rd = new Random(); + public static void main(String[] args) { // TODO: 프로그램 구현 + while (true) { + //정답 배열 생성 + for (int i = 0; i < 3; i++) { + answer[i] = rd.nextInt(9) + 1; //1~9 + //중복 검증 + for (int j = 0; j < i; j++) { + if (answer[i] == answer[j]) { + answer[j] = rd.nextInt(9) + 1; + } + } + } + for (int i : answer) { + System.out.print(i); + } + System.out.println(); + //게임 시작 + System.out.print("숫자 야구 게임을 시작합니다."); + + start_game(); + + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int continue_answer = Integer.parseInt(Console.readLine()); + if (continue_answer == 1) continue; + else if (continue_answer == 2) { + break; + } + } + } + + private static void start_game() { + while (true) { + int strike = 0; + int ball = 0; + + System.out.print("\n숫자를 입력해주세요: "); + String userInput = Console.readLine(); + if (userInput.length() != 3) { + throw new IllegalArgumentException("숫자 3자리를 입력해야 합니다."); + } + + + String array[] = userInput.split(""); + int[] userInputArray = new int[3]; + + for (int i = 0; i < 3; i++) { + userInputArray[i] = Integer.parseInt(array[i]); + } + + //입력받은 숫자 검증 + for (int i = 0; i < 3; i++) { //정답 + for (int j = 0; j < 3; j++) { //유저 + if (userInputArray[j] == answer[i]) { + if (j == i) { + strike++; //숫자, 자리 같으면 스트라이크 + } + else ball++; + } + } + } + + + if (ball != 0) System.out.print(ball + "볼 "); + if (strike != 0) System.out.print(strike + "스트라이크"); + if (ball == 0 && strike == 0) System.out.print("낫싱"); + + //결과 + if (Arrays.equals(answer, userInputArray)) { + System.out.println("\n3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + break; + } + else continue; + } } -} +} \ No newline at end of file From 4c7bdb65a99b1121af79eafd61c3b1b478cf5aec Mon Sep 17 00:00:00 2001 From: hyunj24 Date: Sun, 26 Jan 2025 17:26:15 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 101 +++++++++++++----------- src/main/java/baseball/Result.java | 11 +++ 2 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 src/main/java/baseball/Result.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 3d57054672..98953d0412 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -2,10 +2,7 @@ import camp.nextstep.edu.missionutils.Console; -import javax.swing.text.ElementIterator; -import java.util.Arrays; -import java.util.Random; -import java.util.Scanner; +import java.util.*; public class Application { static Scanner scanner = new Scanner(System.in); @@ -16,25 +13,10 @@ public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 while (true) { - //정답 배열 생성 - for (int i = 0; i < 3; i++) { - answer[i] = rd.nextInt(9) + 1; //1~9 - //중복 검증 - for (int j = 0; j < i; j++) { - if (answer[i] == answer[j]) { - answer[j] = rd.nextInt(9) + 1; - } - } - } - for (int i : answer) { - System.out.print(i); - } - System.out.println(); //게임 시작 System.out.print("숫자 야구 게임을 시작합니다."); - start_game(); - + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); int continue_answer = Integer.parseInt(Console.readLine()); if (continue_answer == 1) continue; @@ -46,46 +28,69 @@ else if (continue_answer == 2) { private static void start_game() { while (true) { - int strike = 0; - int ball = 0; + //정답 배열 생성 + List answerNumbers = make_answer(); + for (Integer i : answerNumbers) { + System.out.print(i); + } + + //볼, 스트라이크 판별 + while (true) { + //숫자 입력받고 list에 저장 + List userNumbers = getNumbers(); + Result result = verifyNumbers(answerNumbers, userNumbers); - System.out.print("\n숫자를 입력해주세요: "); - String userInput = Console.readLine(); - if (userInput.length() != 3) { - throw new IllegalArgumentException("숫자 3자리를 입력해야 합니다."); + if (result.ball != 0) System.out.println(result.ball + "볼"); + if (result.strike == 1 || result.strike == 2) System.out.println(result.strike + "스트라이크"); + else if (result.strike == 0 && result.ball == 0) { + System.out.println("낫싱"); + } + if (result.strike == 3) { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + break; + } else continue; } - String array[] = userInput.split(""); - int[] userInputArray = new int[3]; + } + } - for (int i = 0; i < 3; i++) { - userInputArray[i] = Integer.parseInt(array[i]); + private static List make_answer() { + List answerNumbers = new ArrayList<>(); + while(answerNumbers.size() < 3) { + int number = rd.nextInt(9) + 1; //1~9 + if (!answerNumbers.contains(number)) { + answerNumbers.add(number); } + } + return answerNumbers; + } + + private static Result verifyNumbers(List answerNumbers, List userNumbers) { + int ball = 0; + int strike = 0; - //입력받은 숫자 검증 - for (int i = 0; i < 3; i++) { //정답 - for (int j = 0; j < 3; j++) { //유저 - if (userInputArray[j] == answer[i]) { - if (j == i) { - strike++; //숫자, 자리 같으면 스트라이크 - } - else ball++; - } + for (int i = 0; i < userNumbers.size(); i++) { + if (answerNumbers.contains(userNumbers.get(i))) { + if (answerNumbers.get(i) == userNumbers.get(i)) { + strike++; } + else ball++; } + } + return new Result(strike, ball); + } + private static List getNumbers() { + List userInput = new ArrayList<>(); - if (ball != 0) System.out.print(ball + "볼 "); - if (strike != 0) System.out.print(strike + "스트라이크"); - if (ball == 0 && strike == 0) System.out.print("낫싱"); + System.out.print("\n숫자를 입력해주세요: "); + String input = Console.readLine(); - //결과 - if (Arrays.equals(answer, userInputArray)) { - System.out.println("\n3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - break; - } - else continue; + for (int i = 0; i < input.length(); i++) { + userInput.add(input.charAt(i) - '0'); } + + return userInput; } } \ No newline at end of file diff --git a/src/main/java/baseball/Result.java b/src/main/java/baseball/Result.java new file mode 100644 index 0000000000..040eea7cec --- /dev/null +++ b/src/main/java/baseball/Result.java @@ -0,0 +1,11 @@ +package baseball; + +public class Result { + public int strike; + public int ball; + + public Result(int strike, int ball) { + this.strike = strike; + this.ball = ball; + } +} From ec1a48dc5a9ae25137f5897142ac9fc1459057b1 Mon Sep 17 00:00:00 2001 From: hyunj24 Date: Sun, 26 Jan 2025 17:42:23 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EC=99=84=EB=A3=8C=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 41 +++++++++++++------------ src/main/java/baseball/Result.java | 8 +++++ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 98953d0412..f6a78cdf36 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -4,35 +4,32 @@ import java.util.*; -public class Application { - static Scanner scanner = new Scanner(System.in); +// M model -> 계산 로직 데이터 +// v View -> 입력 출력 +// C Controller -> 모델 뷰를 합쳐서 관리함 - static int[] answer = new int[3]; - static Random rd = new Random(); +public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 while (true) { //게임 시작 System.out.print("숫자 야구 게임을 시작합니다."); - start_game(); + startGame(); System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - int continue_answer = Integer.parseInt(Console.readLine()); - if (continue_answer == 1) continue; - else if (continue_answer == 2) { + int continueAnswer = Integer.parseInt(Console.readLine()); + if (continueAnswer == 1) continue; + else if (continueAnswer == 2) { break; } } } - private static void start_game() { + private static void startGame() { while (true) { //정답 배열 생성 - List answerNumbers = make_answer(); - for (Integer i : answerNumbers) { - System.out.print(i); - } + List answerNumbers = makeAnswer(); //볼, 스트라이크 판별 while (true) { @@ -40,9 +37,12 @@ private static void start_game() { List userNumbers = getNumbers(); Result result = verifyNumbers(answerNumbers, userNumbers); - if (result.ball != 0) System.out.println(result.ball + "볼"); - if (result.strike == 1 || result.strike == 2) System.out.println(result.strike + "스트라이크"); - else if (result.strike == 0 && result.ball == 0) { + if (result.hasBall()) { + System.out.println(result.ball + "볼"); + } + if (result.strike == 1 || result.strike == 2) { + System.out.println(result.strike + "스트라이크"); + } else if (result.isNothing()) { System.out.println("낫싱"); } if (result.strike == 3) { @@ -50,19 +50,20 @@ else if (result.strike == 0 && result.ball == 0) { break; } else continue; } - - } } - private static List make_answer() { + private static List makeAnswer() { List answerNumbers = new ArrayList<>(); + Random random = new Random(); + while(answerNumbers.size() < 3) { - int number = rd.nextInt(9) + 1; //1~9 + int number = random.nextInt(9) + 1; //1~9 if (!answerNumbers.contains(number)) { answerNumbers.add(number); } } + return answerNumbers; } diff --git a/src/main/java/baseball/Result.java b/src/main/java/baseball/Result.java index 040eea7cec..465d79bb15 100644 --- a/src/main/java/baseball/Result.java +++ b/src/main/java/baseball/Result.java @@ -8,4 +8,12 @@ public Result(int strike, int ball) { this.strike = strike; this.ball = ball; } + + public boolean hasBall() { + return ball > 0; + } + + public boolean isNothing() { + return ball == 0 && strike == 0; + } }