Advertisement
raddyroro

step program

Oct 31st, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. public void step() {
  2. //Goes through and checks if the cells will be dead or alive next round.
  3. boolean[][] nextCells = new boolean[cells.length][cells[0].length];
  4. for (int row = 0; row < cells.length; row++) {
  5. for (int column = 0; column < cells[0].length; column++) {
  6. int neighborCount = 0;
  7. if (row > 0 && column > 0 && cells[row-1][column-1] == true) {//Up left
  8. neighborCount++;
  9. }
  10. if (row > 0 && cells[row-1][column] == true) { //Up
  11. neighborCount++;
  12. }
  13. if (row > 0 && column < cells[0].length-1 && cells[row-1][column+1] == true) { // up Right
  14. neighborCount++;
  15. }
  16. if (column > 0 && cells[row][column-1] == true) { //Left
  17. neighborCount++;
  18. }
  19. if (column < cells[0].length-1 && cells[row][column+1] == true) { // right
  20. neighborCount++;
  21. }
  22. if (row < cells.length-1 && column > 0 && cells[row+1][column-1] == true){ //down left
  23. neighborCount++;
  24. }
  25. if (row < cells.length-1 && cells[row+1][column] == true) { //down
  26. neighborCount++;
  27. }
  28. if (row < cells.length-1 && column < cells[0].length-1 && cells[row+1][column+1] == true) { //down right
  29. neighborCount++;
  30. }
  31. //Rules of living
  32. if (cells[row][column] == true) {//It's alive!
  33. if (neighborCount == 2 || neighborCount == 3) {
  34. nextCells[row][column] = true;//Alive next time
  35. }
  36. else {
  37. nextCells[row][column] = false;//Dead next time
  38. }
  39. }
  40. else {
  41. if (neighborCount == 3) {// I'm dead
  42. nextCells[row][column]= true;//alive next time
  43. }
  44. else {
  45. nextCells[row][column] = false; //dead next time
  46. }
  47. }
  48. }
  49. }
  50. //Set all cells to their new state, redraw the grid
  51. cells = nextCells;
  52. panel.setCells(nextCells);
  53. frame.repaint();
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement