From cbfd3babd659c178d360b460b115da8442ef348c Mon Sep 17 00:00:00 2001 From: HYH Date: Mon, 28 Oct 2024 09:22:56 -0500 Subject: [PATCH 1/4] add Application.java file --- src/main/java/Application.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/Application.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 0000000000..93a34ad491 --- /dev/null +++ b/src/main/java/Application.java @@ -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 cars = initializeCars(); + int attempts = getAttempts(); + + RacingGame game = new RacingGame(cars, attempts); + game.start(); + } + + private static List initializeCars() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + String input = Console.readLine(); + String[] carNames = input.split(","); + List 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()); + } +} From deda7400e1100cec1d2bd75066ba92d8c93b42cd Mon Sep 17 00:00:00 2001 From: HYH Date: Mon, 28 Oct 2024 09:23:47 -0500 Subject: [PATCH 2/4] add RacingGame.java file --- src/main/java/RacingGame.java | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/RacingGame.java diff --git a/src/main/java/RacingGame.java b/src/main/java/RacingGame.java new file mode 100644 index 0000000000..b32ed7efdc --- /dev/null +++ b/src/main/java/RacingGame.java @@ -0,0 +1,43 @@ +import java.util.List; + +public class RacingGame { + private final List cars; + private final int attempts; + + public RacingGame(List 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 winners = cars.stream() + .filter(car -> car.getPosition() == maxPosition) + .map(Car::getName) + .toList(); + + System.out.println("최종 우승자 : " + String.join(", ", winners)); + } +} From bd0989c540b66e02d255c1258b35fc5ac672c010 Mon Sep 17 00:00:00 2001 From: HYH Date: Mon, 28 Oct 2024 09:24:28 -0500 Subject: [PATCH 3/4] add Car.java file --- src/main/java/Car.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/Car.java diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 0000000000..2aa0c10350 --- /dev/null +++ b/src/main/java/Car.java @@ -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); + } +} From a4d330cd4b26daaedf8faf4d3b41971759d1caaf Mon Sep 17 00:00:00 2001 From: HYH Date: Mon, 28 Oct 2024 09:25:17 -0500 Subject: [PATCH 4/4] add readme.md file --- README.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e71d7759f7..98e9dfa3d1 100644 --- a/README.md +++ b/README.md @@ -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)