-
Notifications
You must be signed in to change notification settings - Fork 9
손현수) 자동차 경주 시뮬레이션 기능 추가 #1
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: Untaini
Are you sure you want to change the base?
Changes from 13 commits
62e0cd4
5e38f7e
ecddec2
c4b9717
1b780d9
32e13eb
5e45720
cd48ee3
04615d7
06b3c8b
65b263b
f788aef
681039c
be24ffb
b118782
56122be
e7f1526
0324770
1dabec0
4b6fc87
8dc465d
1fee1e4
1d07b2b
66ed54a
e76735a
dc2657f
63b1b06
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,9 @@ | ||
| <h1>구현할 기능 목록</h1> | ||
|
|
||
| - ### 사용자로부터 자동차 이름 입력받고 유효성 검사하기 | ||
| - ### 사용자로부터 시도할 횟수 입력받고 유효성 검사하기 | ||
| - ### 시도할 횟수만큼 라운드 반복하기 | ||
| - ### 모든 자동차 전진 또는 정지하기 | ||
| - ### 자동차의 상태 출력하기 | ||
| - ### 최종 우승자 찾기 | ||
| - ### 최종 우승자 출력하기 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,64 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO 구현 진행 | ||
| List<Car> carList = InputUtils.getInstance().getCarListFromInput(); | ||
| int totalRound = InputUtils.getInstance().getTotalRoundFromInput(); | ||
|
|
||
| playAllRounds(totalRound, carList); | ||
|
|
||
| printWinners(carList); | ||
| } | ||
|
|
||
| private static void playAllRounds(int totalRound, List<Car> carList) { | ||
| System.out.println("\n실행 결과"); | ||
| while (--totalRound >= 0) { | ||
| playRound(carList); | ||
| } | ||
| } | ||
|
|
||
| private static void playRound(List<Car> carList) { | ||
| moveAllCars(carList); | ||
| printAllCarsStatus(carList); | ||
| } | ||
|
|
||
| private static void moveAllCars(List<Car> carList) { | ||
| carList.forEach(Car::move); | ||
| } | ||
|
|
||
| private static void printAllCarsStatus(List<Car> carList) { | ||
| carList.forEach(System.out::println); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static void printWinners(List<Car> carList) { | ||
| System.out.printf("최종 우승자 : %s", getWinnersString(carList)); | ||
| } | ||
|
|
||
| private static String getWinnersString(List<Car> carList) { | ||
| List<String> winnerNameList = findWinners(carList).stream() | ||
| .map(Car::getName) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return String.join(", ", winnerNameList); | ||
| } | ||
|
|
||
| private static List<Car> findWinners(List<Car> carList) { | ||
| if (carList.size() == 0) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| int maxPosition = carList.stream() | ||
| .map(Car::getPosition) | ||
| .max(Integer::compareTo) | ||
| .get(); | ||
|
|
||
| return carList.stream() | ||
| .filter(car -> car.getPosition() == maxPosition) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position = 0; | ||
|
|
@@ -8,5 +15,29 @@ public Car(String name) { | |
| this.name = name; | ||
| } | ||
|
|
||
| // 추가 기능 구현 | ||
| public int getPosition() { | ||
| return this.position; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return this.name; | ||
| } | ||
|
|
||
| public String toString() { | ||
| return String.format("%s : %s",this.name, getMoveString()); | ||
| } | ||
|
|
||
| public void move() { | ||
| if (Randoms.pickNumberInRange(0, 9) >= 4) { | ||
| this.position += 1; | ||
| } | ||
| } | ||
|
|
||
| private String getMoveString() { | ||
| List<String> movePositionList = Arrays.stream(new String[this.position]) | ||
| .map(s -> "-") | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return String.join("", movePositionList); | ||
| } | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class InputUtils { | ||
| private static InputUtils inputUtils; | ||
|
|
||
| private InputUtils() {} | ||
|
|
||
| public static InputUtils getInstance() { | ||
| if (inputUtils == null) { | ||
| inputUtils = new InputUtils(); | ||
| } | ||
| return inputUtils; | ||
| } | ||
|
|
||
|
||
| public List<Car> getCarListFromInput() { | ||
| while (true) { | ||
| printCarNameInputDescription(); | ||
|
|
||
| String carNames = Console.readLine(); | ||
| try { | ||
| checkCarNameInputAvailable(carNames); | ||
|
|
||
| return convertCarNamesIntoCarList(carNames); | ||
| } catch (IllegalArgumentException exception) { | ||
| System.out.println("[ERROR] " + exception.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void printCarNameInputDescription() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| } | ||
|
|
||
| private void checkCarNameInputAvailable(String carNameInput) throws IllegalArgumentException { | ||
| if (carNameInput.endsWith(",")) { | ||
| throw new IllegalArgumentException("쉼표(,)로 끝난 올바르지 않은 입력입니다."); | ||
| } | ||
| } | ||
|
|
||
| private List<Car> convertCarNamesIntoCarList(String carNames) throws IllegalArgumentException { | ||
| return Arrays.stream(carNames.split(",")) | ||
| .map(this::createCar) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private Car createCar(String carName) throws IllegalArgumentException { | ||
| checkCarNameAvailable(carName); | ||
|
|
||
| return new Car(carName); | ||
| } | ||
|
||
|
|
||
| private void checkCarNameAvailable(String carName) throws IllegalArgumentException { | ||
| if (carName.length() == 0) { | ||
| throw new IllegalArgumentException("자동차 이름은 공백일 수 없습니다."); | ||
| } else if (carName.length() > 5) { | ||
| throw new IllegalArgumentException("자동차 이름은 최대 5자리입니다. 불가능한 이름: " + carName); | ||
| } | ||
| } | ||
|
|
||
| public int getTotalRoundFromInput() { | ||
| while (true) { | ||
| printTotalRoundInputDescription(); | ||
|
|
||
| String roundInput = Console.readLine(); | ||
| try { | ||
| int totalRound = convertRoundInputIntoRoundInteger(roundInput); | ||
|
|
||
| checkTotalRoundAvailable(totalRound); | ||
|
|
||
| return totalRound; | ||
| } catch (IllegalArgumentException exception) { | ||
| System.out.println("[ERROR] " + exception.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void printTotalRoundInputDescription() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| } | ||
|
|
||
| private Integer convertRoundInputIntoRoundInteger(String roundInput) throws IllegalArgumentException { | ||
| try { | ||
| return Integer.parseInt(roundInput); | ||
| } catch (NumberFormatException exception) { | ||
| throw new IllegalArgumentException("자연수가 아닌 값은 입력할 수 없습니다."); | ||
| } | ||
| } | ||
|
||
|
|
||
| private void checkTotalRoundAvailable(int totalRound) throws IllegalArgumentException { | ||
| if (totalRound < 1) { | ||
| throw new IllegalArgumentException("자연수가 아닌 값은 입력할 수 없습니다."); | ||
| } | ||
| } | ||
| } | ||
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.
대부분의 Application method들이 carList를 파라미터로 받는 static 함수들입니다. 게임을 나타내는 클래스를 하나 작성하시고, 파라미터가 아니라 fiel로 carList를 작성해 보시는건 어떠신가요?