-
Notifications
You must be signed in to change notification settings - Fork 3
숫자 입력 및 스트라이크/볼 판정 로직 구현 (Not passed test yet) #3
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
base: master
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # 숫자야구 (Number Baseball) | ||
|
|
||
| ## 프로젝트 소개 | ||
| 컴퓨터가 랜덤으로 생성한 3자리 숫자를 사용자가 입력해서 맞추는 게임입니다. | ||
| 숫자와 자리 모두 맞으면 스트라이크, 숫자만 맞으면 볼, 아무것도 맞지 않으면 미스로 판정합니다. | ||
|
|
||
| ## 기능 | ||
| - 1~9 사이의 서로 다른 숫자 3개를 랜덤으로 생성 | ||
| - 사용자가 숫자 3자리를 입력하면 결과를 출력 | ||
| - 스트라이크, 볼, 미스 판정 | ||
| - 정답을 맞추면 게임을 재시작하거나 종료할 수 있음 | ||
|
|
||
| ## 실행 방법 | ||
| 1. `Application.java` 파일을 실행합니다. | ||
| 2. 콘솔 창에 출력되는 안내에 따라 3자리 숫자를 입력하여 게임을 진행합니다. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,67 @@ | ||
| package baseball; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 코드 구현 | ||
| System.out.println("숫자 야구 게임을 시작합니다."); | ||
| Scanner sc = new Scanner(System.in); | ||
|
|
||
| while (true) { | ||
| List<Integer> computer = generateRandomDigits(); | ||
| boolean correct = false; | ||
|
|
||
| while (!correct) { | ||
| System.out.print("숫자 3자리를 입력하세요 (예: 123): "); | ||
| String input = sc.next(); | ||
| List<Integer> user = new ArrayList<>(); | ||
| for (int i = 0; i < 3; i++) { | ||
| user.add(input.charAt(i) - '0'); | ||
| } | ||
|
Member
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. 사용자의 입력에 대한 예외 처리가 안되어 있어요. 참고할만한 예외 처리로는
등을 생각할 수 있겠네요. |
||
|
|
||
| int strike = 0; | ||
| int ball = 0; | ||
|
|
||
| for (int i = 0; i < 3; i++) { | ||
| if (user.get(i).equals(computer.get(i))) { | ||
| strike++; | ||
| } else if (computer.contains(user.get(i))) { | ||
| ball++; | ||
| } | ||
| } | ||
|
|
||
| if (strike == 0 && ball == 0) { | ||
| System.out.println("미스"); | ||
| } else { | ||
| System.out.println(strike + " 스트라이크 " + ball + " 볼"); | ||
| } | ||
|
|
||
| if (strike == 3) { | ||
| System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); | ||
| System.out.print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요: "); | ||
| int choice = sc.nextInt(); | ||
| if (choice == 2) { | ||
| System.out.println("게임을 종료합니다."); | ||
|
Member
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. 전체적으로 모든 출력이 붙어있어 가독성이 좋지 않은 것 같아요. print에 |
||
| return; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
Member
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. 여기서 말하는 내용은 아직 어려울 수 있으나, 추후 팀 프로젝트를 원활하게 진행하려면 알아둘 필요가 있는 내용이니 참고하시고 공부해 보시면 좋을 것 같아요. 프로그래밍 요구사항에 위 두 문장을 보면 Application의 main() 함수는 프로그램을 실행하는 역할을 수행해요. 25번 라인부터 49번 라인은 실제 야구로 치면 심판이 하는 역할이죠? 숫자 야구로 보면 게임의 룰이 된다고 볼 수 있죠. 굳이 왜 그래야하지? 라고 생각이 들 수 있는데, 현재 과제는 쉬운 편이고 이에 대한 코드도 짧은 편이에요. 추후 팀 프로젝트를 진행할 때 모든 기능을 한 파일에 때려 박는다면 리팩토링 하기도, 유지보수 하기도, 재사용 하기도 힘들 거예요. 현재 과제가 어렵게 느껴지실 수도 있지만 팀 프로젝트에 비하면 훨씬 쉬운 편이니 한 번 생각해보시고 학습해 본다면 크게 성장할 수 있을 거예요.
Author
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. 한번 브랜치를 새로 따서 말씀주신 사항 반영해보려 했는데, 시간 괜찮으시다면 이것도 한번 확인 부탁드릴게요..!! (shinseojin.ver2)
Member
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. 이전보다 훨씬 보기 좋아졌네요. 각 함수 명에 맞는 기능만 잘 넣은 것 같아요.
Author
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 List<Integer> generateRandomDigits() { | ||
| List<Integer> digits = new ArrayList<>(); | ||
| Random random = new Random(); | ||
|
|
||
| while (digits.size() < 3) { | ||
| int num = random.nextInt(9) + 1; // 1~9 | ||
| if (!digits.contains(num)) { | ||
| digits.add(num); | ||
| } | ||
| } | ||
|
|
||
| return digits; | ||
| } | ||
| } | ||
| } | ||
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.
과제 진행 요구사항에 아래와 같은 요구 사항이 있어요.
기능을 구현하기 전README.md에 구현할 기능 목록을 정리합니다.Git의 커밋 단위는 앞 단계에서README.md에 정리한 기능 목록 단위로 추가합니다.현재 작성하신 기능 목록에 맞춰 해당 기능을 구현한 후 이를 커밋해 보면 어떨까요?
지금은 혼자 개발하셔서 크게 상관없을 수 있지만, 나중에 팀 프로젝트를 진행할 때 큰 도움이 될 거예요.
각 기능마다 구현한 후 커밋을 하면 무슨 기능이 구현되었는지 쉽게 파악할 수 있고, 어떤 코드에 에러가 있다면 그 시점으로 되돌아갈 수도 있어요.
그 외에 다양한 git 기능들이 많으니 이번에 수정할 때나 다음 과제부터 적용해보면 좋을 것 같아요.
우선은 기능마다 커밋하는 것에 익숙해지면 다음엔 좀 더 자세히 커밋하는 방법을 알려드릴게요.