forked from ccase/Cowboys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElephant.java
137 lines (115 loc) · 3.63 KB
/
Elephant.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//Elephants never forget
class Elephant extends Shooter
{
static Choice[] analysis = new Choice[9];
public Elephant(){ analysis[0] = new Choice("RR", 0); analysis[1] = new Choice("RB", 0); analysis[2] = new Choice("RS", 0); analysis[3] = new Choice("SR", 0); analysis[4] = new Choice("SB", 0); analysis[5] = new Choice("SS", 0); analysis[6] = new Choice("BR", 0); analysis[7] = new Choice("BB", 0); analysis[8] = new Choice("BS", 0); }
static void main(String[] args){
Elephant e = new Elephant();
//e.play("BBBB","SRSR");
}
public String play(String me, String him){
for (Choice c:analysis) {
c.probability = 0;
}
int hisAmmo = findAmmo(him);
int meAmmo = findAmmo(me);
//Always load first, duh
if(him.length() == 0){
return reload();
}
//Block once to gather data
else if(him.length() == 1){
return block();
}
//Special cases
//If almost at shotgun, wait for them to spend ammo
else if(meAmmo == 5 && hisAmmo > 0){
return block();
}
//If they are almost at shotgun, save a bullet but try to stop them
else if(hisAmmo == 5 && meAmmo > 1){
return shoot();
}
else{
analyze(him);
//What will he do next?
char hisLast = him.toCharArray()[him.toCharArray().length - 1];
Choice[] hisOptions = new Choice[3];
int k = 0;
for(int i = 0; i < 9; i++){
if(analysis[i].pattern.toCharArray()[0] == hisLast){
hisOptions[k++] = analysis[i];
}
}
char hisNext = 'S';
int mostLikely = 0;
for(Choice c : hisOptions){
if(c != null && c.probability > mostLikely){
//System.out.println("Option = " + c.pattern);
//System.out.println("Prob = " + c.probability);
mostLikely = c.probability;
hisNext = c.pattern.toCharArray()[1]; }
}
//Now go through potential cases
//System.out.println("His Next = " + hisNext);
//System.out.println("Me ammo = " + meAmmo);
//Shoot the shotgun
if(meAmmo > 5){ return shoot(); }
else if(meAmmo > 0){
switch(hisNext){
case 'S': return block();
case 'B': return reload();
case 'R': return shoot();
}
}
else{
switch(hisNext){
case 'S': return block();
case 'B': return block();
case 'R': return reload();
}
}
}
return block();
}
static String reload(){
//System.out.println("Reloading");
return "R"; }
static String block(){
//System.out.println("B");
return "B"; }
static String shoot(){
//System.out.println("P");
return "S"; }
void analyze(String his){
//System.out.println("Analyzing");
char[] shortString = his.substring(1,his.length() - 1).toCharArray();
char[] longString = his.toCharArray();
for(int i = 0; i < shortString.length; i++) {
String pattern = "" + longString[i] + shortString[i];
for(Choice c : analysis){
if(c.pattern.equals(pattern)){
//System.out.println("Pattern = " + pattern);
c.probability++;
//System.out.println("Proba = " + c.probability);
}
}
}
}
int findAmmo(String hist){
int Ammo = 0;
for(char c : hist.toCharArray()){
if(c == 'R'){ Ammo++; }
else if (c == 'S'){ Ammo--;}
}
return Ammo;
}
}
class Choice{
String pattern;
int probability;
Choice(String p, int i){
pattern = p;
probability = i;
}
}