Added pause/unpause feature and click on cells to change status.

This commit is contained in:
Cole Landers
2018-04-04 19:34:40 -06:00
parent d0000f0523
commit 7294cf6865
2 changed files with 49 additions and 85 deletions

View File

@@ -1,6 +1,8 @@
package gameoflife; package gameoflife;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
public class Cell extends Rectangle{ public class Cell extends Rectangle{
@@ -9,6 +11,7 @@ public class Cell extends Rectangle{
} }
private Status status; private Status status;
private boolean aliveNextCycle, deadNextCycle; private boolean aliveNextCycle, deadNextCycle;
private int xLocation, yLocation;
public Cell(Double width, Double height, int x, int y){ public Cell(Double width, Double height, int x, int y){
setWidth(width); setWidth(width);
setHeight(height); setHeight(height);
@@ -16,6 +19,18 @@ public class Cell extends Rectangle{
setStatus(Status.DEAD); setStatus(Status.DEAD);
aliveNextCycle = false; aliveNextCycle = false;
deadNextCycle = false; deadNextCycle = false;
xLocation = x;
yLocation = y;
setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
if(getStatus() == Status.DEAD) {
setStatus(Status.ALIVE);
}
else {
setStatus(Status.DEAD);
}
}
});
} }
public Status getStatus(){ public Status getStatus(){
@@ -59,4 +74,12 @@ public class Cell extends Rectangle{
public void makeDeadNextCycle(boolean deadNextCycle){ public void makeDeadNextCycle(boolean deadNextCycle){
this.deadNextCycle = deadNextCycle; this.deadNextCycle = deadNextCycle;
} }
public int getXLocation() {
return xLocation;
}
public int getYLocation() {
return yLocation;
}
} }

View File

@@ -4,32 +4,30 @@ import javafx.application.Application;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.animation.AnimationTimer; import javafx.animation.AnimationTimer;
public class GameOfLife extends Application{ public class GameOfLife extends Application{
private static Cell[][] cells; private static Cell[][] cells;
private static boolean pause;
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
cells = new Cell[320][170]; pause = true;
cells = new Cell[64][64];
Button button = new Button("Resume/Pause");
button.setOnAction(event->{
pause = !pause;
});
GridPane root = new GridPane(); GridPane root = new GridPane();
root.setAlignment(Pos.CENTER); root.setAlignment(Pos.CENTER);
for(int i = 0; i < cells.length; i++){ for(int i = 0; i < cells.length; i++){
for(int j = 0; j < cells[i].length; j++){ for(int j = 0; j < cells[i].length; j++){
cells[i][j] = new Cell((double)5,(double)5, i, j); cells[i][j] = new Cell((double)10,(double)10, i, j);
root.add(cells[i][j], i, j); root.add(cells[i][j], i, j);
} }
} }
//Gun 1 root.add(button, cells.length, cells[cells.length-1].length);
staticLifeCreator();
//Gosper Glider Gun
gosperGliderGun();
Scene scene = new Scene(root); Scene scene = new Scene(root);
primaryStage.setTitle("Conway's Game of Life"); primaryStage.setTitle("Conway's Game of Life");
@@ -39,6 +37,7 @@ public class GameOfLife extends Application{
new AnimationTimer(){ new AnimationTimer(){
@Override @Override
public void handle(long currentNanoTime){ public void handle(long currentNanoTime){
if(!pause) {
for(int i = 0; i< cells.length; i++){ for(int i = 0; i< cells.length; i++){
for(int j = 0; j < cells[i].length; j++){ for(int j = 0; j < cells[i].length; j++){
checkRules(i,j); checkRules(i,j);
@@ -49,12 +48,13 @@ public class GameOfLife extends Application{
cells[i][j].updateStatus(); cells[i][j].updateStatus();
} }
} }
try{Thread.sleep(75);} try {Thread.sleep(50);}
catch(Exception e) {} catch(Exception e) {}
} }
}
}.start(); }.start();
} }
//Works Correctly
public void checkRules(int x, int y){ public void checkRules(int x, int y){
int aliveNeighbors = 0; int aliveNeighbors = 0;
for(int i = y-1; i <= y + 1; i++){ for(int i = y-1; i <= y + 1; i++){
@@ -87,65 +87,6 @@ public class GameOfLife extends Application{
} }
} }
public void gosperGliderGun(){
cells[1][5+2].setStatus(Cell.Status.ALIVE);
cells[1][6+2].setStatus(Cell.Status.ALIVE);
cells[2][5+2].setStatus(Cell.Status.ALIVE);
cells[2][6+2].setStatus(Cell.Status.ALIVE);
cells[14][3+2].setStatus(Cell.Status.ALIVE);
cells[13][3+2].setStatus(Cell.Status.ALIVE);
cells[12][4+2].setStatus(Cell.Status.ALIVE);
cells[11][5+2].setStatus(Cell.Status.ALIVE);
cells[11][6+2].setStatus(Cell.Status.ALIVE);
cells[11][7+2].setStatus(Cell.Status.ALIVE);
cells[12][8+2].setStatus(Cell.Status.ALIVE);
cells[13][9+2].setStatus(Cell.Status.ALIVE);
cells[14][9+2].setStatus(Cell.Status.ALIVE);
cells[15][6+2].setStatus(Cell.Status.ALIVE);
cells[16][4+2].setStatus(Cell.Status.ALIVE);
cells[17][5+2].setStatus(Cell.Status.ALIVE);
cells[17][6+2].setStatus(Cell.Status.ALIVE);
cells[18][6+2].setStatus(Cell.Status.ALIVE);
cells[17][7+2].setStatus(Cell.Status.ALIVE);
cells[16][8+2].setStatus(Cell.Status.ALIVE);
cells[21][3+2].setStatus(Cell.Status.ALIVE);
cells[21][4+2].setStatus(Cell.Status.ALIVE);
cells[21][5+2].setStatus(Cell.Status.ALIVE);
cells[22][3+2].setStatus(Cell.Status.ALIVE);
cells[22][4+2].setStatus(Cell.Status.ALIVE);
cells[22][5+2].setStatus(Cell.Status.ALIVE);
cells[23][2+2].setStatus(Cell.Status.ALIVE);
cells[23][6+2].setStatus(Cell.Status.ALIVE);
cells[25][1+2].setStatus(Cell.Status.ALIVE);
cells[25][2+2].setStatus(Cell.Status.ALIVE);
cells[25][6+2].setStatus(Cell.Status.ALIVE);
cells[25][7+2].setStatus(Cell.Status.ALIVE);
cells[35][3+2].setStatus(Cell.Status.ALIVE);
cells[35][4+2].setStatus(Cell.Status.ALIVE);
cells[36][3+2].setStatus(Cell.Status.ALIVE);
cells[36][4+2].setStatus(Cell.Status.ALIVE);
}
public void staticLifeCreator(){
for(int i = 0; i < 64; i++){
if(i>= 17 && i <= 24){
cells[i][64].setStatus(Cell.Status.ALIVE);
}
if(i>= 26 && i <= 30){
cells[i][64].setStatus(Cell.Status.ALIVE);
}
if(i>= 34 && i <= 36){
cells[i][64].setStatus(Cell.Status.ALIVE);
}
if(i>= 43 && i <= 49){
cells[i][64].setStatus(Cell.Status.ALIVE);
}
if(i>= 51 && i <= 55){
cells[i][64].setStatus(Cell.Status.ALIVE);
}
}
}
public static void main(String[] args){ public static void main(String[] args){
launch(args); launch(args);
} }