-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.pde
More file actions
134 lines (115 loc) · 3.37 KB
/
Copy pathBot.pde
File metadata and controls
134 lines (115 loc) · 3.37 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
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
class Bot extends Player {
public Bot(int startX, int startY, int startDir, int playerID) {
super(startX, startY, startDir, playerID);
}
@Override
public void move() {
int[] justAhead = posInDir(x, y, dir);
if(!testPosition(justAhead)) {
saveSelf();
super.move();
return;
}
super.move();
}
int turn(boolean left, int currDir) {
return mod(currDir + (left ? -1 : 1), 4);
}
void saveSelf() {
int[] turnLeft = posInDir(x, y, turn(true, dir));
boolean leftSafe = testPosition(turnLeft);
int[] turnRight = posInDir(x, y, turn(false, dir));
boolean rightSafe = testPosition(turnRight);
if(leftSafe && rightSafe) {
dir = heuristic();
//dir = Math.random() < .5 ? turn(false, dir) : turn(true, dir);
return;
} else if(leftSafe) {
dir = turn(true, dir);
return;
} else if(rightSafe) {
dir = turn(false, dir);
return;
}
}
public void printCells(Cell[][] arr) {
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; i++) {
Cell c = arr[i][j];
print(c.toString() + " ");
}
println();
}
}
public int heuristic() {
color[][] half1, half2;
// half1 either top or left so if half1 > turn up/left
// half2 either bottom or right so if half2 > turn down/right
if(dir == R || dir == L) {
half1 = new color[fWidth][y];
for(int col = 0; col < fWidth; col++) {
for(int row = 0; row < y; row++) {
half1[col][row] = field[col][row].getColor();
}
}
half2 = new color[fWidth][fHeight-y+1];
for(int col = 0; col < fWidth; col++) {
for (int row = y + 1; row < fHeight; row++) {
half2[col][row-y-1] = field[col][row].getColor();
}
}
} else {
half1 = new color[x][fHeight];
for(int row = 0; row < fHeight; row++) {
for(int col = 0; col < x; col++) {
half1[col][row] = field[col][row].getColor();
}
}
half2 = new color[fWidth-x+1][fHeight];
for(int row = 0; row < fHeight; row++) {
for (int col = x + 1; col < fWidth; col++) {
half2[col-x-1][row] = field[col][row].getColor();
}
}
}
int enemy1 = 0;
int enemy2 = 0;
color enemyColor = pID == 2 ? BLUE : RED;
for(color[] row : half1) {
for(color c : row) {
enemy1 += c == enemyColor ? 1 : 0;
}
}
for(int i = 0; i < half1.length; i++) {
for(int j = 0; j < half1[i].length; j++) {
if(half1[i][j] == enemyColor) {
enemy1++;
}
}
}
for(int i = 0; i < half2.length; i++) {
for(int j = 0; j < half2[i].length; j++) {
if(half2[i][j] == enemyColor) {
enemy2++;
}
}
}
if(enemy2 <= enemy1) {
dir = (dir == R || dir == L) ? D : R;
} else {
dir = (dir == R || dir == L) ? U : L;
}
return dir;
}
public int[] posInDir(int x, int y, int dir) {
int newX = x;
int newY = y;
if(dir == R || dir == L) {
newX += dir == R ? 1 : -1;
}
else if(dir == U || dir == D) {
newY += dir == D ? 1 : -1;
}
return new int[]{newX, newY};
}
}