Skip to content

Commit

Permalink
reworking field update with HUGE Performance Enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
flwall committed Aug 23, 2019
1 parent 2a0c231 commit 43d2a6c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 83 deletions.
4 changes: 1 addition & 3 deletions manifest.mf
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Main-Class : Main

X-COMMENT: Main-Class will be added automatically by build
64 changes: 64 additions & 0 deletions src/gameoflife/Cell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package gameoflife;

import java.awt.Color;

enum LiveState {
LIVING, DEAD
}

public class Cell implements Comparable<Cell> {

private int row, col;
private LiveState state;

public Cell(int row, int col, LiveState state) {
this.setState(state);
this.row = row;
this.col = col;

}

public LiveState getState() {
return state;
}

public void setState(LiveState state) {
this.state = state;
}

public int getRow() {
return row;
}

public void setRow(int row) {
this.row = row;
}

public int getCol() {
return col;
}

public void setCol(int col) {
this.col = col;
}

@Override
public int compareTo(Cell o) {
if (row == o.getRow() && col == o.getCol()) {
return 0;
}
return -1;

}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Cell))
return false;

Cell c = (Cell) obj;
return c.getRow() == row && c.getCol() == col;

}

}
55 changes: 41 additions & 14 deletions src/gameoflife/GameOfLifeCalculation.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package gameoflife;

import java.util.LinkedList;
import java.util.List;
import java.util.Observable;

import com.sun.org.apache.xpath.internal.SourceTree;

public class GameOfLifeCalculation extends Observable implements Runnable {

private boolean[][] field;
Thread t = null;
List<Cell> changedCells = new LinkedList<>();

public GameOfLifeCalculation(boolean[][] f) {

Expand All @@ -25,15 +25,14 @@ public void generateRandomField() {
double isLiving = ((Math.random() * 2));

if (isLiving > 0.25d) {
field[j][k] = false;
changeStateOfCell(j, k, false);
} else {
field[j][k] = true;
changeStateOfCell(j, k, true);
}

}
}
setChanged();
notifyObservers(field);
update();
}

@Override
Expand All @@ -49,11 +48,10 @@ public void run() {
* } catch (InterruptedException e) { break; }
*/

setChanged();
notifyObservers(field);
update();

}

}

}

public void nextGen() {
Expand All @@ -73,20 +71,49 @@ public void nextGen() {
}

if (field[j][k] && aliveNeighbours < 2) {
field[j][k] = false;
changeStateOfCell(j, k, false);
} else if (field[j][k] && aliveNeighbours > 3) {
field[j][k] = false;
changeStateOfCell(j, k, false);

} else if (!field[j][k] && aliveNeighbours == 3) {
field[j][k] = true;
changeStateOfCell(j, k, true);

}

}
}

}

private void changeStateOfCell(int i, int j, boolean isLiving) {
field[i][j] = isLiving;

LiveState state = null;
if (field[i][j]) {
state = LiveState.LIVING;
} else
state = LiveState.DEAD;

Cell c = new Cell(i, j, state);
if (changedCells.contains(c)) {
changedCells.get(changedCells.indexOf(c)).setState(state);
} else {
changedCells.add(c);
}

}

public void changeStateOfCell(int i, int j) {
field[i][j] = !field[i][j];
changeStateOfCell(i, j, field[i][j]);

}

public void update() {
setChanged();
notifyObservers(new LinkedList<>(changedCells));
changedCells.clear();

}

}
13 changes: 6 additions & 7 deletions src/gameoflife/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public void setCols(int cols) {
this.cols = cols;
}

private boolean[][] field;


private GameOfLifeCalculation calc;

public void initField(int rows, int cols) {

this.rows=rows;
this.cols=cols;
field = new boolean[cols][rows];
boolean[][] field = new boolean[cols][rows];

calc = new GameOfLifeCalculation(field);
calc.addObserver(this);
Expand All @@ -46,8 +46,7 @@ public void nextGen() {
}
calc.nextGen();

setChanged();
notifyObservers(field);
calc.update();

}

Expand Down Expand Up @@ -87,12 +86,11 @@ public void stopGame() {
public void generateRandomField(){
if(gameThread!=null) gameThread.interrupt();

field=new boolean [cols][rows];
boolean[][] field=new boolean [cols][rows];
calc=new GameOfLifeCalculation(field);
calc.addObserver(this);
calc.generateRandomField();



}

@Override
Expand All @@ -106,6 +104,7 @@ public void update(Observable o, Object arg) {
public void changeStateOfCell(int i, int j) {
calc.changeStateOfCell(i, j);


}

public void interrupt() {
Expand Down
Loading

0 comments on commit 43d2a6c

Please sign in to comment.