Updated structure of files and folders; edited Readme.md
This commit is contained in:
83
gameoflife/Cell.java
Normal file
83
gameoflife/Cell.java
Normal file
@@ -0,0 +1,83 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
63
gameoflife/GameOfLife.java
Normal file
63
gameoflife/GameOfLife.java
Normal file
@@ -0,0 +1,63 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
33
gameoflife/GameOfLifeRules.java
Normal file
33
gameoflife/GameOfLifeRules.java
Normal file
@@ -0,0 +1,33 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
gameoflife/Main.java
Normal file
7
gameoflife/Main.java
Normal file
@@ -0,0 +1,7 @@
|
||||
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
|
||||
Reference in New Issue
Block a user