Skip to content
Snippets Groups Projects
Commit 52a34777 authored by Mathias's avatar Mathias
Browse files

First part of the TP.

parent b6abba86
No related branches found
No related tags found
No related merge requests found
...@@ -5,10 +5,16 @@ ...@@ -5,10 +5,16 @@
public class Cell { public class Cell {
private boolean isAlive; private boolean isAlive;
private String color = "Blue";
public Cell(){ public Cell(){
this.isAlive = false; this.isAlive = false;
} }
public void setColor(String hisColor) { color = hisColor; }
public String getColor() { return color; }
/** /**
* Determines whether this {@link Cell} is alive or not. * Determines whether this {@link Cell} is alive or not.
* *
......
...@@ -39,8 +39,13 @@ public class GameOfLifeGUI extends JFrame { ...@@ -39,8 +39,13 @@ public class GameOfLifeGUI extends JFrame {
for (int x = 0; x < numberOfColumns; x++) for (int x = 0; x < numberOfColumns; x++)
for (int y = 0; y < numberOfRows; y++){ for (int y = 0; y < numberOfRows; y++){
JLabel label = labelGrid[x][y]; JLabel label = labelGrid[x][y];
if(g.getCell(x,y).isAlive()) if(g.getCell(x,y).isAlive()) {
if ((g.getCell(x,y).getColor()).equals("Red")) {
label.setForeground(Color.red); label.setForeground(Color.red);
} else {
label.setForeground(Color.blue);
}
}
else else
label.setForeground(Color.white); label.setForeground(Color.white);
} }
......
...@@ -119,7 +119,12 @@ public class Grid implements Iterable<Cell> { ...@@ -119,7 +119,12 @@ public class Grid implements Iterable<Cell> {
} }
private boolean calculateNextState(int rowIndex, int columnIndex, Cell 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) { private int countAliveNeighbours(int rowIndex, int columnIndex) {
...@@ -132,6 +137,31 @@ public class Grid implements Iterable<Cell> { ...@@ -132,6 +137,31 @@ public class Grid implements Iterable<Cell> {
return CounterAliveCell; 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) { private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
List<Cell> Neighbours = new ArrayList<>(); List<Cell> Neighbours = new ArrayList<>();
...@@ -167,10 +197,18 @@ public class Grid implements Iterable<Cell> { ...@@ -167,10 +197,18 @@ public class Grid implements Iterable<Cell> {
*/ */
void randomGeneration(Random random) { void randomGeneration(Random random) {
for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) { for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) {
Cell cell = it.next(); Cell cell = it.next();
if (random.nextBoolean()) { if (random.nextBoolean()) {
cell.setAlive(); cell.setAlive();
if (random.nextBoolean()) {
cell.setColor("Red");
}
else {
cell.setColor("Blue");
}
} }
else { else {
cell.setDead(); cell.setDead();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment