From 52a347776a97cd96131fd33a79262d3abf83e976 Mon Sep 17 00:00:00 2001 From: Mathias <mathias.faure@univ-amu.fr> Date: Tue, 6 Oct 2020 09:28:14 +0200 Subject: [PATCH] First part of the TP. --- Cell.java | 8 +++++++- GameOfLifeGUI.java | 9 +++++++-- Grid.java | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Cell.java b/Cell.java index 9a0b4bd..4ace922 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 c162ce0..ffb5d54 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 4fcdfd3..e8a18d2 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(); -- GitLab