Skip to content
Snippets Groups Projects
Commit e0901c67 authored by DURIEZ Kilian's avatar DURIEZ Kilian
Browse files

tp3 fin

parent adb13dc8
Branches master
No related tags found
No related merge requests found
...@@ -4,6 +4,14 @@ ...@@ -4,6 +4,14 @@
public class Cell { public class Cell {
private boolean isAlive; private boolean isAlive;
private String color = "Red";
public void setColor(String hisColor) {
color = hisColor;
}
public String getColor() {
return color;
}
public Cell(){ public Cell(){
this.isAlive = false; this.isAlive = false;
......
...@@ -39,10 +39,17 @@ public class GameOfLifeGUI extends JFrame { ...@@ -39,10 +39,17 @@ 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);
} }
} }
} }
import java.util.Arrays; import java.util.*;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
/** /**
* {@code Grid} instances represent the grid in <i>The Game of Life</i>. * {@code Grid} instances represent the grid in <i>The Game of Life</i>.
...@@ -98,29 +95,94 @@ public class Grid implements Iterable<Cell> { ...@@ -98,29 +95,94 @@ public class Grid implements Iterable<Cell> {
} }
private boolean[][] calculateNextStates() { private boolean[][] calculateNextStates() {
return null; boolean[][] nextState = new boolean[getNumberOfRows()][getNumberOfColumns()] ;
for(int row=0; row<numberOfRows; row++) {
for(int column = 0; column<numberOfColumns; column++){
nextState[row][column] = this.calculateNextState(row, column, this.getCell(row, column));
}
}
return nextState;
} }
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
return false; boolean nextState = cell.isAliveInNextState(this.countAliveNeighbours(rowIndex, columnIndex));
if(nextState) {
if(this.countAliveNeighbours(rowIndex, columnIndex) != 2) {
cell.setColor(this.countColorNeighbours(rowIndex, columnIndex));
}
}
return nextState;
}
private String countColorNeighbours(int rowIndex, int columnIndex) {
String countColorCell;
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) {
countColorCell="Red";
}
else {
countColorCell="Blue";
}
return countColorCell;
} }
private int countAliveNeighbours(int rowIndex, int columnIndex) { private int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0; int alive = 0;
for(Cell NeighboursCell : this.getNeighbours(rowIndex, columnIndex)) {
if (NeighboursCell.isAlive()) {
alive = alive + 1;
}
}
return alive;
} }
private List<Cell> getNeighbours(int rowIndex, int columnIndex) { private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null; List<Cell> neighbours = new ArrayList<Cell>();
neighbours.add(this.getCell(rowIndex-1, columnIndex-1));
neighbours.add(this.getCell(rowIndex-1, columnIndex));
neighbours.add(this.getCell(rowIndex-1, columnIndex+1));
neighbours.add(this.getCell(rowIndex+1, columnIndex-1));
neighbours.add(this.getCell(rowIndex+1, columnIndex));
neighbours.add(this.getCell(rowIndex+1, columnIndex+1));
neighbours.add(this.getCell(rowIndex, columnIndex-1));
neighbours.add(this.getCell(rowIndex, columnIndex+1));
return neighbours;
} }
private void goToNextState(boolean[][] nextState) { private void goToNextState(boolean[][] nextState) {
for (int row = 0; row < numberOfRows; row++) {
for (int column = 0; column < numberOfColumns; column++) {
if (nextState[row][column])
getCell(row, column).setAlive();
else
getCell(row, column).setDead();
}
}
} }
/** /**
* Sets all {@link Cell}s in this {@code Grid} as dead. * Sets all {@link Cell}s in this {@code Grid} as dead.
*/ */
void clear() { void clear() {
for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) {
Cell cell = it.next();
cell.setDead();
}
} }
/** /**
...@@ -131,6 +193,23 @@ public class Grid implements Iterable<Cell> { ...@@ -131,6 +193,23 @@ public class Grid implements Iterable<Cell> {
*/ */
void randomGeneration(Random random) { 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();
}
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment