diff --git a/Cell.java b/Cell.java index 9a0b4bd6665f70adc5b71de952f25e175b04b0a6..05af5cf36acb6ff831059befc895fb0677ff3a45 100644 --- a/Cell.java +++ b/Cell.java @@ -4,9 +4,11 @@ public class Cell { private boolean isAlive; + private boolean isRed; public Cell(){ this.isAlive = false; + this.isRed = false; } /** @@ -19,6 +21,8 @@ public class Cell { return this.isAlive; } + public boolean isRed() { return this.isRed; } + /** * Determines whether this {@link Cell} is dead or not. * @@ -39,6 +43,9 @@ public class Cell { this.isAlive = true; } + public void setBlue() { isRed = false; } + public void setRed() { isRed = true; } + /** * Sets the state of this {@link Cell} to dead. * diff --git a/GameOfLifeGUI.java b/GameOfLifeGUI.java index c162ce018cddf7f4a3d1d1290a62d4525f4312d9..bd7d040ece4f038416e2eca0112fd323802b24a4 100644 --- a/GameOfLifeGUI.java +++ b/GameOfLifeGUI.java @@ -39,8 +39,12 @@ public class GameOfLifeGUI extends JFrame { 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); + if(g.getCell(x,y).isAlive()) { + if (g.getCell(x, y).isRed()) + label.setForeground(Color.red); + else + label.setForeground(Color.blue); + } else label.setForeground(Color.white); } diff --git a/Grid.java b/Grid.java index 2567832e4d92dbb6764925642689508a6dbbdd32..ee2d08406cc5dd9d7bac93b74e6f948336b64cd3 100644 --- a/Grid.java +++ b/Grid.java @@ -109,6 +109,10 @@ public class Grid implements Iterable<Cell> { if (cell.isAlive()) { return aliveNeighbours >= 2 && aliveNeighbours <= 3; } + if (willBeRed(rowIndex, columnIndex)) + getCell(rowIndex, columnIndex).setRed(); + else + getCell(rowIndex, columnIndex).setBlue(); return aliveNeighbours == 3; } @@ -165,10 +169,27 @@ public class Grid implements Iterable<Cell> { void randomGeneration(Random random) { GridIterator iterator = new GridIterator(this); while (iterator.hasNext()) { - if (random.nextBoolean()) - iterator.next().setAlive(); + Cell cell = iterator.next(); + if (random.nextBoolean()) { + cell.setAlive(); + if (random.nextBoolean()) + cell.setRed(); + else + cell.setBlue(); + } else - iterator.next().setDead(); + cell.setDead(); } } + + private boolean willBeRed(int rowIndex, int cocolumnIndex) { + List<Cell> cells = getNeighbours(rowIndex, cocolumnIndex); + int redCells = 0; + for (Cell cell : cells) { + if (cell.isAlive() && cell.isRed()) + redCells++; + } + return redCells >= 2; + } + } diff --git a/out/production/tp3/Cell.class b/out/production/tp3/Cell.class index f25b7df31b6ba9bd790deb4f3f8a3631cf724d96..fc3dbe93e1e0f97985ae8a20c4e0e52d6780bc1a 100644 Binary files a/out/production/tp3/Cell.class and b/out/production/tp3/Cell.class differ diff --git a/out/production/tp3/GameOfLifeGUI.class b/out/production/tp3/GameOfLifeGUI.class index 34b3b75fedc0ad93a1ffac7106e0a17cd2618c0c..a8ecd1c3d3efaf7aeeb4ed0cc3acd1e31b8b6cab 100644 Binary files a/out/production/tp3/GameOfLifeGUI.class and b/out/production/tp3/GameOfLifeGUI.class differ diff --git a/out/production/tp3/Grid.class b/out/production/tp3/Grid.class index e700d5d726f22901bb94e808d42c7e2b105862f1..4e975479689867b0cf3bdc93bb0678ac8419c48f 100644 Binary files a/out/production/tp3/Grid.class and b/out/production/tp3/Grid.class differ