/* * Board.java * * Created on 14. oktober 2006, 16:35 * */ package gameoflife; import java.util.Observable; public class Board extends Observable { private static final int SIZE = 50; private boolean[][] currentBoard = new boolean[SIZE][SIZE]; private boolean[][] nextBoard = new boolean[SIZE][SIZE]; /** Creates a new instance of Board */ public Board() { } /** Return the current board */ public boolean[][] getCurrentBoard() { return currentBoard; } /** Swap the current board with the newly calculated next board */ public void swapCurrentAndNextBoard() { for(int i = 0; i= 1){ if (currentBoard[i][j-1]){ numberOfNeighbours++; } } // ...then the cell on the top-left if (i >= 1 && j >= 1){ if (currentBoard[i-1][j-1]){ numberOfNeighbours++; } } // ...the cell above if (i >= 1) { if (currentBoard[i-1][j]){ numberOfNeighbours++; } } // ...the cell on the top-right if ((j <= SIZE-1) && (i>=1)){ if (currentBoard[i-1][j+1]){ numberOfNeighbours++; } } // ...the cell to the right if (j <= SIZE-1){ if (currentBoard[i][j+1]){ numberOfNeighbours++; } } // ...the cell on the bottom-right if ((i <= SIZE-1) && j <= SIZE-1){ if (currentBoard[i+1][j+1]){ numberOfNeighbours++; } } // ...the cell below if (i <= SIZE-1){ if (currentBoard[i+1][j]){ numberOfNeighbours++; } } // ...the cell on the bottom-left if (i <= SIZE-1 && j >= 1){ if (currentBoard[i+1][j-1]){ numberOfNeighbours++; } } // If the cell has zero or one neighbour it dies // ...it also dies if it has more than for neighbours if ((numberOfNeighbours == 0) || (numberOfNeighbours == 1) || (numberOfNeighbours >= 4)){ nextBoard[i][j] = false; } // If the cell has to or three neigbours it survives to next iteration if ((numberOfNeighbours == 2) || (numberOfNeighbours == 3)) nextBoard[i][j] = true; //System.out.println("Cell was alive. Cell number i: " +i + "j: " + j + " Number of neighbours is: " +numberOfNeighbours); } // If the cell is dead we check if it should be waked else if (!currentBoard[i][j]) { numberOfNeighbours = 0; //System.out.println("Er i elseif. I: " +i+ " J:" +j); // Count the number of neighbours // ...first the cell to the left if (j >= 1){ if (currentBoard[i][j-1]){ numberOfNeighbours++; } } // ...then the cell on the top-left if (i >= 1 && j >= 1){ if (currentBoard[i-1][j-1]){ numberOfNeighbours++; } } // ...the cell above if (i >= 1) { if (currentBoard[i-1][j]){ numberOfNeighbours++; } } // ...the cell on the top-right if ((j <= SIZE-1) && (i>=1)){ if (currentBoard[i-1][j+1]){ numberOfNeighbours++; } } // ...the cell to the right if (j <= SIZE-1){ if (currentBoard[i][j+1]){ numberOfNeighbours++; } } // ...the cell on the bottom-right if ((i <= SIZE-1) && j <= SIZE-1){ if (currentBoard[i+1][j+1]){ numberOfNeighbours++; } } // ...the cell below if (i <= SIZE-1){ if (currentBoard[i+1][j]){ numberOfNeighbours++; } } // ...the cell on the bottom-left if (i <= SIZE-1 && j >= 1){ if (currentBoard[i+1][j-1]){ numberOfNeighbours++; } } if (numberOfNeighbours == 3){ nextBoard[i][j] = true; } else{ nextBoard[i][j] = false; } } } } swapCurrentAndNextBoard(); } /** Wakes a cell (because of user interaction with the associated GUI */ public void wakeCell(int row, int col) { currentBoard[row][col] = true; } /** Burys a cell (because of user interaction with the associated GUI */ public void buryCell(int row, int col) { currentBoard[row][col] = false; } public boolean aliveCell(int row, int col) { if(currentBoard[row][col]){ return true; } else return false; } }