diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..ce723b4a39 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -2,6 +2,7 @@ public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + Game game = new Game(); + game.start(); } } diff --git a/src/main/java/baseball/Computer.java b/src/main/java/baseball/Computer.java new file mode 100644 index 0000000000..d51593a867 --- /dev/null +++ b/src/main/java/baseball/Computer.java @@ -0,0 +1,22 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class Computer { + private List ComputerNumbers; + public void Random(){ + ComputerNumbers = new ArrayList<>(); + while(ComputerNumbers.size() < 3){ + int randomNumber = Randoms.pickNumberInRange(1,9); + if(!ComputerNumbers.contains(randomNumber)){ + ComputerNumbers.add(randomNumber); + } + } + } + public List getCom(){ + return ComputerNumbers; + } +} diff --git a/src/main/java/baseball/Game.java b/src/main/java/baseball/Game.java new file mode 100644 index 0000000000..c72cfcddf3 --- /dev/null +++ b/src/main/java/baseball/Game.java @@ -0,0 +1,42 @@ +package baseball; + +public class Game { + private Computer computer; + private Input input; + private Go go; + public Game(){ + this.computer = new Computer(); + this.input = new Input(); + this.go = new Go(); + } + public void newGame(){ + while(true){ + System.out.println("숫자를 입력해주세요 : "); + int[] player = input.getInput(); + String result = go.Check(computer.getCom(), player); + System.out.println(result); + + if(result.equals("3스트라이크")){ + break; + } + } + } + public void start(){ + while(true){ + System.out.println("숫자 야구 게임을 시작합니다."); + computer.Random(); + + newGame(); + + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String next = input.getUserInput(); + if (next.equals("2")) { + break; + } + else if(!next.equals("1")){ + continue; + } + } + } +} diff --git a/src/main/java/baseball/Go.java b/src/main/java/baseball/Go.java new file mode 100644 index 0000000000..2b0fdde6c7 --- /dev/null +++ b/src/main/java/baseball/Go.java @@ -0,0 +1,34 @@ +package baseball; + +import java.util.List; + +public class Go { + public String Check(List computer, int[] input){ + int strike = 0; + int ball = 0; + for(int i = 0; i < 3; i++){ + for(int j = 0; j < 3; j++){ + if(computer.get(i).equals(input[j])){ + if(i == j){ + strike++; + } + else{ + ball++; + } + } + } + } + if(ball == 0 && strike == 0){ + return "낫싱"; + } + else if(strike == 0){ + return ball + "볼"; + } + else if(ball == 0){ + return strike + "스트라이크"; + } + else{ + return ball + "볼 " + strike + "스트라이크"; + } + } +} diff --git a/src/main/java/baseball/Input.java b/src/main/java/baseball/Input.java new file mode 100644 index 0000000000..bd6fae9a52 --- /dev/null +++ b/src/main/java/baseball/Input.java @@ -0,0 +1,21 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Console; + +public class Input { + private int[] in = new int[3]; + public String getUserInput(){ + return Console.readLine(); + } + public int[] getInput(){ + String input = getUserInput(); + int a = Integer.parseInt(input); + if(a < 0 || a > 999){ + throw new IllegalArgumentException(); + } + for(int i = 0; i < 3; i++){ + in[i] = input.charAt(i)-'0'; + } + return in; + } +}