Advertisement
Guest User

Untitled

a guest
Feb 20th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1.  
  2. public class GameOfLife
  3. {
  4. private final int GRID_SIZE = 64;
  5. public boolean [][] grid = new boolean[GRID_SIZE][GRID_SIZE];
  6. //default constructor
  7. public GameOfLife()
  8. {
  9. for(int i=0;i<GRID_SIZE;++i)
  10. {
  11. for(int j=0;j<GRID_SIZE;++j)
  12. {
  13. grid[i][j] = false;
  14. }
  15. }
  16. }
  17.  
  18. public int getGridSize()
  19. {
  20. return GRID_SIZE;
  21. }
  22.  
  23. public int getLiveNeighbors(int i,int j)
  24. {
  25. int neighbors = 0;
  26. for( int tmp_i = i-1; tmp_i <= i+1; ++tmp_i )
  27. {
  28. for( int tmp_j = j-1; tmp_j <= j+1; ++tmp_j )
  29. {
  30. if( tmp_i < 0 || tmp_i >= GRID_SIZE || tmp_j < 0 || tmp_j >= GRID_SIZE )
  31. {}
  32. else
  33. {
  34. if( grid[tmp_i][tmp_j] )
  35. {
  36. neighbors++;
  37. }
  38. }
  39. }
  40. }
  41. return neighbors;
  42. }
  43.  
  44. public void nextIteration()
  45. {
  46. boolean [][] newGrid = new boolean[GRID_SIZE][GRID_SIZE];
  47.  
  48. for(int i=0;i<GRID_SIZE;++i)
  49. {
  50. for(int j=0;j<GRID_SIZE;++j)
  51. {
  52. newGrid[i][j] = grid[i][j];
  53. }
  54. }
  55.  
  56. for( int i=0;i<GRID_SIZE;++i)
  57. {
  58. for( int j=0;j<GRID_SIZE;++j)
  59. {
  60. int my_neighbors = getLiveNeighbors(i,j);
  61. if( !newGrid[i][j] && my_neighbors == 3)
  62. {
  63. grid[i][j] = true;
  64. }
  65.  
  66. else if( newGrid[i][j] && ( my_neighbors == 2 || my_neighbors == 3 ) )
  67. {
  68. grid[i][j] = true;
  69. }
  70.  
  71. else
  72. {
  73. grid[i][j] = false;
  74. }
  75. }
  76. }
  77. System.out.println("Change of assignment");
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement