diff --git a/Cell.java b/Cell.java index 9a0b4bd6665f70adc5b71de952f25e175b04b0a6..4ace9225317aa37f0c574e16be238c83d0d0ca64 100644 --- a/Cell.java +++ b/Cell.java @@ -5,10 +5,16 @@ public class Cell { private boolean isAlive; + private String color = "Blue"; + public Cell(){ this.isAlive = false; } - + + public void setColor(String hisColor) { color = hisColor; } + + public String getColor() { return color; } + /** * Determines whether this {@link Cell} is alive or not. * diff --git a/GameOfLifeGUI.java b/GameOfLifeGUI.java index c162ce018cddf7f4a3d1d1290a62d4525f4312d9..ffb5d5475c90a8a69a9fc9ac281feb5820e30cfd 100644 --- a/GameOfLifeGUI.java +++ b/GameOfLifeGUI.java @@ -39,8 +39,13 @@ 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).getColor()).equals("Red")) { + label.setForeground(Color.red); + } else { + label.setForeground(Color.blue); + } + } else label.setForeground(Color.white); } diff --git a/Grid.java b/Grid.java index 4fcdfd35f888d15d317acf1084ef9bd49cb7cd26..e8a18d2c90be38b55ed63e13ed75523305606d44 100644 --- a/Grid.java +++ b/Grid.java @@ -119,7 +119,12 @@ public class Grid implements Iterable<Cell> { } private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { - return cell.isAliveInNextState(this.countAliveNeighbours(rowIndex, columnIndex)); + boolean Nextstate = cell.isAliveInNextState(this.countAliveNeighbours(rowIndex, columnIndex)); + if (Nextstate) { + cell.setColor(this.countColorNeighbours(rowIndex, columnIndex)); + } + + return Nextstate; } private int countAliveNeighbours(int rowIndex, int columnIndex) { @@ -132,6 +137,31 @@ public class Grid implements Iterable<Cell> { return CounterAliveCell; } + private String countColorNeighbours(int rowIndex, int columnIndex) { + String CounterColorCell; + int Red = 0; + int Blue = 0; + for (Cell NeighboursCell : this.getNeighbours(rowIndex, columnIndex)) { + if (NeighboursCell.isAlive()) { + if ((NeighboursCell.getColor()).equals("Red")) { + Red+=1; + } + else { + Blue+=1; + } + } + } + + if (Red>=Blue) { + CounterColorCell="Red"; + } + else { + CounterColorCell="Blue"; + } + + return CounterColorCell; + } + private List<Cell> getNeighbours(int rowIndex, int columnIndex) { List<Cell> Neighbours = new ArrayList<>(); @@ -167,10 +197,18 @@ public class Grid implements Iterable<Cell> { */ void randomGeneration(Random random) { + for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) { Cell cell = it.next(); + if (random.nextBoolean()) { cell.setAlive(); + if (random.nextBoolean()) { + cell.setColor("Red"); + } + else { + cell.setColor("Blue"); + } } else { cell.setDead();