Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GameOfLife
- {
- private final int GRID_SIZE = 64;
- public boolean [][] grid = new boolean[GRID_SIZE][GRID_SIZE];
- //default constructor
- public GameOfLife()
- {
- for(int i=0;i<GRID_SIZE;++i)
- {
- for(int j=0;j<GRID_SIZE;++j)
- {
- grid[i][j] = false;
- }
- }
- }
- public int getGridSize()
- {
- return GRID_SIZE;
- }
- public int getLiveNeighbors(int i,int j)
- {
- int neighbors = 0;
- for( int tmp_i = i-1; tmp_i <= i+1; ++tmp_i )
- {
- for( int tmp_j = j-1; tmp_j <= j+1; ++tmp_j )
- {
- if( tmp_i < 0 || tmp_i >= GRID_SIZE || tmp_j < 0 || tmp_j >= GRID_SIZE )
- {}
- else
- {
- if( grid[tmp_i][tmp_j] )
- {
- neighbors++;
- }
- }
- }
- }
- return neighbors;
- }
- public void nextIteration()
- {
- boolean [][] newGrid = new boolean[GRID_SIZE][GRID_SIZE];
- for(int i=0;i<GRID_SIZE;++i)
- {
- for(int j=0;j<GRID_SIZE;++j)
- {
- newGrid[i][j] = grid[i][j];
- }
- }
- for( int i=0;i<GRID_SIZE;++i)
- {
- for( int j=0;j<GRID_SIZE;++j)
- {
- int my_neighbors = getLiveNeighbors(i,j);
- if( !newGrid[i][j] && my_neighbors == 3)
- {
- grid[i][j] = true;
- }
- else if( newGrid[i][j] && ( my_neighbors == 2 || my_neighbors == 3 ) )
- {
- grid[i][j] = true;
- }
- else
- {
- grid[i][j] = false;
- }
- }
- }
- System.out.println("Change of assignment");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement