diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 000000000..37aa17a2f --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,13 @@ +import baseball.controller.BaseballGame; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Application { + public static void main(String[]args) throws IOException { + BaseballGame game = new BaseballGame(); + game.play(); + } +} diff --git a/src/main/java/baseball/controller/BaseballGame.java b/src/main/java/baseball/controller/BaseballGame.java new file mode 100644 index 000000000..190f1d5a8 --- /dev/null +++ b/src/main/java/baseball/controller/BaseballGame.java @@ -0,0 +1,36 @@ +package baseball.controller; + +import baseball.model.Computer; +import java.io.IOException; +import java.util.HashMap; +import view.InputView; + +public class BaseballGame { + + private final InputView inputView; + private Computer computer; + + public BaseballGame() { + this.inputView = new InputView(); + this.computer = new Computer(); + } + + public void play() throws IOException { + + while (true) { + HashMap answer = computer.getAnswer(); + + while (true) { + String userNumber = inputView.validationNumber(); + if (computer.validationStrikeAndBall(answer, userNumber)) { + break; + } + } + + if (inputView.isExit()) { + return; + } + computer = computer.newGame(); + } + } +} diff --git a/src/main/java/baseball/model/Computer.java b/src/main/java/baseball/model/Computer.java new file mode 100644 index 000000000..24d711feb --- /dev/null +++ b/src/main/java/baseball/model/Computer.java @@ -0,0 +1,90 @@ +package baseball.model; + +import java.util.HashMap; +import java.util.Random; + +public class Computer { + + private final HashMap answer; + + public Computer() { + this.answer = createAnswer(); + } + + public Computer newGame() { + return new Computer(); + } + + public HashMap createAnswer() { + HashMap answer = new HashMap<>(); + + int NUMBER_SPACE_SIZE = 3; + + while (answer.size() < NUMBER_SPACE_SIZE) { + int randomNumber = getRandomNumber(); + if (answer.containsKey(randomNumber)) { + continue; + } + answer.put(randomNumber, answer.size() + 1); + } + + return answer; + } + + public int[] calculationStrikeAndBall(HashMap answer, String userNumber) { + int strike = 0; + int ball = 0; + + for (int i = 0; i < userNumber.length(); i++) { + int digit = Character.getNumericValue(userNumber.charAt(i)); + Integer answerValue = answer.get(digit); + + if (answerValue != null) { + if (answerValue.equals(i + 1)) { + strike++; + } else { + ball++; + } + } + } + return new int[]{strike, ball}; + } + + public boolean validationStrikeAndBall(HashMap answer, String userNumber) { + int[] strikeAndBall = calculationStrikeAndBall(answer, userNumber); + int strike = strikeAndBall[0]; + int ball = strikeAndBall[1]; + + if (strike == 3) { + System.out.println("정답입니다."); + } + + if (strike == 0 && ball == 0) { + System.out.println("낫씽"); + } + + if (strike > 0 && ball == 0) { + System.out.printf("%s 스트라이크%s", strike, System.lineSeparator()); + } + + if (strike == 0 && ball > 0) { + System.out.printf("%s 볼%s", ball, System.lineSeparator()); + } + + if (strike > 0 && ball > 0) { + System.out.printf("%s 스트라이크, %s 볼%s", strike, ball, System.lineSeparator()); + } + + return strike == 3; + } + + public int getRandomNumber() { + Random random = new Random(); + return random.nextInt(9) + 1; + } + + public HashMap getAnswer() { + return answer; + } +} + diff --git a/src/main/java/global/ValidationInput.java b/src/main/java/global/ValidationInput.java new file mode 100644 index 000000000..7b2d5cc3e --- /dev/null +++ b/src/main/java/global/ValidationInput.java @@ -0,0 +1,55 @@ +package global; + +import java.util.HashSet; +import java.util.Set; + +public class ValidationInput { + + public boolean isValidationUserNumber(String input) { + if (!isEmpty(input) || !isValidationLength(input) || !isContainsZero(input) || !isDuplicate( + input)) { + return false; + } + return true; + } + + public boolean isEmpty(String input) { + if (input.isEmpty() || input.contains(" ")) { + System.out.println("공백을 입력하였습니다."); + return false; + } + return true; + } + + public boolean isValidationLength(String input) { + if (input.length() != 3) { + System.out.println("3자리를 입력해 주세요."); + return false; + } + return true; + } + + public boolean isContainsZero(String input) { + if (input.contains("0")) { + System.out.println("1~9를 입력해 주세요."); + return false; + } + return true; + } + + public boolean isDuplicate(String input) { + Set number = new HashSet<>(); + + for (int i = 0; i < input.length(); i++) { + char word = input.charAt(i); + if (!number.add(word)) { + System.out.println("숫자는 중복되면 안됩니다."); + return false; + } + } + return true; + } + +} + + diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 000000000..828963b00 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,46 @@ +package view; + +import global.ValidationInput; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class InputView { + + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + private final ValidationInput validationInput; + + public InputView() { + validationInput = new ValidationInput(); + } + + public String getUserNumber() throws IOException { + System.out.print("숫자를 입력해 주세요 : "); + return in.readLine(); + } + + public boolean isExit() throws IOException { + while (true) { + System.out.println("게임을 계속하시려면 1, 종료하시려면 2를 입력해주세요"); + String input = in.readLine(); + String CONTINUE = "1"; + String EXIT = "2"; + if (input.equals(EXIT)) { + return true; + } + if (input.equals(CONTINUE)) { + return false; + } + } + } + + public String validationNumber() throws IOException { + String input; + + do { + input = getUserNumber(); + } while (!validationInput.isValidationUserNumber(input)); + + return input; + } +} diff --git a/src/test/java/baseball/BaseBallTest.java b/src/test/java/baseball/BaseBallTest.java new file mode 100644 index 000000000..e7941c100 --- /dev/null +++ b/src/test/java/baseball/BaseBallTest.java @@ -0,0 +1,25 @@ +package baseball; + +import static org.assertj.core.api.Assertions.assertThat; + +import baseball.model.Computer; +import java.util.HashMap; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class BaseBallTest { + + @Test + @DisplayName("컴퓨터 숫자 생성 테스트") + void CREATE_RANDOM_NUMBER() { + //given + Computer computer = new Computer(); + computer.createAnswer(); + + //when + HashMap number = computer.getAnswer(); + + //then + assertThat(number.size()).isEqualTo(3); + } +}