-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRacingGameController.java
More file actions
55 lines (46 loc) · 1.57 KB
/
RacingGameController.java
File metadata and controls
55 lines (46 loc) · 1.57 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
package controller;
import java.util.List;
import java.util.function.Supplier;
import domain.Car;
import domain.RacingGame;
import domain.RandomNumberGenerator;
import view.InputView;
import view.OutputView;
public class RacingGameController {
private final InputView inputView;
private final OutputView outputView;
public RacingGameController(final InputView inputView, final OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
public <T> T retry(final Supplier<T> supplier) {
try {
return supplier.get();
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
return retry(supplier);
}
}
public void run() {
RacingGame racingGame = initialize();
play(racingGame);
findWinners(racingGame);
}
private RacingGame initialize() {
List<String> carNames = retry(inputView::readCarNames);
int count = retry(inputView::readCount);
return new RacingGame(new RandomNumberGenerator(), carNames, count);
}
private void play(final RacingGame racingGame) {
outputView.printResultMessage();
while (racingGame.isPlayable()) {
racingGame.play();
List<Car> cars = racingGame.findCurrentCarPositions();
outputView.printCurrentCarPositions(cars);
}
}
private void findWinners(final RacingGame racingGame) {
List<String> winners = racingGame.findWinners();
outputView.printWinnersMessage(winners);
}
}