From e43a482119c25e0e2f8c590c032cf1adee60a8cc Mon Sep 17 00:00:00 2001 From: "myung_107@naver.com" Date: Wed, 25 Oct 2023 23:42:56 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=95=BC=EA=B5=AC=20=EA=B2=8C=EC=9E=84?= =?UTF-8?q?=EC=9D=84=20=EA=B5=AC=EC=84=B1=ED=95=98=EB=8A=94=20Game=20class?= =?UTF-8?q?=20=EC=A0=9C=EC=9E=91=20\n=204=EA=B0=80=EC=A7=80=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=EB=A1=9C=20=EA=B5=AC=EC=84=B1=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EC=9E=90=EC=9E=85=EB=A0=A5,=20=EC=BB=B4=ED=93=A8=ED=84=B0?= =?UTF-8?q?=EC=88=AB=EC=9E=90=EC=83=9D=EC=84=B1,=20ball=20strike=20?= =?UTF-8?q?=ED=8C=90=EB=B3=84,=20=EA=B2=B0=EA=B3=BC=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++ src/main/java/baseball/Application.java | 6 +- src/main/java/baseball/Game.java | 154 ++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 src/main/java/baseball/Game.java diff --git a/README.md b/README.md index 1bee39036..b11b01a58 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,20 @@ - 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다. - 아래의 프로그래밍 실행 결과 예시와 동일하게 입력과 출력이 이루어져야 한다. +기능요구사항정리 +-숫자야구게임을 담당하는 Game class 제작 +- 사용자로부터 입력 받기 +public List GetUser() + +- 랜덤으로 세자리수의 수 생성 +public List GetComputer() + +- 볼, 스트라이크 판단 후 결과 반환 +public List checkResult + +- 볼, 스트라이크 결과 입력 후 모니터 출력 혹은 게임 재시작 +public int printResult +
## ✍🏻 입출력 요구사항 diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 7f1901b8b..b697ca610 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,9 @@ package baseball; - +//TODO: 숫자 야구 게임 구현 public class Application { public static void main(String[] args) { - //TODO: 숫자 야구 게임 구현 + + Game game = new Game(); + game.playGame(); } } diff --git a/src/main/java/baseball/Game.java b/src/main/java/baseball/Game.java new file mode 100644 index 000000000..2987c19fa --- /dev/null +++ b/src/main/java/baseball/Game.java @@ -0,0 +1,154 @@ +package baseball; +import camp.nextstep.edu.missionutils.Randoms; +import static camp.nextstep.edu.missionutils.Console.readLine; +import java.util.ArrayList; +import java.util.List; + +public class Game { + + public List GetUser(){ + System.out.print("숫자를 입력해주세요 : "); + String inputString = readLine(); + + if(inputString.length()>=4){ + throw new IllegalArgumentException(); + } + + int intValue; + + try { + intValue = Integer.parseInt(inputString); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(); + } + + if (intValue <= 1 && intValue <= 9) { + throw new IllegalArgumentException(); + } + + + + + List user = new ArrayList<>(); + + for (int i=0; i GetComputer(){ + List computer = new ArrayList<>(); + while (computer.size() < 3) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!computer.contains(randomNumber)) { + computer.add(randomNumber); + } + } + + return computer; + } + + public List checkResult(List user, List computer){ + List result = new ArrayList<>(); + List tmp = new ArrayList<>(); + for (int i=0; i<3; i++) { + tmp.add(computer.get(i)); + } + int strike=0; + int ball=0; + //같은자리에 같은숫자 = S, 다른자리에 같은 숫자 = B; + + for (int i=0; i<3; i++) { + if (user.get(i) == tmp.get(i)) { + strike += 1; + } + } + + for (int i=0; i<3; i++) { + if (user.get(0) == tmp.get(i)) { + ball += 1; + tmp.set(i,0); + continue; + } + if (user.get(1) == tmp.get(i)) { + ball += 1; + tmp.set(i,0); + continue; + } + if (user.get(2) == tmp.get(i)) { + ball += 1; + tmp.set(i,0); + continue; + } + } + + result.add(strike); + result.add(ball-strike); + return result; + } + + public int printResult(List result){ + if(result.get(0) == 3){ + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String exitcode = readLine(); + + int intValue; + try { + intValue = Integer.parseInt(exitcode); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(); + } + + if (intValue != 1 && intValue != 2) { + throw new IllegalArgumentException(); + } + + if(intValue == 2){ + return 2; + } + else{ + return 3; + } + } + else if(result.get(0)==0 && result.get(1) == 0){ + System.out.println("낫싱"); + } + else if(result.get(0) !=0 && result.get(1) == 0){ + System.out.printf("%d스트라이크",result.get(0)); + System.out.println(); + } + else if(result.get(0)==0 && result.get(1) != 0){ + System.out.printf("%d볼",result.get(1)); + System.out.println(); + } + else{ + System.out.printf("%d볼 %d스트라이크",result.get(0), result.get(1)); + System.out.println(); + } + + return 1; + } + public void playGame(){ + Game game = new Game(); + List computer = game.GetComputer(); + List user = game.GetUser(); + int flag; + while(true){ + flag = printResult(game.checkResult(user,computer)); + if(flag==1){ + user = game.GetUser(); + } + else if(flag==3){ + computer = game.GetComputer(); + user = game.GetUser(); + } + else{ + break; + } + } + } +}