-
Notifications
You must be signed in to change notification settings - Fork 9
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
최성원 자동차 경주 게임 #4
base: master
Are you sure you want to change the base?
Changes from all commits
48114f3
6f64ef8
7b5be11
bba56e1
97352a9
48f3e95
5aee294
b4ca696
915c62c
91670b6
83eb481
145f84f
6e43090
61668ec
3cd358a
35bc1ff
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,10 @@ | ||
# 기능 목록 | ||
1. 경주할 자동차의 이름을 입력 받는다 | ||
2. 입력 받은 자동차 이름을 검증 한 후 실패시 에러를 띄운다 | ||
3. 시도할 횟수를 입력 받는다 | ||
4. 시도할 횟수를 검증 한 후 실패시 에러를 띄운다 | ||
5. 시도할 횟수에 맞게 무작위 값을 계산한다 | ||
6. 무작위 값에 따라 자동차들을 전진 시킨다 | ||
7. 전진시킬 때마다 각 차수별 실행 결과를 출력한다 | ||
8. 전진이 끝나면 우승자를 뽑는다 | ||
9. 우승자들을 출력한다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package racingcar; | ||
|
||
import camp.nextstep.edu.missionutils.Randoms; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CarGame { | ||
private final InputView inputView; | ||
private final OutputView outputView; | ||
|
||
public CarGame() { | ||
this.inputView = new InputView(); | ||
this.outputView = new OutputView(); | ||
} | ||
Comment on lines
+11
to
+17
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. DI 적인 요소들이 많이 보여서 엄청 좋은것 같아요! 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. spring에서 하던 느낌과 최대한 비슷하게 해보려 했습니다! |
||
|
||
public void start() { | ||
List<Car> cars = inputView.getCars(); | ||
int trial = inputView.getTrial(); | ||
startGame(cars, trial); | ||
} | ||
|
||
private void startGame(List<Car> cars, int trial) { | ||
outputView.printInit(); | ||
for (int i = 0; i < trial; i++) { | ||
for (Car car : cars) { | ||
goOrNot(car); | ||
} | ||
outputView.printResult(cars); | ||
} | ||
List<String> result = pickWinner(cars); | ||
outputView.printFinalWinner(result); | ||
} | ||
|
||
private void goOrNot(Car car) { | ||
int randomNum = Randoms.pickNumberInRange(0, 9); | ||
if (randomNum >= 4) { | ||
car.go(); | ||
} | ||
} | ||
|
||
private List<String> pickWinner(List<Car> cars) { | ||
List<String> winner = new ArrayList<>(); | ||
int maxDistance = -1; | ||
for (Car car : cars) { | ||
if (car.getPosition() == maxDistance) { | ||
winner.add(car.getName()); | ||
} | ||
if (car.getPosition() > maxDistance) { | ||
maxDistance = car.getPosition(); | ||
winner.clear(); | ||
winner.add(car.getName()); | ||
} | ||
} | ||
return winner; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package view; | ||
|
||
import racingcar.Car; | ||
|
||
import java.util.List; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
public class InputValidator { | ||
public static void validateSplit(String carString) { | ||
try { | ||
String[] split = carString.split(","); | ||
} catch (PatternSyntaxException e) { | ||
throw new IllegalArgumentException("[ERROR] 자동차 이름은 쉼표를 기준으로 구분되어야 한다."); | ||
} | ||
} | ||
|
||
public static void validateCarNames(List<Car> cars) { | ||
for (Car car : cars) { | ||
String carName = car.getName(); | ||
if (carName.length() > 5) { | ||
throw new IllegalArgumentException("[ERROR] 자동차 이름은 5자 이하만 가능하다."); | ||
} | ||
} | ||
} | ||
|
||
public static void validateEmpty(String[] carNames) { | ||
if (carNames.length == 0) { | ||
throw new IllegalArgumentException("[ERROR] 자동차는 1대 이상 입력 되어야 한다."); | ||
} | ||
for (String carName : | ||
carNames) { | ||
if (carName.trim().isEmpty()) { | ||
throw new IllegalArgumentException("[ERROR] 자동차 이름은 빈칸일 수 없다."); | ||
Comment on lines
+30
to
+33
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. 자동차 이름 빈칸 검증 부분이 단지 length가 0인 것만 생각했는데, 이런 방법도 있네요! 좋은 방법인 것 같습니다. 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 static void validateTrial(String trial) { | ||
try { | ||
int trialNum = Integer.parseInt(trial); | ||
if (trialNum < 0) { | ||
throw new IllegalArgumentException("[ERROR] 시도 횟수는 음수일 수 없다."); | ||
} | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("[ERROR] 시도 횟수는 숫자여야 한다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package view; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
import racingcar.Car; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class InputView { | ||
|
||
public List<Car> getCars() { | ||
while (true) { | ||
try { | ||
System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); | ||
String carString = Console.readLine(); | ||
String[] carNames = getCarNamesStringArray(carString); | ||
return getCarList(carNames); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
private String[] getCarNamesStringArray(String carString) { | ||
InputValidator.validateSplit(carString); | ||
String[] carNames = carString.split(","); | ||
InputValidator.validateEmpty(carNames); | ||
return carNames; | ||
} | ||
|
||
private List<Car> getCarList(String[] carNames) { | ||
List<Car> cars = getCarArrayList(carNames); | ||
InputValidator.validateCarNames(cars); | ||
return cars; | ||
} | ||
|
||
private List<Car> getCarArrayList(String[] carNames) { | ||
List<Car> cars = new ArrayList<>(); | ||
for (String carName : | ||
carNames) { | ||
cars.add(new Car(carName)); | ||
} | ||
return cars; | ||
} | ||
|
||
public int getTrial() { | ||
while (true) { | ||
try { | ||
System.out.println("시도할 횟수는 몇회인가요?"); | ||
String trial = Console.readLine(); | ||
InputValidator.validateTrial(trial); | ||
return Integer.parseInt(trial); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package view; | ||
|
||
import racingcar.Car; | ||
|
||
import java.util.List; | ||
|
||
public class OutputView { | ||
public void printInit() { | ||
System.out.println("실행 결과"); | ||
} | ||
|
||
public void printResult(List<Car> cars) { | ||
for (Car car : cars) { | ||
printCarStatus(car); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
private void printCarStatus(Car car) { | ||
System.out.print(car.getName() + " : "); | ||
for (int i = 0; i < car.getPosition(); i++) { | ||
System.out.print("-"); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public void printFinalWinner(List<String> winner) { | ||
System.out.println("최종 우승자 : " + String.join(", ", winner)); | ||
} | ||
} |
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.
간결합니다 :) 너무 좋아보이네용
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.
감사합니다 :)