Created
May 8, 2022 09:12
-
-
Save avgprog/39699f6174d80519d7f1a0f0b825b7e2 to your computer and use it in GitHub Desktop.
Conway's game of life visualisation implemented using JavaFX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This code implements Conway's game of life, the universe of which is a two dimensional grid of cells which can either be dead or alive. | |
The grid changes its state based on these rules: | |
1) Any live cell with fewer than two live neighbours dies, as if caused by underpopulation. | |
2) Any live cell with two or three live neighbours lives on to the next generation. | |
3) Any live cell with more than three live neighbours dies, as if by overpopulation. | |
4) Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. | |
In this case, color of a live cell is black and color of a dead cell is white. | |
Sample input 1: | |
10 | |
10 | |
1 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 1 0 0 0 0 0 | |
0 0 0 0 1 1 0 0 0 0 | |
0 0 0 0 1 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
Sample input 2: | |
10 | |
10 | |
1 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 1 1 0 0 | |
0 0 0 0 0 0 0 1 0 0 | |
0 0 0 0 1 0 0 0 0 0 | |
0 0 0 0 1 1 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
Sample input 3: | |
20 | |
20 | |
2 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 | |
0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
*/ | |
import javafx.animation.KeyFrame; | |
import javafx.animation.Timeline; | |
import javafx.application.Application; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.scene.Scene; | |
import javafx.scene.shape.Rectangle; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
import javafx.scene.paint.Color; | |
import javafx.scene.layout.GridPane; | |
import javafx.util.Duration; | |
import java.util.Scanner; | |
public class ConwayGameOfLife extends Application { | |
static boolean grid[][]; | |
static int number_of_rows, number_of_columns; | |
@Override | |
public void start(Stage primary_stage) { | |
Scanner sc = new Scanner(System.in); | |
toPrint("Enter the number of rows:"); | |
number_of_rows = sc.nextInt(); | |
toPrint("Enter the number of columns:"); | |
number_of_columns = sc.nextInt(); | |
toPrint("Enter the refresh rate:"); | |
double refresh_rate = sc.nextDouble(); | |
grid = new boolean[number_of_rows][number_of_columns]; | |
toPrint("Enter the initial grid values:"); | |
initializeGrid(sc); | |
GridPane grid_pane = createGridOfRectangles(); | |
StackPane root = new StackPane(); | |
root.getChildren().add(grid_pane); | |
Scene scene = new Scene(root, 300, 250); | |
primary_stage.setTitle("Conway Game of Life"); | |
primary_stage.setScene(scene); | |
primary_stage.show(); | |
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(1000.0 / refresh_rate), | |
new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent actionEvent) { | |
updateGrid(); | |
GridPane grid_pane = createGridOfRectangles(); | |
root.getChildren().add(grid_pane); | |
} | |
})); | |
timeline.setCycleCount(Timeline.INDEFINITE); | |
timeline.play(); | |
} | |
public static void initializeGrid(Scanner sc) { | |
for (int i = 0; i < number_of_rows; i++) | |
for (int j = 0; j < number_of_columns; j++) { | |
if (sc.nextInt() == 0) | |
grid[i][j] = false; | |
else | |
grid[i][j] = true; | |
} | |
} | |
public static void updateGrid() { | |
boolean new_grid[][] = new boolean[number_of_rows][number_of_columns]; | |
for (int i = 0; i < number_of_rows; i++) | |
for (int j = 0; j < number_of_columns; j++) { | |
int live_neighbours = countLiveNeighbours(i,j); | |
new_grid[i][j] = false; | |
if((grid[i][j] && live_neighbours == 2) || live_neighbours == 3) | |
new_grid[i][j] = true; | |
} | |
grid = new_grid; | |
} | |
public static int countLiveNeighbours(int i, int j) { | |
int k, l, live_neighbours = 0; | |
for(k = -1; k <= 1; k++) | |
for(l = -1; l <= 1; l++) | |
if(k != 0 || l != 0) | |
live_neighbours = live_neighbours + checkLiveOrNot(k+i,l+j); | |
return live_neighbours; | |
} | |
public static int checkLiveOrNot(int i, int j) { | |
if(i < number_of_rows && i >= 0 && j < number_of_columns && j >= 0) | |
if(grid[i][j]) | |
return 1; | |
return 0; | |
} | |
public static GridPane createGridOfRectangles() { | |
GridPane grid_pane = new GridPane(); | |
for (int i = 0; i < number_of_rows; i++) { | |
for (int j = 0; j < number_of_columns; j++) { | |
System.out.println(grid[i][j]); | |
Rectangle r = new Rectangle(); | |
r.setWidth(10); | |
r.setHeight(10); | |
if (grid[i][j]) { | |
r.setFill(Color.BLACK); | |
grid_pane.add(r, i, j, 1, 1); | |
} else { | |
r.setFill(Color.WHITE); | |
grid_pane.add(r, i, j, 1, 1); | |
} | |
} | |
} | |
return grid_pane; | |
} | |
public static void toPrint(String str) { | |
System.out.println(str); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment