-
Notifications
You must be signed in to change notification settings - Fork 3
자바 야구 게임 완성 #5
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: chlwlgus4
Are you sure you want to change the base?
자바 야구 게임 완성 #5
Changes from all commits
4c43cc6
34d48fa
6f51f29
5156650
5d6a3d2
05c2852
f799fa8
bdecc52
7e869a0
a4ed1ae
70ebcb7
4f46fe8
a536359
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,14 @@ | ||
| # 기능 목록 | ||
| <ol>게임 시작 | ||
| <ol>컴퓨터 볼 생성 | ||
| <ul>볼 중복 검사</ul> | ||
| </ol> | ||
| <ol>유저, 컴퓨터 볼 비교 | ||
| <ul>결과 저장</ul> | ||
| </ol> | ||
| <ol>조건에 맞으면 종료 | ||
| <ul>재시작 or 종료</ul> | ||
| </ol> | ||
| </ol> | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.baseball; | ||
|
|
||
| import com.baseball.domain.BaseBallPlayService; | ||
|
|
||
| public class BaseBallMainApplication { | ||
| public static void main(String[] args) { | ||
| BaseBallPlayService baseBallPlayService = new BaseBallPlayService(); | ||
| baseBallPlayService.playGame(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.baseball.domain; | ||
|
|
||
| public class Ball { | ||
| String computerBall; | ||
| String userBall; | ||
| String findBallBuffer; | ||
| int strikeCount = 0; | ||
| int ballCount = 0; | ||
|
|
||
| StringBuffer pitchZone = new StringBuffer(); | ||
| StringBuilder stringBuilder = new StringBuilder(); | ||
|
|
||
| public String createComputeBall() { | ||
| do { | ||
| int createNumber = (int) (Math.random() * 9) + 1; | ||
| if (createNumber > 0) { | ||
| stringBuilder.append(createNumber); | ||
| this.computerBallInspection(createNumber); | ||
| } | ||
| } while (stringBuilder.length() != 3); | ||
| computerBall = stringBuilder.toString(); | ||
| stringBuilder.delete(0, stringBuilder.length()); | ||
| findBallBuffer = computerBall; | ||
| return computerBall; | ||
| } | ||
|
|
||
| public void computerBallInspection(int number) { | ||
| if (stringBuilder.length() >= 2) { | ||
| int index = stringBuilder.substring(0, stringBuilder.length()-1).indexOf(String.valueOf(number)); | ||
| if (index > -1) { | ||
| stringBuilder.delete(stringBuilder.length()-1, stringBuilder.length()); | ||
| createComputeBall(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package com.baseball.domain; | ||
|
|
||
| import com.baseball.view.InputView; | ||
| import com.baseball.view.OutputView; | ||
|
|
||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class BaseBallPlayService implements BaseballPlayImpl { | ||
| private static final OutputView outputView = new OutputView(); | ||
| private static final Ball ball = new Ball(); | ||
| private static String computerBall = ball.createComputeBall(); | ||
|
|
||
| @Override | ||
| public void playGame() { | ||
| outputView.messagePrint("START", ""); | ||
| ball.userBall = InputView.inputBalls(); | ||
| this.userBallNumberVerification(ball.userBall); | ||
| this.messageDelete(); | ||
| this.baseBallMatching(ball.userBall); | ||
| if (ball.strikeCount < 3) outputView.messagePrint("GAME_RESULT", ball.pitchZone.toString()); | ||
| else outputView.messagePrint("GAME_RESULT", "3 스트라이크"); | ||
| this.gameRestartOrGameEnd(); | ||
| } | ||
|
|
||
| public void userBallNumberVerification(String userBall) { | ||
| try { | ||
| Pattern pattern = Pattern.compile("(^[0-9]{3})"); | ||
| Matcher matchingResult = pattern.matcher(String.valueOf(userBall)); | ||
| if (!matchingResult.find()) throw new IllegalArgumentException(); | ||
| } catch (IllegalArgumentException e) { | ||
| outputView.messagePrint("VERIFICATION", ""); | ||
| InputView.getClose(); | ||
| e.printStackTrace(); | ||
| throw e; | ||
|
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. catch 문에서 다시 에러를 throw 한다는 건 userBallNumberVerification 메소드를 호출한 곳에서 이 에러를 처리하겠다는 것 같은데(확실하진 않아요.. ㅠ) 딱히 보이지가 않아서요. 혹시 의도하신 건지 궁금합니다!
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. 원하던 것은 try/catch로 처리하려 했는데 좀 더 고민을 못하는 바람에.. |
||
| } | ||
| } | ||
|
|
||
| public void baseBallMatching(String userBall) { | ||
| System.out.println(computerBall); | ||
| if (!userBall.trim().isEmpty()) { | ||
| if (String.valueOf(userBall.charAt(0)).equals(String.valueOf(computerBall.charAt(0)))) { | ||
| ball.strikeCount++; | ||
| } | ||
| if (ball.findBallBuffer.indexOf(String.valueOf(userBall.charAt(0))) > 0) { | ||
| ball.ballCount++; | ||
| } | ||
| userBall = userBall.substring(1); | ||
| computerBall = computerBall.substring(1); | ||
| baseBallMatching(userBall); | ||
| } else { | ||
| if (ball.strikeCount > 0) ball.pitchZone.append(ball.strikeCount).append(" 스트라이크 "); | ||
| if (ball.ballCount > 0) ball.pitchZone.append(ball.ballCount).append(" 볼"); | ||
| } | ||
| } | ||
|
|
||
| public void gameRestartOrGameEnd() { | ||
| int coin = 0; | ||
| if (ball.strikeCount < 3) { | ||
| computerBall = String.valueOf(ball.computerBall); | ||
| playGame(); | ||
| } else { | ||
| outputView.messagePrint("GAME_COMPLETE", ball.pitchZone.toString()); | ||
| coin = InputView.inputCoin(); | ||
| } | ||
| this.messageDelete(); | ||
| if (coin == 1) this.gameReset(); | ||
| else InputView.getClose(); | ||
| } | ||
|
|
||
| public void gameReset() { | ||
| computerBall = ball.createComputeBall(); | ||
| ball.strikeCount = 0; | ||
| ball.ballCount = 0; | ||
| this.playGame(); | ||
| } | ||
|
|
||
| public void messageDelete() { | ||
| ball.strikeCount = 0; | ||
| ball.ballCount = 0; | ||
| ball.pitchZone.delete(0, ball.pitchZone.length()); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.baseball.domain; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public interface BaseballPlayImpl { | ||
| void playGame() throws IOException; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.baseball.mssage; | ||
|
|
||
| public enum Message { | ||
| START_MESSAGE("숫자를 입력해주세요 : "), | ||
| VERIFICATION_MESSAGE("볼 입력이 잘못되었습니다."), | ||
| GAME_RESULT_MESSAGE("낫 싱"), | ||
| GAME_COMPLETE_MESSAGE("3개의 숫자를 모두 맞히셨습니다! 게임 종료 \n게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
|
|
||
| private final String message; | ||
|
|
||
| Message(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.baseball.view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private static Scanner scanner; | ||
| public static String inputBalls() { | ||
| scanner = new Scanner(System.in); | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
| public static int inputCoin() { | ||
| scanner = new Scanner(System.in); | ||
| return scanner.nextInt(); | ||
| } | ||
|
|
||
| public static void getClose() { | ||
| scanner.close(); | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.baseball.view; | ||
|
|
||
| import com.baseball.mssage.Message; | ||
|
|
||
| public class OutputView { | ||
|
|
||
| public void messagePrint(String message, String resultMessage) { | ||
| switch (message) { | ||
| case "START": | ||
| System.out.println(Message.START_MESSAGE.getMessage()); | ||
| break; | ||
| case "VERIFICATION": | ||
| System.out.println(Message.VERIFICATION_MESSAGE.getMessage()); | ||
| break; | ||
| case "GAME_RESULT": | ||
| if (!resultMessage.isEmpty()) System.out.println(resultMessage); | ||
| else System.out.println(Message.GAME_RESULT_MESSAGE.getMessage()); | ||
| break; | ||
| case "GAME_COMPLETE": | ||
| System.out.println(Message.GAME_COMPLETE_MESSAGE.getMessage()); | ||
| break; | ||
| } | ||
| } | ||
| } |
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.
setter 메소드로 값을 할당하지 않고 프로퍼티에 직접적으로 접근해서 할당한 이유가 궁금합니다
프로퍼티에 private 접근지시자를 쓰지않고 외부에서 값을 조작하게 된다면 보안에 취약하다고 알고 있어서요!
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.
음... 그냥 게터세터를 안쓰고 만들어 보고 싶어서 하긴했는데
조금 급하게 만드느라 미흡하게 만든것 같아요 ㅠㅠ