-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathRace.java
More file actions
53 lines (44 loc) · 1.27 KB
/
Race.java
File metadata and controls
53 lines (44 loc) · 1.27 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
package model;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import view.OutputView; // 추가
public class Race {
private final List<Car> cars;
private static final int FORWARD_THRESHOLD = 4;
private static final int RANDOM_BOUND = 10;
private static final Random RANDOM = new Random();
public Race(List<String> carNames) {
this.cars = carNames.stream()
.map(Car::new)
.collect(Collectors.toList());
}
public List<Car> getCars() {
return cars;
}
public void run(int raceCount) {
for (int i = 0; i < raceCount; i++) {
moveCars();
OutputView.printRaceStatus(cars); // 현재 상태 출력
}
}
private void moveCars() {
for (Car car : cars) {
moveCar(car);
}
}
private void moveCar(Car car) {
if (RANDOM.nextInt(RANDOM_BOUND) >= FORWARD_THRESHOLD) {
car.move();
}
}
public List<Car> getWinners() {
int maxPosition = cars.stream()
.mapToInt(Car::getPosition)
.max()
.orElse(0);
return cars.stream()
.filter(car -> car.getPosition() == maxPosition)
.collect(Collectors.toList());
}
}