-
Notifications
You must be signed in to change notification settings - Fork 52
gray.ts #45
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
Open
abcworld123
wants to merge
6
commits into
next-step:main
Choose a base branch
from
abcworld123:gray.ts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
gray.ts #45
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
32b1322
docs: Edit `README.md`
abcworld123 55ec088
feat: Add main, game class
abcworld123 522fdc9
feat: Add random number generator
abcworld123 e146841
feat: Add number input, judgement
abcworld123 f079e5f
feat: Add menu select
abcworld123 3544cdf
test: Add judge test
abcworld123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| public class Application { | ||
| public static void main(String[] args) { | ||
| BaseballGame game = new BaseballGame(); | ||
| game.start(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import java.util.Random; | ||
| import java.util.Scanner; | ||
|
|
||
|
|
||
| class BaseballGame { | ||
| private String answerNumber; | ||
| private final int numberLength; | ||
| private final Scanner sc; | ||
| private final Random random; | ||
| private GameStatus gameStatus; | ||
|
|
||
| BaseballGame() { | ||
| numberLength = 3; | ||
| sc = new Scanner(System.in); | ||
| random = new Random(); | ||
| gameStatus = GameStatus.IN_GAME; | ||
| } | ||
|
|
||
| void start() { | ||
| while (gameStatus == GameStatus.IN_GAME) { | ||
| setRandomNumber(); | ||
| guessNumber(); | ||
| System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); | ||
| System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
| handleMenuSelect(); | ||
| } | ||
| } | ||
|
|
||
| void setRandomNumber() { | ||
| char[] numbers = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; | ||
| char[] results = new char[numberLength]; | ||
| for (int i = 0; i < numberLength; i++) { | ||
| int j = random.nextInt(9 - i); | ||
| results[i] = numbers[j]; | ||
| swap(numbers, j, 8 - i); | ||
| } | ||
| answerNumber = new String(results); | ||
| } | ||
|
|
||
| // for test | ||
| void setSpecificNumber(String number) { | ||
| answerNumber = number; | ||
| } | ||
|
|
||
| private void guessNumber() { | ||
| Result result; | ||
| do { | ||
| System.out.print("숫자를 입력해주세요 : "); | ||
| String submitNumber = sc.next(); | ||
| result = getResult(submitNumber); | ||
| System.out.println(result.resultString()); | ||
| } while (!result.isCorrect()); | ||
| } | ||
|
|
||
| Result getResult(String submitNumber) { | ||
| return new Result(answerNumber, submitNumber); | ||
| } | ||
|
|
||
| private void handleMenuSelect() { | ||
| MenuItem menu; | ||
| do { | ||
| menu = getMenuItem(); | ||
| } while (menu == MenuItem.WRONG_INPUT); | ||
| if (menu == MenuItem.EXIT) { | ||
| gameStatus = GameStatus.TERMINATED; | ||
| } | ||
| } | ||
|
|
||
| private MenuItem getMenuItem() { | ||
| String menuNumber = sc.next(); | ||
| if (menuNumber.equals("1")) { | ||
| return MenuItem.START; | ||
| } else if (menuNumber.equals("2")) { | ||
| return MenuItem.EXIT; | ||
| } | ||
| return MenuItem.WRONG_INPUT; | ||
| } | ||
|
|
||
| private void swap(char[] arr, int i, int j) { | ||
| char temp = arr[i]; | ||
| arr[i] = arr[j]; | ||
| arr[j] = temp; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| enum GameStatus { | ||
| IN_GAME, | ||
| TERMINATED, | ||
| } | ||
|
|
||
|
|
||
| enum MenuItem { | ||
| START, | ||
| EXIT, | ||
| WRONG_INPUT, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| class Result { | ||
| int strike; | ||
| int ball; | ||
| int numberLength; | ||
|
|
||
| Result(String answerNumber, String submitNumber) { | ||
| numberLength = answerNumber.length(); | ||
| for (int i = 0; i < numberLength; i++) { | ||
| checkIndex(answerNumber, submitNumber.charAt(i), i); | ||
| } | ||
| } | ||
|
|
||
| private void checkIndex(String answerNumber, char n, int idx) { | ||
| if (answerNumber.charAt(idx) == n) { | ||
| addStrike(); | ||
| } | ||
| else if (answerNumber.indexOf(n) != -1) { | ||
| addBall(); | ||
| } | ||
| } | ||
|
|
||
| void addStrike() { | ||
| strike += 1; | ||
| } | ||
|
|
||
| void addBall() { | ||
| ball += 1; | ||
| } | ||
|
|
||
| boolean isNothing() { | ||
| return strike == 0 && ball == 0; | ||
| } | ||
|
|
||
| boolean isCorrect() { | ||
| return strike == numberLength; | ||
| } | ||
|
|
||
| String strikeString() { | ||
| if (strike == 0) { | ||
| return ""; | ||
| } | ||
| return String.format("%d 스트라이크 ", strike); | ||
| } | ||
|
|
||
| String ballString() { | ||
| if (ball == 0) { | ||
| return ""; | ||
| } | ||
| return String.format("%d볼", ball); | ||
| } | ||
|
|
||
| String resultString() { | ||
| if (isNothing()) { | ||
| return "낫싱"; | ||
| } | ||
| return strikeString() + ballString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
|
|
||
| public class JudgeTest { | ||
| BaseballGame game; | ||
|
|
||
| JudgeTest() { | ||
| game = new BaseballGame(); | ||
| game.setSpecificNumber("836"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test: 1 Strike") | ||
| void oneStrikeTest() { | ||
| Result result = game.getResult("132"); | ||
| assertThat(result.strike == 1 && result.ball == 0) | ||
| .isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test: 1 Ball") | ||
| void oneBallTest() { | ||
| Result result = game.getResult("123"); | ||
| assertThat(result.strike == 0 && result.ball == 1) | ||
| .isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test: 1 Strike 1 Ball") | ||
| void oneStrikeOneBallTest() { | ||
| Result result = game.getResult("823"); | ||
| assertThat(result.strike == 1 && result.ball == 1) | ||
| .isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test: Nothing") | ||
| void nothingTest() { | ||
| Result result = game.getResult("124"); | ||
| assertThat(result.isNothing()) | ||
| .isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test: Correct answer") | ||
| void correctTest() { | ||
| Result result = game.getResult("836"); | ||
| assertThat(result.isCorrect()) | ||
| .isTrue(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
띄어쓰기가 빠진 것 같습니다. "%d볼" -> "%d 볼" :)
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.
띄어쓰기 없는게 맞았네요!!