-
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
[201800288 채다영] 1주차 미션을 제출합니다. #4
base: main
Are you sure you want to change the base?
Changes from 18 commits
b73070a
890d3f5
b5bd8c3
b7abdc8
122f690
4d38f19
ba3256d
27573ff
524baca
49a2dd5
df760c9
99e27a4
eb1681e
26f07f3
7a4ca73
02eb68e
46e6c69
91d1ccb
8bd6a98
9b9c939
a60ef3f
4601e7e
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## 소감 | ||
기능목록을 작성하고 구현을 해보는 것을 처음해보았는데 생각보다 쉽지 않았습니다. | ||
기본적인 개념이 헷갈라는 것이 있었고 생각한 것을 코드로 작성하는 것이 어려웠습니다. | ||
제대로 작동이 되지 않고 고쳐야 할 부분이 있다는 것을 알지만 어떻게 수정해야하는지 잘 모르겠습니다. | ||
더 고민해 본 후 추가제출 기한에는 제대로 작동되도록 제출하도록 하겠습니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,89 @@ | ||
package baseball; | ||
import mallang.missionutils.Randoms; | ||
import mallang.missionutils.Console; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
//TODO: 숫자 야구 게임 구현 | ||
startGame(); | ||
} | ||
// 게임 시작 | ||
public static void startGame() { | ||
int[] answer = answerNum(); | ||
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를 사용해볼까요? |
||
|
||
while(true) { | ||
int[] guess = userNum(); | ||
int[] result = checkNum(answer, guess); | ||
printResult(result); | ||
|
||
if (result[2] == 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. result[2] == 3이 정확히 뭘 의미하는지 알기 어려워요! |
||
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료 "); | ||
break; | ||
} | ||
if(!continueGame()) { | ||
break; | ||
} | ||
} | ||
} | ||
// 1부터 9까지의 서로 다른 임의의 수를 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. 주석을 사용하면 주석 위에도 공백 한 칸 부탁드려요! |
||
public static int[] answerNum() { | ||
int[] answer = new int[3]; | ||
for(int i = 0; i < answer.length; i++) { | ||
answer[i] = Randoms.pickNumberInRange(1,9); | ||
} | ||
return answer; | ||
} | ||
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. 행위를 보니 answerNumber를 생성하는 코드 같아요~ 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. 감사합니다! |
||
// 사용자로부터 숫자 입력받아 배열에 저장 | ||
// 사용자가 잘못된 값 입력할 경우 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. 만들어야 함 -> 즉 TODO죠? TODO 는 주석을 다음과 같이 작성해주는게 일반적입니다~ // TODO 사용자가 잘못된 값 입력할 경우 IllegalArgumentException 발생 |
||
public static int[] userNum() { | ||
System.out.println("숫자를 입력해주세요 : "); | ||
String input = Console.readLine(); | ||
int[] guess = parseInput(input); | ||
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. guess 변수명 너무 좋은 것 같아요 💯 추가로 왜 배열을 List로 바꾸라 할까요? 배열은 어떤 단점이 있을까요? |
||
return guess; | ||
} | ||
// 입력받은 String값 Int로 파싱해주기 | ||
public static int[] parseInput(String input) { | ||
int[] guess = new int[3]; | ||
for(int i = 0; i < guess.length; i++) { | ||
guess[i] = Character.getNumericValue(input.charAt(i)); | ||
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. Integer.parseInt도 있습니다~ |
||
} | ||
return guess; | ||
} | ||
// 정답 확인 함수 | ||
public static int[] checkNum(int[] answer, int[] guess) { | ||
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. checkNum 대신 정답 확인 함수라는 것을 명확히 드러내는 이름을 사용해볼까요? |
||
int[] result = new int[3]; | ||
countBalls(answer, guess, result); | ||
countStrikes(answer, guess, result); | ||
result[2] = 3 - result[0] - result[1]; // 낫싱 개수 | ||
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. 주석이 없으면 이해하기 힘들겠죠? 주석을 지우고, 주석 없이도 이해하기 쉽게 코드를 바꿔볼 수 있을까요? |
||
return result; | ||
} | ||
// 볼 개수 | ||
public static void countBalls(int[] answer, int[] guess, int[] result) { | ||
for(int i = 0; i < answer.length; i++) { | ||
for(int j = 0; j < guess.length; j++) { | ||
if(answer[i] == guess[j] && i != j) { | ||
result[0]++; | ||
} | ||
} | ||
} | ||
} | ||
// 스트라이크 개수 | ||
public static void countStrikes(int[] answer, int[] guess, int[] result) { | ||
for(int i = 0; i < answer.length; i++){ | ||
if(answer[i] == guess[i]) { | ||
result[1]++; | ||
} | ||
} | ||
} | ||
// 결과 출력 (출력 수정 필요) | ||
public static void printResult(int[] result) { | ||
System.out.println(result[0] + "볼" + " " + result[1] + "스트라이크" + result[2] + "낫싱"); | ||
} | ||
// 게임 재시작 여부 | ||
public static boolean continueGame() { | ||
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
int choice = Integer.parseInt(Console.readLine()); | ||
return choice == 1; | ||
} | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
기능 목록 잘 작성해주셨네요 💯