diff --git a/README.md b/README.md index 1bee39036..1100f949c 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,34 @@
+## πŸ‘€ κΈ°λŠ₯ λͺ©λ‘ +<숫자 생성> +1. 숫자 랜덀 생성(1~9 λ²”μœ„μ˜ 3자리 μžμ—°μˆ˜) +2. 각 자리 μˆ«μžλŠ” 쀑볡 ν—ˆμš© XX + +<μž…λ ₯> +1.[μ˜ˆμ™Έ] μˆ«μžκ°€ μ•„λ‹Œ 값을 μž…λ ₯받은 경우 +2.[μ˜ˆμ™Έ] 3자리 μžμ—°μˆ˜ μ•„λ‹Œ 경우 +3.[μ˜ˆμ™Έ] 각 자릿수의 μˆ«μžμ€‘ μ€‘λ³΅λœ 값이 μžˆλŠ” 경우 +4. [μ˜ˆμ™Έ] 0 μ΄ν•˜ 음수인 경우 +5. [μ˜ˆμ™Έ] IllegalArgumentException λ°œμƒ μ‹œν‚¨ ν›„ μ’…λ£Œ + +<힌트> +1. 같은 μžλ¦Ώμˆ˜μ— 숫자 일치 : 슀트라이크 +2. λ‹€λ₯Έ μžλ¦Ώμˆ˜μ— 숫자 일치 : λ³Ό +3. λ‹€λ₯Έ μžλ¦Ώμˆ˜μ— λ‹€λ₯Έ 수 : λ‚«μ‹± + +<좜λ ₯> +1. λ³Ό, 슀트라이크 카운트 ν›„ 좜λ ₯ +2. 좜λ ₯ μˆœμ„œ: nλ³Ό m슀트라이크 +3. m=3일 λ•Œ(3 슀트라이크) κ²Œμž„ μ’…λ£Œ 문ꡬ 좜λ ₯ + +<μ’…λ£Œμ‘°κ±΄> +1. μž¬μ‹œμž‘ - 숫자 랜덀 생성 λ©”μ„œλ“œ ν˜ΈμΆœλΆ€λΆ„μœΌλ‘œ λŒμ•„κ° +2. μž…λ ₯ μ’…λ£Œ - κ²Œμž„ μ’…λ£Œ! + +
+ ## ✍🏻 μž…μΆœλ ₯ μš”κ΅¬μ‚¬ν•­ ### ⌨️ μž…λ ₯ diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 7f1901b8b..60b9e636c 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,31 @@ package baseball; +import java.util.List; + +import service.ComputerRandomNum; +import service.JudgeResult; +import util.InputUtil; + public class Application { - public static void main(String[] args) { - //TODO: 숫자 야ꡬ κ²Œμž„ κ΅¬ν˜„ - } + public static void main(String[] args) { + //TODO: 숫자 야ꡬ κ²Œμž„ κ΅¬ν˜„ + ComputerRandomNum clearNumber = new ComputerRandomNum(); + InputUtil inputUtil = new InputUtil(); + JudgeResult judge = new JudgeResult(); + + boolean again = true; + + while (again){ + List computer = clearNumber.getComputerNumber(); + String result = ""; + while (!result.equals("3슀트라이크")){ + result = judge.judgement(computer, inputUtil.userNumber()); + System.out.println(result); + } + System.out.println("3개의 숫자λ₯Ό λͺ¨λ‘ λ§žνžˆμ…¨μŠ΅λ‹ˆλ‹€! κ²Œμž„ μ’…λ£Œ"); + again = inputUtil.replay(); + } + } + + } diff --git a/src/main/java/service/CompareNum.java b/src/main/java/service/CompareNum.java new file mode 100644 index 000000000..e71f98687 --- /dev/null +++ b/src/main/java/service/CompareNum.java @@ -0,0 +1,26 @@ +package service; + +import java.util.List; + +public class CompareNum { + public int countBall(List computer, List user){ + int result = 0; + for(int i = 0; i < user.size(); i++){ + if(computer.contains(user.get(i))){ + result += 1; + } + } + return result; + } + + public int countStrike(List computer, List user){ + int strike = 0; + for(int i = 0; i < user.size(); i++){ + if(computer.get(i) == user.get(i)){ + strike += 1; + } + } + return strike; + } + +} diff --git a/src/main/java/service/ComputerRandomNum.java b/src/main/java/service/ComputerRandomNum.java new file mode 100644 index 000000000..33bf3ef21 --- /dev/null +++ b/src/main/java/service/ComputerRandomNum.java @@ -0,0 +1,35 @@ +package service; + +import java.util.List; +import java.util.ArrayList; +import camp.nextstep.edu.missionutils.Randoms; + +public class ComputerRandomNum { + + private List computerNumber; + + public ComputerRandomNum() { + computerNumber = setGameNumber(); + } + + public List getComputerNumber() { + return computerNumber; + } + public static int getComputerNum() { + return Randoms.pickNumberInRange(1, 9); + } + + public List setGameNumber() { + List computerNumber = new ArrayList<>(); + while (computerNumber.size() < 3) { + int randomNumber = getComputerNum(); + if(computerNumber.contains(randomNumber)){ + continue; + }else { + computerNumber.add(randomNumber); + } + } + return computerNumber; + } + +} diff --git a/src/main/java/service/JudgeResult.java b/src/main/java/service/JudgeResult.java new file mode 100644 index 000000000..7a09c84da --- /dev/null +++ b/src/main/java/service/JudgeResult.java @@ -0,0 +1,27 @@ +package service; + +import java.util.List; + +public class JudgeResult { + + CompareNum compare = new CompareNum(); + + public String judgement(List computer, List player){ + int total = compare.countBall(computer, player); + int strike = compare.countStrike(computer, player); + int ball = total - strike; + + String result = null; + + if(total == 0){ + result = "λ‚«μ‹±"; + }else if(ball != 0 && strike == 0){ + result = ball + "λ³Ό"; + }else if(strike != 0 && ball == 0){ + result = strike + "슀트라이크"; + }else { + result = ball + "λ³Ό " + strike + "슀트라이크"; + } + return result; + } +} diff --git a/src/main/java/util/InputException.java b/src/main/java/util/InputException.java new file mode 100644 index 000000000..10427ff6c --- /dev/null +++ b/src/main/java/util/InputException.java @@ -0,0 +1,50 @@ +package util; + +import java.util.HashSet; +import java.util.Set; + +public class InputException { + private String inputNumber; + + public InputException(String inputNumber) { + + isStringLengthCorrect(inputNumber); + isDigitPlayerNumber(inputNumber); + isDifferentPlayerNumber(inputNumber); + + this.inputNumber = inputNumber; + } + + public String getInputNumber() { + return inputNumber; + } + + public static void isStringLengthCorrect(String word) throws IllegalArgumentException { + if (word.length() != 3) { + throw new IllegalArgumentException("잘λͺ»λœ 값을 μž…λ ₯ν•˜μ…¨μŠ΅λ‹ˆλ‹€. "); + } + } + + + public static void isDifferentPlayerNumber(String word) throws IllegalArgumentException { + Set set = new HashSet<>(); + + for (int i = 0; i < word.length(); i++) { + set.add(word.charAt(i)); + } + if (set.size() != word.length()) { + throw new IllegalArgumentException("잘λͺ»λœ 값을 μž…λ ₯ν•˜μ…¨μŠ΅λ‹ˆλ‹€."); + } + } + public static void isDigitPlayerNumber(String word) { + for (int i = 0; i < word.length(); i++) { + isDigitCharInString(word, i); + } + } + + public static void isDigitCharInString(String word, int index) throws IllegalArgumentException { + if (!Character.isDigit(word.charAt(index))) { + throw new IllegalArgumentException("잘λͺ»λœ 값을 μž…λ ₯ν•˜μ…¨μŠ΅λ‹ˆλ‹€."); + } + } +} diff --git a/src/main/java/util/InputUtil.java b/src/main/java/util/InputUtil.java new file mode 100644 index 000000000..0a10eba7c --- /dev/null +++ b/src/main/java/util/InputUtil.java @@ -0,0 +1,31 @@ +package util; + +import java.util.List; +import java.util.ArrayList; +import camp.nextstep.edu.missionutils.Console; + +public class InputUtil { + + public List userNumber() { + System.out.print("숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” : "); + String input = Console.readLine(); + // μ˜ˆμ™Έ 처리 λ©”μ„œλ“œ λ§Œλ“€κΈ°.... + + List userNum = new ArrayList<>(); + for(String number: input.split("")){ + userNum.add(Integer.parseInt(number)); + } + return userNum; + } + + public boolean replay(){ + System.out.println("κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜λ €λ©΄ 1, μ’…λ£Œν•˜λ €λ©΄ 2λ₯Ό μž…λ ₯ν•˜μ„Έμš”."); + String input = Console.readLine(); + char answer = input.charAt(0); + if(answer == '1'){ + return true; + } + return false; + } + +}