-
Notifications
You must be signed in to change notification settings - Fork 11
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
[202201912 이서인] 1주차 미션을 제출합니다. #1
base: main
Are you sure you want to change the base?
Changes from 2 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,141 @@ | ||
package baseball; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
import java.io.IOException; | ||
import java.util.*; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
//TODO: 숫자 야구 게임 구현 | ||
} | ||
private static final int MIN = 1; | ||
private static final int MAX = 9; | ||
private static final int BALL_SIZE = 3; | ||
|
||
public static void main(String[] args) { | ||
while (true) { | ||
playGame(); | ||
} | ||
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. System.exit를 사용하지 않고 게임을 종료하려면 이 부분에 대한 수정이 필요할 것 같네요 :) 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. 불필요한 주석 같아요! 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. 아하 그렇군요! 꼭 필요한 부분에만 주석 작성하도록 하겠습니다 :> |
||
private static void playGame() { | ||
// 랜덤 3자리 숫자 생성 | ||
int computerBall[] = generateRandomBall(); | ||
System.out.println("랜덤 숫자 : " + Arrays.toString(computerBall)); | ||
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. 넵! 메서드로 코드 작성해보겠습니당 |
||
|
||
while (true) { | ||
// 사용자 입력 3자리 숫자 | ||
int[] userBall = getUserInput(); | ||
checkBall(userBall); | ||
|
||
// 볼, 스트라이크, 낫싱 체크 | ||
int ballScore = ballCheck(computerBall, userBall); | ||
int strikeScore = strikeCheck(computerBall, userBall); | ||
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. ballCheck, strikeCheck 만으로도 이미 무엇을 하는지 의미가 명확하다고 생각해요! 💯 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 (strikeScore != 0 && ballScore == 0) { | ||
System.out.println(strikeScore + "스트라이크"); | ||
} else if (strikeScore == 0 && ballScore != 0) { | ||
System.out.println(ballScore + "볼"); | ||
} else if (strikeScore == 3) { | ||
System.out.println(strikeScore + "스트라이크"); | ||
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); | ||
playAgain(); | ||
} else if (strikeScore == 0 && ballScore == 0) { | ||
System.out.println("낫싱"); | ||
} else { | ||
System.out.println(ballScore + "볼" + strikeScore + "스트라이크"); | ||
} | ||
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. 넵! 수정하겠습니당 |
||
} | ||
} | ||
|
||
/* 게임 반복 메소드 */ | ||
private static void playAgain() { | ||
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. 제공드린 코드 컨벤션 문서를 참고하여 메서드 순서를 올바르게 변경해주세요 :) |
||
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
String inputNum = Console.readLine(); | ||
if (inputNum.equals("1")) { | ||
playGame(); | ||
} else { | ||
System.exit(0); | ||
} | ||
} | ||
|
||
/* 랜덤 3자리 숫자 생성 메소드 */ | ||
private static int[] generateRandomBall() { | ||
Set<Integer> set = new HashSet<>(); | ||
int[] randomBall = new int[3]; | ||
int count = 0; | ||
while (count < 3) { | ||
int number = Randoms.pickNumberInRange(1, 9); | ||
if (!set.contains(number)) { // 중복이 없을 경우 | ||
randomBall[count] = number; | ||
set.add(number); | ||
count++; | ||
} | ||
} | ||
return randomBall; | ||
} | ||
|
||
/* 사용자 입력 3자리 숫자 메소드 */ | ||
private static int[] getUserInput() { | ||
System.out.print("숫자를 입력해주세요 : "); | ||
String inputNum = Console.readLine(); | ||
if (inputNum.length() != BALL_SIZE) { | ||
throw new IllegalArgumentException("3자리 숫자를 입력해주세요"); | ||
} | ||
int[] userBall = new int[BALL_SIZE]; | ||
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. 배열 대신 List를 사용해봐요! 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. 넵! 리스트로 변경하겠습니당 |
||
for (int i = 0; i < BALL_SIZE; i++) { | ||
userBall[i] = Character.getNumericValue(inputNum.charAt(i)); | ||
} | ||
return userBall; | ||
} | ||
|
||
/* 오류를 체크하는 메소드*/ | ||
private static void checkBall(int[] inputBalls) { | ||
Set<Integer> set = new HashSet<>(); | ||
for (int num : inputBalls) { | ||
if (num < MIN || num > MAX) { | ||
throw new IllegalArgumentException("1~9의 범위 내에서 입력해주세요."); | ||
} | ||
if (!set.add(num)) { | ||
throw new IllegalArgumentException("숫자를 중복해서 입력하지 마세요."); | ||
} | ||
} | ||
System.out.println("오류 없음"); | ||
} | ||
|
||
/* 볼 카운트 메소드 */ | ||
private static int ballCheck(int[] computerNum, int[] userNum) { | ||
int ball = 0; | ||
for (int num : userNum) { | ||
if (contains(computerNum, num)) { | ||
ball++; | ||
} | ||
} | ||
return ball; | ||
} | ||
|
||
/* 스트라이크 카운트 메소드 */ | ||
private static int strikeCheck(int[] computerNum, int[] userNum) { | ||
int strike = 0; | ||
for (int i = 0; i < BALL_SIZE; i++) { | ||
if (computerNum[i] == userNum[i]) { | ||
strike++; | ||
} | ||
} | ||
return strike; | ||
} | ||
|
||
/* 배열에 숫자가 포함되어 있는지 확인하는 메소드 */ | ||
private static boolean contains(int[] array, int target) { | ||
for (int num : array) { | ||
if (num == target) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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.
매직 넘버 대신 상수로 정의해서 사용해 주셨네요 💯
변수의 이름을 조금 더 명확하게 지어주는 것은 어떨까요?
MIN이 정확히 어떤 것에 대한 최소인지,
MAX가 정확히 어떤 것에 대한 최대인지 알기 어려워 보입니다!
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.
넵 수정하겠습니다!