Skip to content
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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions TODOList.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 기능 목록
1. 경주할 자동차의 이름을 입력 받는다
2. 입력 받은 자동차 이름을 검증 한 후 실패시 에러를 띄운다
3. 시도할 횟수를 입력 받는다
4. 시도할 횟수를 검증 한 후 실패시 에러를 띄운다
5. 시도할 횟수에 맞게 무작위 값을 계산한다
6. 무작위 값에 따라 자동차들을 전진 시킨다
7. 전진시킬 때마다 각 차수별 실행 결과를 출력한다
8. 전진이 끝나면 우승자를 뽑는다
9. 우승자들을 출력한다
2 changes: 2 additions & 0 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
public class Application {
public static void main(String[] args) {
// TODO 구현 진행
CarGame carGame = new CarGame();
carGame.start();
}
Comment on lines 5 to 8
Copy link
Collaborator

Choose a reason for hiding this comment

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

간결합니다 :) 너무 좋아보이네용

Copy link
Author

Choose a reason for hiding this comment

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

감사합니다 :)

}
12 changes: 12 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ public Car(String name) {
}

// 추가 기능 구현

public String getName() {
return name;
}

public int getPosition() {
return position;
}

public void go() {
this.position++;
}
}
59 changes: 59 additions & 0 deletions src/main/java/racingcar/CarGame.java
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

DI 적인 요소들이 많이 보여서 엄청 좋은것 같아요!

Copy link
Author

Choose a reason for hiding this comment

The 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;
}
}
48 changes: 48 additions & 0 deletions src/main/java/view/InputValidator.java
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
Copy link

Choose a reason for hiding this comment

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

자동차 이름 빈칸 검증 부분이 단지 length가 0인 것만 생각했는데, 이런 방법도 있네요! 좋은 방법인 것 같습니다.

Copy link
Author

Choose a reason for hiding this comment

The 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] 시도 횟수는 숫자여야 한다.");
}
}
}
58 changes: 58 additions & 0 deletions src/main/java/view/InputView.java
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());
}
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/view/OutputView.java
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));
}
}