Added Main class and separate class to check rules
This commit is contained in:
@@ -10,37 +10,26 @@ 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);
|
|
||||||
|
|
||||||
Scene scene = new Scene(root);
|
AnimationTimer animate = new AnimationTimer(){
|
||||||
primaryStage.setTitle("Conway's Game of Life");
|
|
||||||
primaryStage.setScene(scene);
|
|
||||||
primaryStage.show();
|
|
||||||
|
|
||||||
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);
|
GameOfLifeRules.checkRules(cells, i, j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(int i = 0; i< cells.length; i++){
|
for(int i = 0; i< cells.length; i++){
|
||||||
@@ -51,43 +40,23 @@ public class GameOfLife extends Application{
|
|||||||
try {Thread.sleep(50);}
|
try {Thread.sleep(50);}
|
||||||
catch(Exception e) {}
|
catch(Exception e) {}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void checkRules(int x, int y){
|
Button stop = new Button("Stop");
|
||||||
int aliveNeighbors = 0;
|
stop.setOnAction(event->{
|
||||||
for(int i = y-1; i <= y + 1; i++){
|
animate.stop();
|
||||||
if(i >= 0 && i < cells[x].length){
|
});
|
||||||
if(x-1 >= 0){
|
Button start = new Button("Start");
|
||||||
if(cells[x-1][i].getStatus() == Cell.Status.ALIVE){
|
start.setOnAction(event->{
|
||||||
aliveNeighbors++;
|
animate.start();
|
||||||
}
|
});
|
||||||
}
|
|
||||||
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){
|
root.add(start, cells.length, cells[cells.length-1].length);
|
||||||
launch(args);
|
root.add(stop, cells.length, cells[cells.length-1].length+1);
|
||||||
|
|
||||||
|
Scene scene = new Scene(root);
|
||||||
|
primaryStage.setTitle("Conway's Game of Life");
|
||||||
|
primaryStage.setScene(scene);
|
||||||
|
primaryStage.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
35
src/gameoflife/GameOfLifeRules.java
Normal file
35
src/gameoflife/GameOfLifeRules.java
Normal 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
9
src/gameoflife/Main.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user