From 7294cf6865b8cbe81d329b4681b8474947ad5ffe Mon Sep 17 00:00:00 2001 From: Cole Landers Date: Wed, 4 Apr 2018 19:34:40 -0600 Subject: [PATCH] Added pause/unpause feature and click on cells to change status. --- src/gameoflife/Cell.java | 23 +++++++ src/gameoflife/GameOfLife.java | 111 ++++++++------------------------- 2 files changed, 49 insertions(+), 85 deletions(-) diff --git a/src/gameoflife/Cell.java b/src/gameoflife/Cell.java index c8c4576..dbfdc82 100644 --- a/src/gameoflife/Cell.java +++ b/src/gameoflife/Cell.java @@ -1,6 +1,8 @@ package gameoflife; import javafx.scene.shape.Rectangle; +import javafx.event.EventHandler; +import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; public class Cell extends Rectangle{ @@ -9,6 +11,7 @@ public class Cell extends Rectangle{ } private Status status; private boolean aliveNextCycle, deadNextCycle; + private int xLocation, yLocation; public Cell(Double width, Double height, int x, int y){ setWidth(width); setHeight(height); @@ -16,6 +19,18 @@ public class Cell extends Rectangle{ setStatus(Status.DEAD); aliveNextCycle = false; deadNextCycle = false; + xLocation = x; + yLocation = y; + setOnMousePressed(new EventHandler() { + public void handle(MouseEvent me) { + if(getStatus() == Status.DEAD) { + setStatus(Status.ALIVE); + } + else { + setStatus(Status.DEAD); + } + } + }); } public Status getStatus(){ @@ -59,4 +74,12 @@ public class Cell extends Rectangle{ public void makeDeadNextCycle(boolean deadNextCycle){ this.deadNextCycle = deadNextCycle; } + + public int getXLocation() { + return xLocation; + } + + public int getYLocation() { + return yLocation; + } } diff --git a/src/gameoflife/GameOfLife.java b/src/gameoflife/GameOfLife.java index 958c1c8..5939260 100644 --- a/src/gameoflife/GameOfLife.java +++ b/src/gameoflife/GameOfLife.java @@ -4,33 +4,31 @@ import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.GridPane; import javafx.stage.Stage; -import javafx.scene.paint.Color; import javafx.geometry.Pos; +import javafx.scene.control.Button; import javafx.animation.AnimationTimer; public class GameOfLife extends Application{ private static Cell[][] cells; + private static boolean pause; @Override 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(); root.setAlignment(Pos.CENTER); for(int i = 0; i < cells.length; i++){ 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); } } - //Gun 1 - staticLifeCreator(); - - - //Gosper Glider Gun - gosperGliderGun(); - - - + root.add(button, cells.length, cells[cells.length-1].length); + Scene scene = new Scene(root); primaryStage.setTitle("Conway's Game of Life"); primaryStage.setScene(scene); @@ -39,22 +37,24 @@ public class GameOfLife extends Application{ 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++){ - 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(75);} - catch(Exception e){} + 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(); } - //Works Correctly + public void checkRules(int x, int y){ int aliveNeighbors = 0; 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){ launch(args); }