diff --git a/GameOfLife.java b/GameOfLife.java index 1d5bb85f326f29587b01ae16657bada3ef23b191..b8bb5fd3a954442218cf9ccefe11020fa6eb8627 100644 --- a/GameOfLife.java +++ b/GameOfLife.java @@ -16,8 +16,8 @@ public class GameOfLife { * @throws NullPointerException if {@code grid} is {@code null} */ public GameOfLife(Grid grid) { - // this.grid = requireNonNull(grid, "grid is null"); - this.grid = grid; + // this.grid = requireNonNull(grid, "grid is null"); + this.grid = grid; grid.randomGeneration(random); } @@ -26,7 +26,7 @@ public class GameOfLife { */ public void next() { grid.nextGeneration(); - generationNumber++; + generationNumber++; } @@ -52,19 +52,19 @@ public class GameOfLife { * Plays the game. */ public void play(int maxGenerations) { - for(int generation =0; generation < maxGenerations; generation++){ - this.next(); - System.out.println("generation : " + generation); - } - - + for(int generation =0; generation < maxGenerations; generation++){ + this.next(); + System.out.println("generation : " + generation); + } + + } /** * Pauses the game. */ public void pause() { - // timeline.pause(); + // timeline.pause(); } /** @@ -73,7 +73,7 @@ public class GameOfLife { public void clear() { pause(); grid.clear(); - generationNumber = 0; + generationNumber = 0; } /** @@ -84,4 +84,4 @@ public class GameOfLife { grid.randomGeneration(random); } -} +} \ No newline at end of file diff --git a/GameOfLifeGUI.java b/GameOfLifeGUI.java index c162ce018cddf7f4a3d1d1290a62d4525f4312d9..bb17d5d5f4325b3038fc00d6156c60a89407e390 100644 --- a/GameOfLifeGUI.java +++ b/GameOfLifeGUI.java @@ -10,39 +10,52 @@ public class GameOfLifeGUI extends JFrame { private GridLayout gridLayout; private JPanel gridPanel; private JFrame frame; - + public GameOfLifeGUI(Grid g) { - this.numberOfRows = g.getNumberOfRows(); - this.numberOfColumns = g.getNumberOfColumns(); - gridLayout = new GridLayout(numberOfRows, numberOfColumns); + this.numberOfRows = g.getNumberOfRows(); + this.numberOfColumns = g.getNumberOfColumns(); + gridLayout = new GridLayout(numberOfRows, numberOfColumns); gridPanel = new JPanel(gridLayout); - labelGrid = new JLabel[numberOfRows][numberOfColumns]; - for (int x = 0; x < numberOfColumns; x++) - for (int y = 0; y < numberOfRows; y++){ - labelGrid[x][y] = new JLabel("*"); - JLabel label; - if(g.getCell(x,y).isAlive()) - labelGrid[x][y].setForeground(Color.red); - else - labelGrid[x][y].setForeground(Color.white); - gridPanel.add(labelGrid[x][y]); - } + labelGrid = new JLabel[numberOfRows][numberOfColumns]; + for (int x = 0; x < numberOfColumns; x++) { + for (int y = 0; y < numberOfRows; y++) { + labelGrid[x][y] = new JLabel("*"); + JLabel label; + if (g.getCell(x, y).isAlive()) { + if (g.getCell(x, y).isRed()) { + labelGrid[x][y].setForeground(Color.red); + } else { + labelGrid[x][y].setForeground(Color.blue); + } + } else { + labelGrid[x][y].setForeground(Color.white); + } + gridPanel.add(labelGrid[x][y]); + } + } + frame = new JFrame("Game of Life"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setContentPane(gridPanel); - frame.setSize(squareSize * numberOfRows, squareSize * numberOfColumns); - frame.setLocationByPlatform(true); + frame.setSize(squareSize * numberOfRows, squareSize * numberOfColumns); + frame.setLocationByPlatform(true); frame.setVisible(true); } - public void update(Grid g){ - for (int x = 0; x < numberOfColumns; x++) - for (int y = 0; y < numberOfRows; y++){ - JLabel label = labelGrid[x][y]; - if(g.getCell(x,y).isAlive()) - label.setForeground(Color.red); - else - label.setForeground(Color.white); - } + public void update(Grid g) { + for (int x = 0; x < numberOfColumns; x++) { + for (int y = 0; y < numberOfRows; y++) { + JLabel label = labelGrid[x][y]; + if (g.getCell(x, y).isAlive()) { + if (g.getCell(x, y).isBlue()) { + label.setForeground(Color.blue); + } else { + label.setForeground(Color.red); + } + } else { + label.setForeground(Color.white); + } + } + } } } diff --git a/Grid.java b/Grid.java index 939e05fd8e4207e92eb7ffaedebfcd6d4d0f6d0b..fe65fffa7e2694cb06a342db71f9df365c543cf2 100644 --- a/Grid.java +++ b/Grid.java @@ -1,4 +1,4 @@ -import java.util.Arrays; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; @@ -98,29 +98,110 @@ public class Grid implements Iterable<Cell> { } private boolean[][] calculateNextStates() { - return null; + boolean[][] states = new boolean[numberOfRows][numberOfColumns]; + for (int i = 0; i < numberOfRows; i++) { + for (int j = 0; j < numberOfColumns; j++) { + states[i][j] = calculateNextState(i, j, cells[i][j]); + } + } + + return states; } private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { - return false; + int neighboursAlive = countAliveNeighbours(rowIndex, columnIndex); + int blueNeighbours = countBlueNeighbours(rowIndex, columnIndex); + boolean nextStateAlive = cell.isAlive(); + if (cell.isAlive()) { + if (neighboursAlive == 2) { + nextStateAlive = true; + } + else if (neighboursAlive == 3) { + nextStateAlive = true; + } + else { + cell.isDead(); + nextStateAlive = false; + } + } + else if (cell.isDead()) { + if (neighboursAlive == 3) { + cell.isAlive(); + nextStateAlive = true; + if (blueNeighbours >= 2) { + cell.setBlue(); + } + else { + cell.setRed(); + } + } + else { + nextStateAlive = false; + } + } + + + return nextStateAlive; } private int countAliveNeighbours(int rowIndex, int columnIndex) { - return 0; + int nbAlive = 0; + for (Cell cell : getNeighbours(rowIndex, columnIndex)) { + nbAlive += cell.isAlive() ? 1 : 0; + } + return nbAlive; + } + + private int countBlueNeighbours(int rowIndex, int columnIndex) { + int nbBlue = 0; + for (Cell cell : getNeighbours(rowIndex, columnIndex)) { + if (cell.isAlive()) { + nbBlue += cell.isBlue() ? 1 : 0; + + } + } + return nbBlue; } private List<Cell> getNeighbours(int rowIndex, int columnIndex) { - return null; + List<Cell> neighbourCells = new ArrayList<Cell>(); + neighbourCells.add(cells[rowIndex - 1 < 0 ? numberOfRows - 1 : rowIndex - 1][columnIndex - 1 < 0 ? numberOfColumns - 1 : columnIndex - 1]); + neighbourCells.add(cells[rowIndex][columnIndex - 1 < 0 ? numberOfColumns - 1 : columnIndex - 1]); + neighbourCells.add(cells[rowIndex + 1 > numberOfRows - 1 ? 0 : rowIndex + 1][columnIndex - 1 < 0 ? numberOfColumns - 1 : columnIndex - 1]); + + neighbourCells.add(cells[rowIndex - 1 < 0 ? numberOfRows - 1 : rowIndex - 1][columnIndex]); + neighbourCells.add(cells[rowIndex + 1 > numberOfRows - 1 ? 0 : rowIndex + 1][columnIndex]); + + neighbourCells.add(cells[rowIndex - 1 < 0 ? numberOfRows - 1 : rowIndex - 1][columnIndex + 1 > numberOfColumns - 1 ? 0 : columnIndex + 1]); + neighbourCells.add(cells[rowIndex][columnIndex + 1 > numberOfColumns - 1 ? 0 : columnIndex + 1]); + neighbourCells.add(cells[rowIndex + 1 > numberOfRows - 1 ? 0 : rowIndex + 1][columnIndex + 1 > numberOfColumns - 1 ? 0 : columnIndex + 1]); + + return neighbourCells; } private void goToNextState(boolean[][] nextState) { + for (int i = 0; i < numberOfRows; i++) { + for (int j = 0; j < numberOfColumns; j++) { + if (nextState[i][j] == true) { + cells[i][j].setAlive(); + } else { + cells[i][j].setDead(); + } + } + } + } /** * Sets all {@link Cell}s in this {@code Grid} as dead. */ void clear() { + for (int i = 0; i < numberOfRows; i++) { + for (int j = 0; j < numberOfColumns; j++) { + cells[i][j].setDead(); + } + } } /** @@ -129,8 +210,23 @@ public class Grid implements Iterable<Cell> { * @param random {@link Random} instance used to decide if each {@link Cell} is alive or dead * @throws NullPointerException if {@code random} is {@code null} */ - + void randomGeneration(Random random) { + for (int i = 0; i < numberOfRows; i++) { + for (int j = 0; j < numberOfColumns; j++) { + if (random.nextBoolean()) { + cells[i][j].setAlive(); + if (random.nextBoolean()) { + cells[i][j].setBlue(); + } else { + cells[i][j].setRed(); + } + } else { + cells[i][j].setDead(); + + } + } + } } } diff --git a/GridIterator.java b/GridIterator.java index 19fc9e0f9169cd236e9195989a90ecdf71e3a91c..160f830391f3e574b63175f0616a093f8f45df8c 100644 --- a/GridIterator.java +++ b/GridIterator.java @@ -28,4 +28,4 @@ public class GridIterator implements Iterator<Cell> { } return result; } -} +} \ No newline at end of file diff --git a/Main.java b/Main.java index bf344eedcdd0bf23ac4a256c6e08c5b4037f5117..85e23c8e67b78c6d0a176945fab919bf1567273b 100644 --- a/Main.java +++ b/Main.java @@ -3,23 +3,24 @@ import java.awt.*; import javax.swing.*; public class Main{ - public static void main(String args[]) throws IOException { - int NUMBER_OF_ROWS = 64; - int NUMBER_OF_COLUMNS = 64; - - GameOfLife gameOfLife = new GameOfLife(new Grid(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS)); - - GameOfLifeGUI gui = new GameOfLifeGUI(gameOfLife.getGrid()); + public static void main(String args[]) throws IOException { + int NUMBER_OF_ROWS = 64; + int NUMBER_OF_COLUMNS = 64; - for(int generation =0; generation < 1000; generation++){ - try { - Thread.sleep(100); - } catch (InterruptedException ie) { + GameOfLife gameOfLife = new GameOfLife(new Grid(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS)); + + GameOfLifeGUI gui = new GameOfLifeGUI(gameOfLife.getGrid()); + + for(int generation =0; generation < 1000; generation++){ + try { + Thread.sleep(100); + } catch (InterruptedException ie) { + + } + + gameOfLife.next(); + gui.update(gameOfLife.getGrid()); + } - } - gameOfLife.next(); - gui.update(gameOfLife.getGrid()); } - - } }