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 (3)
......@@ -5,10 +5,16 @@
public class Cell {
private boolean isAlive;
private String color = "Red";
public Cell(){
this.isAlive = false;
}
public void setColor(String hisColor) { color = hisColor; }
public String getColor() { return color; }
/**
* Determines whether this {@link Cell} is alive or not.
*
......
......@@ -39,8 +39,13 @@ 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).getColor()).equals("Red")) {
label.setForeground(Color.red);
} else {
label.setForeground(Color.blue);
}
}
else
label.setForeground(Color.white);
}
......
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.*;
/**
* {@code Grid} instances represent the grid in <i>The Game of Life</i>.
......@@ -98,29 +95,101 @@ public class Grid implements Iterable<Cell> {
}
private boolean[][] calculateNextStates() {
return null;
boolean[][] newState = new boolean[getNumberOfRows()][getNumberOfColumns()];
for (int r=0; r<numberOfRows; r++) {
for (int c=0; c<numberOfColumns; c++) {
newState[r][c] = this.calculateNextState(r,c,this.getCell(r,c));
}
}
return newState;
}
private void goToNextState(boolean[][] nextState) {
for (int r=0; r<numberOfRows; r++) {
for (int c=0; c<numberOfColumns; c++) {
if (nextState[r][c]) {
this.getCell(r,c).setAlive();
}
else {
this.getCell(r,c).setDead();
}
}
}
}
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 int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0;
int CounterAliveCell = 0;
for (Cell NeighboursCell : this.getNeighbours(rowIndex, columnIndex)) {
if (NeighboursCell.isAlive()) {
CounterAliveCell+=1;
}
}
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) {
return null;
List<Cell> Neighbours = new ArrayList<>();
for (int r=-1; r<=1; r++) {
for (int c=-1; c<=1; c++) {
if ((r != 0) || (c != 0)) {
Neighbours.add(this.getCell(rowIndex + r, columnIndex + c));
}
}
}
private void goToNextState(boolean[][] nextState) {
return Neighbours;
}
/**
* Sets all {@link Cell}s in this {@code Grid} as dead.
*/
void clear() {
for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) {
Cell cell = it.next();
cell.setDead();
}
}
/**
......@@ -131,6 +200,23 @@ public class Grid implements Iterable<Cell> {
*/
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();
}
}
}
}
\ No newline at end of file