-
Notifications
You must be signed in to change notification settings - Fork 10
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
[숫자 야구 게임] 정건우 과제 제출합니다. #2
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package baseball; | ||
|
||
import java.util.*; | ||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
// TODO: 프로그램 구현 | ||
GameController gameController = new GameController(); | ||
gameController.startGame(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package baseball; | ||
|
||
import java.util.*; | ||
import camp.nextstep.edu.missionutils.Console; | ||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
public class GameController { | ||
public static void startGame() { | ||
boolean again = true; | ||
System.out.println("숫자 야구 게임을 시작합니다."); | ||
|
||
while (again) { | ||
NumberGenerator numberGenerator = new NumberGenerator(); | ||
List<Integer> computerNum = numberGenerator.getRandomComputerNumber(); | ||
|
||
boolean baseballGame = true; | ||
while (baseballGame) { | ||
InputHandler inputHandler = new InputHandler(); | ||
List<Integer> userNum = inputHandler.getUserInput(); | ||
|
||
NumberChecker numberChecker = new NumberChecker(); | ||
baseballGame = numberChecker.checkNumber(computerNum, userNum); | ||
} | ||
|
||
again = askForRestart(); | ||
} | ||
} | ||
|
||
private static boolean askForRestart() { | ||
try { | ||
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
int restart = Integer.parseInt(Console.readLine()); | ||
if (restart == 1) { | ||
return true; | ||
} else if (restart == 2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if 조건절에서 값을 return하는 방식으로 구현하여 else if, else 문을 사용하지 않아도 됩니다. else, else if를 지양하는 것이 코드 가독성 면에서 뛰어납니다. |
||
return false; | ||
} else { | ||
throw new IllegalArgumentException(); | ||
} | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalArgumentException("잘못된 입력입니다. 게임을 종료합니다."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외 처리를 잘해주었습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validator 클래스를 좀 더 잘 이용해보는것도 방법입니다!! |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package baseball; | ||
|
||
import java.util.*; | ||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
public class InputHandler { | ||
public static List<Integer> getUserInput() { | ||
try { | ||
System.out.print("숫자를 입력해주세요 : "); | ||
String inputUserNum = camp.nextstep.edu.missionutils.Console.readLine(); | ||
if (inputUserNum == null || inputUserNum.length() != 3) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상수값을 그냥 입력하기보다는 numberLength 등의 변수를 사용해보는 것이 어떨까요? 숫자야구의 숫자길이가 바뀌는 변화에 대응할 수 있는 코드를 생각해볼 수 있어야합니다. |
||
throw new IllegalArgumentException(); | ||
} | ||
|
||
List<Integer> userInput = new ArrayList<>(); | ||
for (char digitChar : inputUserNum.toCharArray()) { | ||
int digit = Character.getNumericValue(digitChar); | ||
if (digit < 1 || digit > 9) { | ||
throw new IllegalArgumentException(); | ||
} | ||
userInput.add(digit); | ||
} | ||
return userInput; | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalArgumentException("잘못된 입력입니다. 다시 시도해주세요."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package baseball; | ||
|
||
import java.util.*; | ||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
public class NumberChecker { | ||
public boolean checkNumber(List<Integer> computerNum, List<Integer> userNum) { | ||
int strike = 0; | ||
int ball = 0; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
if (userNum.get(i).equals(computerNum.get(i))) { | ||
strike++; | ||
} else if (computerNum.contains(userNum.get(i))) { | ||
ball++; | ||
} | ||
} | ||
if (strike == 0 && ball == 0) { | ||
System.out.println("낫싱"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ResultPrinter 클래스를 사용하면 될것같습니다. 출력기 클래스가 있느니 관심사의 분리를 적용해보세요 |
||
} else if (strike == 3) { | ||
System.out.println(strike + "스트라이크"); | ||
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); | ||
return false; // 게임 종료 | ||
} else { | ||
if (ball > 0) { | ||
System.out.print(ball + "볼 "); | ||
} | ||
if (strike > 0) { | ||
System.out.print(strike + "스트라이크"); | ||
} | ||
System.out.println(); | ||
} | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package baseball; | ||
|
||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class NumberGenerator { | ||
public static List<Integer> getRandomComputerNumber() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 특별히 static을 사용한 이유가 있나요?? 일반적으로 static을 사용하는 경우엔 특별한 이유가 있어야 합니다 어떤 경우에 static을 사용하고 static인 경우에는 어떤 특징이 생기는 지 생각해보세요!! |
||
List<Integer> computerNum = new ArrayList<>(); | ||
while (computerNum.size() < 3) { | ||
int randomNumber = Randoms.pickNumberInRange(1, 9); | ||
if (!computerNum.contains(randomNumber)) { | ||
computerNum.add(randomNumber); | ||
} | ||
} | ||
return computerNum; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package baseball; | ||
|
||
public class ResultPrinter { | ||
public static boolean printResult(int strike, int ball) { | ||
if (strike == 0 && ball == 0) { | ||
System.out.println("낫싱"); | ||
} else if (strike == 3) { | ||
System.out.println(strike + "스트라이크"); | ||
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); | ||
return false; // 게임 종료 | ||
} else { | ||
if (ball > 0) { | ||
System.out.print(ball + "볼 "); | ||
} | ||
if (strike > 0) { | ||
System.out.print(strike + "스트라이크"); | ||
} | ||
System.out.println(); | ||
} | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package baseball; | ||
|
||
public class Validator { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외처리 하는 클래스를 잘 만들어두고 제대로 사용하지 않은것 같아 아쉽습니다. 적용해서 리팩토링 해보세요 |
||
public static void validateInput(String input) { | ||
if (input == null || input.length() != 3) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
for (char digitChar : input.toCharArray()) { | ||
int digit = Character.getNumericValue(digitChar); | ||
if (digit < 1 || digit > 9) { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 while문은 하나의 메소드를 추가하여 분리할 수 있을것 같습니다.