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 @@ ...@@ -4,9 +4,11 @@
public class Cell { public class Cell {
private boolean isAlive; private boolean isAlive;
private boolean isBlue;
public Cell() { public Cell() {
this.isAlive = false; this.isAlive = false;
this.isBlue = false;
} }
/** /**
...@@ -19,6 +21,10 @@ public class Cell { ...@@ -19,6 +21,10 @@ public class Cell {
return this.isAlive; return this.isAlive;
} }
public boolean isBlue() {
return this.isBlue;
}
/** /**
* Determines whether this {@link Cell} is dead or not. * Determines whether this {@link Cell} is dead or not.
* *
...@@ -29,6 +35,10 @@ public class Cell { ...@@ -29,6 +35,10 @@ public class Cell {
return !this.isAlive; return !this.isAlive;
} }
public boolean isRed() {
return !this.isBlue;
}
/** /**
* Sets the state of this {@link Cell} to alive. * Sets the state of this {@link Cell} to alive.
* *
...@@ -39,6 +49,11 @@ public class Cell { ...@@ -39,6 +49,11 @@ public class Cell {
this.isAlive = true; this.isAlive = true;
} }
public void setBlue() {
this.isBlue = true;
}
/** /**
* Sets the state of this {@link Cell} to dead. * Sets the state of this {@link Cell} to dead.
* *
...@@ -49,6 +64,10 @@ public class Cell { ...@@ -49,6 +64,10 @@ public class Cell {
this.isAlive = false; 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. * Change the state of this {@link Cell} from ALIVE to DEAD or from DEAD to ALIVE.
...@@ -67,8 +86,7 @@ public class Cell { ...@@ -67,8 +86,7 @@ public class Cell {
return true; return true;
else else
return false; return false;
} } else {
else{
if (numberOfAliveNeighbours == 3) if (numberOfAliveNeighbours == 3)
return true; return true;
else else
......
...@@ -17,16 +17,23 @@ public class GameOfLifeGUI extends JFrame { ...@@ -17,16 +17,23 @@ public class GameOfLifeGUI extends JFrame {
gridLayout = new GridLayout(numberOfRows, numberOfColumns); gridLayout = new GridLayout(numberOfRows, numberOfColumns);
gridPanel = new JPanel(gridLayout); gridPanel = new JPanel(gridLayout);
labelGrid = new JLabel[numberOfRows][numberOfColumns]; 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++) { for (int y = 0; y < numberOfRows; y++) {
labelGrid[x][y] = new JLabel("*"); labelGrid[x][y] = new JLabel("*");
JLabel label; 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); labelGrid[x][y].setForeground(Color.red);
else } else {
labelGrid[x][y].setForeground(Color.blue);
}
} else {
labelGrid[x][y].setForeground(Color.white); labelGrid[x][y].setForeground(Color.white);
}
gridPanel.add(labelGrid[x][y]); gridPanel.add(labelGrid[x][y]);
} }
}
frame = new JFrame("Game of Life"); frame = new JFrame("Game of Life");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(gridPanel); frame.setContentPane(gridPanel);
...@@ -36,13 +43,19 @@ public class GameOfLifeGUI extends JFrame { ...@@ -36,13 +43,19 @@ public class GameOfLifeGUI extends JFrame {
} }
public void update(Grid g) { 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++) { 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).isBlue()) {
label.setForeground(Color.blue);
} else {
label.setForeground(Color.red); label.setForeground(Color.red);
else }
} else {
label.setForeground(Color.white); label.setForeground(Color.white);
} }
} }
} }
}
}
import java.util.Arrays; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
...@@ -98,29 +98,110 @@ public class Grid implements Iterable<Cell> { ...@@ -98,29 +98,110 @@ public class Grid implements Iterable<Cell> {
} }
private boolean[][] calculateNextStates() { 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) { 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) { 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) { 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) { 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. * Sets all {@link Cell}s in this {@code Grid} as dead.
*/ */
void clear() { 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> { ...@@ -131,6 +212,21 @@ public class Grid implements Iterable<Cell> {
*/ */
void randomGeneration(Random random) { 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{ ...@@ -17,6 +17,7 @@ public class Main{
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
} }
gameOfLife.next(); gameOfLife.next();
gui.update(gameOfLife.getGrid()); gui.update(gameOfLife.getGrid());
} }
......