-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
3단계 - 레이싱게임 #5739
base: jongminch0i
Are you sure you want to change the base?
3단계 - 레이싱게임 #5739
Changes from all commits
5933407
7ddf5fb
9cf1712
67ee6fb
ee37ab5
3461777
41abb21
51a2284
8c89ec7
d610ab1
374c343
dfe8dda
94ee89a
f093e98
68fbe54
215a2f4
5ec6324
a9d166e
2f33e8d
c61d298
3d07667
7a60ee1
c5a72f4
d662b38
d3d6551
514ced0
c93ef74
d6b3471
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,17 @@ | ||
package study.step_2.constant.error; | ||
|
||
public enum ErrorMessage { | ||
INVALID_NEGATIVE_NUMBER("음수 값은 허용되지 않습니다."), | ||
INVALID_NUMBER_TYPE("숫자 이외의 값은 허용되지 않습니다."), | ||
INVALID_DELIMITER_TYPE("구분자는 숫자를 사용할 수 없습니다."); | ||
|
||
private final String message; | ||
|
||
ErrorMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package study.step_2.domain; | ||
|
||
public interface Calculate { | ||
int splitAndSum(String express); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package study.step_2.domain; | ||
|
||
import static study.step_2.constant.error.ErrorMessage.*; | ||
import static study.step_2.domain.DelimConstant.*; | ||
|
||
public class Calculator implements Calculate { | ||
|
||
public static final int INVALID_INPUT_VALUE = 0; | ||
public static final String USING_PREFIX = "//"; | ||
public Character customDelimiter; | ||
|
||
public String expression; | ||
|
||
public Calculator(String expression) { | ||
if (BlankAndNull(expression)) return; | ||
extractCustomDelim(expression); | ||
validExpression(expression); | ||
this.expression = expression; | ||
} | ||
|
||
private boolean BlankAndNull(String expression) { | ||
if (checkBlankAndNull(expression)) { | ||
this.expression = "0"; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private boolean checkBlankAndNull(String expression) { | ||
return expression == null || expression.trim().isEmpty(); | ||
} | ||
|
||
private void extractCustomDelim(String expression) { | ||
if (expression.startsWith("//")) { | ||
customDelimiter = expression.substring(2, 3).charAt(0); | ||
validCustomDelimiterDigit(customDelimiter); | ||
} | ||
} | ||
|
||
private void validCustomDelimiterDigit(char c) { | ||
if ((Character.isDigit(c))) { | ||
throw new RuntimeException(INVALID_DELIMITER_TYPE.getMessage()); | ||
} | ||
} | ||
|
||
private void validExpression(String expression) { | ||
validNegativeAndDelimiter(expression); | ||
} | ||
|
||
private void validNegativeAndDelimiter(String expression) { | ||
|
||
if (startWithCustomDelimiter(expression)) { | ||
validateInvalidCharacterWithCustomDelimiter(expression); | ||
} else { | ||
validateInvalidCharacter(expression); | ||
} | ||
} | ||
|
||
private void validateInvalidCharacter(String expression) { | ||
for (Character c : expression.toCharArray()) { | ||
validateNegativeNumber(c); | ||
validateInvalidCharacter(c); | ||
} | ||
} | ||
|
||
private void validateNegativeNumber(Character c) { | ||
if (c.equals('-') && customDelimiter != '-') { | ||
throw new RuntimeException(INVALID_NEGATIVE_NUMBER.getMessage()); | ||
} | ||
} | ||
|
||
public boolean startWithCustomDelimiter(String expression) { | ||
return expression.startsWith(USING_PREFIX); | ||
} | ||
|
||
private void validateInvalidCharacter(Character c) { | ||
if (c != DELIMITER_COMMA.getValue() && c != DELIMITER_COLON.getValue() | ||
&& !Character.isDigit(c)) { | ||
throw new RuntimeException(INVALID_NUMBER_TYPE.getMessage()); | ||
} | ||
} | ||
|
||
private void validateInvalidCharacterWithCustomDelimiter(String expression) { | ||
for(int i = 4; i < expression.length(); i++) { | ||
char c = expression.charAt(i); | ||
validateNumberType(c); | ||
validateNegative(c); | ||
} | ||
} | ||
|
||
private void validateNegative(char c) { | ||
if (c == ('-') && customDelimiter != '-') { | ||
throw new RuntimeException(INVALID_NEGATIVE_NUMBER.getMessage()); | ||
} | ||
} | ||
|
||
private void validateNumberType(char c) { | ||
if (c != customDelimiter && !Character.isDigit(c) && c != '\n') { | ||
throw new RuntimeException(INVALID_NUMBER_TYPE.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public int splitAndSum(String express) { | ||
int sumValue = 0; | ||
|
||
if (expression.equals("0")) { | ||
return 0; | ||
} | ||
|
||
if (customDelimiterUsing(customDelimiter)) { | ||
return executeWithCustomDelimiter(express); | ||
} | ||
|
||
int i = executeWithDefaultDelimiter(express); | ||
return (i >= 0) ? i : sumValue; | ||
} | ||
|
||
private int executeWithDefaultDelimiter(String express) { | ||
int sumValue = 0; | ||
|
||
for (int i = 0; i < express.length(); i++) { | ||
sumValue += getValue(express, i); | ||
} | ||
return sumValue; | ||
} | ||
|
||
private static int getValue(String express, int i) { | ||
if (express.charAt(i) != DELIMITER_COMMA.getValue() && express.charAt(i) != DELIMITER_COLON.getValue()) { | ||
return Integer.parseInt(String.valueOf(express.charAt(i))); | ||
} | ||
return 0; | ||
} | ||
|
||
private boolean customDelimiterUsing(Character customDelimiter) { | ||
return customDelimiter != null; | ||
} | ||
|
||
private int executeWithCustomDelimiter(String express) { | ||
int sumValue = 0; | ||
|
||
for (int i = 4; i < express.length(); i++) { | ||
sumValue = getSumValue(express, i, sumValue); | ||
} | ||
return sumValue; | ||
} | ||
|
||
private int getSumValue(String express, int i, int sumValue) { | ||
if (express.charAt(i) != DELIMITER_COMMA.getValue() && express.charAt(i) != DELIMITER_COLON.getValue() && express.charAt(i) != customDelimiter) { | ||
sumValue += Integer.parseInt(String.valueOf(express.charAt(i))); | ||
} | ||
return sumValue; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package study.step_2.domain; | ||
|
||
public enum DelimConstant { | ||
|
||
DELIMITER_COMMA(','), | ||
DELIMITER_COLON(':'); | ||
|
||
private final char value; | ||
|
||
DelimConstant(char value) { | ||
this.value = value; | ||
} | ||
|
||
public char getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package study.step_2.service; | ||
|
||
import study.step_2.domain.Calculate; | ||
|
||
public class AddCalculator { | ||
|
||
private final Calculate calculate; | ||
|
||
public AddCalculator(Calculate calculate) { | ||
this.calculate = calculate; | ||
} | ||
|
||
public int executeAdd(String express) { | ||
return calculate.splitAndSum(express); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 🚀 3단계 - 자동차 경주 | ||
- 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다. | ||
- 사용자는 몇 대의 자동차로 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다. | ||
- 전진하는 조건은 0에서 9 사이에서 random 값을 구한 후 random 값이 4이상일 경우이다. | ||
- 자동차의 상태를 화면에 출력한다. 어느 시점에 출력할 것인지에 대한 제약은 없다. | ||
|
||
### play | ||
|
||
--- | ||
|
||
- 전진하는 조건 | ||
- [ ] 주어진 random 값이 조건 값 이상인지 판별하고 결과에 따라서 이상일 경우 True, 이하일 경우 False 로 반환하는 기능 | ||
|
||
### input | ||
|
||
--- | ||
|
||
- 게임 플레이를 위한 자동차 대수와 횟수를 입력 | ||
- [ ] 자동차 대수를 정수로 입력받는 기능 | ||
- [ ] 자동차 횟수를 정수로 입력받는 기능 | ||
|
||
### output | ||
|
||
--- | ||
|
||
- 게임에 대한 안내 문구를 출력 | ||
- [ ] 자동차 대수가 몇 대 인지 안내문구를 출력하는 기능 | ||
- [ ] 시도할 횟수가 몇 회 인지를 안내문구를 출력하는 기능 | ||
- [ ] 실행결과 안내 문구를 출력하는 기능 | ||
- 각 시도 횟수 당 이동한 자동차에 대해서 출력 | ||
- [ ] 이동한 자동차에 대해서는 ‘-’ 를 출력하는 기능 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package study.step_3; | ||
|
||
import study.step_3.service.RacingCar; | ||
import study.step_3.ui.UiController; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RacingCarGame { | ||
|
||
private final UiController uiController; | ||
|
||
public RacingCarGame(UiController uiController) { | ||
this.uiController = uiController; | ||
} | ||
|
||
public List<RacingCar> setUpRacingCar(int numberOfCars) { | ||
ArrayList<RacingCar> garage = new ArrayList<>(); | ||
for (int i = 0; i < numberOfCars; i++) { | ||
garage.add(new RacingCar()); | ||
} | ||
return garage; | ||
} | ||
|
||
public void gamePlay() { | ||
int numberOfCars = uiController.welcomeMessage(); | ||
int attempt = uiController.askAttemptMessage(); | ||
List<RacingCar> garage = setUpRacingCar(numberOfCars); | ||
startGame(attempt, numberOfCars, garage); | ||
} | ||
|
||
private void startGame(int attempt, int numberOfCars, List<RacingCar> garage) { | ||
uiController.endGameMessage(); | ||
|
||
for (int i = 0; i < attempt; i++) { | ||
startRacing(numberOfCars, garage); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
private void startRacing(int numberOfCars, List<RacingCar> garage) { | ||
for (int j = 0; j < numberOfCars; j++) { | ||
racingCarMoving(garage, j); | ||
} | ||
} | ||
|
||
private void racingCarMoving(List<RacingCar> garage, int numberOfCar) { | ||
int distance = garage.get(numberOfCar).drive(10, 4); | ||
|
||
if ((distance > 0)) { | ||
uiController.SkidMark(distance); | ||
} | ||
|
||
uiController.cantDrive(distance); | ||
} | ||
|
||
public static void main(String[] args) { | ||
RacingCarGame game = new RacingCarGame(new UiController()); | ||
game.gamePlay(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,28 @@ | ||||||
package study.step_3.service; | ||||||
|
||||||
import java.util.Random; | ||||||
|
||||||
public class RacingCar { | ||||||
|
||||||
private int distance = 0; | ||||||
Random random = new Random(); | ||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
|
||||||
public int drive(int bound, int condition) { | ||||||
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. bound는 무작위의 최대값을 정하기 위해 받는 매개변수입니다. 힌트를 몇 가지 드려보겠습니다.
|
||||||
if (canMove(bound, condition)) { | ||||||
Move(); | ||||||
} | ||||||
return distance; | ||||||
} | ||||||
|
||||||
public int getRandomValue(int bound) { | ||||||
return random.nextInt(bound); | ||||||
} | ||||||
|
||||||
private boolean canMove(int bound, int condition) { | ||||||
return getRandomValue(bound) >= condition; | ||||||
} | ||||||
|
||||||
private void Move() { | ||||||
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.
Suggested change
소문자로 바꿔주세요 |
||||||
distance += 1; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package study.step_3.ui; | ||
|
||
public enum InfoMessage { | ||
CAR_COUNT_QUESTION("자동차 대수는 몇 대 인가요?"), | ||
ATTEMPT_COUNT_QUESTION("시도할 회수는 몇 회 인가요?"), | ||
EXECUTION_RESULT("실행 결과"), | ||
WHEEL_LOSS("바퀴가 빠졌습니다..."), | ||
SKID_MARK("-"); | ||
|
||
private final String message; | ||
|
||
InfoMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
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.
미리 메모해둔 기능을 모두 완성하셨다면 체크도 해주세요.