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

[자동차 경주] 현지혜 미션 제출합니다. #826

Open
wants to merge 4 commits into
base: main
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
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# java-racingcar
# Racing Game

자동차 경주 미션 저장소
## 기능 요구 사항
1. **자동차 이름 입력**: 쉼표(,)로 구분된 자동차 이름을 입력받습니다. 각 이름은 5자 이하여야 합니다.
2. **시도 횟수 입력**: 시도할 횟수를 입력받습니다.
3. **경주 진행**:
- 각 자동차는 무작위 값(0~9)을 받아 4 이상일 때 전진합니다.
- 경주가 진행될 때마다 각 자동차의 위치를 출력합니다.
4. **우승자 발표**: 최종 우승자를 출력합니다. 동점자가 있으면 쉼표(,)로 구분하여 여러 우승자를 표시합니다.
5. **잘못된 입력 처리**: 자동차 이름이 5자를 초과하거나, 입력 값이 부적절하면 `IllegalArgumentException`이 발생하고 프로그램은 종료됩니다.

## 우아한테크코스 코드리뷰

- [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md)
32 changes: 32 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;
import java.util.List;

public class Application {
public static void main(String[] args) {
List<Car> cars = initializeCars();
int attempts = getAttempts();

RacingGame game = new RacingGame(cars, attempts);
game.start();
}

private static List<Car> initializeCars() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String input = Console.readLine();
String[] carNames = input.split(",");
List<Car> cars = new ArrayList<>();

for (String name : carNames) {
cars.add(new Car(name.trim()));
}

return cars;
}

private static int getAttempts() {
System.out.println("시도할 횟수는 몇 회인가요?");
return Integer.parseInt(Console.readLine());
}
}
30 changes: 30 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public class Car {
private final String name;
private int position;

public Car(String name) {
if (name.length() > 5) {
throw new IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다.");
}
this.name = name;
this.position = 0;
}

public void move(int randomValue) {
if (randomValue >= 4) {
position++;
}
}

public String getName() {
return name;
}

public int getPosition() {
return position;
}

public String getProgress() {
return "-".repeat(position);
}
}
43 changes: 43 additions & 0 deletions src/main/java/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.List;

public class RacingGame {
private final List<Car> cars;
private final int attempts;

public RacingGame(List<Car> cars, int attempts) {
this.cars = cars;
this.attempts = attempts;
}

public void start() {
for (int i = 0; i < attempts; i++) {
playRound();
printRoundResult();
}
printWinners();
}

private void playRound() {
for (Car car : cars) {
int randomValue = Randoms.pickNumberInRange(0, 9);
car.move(randomValue);
}
}

private void printRoundResult() {
for (Car car : cars) {
System.out.println(car.getName() + " : " + car.getProgress());
}
System.out.println();
}

private void printWinners() {
int maxPosition = cars.stream().mapToInt(Car::getPosition).max().orElse(0);
List<String> winners = cars.stream()
.filter(car -> car.getPosition() == maxPosition)
.map(Car::getName)
.toList();

System.out.println("최종 우승자 : " + String.join(", ", winners));
}
}