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

/** 0 means dead, 1 means alive.

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

}

}