-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathRace.java
More file actions
48 lines (42 loc) · 1.21 KB
/
Race.java
File metadata and controls
48 lines (42 loc) · 1.21 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
import java.util.ArrayList;
import java.util.List;
public class Race {
private final List<Car> cars;
private final GameView gameView;
public Race(String[] carNames) {
this.gameView = new GameView();
this.cars = new ArrayList<>();
for (String name : carNames) {
this.cars.add(new Car(name));
}
}
public List<Car> getCars() {
return cars;
}
public void run(int attempts) {
System.out.println("실행 결과");
for (int i = 0; i < attempts; i++) {
for (Car car : cars) {
car.move((int) (Math.random() * 10));
}
gameView.requestMoveResult(cars);
}
}
public List<String> getWinners() {
List<String> winners = new ArrayList<>();
int maxPosition = getMaxPosition();
for (Car car : cars) {
if (car.getPosition() == maxPosition) {
winners.add(car.getName());
}
}
return winners;
}
private int getMaxPosition() {
int maxPosition = 0;
for (Car car : cars) {
maxPosition = Math.max(maxPosition, car.getPosition());
}
return maxPosition;
}
}