Skip to content
Snippets Groups Projects
Commit 1ce44d8e authored by arthur's avatar arthur
Browse files

implementation de la variante coloré

parent 41c69169
No related branches found
No related tags found
No related merge requests found
......@@ -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.
*
......
......@@ -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())
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);
}
......
......@@ -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()) {
Cell cell = iterator.next();
if (random.nextBoolean()) {
cell.setAlive();
if (random.nextBoolean())
iterator.next().setAlive();
cell.setRed();
else
iterator.next().setDead();
cell.setBlue();
}
else
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;
}
}
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment