-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGame.java
More file actions
49 lines (42 loc) · 1.23 KB
/
Game.java
File metadata and controls
49 lines (42 loc) · 1.23 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
import java.util.ArrayList;
import java.util.List;
public class Game {
private List<Car> cars;
public Game(List<Car> cars) {
this.cars = cars;
}
public List<Car> getCars() {
return cars;
}
private void printCarPosition(Car car) {
System.out.print(car.getName() + " : ");
int carPosition = car.getPosition();
for (int i = 0; i < carPosition; i++) {
System.out.print("-");
}
System.out.println();
}
public void startGame(int rounds){
for (int i = 0; i < rounds; i++) {
for (Car car : cars) {
car.moveOrStop();
printCarPosition(car);
}
System.out.println();
}
decideGameWinner();
}
private void decideGameWinner() {
int maxPosition = 0;
for (Car car : cars) {
maxPosition = Math.max(maxPosition, car.getPosition());
}
List<String> winnerNames = new ArrayList<>();
for (Car car : cars) {
if (car.getPosition() == maxPosition) {
winnerNames.add(car.getName());
}
}
System.out.println("최종 우승자 : " + String.join(", ", winnerNames));
}
}