From 8e7e1b2642c041e84a861fd6ef7d50fcd77c085e Mon Sep 17 00:00:00 2001 From: misonme <108711609+oboil@users.noreply.github.com> Date: Sun, 14 Jul 2024 11:59:11 +0900 Subject: [PATCH 1/2] Application.java --- src/main/java/baseball/Application.java | 108 ++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..113c5e0efc 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,115 @@ package baseball; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import camp.nextstep.edu.missionutils.Randoms; +import camp.nextstep.edu.missionutils.Console; + public class Application { + + static int strike; + static int ball; public static void main(String[] args) { // TODO: 프로그램 구현 + boolean condition = true; + while (condition) { + System.out.println("숫자 야구 게임을 시작합니다."); + ArrayList computer = setComputer(); + + boolean findEx = true; + do { + try { + + System.out.printf("숫자를 입력해주세요 : "); + String userNum = Console.readLine(); + findEx(userNum); + + ArrayList user = StoI(userNum); + getResult(computer, user); + + if (strike == 3){ + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String choice = Console.readLine(); + if (choice.equals("2")){ + condition = false; + break; + } + break; + } + else if (strike == 0 && ball ==0 ) + System.out.println("낫싱"); + else if (strike == 0) + System.out.println(ball + "볼"); + else if (ball == 0) + System.out.println(strike + "스트라이크"); + else + System.out.println(ball + "볼 " + strike + "스트라이크"); + + + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + condition = false; + break; + } + } while (findEx); + + + + } + } + + public static ArrayList setComputer(){ + ArrayList computer = new ArrayList<>(); + while (computer.size() < 3 ) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!computer.contains(randomNumber)) { + computer.add(randomNumber); + } + } + return computer; } + + public static void findEx(String userNum){ + if (userNum.length() != 3) + throw new IllegalArgumentException("세 자리가 아님"); + + Set uniqueDigits = new HashSet<>(); + for (char c : userNum.toCharArray()) { + if (!Character.isDigit(c)) { + throw new IllegalArgumentException("숫자가 아닌 타입이 포함됨"); + } + uniqueDigits.add(c); + } + + if (uniqueDigits.size() != 3){ + throw new IllegalArgumentException("중복된 숫자가 포함됨"); + } + } + + public static ArrayList StoI(String userNum){ + ArrayList user = new ArrayList<>(); + for (char c : userNum.toCharArray()){ + user.add(Character.getNumericValue(c)); + } + return user; + } + + public static void getResult(ArrayList computer, ArrayList user){ + strike = 0; ball = 0; + + for (int i = 0; i < 3; i++){ + if (user.get(i).equals(computer.get(i))) + strike++; + else if (computer.contains(user.get(i))) + ball++; + } + } + } + From 14300c3565fe72db36a95dd31781ceb85a22a8f6 Mon Sep 17 00:00:00 2001 From: misonme <108711609+oboil@users.noreply.github.com> Date: Sun, 14 Jul 2024 11:59:46 +0900 Subject: [PATCH 2/2] Update README.md --- docs/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29bb2..4354ecaf39 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,6 @@ +---**구현 기능**--- + +1. 상대방(컴퓨터)의 번호 정하기 +2. 유저가 입력한 숫자로 IllegalArgumentException 확인하기 +3. 컴퓨터와 유저의 번호 비교하기 -스트라이크, 볼, 낫싱, 3스트라이크 +4. 게임 종료(3스트라이크) 후 유저의 선택에 따라 반복, 종료하기