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

[야구 게임 미션] 오지민 미션 제출합니다. #11

Open
wants to merge 8 commits into
base: Ojimin
Choose a base branch
from
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,24 @@
<br>

---
## 구현할 기능 목록
- 컴퓨터가 임의의 수 3개 선택하도록 구현
- 게임 진행
- 스트라이크일 경우
- 볼일 경우
- 낫싱일 경우
- 3개의 숫자를 모두 맞힐시에 대한 결과 출력
- 게임이 끝났을 경우에 대한 처리
- 입력에 대한 에러처리
- 3자리의 수가 아닌경우
- 숫자가 아닌 입력을 받은 경우
- 입력받은 수가 서로 다른 자릿수가 아닐 경우
- 게임이 끝난 경우 재시작/종료를 구분하는 1과 2가 아닌 다른
숫자를 입력한 경우

<br>

---
## 📝 License

This project is [MIT](https://github.com/woowacourse/java-baseball-precourse/blob/master/LICENSE) licensed.
12 changes: 12 additions & 0 deletions src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package baseball;

import baseball.controller.BaseballGame;
import baseball.model.ComputerNumber;
import baseball.model.Result;
import baseball.model.UserNumber;
import baseball.view.InputView;
import baseball.view.OutputView;

import java.awt.*;
import java.util.List;

public class Application {
public static void main(String[] args) {
//TODO: 숫자 야구 게임 구현
BaseballGame baseballGame = new BaseballGame();
baseballGame.start();
}
}
52 changes: 52 additions & 0 deletions src/main/java/baseball/controller/BaseballGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package baseball.controller;

import baseball.model.ComputerNumber;
import baseball.model.Result;
import baseball.model.UserNumber;
import baseball.validate.ValidateRestart;
import baseball.view.InputView;
import baseball.view.OutputView;

import java.util.List;

public class BaseballGame {

public void start() {
do {
List<Integer> computerRandomNumber = makeComputerNumber();
playGame(computerRandomNumber);
} while (isRestart());
}

public List<Integer> makeComputerNumber() {
ComputerNumber computerNumber = new ComputerNumber();
return computerNumber.getRandomNum();
}

public void playGame(List<Integer> computerRandomNumber) {
while (true) {
UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber());
Result result = makeResult(inputUserNumber, computerRandomNumber);
OutputView.printResult(result);
if (result.isSuccess()) {
break;
}
}
}

public Result makeResult(UserNumber inputUserNumber, List<Integer> computerRandomNumber) {
Result result = new Result();
inputUserNumber.countBall(computerRandomNumber, result);
inputUserNumber.countStrike(computerRandomNumber, result);
result.updateIsNothing();
return result;
}

public boolean isRestart() {
OutputView.outputRestartMessage();
String inputUserRestartNumber = InputView.inputUserRestartNumber();
ValidateRestart.validateInputRestartNumber(inputUserRestartNumber);
return inputUserRestartNumber.equals("1");
}

}
29 changes: 29 additions & 0 deletions src/main/java/baseball/model/ComputerNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package baseball.model;

import java.util.ArrayList;
import java.util.List;

import static camp.nextstep.edu.missionutils.Randoms.pickNumberInRange;

public class ComputerNumber {
private List<Integer> randomNum;

public ComputerNumber() {
this.randomNum = makeRandomNum();
}

public List<Integer> makeRandomNum() {
List<Integer> randomNum = new ArrayList<>();
while (randomNum.size() < 3) {
int random = pickNumberInRange(1, 9);
if (!randomNum.contains(random)) {
randomNum.add(random);
}
}
return randomNum;
}

public List<Integer> getRandomNum() {
return this.randomNum;
}
}
46 changes: 46 additions & 0 deletions src/main/java/baseball/model/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package baseball.model;

/**
* 온전히 결과에 대한 책임만
*/
public class Result {
private int ball;
private int strike;
private boolean isNothing;

public Result() {
this.ball = 0;
this.strike = 0;
this.isNothing = false;
}

public int getBall() {
return ball;
}

public int getStrike() {
return strike;
}

public boolean isNothing() {
return isNothing;
}

public void updateBall() {
this.ball+=1;
}

public void updateStrike() {
this.strike+=1;
}

public void updateIsNothing() {
if (this.ball == 0 && this.strike == 0) {
this.isNothing = true;
}
}

public boolean isSuccess() {
return this.strike == 3;
}
}
44 changes: 44 additions & 0 deletions src/main/java/baseball/model/UserNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package baseball.model;

import baseball.validate.ValidateUserNumber;

import java.util.List;

public class UserNumber {
private String userNum;

public UserNumber(String userNum) {
ValidateUserNumber.isNumber(userNum);
ValidateUserNumber.is3DigitNumber(userNum);
ValidateUserNumber.isDulplicateDigit(userNum);
this.userNum = userNum;
}

public String getUserNum() {
return this.userNum;
}

public void countBall(List<Integer> computerNumber, Result result) {
for (int i = 0; i < computerNumber.size(); i++) {
checkBallByUserNumber(i, computerNumber, result);
}
}

public void checkBallByUserNumber(int i, List<Integer> computerNumber, Result result) {
for (int j = 0; j < this.getUserNum().length(); j++) {
if (i != j && computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(j))) {
result.updateBall();
}
}
}

public void countStrike(List<Integer> computerNumber, Result result) {
for (int i = 0; i < computerNumber.size(); i++) {
if (computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(i))) {
result.updateStrike();
}
}
}


}
10 changes: 10 additions & 0 deletions src/main/java/baseball/validate/ValidateRestart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package baseball.validate;

public class ValidateRestart {

public static void validateInputRestartNumber(String inputRestartNumber){
if (!inputRestartNumber.equals("1") && !inputRestartNumber.equals("2")) {
throw new IllegalArgumentException("잘못된 입력입니다");
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/baseball/validate/ValidateUserNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package baseball.validate;

import java.util.HashSet;
import java.util.Set;

public class ValidateUserNumber {

public static void isNumber(String userNumber) {
try {
int toNumber = Integer.parseInt(userNumber);
} catch (NumberFormatException e) {
System.out.println("숫자로 입력해주세요");
}
}

public static void is3DigitNumber(String userNumber) {
if (userNumber.length() != 3) {
throw new IllegalArgumentException("3자리 숫자로 입력해주세요");
}
}

public static void isDulplicateDigit(String userNumber) {
Set<Character> digitSet = new HashSet<>();
for (char digit : userNumber.toCharArray()) {
if (!digitSet.add(digit)) {
throw new IllegalArgumentException("각 자리 숫자가 중복되지 않게 입력해주세요.");
}
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/baseball/view/InputView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package baseball.view;

import camp.nextstep.edu.missionutils.Console;

public class InputView {

public static String inputUserNumber() {
System.out.print("숫자를 입력해주세요 : ");
return Console.readLine();
}

public static String inputUserRestartNumber() {
return Console.readLine();
}
}
50 changes: 50 additions & 0 deletions src/main/java/baseball/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
package baseball.view;

import baseball.model.Result;

public class OutputView {

public static void printResult(Result result) {
if (result.isSuccess()) {
outputSuccessMessage();
}
if (!result.isSuccess()) {
printNotSuccessResult(result);
}
}

public static void printNotSuccessResult(Result result) {
outputNothingMessage(result);
outputBallMessage(result);
outputStrikeMessage(result);
outputBallStrikeMessage(result);
}


public static void outputNothingMessage(Result result) {
if (result.isNothing()) {
System.out.println("낫싱");
}
}

public static void outputBallMessage(Result result) {
if (result.getBall() > 0 && result.getStrike() == 0) {
System.out.println(result.getBall() + "볼");
}
}

public static void outputStrikeMessage(Result result) {
if (result.getStrike() > 0 && result.getBall() == 0) {
System.out.println(result.getStrike() + "스트라이크");
}
}

public static void outputBallStrikeMessage(Result result) {
if (result.getBall() > 0 && result.getStrike() > 0) {
System.out.println(result.getBall() + "볼 " + result.getStrike() + "스트라이크");
}
}

public static void outputSuccessMessage() {
System.out.println("3스트라이크\n3개의 숫자를 모두 맞히셨습니다! 게임 종료");
}

public static void outputRestartMessage() {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
}
}