-
Notifications
You must be signed in to change notification settings - Fork 2
Java baseball game 이현 #6
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: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "java_baseball_game_\uC774\uD604"
Changes from 1 commit
2b3edc9
7986426
6abe3be
5a04bb5
677bee7
8b97297
4e69a6a
3171420
8d56d30
83f96f9
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,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); | ||
|
|
@@ -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) { | ||
|
|
@@ -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++; | ||
|
|
@@ -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); | ||
| } | ||
|
|
||
| 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()); | ||
|
Contributor
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 (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) { | ||
|
Contributor
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. 감사합니다 :) |
||
| 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() { | ||
|
Contributor
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. 맞습니다.
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. 지금 생각해보면
Contributor
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.
오류들도 메서드로 각 각 분리한 점 좋습니다👍