-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgol.java
executable file
·103 lines (87 loc) · 3.33 KB
/
cgol.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
#!/bin/bash
# The below line will compile and run the Java program
javac cgol.java && java cgol;
rm cgol.class
exit
*/
import java.util.Random;
// Main class to simulate Conway's Game of Life
public class cgol {
// Constants for the grid size and density of alive cells
private static final int WIDTH = 50;
private static final int HEIGHT = 30;
private static final double DENSITY = 0.2;
// Function to initialize the grid with random alive or dead cells
public static boolean[][] initializeGrid() {
boolean[][] grid = new boolean[HEIGHT][WIDTH];
Random random = new Random();
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
// Randomly assign alive cells based on the density constant
grid[y][x] = random.nextDouble() < DENSITY;
}
}
return grid;
}
// Function to count the number of alive neighbors around a given cell
public static int countNeighbors(boolean[][] grid, int x, int y) {
int count = 0;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) {
// Skip the cell itself
continue;
}
// Calculate neighbor coordinates with wrapping (toroidal)
int nx = (x + dx + WIDTH) % WIDTH;
int ny = (y + dy + HEIGHT) % HEIGHT;
// Count alive neighbors
if (grid[ny][nx]) {
count++;
}
}
}
return count;
}
// Function to compute the next state of the grid based on the Game of Life rules
public static boolean[][] computeNextState(boolean[][] grid) {
boolean[][] newGrid = new boolean[HEIGHT][WIDTH];
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
boolean alive = grid[y][x];
int neighbors = countNeighbors(grid, x, y);
// Apply Game of Life rules:
// - A live cell with 2 or 3 neighbors survives.
// - A dead cell with exactly 3 neighbors becomes alive.
// - Otherwise, the cell dies or remains dead.
newGrid[y][x] = (alive && neighbors == 2) || neighbors == 3;
}
}
return newGrid;
}
// Function to print the grid to the console
public static void printGrid(boolean[][] grid) {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (grid[y][x]) {
System.out.print("█"); // Print alive cell
} else {
System.out.print(" "); // Print dead cell
}
}
System.out.println();
}
}
public static void main(String[] args) throws InterruptedException {
boolean[][] grid = initializeGrid(); // Initialize the grid
while (true) {
printGrid(grid); // Print the current grid
grid = computeNextState(grid); // Compute the next generation
Thread.sleep(100); // Pause for 100ms
// Clear the console screen using ANSI escape codes
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
}