The rules of the game in my book are that if there is a dead cell and 3 live ones surrounding it, then that dead cell becomes alive. If there is a cell that is alive and it has one live neighbor, it dies. If there is a live cell with 3 or more cells surrounding, then it dies.
here is my extremely long and stupid code.
public class Life{
public static void main(String[] args){
// BEGIN Random matrix
int nCols =10;
int nRows =10;
int generationNumber = 1;
int[][] cells = new int[nCols][nRows];
int[][] Ncount = new int[nCols][nRows];
int[][] newCells = new int[nCols][nRows];
sneeze(cells);
for(int i = 0;i<8;i++){
System.out.println("The " + i + " Generation is :"+"\n");
printArray(cells);
countNeighbors(cells,Ncount);
System.out.println("The Neighbor Count is:"+"\n");
printArray(Ncount);
death(cells,Ncount);
}
}
public static void setZeroes(int x[][]){
for(int i = 0;i<x.length;i++){
for(int j = 0;j<x[0].length;j++){
x[i][j] = 0;
}
}
}
public static void sneeze(int x[][]){
x[1][1]=1;
x[1][2]=1;
x[2][1]=1;
x[2][2]=1;
x[3][3]=1;
x[5][5]=1;
x[5][6]=1;
x[5][7]=1;
for(int i = 0;i<x.length;i++){
for(int j = 0;j<x[0].length;j++){
}
}
}
public static void countNeighbors(int cellx[][],int nbrcountX[][]){
setZeroes(nbrcountX);
for(int i = 1;i<cellx.length-1;i++){
for(int j = 1;j<cellx[0].length-1;j++){
if (cellx[i][j] == 1){
nbrcountX[i-1][j-1] ++;
nbrcountX[i-1][j] ++;
nbrcountX[i-1][j+1] ++;
nbrcountX[i][j+1] ++;
nbrcountX[i+1][j+1] ++;
nbrcountX[i+1][j] ++;
nbrcountX[i+1][j-1] ++;
nbrcountX[i][j-1] ++;
}
//TOP LEFT CORNER
if (cellx[0][0]==1){
nbrcountX[1][0]++;
nbrcountX[1][1]++;
nbrcountX[0][1]++;
}
//BOTTOM LEFT CORNER
if (cellx[9][0]==1){
nbrcountX[8][0]++;
nbrcountX[8][1]++;
nbrcountX[9][1]++;
}
//TOP RIGHT CORNER
if (cellx[0][9]==1){
nbrcountX[0][8]++;
nbrcountX[1][9]++;
nbrcountX[1][8]++;
}
//BOTTOM RIGHT CORNER
if (cellx[9][9]==1){
nbrcountX[8][9]++;
nbrcountX[9][8]++;
nbrcountX[8][8]++;
}
}
}
//TOP <------PROBLEM AREA FROM HERE TO RIGHT SIDE
for (int i=1; i < 9; i++){
if(cells[i][j]==1)
nbrcount[i-1][j];
if(cells[i][j]==1)
nbrcount[i-1][j+1];
if(cells[i][j]==1)
nbrcount[i][j+1];
if (cells[i][j]==1)
nbrcount[i+1][j+1];
if (cells[i][j]==1)
nbrcount[i+1][j];
}
//LEFT SIDE
for(int i=1; i < 9; i++){
if(cells[i][j]==1)
nbrcount[i][j+1];
if(cells[i][j]==1)
nbrcount[i+1][j+1];
if(cells[i][j]==1)
nbrcount[i+1][j];
if(cells[i][j]==1)
nbrcount[i+1][j-1];
if(cells[i][j]==1)
nbrcount[i][j-1];
}
//BOTTOM SIDE
for(int i=1; i < 9; i++){
if(cells[i][j]==1)
nbrcount[i-1][j];
if(cells[i][j]==1)
nbrcount[i-1][j-1];
if(cells[i][j]==1)
nbrcount[i][j-1];
if(cells[i][j]==1)
nbrcount[i+1][j-1];
if(cells[i][j]==1)
nbrcount[i+1][j];
}
//RIGHT SIDE
for(int i=1; i < 9; i++){
if(cells[i][j]==1)
nbrcount[i][j-1];
if(cells[i][j]==1)
nbrcount[i-1][j-1];
if(cells[i][j]==1)
nbrcount[i-1][j];
if(cells[i][j]==1)
nbrcount[i-1][j+1];
if(cells[i][j]==1)
nbrcount[i][j+1];
}
}
public static void death(int cells [][], int nbrCount[][]){
for(int j=0;j<nbrCount.length;j++){
for(int i=0;i<nbrCount[0].length;i++){
if(nbrCount[i][j]==1&&cells[i][j]==1){
cells[i][j]=0;
}
if(nbrCount[i][j]>3&&cells[i][j]==1){
cells[i][j]=0;
}
if(nbrCount[i][j]==3&&cells[i][j]==0){
cells[i][j]=1;
}
if(nbrCount[i][j]==2&&cells[i][j]==1){
cells[i][j]=1;
}
if(nbrCount[i][j]==3&&cells[i][j]==1){
cells[i][j]=1;
}
}}}
public static void printArray(int b[][]){
for(int j=0;j<b.length;j++){
for(int i=0;i<b[0].length;i++){
System.out.print(b[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
}

New Topic/Question
Reply



MultiQuote




|