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

test #2821

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

test #2821

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
9 changes: 9 additions & 0 deletions src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package baseball;

import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;

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

public class Application {

public static void main(String[] args) {
// TODO: 프로그램 구현
BaseballController baseballController = new BaseballController();
baseballController.gameStart();
}
}
70 changes: 70 additions & 0 deletions src/main/java/baseball/BaseballController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package baseball;

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

public class BaseballController {
private final BaseballModel baseballModel;
private final BaseballView baseballView;
private final List<Integer> user;

public BaseballController() {
baseballModel = new BaseballModel();
baseballView = new BaseballView();
user = new ArrayList<>();
}

public void gameStart() {
System.out.println("숫자 야구 게임을 시작합니다.");
while (true) {
user.clear();
String userInput = baseballView.getUserInput();
exception(userInput);
for (int i = 0; i < Constants.NUMBER_SIZE; i++) {
user.add(userInput.charAt(i) - '0');
}
int[] result = baseballModel.calculateStrikeAndBall(user);
if (result[0] == 3) { //게임 승리
baseballView.printWinMessage();
int restart = baseballView.askForRestart();
if (restart == 1) {
baseballModel.getNewComputerNum();
} else if (restart == 2) {
return;
} else {
throw new IllegalArgumentException("1,2만 입력하세요");
}
} else {
baseballView.printResult(result[0], result[1]);
}
}
}

private static void exception(String input) {
if (input.length() != Constants.NUMBER_SIZE) {
throw new IllegalArgumentException("3개의 값이 들어가야 합니다.");
}

try {
Integer.parseInt(input);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("정수형 변환 불가");
}

if (hasDuplicatedDigits(input)) {
throw new IllegalArgumentException("3개의 숫자는 모두 달라야 합니다.");
}

}

private static boolean hasDuplicatedDigits(String input) {
for (int i = 0; i < input.length(); i++) {
for (int j = i + 1; j < input.length(); j++) {
if (input.charAt(i) == input.charAt(j)) {
return true;
}
}
}
return false;
}
}
41 changes: 41 additions & 0 deletions src/main/java/baseball/BaseballModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package baseball;

import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;
import java.util.List;

public class BaseballModel {
private List<Integer> computer;
public BaseballModel() {
this.computer = generateRandomNum();
}

public List<Integer> getNewComputerNum() {
return this.computer = generateRandomNum();
}

public int[] calculateStrikeAndBall(List<Integer> user) {
int strike = 0;
int ball = 0;
for (int i = 0; i < Constants.NUMBER_SIZE; i++) {
if (user.get(i).equals(computer.get(i))) {
strike++;
} else if (user.contains(computer.get(i))) {
ball++;
}
}
return new int[]{strike, ball};
}


public List<Integer> generateRandomNum() {
List<Integer> number = new ArrayList<>();
while (number.size() < Constants.NUMBER_SIZE) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (!number.contains(randomNumber)) {
number.add(randomNumber);
}
}
return number;
}
}
39 changes: 39 additions & 0 deletions src/main/java/baseball/BaseballView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package baseball;

import camp.nextstep.edu.missionutils.Console;

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

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

public void printResult(int strike, int ball) {
if (strike == 0 && ball == 0) {
System.out.println("낫싱");
} else if (strike > 0 && ball > 0) {
System.out.println(ball + "볼 " + strike + "스트라이크");
} else if (strike > 0) {
System.out.println(strike + "스트라이크");
} else if (ball > 0) {
System.out.println(ball + "볼");
}
}

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

public int askForRestart() {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
return Integer.parseInt(Console.readLine()); //이것도 예외처리 해야함
}

public void printError(String message) {
System.out.println(message);
}
}
5 changes: 5 additions & 0 deletions src/main/java/baseball/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package baseball;

public class Constants {
public static final int NUMBER_SIZE = 3;
}