-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWolframRule30.java
72 lines (58 loc) · 1.82 KB
/
WolframRule30.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
import java.util.Scanner;
public class WolframRule30{
//x is the dimension (i.e. x times x array)
public static int rule30( int [][] arr, int rowIndex, int colIndex, int x){
int y = rowIndex - 1; //to represent row index
int first, second, third, toComp;
second = arr[y][colIndex];
//changing some values acc. to cases
if ( (colIndex != 0) && (colIndex != (x - 1)) ){
first = arr[y][colIndex-1];
third = arr[y][colIndex+1];
} else{
if ( colIndex == 0 ){
first = 0;
third = arr[y][colIndex+1];
}
//colIndex == (x - 1)
else{
third = 0;
first = arr[y][colIndex-1];
}
} //end if-else
toComp = (first * 100) + (second * 10) + third;
//check toComp and return either 1 or 0 acc. to rule 30
if ( toComp == 111 ) return 0;
else if ( toComp == 110 ) return 0;
else if ( toComp == 101 ) return 0;
else if ( toComp == 000 ) return 0;
else if ( toComp == 100 ) return 1;
else if ( toComp == 011 ) return 1;
else if ( toComp == 010 ) return 1;
return 1; //if 001
} //end rule30
public static void modify( int [][] arr, int x ){
arr[0][x / 2] = 1;
for ( int i = 1; i < x ; i++ ){
for ( int j = 0; j < x ; j++ ){
arr[i][j] = rule30(arr, i, j, x);
} // end inner for
} //end outer for
} //end printArray
public static void printArray( int [][] arr, int x ){
for ( int i = 0; i < x ; i++ ){
for ( int j = 0; j < x ; j++ ){
System.out.print( arr[i][j] );
} // end inner for
System.out.println();
} //end outer for
} //end printArray
public static void main( String[] args ){
//System.out.println("Enter a num (from 1 to 30): ");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int[][] hey = new int[x][x];
modify( hey, x );
printArray( hey, x );
}
}