-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRacingGameController.java
More file actions
72 lines (60 loc) · 2.14 KB
/
RacingGameController.java
File metadata and controls
72 lines (60 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package controller;
import domain.AttemptNumber;
import domain.Cars;
import dto.CarDto;
import utils.NumberGenerator;
import utils.RandomGenerator;
import view.InputView;
import view.OutputView;
import java.io.IOException;
import java.util.List;
public class RacingGameController {
private final NumberGenerator numberGenerator;
public RacingGameController(){
this.numberGenerator = new RandomGenerator();
}
//run 매서드 게임 실행을 위한 매서드
//자동차 이름 입력, 시도 횟수 입력, 경주 진행, 최종 우ㅇ승자 출력
public void run() throws IOException {
Cars cars = getCars();
System.out.println("check");
AttemptNumber attemptNumber = getAttemptNumber();
race(cars, attemptNumber);
printWinners(cars);
}
private Cars getCars() throws IOException {
List<String> carNames = InputView.readCarNames();
try {
return Cars.from(carNames);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getCars();
}
}
private AttemptNumber getAttemptNumber() throws IOException{
try{
int number = InputView.readAttemptNumber(); //시도할 횟수 입력
return new AttemptNumber(number);
} catch(IllegalArgumentException e){
System.out.println(e.getMessage());
return getAttemptNumber(); //재귀
}
}
private void race(final Cars cars, final AttemptNumber attemptNumber) throws IOException{
OutputView.printResult(); //"실행결과" 메세지 출력
while(attemptNumber.isRemain()){
attemptNumber.decrease();
cars.moveAll(numberGenerator);
printStatus(cars);
}
}
private void printStatus(final Cars cars){
List<CarDto> carDtos = CarDto.getInstances(cars);
OutputView.printStatus(carDtos);
}
private void printWinners(final Cars cars) {
Cars winnerCars = cars.findWinners();
List<CarDto> winnerCarDtos = CarDto.getInstances(winnerCars);
OutputView.printWinners(winnerCarDtos);
}
}