-
Notifications
You must be signed in to change notification settings - Fork 3
Hyunje #4
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?
Hyunje #4
Changes from all commits
68f468c
db22f39
4fd920a
4ade51f
b515286
a7bad90
54f0d21
0dd8c67
9b48255
c33df81
b95ad3d
4891135
f965b4c
d557496
3e69c01
33e7d9d
04ccc5d
d5751a7
4c2b2dd
112c522
a7b339f
77d0740
fda9895
b6682d0
c14b790
6cce9fc
a492d6b
fc1b2f7
7c83502
edd9b2e
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 +1,37 @@ | ||
| # baseball | ||
| # κΈ°λ₯ λͺ©λ‘ | ||
|
|
||
| ## λͺ©νκ° μμ± μ°μ° | ||
|
|
||
| - `Random` ν΄λμ€λ₯Ό μ΄μ©ν μμμ μ 3κ° μμ± (1~9) | ||
| - μΆν μ°μ°μ μν΄ intν λ°°μ΄λ‘ μμ± | ||
|
|
||
| ## μΌκ΅¬ κ²μ μμ μλ΄ λ¬Έκ΅¬ μΆλ ₯ | ||
|
|
||
| ## μ«μ μ λ ₯ μμ² | ||
|
|
||
| ## μ λ ₯κ°μ λν κ²μ¬ | ||
|
|
||
| - μλͺ»λ κ°μ λν μμΈ μ²λ¦¬ `IllegalArgumentException` νΈμΆ ν μ’ λ£ | ||
|
|
||
| - 곡백 κ²μ¬ -> μ²μ μ λ ₯κ° μ μ₯ μ λͺ¨λ 곡백 μ κ±° // 4 5 6 κ°λ₯ // 456 κ°λ₯ // 4 56 κ°λ₯ | ||
| - νμ κ²μ¬ -> μμΈ | ||
| - μ¬μ΄μ¦ κ²μ¬ -> μμΈ | ||
| - 0 κ²μ¬ (λ²μ κ²μ¬) -> μμΈ | ||
|
|
||
| ## μ λ ₯κ°κ³Ό λͺ©νκ° λΉκ΅ μ°μ° | ||
|
|
||
| - μ λ ₯κ³Ό λͺ©ν κ° κ° κ³Ό μμΉκ° κ°λ€λ©΄ μ€νΈλΌμ΄ν¬ | ||
| - κ°λ§ κ°λ€λ©΄ λ³Ό | ||
| - λ λ€ λ€λ₯΄λ€λ©΄ λ―Έμ€ | ||
|
|
||
| ## μ°μ° κ²°κ³Ό(ννΈ) μΆλ ₯ | ||
|
|
||
| ## μ€ν¨ μ `μ«μ μ λ ₯ μμ²`λΆν° λ°λ³΅ | ||
|
|
||
| ## μ±κ³΅ μ μ λ ₯ μμ² | ||
|
|
||
| ## μ±κ³΅ μ μ λ ₯κ°μ λ°λ₯Έ λ°λ³΅ | ||
|
|
||
| - 1μ κ²½μ° νλ‘κ·Έλ¨ μ²μλΆν° λ°λ³΅ | ||
| - 2μ κ²½μ° νλ‘κ·Έλ¨ μ’ λ£ | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package baseball; | ||
|
|
||
| import baseball.controller.BaseballGameController; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: μ½λ ꡬν | ||
| new BaseballGameController().run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package baseball.controller; | ||
|
|
||
| import baseball.model.BaseballGame; | ||
| import baseball.view.InputView; | ||
| import baseball.view.OutputView; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class BaseballGameController { | ||
| private final BaseballGame game = new BaseballGame(); | ||
| private final InputView inputView = new InputView(); | ||
| private final OutputView outputView = new OutputView(); | ||
|
|
||
| private static final int START = 1; | ||
| private static final int END = 9; | ||
|
|
||
| public void run() { | ||
| game.createTargetNumber(START, END); | ||
| outputView.printStartMessage(); | ||
|
|
||
| while (true) { | ||
| game.reset(); // targetNumberμ μν μ΄κΈ°ν λ° μ΄μ userNumber free | ||
| inputView.getUserNumber(game); | ||
| game.match(); | ||
| outputView.printResult(game); //todo: ball, strike μ°μ°μ viewν΄λμ€μμ? | ||
|
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. OutputView μμ νλ κ²μ ν° μ°μ°μ΄ μλ 쑰건 λΆκΈ°λΌμ ν¬κ² μκ΄μ μλ€κ³ μκ°ν©λλ€. |
||
|
|
||
| if (game.getStatus() == baseball.model.GameStatus.SUCCESS) { // todo: μ΄κ²λ λ°λ‘ ν¨μλ‘ λΉΌμΌλ λ― | ||
| if (outputView.isRestart(game)) continue; // todo: restart μ°μ°μ viewν΄λμ€μμ? | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package baseball.model; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class BaseballGame { | ||
| private static final int NUM_SIZE = 3; | ||
|
|
||
| BaseballNumber[] targetNumbers; | ||
| BaseballNumber[] userNumbers; | ||
|
|
||
| GameStatus status; | ||
|
|
||
| public void createTargetNumber(int start, int end) { | ||
| targetNumbers = new BaseballNumber[NUM_SIZE]; | ||
| for (int i = 0; i < NUM_SIZE; i++) { | ||
| targetNumbers[i] = new BaseballNumber(new Random().nextInt(end) + start); | ||
| } | ||
| } | ||
|
|
||
| public void createUserNumbers(String input) { | ||
| userNumbers = new BaseballNumber[NUM_SIZE]; | ||
| for (int i = 0; i < NUM_SIZE; i++) { | ||
| int inputNumber = Integer.parseInt(String.valueOf(input.charAt(i))); | ||
| userNumbers[i] = new BaseballNumber(inputNumber); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public void match() { | ||
| checkStrike(); | ||
| checkBall(); | ||
| } | ||
|
|
||
| private void checkStrike() { | ||
| for (int index = 0; index < NUM_SIZE; index++) { | ||
| if (userNumbers[index].getValue() == targetNumbers[index].getValue()) { | ||
| userNumbers[index].setStrike(true); | ||
| targetNumbers[index].setStrike(true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void checkBall() { | ||
| for (int targetIndex = 0; targetIndex < NUM_SIZE; targetIndex++) { | ||
| if (targetNumbers[targetIndex].isStrike()) continue; | ||
| for (int userIndex = 0; userIndex < NUM_SIZE; userIndex++) { | ||
| if (userNumbers[userIndex].isBall()) continue; | ||
| if (targetNumbers[targetIndex].getValue() == userNumbers[userIndex].getValue()) { | ||
| targetNumbers[userIndex].setBall(true); | ||
| userNumbers[userIndex].setBall(true); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void reset() { | ||
| for (BaseballNumber targetNumber: targetNumbers) { | ||
| targetNumber.setStrike(false); | ||
| targetNumber.setBall(false); | ||
| } | ||
|
|
||
| userNumbers = null; | ||
| } | ||
|
|
||
| public BaseballNumber[] getTargetNumbers() { | ||
| return targetNumbers; | ||
| } | ||
|
|
||
| public GameStatus getStatus() { | ||
| return status; | ||
| } | ||
|
|
||
| public void setStatus(GameStatus status) { | ||
| this.status = status; | ||
| } | ||
|
|
||
| public static int getNumSize() { | ||
| return NUM_SIZE; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package baseball.model; | ||
|
|
||
| // vo class | ||
| public class BaseballNumber { | ||
| private final int value; | ||
|
|
||
| private boolean strike = false; | ||
| private boolean ball = false; | ||
|
|
||
| public BaseballNumber(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public boolean isStrike() { | ||
| return strike; | ||
| } | ||
|
|
||
| public boolean isBall() { | ||
| return ball; | ||
| } | ||
|
|
||
| public void setStrike(boolean strike) { | ||
| this.strike = strike; | ||
| } | ||
|
|
||
| public void setBall(boolean ball) { | ||
| this.ball = ball; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package baseball.model; | ||
|
|
||
| public enum GameStatus { | ||
| FAIL, | ||
| SUCCESS, | ||
| RESTART | ||
| } | ||
|
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. κ²μ μνλ₯Ό enum ν΄λμ€λ‘ ꡬλΆν κ² μ’λ€μ~ |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package baseball.util; | ||
|
|
||
| public class Message { | ||
| public static final String MESSAGE_START = "μ«μ μΌκ΅¬ κ²μμ μμν©λλ€."; | ||
| public static final String MESSAGE_INPUT_PROMPT = "μ«μλ₯Ό μ λ ₯ν΄μ£ΌμΈμ : "; | ||
| public static final String MESSAGE_INPUT_ERROR = "1~9 μ¬μ΄ 3κ°μ μ«μλ₯Ό μ λ ₯ν΄μ£ΌμΈμ !!"; | ||
| public static final String MESSAGE_MISS = "λ―Έμ€"; | ||
| public static final String MESSAGE_STRIKE_ONLY_FORMATTED = "%dμ€νΈλΌμ΄ν¬"; | ||
| public static final String MESSAGE_BALL_ONLY_FORMATTED = "%dλ³Ό"; | ||
| public static final String MESSAGE_BALL_STRIKE_FORMATTED = "%dλ³Ό %dμ€νΈλΌμ΄ν¬"; | ||
| public static final String MESSAGE_SUCCESS_FORMATTED = "%dκ°μ μ«μλ₯Ό λͺ¨λ λ§νμ ¨μ΅λλ€!"; | ||
| public static final String MESSAGE_RESTART_PROMPT = "κ²μμ μλ‘ μμνλ €λ©΄ 1, μ’ λ£νλ €λ©΄ 2λ₯Ό μ λ ₯νμΈμ."; | ||
| public static final String MESSAGE_RESTART_ERROR = "1 λλ 2λ₯Ό μ λ ₯νμΈμ !!"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package baseball.util; | ||
|
|
||
| import baseball.model.BaseballGame; | ||
|
|
||
| public class Validator { | ||
| public static void inputValidate(String input) { | ||
| try { | ||
| Integer.parseInt(input); | ||
| if (input.contains("0") || input.length() != BaseballGame.getNumSize()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } catch (Exception e) { | ||
| System.out.println(Message.MESSAGE_INPUT_ERROR); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package baseball.view; | ||
|
|
||
| import baseball.model.BaseballGame; | ||
| import baseball.util.Message; | ||
| import baseball.util.Validator; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| public void getUserNumber(BaseballGame baseballGame) { | ||
| System.out.print(Message.MESSAGE_INPUT_PROMPT); | ||
| Scanner scanner = new Scanner(System.in); | ||
| String input = scanner.nextLine().replaceAll("\\s+", ""); | ||
|
|
||
| Validator.inputValidate(input); | ||
|
|
||
| baseballGame.createUserNumbers(input); | ||
|
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. MVC ν¨ν΄μμλ λ·°μ λͺ¨λΈμ΄ μ§μ μ μΌλ‘ μνΈμμ© νλ©΄ μλ©λλ€.
μ§κΈ μκ°λλ 건 μ΄μ λκ° μκ³ , inputμ 컨νΈλ‘€λ¬μμ λ°κ³ 컨νΈλ‘€λ¬μμ λͺ¨λΈλ‘ μ λ¬νλ λ°©ν₯μ κ³ λ €ν΄ λ³΄μΈμ! |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package baseball.view; | ||
|
|
||
| import baseball.model.BaseballGame; | ||
| import baseball.model.BaseballNumber; | ||
| import baseball.model.GameStatus; | ||
| import baseball.util.Message; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class OutputView { | ||
| public void printStartMessage() { | ||
| System.out.println(Message.MESSAGE_START); | ||
| } | ||
|
|
||
| public void printResult(BaseballGame game) { | ||
| int strike = 0; | ||
| int ball = 0; | ||
|
|
||
| for (BaseballNumber targetNumber : game.getTargetNumbers()) { | ||
| if (targetNumber.isStrike()) { | ||
| strike++; | ||
| continue; | ||
| } | ||
| if (targetNumber.isBall()) { | ||
| ball++; | ||
| } | ||
| } | ||
|
|
||
| if (ball == 0 && strike == 0) { // todo: μ°μ°μ viewν΄λμ€μμ? | ||
| game.setStatus(GameStatus.FAIL); | ||
| System.out.println(Message.MESSAGE_MISS); | ||
| } else if (ball == 0) { | ||
| game.setStatus(GameStatus.FAIL); | ||
| System.out.println(String.format(Message.MESSAGE_STRIKE_ONLY_FORMATTED, strike)); | ||
| if (strike == game.getNumSize()) { | ||
| game.setStatus(GameStatus.SUCCESS); | ||
| System.out.println(String.format(Message.MESSAGE_SUCCESS_FORMATTED, BaseballGame.getNumSize())); | ||
| } | ||
| } else if (strike == 0) { | ||
| game.setStatus(GameStatus.FAIL); | ||
| System.out.println(String.format(Message.MESSAGE_BALL_ONLY_FORMATTED, ball)); | ||
| } else { | ||
| game.setStatus(GameStatus.FAIL); | ||
| System.out.println(String.format(Message.MESSAGE_BALL_STRIKE_FORMATTED, ball, strike)); | ||
| } | ||
|
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. λ³Όκ³Ό μ€νΈλΌμ΄ν¬ λͺ¨λ 0μΈ κ²μ FAILλ‘ νλ¨ν 건 μ’λ€κ³ μκ°νλλ°, μ¬κΈ°μ μ±κ³΅κ³Ό μ€ν¨λΌλ νμ κΉ¨κ³ μ‘°κΈ λ νμ
νκΈ° μ¬μ΄ μνλ₯Ό λ§λ€μ΄λ³΄λ 건 μ΄λ¨κΉμ? |
||
| } | ||
|
|
||
| public boolean isRestart(BaseballGame game) { // todo: μΆλ ₯λΏ μλλΌ μ‘°κ±΄ λΆκΈ°λ νκ³ μμ.. | ||
| System.out.println(Message.MESSAGE_RESTART_PROMPT); | ||
| Scanner scanner = new Scanner(System.in); | ||
| try { | ||
| int option = scanner.nextInt(); | ||
| if (option == 1) { | ||
| game.createTargetNumber(1, 9); | ||
| game.setStatus(GameStatus.RESTART); // μλ―Έκ° μλ? | ||
|
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. 15λ² λΌμΈλΆν° μ¬κΈ°κΉμ§λ λ§μ°¬κ°μ§λ‘ λ·°μ λͺ¨λΈμ΄ μ§μ μ μΌλ‘ μ°κ²°λμ΄ μ μ§λ³΄μμ±μ΄ λ§μ΄ λ¨μ΄μ§κ² λ©λλ€.
|
||
| return true; | ||
| } else if (option == 2) return false; | ||
| else { | ||
| // 1,2 μΈμ μ«μ | ||
| System.out.println(Message.MESSAGE_RESTART_ERROR); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } catch (Exception e) { // μλͺ»λ νμ | ||
| System.out.println(Message.MESSAGE_RESTART_ERROR); | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
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.
컨νΈλ‘€λ¬μμ νλμ λ€λ₯Έ ν΄λμ€ κ°μ²΄λ₯Ό μμ±ν μ΄μ κ° μλμ?
νμ¬ λ°©μμ΄ μλͺ»λ λ°©μμΈ κ±΄ μλκ³ , λ³ΈμΈλ§μ μλκ° μμλ€λ©΄ μκ΄μ μμ΅λλ€!