-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathPlayer.java
More file actions
50 lines (43 loc) · 1.25 KB
/
Player.java
File metadata and controls
50 lines (43 loc) · 1.25 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
import constant.GameConstant;
import data.GameResult;
import java.util.HashSet;
import java.util.Set;
public class Player {
private int[] answer = new int[GameConstant.NUMBER_COUNT];
Set<Integer> numbers = new HashSet<>();
public Player() {
createRandomAnswer();
}
private void createRandomAnswer() {
answer = RandomNumberGenerator.generate(GameConstant.NUMBER_COUNT);
for (int i = 0; i < GameConstant.NUMBER_COUNT; i++) {
numbers.add(answer[i]);
}
}
public GameResult checkMatching(int[] guess) {
int ball = checkBall(guess);
int strike = checkStrike(guess);
return new GameResult(ball, strike);
}
private int checkBall(int[] guess) {
int count = 0;
for (int i = 0; i < GameConstant.NUMBER_COUNT; i++) {
if (answer[i] == guess[i]) {
continue;
}
if (numbers.contains(guess[i])) {
count++;
}
}
return count;
}
private int checkStrike(int[] guess) {
int count = 0;
for (int i = 0; i < GameConstant.NUMBER_COUNT; i++) {
if (answer[i] == guess[i]) {
count++;
}
}
return count;
}
}