Skip to content

Commit

Permalink
Monitor.java update: Streams, optimization, GridPane; Housekeeping;
Browse files Browse the repository at this point in the history
  • Loading branch information
m1rza-s committed Apr 21, 2018
1 parent 8a1a53e commit 1db37db
Show file tree
Hide file tree
Showing 12 changed files with 698 additions and 809 deletions.
1,091 changes: 514 additions & 577 deletions .idea/workspace.xml

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions src/edu/lexaron/cells/Breed.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@
* Date: 19.4.2018. @ 00:49
*/
public enum Breed {
HUNT_CLOSEST,
HUNT_FIRST,
HUNT_LARGEST,
LEECH,
PREDATOR,
TREE,
VULTURE
HUNT_CLOSEST("#ff33ff"),
HUNT_FIRST("#66ff33"),
HUNT_MAX("#ffff33"),
LEECH("#0000ff"),
PREDATOR("#ff0000"),
TREE("#ffffff"),
VULTURE("#33ffff");

private final String colorCode;

Breed(String colorCode) {
this.colorCode = colorCode;
}

/**
* @return a HEX value of this breeds color
*/
public String getColorCode() {
return colorCode;
}
}
10 changes: 2 additions & 8 deletions src/edu/lexaron/cells/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
public abstract class Cell {

private final static Random RANDOM = new SecureRandom();
private final String color;
private final int movement;
private final Queue<Integer> path = new ArrayDeque<>();
private boolean alive;
Expand All @@ -37,8 +36,8 @@ public abstract class Cell {
private int lastRandomStep;
private String geneCode;

protected Cell(String ID, int x, int y, double energy, int vision, double speed, double efficiency, String color, double biteSize) {
this.geneCode = ID;
protected Cell(String id, int x, int y, double energy, int vision, double speed, double efficiency, double biteSize) {
this.geneCode = id;
this.x = x;
this.y = y;
this.energy = energy;
Expand All @@ -49,7 +48,6 @@ protected Cell(String ID, int x, int y, double energy, int vision, double speed,
if (energy > 0) {
this.alive = true;
}
this.color = color;
this.trailSize = 50;
this.targetFood = null;
this.biteSize = biteSize;
Expand Down Expand Up @@ -176,10 +174,6 @@ public int[] getTargetFood() {
return targetFood;
}

public String getColor() {
return color;
}

public double getBiteSize() {
return biteSize;
}
Expand Down
6 changes: 5 additions & 1 deletion src/edu/lexaron/cells/HuntClosest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/
public class HuntClosest extends Cell {
public HuntClosest(String ID, int x, int y) {
super(ID, x, y, 50, 3, 1, 1, "#ff33ff", 1);
super(ID, x, y, 50, 3, 1, 1, 1);
}

public HuntClosest(World world) {
this("C", getRandom().nextInt(world.getWidth()), getRandom().nextInt(world.getHeight()));
}

@Override
Expand Down
6 changes: 5 additions & 1 deletion src/edu/lexaron/cells/HuntFirst.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/
public class HuntFirst extends Cell {
public HuntFirst(String ID, int x, int y) {
super(ID, x, y, 50, 3, 3, 1, "#66ff33", 1);
super(ID, x, y, 50, 3, 3, 1, 1);
}

public HuntFirst(World world) {
this("F", getRandom().nextInt(world.getWidth()), getRandom().nextInt(world.getHeight()));
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions src/edu/lexaron/cells/HuntLargest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
*/
public class HuntLargest extends Cell {
public HuntLargest(String ID, int x, int y) {
super(ID, x, y, 50, 3, 2, 1, "#ffff33", 1);
super(ID, x, y, 50, 3, 2, 1, 1);
}

public HuntLargest(World world) {
this("L", getRandom().nextInt(world.getWidth()), getRandom().nextInt(world.getHeight()));
}

@Override
public Breed getBreed() {
return Breed.HUNT_LARGEST;
return Breed.HUNT_MAX;
}

@Override
Expand Down
6 changes: 5 additions & 1 deletion src/edu/lexaron/cells/Leech.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
public class Leech extends Cell {

public Leech(String ID, int x, int y) {
super(ID, x, y, 50, 5, 3, 0.25, "#0000ff", 0.5);
super(ID, x, y, 50, 5, 3, 0.25, 0.5);
}

public Leech(World world) {
this("L", getRandom().nextInt(world.getWidth()), getRandom().nextInt(world.getHeight()));
}

@Override
Expand Down
6 changes: 5 additions & 1 deletion src/edu/lexaron/cells/Predator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/
public class Predator extends Cell {
public Predator(String ID, int x, int y) {
super(ID, x, y, 50, 5, 1, 0.33, "#ff0000", 1);
super(ID, x, y, 50, 5, 1, 0.33, 1);
}

public Predator(World world) {
this("P", getRandom().nextInt(world.getWidth()), getRandom().nextInt(world.getHeight()));
}

@Override
Expand Down
6 changes: 5 additions & 1 deletion src/edu/lexaron/cells/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
public class Tree extends Cell {

public Tree(String ID, int x, int y) {
super(ID, x, y, 50, 10, 1, 0.1, "#ffffff", 0.2);
super(ID, x, y, 50, 10, 1, 0.1, 0.2);
}

public Tree(World world) {
this("T", getRandom().nextInt(world.getWidth()), getRandom().nextInt(world.getHeight()));
}

@Override
Expand Down
6 changes: 5 additions & 1 deletion src/edu/lexaron/cells/Vulture.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
public class Vulture extends Cell {

public Vulture(String ID, int x, int y) {
super(ID, x, y, 50, 3, 1, 0.5, "#33ffff", 1);
super(ID, x, y, 50, 3, 1, 0.5, 1);
}

public Vulture(World world) {
this("V", getRandom().nextInt(world.getWidth()), getRandom().nextInt(world.getHeight()));
}

@Override
Expand Down
46 changes: 28 additions & 18 deletions src/edu/lexaron/simulation/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class Engine {
private static final double GLOBAL_SCALE = 5.0;
private static final Random RANDOM = new SecureRandom();

private final Monitor monitor = new Monitor();
private final World world = new World(WIDTH, HEIGHT);
private final Life life = new Life(world);
private final VBox infoPanel = new VBox();
Expand Down Expand Up @@ -106,9 +105,9 @@ public void run() {
synchronized (world) {
world.notifyAll();
}
if (monitor.worldHasLiveCells(world)) {
if (world.getAllCells().stream().anyMatch(Cell::isAlive)) {
infoPanel.getChildren().clear();
infoPanel.getChildren().add(monitor.refreshLiveCellInfo(world));
infoPanel.getChildren().add(Monitor.refreshCellInformation(world));
}
else {
infoPanel.getChildren().clear();
Expand All @@ -121,9 +120,10 @@ public void run() {
}
generations++;
gens_L.setText(generations + " generations");
liveCells_L.setText("Alive: " + monitor.countLiveCells(world));
deadCells_L.setText("Dead: " + monitor.countDeadCells(world));
cells_L.setText("Total: " + monitor.countAllCells(world));
cells_L .setText("Total: " + world.getAllCells().size());
liveCells_L.setText("Alive: " + world.getAllCells().stream().filter(Cell::isAlive).count());
deadCells_L.setText("Dead: " + world.getAllCells().stream().filter(cell -> !cell.isAlive()).count());

sugar_L.setText("Sugar in world: " + totalSugar);
totalSugar = 0;
// END OF UI UPDATE //
Expand Down Expand Up @@ -173,7 +173,7 @@ else if (sugarTemp < 4 && sugarTemp >= 1) {
if (world.getWorld()[i][j].getTrail().getAmount() > 0) {

int trailTemp = world.getWorld()[i][j].getTrail().getAmount();
gc.setFill(Color.web(world.getWorld()[i][j].getTrail().getSource().getColor()));
gc.setFill(Color.web(world.getWorld()[i][j].getTrail().getSource().getBreed().getColorCode()));
if (trailTemp == 50) {
gc.setGlobalAlpha(0.5);
}
Expand All @@ -197,13 +197,23 @@ else if (trailTemp < 20 && trailTemp > 0) {
gc.restore();
}

private static void reseedCells(World world) {
world.getNewBornCells().add(new Vulture(world));
world.getNewBornCells().add(new Predator(world));
world.getNewBornCells().add(new HuntFirst(world));
world.getNewBornCells().add(new HuntLargest(world));
world.getNewBornCells().add(new HuntClosest(world));
world.getNewBornCells().add(new Tree(world));
world.getNewBornCells().add(new Leech(world));
}

private void paintCells(Cell c) {
if (!c.isAlive()) {
gc.setGlobalAlpha(0.2);
paintCellGFX(c);
}
else if (c.isAlive()) {
gc.setFill(Color.web(c.getColor()));
gc.setFill(Color.web(c.getBreed().getColorCode()));
gc.setGlobalAlpha(0.1);
// gc.fillRect(
// (c.getX() - c.getVision() - 0.25) * globalScale,
Expand All @@ -226,7 +236,7 @@ else if (a < 20 && a > 0) {
gc.setGlobalAlpha(0.6);
}
paintCellGFX(c);
gc.setStroke(Color.web(c.getColor()));
gc.setStroke(Color.web(c.getBreed().getColorCode()));
gc.fillText((int) c.getEnergy() + "", (c.getX() - 3) * GLOBAL_SCALE, (c.getY() - 1.5) * GLOBAL_SCALE);
paintTargetLine(c);
}
Expand All @@ -247,7 +257,7 @@ private void paintCellGFX(Cell cell) {
case HUNT_CLOSEST:
gc.drawImage(huntClosest, (cell.getX() - 1.5) * GLOBAL_SCALE, (cell.getY() - 1.5) * GLOBAL_SCALE);
break;
case HUNT_LARGEST:
case HUNT_MAX:
gc.drawImage(huntLargest, (cell.getX() - 1.5) * GLOBAL_SCALE, (cell.getY() - 1.5) * GLOBAL_SCALE);
break;
case HUNT_FIRST:
Expand All @@ -274,10 +284,10 @@ private void paintGrid() {
/**
* @param c
*/
public void paintTargetLine(Cell c) {
private void paintTargetLine(Cell c) {
if (c.getTargetFood() != null) {
gc.setGlobalAlpha(2);
gc.setStroke(Color.web(c.getColor()));
gc.setStroke(Color.web(c.getBreed().getColorCode()));
gc.strokeLine(
(c.getX() + 0.25) * GLOBAL_SCALE, (c.getY() + 0.25) * GLOBAL_SCALE,
(c.getTargetFood()[1] + 0.25) * GLOBAL_SCALE, (c.getTargetFood()[0] + 0.25) * GLOBAL_SCALE
Expand All @@ -293,15 +303,15 @@ public void setup(boolean cellsToo) {
world.generateWorld(sugarFactor);
gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
vulture = new Image("edu/lexaron/gfx/vulture.png");
predator = new Image("edu/lexaron/gfx/predator.png");
tree = new Image("edu/lexaron/gfx/tree.png");
huntFirst = new Image("edu/lexaron/gfx/huntFirst.png");
huntClosest = new Image("edu/lexaron/gfx/huntClosest.png");
huntFirst = new Image("edu/lexaron/gfx/huntFirst.png");
huntLargest = new Image("edu/lexaron/gfx/huntLargest.png");
leech = new Image("edu/lexaron/gfx/leech.png");
leech = new Image("edu/lexaron/gfx/leech.png");
predator = new Image("edu/lexaron/gfx/predator.png");
vulture = new Image("edu/lexaron/gfx/vulture.png");
tree = new Image("edu/lexaron/gfx/tree.png");
if (cellsToo) {
monitor.reseed(world);
reseedCells(world);
}
}

Expand Down
Loading

0 comments on commit 1db37db

Please sign in to comment.