-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules.java
38 lines (34 loc) · 1.65 KB
/
Rules.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
public class Rules {
//rule number must be between 0-255 inclusive
public static int[] getRule(int ruleNumber, int[] previousRow){
String ruleBinary = Integer.toBinaryString(ruleNumber);
int[] nextRow = new int[previousRow.length];
//pad to 8 digits
while(ruleBinary.length() < 8){
ruleBinary = "0" + ruleBinary;
}
//borders are always 0
nextRow[0] = 0;
nextRow[nextRow.length - 1] = 0;
for(int i = 1; i < previousRow.length - 1; i++){
if(previousRow[i-1] == 1 && previousRow[i] == 1 && previousRow[i+1] == 1){
nextRow[i] = Character.getNumericValue(ruleBinary.charAt(0));
}else if(previousRow[i-1] == 1 && previousRow[i] == 1 && previousRow[i+1] == 0){
nextRow[i] = Character.getNumericValue(ruleBinary.charAt(1));
}else if(previousRow[i-1] == 1 && previousRow[i] == 0 && previousRow[i+1] == 1){
nextRow[i] = Character.getNumericValue(ruleBinary.charAt(2));
}else if(previousRow[i-1] == 1 && previousRow[i] == 0 && previousRow[i+1] == 0){
nextRow[i] = Character.getNumericValue(ruleBinary.charAt(3));
}else if(previousRow[i-1] == 0 && previousRow[i] == 1 && previousRow[i+1] == 1){
nextRow[i] = Character.getNumericValue(ruleBinary.charAt(4));
}else if(previousRow[i-1] == 0 && previousRow[i] == 1 && previousRow[i+1] == 0){
nextRow[i] = Character.getNumericValue(ruleBinary.charAt(5));
}else if(previousRow[i-1] == 0 && previousRow[i] == 0 && previousRow[i+1] == 1){
nextRow[i] = Character.getNumericValue(ruleBinary.charAt(6));
}else if(previousRow[i-1] == 0 && previousRow[i] == 0 && previousRow[i+1] == 0){
nextRow[i] = Character.getNumericValue(ruleBinary.charAt(7));
}
}
return nextRow;
}
}