-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 PR 머지 완료! 🎉
- Loading branch information
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
public class Car { | ||
|
||
private final String name; | ||
private int position; | ||
private static final int MOVE_TRIGGER = 4; | ||
|
||
public Car(String name) { | ||
this.name = name; | ||
this.position = 0; | ||
} | ||
|
||
public void move(int value) { | ||
if (value >= MOVE_TRIGGER) { | ||
position++; | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private static final int MAX_NAME_LENGTH = 5; | ||
private final Scanner scanner = new Scanner(System.in); | ||
|
||
private String getUserInput() { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
return scanner.nextLine(); | ||
} | ||
|
||
private String[] splitCarNames(String inputValue) { | ||
return inputValue.split(","); | ||
} | ||
|
||
public String[] inputCarNames() throws Exception { | ||
String inputValue = getUserInput(); | ||
String[] carsName = splitCarNames(inputValue); | ||
|
||
validateName(carsName); | ||
return carsName; | ||
} | ||
|
||
private void validateName(String[] carsName) throws Exception { | ||
for (String carName : carsName) { | ||
validateInput(carName); | ||
} | ||
} | ||
|
||
private void validateInput(String carName) throws Exception { | ||
if (carName.length() > MAX_NAME_LENGTH) { | ||
throw new Exception("자동차 이름은 "+MAX_NAME_LENGTH+ "자 이하여야합니다."); | ||
} | ||
} | ||
|
||
public int inputRaceRounds() { | ||
System.out.println("시도할 회수는 몇회인가요?"); | ||
int rounds = scanner.nextInt(); | ||
System.out.println(); | ||
return rounds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
class RacingGame { | ||
|
||
private List<Car> cars; | ||
|
||
public RacingGame(List<Car> cars) { | ||
this.cars = new ArrayList<>(cars); | ||
} | ||
|
||
public void gameStart(int rounds) { | ||
while (rounds-- > 0) { | ||
moveCars(); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
private void moveCars() { | ||
Random random = new Random(); | ||
for (Car car : cars) { | ||
System.out.print(car.getName() + " : "); | ||
int randomValue = random.nextInt(10); | ||
car.move(randomValue); | ||
System.out.print("-".repeat(car.getPosition())); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
public List<Car> getWinner() { | ||
int maxPosition = getMaxPosition(); | ||
return cars.stream() | ||
.filter(car -> car.getPosition() == maxPosition) | ||
.collect(Collectors.toUnmodifiableList()); | ||
} | ||
|
||
private int getMaxPosition() { | ||
return cars.stream() | ||
.mapToInt(Car::getPosition) | ||
.max() | ||
.orElse(0); | ||
} | ||
|
||
public String findWinnerName(List<Car> winnerCars) { | ||
return winnerCars.stream() | ||
.map(car -> car.getName()) | ||
.collect(Collectors.joining(", ")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RacingGameApplication { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
InputView inputView = new InputView(); | ||
|
||
// 1. 자동차 이름 입력 및 검증 | ||
String[] carsName = inputView.inputCarNames(); | ||
List<Car> cars = createCars(carsName); | ||
|
||
RacingGame racingGame = new RacingGame(cars); | ||
ResultView resultView = new ResultView(); | ||
|
||
// 2. 시도할 횟수 입력 | ||
int rounds = inputView.inputRaceRounds(); | ||
|
||
// 3. 게임 실행 | ||
racingGame.gameStart(rounds); | ||
|
||
// 4. 결과 출력 | ||
List<Car> winnerCars = racingGame.getWinner(); | ||
String winnerNames = racingGame.findWinnerName(winnerCars); | ||
|
||
resultView.printResult(winnerCars, winnerNames); | ||
} | ||
|
||
public static List<Car> createCars(String[] carNames) { | ||
List<Car> cars = new ArrayList<>(); | ||
for (String name : carNames) { | ||
cars.add(new Car(name.trim())); | ||
} | ||
return cars; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import java.util.List; | ||
|
||
public class ResultView { | ||
|
||
public void printResult(List<Car> winnerCars, String winnerNames) { | ||
System.out.println("실행 결과"); | ||
System.out.println(winnerNames + "가 최종 우승했습니다."); | ||
} | ||
} |