diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 7f1901b8b..0d56b6417 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,11 @@ package baseball; +import baseball.game.Game; + public class Application { public static void main(String[] args) { //TODO: 숫자 야구 게임 구현 + Game game = new Game(); + game.start(); } } diff --git a/src/main/java/baseball/game/Check.java b/src/main/java/baseball/game/Check.java new file mode 100644 index 000000000..722822964 --- /dev/null +++ b/src/main/java/baseball/game/Check.java @@ -0,0 +1,41 @@ +package baseball.game; + +import java.util.ArrayList; +import java.util.Objects; + +public class Check { + public boolean duplicateCheck(ArrayList question){ + for(int i = 0; i question, ArrayList inputNumbers){ + // strike ball check + Count count = new Count(); + + // 존재여부 동일성, 위치 동일성 판별 + for(int i = 0; i question = makeQuestion(); + + //3스트라이크면 중단 + boolean result = false; + while(!result){ + + // input + Input input = new Input(); + System.out.print("숫자를 입력해주세요 : "); + ArrayList inputNumbers = input.getInputNumbers(); + + // 스트라이크, 볼 체크 + Check check = new Check(); + Count count = check.checkResult(question, inputNumbers); + + // 결과처리 + Print print = new Print(); + String printResult = print.printResult(count); + System.out.println(printResult); + + // 종료조건 처리 + if(count.getStrike()==3){ + result = true; + } + } + + // restart or not + boolean gameResume = gameResume(); + if(gameResume){ + continue; + } + break; + } + } + + public boolean gameResume(){ + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String resume = Console.readLine(); + if(resume.equals("2")){ + return false; + } + return true; + } + + public ArrayList makeQuestion(){ + ArrayList question = new ArrayList<>(); + for(int i = 0; i<3; i++){ + int n = Randoms.pickNumberInRange(1, 9); + question.add(n); + } + + // 중복체크 필요 + Check check = new Check(); + boolean isDuplicated = check.duplicateCheck(question); + if(isDuplicated){ + return question; + } + return makeQuestion(); + } + +} \ No newline at end of file diff --git a/src/main/java/baseball/game/Input.java b/src/main/java/baseball/game/Input.java new file mode 100644 index 000000000..a468d98e1 --- /dev/null +++ b/src/main/java/baseball/game/Input.java @@ -0,0 +1,40 @@ +package baseball.game; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.ArrayList; + +import static java.lang.Integer.*; + +public class Input { + public ArrayList getInputNumbers(){ + String str = Console.readLine(); + String[] split = str.split(""); + + // input check + if(str.length()!=3){ + throw new IllegalArgumentException(); + } + if(str.contains("0")){ + throw new IllegalArgumentException(); + } + + // arraylist 로 변환 + ArrayList arr = new ArrayList<>(); // return 해줄 것 + for(int i = 0; i<3; i++){ + int temp = parseInt(split[i]); + arr.add(temp); + } + + // 중복 check + Check check = new Check(); + boolean isDuplicate = check.duplicateCheck(arr); + + if(!isDuplicate){ + throw new IllegalArgumentException(); + } + + return arr; + } + +} diff --git a/src/main/java/baseball/game/Print.java b/src/main/java/baseball/game/Print.java new file mode 100644 index 000000000..b28214102 --- /dev/null +++ b/src/main/java/baseball/game/Print.java @@ -0,0 +1,23 @@ +package baseball.game; + +public class Print { + public String printResult(Count count){ + int ball = count.getBall(); + int strike = count.getStrike(); + String result = ""; + + if(strike==0&&ball==0){ + result += "낫싱"; + } + if(ball>0) { + result += ball + "볼 "; + } + if(strike>0){ + result += strike + "스트라이크"; + } + if(strike==3){ + result += "\n3개의 숫자를 모두 맞히셨습니다! 게임 종료"; + } + return result; + } +} diff --git a/src/test/java/baseball/ApplicationTest.java b/src/test/java/baseball/ApplicationTest.java index 359aacfa4..057710c26 100644 --- a/src/test/java/baseball/ApplicationTest.java +++ b/src/test/java/baseball/ApplicationTest.java @@ -21,16 +21,47 @@ class ApplicationTest extends NsTest { ); } + @Test + void 모든_경우의_수() { + assertRandomNumberInRangeTest( + () -> { + run("456", + "416", "412", "312", + "136", "132", + "145", "124", "123", + "2" + ); + assertThat(output()).contains( + "낫싱", + "1볼", "2볼", "3볼", + "1볼 1스트라이크", "2볼 1스트라이크", + "1스트라이크", "2스트라이크", "3스트라이크", + "게임 종료"); + }, + 1, 2, 3 + ); + } + @Test void 예외_테스트() { assertSimpleTest(() -> assertThatThrownBy(() -> runException("1234")) .isInstanceOf(IllegalArgumentException.class) ); + + assertSimpleTest(() -> + assertThatThrownBy(() -> runException("023")) + .isInstanceOf(IllegalArgumentException.class) + ); + + assertSimpleTest(() -> + assertThatThrownBy(() -> runException("112")) + .isInstanceOf(IllegalArgumentException.class) + ); } @Override public void runMain() { Application.main(new String[]{}); } -} +} \ No newline at end of file