|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +public class GameStart { |
| 5 | + private static Car[] cars; |
| 6 | + private static int times; |
| 7 | + private static FindWinCar race; |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + carNameInput(); |
| 11 | + timesInput(); |
| 12 | + |
| 13 | + // 경기 진행 |
| 14 | + race = new FindWinCar(cars.length, cars); |
| 15 | + for (int i = 0; i < times; ++i){ |
| 16 | + game(); |
| 17 | + } |
| 18 | + |
| 19 | + // 최종 출력 |
| 20 | + print(race.getLocate()); |
| 21 | + String winNames = Arrays.stream(race.findWin()) |
| 22 | + .map(Car::getName) |
| 23 | + .reduce((a, b) -> a + ", " + b) |
| 24 | + .orElse(""); |
| 25 | + System.out.println(winNames + "가 최종 우승했습니다."); |
| 26 | + } |
| 27 | + |
| 28 | + private static void carNameInput(){ |
| 29 | + Scanner in = new Scanner(System.in); |
| 30 | + |
| 31 | + // 자동차 이름 |
| 32 | + System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); |
| 33 | + String[] carNames = in.nextLine().split(","); |
| 34 | + |
| 35 | + for (String name : carNames){ |
| 36 | + if (name.length() > 5 || name.isEmpty()){ |
| 37 | + throw new IllegalArgumentException(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // 객체 생성 |
| 42 | + cars = Arrays.stream(carNames) |
| 43 | + .map(Car::new) |
| 44 | + .toArray(Car[]::new); |
| 45 | + |
| 46 | + in.close(); |
| 47 | + } |
| 48 | + |
| 49 | + private static void timesInput(){ |
| 50 | + Scanner in = new Scanner(System.in); |
| 51 | + |
| 52 | + // 이동 횟수 |
| 53 | + System.out.println("시도할 회수는 몇회인가요?"); |
| 54 | + times = in.nextInt(); |
| 55 | + if (times < 0){ |
| 56 | + throw new IllegalArgumentException(); |
| 57 | + } |
| 58 | + |
| 59 | + in.close(); |
| 60 | + } |
| 61 | + |
| 62 | + private static void game(){ |
| 63 | + print(race.getLocate()); |
| 64 | + race.move(); |
| 65 | + System.out.println(); |
| 66 | + } |
| 67 | + |
| 68 | + private static void print(int[] locate){ |
| 69 | + for (int i = 0; i < cars.length; ++i){ |
| 70 | + System.out.println(cars[i].getName() + " : " + locate[i]); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments