-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCars.java
More file actions
67 lines (57 loc) · 1.79 KB
/
Cars.java
File metadata and controls
67 lines (57 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
package racingcar;
import outpututils.OutputView;
import utils.RandomUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Cars {
private static final int START_NUMBER = 0;
private static final int END_NUMBER = 9;
private static final int BOUNDARY_NUMBER = 4;
private static List<Car> carList;
private static ArrayList<String> winner;
public Cars(List<Car> carList) {
this.carList = carList;
}
public static boolean isMove(int randomNumber) {
if (randomNumber >= BOUNDARY_NUMBER) {
return true;
}
return false;
}
public static void playGame() {
for (int j = 0; j < carList.size(); j++) {
OutputView.printCarName(carList.get(j).getName());
if (isMove(RandomUtils.nextInt(START_NUMBER, END_NUMBER))) {
carList.get(j).plusPosition();
}
for (int k = 0; k < carList.get(j).getPosition(); k++) {
OutputView.printMove();
}
System.out.println();
}
}
public static int getMax() {
int max = carList.stream()
.mapToInt(Car::getPosition)
.max()
.getAsInt();
return max;
}
public static void setWinnerList(int max) {
winner = new ArrayList<String>();
carList.stream()
.filter(s -> s.getPosition() == max)
.filter(s -> winner.add(s.getName()))
.collect(Collectors.toList());
}
public static ArrayList<String> getWinnerList() {
return winner;
}
public static void playGames(int inputCount){
for (int i = 0; i < inputCount; i++) {
playGame();
System.out.println();
}
}
}