-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRacingGame.java
More file actions
68 lines (60 loc) · 1.4 KB
/
RacingGame.java
File metadata and controls
68 lines (60 loc) · 1.4 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
package controller;
import java.util.ArrayList;
import java.util.Random;
import model.Car;
import model.RacingCars;
public class RacingGame {
final private static String runMessage = "실행 결과";
final private static int RANDOM_NUMBER_RANGE = 10;
final private static int MOVE_MINIMUM = 4;
private static RacingCars cars;
private static int tryCount;
// 생성자
public RacingGame(ArrayList<String> carNames, int tryCount) {
cars = new RacingCars(carNames);
this.tryCount = tryCount;
}
// 함수
public static int getTryCount() {
return tryCount;
}
// 랜덤 숫자 리턴
private int getRandomNumber() {
return new Random().nextInt(RANDOM_NUMBER_RANGE);
}
// 레이스 진행
private void racing() {
for (Car c : cars.getList()) {
int randomNum = getRandomNumber();
if(randomNum >= MOVE_MINIMUM)
c.move();
c.printNow();
}
System.out.println();
}
// 우승자 업데이트
private void updateResult() {
for(Car c : cars.getList()) {
if(c.getDist() == tryCount) {
c.win();
}
}
}
// 레이스 시작
public void race() {
System.out.println(runMessage);
for(int i=0;i<tryCount;i++) {
racing();
}
updateResult();
}
// 우승자목록 리턴
public ArrayList<String> getWinners() {
ArrayList<String> results = new ArrayList<>();
for(Car c : cars.getList()) {
if(c.getResult())
results.add(c.getCarName());
}
return results;
}
}