Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void step() {
- //Goes through and checks if the cells will be dead or alive next round.
- boolean[][] nextCells = new boolean[cells.length][cells[0].length];
- for (int row = 0; row < cells.length; row++) {
- for (int column = 0; column < cells[0].length; column++) {
- int neighborCount = 0;
- if (row > 0 && column > 0 && cells[row-1][column-1] == true) {//Up left
- neighborCount++;
- }
- if (row > 0 && cells[row-1][column] == true) { //Up
- neighborCount++;
- }
- if (row > 0 && column < cells[0].length-1 && cells[row-1][column+1] == true) { // up Right
- neighborCount++;
- }
- if (column > 0 && cells[row][column-1] == true) { //Left
- neighborCount++;
- }
- if (column < cells[0].length-1 && cells[row][column+1] == true) { // right
- neighborCount++;
- }
- if (row < cells.length-1 && column > 0 && cells[row+1][column-1] == true){ //down left
- neighborCount++;
- }
- if (row < cells.length-1 && cells[row+1][column] == true) { //down
- neighborCount++;
- }
- if (row < cells.length-1 && column < cells[0].length-1 && cells[row+1][column+1] == true) { //down right
- neighborCount++;
- }
- //Rules of living
- if (cells[row][column] == true) {//It's alive!
- if (neighborCount == 2 || neighborCount == 3) {
- nextCells[row][column] = true;//Alive next time
- }
- else {
- nextCells[row][column] = false;//Dead next time
- }
- }
- else {
- if (neighborCount == 3) {// I'm dead
- nextCells[row][column]= true;//alive next time
- }
- else {
- nextCells[row][column] = false; //dead next time
- }
- }
- }
- }
- //Set all cells to their new state, redraw the grid
- cells = nextCells;
- panel.setCells(nextCells);
- frame.repaint();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement