From 3ebec05095170c11e7a5ef0ece87ee7515ebf7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=82=98=EC=9A=A9=EC=A4=80?= <141994188+youngJun99@users.noreply.github.com> Date: Fri, 11 Oct 2024 22:31:42 +0900 Subject: [PATCH] fe --- src/main/java/baseball/Application.java | 23 +++++++++- .../baseball/controller/GameController.java | 46 +++++++++++++++++++ .../baseball/model/computer/Computer.java | 28 +++++++++++ .../model/computer/InputValidator.java | 33 +++++++++++++ .../model/computer/RandomNumberGenerator.java | 21 +++++++++ .../java/baseball/model/computer/Score.java | 33 +++++++++++++ .../java/baseball/model/player/Player.java | 30 ++++++++++++ src/main/java/baseball/view/CommonView.java | 10 ++++ src/main/java/baseball/view/InputView.java | 31 +++++++++++++ src/main/java/baseball/view/OutputView.java | 27 +++++++++++ src/test/java/baseball/ApplicationTest.java | 35 -------------- 11 files changed, 281 insertions(+), 36 deletions(-) create mode 100644 src/main/java/baseball/controller/GameController.java create mode 100644 src/main/java/baseball/model/computer/Computer.java create mode 100644 src/main/java/baseball/model/computer/InputValidator.java create mode 100644 src/main/java/baseball/model/computer/RandomNumberGenerator.java create mode 100644 src/main/java/baseball/model/computer/Score.java create mode 100644 src/main/java/baseball/model/player/Player.java create mode 100644 src/main/java/baseball/view/CommonView.java create mode 100644 src/main/java/baseball/view/InputView.java create mode 100644 src/main/java/baseball/view/OutputView.java delete mode 100644 src/test/java/baseball/ApplicationTest.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..99976321d2 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,28 @@ package baseball; +import baseball.controller.GameController; +import baseball.model.computer.Computer; +import baseball.model.computer.InputValidator; +import baseball.model.computer.RandomNumberGenerator; +import baseball.model.computer.Score; +import baseball.model.player.Player; +import baseball.view.CommonView; +import baseball.view.InputView; +import baseball.view.OutputView; + +import java.util.ArrayList; + + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + GameController gameController = new GameController( + new Computer(new ArrayList<>(),new Score(), new RandomNumberGenerator()), + new InputValidator(), + new Player(new ArrayList<>(), 1), + new CommonView(), + new InputView(new InputValidator()), + new OutputView()); + + gameController.start(); } } diff --git a/src/main/java/baseball/controller/GameController.java b/src/main/java/baseball/controller/GameController.java new file mode 100644 index 0000000000..b5788d2515 --- /dev/null +++ b/src/main/java/baseball/controller/GameController.java @@ -0,0 +1,46 @@ +package baseball.controller; + +import baseball.model.computer.Computer; +import baseball.model.computer.InputValidator; +import baseball.model.computer.RandomNumberGenerator; +import baseball.model.player.Player; +import baseball.view.CommonView; +import baseball.view.InputView; +import baseball.view.OutputView; + +public class GameController { + private final Computer computer; + private final InputValidator inputValidator; + private final Player player; + private final CommonView commonView; + private final InputView inputView; + private final OutputView outputView; + + public GameController(Computer computer, InputValidator inputValidator, Player player, CommonView commonView, InputView inputView, OutputView outputView) { + this.computer = computer; + this.inputValidator = inputValidator; + this.player = player; + this.commonView = commonView; + this.inputView = inputView; + this.outputView = outputView; + } + + public void start(){ + commonView.printStartMessage(); + while (player.getToken()==1){ + computer.makeAnswer(); + playGame(); + player.setToken(inputValidator.validateRestartNumber(inputView.printRestartMessage())); + } + } + + public void playGame(){ + + while(computer.getScore().getStrike() != RandomNumberGenerator.NUMBER_LENGTH){ + player.setGuess(inputValidator.validateGameNumber(inputView.printInputMessage())); + computer.validateAnswer(player.getGuess()); + outputView.printScore(computer.getScore()); + } + computer.getScore().resetScore(); + } +} diff --git a/src/main/java/baseball/model/computer/Computer.java b/src/main/java/baseball/model/computer/Computer.java new file mode 100644 index 0000000000..25f232ade8 --- /dev/null +++ b/src/main/java/baseball/model/computer/Computer.java @@ -0,0 +1,28 @@ +package baseball.model.computer; + +import java.util.List; + +public class Computer { + + private List answerNumber; + private Score score; + private RandomNumberGenerator randomNumberGenerator; + + public Computer(List answerNumber, Score score, RandomNumberGenerator randomNumberGenerator) { + this.answerNumber = answerNumber; + this.score = score; + this.randomNumberGenerator = randomNumberGenerator; + } + + public void makeAnswer(){ + answerNumber = randomNumberGenerator.generateRandNum(); + } + + public void validateAnswer(List guess){ + score.validateAnswer(answerNumber, guess); + } + + public Score getScore() { + return score; + } +} diff --git a/src/main/java/baseball/model/computer/InputValidator.java b/src/main/java/baseball/model/computer/InputValidator.java new file mode 100644 index 0000000000..13be79105e --- /dev/null +++ b/src/main/java/baseball/model/computer/InputValidator.java @@ -0,0 +1,33 @@ +package baseball.model.computer; + +import java.util.ArrayList; +import java.util.List; + +public class InputValidator { + + public Integer validateRestartNumber(String restartNum){ + + if( !restartNum.equals("1") && !restartNum.equals("2")) { + throw new IllegalArgumentException("입력값이 1 또는 2가 아닙니다."); + } + + return Integer.parseInt(restartNum); + } + + public List validateGameNumber(String gameNumber) { + + if (gameNumber.length() != RandomNumberGenerator.NUMBER_LENGTH ){ + throw new IllegalArgumentException(String.format("%d 자리가 아닙니다", RandomNumberGenerator.NUMBER_LENGTH)); + } + + List number = new ArrayList<>(); + + for(int i=0; i<3; i++){ + if(!(gameNumber.charAt(i)>= 49 && gameNumber.charAt(i)<=57)) throw new IllegalArgumentException("1 부터 9 사이 숫자가 아닙니다"); + number.add(Character.getNumericValue(gameNumber.charAt(i))); + } + + return number; + } + +} diff --git a/src/main/java/baseball/model/computer/RandomNumberGenerator.java b/src/main/java/baseball/model/computer/RandomNumberGenerator.java new file mode 100644 index 0000000000..8eb80f9247 --- /dev/null +++ b/src/main/java/baseball/model/computer/RandomNumberGenerator.java @@ -0,0 +1,21 @@ +package baseball.model.computer; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class RandomNumberGenerator { + + public static final Integer NUMBER_LENGTH = 3; + + public List generateRandNum(){ + List number = new ArrayList<>(); + while(number.size()< NUMBER_LENGTH){ + int randNum = Randoms.pickNumberInRange(1,9); + if(!number.contains(randNum)) number.add(randNum); + } + return number; + } + +} diff --git a/src/main/java/baseball/model/computer/Score.java b/src/main/java/baseball/model/computer/Score.java new file mode 100644 index 0000000000..8d55ba7471 --- /dev/null +++ b/src/main/java/baseball/model/computer/Score.java @@ -0,0 +1,33 @@ +package baseball.model.computer; + +import java.util.List; + +public class Score { + + private int balls; + private int strikes; + + public int getBalls() { + return balls; + } + + public int getStrike() { + return strikes; + } + + public void validateAnswer(List answer ,List guess){ + balls=0; + strikes=0; + + for(int i=0; i guess; + private int token; + + public Player(List guess, int token) { + this.guess = guess; + this.token = token; + } + + public void setGuess(List newGuess){ + guess = newGuess; + } + + public void setToken(int newToken){ + token=newToken; + } + + public int getToken() { + return token; + } + + public List getGuess() { + return guess; + } +} diff --git a/src/main/java/baseball/view/CommonView.java b/src/main/java/baseball/view/CommonView.java new file mode 100644 index 0000000000..aac07fe5b8 --- /dev/null +++ b/src/main/java/baseball/view/CommonView.java @@ -0,0 +1,10 @@ +package baseball.view; + +public class CommonView { + + public static final String START_MESSAGE = "숫자 야구 게임을 시작합니다."; + + public void printStartMessage(){ + System.out.println(START_MESSAGE); + } +} diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java new file mode 100644 index 0000000000..b9c2474ec3 --- /dev/null +++ b/src/main/java/baseball/view/InputView.java @@ -0,0 +1,31 @@ +package baseball.view; + +import baseball.model.computer.InputValidator; +import camp.nextstep.edu.missionutils.Console; + +import java.util.List; + +import static camp.nextstep.edu.missionutils.Console.readLine; + +public class InputView { + private static final String RESTART_MESSAGE = "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."; + private static final String INPUT_MESSAGE = "숫자를 입력해주세요 : "; + + private final InputValidator inputValidator; + + public InputView(InputValidator inputValidator) { + this.inputValidator = inputValidator; + } + + public String printRestartMessage(){ + System.out.println(RESTART_MESSAGE); + String restart = readLine(); + return restart; + } + + public String printInputMessage(){ + System.out.println(INPUT_MESSAGE); + String input = readLine(); + return input; + } +} diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java new file mode 100644 index 0000000000..acf09018a7 --- /dev/null +++ b/src/main/java/baseball/view/OutputView.java @@ -0,0 +1,27 @@ +package baseball.view; + +import baseball.model.computer.RandomNumberGenerator; +import baseball.model.computer.Score; + +public class OutputView { + private static final String WIN_MESSAGE = "%d개의 숫자를 모두 맞히셨습니다! 게임종료"; + public void printScore(Score score){ + int ball = score.getBalls(); + int strike = score.getStrike(); + + if(ball == 0 && strike==0) { + System.out.println("낫싱"); + }else if(strike == RandomNumberGenerator.NUMBER_LENGTH){ + System.out.println(strike+"스트라이크"); + System.out.println(String.format(WIN_MESSAGE,RandomNumberGenerator.NUMBER_LENGTH)); + }else if(ball==0){ + System.out.println(strike+"스트라이크"); + }else if(strike==0){ + System.out.println(ball+"볼"); + }else{ + System.out.println(ball+"볼 "+strike+"스트라이크"); + } + + + } +} diff --git a/src/test/java/baseball/ApplicationTest.java b/src/test/java/baseball/ApplicationTest.java deleted file mode 100644 index 3fa29fa67b..0000000000 --- a/src/test/java/baseball/ApplicationTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package baseball; - -import camp.nextstep.edu.missionutils.test.NsTest; -import org.junit.jupiter.api.Test; - -import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomNumberInRangeTest; -import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -class ApplicationTest extends NsTest { - @Test - void 게임종료_후_재시작() { - assertRandomNumberInRangeTest( - () -> { - run("246", "135", "1", "597", "589", "2"); - assertThat(output()).contains("낫싱", "3스트라이크", "1볼 1스트라이크", "3스트라이크", "게임 종료"); - }, - 1, 3, 5, 5, 8, 9 - ); - } - - @Test - void 예외_테스트() { - assertSimpleTest(() -> - assertThatThrownBy(() -> runException("1234")) - .isInstanceOf(IllegalArgumentException.class) - ); - } - - @Override - public void runMain() { - Application.main(new String[]{}); - } -}