-
Notifications
You must be signed in to change notification settings - Fork 0
/
EffGraph.java
206 lines (203 loc) · 5.76 KB
/
EffGraph.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
/* Charlie Gerrie 2018
*
* This is a more efficient, thought less flexible, simulator for power-index games on a grid.
*/
public class EffGraph implements Simulatable {
Side[][] sides, next;
int[][] computeStates, powers;
public EffGraph(Side[][] sides, int[][] computeStates) {
this.sides = sides;
this.computeStates = computeStates;
powers = new int[sides.length][];
next = new Side[sides.length][];
for(int i=0;i<sides.length;i++) {
powers[i] = new int[sides[i].length];
next[i] = new Side[sides[i].length];
}
}
public Side gridGet(int x, int y) {
return sides[x][y];
}
public void step() {
// compute power
Side[] neighbors;
int forCount;
for(int i=0;i<sides.length;i++) {
for(int j=0;j<sides[i].length;j++) {
if((computeStates[i][j]&1)!=0) {
forCount = 0;
neighbors = closedNeighborhood(i,j);
for(int k=0;k<5;k++)
if(sides[i][j].equals(neighbors[k]))
forCount++;
if(forCount>=3)
powers[i][j] = forCount;
else
powers[i][j] = 0;
}
}
}
// compute next
int maxPower;
Side changeTo;
int[] neighborsPower;
for(int i=0;i<sides.length;i++) {
for(int j=0;j<sides[i].length;j++) {
if((computeStates[i][j]&2)!=0) {
maxPower = 0;
changeTo = null;
neighborsPower = closedNeighborhoodPower(i,j);
neighbors = closedNeighborhood(i,j);
for(int k=0;k<5;k++) {
if(maxPower == 0 ||
(neighborsPower[k] != 0 && neighborsPower[k]<maxPower) ||
(neighborsPower[k]==maxPower &&
neighbors[k].equals(sides[i][j]))) {
maxPower = neighborsPower[k];
changeTo = neighbors[k];
}
}
next[i][j] = changeTo;
}
}
}
// set next
for(int i=0;i<sides.length;i++) {
for(int j=0;j<sides[i].length;j++) {
if(computeStates[i][j]>=2)
sides[i][j] = next[i][j];
}
}
}
public void stepTorus() {
// compute power
Side[] neighbors;
int forCount;
for(int i=0;i<sides.length;i++) {
for(int j=0;j<sides[i].length;j++) {
if((computeStates[i][j]&1)!=0) {
forCount = 0;
neighbors = closedNeighborhoodTorus(i,j);
for(int k=0;k<5;k++)
if(sides[i][j].equals(neighbors[k]))
forCount++;
if(forCount>=3)
powers[i][j] = forCount;
else
powers[i][j] = 0;
}
}
}
// compute next
int maxPower;
Side changeTo;
int[] neighborsPower;
for(int i=0;i<sides.length;i++) {
for(int j=0;j<sides[i].length;j++) {
if((computeStates[i][j]&2)!=0) {
maxPower = 0;
changeTo = null;
neighborsPower = closedNeighborhoodPowerTorus(i,j);
neighbors = closedNeighborhoodTorus(i,j);
for(int k=0;k<5;k++) {
if(maxPower == 0 ||
(neighborsPower[k] != 0 && neighborsPower[k]<maxPower) ||
(neighborsPower[k]==maxPower &&
neighbors[k].equals(sides[i][j]))) {
maxPower = neighborsPower[k];
changeTo = neighbors[k];
}
}
next[i][j] = changeTo;
}
}
}
// set next
for(int i=0;i<sides.length;i++) {
for(int j=0;j<sides[i].length;j++) {
if(computeStates[i][j]>=2)
sides[i][j] = next[i][j];
}
}
}
private Side[] closedNeighborhood(int x, int y) {
Side[] list = new Side[5];
list[0] = sides[x+1][y];
list[1] = sides[x][y+1];
list[2] = sides[x-1][y];
list[3] = sides[x][y-1];
list[4] = sides[x][y];
return list;
}
private int[] closedNeighborhoodPower(int x, int y) {
int[] list = new int[5];
list[0] = powers[x+1][y];
list[1] = powers[x][y+1];
list[2] = powers[x-1][y];
list[3] = powers[x][y-1];
list[4] = powers[x][y];
return list;
}
private Side[] closedNeighborhoodTorus(int x, int y) {
Side[] list = new Side[5];
list[0] = sides[Util.mod(x+1, sides.length)][y];
list[1] = sides[x][Util.mod(y+1, sides[x].length)];
list[2] = sides[Util.mod(x-1, sides.length)][y];
list[3] = sides[x][Util.mod(y-1, sides[x].length)];
list[4] = sides[x][y];
return list;
}
private int[] closedNeighborhoodPowerTorus(int x, int y) {
int[] list = new int[5];
list[0] = powers[Util.mod(x+1, sides.length)][y];
list[1] = powers[x][Util.mod(y+1, sides[x].length)];
list[2] = powers[Util.mod(x-1, sides.length)][y];
list[3] = powers[x][Util.mod(y-1, sides[x].length)];
list[4] = powers[x][y];
return list;
}
public int[] count2shapes() {
int[] ret = new int[16];
for(int i=0;i<sides.length;i++)
for(int j=0;j<sides[i].length;j++)
ret[getShape(i,j)]++;
return ret;
}
public int[] count2shapesSym() {
int[] ret = new int[6];
for(int i=0;i<sides.length;i++)
for(int j=0;j<sides[i].length;j++)
ret[getShapeSym(i,j)]++;
return ret;
}
public int[] count2ShapesSymSpecified() {
int[] ret = new int[6];
for(int i=0;i<sides.length;i++)
for(int j=0;j<sides[i].length;j++)
if((computeStates[i][j]&8)!=0)
ret[getShapeSym(i,j)]++;
return ret;
}
private int getShape(int i, int j) {
return (sides[i][j]==Side.STRONG?8:0)+
(sides[Util.mod(i+1, sides.length)][j]==Side.STRONG?4:0)+
(sides[Util.mod(i+1, sides.length)][Util.mod(j+1, sides[i].length)]==Side.STRONG?2:0)+
(sides[i][Util.mod(j+1, sides[i].length)]==Side.STRONG?1:0);
}
public int getShapeSym(int i, int j) {
int shape = getShape(i,j);
if(shape==0) // white/weak square
return 0;
if(shape==15) // black/strong square
return 1;
if(shape==3 || shape==6 || shape==9 || shape==12) // side
return 2;
if(shape==5 || shape==10) // diag
return 3;
if(shape==7 || shape==11 || shape==13 || shape==14) // white/weak corner
return 4;
if(shape==1 || shape==2 || shape==4 || shape==8) // black/strong corner
return 5;
return -1;
}
}