-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputer.java
More file actions
55 lines (44 loc) · 1.35 KB
/
Computer.java
File metadata and controls
55 lines (44 loc) · 1.35 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
package baseball.model;
import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;
import java.util.List;
public class Computer {
public static final int NUMBER_LENGTH = 3;
public static final int MIN_RANGE = 1;
public static final int MAX_RANGE = 9;
private List<Integer> randomNumber;
public Computer() {
saveRandomNumberWithGameStart();
}
public List<Integer> getRandomNumber() {
return randomNumber;
}
private void saveRandomNumberWithGameStart() {
randomNumber = new ArrayList<>();
Integer digit;
while (checkLengthSmallThanThree()) {
digit = getRandomDigit();
if (!hasDuplicateDigitInRandomNumber(digit)) {
randomNumber.add(digit);
}
}
}
private boolean checkLengthSmallThanThree() {
if (randomNumber.size() < NUMBER_LENGTH) {
return true;
}
return false;
}
private Integer getRandomDigit() {
return Randoms.pickNumberInRange(MIN_RANGE, MAX_RANGE);
}
private boolean hasDuplicateDigitInRandomNumber(final Integer digit) {
if (randomNumber.contains(digit)) {
return true;
}
return false;
}
public void setRandomNumber(final List<Integer> randomNumber) {
this.randomNumber = randomNumber;
}
}