Fortune Telling Collection - Comprehensive fortune-telling - Seek the source code of life chess written in java
Seek the source code of life chess written in java
Enter the status manually. Calculated according to the following rules:
1 Whether there is the next generation of living cells in each grid depends on the cell survival in the eight grids adjacent to the grid.
When there are 2 or 3 living cells around a living cell, the cell will still survive to the next generation; Or death.
When there are three living cells around a grid, the living cells will appear in the grid in the next generation.
*/
Public class CellLife {
/**
* @param args
*/
Public static void main(String[] args) {
// TODO automatically generated method stub
//int[][]arr = new int[5][5]; //5*5 chessboard
int[][] arr = {{0, 1,0, 1,0},{ 1, 1,0,0},{0,0, 1, 1, 1},{ 1, 1,0,0,0},{0,0, 1
int[][] brr = {{0,0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0,0},{0,0,0,0 } }; ; //state standby
int x,y; //Record rows and columns
int CNT = 0; //Record the number of iterations
Boolean flag = true;
System.out.println ("initial state:");
for(int I = 0; I<5; i++) {
for(int j = 0; j & lt5; j++)
system . out . print(arr[I][j]+" ");
system . out . println();
}
int[]nb = new int[8]; //8 neighbors
while(flag) {
cnt++;
for(int I = 0; I<5; i++) {
for(int j = 0; j & lt5; j++) {
x = I- 1;
y = j- 1;
If ((x> = 0)&; & amp(y & gt=0))
nb[0]= arr[x][y];
other
nb[0]= 0;
x = I- 1;
y = j;
if(x & gt; =0)
nb[ 1]= arr[x][y];
other
nb[ 1]= 0;
x = I- 1;
y = j+ 1;
If ((x> = 0)&; & amp(y & lt5))
nb[2]= arr[x][y];
other
nb[2]= 0;
x = I;
y = j- 1;
If (y & gt=0)
nb[3]= arr[x][y];
other
nb[3]= 0;
x = I;
y = j+ 1;
if(y & lt; 5)
nb[4]= arr[x][y];
other
nb[4]= 0;
x = I+ 1;
y = j- 1;
If ((x<5)&; & amp(y & gt=0))
nb[5]= arr[x][y];
other
nb[5]= 0;
x = I+ 1;
y = j;
if(x & lt; 5)
nb[6]= arr[x][y];
other
nb[6]= 0;
x = I+ 1;
y = j+ 1;
If ((x<5)&; & amp(y & lt5))
nb[7]= arr[x][y];
other
nb[7]= 0;
//Judge the state of the next generation
int sum = 0;
for(int n = 0; n & lt8; n++)
sum = sum+nb[n];
If (arr [I] [j] =1)/living cells
if(sum==2||sum==3)
brr[I][j]= 1;
other
brr[I][j]= 0;
Else // dead cells
if(sum==3)
brr[I][j]= 1;
other
brr[I][j]= 0;
}
}
flag =! CellLife.same(arr,brr);
// System.out.println ("Next Generation Status:");
for(int I = 0; I<5; i++) {
for(int j = 0; j & lt5; j++) {
arr[I][j]= brr[I][j];
//system . out . print(arr[I][j]+" ");
}
//system . out . println();
}
}
System.out.println ("final status:");
for(int I = 0; I<5; i++) {
for(int j = 0; j & lt5; j++)
system . out . print(arr[I][j]+" ");
system . out . println();
}
System.out.println ("number of iterations:"+CNT);
}
public static boolean same(int[][]a,int[][] b) {
for(int I = 0; I<5; i++)
for(int j = 0; j & lt5; j++)
if(a[i][j]! =b[i][j])
Returns false
Return true
}
}
- Related articles
- Jiangsu fortune tellers _ Jiangsu fortune tellers
- Does the fortune-teller Long really tell a fortune _ Does the fortune-teller Long really tell a fortune?
- Male surname
- Finger fortune telling _ finger fortune telling
- Watch the complete HD video of Major League Baseball online for free, and seek Baidu's network disk resources.
- What are the five elements of strangers born on the evening of April 4th1982 10? What constellation?
- Yongchun fortune-telling blindness _ Who is Yongchun fortune-telling blindness?
- What is the meaning of "Peach Blossom" in Peach Blossom Luck?
- Which courier or logistics company is the cheapest to send a 20kg package from Xiao Zhao Village, Mingcun Town, pingdu city City, Shandong Province to Qufu, Shandong Province? How much will it cost?
- What is Zodiac Snake and what is Zodiac Snake?