Eclipse999 - 4 months ago 22
Java Question

# Conways Game of Life trouble [java]

``````public static boolean[][] calculate(boolean cellState[][])//imports cell patern(in the form of a graph)
{
int x = 0, y = 0;
int alive = 0;
boolean newCellState[][] = new boolean[50][50];//what the next generation is stored in

while(x < 50 && y < 50)//calculation loop
{
{
if(cellState[x-1][y] == true)
alive++; //if the  adjacent cell is alive the alive variable gets + 1
}catch(IndexOutOfBoundsException e) {}

try
{
if (cellState[x-1][y-1] == true)
alive++;
} catch(IndexOutOfBoundsException e) { }

try
{
if(cellState[x-1][y+1] == true)
alive++;
} catch(IndexOutOfBoundsException e) { }

try
{
if (cellState[x][y+1] == true)
alive++;
} catch(IndexOutOfBoundsException e) { }

try
{
if (cellState[x][y-1] == true)
alive++;
} catch(IndexOutOfBoundsException e) { }

try
{
if(cellState[x+1][y] == true)
alive++;
} catch(IndexOutOfBoundsException e) { }

try
{
if(cellState[x+1][y-1] == true)
alive++;
} catch(IndexOutOfBoundsException e) { }

try
{
if(cellState[x+1][y+1] == true)
alive++;
} catch(IndexOutOfBoundsException e) { }

if(cellState[x][y] = true) //determines if the cell should be alive in the next generation
{
if(alive < 2)
newCellState[x][y] = false;
else if(alive > 3)
newCellState[x][y] = false;
else if(alive == 2)
newCellState[x][y] = true;//stores in new cell state for next gen
else
newCellState[x][y] = true;
} else
{
newCellState[x][y] = false;
}

alive = 0; //resets alive

if(x == 49)//counter for graph
{
y++;
x=0;
}else
{
x++;
}
}

return newCellState;
}
``````

This is my method that is used to calculate the next generation of Conway's game of life. Each time it is run, no matter what the pattern is, it prints out every cell as dead except the edges of the grid.

This is the main method:

``````public static void main(String Args[])
{
int x = 0;
int y = 0;
boolean cellState[][] = new boolean[50][50];
Scanner input = new Scanner(System.in);

while(true)
{
System.out.print("\n Type anything for next generation, 'new' for new grid, or 'stop' to end>> ");
{
cellState = newCells();
{
break;
}else
{
cellState = calculate(cellState);
print(cellState);
}

}

}
``````

I have tested the printing method so it doesn't have a problem there. I just can't figure out why it does this. All help is appreciated! Thanks!

``````if(cellState[x][y] = true)
You forgot to use `==` for this one. Otherwise, your code looks fine.