diff --git a/docs/README.md b/docs/README.md index e69de29..db27e94 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,8 @@ +자동차게임 기능 구현 목록\ +1.사용자 이름 입력받기(,로 나눠서 입력)\ + \# 5자 이상일 경우 에러발생\ + \# 빈칸일 경우 에러발생\ +2.게임 횟수 입력받기\ +3.0~9 사이의 값을 난수로 발생시켜 4이상일 경우 움직이게 한다.\ +4.정해진 횟수만큼 게임 실행 후 최종 가장 많이 움직인 플레이어를 우승자로 한다.\ + \# 공동우승 가능(,로 구분하여 출력) \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e..f543b81 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,79 @@ package racingcar; +import camp.nextstep.edu.missionutils.*; +import java.util.Arrays; public class Application { + + public void forward(String[] cars){ + for (int i =0;i < cars.length; i++){ + + } + } + public static void main(String[] args) { - // TODO: 프로그램 구현 + + //플레이어 자동차 이름 입력받기 + System.out.print("경주할 자동차 이름을 입력하세요 (,로 구분하고 5자 이하) : "); + String carsinput = Console.readLine(); + String[] cars = carsinput.split(","); + + //이름 예외처리 + + for(int i =0;i < cars.length;i++){ //5자 이하로 + if (cars[i].length() > 5){ + throw new IllegalArgumentException(); + } + + if(cars[i].isBlank()){ //빈칸 + throw new IllegalArgumentException(); + } + } + //게임 횟수 입력 + System.out.print("게임 횟수를 입력하세요 : "); + int game = Integer.parseInt(Console.readLine()); + //횟수 예외처리 + if (game <1){ + throw new IllegalArgumentException(); + } + + //경주 게임 출력 + + String[] race = new String[cars.length]; + Arrays.fill(race,""); + int[] count = new int[cars.length]; + for(int i =0;i= 4) {race[j] += "-";System.out.println(race[j]);count[j]++;} + else System.out.println(race[j]); + + } + System.out.println(); + } + + //우승자 출력 + System.out.print("최종 우승자 : "); + int s = cars.length; + for (int i = 0 ; i < s ; i++){ + + for (int j = 0; j < s; j++){ + if (j == i)continue; + if ( count[j] > count[i]){ + System.out.print(cars[j]); + break; + } + else if(count[j] == count[i]){ + System.out.print(cars[j]+","+cars[i]); + break; + } + + + } + } + + + + } }