Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

숫자야구 연습 #2820

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
46 changes: 46 additions & 0 deletions src/main/java/baseball/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
28 changes: 28 additions & 0 deletions src/main/java/baseball/model/computer/Computer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package baseball.model.computer;

import java.util.List;

public class Computer {

private List<Integer> answerNumber;
private Score score;
private RandomNumberGenerator randomNumberGenerator;

public Computer(List<Integer> answerNumber, Score score, RandomNumberGenerator randomNumberGenerator) {
this.answerNumber = answerNumber;
this.score = score;
this.randomNumberGenerator = randomNumberGenerator;
}

public void makeAnswer(){
answerNumber = randomNumberGenerator.generateRandNum();
}

public void validateAnswer(List<Integer> guess){
score.validateAnswer(answerNumber, guess);
}

public Score getScore() {
return score;
}
}
33 changes: 33 additions & 0 deletions src/main/java/baseball/model/computer/InputValidator.java
Original file line number Diff line number Diff line change
@@ -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<Integer> validateGameNumber(String gameNumber) {

if (gameNumber.length() != RandomNumberGenerator.NUMBER_LENGTH ){
throw new IllegalArgumentException(String.format("%d 자리가 아닙니다", RandomNumberGenerator.NUMBER_LENGTH));
}

List<Integer> 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;
}

}
21 changes: 21 additions & 0 deletions src/main/java/baseball/model/computer/RandomNumberGenerator.java
Original file line number Diff line number Diff line change
@@ -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<Integer> generateRandNum(){
List<Integer> number = new ArrayList<>();
while(number.size()< NUMBER_LENGTH){
int randNum = Randoms.pickNumberInRange(1,9);
if(!number.contains(randNum)) number.add(randNum);
}
return number;
}

}
33 changes: 33 additions & 0 deletions src/main/java/baseball/model/computer/Score.java
Original file line number Diff line number Diff line change
@@ -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<Integer> answer ,List<Integer> guess){
balls=0;
strikes=0;

for(int i=0; i<answer.size(); i++){
if(answer.get(i)==guess.get(i)) strikes++;
for(int j=0; j<answer.size(); j++){
if(answer.get(i)==guess.get(j) && i!=j) balls ++;
}
}
}
public void resetScore(){
strikes=0;
balls=0;
}
}
30 changes: 30 additions & 0 deletions src/main/java/baseball/model/player/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package baseball.model.player;

import java.util.List;

public class Player {

private List<Integer> guess;
private int token;

public Player(List<Integer> guess, int token) {
this.guess = guess;
this.token = token;
}

public void setGuess(List<Integer> newGuess){
guess = newGuess;
}

public void setToken(int newToken){
token=newToken;
}

public int getToken() {
return token;
}

public List<Integer> getGuess() {
return guess;
}
}
10 changes: 10 additions & 0 deletions src/main/java/baseball/view/CommonView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package baseball.view;

public class CommonView {

public static final String START_MESSAGE = "숫자 야구 게임을 시작합니다.";

public void printStartMessage(){
System.out.println(START_MESSAGE);
}
}
31 changes: 31 additions & 0 deletions src/main/java/baseball/view/InputView.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions src/main/java/baseball/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -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+"스트라이크");
}


}
}
35 changes: 0 additions & 35 deletions src/test/java/baseball/ApplicationTest.java

This file was deleted.