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

Bowook #2817

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

Bowook #2817

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

import baseball.controller.BaseballController;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
BaseballController baseballController = new BaseballController();
baseballController.run();

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

import baseball.domain.Baseball;
import baseball.service.Conversion;
import baseball.service.Validate;
import baseball.view.InputView;
import baseball.view.OutputView;

public class BaseballController {
private final Baseball baseball;
private final InputView inputView;
private final OutputView outputView;

public BaseballController() {
this.baseball = new Baseball();
this.inputView = new InputView();
this.outputView = new OutputView();
outputView.startMessage();
}
public void run() {
boolean isRunning = true;
while (isRunning) {
isRunning = play();
}
}

public boolean play() {
baseball.computerNumbersGenerator();
while (true) {
String userInput = inputView.readBaseballNumbers();

if (!Validate.validator(userInput)) {
throw new IllegalArgumentException(); // 예외 발생
}

boolean endFlag = outputView.printMessage(baseball.compare(Conversion.stringToList(userInput)));

while(endFlag) {
outputView.endMessage();
userInput = inputView.readCommandNumbers();
int commandType = Validate.commandValidator(userInput);
if(commandType == 1) {
return true;
}
else if(commandType == 2) {
return false;
}
}
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/baseball/domain/Baseball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package baseball.domain;

import baseball.service.BaseballNumberGenerator;

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

public class Baseball {
private List<Integer> computerBaseballNumbers;

public Baseball() {
this.computerBaseballNumbers = new ArrayList<>();
}

public void computerNumbersGenerator() {
computerBaseballNumbers = BaseballNumberGenerator.generator();
}

public String compare(List<Integer> convertedNumbers) {
StringBuilder sb = new StringBuilder();
int strike = 0;
int ball = 0;
for(int i = 0; i < 3; i ++) {
if(Objects.equals(computerBaseballNumbers.get(i), convertedNumbers.get(i))) {
strike += 1;
}
else if (computerBaseballNumbers.contains(convertedNumbers.get(i))) {
ball += 1;
}
}
if(strike != 0 && ball != 0) {
sb.append(ball).append("볼 ").append(strike).append("스트라이크");
}
else if(strike == 0 && ball != 0) {
sb.append(ball).append("볼");
}
else if(ball == 0 && strike != 0) {
sb.append(strike).append("스트라이크");
}
else if(strike == 0 && ball == 0) {
sb.append("낫싱");
}

return sb.toString();
}

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

import camp.nextstep.edu.missionutils.Randoms;

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


public class BaseballNumberGenerator {

public static List<Integer> generator() {
List<Integer> computer = new ArrayList<>();
while (computer.size() < 3) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (!computer.contains(randomNumber)) {
computer.add(randomNumber);
}
}

return computer;
}
}
14 changes: 14 additions & 0 deletions src/main/java/baseball/service/Conversion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package baseball.service;

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

public class Conversion {
public static List<Integer> stringToList(String userNumbers) {
List<Integer> convertedNumbers = new ArrayList<>();
for(int i = 0; i < 3; i ++) {
convertedNumbers.add(userNumbers.charAt(i)-'0');
}
return convertedNumbers;
}
}
44 changes: 44 additions & 0 deletions src/main/java/baseball/service/Validate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package baseball.service;

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

public class Validate {

public static boolean validator(String userInput) {
if (userInput == null || userInput.isBlank()) {
return false;
} else if (!userInput.matches("\\d+")) {
return false;
} else if (userInput.length() != 3) {
return false;
} else if (!hasUniqueDigits(userInput)) {
return false;
}

return true;
}


public static int commandValidator(String userCommandInput) {
if(!(userCommandInput.equals("1") || userCommandInput.equals("2"))) {
throw new IllegalArgumentException("1또는 2를 입력해주세요.");
}
else if (userCommandInput.equals("1")) {
return 1;
}
else {
return 2;
}
}

private static boolean hasUniqueDigits(String userInput) {
Set<Character> digitSet = new HashSet<>();
for (char c : userInput.toCharArray()) {
if (!digitSet.add(c)) {
return false;
}
}
return true;
}
}
20 changes: 20 additions & 0 deletions src/main/java/baseball/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package baseball.view;

import camp.nextstep.edu.missionutils.Console;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputView {

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

public String readCommandNumbers() {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
return Console.readLine();
}
}
22 changes: 22 additions & 0 deletions src/main/java/baseball/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package baseball.view;

public class OutputView {

public void startMessage() {
System.out.println("숫자 야구 게임을 시작합니다.");
}

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

public boolean printMessage(String message) {
System.out.println(message);
boolean threeStrikeFlag = false;
if(message.equals("3스트라이크")) {
threeStrikeFlag = true;
}

return threeStrikeFlag;
}
}