First commit of Conway's Game of Life project!
This commit is contained in:
16
.classpath
Normal file
16
.classpath
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="test"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-9">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/bin/
|
||||
17
.project
Normal file
17
.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>GameOfLife</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
11
.settings/org.eclipse.jdt.core.prefs
Normal file
11
.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -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
|
||||
62
src/gameoflife/Cell.java
Normal file
62
src/gameoflife/Cell.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
152
src/gameoflife/GameOfLife.java
Normal file
152
src/gameoflife/GameOfLife.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
16
test/gameoflifetest/CellTest.java
Normal file
16
test/gameoflifetest/CellTest.java
Normal file
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user