-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKemosabe.java
96 lines (70 loc) · 2.93 KB
/
Kemosabe.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
import java.util.Random;
public class Kemosabe extends Shooter {
private Boolean shoot = false;
public String play(String mine, String other){
if(mine.equals("")){shoot = false;return "R";} //Always reload the first move
if(shoot) {shoot = false;return "S";}
int rounds = mine.length();
char myLastMove = mine.charAt(mine.length() - 1);
char hisLastMove = other.charAt(mine.length() - 1);
int myRCount = mine.length() - mine.replace("R", "").length();
int hisRCount = other.length() - other.replace("R", "").length();
int mySCount = mine.length() - mine.replace("S", "").length();
int hisSCount = other.length() - other.replace("S", "").length();
int myBullets = myRCount - mySCount;
int hisBullets = hisRCount - hisSCount;
if (myBullets < 0) {myBullets = 0;}
if (hisBullets < 0) {hisBullets = 0;}
if (myBullets > 5) {return "S";}
if (hisBullets == 0) {return "R";}
if (rounds > 3) {
String myLastThree = mine.substring(mine.length()-3);
String hisLastThree = other.substring(mine.length()-3);
if (rounds > 4) {
String myLastFive = mine.substring(mine.length()-5);
String hisLastFive = other.substring(mine.length()-5);
if (myLastFive.substring(0,4).equals(hisLastFive.substring(1))) {
if (myLastMove == 'R' && myBullets > 0) {return "S";}
else {return "R";}
}
if (hisLastFive.equals("RRRRR") && myBullets > 0) {
return "S";
}
if (hisLastFive.equals("BBBBB")) {
if (myBullets < 4) {return "R";}
else if (hisBullets < 1) {return "R";}
else {return "B";}
}
if (rounds > 5) {
String myLastSix = mine.substring(mine.length()-6);
String hisLastSix = other.substring(mine.length()-6);
if (hisLastSix.substring(0,3).equals(hisLastThree)) {
if (hisLastThree.substring(0,1).equals("R")) {
if (myBullets > 0) {return "S";}
}
}
}
}
if (myLastThree.equals(hisLastThree) || (hisLastThree == "BBB")) {
int r = randInt(0,4);
if ((r == 0 || r == 1) && myBullets > 0) {return "S";}
else if (r == 2) {return "B";}
else {shoot = true;return "R";}
}
}
if (hisBullets == 5 && myBullets == 5) {return "S";}
else if (hisBullets == 5 && myBullets < 4) {return "R";}
else if (hisBullets - myBullets == 1) {return "B";}
else if (hisBullets > 0 && myBullets > 0) {return "B";}
else {return "R";}
}
public static int randInt(int min, int max) {
// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}