-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTOOLS_Rules.java
More file actions
185 lines (152 loc) · 4.41 KB
/
Copy pathTOOLS_Rules.java
File metadata and controls
185 lines (152 loc) · 4.41 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
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
import java.util.List;
class ET extends OperatorNode {
public ET(TreeNode left, TreeNode right) {
super(left, right);
}
@Override
int getValue() {
return (left.getValue() != 0 && right.getValue() != 0) ? 1 : 0;
}
}
class OU extends OperatorNode {
public OU(TreeNode left, TreeNode right) {
super(left, right);
}
@Override
int getValue() {
return (left.getValue() != 0 || right.getValue() != 0) ? 1 : 0;
}
}
class NON extends OperatorNode {
public NON(TreeNode left) {
super(left);
}
@Override
int getValue() {
return (left.getValue() == 0) ? 1 : 0;
}
}
class SUP extends OperatorNode {
public SUP(TreeNode left, TreeNode right) {
super(left, right);
}
@Override
int getValue() {
return (left.getValue() > right.getValue()) ? 1 : 0;
}
}
class SUPEQ extends OperatorNode {
public SUPEQ(TreeNode left, TreeNode right) {
super(left, right);
}
@Override
int getValue() {
return (left.getValue() >= right.getValue()) ? 1 : 0;
}
}
class EQ extends OperatorNode {
public EQ(TreeNode left, TreeNode right) {
super(left, right);
}
@Override
int getValue() {
return (left.getValue() == right.getValue()) ? 1 : 0;
}
}
class ADD extends OperatorNode {
public ADD(TreeNode left, TreeNode right) {
super(left, right);
}
@Override
int getValue() {
return left.getValue() + right.getValue();
}
}
class SUB extends OperatorNode {
public SUB(TreeNode left, TreeNode right) {
super(left, right);
}
@Override
int getValue() {
return left.getValue() - right.getValue();
}
}
class MUL extends OperatorNode {
public MUL(TreeNode left, TreeNode right) {
super(left, right);
}
@Override
int getValue() {
return left.getValue() * right.getValue();
}
}
class SI extends OperatorNode {
public SI(TreeNode left, TreeNode middle, TreeNode right) {
super(left, middle, right);
}
@Override
int getValue() {
return (left.getValue() != 0) ? middle.getValue() : right.getValue();
}
}
class COMPTER extends TreeNode {
private static STRUCT_Grid_ND grid;
private static int[] position;
private String voisinage;
// Constructor to initialize the 'voisinage' variable
public COMPTER(String voisinage) {
this.voisinage = voisinage;
}
// Static method to set the grid and position for all COMPTER objects
public static void setSettings(STRUCT_Grid_ND grid, int... position) {
COMPTER.grid = grid;
COMPTER.position = position;
}
// Method to get neighbors based on the current position and voisinage
private List<int[]> getNeighbors() {
return TOOLS_Neighborhoods.getNeighborhoodByName(voisinage).getNeighbors(position);
}
@Override
int getValue() {
int liveNeighbors = 0;
List<int[]> neighbors = getNeighbors();
for (int[] neighbor : neighbors) {
try {
if (grid.getCell(neighbor).getCellValue()) {
liveNeighbors++;
}
} catch (ArrayIndexOutOfBoundsException e) {
// If the neighbor is out of bounds, treat it as a dead cell
}
}
return liveNeighbors;
}
}
class Rule3D {
public boolean isAlive(STRUCT_Grid_ND grid, int ...position)
{
int x = position[0];
int y = position[1];
int z = position[2];
COMPTER.setSettings(grid, x, y, z);
TOOLS_EvolutionRule rule3d = new TOOLS_EvolutionRule("SI(EQ(COMPTER(G0),1), SI(OU(EQ(COMPTER(G26*),5),EQ(COMPTER(G26*),6)),1,0) , SI(EQ(COMPTER(G26*),4),1,0))", false);
rule3d.cursor = 0;
return rule3d.createNode(rule3d.parseFile()).getValue() == 1;
}
}
class Rule2D {
public boolean isAlive(STRUCT_Grid_ND grid, int ...position)
{
int x = position[0];
int y = position[1];
COMPTER.setSettings(grid, x, y);
TOOLS_EvolutionRule rule2d = new TOOLS_EvolutionRule("SI(EQ(COMPTER(G0),1), SI(OU(EQ(COMPTER(G8*),2),EQ(COMPTER(G8*),3)),1,0),SI(EQ(COMPTER(G8*),3),1,0))", false);
rule2d.cursor = 0;
return rule2d.createNode(rule2d.parseFile()).getValue() == 1;
}
}
public class TOOLS_Rules {
public static int evaluate(TreeNode tree) {
return tree.getValue();
}
}