Hi everyone I am trying to simulate a simple Conway's game of life, these are the rules of the game:
Any live cell with fewer than two live neighbours dies, as if caused by under-population. Any live cell with two or three live neighbours lives on to the next generation. Any live cell with more than three live neighbours dies, as if by overcrowding. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
Now the problem is that I need to modify my actual grid in order to print it and then re-change it as its previously value before any rules is applied, however I cannot find any system to accomplish this task, I hope someone can help me. Here my code:
import java.util.Scanner;
import java.io.*;
class LifeGrid
{
private int[][] grid, newGrid;
private int generation = 0;
public LifeGrid(int x, int y, String filename) throws FileNotFoundException
{
grid = new int[x][y];
newGrid = new int[x][y];
int j = 0;
Scanner scanner = new Scanner(new File(filename));
while(scanner.hasNextLine() && j < x)
{
String line = scanner.nextLine();
for(int i=0; i<line.length() && i<y; i++)
{
if(line.charAt(i) == '*')
grid[j][i] = 1;
else
grid[j][i] = 0;
}
j++;
}
scanner.close();
}
public void show()
{
for(int i=0; i<grid.length; i++)
{
for(int j=0; j<grid[i].length; j++)
{
if(grid[i][j] == 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
System.out.println("Generation:" + generation);
}
//Getter methods
public int getWidth() { return grid[0].length; }
public int getHeight() { return grid.length; }
public int getGeneration() { return this.generation; }
public int getCell(int x, int y) { return grid[x][y]; }
public static void main(String[] args)throws FileNotFoundException
{
LifeGrid life = new LifeGrid(6, 10, args[0]);
life.run();
}
//Check neighbours
public int neighbours(int x, int y)
{
int neighbours = 0;
if(x == 0 && y == 0)
{
if(grid[x][y+1] == 1) {neighbours++;}
if(grid[x+1][y] == 1) {neighbours++;}
if(grid[x+1][y+1] == 1) {neighbours++;}
}
else if(x == 0 && y >= 1 && y < getWidth() -1)
{
if(grid[x][y+1] == 1) {neighbours++;}
if(grid[x][y-1] == 1) {neighbours++;}
if(grid[x+1][y] == 1) {neighbours++;}
if(grid[x+1][y+1] == 1) {neighbours++;}
if(grid[x+1][y-1] == 1) {neighbours++;}
}
else if(x >= 1 && x < getHeight() -1 && y == 0)
{
if(grid[x][y+1] == 1) {neighbours++;}
if(grid[x+1][y] == 1) {neighbours++;}
if(grid[x+1][y+1] == 1) {neighbours++;}
if(grid[x-1][y+1] == 1) {neighbours++;}
}
else if(x == getHeight()-1 && y >= 1 && y < getWidth() - 1)
{
if(grid[x][y+1] == 1) {neighbours++;}
if(grid[x][y-1] == 1) {neighbours++;}
if(grid[x-1][y-1] == 1) {neighbours++;}
if(grid[x-1][y+1] == 1) {neighbours++;}
}
else if(x >=1 && x < getHeight() - 1 && y == getWidth()-1 )
{
if(grid[x][y-1] == 1) {neighbours++;}
if(grid[x+1][y] == 1) {neighbours++;}
if(grid[x+1][y-1] == 1) {neighbours++;}
if(grid[x-1][y-1] == 1) {neighbours++;}
}
else if(x == 0 && y == getWidth()-1 )
{
if(grid[x][y-1] == 1) {neighbours++;}
if(grid[x+1][y] == 1) {neighbours++;}
if(grid[x+1][y-1] == 1) {neighbours++;}
}
else if(x == getHeight()-1 && y == 0)
{
if(grid[x-1][y] == 1) {neighbours++;}
if(grid[x][y+1] == 1) {neighbours++;}
if(grid[x-1][y+1] == 1) {neighbours++;}
}
else if(x == getHeight()-1 && y == getWidth()-1)
{
if(grid[x][y-1] == 1) {neighbours++;}
if(grid[x-1][y] == 1) {neighbours++;}
if(grid[x-1][y-1] == 1) {neighbours++;}
}
else
{
if(grid[x][y+1] == 1) {neighbours++;}
if(grid[x][y-1] == 1) {neighbours++;}
if(grid[x+1][y] == 1) {neighbours++;}
if(grid[x+1][y+1] == 1) {neighbours++;}
if(grid[x+1][y-1] == 1) {neighbours++;}
if(grid[x-1][y-1] == 1) {neighbours++;}
if(grid[x-1][y+1] == 1) {neighbours++;}
}
return neighbours;
}
public void run()
{
int n;
int[][] old;
for(int i=0; i<grid.length; i++)
{
for(int j=0; j<grid[i].length; j++)
{
n = neighbours(i,j);
if(grid[i][j] == 1)
{
if(n < 2 || n > 3) {generation = 0;}
if(n == 2 || n == 3) {generation = 1;}
}
else
{
if(n == 3) {generation = 1;}
else {generation = 0;}
}
if(generation == 1)
{
old = grid;
newGrid[i][j] = 1;
grid = newGrid;
show();
grid = old;
}
else
{
old = grid;
newGrid[i][j] = 0;
grid = newGrid;
show();
grid = old;
}
}
}
}
}
File:
* * *
Expected output:
*
*
*
The new run() method is:
public void run()
{
int n;
for(int i=0; i<grid.length; i++)
{
for(int j=0; j<grid[i].length; j++)
{
n = neighbours(i,j);
if(grid[i][j] == 1)
{
if(n < 2 || n > 3) {generation = 0;}
if(n == 2 || n == 3) {generation = 1;}
}
else
{
if(n == 3) {generation = 1;}
else {generation = 0;}
}
newGrid[i][j] = generation;
}
}
grid = newGrid.clone();
show();
}
Now instead of getting as output:
*
*
*
I get:
*
*
Can someone help me to figure out why?