From a7dd2477162e78c85fa3a821c88ee05c61cbe4cc Mon Sep 17 00:00:00 2001 From: Cole Landers Date: Fri, 30 Mar 2018 23:46:05 -0600 Subject: [PATCH] First commit of Conway's Game of Life project! --- .classpath | 16 +++ .gitignore | 1 + .project | 17 +++ .settings/org.eclipse.jdt.core.prefs | 11 ++ src/gameoflife/Cell.java | 62 +++++++++++ src/gameoflife/GameOfLife.java | 152 +++++++++++++++++++++++++++ test/gameoflifetest/CellTest.java | 16 +++ 7 files changed, 275 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/gameoflife/Cell.java create mode 100644 src/gameoflife/GameOfLife.java create mode 100644 test/gameoflifetest/CellTest.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..6caf0ec --- /dev/null +++ b/.classpath @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..4cbf201 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + GameOfLife + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..08ebd1b --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=9 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=9 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=9 diff --git a/src/gameoflife/Cell.java b/src/gameoflife/Cell.java new file mode 100644 index 0000000..c8c4576 --- /dev/null +++ b/src/gameoflife/Cell.java @@ -0,0 +1,62 @@ +package gameoflife; + +import javafx.scene.shape.Rectangle; +import javafx.scene.paint.Color; + +public class Cell extends Rectangle{ + public enum Status{ + ALIVE, DEAD + } + private Status status; + private boolean aliveNextCycle, deadNextCycle; + public Cell(Double width, Double height, int x, int y){ + setWidth(width); + setHeight(height); + setStroke(Color.LIGHTGRAY); + setStatus(Status.DEAD); + aliveNextCycle = false; + deadNextCycle = false; + } + + public Status getStatus(){ + return status; + } + + public void setStatus(Status status){ + if(status == Status.ALIVE){ + setFill(Color.BLACK); + } + else{ + setFill(Color.WHITE); + } + this.status = status; + } + + public void updateStatus(){ + if(isAliveNextCycle()){ + setStatus(Status.ALIVE); + makeAliveNextCycle(false); + } + else if(isDeadNextCycle()){ + setStatus(Status.DEAD); + makeDeadNextCycle(false); + } + else{return;} + } + + public boolean isAliveNextCycle(){ + return aliveNextCycle; + } + + public boolean isDeadNextCycle(){ + return deadNextCycle; + } + + public void makeAliveNextCycle(boolean aliveNextCycle){ + this.aliveNextCycle = aliveNextCycle; + } + + public void makeDeadNextCycle(boolean deadNextCycle){ + this.deadNextCycle = deadNextCycle; + } +} diff --git a/src/gameoflife/GameOfLife.java b/src/gameoflife/GameOfLife.java new file mode 100644 index 0000000..958c1c8 --- /dev/null +++ b/src/gameoflife/GameOfLife.java @@ -0,0 +1,152 @@ +package gameoflife; + +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.animation.AnimationTimer; + +public class GameOfLife extends Application{ + private static Cell[][] cells; + @Override + public void start(Stage primaryStage) throws Exception { + cells = new Cell[320][170]; + + 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); + root.add(cells[i][j], i, j); + } + } + //Gun 1 + staticLifeCreator(); + + + //Gosper Glider Gun + gosperGliderGun(); + + + + Scene scene = new Scene(root); + primaryStage.setTitle("Conway's Game of Life"); + primaryStage.setScene(scene); + primaryStage.show(); + + 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){} + } + }.start(); + } + //Works Correctly + 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 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); + } +} \ No newline at end of file diff --git a/test/gameoflifetest/CellTest.java b/test/gameoflifetest/CellTest.java new file mode 100644 index 0000000..23bdb00 --- /dev/null +++ b/test/gameoflifetest/CellTest.java @@ -0,0 +1,16 @@ +package gameoflifetest; + +import gameoflife.Cell; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +class CellTest { + static Cell cell; + @Test + void getStatusTest() { + cell = new Cell((double)5, (double)5, 3, 1); + assertEquals(Cell.Status.DEAD,cell.getStatus()); + } + +}