-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlobGoal.java
More file actions
80 lines (73 loc) · 2.6 KB
/
BlobGoal.java
File metadata and controls
80 lines (73 loc) · 2.6 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
import java.awt.Color;
public class BlobGoal extends Goal {
public BlobGoal(Color c) {
super(c);
}
@Override
public int score(Block board) {
Color[][] flattened = board.flatten();
boolean[][] visited = new boolean[flattened.length][flattened.length];
int score = 0;
for (int i = 0; i < flattened.length; i++) {
for (int j = 0; j < flattened.length; j++) {
score += undiscoveredBlobSize(i, j, flattened, visited);
}
}
return score;
}
@Override
public String description() {
return "Create the largest connected blob of " + GameColors.colorToString(targetGoal)
+ " blocks, anywhere within the block";
}
public int undiscoveredBlobSize(int i, int j, Color[][] unitCells, boolean[][] visited) {
/*if (i < 0 || j < 0 || i >= unitCells.length || j >= unitCells.length || visited[i][j] || unitCells[i][j] != targetGoal) {
return 0;
} else {
visited[i][j] = true;
int size = 1 + undiscoveredBlobSize(i - 1, j, unitCells, visited)
+ undiscoveredBlobSize(i + 1, j, unitCells, visited)
+ undiscoveredBlobSize(i, j - 1, unitCells, visited)
+ undiscoveredBlobSize(i, j + 1, unitCells, visited);
return size;
}
}*/
if (i < 0 || j < 0 || i >= unitCells.length || j >= unitCells.length || visited[i][j] || unitCells[i][j] != targetGoal) {
return 0;
}
int size = 1;
//check input cell
if (unitCells[i][j] == targetGoal && !(visited[i][j])) {
visited[i][j] = true;
size += undiscoveredBlobSize(i - 1, j, unitCells, visited);
size += undiscoveredBlobSize(i + 1, j, unitCells, visited);
size += undiscoveredBlobSize(i, j - 1, unitCells, visited);
size += undiscoveredBlobSize(i, j + 1, unitCells, visited);
}
//check for cell above
//if (i != 0) {
//if (unitCells[i - 1][j] == targetGoal && !(visited[i - 1][j])) {
// size += undiscoveredBlobSize(i - 1, j, unitCells, visited);
//}
//}
//check for cell below
//if (i != unitCells.length - 1) {
//if (unitCells[i + 1][j] == targetGoal && !(visited[i + 1][j])) {
// size += undiscoveredBlobSize(i + 1, j, unitCells, visited);
//}
//}
//check cell left to input cell
//if (j != 0) {
//if (unitCells[i][j - 1] == targetGoal && !(visited[i][j - 1])) {
//size += undiscoveredBlobSize(i, j - 1, unitCells, visited);
//}
//}
//check cell right to input cell
//if (j != unitCells.length - 1) {
//if (unitCells[i][j + 1] == targetGoal && !(visited[i][j + 1])) {
//size += undiscoveredBlobSize(i, j + 1, unitCells, visited);
//}
//}
return size;
}
}