Updated structure of files and folders; edited Readme.md
This commit is contained in:
15
.classpath
15
.classpath
@@ -1,15 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<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
1
.gitignore
vendored
@@ -1 +0,0 @@
|
||||
/bin/
|
||||
17
.project
17
.project
@@ -1,17 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,11 +0,0 @@
|
||||
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
|
||||
13
README.md
13
README.md
@@ -4,6 +4,15 @@ My Conway's Game of Life project
|
||||
***Not optimized yet***
|
||||
This is just a little side project to work on optimization skills and mess around with different patterns in Conway's Game of Life.
|
||||
|
||||
Download the project and open it in eclipse to edit, or to just run the project download and run the attached jar(***located in the included jar folder***).
|
||||
**To run**
|
||||
Step 1 : Download Repository
|
||||
Step 2 : Open Command Prompt and navigate to the folder you downloaded
|
||||
Step 3 : Enter the following commands
|
||||
cd gameoflife
|
||||
javac GameOfLife.java
|
||||
javac Main.java
|
||||
jar cmvf manifest.txt GameOfLife.jar ./*.java ./*.class
|
||||
java -jar GameOfLife.java
|
||||
Step 4 : *Have fun using my Conway's Game of Life Program!!!*
|
||||
|
||||
***Note: Requires Java 10***
|
||||
***Note: Requires Java 8***
|
||||
@@ -1,85 +1,83 @@
|
||||
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{
|
||||
public enum Status{
|
||||
ALIVE, DEAD
|
||||
}
|
||||
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);
|
||||
setStroke(Color.LIGHTGRAY);
|
||||
setStatus(Status.DEAD);
|
||||
aliveNextCycle = false;
|
||||
deadNextCycle = false;
|
||||
xLocation = x;
|
||||
yLocation = y;
|
||||
setOnMousePressed(new EventHandler<MouseEvent>() {
|
||||
public void handle(MouseEvent me) {
|
||||
if(getStatus() == Status.DEAD) {
|
||||
setStatus(Status.ALIVE);
|
||||
}
|
||||
else {
|
||||
setStatus(Status.DEAD);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public int getXLocation() {
|
||||
return xLocation;
|
||||
}
|
||||
|
||||
public int getYLocation() {
|
||||
return yLocation;
|
||||
}
|
||||
}
|
||||
import javafx.scene.shape.Rectangle;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.paint.Color;
|
||||
|
||||
public class Cell extends Rectangle{
|
||||
public enum Status{
|
||||
ALIVE, DEAD
|
||||
}
|
||||
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);
|
||||
setStroke(Color.LIGHTGRAY);
|
||||
setStatus(Status.DEAD);
|
||||
aliveNextCycle = false;
|
||||
deadNextCycle = false;
|
||||
xLocation = x;
|
||||
yLocation = y;
|
||||
setOnMousePressed(new EventHandler<MouseEvent>() {
|
||||
public void handle(MouseEvent me) {
|
||||
if(getStatus() == Status.DEAD) {
|
||||
setStatus(Status.ALIVE);
|
||||
}
|
||||
else {
|
||||
setStatus(Status.DEAD);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public int getXLocation() {
|
||||
return xLocation;
|
||||
}
|
||||
|
||||
public int getYLocation() {
|
||||
return yLocation;
|
||||
}
|
||||
}
|
||||
@@ -1,65 +1,63 @@
|
||||
package gameoflife;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.animation.AnimationTimer;
|
||||
|
||||
public class GameOfLife extends Application{
|
||||
private Cell[][] cells;
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
cells = new Cell[64][64];
|
||||
|
||||
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)10,(double)10, i, j);
|
||||
root.add(cells[i][j], i, j);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
root.setStyle("-fx-background-color: white");
|
||||
|
||||
Scene scene = new Scene(root);
|
||||
|
||||
primaryStage.setTitle("Conway's Game of Life");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
import javafx.application.Application;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.animation.AnimationTimer;
|
||||
|
||||
public class GameOfLife extends Application{
|
||||
private Cell[][] cells;
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
cells = new Cell[64][64];
|
||||
|
||||
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)10,(double)10, i, j);
|
||||
root.add(cells[i][j], i, j);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
root.setStyle("-fx-background-color: white");
|
||||
|
||||
Scene scene = new Scene(root);
|
||||
|
||||
primaryStage.setTitle("Conway's Game of Life");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,33 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
package gameoflife;
|
||||
|
||||
import javafx.application.Application;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Application.launch(GameOfLife.class, args);
|
||||
}
|
||||
}
|
||||
import javafx.application.Application;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Application.launch(GameOfLife.class, args);
|
||||
}
|
||||
}
|
||||
1
gameoflife/manifest.mf
Normal file
1
gameoflife/manifest.mf
Normal file
@@ -0,0 +1 @@
|
||||
Main-Class: Main
|
||||
Binary file not shown.
Reference in New Issue
Block a user