Skip to content
46 changes: 25 additions & 21 deletions hyuunnn/src/main/java/com/hyuunnn/baseball/Baseball.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.hyuunnn.baseball;

import java.util.*;
import java.util.Scanner;
import java.util.Set;
import java.util.LinkedHashSet;
import java.util.Random;


public class Baseball {

final private int GAME_RESTART = 1;
final private int GAME_END = 2;
final private int BALL_LENGTH = 3;
final private Scanner scanner = new Scanner(System.in);

private String randomize() {
final Random r = new Random();
final Set<Integer> numberList = new LinkedHashSet<>();
Random r = new Random();
Set<Integer> numberList = new LinkedHashSet<>();

while (numberList.size() < BALL_LENGTH) {
numberList.add(r.nextInt(9) + 1);
Expand All @@ -19,19 +24,19 @@ private String randomize() {
return numberList.toString().replaceAll("[^0-9]", "");
}

private boolean checkStrike(final char randomNumberAt, final char inputNumberAt) {
private boolean checkStrike(char randomNumberAt, char inputNumberAt) {
return randomNumberAt == inputNumberAt;
}

private boolean checkBall(final String inputNumber, final char randomNumberAt) {
private boolean checkBall(String inputNumber, char randomNumberAt) {
return inputNumber.contains(String.valueOf(randomNumberAt));
}

private boolean checkThreeStrike(final int strike) {
private boolean checkThreeStrike(int strike) {
return strike == BALL_LENGTH;
}

private void printBaseballResult(final int strike, final int ball) {
private void printBaseballResult(int strike, int ball) {
if (strike == 0 && ball == 0) {
System.out.println("낫싱");
} else if (strike == 0 && ball > 0) {
Expand All @@ -43,13 +48,13 @@ private void printBaseballResult(final int strike, final int ball) {
}
}

private boolean checkBaseball(final String inputNumber, final String randomNumber) {
private boolean checkBaseball(String inputNumber, String randomNumber) {
int strike = 0;
int ball = 0;

for (int i = 0; i < randomNumber.length(); i++) {
final char randomNumberAt = randomNumber.charAt(i);
final char inputNumberAt = inputNumber.charAt(i);
char randomNumberAt = randomNumber.charAt(i);
char inputNumberAt = inputNumber.charAt(i);

if (checkStrike(randomNumberAt, inputNumberAt)) {
strike++;
Expand All @@ -62,54 +67,53 @@ private boolean checkBaseball(final String inputNumber, final String randomNumbe
return checkThreeStrike(strike);
}

private String inputStringNumber(final Scanner scanner) {
private String inputStringNumber() {
System.out.print("숫자를 입력해주세요 : ");
return scanner.nextLine();
}

private void validateLength(final String inputNumber) {
private void validateLength(String inputNumber) {
if (inputNumber.length() != BALL_LENGTH) {
throw new IllegalStateException("입력 값의 길이가 3이 아닙니다.");
}
}

private void validateNumberRange(final String inputNumber) {
private void validateNumberRange(String inputNumber) {
if (!inputNumber.matches("^[0-9]*$")) {
throw new IllegalStateException("입력 값이 숫자가 아닙니다.");
}
}

private void validateInputNumber(final String inputNumber) {
private void validateInputNumber(String inputNumber) {
validateLength(inputNumber);
validateNumberRange(inputNumber);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오류들도 메서드로 각 각 분리한 점 좋습니다👍

}

private void checkRestart(final Scanner scanner) {
private void checkRestart() {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
final int number = scanner.nextInt();
int number = Integer.parseInt(scanner.nextLine());
Copy link
Contributor

@shkisme shkisme Nov 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

찾기 어려운 오류였네요... nextInt를 사용하면 개행 처리를 하지 않는다... 저도 몰랐던 부분이였는데 주의하면서 사용해야겠습니다!👍


if (number == GAME_RESTART) {
run();
} else if (number == GAME_END) {
scanner.close();
System.exit(0);
} else {
throw new IllegalStateException("입력 값이 1 또는 2가 아닙니다.");
}
}

private void start(final String randomNumber) {
final Scanner scanner = new Scanner(System.in);
private void start(String randomNumber) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바의 경우, 메소드가 순서에 영향을 받지 않습니다. 자바 컴파일러가 자동으로 모든 메소드를 앞쪽에 선언하기 때문입니다! 예를 들어 start를 호출하고있는 run함수 위에 start 함수를 배치할 필요가 없습니다.
또한 클린 코드 책 5장을 보면, 사용하는 함수를 바로 밑에 선언하는 것을 추천하고 있습니다!
참고하시면 좋을 것 같아요😊

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다 :)

while (true) {
final String inputNumber = inputStringNumber(scanner);
String inputNumber = inputStringNumber();
validateInputNumber(inputNumber);

if (checkBaseball(inputNumber, randomNumber)) {
break;
}
}
checkRestart(scanner);
scanner.close();
checkRestart();
}

public void run() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 메서드는 main에서 사용할 때 Baseball.run() 이렇게 사용되기 때문에 네이밍을 baseBallRun이 아니라 run으로 하셨나요?

Copy link
Author

@hyuunnn hyuunnn Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다. Baseball 객체를 생성해서 시작하기 때문에 run이 더 간결하고 직관적이라고 생각했습니다.
파이썬으로 개발할 때도 run 메서드를 따로 만드는 편인데, 좋은 방법인지는 모르겠습니다 ㅎㅎ..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금 생각해보면 startBaseballstart가 더 간결하지 않나 생각이 드네요

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseBall 클래스 안의 메서드들이므로, runstart로 네이밍 해도 좋은 것 같네요!

Expand Down