-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachine.java
61 lines (53 loc) · 1.98 KB
/
Machine.java
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
import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;
public class Machine extends Player {
public ArrayList<Integer> excludelist = new ArrayList<>();
public Machine(){
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
this.choiceindex_1 = ThreadLocalRandom.current().nextInt(1, 10 + 1);
excludelist.add(this.choiceindex_1);
this.choiceindex_2 = GetRandomWithExclusion(1,10,excludelist);
excludelist.add(this.choiceindex_2);
this.choiceindex_3 = GetRandomWithExclusion(1,10,excludelist);
//form player's choice list, with order following user's choices
for (int i = 0; i < 10; i++) {
if (i == choiceindex_1-1) {
choicelist.add(optionlist.get(i));
}
}
for (int i = 0; i < 10; i++) {
if (i == choiceindex_2-1) {
choicelist.add(optionlist.get(i));
}
}
for (int i = 0; i < 10; i++) {
if (i == choiceindex_3-1) {
choicelist.add(optionlist.get(i));
}
}
System.out.print("System's choices: " +this.choicelist.get(0).name+" " +choicelist.get(1).name+" " +choicelist.get(2).name);
System.out.print("\n");
System.out.print("\n");
}
public int GetRandomWithExclusion(int min, int max, ArrayList<Integer> exclusion) {
int Random = ThreadLocalRandom.current().nextInt(min, max + 1);
while (this.Contain(Random, exclusion) == true) {
if (Random ==10){
Random = 1;
}
else{Random++;}
}
return Random;
}
//whether the excludelist contains the random number
public Boolean Contain(int num, ArrayList<Integer> exclusion){
Boolean contain = false;
for (int exclude : exclusion){
if(num == exclude){
contain = true;
}
}
return contain;
}
}