Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
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>


1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = '8'

repositories {
mavenCentral()
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/baseball/BaseBallMainApplication.java
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();
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/baseball/domain/Ball.java
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();
}
}
}

}
84 changes: 84 additions & 0 deletions src/main/java/com/baseball/domain/BaseBallPlayService.java
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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setter 메소드로 값을 할당하지 않고 프로퍼티에 직접적으로 접근해서 할당한 이유가 궁금합니다
프로퍼티에 private 접근지시자를 쓰지않고 외부에서 값을 조작하게 된다면 보안에 취약하다고 알고 있어서요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음... 그냥 게터세터를 안쓰고 만들어 보고 싶어서 하긴했는데
조금 급하게 만드느라 미흡하게 만든것 같아요 ㅠㅠ

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch 문에서 다시 에러를 throw 한다는 건 userBallNumberVerification 메소드를 호출한 곳에서 이 에러를 처리하겠다는 것 같은데(확실하진 않아요.. ㅠ) 딱히 보이지가 않아서요. 혹시 의도하신 건지 궁금합니다!

Copy link
Author

Choose a reason for hiding this comment

The 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());
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/baseball/domain/BaseballPlayImpl.java
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;
}
18 changes: 18 additions & 0 deletions src/main/java/com/baseball/mssage/Message.java
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;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/baseball/view/InputView.java
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();
}


}
24 changes: 24 additions & 0 deletions src/main/java/com/baseball/view/OutputView.java
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;
}
}
}
Empty file removed src/main/java/empty.txt
Empty file.