Skip to content
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# java-racingcar-precourse
# java-racingcar-precourse
기능구현목록

1. 차량이름 입력받기
2. 차량이름 유효성 검사(사용자가 잘못된 값을 입력할 경우 를 발생시키고, "[ERROR]"로 시작하는 에러 메시지를 출력 후 그 부분부터 입력을 다 시 받는다.)
3. 게임실행
4. 게임결과출력(매 회차마다 결과출력 및 우승차량 출력)
Empty file added bin/main/.gitkeep
Empty file.
Binary file added bin/main/Application.class
Binary file not shown.
Binary file added bin/main/Car.class
Binary file not shown.
Binary file added bin/main/GameController.class
Binary file not shown.
Binary file added bin/main/GameView.class
Binary file not shown.
Binary file added bin/main/Race.class
Binary file not shown.
Empty file added bin/test/.gitkeep
Empty file.
Binary file added bin/test/CarTest.class
Binary file not shown.
Binary file added bin/test/RaceTest.class
Binary file not shown.
6 changes: 6 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Application {
public static void main(String[] args) {
GameController gameController = new GameController();
gameController.startGame();
}
}
23 changes: 23 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -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++;
}
}
}
64 changes: 64 additions & 0 deletions src/main/java/GameController.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 39 additions & 0 deletions src/main/java/GameView.java
Original file line number Diff line number Diff line change
@@ -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<Car> cars) {
for (Car car : cars) {
System.out.print(car.getName()+" : ");
for(int i=0; i<car.getPosition();i++){
System.out.print("-");
}
System.out.println();
}
System.out.println();
}



public void displayWinners(List<String> 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);
}
}
48 changes: 48 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.List;

public class Race {
private final List<Car> 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<Car> 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<String> getWinners() {
List<String> 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;
}
}
13 changes: 13 additions & 0 deletions src/test/java/CarTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
24 changes: 24 additions & 0 deletions src/test/java/RaceTest.java
Original file line number Diff line number Diff line change
@@ -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<Car> 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<String> winners = race.getWinners();
assertEquals(3, winners.size());
}
}