forked from BCSDLab-Edu/java-racingcar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRace.java
More file actions
71 lines (60 loc) · 1.94 KB
/
Race.java
File metadata and controls
71 lines (60 loc) · 1.94 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
package racingcar;
import camp.nextstep.edu.missionutils.Randoms;
import racingcar.CreateCar;
import java.util.Random;
public class Race {
private Random Rand = new Random();
private CreateCar cr = new CreateCar();
private int raceRound;
private Car[] cars;
private int CarNum;
Race() {
cars = cr.createCars();
CarNum = cars.length;
}
public void initRace() {
System.out.println("경기 횟수를 입력해주세요.");
raceRound = Integer.parseInt(camp.nextstep.edu.missionutils.Console.readLine());
}
public int getRaceRound() {
return raceRound;
}
public boolean move(int random) {
//nt num = Randoms.pickNumberInRange(0,9);
int num = Rand.nextInt(10);
return (num >= random);
}
public void doRace() {
for (int i = 0; i < CarNum; i++) {
if (move(cars[i].getRandomFactor())) {
cars[i].increaseWinNum();
}
}
for (int i = 0; i < CarNum; i++) {
System.out.print(String.format("%s : ", cars[i].getName()));
System.out.println("-".repeat(cars[i].getWinNum()));
}
}
public void printWinner() {
int max = 0;
boolean isFirst = true;
//get max winNums
for (int i = 0; i < CarNum; i++) {
if (max < cars[i].getWinNum()) {
max = cars[i].getWinNum();
}
}
//print Winner
System.out.print("최종 우승자 : ");
for (int i = 0; i < CarNum; i++) {
// add ',' if isn't first winner
if (!isFirst && (max == cars[i].getWinNum())) {
System.out.print(",");
}
if (max == cars[i].getWinNum()) {
System.out.print(String.format("%s", cars[i].getName()));
isFirst = false;
}
}
}
}