-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathNumBallsNBalls.java
More file actions
84 lines (74 loc) · 2.55 KB
/
NumBallsNBalls.java
File metadata and controls
84 lines (74 loc) · 2.55 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
72
73
74
75
76
77
78
79
80
81
82
83
84
package num3baseball.dataobject;
import num3baseball.exception.WrongNumberException;
import num3baseball.exception.WrongTypeException;
import java.util.*;
public class NumBallsNBalls implements NumBalls{
private final ArrayList<Integer> nums = new ArrayList<>();
private final int size;
private int randomlyChoose(Set<Integer> numSet){
Iterator iter = numSet.iterator();
int randomChoose = (int)(numSet.size() * Math.random());
for (int i=1;i<=randomChoose;i++){
iter.next();
}
return (int)iter.next();
}
private void isValidNumber(ArrayList<Integer> num) throws WrongNumberException{
Set<Integer> numSet = new HashSet<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9));
int initialSize = numSet.size();
for (int i:num){
numSet.remove(i);
}
int endSize = numSet.size();
if (initialSize - endSize != num.size()){ // 모든 번호가 한번씩 빠짐 = 중복되지 않고
throw new WrongNumberException();
}
}
public NumBallsNBalls(int size) throws WrongNumberException{
this.size=size;
if (size>9 || size<1){
throw new WrongNumberException();
}
Set<Integer> numSet = new HashSet<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9));
for (int i=1;i<=this.size;i++){
nums.add(randomlyChoose(numSet));
numSet.remove(nums.get(i-1));
}
isValidNumber(nums);
}
public NumBallsNBalls(ArrayList<Integer> nums) throws WrongNumberException{
this.size = nums.size();
isValidNumber(nums);
this.nums.addAll(nums);
}
@Override
public int getNum(int index) {
return nums.get(index);
}
@Override
public int getSize() {
return size;
}
private void compareOne(int thisIndex, int trialIndex, TrialResult result){
if (trialIndex==-1){
return;
}
if (thisIndex == trialIndex){
result.setStrikeBall(result.getStrike()+1, result.getBall());
}
if (thisIndex != trialIndex){
result.setStrikeBall(result.getStrike(), result.getBall()+1);
}
}
@Override
public TrialResult compare(NumBalls trial) {
if (!(trial instanceof NumBallsNBalls) || trial.getSize()!=size){
throw new WrongTypeException();
}
TrialResult result = new TrialResult(size);
for (int i=0;i<size;i++){
compareOne(i, nums.indexOf(trial.getNum(i)), result);
}
return result;
}
}