-
Notifications
You must be signed in to change notification settings - Fork 0
/
WallHugger.java
235 lines (214 loc) · 9.89 KB
/
WallHugger.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import java.util.Random;
public class WallHugger {
//wall hugger hand written agent
int[] increments;
Random rand;
int boardSize;
public WallHugger(Random rand, int boardSize) {
increments = new int[4];
this.rand = rand;
this.boardSize = boardSize;
}
public void calculateIncrements(double[] featureVector){
increments[0] = 0;
increments[1] = 0;
increments[2] = 0;
increments[3] = 0;
if(featureVector[0]<0 || featureVector[7]<0){//top left
increments[0]++;
}
if(featureVector[1]<0 || featureVector[2]<0){ //top right
increments[1]++;
}
if(featureVector[3]<0 || featureVector[4]<0){ //bottom right
increments[2]++;
}
if(featureVector[5]<0 || featureVector[6]<0){ //bottom left
increments[3]++;
}
if(featureVector[8] == 1){
increments[0]++;
}
if( featureVector[8] == -1){
increments[2]++;
}
if(featureVector[9] == -1){
increments[1]++;
}
if(featureVector[9] == 1){
increments[3]++;
}
}
double[] run(int numGames){
double[] averageScoreAndMoves = new double[3];
averageScoreAndMoves[0] = 0;
averageScoreAndMoves[1] = 0;
averageScoreAndMoves[2] = 0;
char directionChar = ' ';
for(int i = 0; i < numGames; i++){
Snake snake = new Snake(boardSize, rand);
char flag = 'C';
while (flag != 'X' && flag != 'W') {
//snake.printBoard();
double[] featureVector = snake.getFeatureInput();
calculateIncrements(featureVector);
//create an array that stores the indexes of increments in descending order
int[] sortedIncrements = new int[4];
int[] listInOrder = new int[4];
sortedIncrements[0] = 0;
sortedIncrements[1] = 1;
sortedIncrements[2] = 2;
sortedIncrements[3] = 3;
for(int j = 0; j < 4; j++){
for(int k = j+1; k < 4; k++){
if(increments[sortedIncrements[j]] < increments[sortedIncrements[k]]){
int temp = sortedIncrements[j];
sortedIncrements[j] = sortedIncrements[k];
sortedIncrements[k] = temp;
}
}
}
if(increments[sortedIncrements[0]] == increments[sortedIncrements[1]]){
if(increments[sortedIncrements[1]] == increments[sortedIncrements[2]]){
if(increments[sortedIncrements[2]] == increments[sortedIncrements[3]]){
//all the same, fill ListInOrder randomly
listInOrder[0] = sortedIncrements[rand.nextInt(4)];
while(listInOrder[1] == listInOrder[0]){
listInOrder[1] = sortedIncrements[rand.nextInt(4)];
}
while(listInOrder[2] == listInOrder[0] || listInOrder[2] == listInOrder[1]){
listInOrder[2] = sortedIncrements[rand.nextInt(4)];
}
while(listInOrder[3] == listInOrder[0] || listInOrder[3] == listInOrder[1] || listInOrder[3] == listInOrder[2]){
listInOrder[3] = sortedIncrements[rand.nextInt(4)];
}
}
else{
//3 are the same, fill listInOrder with the 3 that are the same
listInOrder[0] = sortedIncrements[rand.nextInt(3)];
while(listInOrder[1] == listInOrder[0]){
listInOrder[1] = sortedIncrements[rand.nextInt(3)];
}
while(listInOrder[2] == listInOrder[0] || listInOrder[2] == listInOrder[1]){
listInOrder[2] = sortedIncrements[rand.nextInt(3)];
}
listInOrder[3] = sortedIncrements[3];
}
}
else{
//2 are the same, fill listInOrder with the 2 that are the same
listInOrder[0] = sortedIncrements[rand.nextInt(2)];
while(listInOrder[1] == listInOrder[0]){
listInOrder[1] = sortedIncrements[rand.nextInt(2)];
}
if(sortedIncrements[2] == sortedIncrements[3]){
if(rand.nextInt(2) > 0){
listInOrder[2] = sortedIncrements[2];
listInOrder[3] = sortedIncrements[3];
}
else {
listInOrder[2] = sortedIncrements[3];
listInOrder[3] = sortedIncrements[2];
}
}
else{
listInOrder[2] = sortedIncrements[2];
listInOrder[3] = sortedIncrements[3];
}
}
}
else{
listInOrder[0] = sortedIncrements[0];
if(sortedIncrements[1] == sortedIncrements[2]){
if(sortedIncrements[2] == sortedIncrements[3]){
//next three the same, fill randomly
listInOrder[1] = sortedIncrements[rand.nextInt(3)+1];
while(listInOrder[2] == listInOrder[1]){
listInOrder[2] = sortedIncrements[rand.nextInt(3)+1];
}
while(listInOrder[3] == listInOrder[1] || listInOrder[3] == listInOrder[2]){
listInOrder[3] = sortedIncrements[rand.nextInt(3)+1];
}
}
else{
//next two the same, fill listInOrder with the 2 that are the same
listInOrder[1] = sortedIncrements[rand.nextInt(2)+1];
while(listInOrder[2] == listInOrder[1]){
listInOrder[2] = sortedIncrements[rand.nextInt(2)+1];
}
listInOrder[3] = sortedIncrements[3];
}
}
else{
listInOrder[1] = sortedIncrements[1];
if(sortedIncrements[2] == sortedIncrements[3]){
if(rand.nextInt(2) > 0){
listInOrder[2] = sortedIncrements[2];
listInOrder[3] = sortedIncrements[3];
}
else {
listInOrder[2] = sortedIncrements[3];
listInOrder[3] = sortedIncrements[2];
}
}
else{
listInOrder[2] = sortedIncrements[2];
listInOrder[3] = sortedIncrements[3];
}
}
}
if(snake.nextMoveLosesGame(getDirection(listInOrder, 0))){
if(snake.nextMoveLosesGame(getDirection(listInOrder, 1))){
if(snake.nextMoveLosesGame(getDirection(listInOrder, 2))){
if(snake.nextMoveLosesGame(getDirection(listInOrder, 3))){
directionChar = getDirection(sortedIncrements, 0);
}
else{
directionChar = getDirection(listInOrder, 3);
}
}
else{
directionChar = getDirection(listInOrder, 2);
}
}
else{
directionChar = getDirection(listInOrder, 1);
}
}
else{
directionChar = getDirection(listInOrder, 0);
}
// System.out.println("Direction: " + directionChar);
// System.out.println("Increment: " + increments[0] + " " + increments[1] + " " + increments[2] + " " + increments[3]);
flag = snake.move(directionChar);
}
// snake.printBoard();
// System.out.println("Direction: " + directionChar);
// System.out.println("Increment: " + increments[0] + " " + increments[1] + " " + increments[2] + " " + increments[3]);
averageScoreAndMoves[0] += snake.score;
averageScoreAndMoves[1] += snake.numMoves;
if (snake.score == snake.boardSize * snake.boardSize - 1){
averageScoreAndMoves[2]++;
}
}
averageScoreAndMoves[0] = averageScoreAndMoves[0]/numGames;
averageScoreAndMoves[1] = averageScoreAndMoves[1]/numGames;
return averageScoreAndMoves;
}
public char getDirection(int[] sortedIncrements, int val){
char directionChar = ' ';
if(sortedIncrements[val] == 0){
directionChar = 'U';
}
if(sortedIncrements[val] == 1){
directionChar = 'R';
}
if(sortedIncrements[val] == 2){
directionChar = 'D';
}
if(sortedIncrements[val] == 3){
directionChar = 'L';
}
return directionChar;
}
}