diff --git a/README.md b/README.md index 491aece1..1501f0ea 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ -# java-racingcar-precourse \ No newline at end of file +# java-racingcar-precourse +기능구현목록 + +1. 차량이름 입력받기 +2. 차량이름 유효성 검사(사용자가 잘못된 값을 입력할 경우 를 발생시키고, "[ERROR]"로 시작하는 에러 메시지를 출력 후 그 부분부터 입력을 다 시 받는다.) +3. 게임실행 +4. 게임결과출력(매 회차마다 결과출력 및 우승차량 출력) diff --git a/bin/main/.gitkeep b/bin/main/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/bin/main/Application.class b/bin/main/Application.class new file mode 100644 index 00000000..280c0f92 Binary files /dev/null and b/bin/main/Application.class differ diff --git a/bin/main/Car.class b/bin/main/Car.class new file mode 100644 index 00000000..b182df9b Binary files /dev/null and b/bin/main/Car.class differ diff --git a/bin/main/GameController.class b/bin/main/GameController.class new file mode 100644 index 00000000..33a1c1d8 Binary files /dev/null and b/bin/main/GameController.class differ diff --git a/bin/main/GameView.class b/bin/main/GameView.class new file mode 100644 index 00000000..932577af Binary files /dev/null and b/bin/main/GameView.class differ diff --git a/bin/main/Race.class b/bin/main/Race.class new file mode 100644 index 00000000..e548bb65 Binary files /dev/null and b/bin/main/Race.class differ diff --git a/bin/test/.gitkeep b/bin/test/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/bin/test/CarTest.class b/bin/test/CarTest.class new file mode 100644 index 00000000..4cc02d47 Binary files /dev/null and b/bin/test/CarTest.class differ diff --git a/bin/test/RaceTest.class b/bin/test/RaceTest.class new file mode 100644 index 00000000..9b8e0ecd Binary files /dev/null and b/bin/test/RaceTest.class differ diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..a806aa87 --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,6 @@ +public class Application { + public static void main(String[] args) { + GameController gameController = new GameController(); + gameController.startGame(); + } +} \ No newline at end of file diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 00000000..deff4870 --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,23 @@ +public class Car { + private final String name; + private int position; + + public Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void move(int distance) { + if (distance >= 4) { + position++; + } + } +} diff --git a/src/main/java/GameController.java b/src/main/java/GameController.java new file mode 100644 index 00000000..2eea99de --- /dev/null +++ b/src/main/java/GameController.java @@ -0,0 +1,64 @@ +import java.util.Scanner; + +public class GameController { + private Race race; + private final GameView gameView; + private final Scanner scanner; + + public GameController() { + this.race = null; + this.gameView = new GameView(); + this.scanner = new Scanner(System.in); + } + + public void startGame() { + String[] carNames = getCarNames(); + int attemptCount = getAttemptCount(); + race = new Race(carNames); + race.run(attemptCount); + printWinners(); + } + + private String[] getCarNames() { + while (true) { + gameView.requestCarNamesInput(); + String input = scanner.nextLine(); + String[] carNames = input.split(","); + if (isValidInput(carNames)) { + return carNames; + } else { + gameView.displayErrorMessage("유효하지 않은 입력입니다. 자동차 이름은 5자 이하이어야 합니다."); + } + } + } + + private int getAttemptCount() { + while (true) { + gameView.requestAttemptCountInput(); + String input = scanner.nextLine(); + try { + int attemptCount = Integer.parseInt(input); + if (attemptCount > 0) { + return attemptCount; + } else { + gameView.displayErrorMessage("유효하지 않은 입력입니다. 회수는 1 이상이어야 합니다."); + } + } catch (NumberFormatException e) { + gameView.displayErrorMessage("유효하지 않은 입력입니다. 숫자를 입력해주세요."); + } + } + } + + private void printWinners() { + gameView.displayWinners(race.getWinners()); + } + + private boolean isValidInput(String[] carNames) { + for (String name : carNames) { + if (name.length() > 5) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/GameView.java b/src/main/java/GameView.java new file mode 100644 index 00000000..455f9ee6 --- /dev/null +++ b/src/main/java/GameView.java @@ -0,0 +1,39 @@ +import java.util.List; + +public class GameView { + public void requestCarNamesInput() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + } + + public void requestAttemptCountInput() { + System.out.println("시도할 회수는 몇회인가요?"); + } + + public void requestMoveResult(List cars) { + for (Car car : cars) { + System.out.print(car.getName()+" : "); + for(int i=0; i winners) { + System.out.print("최종 우승자 : "); + for (int i = 0; i < winners.size(); i++) { + System.out.print(winners.get(i)); + if (i != winners.size() - 1) { + System.out.print(", "); + } + } + System.out.println(); + } + + public void displayErrorMessage(String message) { + System.out.println("[ERROR] " + message); + } +} diff --git a/src/main/java/Race.java b/src/main/java/Race.java new file mode 100644 index 00000000..c801857e --- /dev/null +++ b/src/main/java/Race.java @@ -0,0 +1,48 @@ +import java.util.ArrayList; +import java.util.List; + +public class Race { + private final List cars; + private final GameView gameView; + + public Race(String[] carNames) { + this.gameView = new GameView(); + this.cars = new ArrayList<>(); + for (String name : carNames) { + this.cars.add(new Car(name)); + } + } + + public List getCars() { + return cars; + } + + public void run(int attempts) { + System.out.println("실행 결과"); + for (int i = 0; i < attempts; i++) { + for (Car car : cars) { + car.move((int) (Math.random() * 10)); + } + gameView.requestMoveResult(cars); + } + } + + public List getWinners() { + List winners = new ArrayList<>(); + int maxPosition = getMaxPosition(); + for (Car car : cars) { + if (car.getPosition() == maxPosition) { + winners.add(car.getName()); + } + } + return winners; + } + + private int getMaxPosition() { + int maxPosition = 0; + for (Car car : cars) { + maxPosition = Math.max(maxPosition, car.getPosition()); + } + return maxPosition; + } +} diff --git a/src/test/java/CarTest.java b/src/test/java/CarTest.java new file mode 100644 index 00000000..39f88a5c --- /dev/null +++ b/src/test/java/CarTest.java @@ -0,0 +1,13 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class CarTest { + @Test + void testMove() { + Car car = new Car("car1"); + car.move(4); // 조건에 따라 이동 + assertEquals(1, car.getPosition()); + car.move(3); // 이동하지 않음 + assertEquals(1, car.getPosition()); + } +} diff --git a/src/test/java/RaceTest.java b/src/test/java/RaceTest.java new file mode 100644 index 00000000..dfa2ea93 --- /dev/null +++ b/src/test/java/RaceTest.java @@ -0,0 +1,24 @@ +// RaceTest.java +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.List; + +public class RaceTest { + @Test + void testRun() { + String[] carNames = {"car1", "car2", "car3"}; + Race race = new Race(carNames); + race.run(5); + List cars = race.getCars(); + for (Car car : cars) { + assertEquals(true, car.getPosition() <= 5); + } + } + + @Test + void testGetWinners() { + Race race = new Race(new String[]{"car1", "car2", "car3"}); + List winners = race.getWinners(); + assertEquals(3, winners.size()); + } +}