-
Notifications
You must be signed in to change notification settings - Fork 830
Expand file tree
/
Copy pathGame.java
More file actions
74 lines (60 loc) · 1.79 KB
/
Copy pathGame.java
File metadata and controls
74 lines (60 loc) · 1.79 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
73
74
package racingcar;
import java.util.ArrayList;
import java.util.List;
public class Game {
List<Car> cars = new ArrayList<>();
public void start() {
View view = new View();
List<String> carNames;
String input;
int count;
input = view.inputCars();
carNames = view.inputCarNames(input); //이름 리스트 생성
count = view.inputCount();
cars = addCars(carNames);
playResult(count, cars);
printWinners(winners(cars));
}
public List<Car> addCars(List<String> carNames) {
for (String carName : carNames) {
Car car = new Car(carName);
cars.add(car);
}
return cars;
}
public void playResult(int count, List<Car> cars) {
System.out.println("실행 결과");
for (int i = 0; i < count; i++) {
for (Car car : cars) {
printGame(car);
}
System.out.println();
}
}
public List<String> winners(List<Car> cars) {
int max = 0;
List<String> winners = new ArrayList<>();
for (Car car : cars) {
if (car.getPosition() > max)
max = car.getPosition();
}
for (Car car : cars) {
if (car.getPosition() == max) {
winners.add(car.getName());
}
}
return winners;
}
public void printWinners(List<String> winners) {
System.out.println("최종 우승자 : " + String.join(", ", winners));
}
private void printGame(Car car) { //start
car.move();
car.getPosition();
System.out.print(car.getName() + " : ");
for (int i = 0; i < car.getPosition(); i++) {
System.out.print("-");
}
System.out.println();
}
}