Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
  • das.s/tp3
  • f19002502/tp3
  • r17010960/tp3
  • l19004806/tp3
  • y19010055/tp3
  • o18034026/tp3
  • z18029613/tp3
  • p19021289/tp3
  • d19027596/tp3
  • f18010428/tp3
  • f19003868/tp3
  • c19022214/tp3
  • c19017929/tp3
  • m16014784/tp3
  • a19028956/tp3
  • c19026071/tp3
  • h18008908/tp3
17 results
Select Git revision
  • master
1 result
Show changes
Commits on Source (2)
......@@ -4,9 +4,11 @@
public class Cell {
private boolean isAlive;
private boolean isBlue;
public Cell() {
this.isAlive = false;
this.isBlue = false;
}
/**
......@@ -19,6 +21,10 @@ public class Cell {
return this.isAlive;
}
public boolean isBlue() {
return this.isBlue;
}
/**
* Determines whether this {@link Cell} is dead or not.
*
......@@ -29,6 +35,10 @@ public class Cell {
return !this.isAlive;
}
public boolean isRed() {
return !this.isBlue;
}
/**
* Sets the state of this {@link Cell} to alive.
*
......@@ -39,6 +49,11 @@ public class Cell {
this.isAlive = true;
}
public void setBlue() {
this.isBlue = true;
}
/**
* Sets the state of this {@link Cell} to dead.
*
......@@ -49,6 +64,10 @@ public class Cell {
this.isAlive = false;
}
public void setRed() {
this.isBlue = false;
}
/**
* Change the state of this {@link Cell} from ALIVE to DEAD or from DEAD to ALIVE.
......@@ -67,8 +86,7 @@ public class Cell {
return true;
else
return false;
}
else{
} else {
if (numberOfAliveNeighbours == 3)
return true;
else
......
......@@ -17,16 +17,23 @@ public class GameOfLifeGUI extends JFrame {
gridLayout = new GridLayout(numberOfRows, numberOfColumns);
gridPanel = new JPanel(gridLayout);
labelGrid = new JLabel[numberOfRows][numberOfColumns];
for (int x = 0; x < numberOfColumns; x++)
for (int x = 0; x < numberOfColumns; x++) {
for (int y = 0; y < numberOfRows; y++) {
labelGrid[x][y] = new JLabel("*");
JLabel label;
if(g.getCell(x,y).isAlive())
if (g.getCell(x, y).isAlive()) {
if (g.getCell(x, y).isRed()) {
labelGrid[x][y].setForeground(Color.red);
else
} else {
labelGrid[x][y].setForeground(Color.blue);
}
} else {
labelGrid[x][y].setForeground(Color.white);
}
gridPanel.add(labelGrid[x][y]);
}
}
frame = new JFrame("Game of Life");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(gridPanel);
......@@ -36,13 +43,19 @@ public class GameOfLifeGUI extends JFrame {
}
public void update(Grid g) {
for (int x = 0; x < numberOfColumns; x++)
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).isBlue()) {
label.setForeground(Color.blue);
} else {
label.setForeground(Color.red);
else
}
} else {
label.setForeground(Color.white);
}
}
}
}
}
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
......@@ -98,29 +98,110 @@ public class Grid implements Iterable<Cell> {
}
private boolean[][] calculateNextStates() {
return null;
boolean[][] states = new boolean[numberOfRows][numberOfColumns];
for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) {
states[i][j] = calculateNextState(i, j, cells[i][j]);
}
}
return states;
}
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
return false;
int neighboursAlive = countAliveNeighbours(rowIndex, columnIndex);
int blueNeighbours = countBlueNeighbours(rowIndex, columnIndex);
boolean nextStateAlive = cell.isAlive();
if (cell.isAlive()) {
if (neighboursAlive == 2) {
nextStateAlive = true;
}
else if (neighboursAlive == 3) {
nextStateAlive = true;
}
else {
cell.isDead();
nextStateAlive = false;
}
}
else if (cell.isDead()) {
if (neighboursAlive == 3) {
cell.isAlive();
nextStateAlive = true;
if (blueNeighbours >= 2) {
cell.setBlue();
}
else {
cell.setRed();
}
}
else {
nextStateAlive = false;
}
}
return nextStateAlive;
}
private int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0;
int nbAlive = 0;
for (Cell cell : getNeighbours(rowIndex, columnIndex)) {
nbAlive += cell.isAlive() ? 1 : 0;
}
return nbAlive;
}
private int countBlueNeighbours(int rowIndex, int columnIndex) {
int nbBlue = 0;
for (Cell cell : getNeighbours(rowIndex, columnIndex)) {
if (cell.isAlive()) {
nbBlue += cell.isBlue() ? 1 : 0;
}
}
return nbBlue;
}
private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null;
List<Cell> neighbourCells = new ArrayList<Cell>();
neighbourCells.add(cells[rowIndex - 1 < 0 ? numberOfRows - 1 : rowIndex - 1][columnIndex - 1 < 0 ? numberOfColumns - 1 : columnIndex - 1]);
neighbourCells.add(cells[rowIndex][columnIndex - 1 < 0 ? numberOfColumns - 1 : columnIndex - 1]);
neighbourCells.add(cells[rowIndex + 1 > numberOfRows - 1 ? 0 : rowIndex + 1][columnIndex - 1 < 0 ? numberOfColumns - 1 : columnIndex - 1]);
neighbourCells.add(cells[rowIndex - 1 < 0 ? numberOfRows - 1 : rowIndex - 1][columnIndex]);
neighbourCells.add(cells[rowIndex + 1 > numberOfRows - 1 ? 0 : rowIndex + 1][columnIndex]);
neighbourCells.add(cells[rowIndex - 1 < 0 ? numberOfRows - 1 : rowIndex - 1][columnIndex + 1 > numberOfColumns - 1 ? 0 : columnIndex + 1]);
neighbourCells.add(cells[rowIndex][columnIndex + 1 > numberOfColumns - 1 ? 0 : columnIndex + 1]);
neighbourCells.add(cells[rowIndex + 1 > numberOfRows - 1 ? 0 : rowIndex + 1][columnIndex + 1 > numberOfColumns - 1 ? 0 : columnIndex + 1]);
return neighbourCells;
}
private void goToNextState(boolean[][] nextState) {
for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) {
if (nextState[i][j] == true) {
cells[i][j].setAlive();
} else {
cells[i][j].setDead();
}
}
}
}
/**
* Sets all {@link Cell}s in this {@code Grid} as dead.
*/
void clear() {
for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) {
cells[i][j].setDead();
}
}
}
/**
......@@ -131,6 +212,21 @@ public class Grid implements Iterable<Cell> {
*/
void randomGeneration(Random random) {
for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) {
if (random.nextBoolean()) {
cells[i][j].setAlive();
if (random.nextBoolean()) {
cells[i][j].setBlue();
} else {
cells[i][j].setRed();
}
} else {
cells[i][j].setDead();
}
}
}
}
}
......@@ -17,6 +17,7 @@ public class Main{
} catch (InterruptedException ie) {
}
gameOfLife.next();
gui.update(gameOfLife.getGrid());
}
......