Added Main class and separate class to check rules

This commit is contained in:
Cole Landers
2018-04-04 22:38:50 -06:00
parent 7294cf6865
commit f7f490f2b8
3 changed files with 76 additions and 63 deletions

View File

@@ -10,84 +10,53 @@ 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 {
pause = true;
cells = new Cell[64][64]; 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)10,(double)10, 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);
} }
} }
root.add(button, cells.length, cells[cells.length-1].length);
AnimationTimer animate = new AnimationTimer(){
@Override
public void handle(long currentNanoTime){
for(int i = 0; i< cells.length; i++){
for(int j = 0; j < cells[i].length; j++){
GameOfLifeRules.checkRules(cells, i, j);
}
}
for(int i = 0; i< cells.length; i++){
for(int j = 0; j < cells[i].length; j++){
cells[i][j].updateStatus();
}
}
try {Thread.sleep(50);}
catch(Exception e) {}
}
};
Button stop = new Button("Stop");
stop.setOnAction(event->{
animate.stop();
});
Button start = new Button("Start");
start.setOnAction(event->{
animate.start();
});
root.add(start, cells.length, cells[cells.length-1].length);
root.add(stop, cells.length, cells[cells.length-1].length+1);
Scene scene = new Scene(root); Scene scene = new Scene(root);
primaryStage.setTitle("Conway's Game of Life"); primaryStage.setTitle("Conway's Game of Life");
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.show(); primaryStage.show();
new AnimationTimer(){
@Override
public void handle(long currentNanoTime){
if(!pause) {
for(int i = 0; i< cells.length; i++){
for(int j = 0; j < cells[i].length; j++){
checkRules(i,j);
}
}
for(int i = 0; i< cells.length; i++){
for(int j = 0; j < cells[i].length; j++){
cells[i][j].updateStatus();
}
}
try {Thread.sleep(50);}
catch(Exception e) {}
}
}
}.start();
}
public void checkRules(int x, int y){
int aliveNeighbors = 0;
for(int i = y-1; i <= y + 1; i++){
if(i >= 0 && i < cells[x].length){
if(x-1 >= 0){
if(cells[x-1][i].getStatus() == Cell.Status.ALIVE){
aliveNeighbors++;
}
}
if(i != y){
if(cells[x][i].getStatus() == Cell.Status.ALIVE){
aliveNeighbors++;
}
}
if(x+1 < cells.length){
if(cells[x+1][i].getStatus() == Cell.Status.ALIVE){
aliveNeighbors++;
}
}
}
}
if((aliveNeighbors == 2 || aliveNeighbors == 3) && cells[x][y].getStatus() == Cell.Status.ALIVE){
cells[x][y].makeAliveNextCycle(true);
}
else if((aliveNeighbors < 2 || aliveNeighbors > 3) && cells[x][y].getStatus() == Cell.Status.ALIVE){
cells[x][y].makeDeadNextCycle(true);
}
if(aliveNeighbors == 3 && cells[x][y].getStatus() == Cell.Status.DEAD){
cells[x][y].makeAliveNextCycle(true);
}
}
public static void main(String[] args){
launch(args);
} }
} }

View File

@@ -0,0 +1,35 @@
package gameoflife;
public class GameOfLifeRules {
public static void checkRules(Cell[][] cells, int x, int y) {
int aliveNeighbors = 0;
for(int i = y-1; i <= y + 1; i++){
if(i >= 0 && i < cells[x].length){
if(x-1 >= 0){
if(cells[x-1][i].getStatus() == Cell.Status.ALIVE){
aliveNeighbors++;
}
}
if(i != y){
if(cells[x][i].getStatus() == Cell.Status.ALIVE){
aliveNeighbors++;
}
}
if(x+1 < cells.length){
if(cells[x+1][i].getStatus() == Cell.Status.ALIVE){
aliveNeighbors++;
}
}
}
}
if((aliveNeighbors == 2 || aliveNeighbors == 3) && cells[x][y].getStatus() == Cell.Status.ALIVE){
cells[x][y].makeAliveNextCycle(true);
}
else if((aliveNeighbors < 2 || aliveNeighbors > 3) && cells[x][y].getStatus() == Cell.Status.ALIVE){
cells[x][y].makeDeadNextCycle(true);
}
if(aliveNeighbors == 3 && cells[x][y].getStatus() == Cell.Status.DEAD){
cells[x][y].makeAliveNextCycle(true);
}
}
}

9
src/gameoflife/Main.java Normal file
View File

@@ -0,0 +1,9 @@
package gameoflife;
import javafx.application.Application;
public class Main {
public static void main(String[] args) {
Application.launch(GameOfLife.class, args);
}
}