Skip to content

Instantly share code, notes, and snippets.

@ebarlas
Last active May 12, 2022 05:32
Show Gist options
  • Save ebarlas/cd7bc8fae2117e81e976fd1b4b633db8 to your computer and use it in GitHub Desktop.
Save ebarlas/cd7bc8fae2117e81e976fd1b4b633db8 to your computer and use it in GitHub Desktop.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameOfLife {
public static void main(String[] args) throws InterruptedException {
String glider = """
........................*...........
......................*.*...........
............**......**............**
...........*...*....**............**
**........*.....*...**..............
**........*...*.**....*.*...........
..........*.....*.......*...........
...........*...*....................
............**......................
""";
int width = 720;
int height = 360;
int period = 50;
int rows = 18;
int cols = 36;
Board board = new Board(rows, cols);
BoardCanvas boardCanvas = new BoardCanvas(board);
JFrame frame = new JFrame("Conway's Game of Life");
frame.add(boardCanvas);
frame.setSize(width, height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] lines = glider.split("\n");
for (int r = 0; r < lines.length; r++) {
for (int c = 0; c < lines[r].length(); c++) {
if (lines[r].charAt(c) == '*') {
board.alive(r, c);
}
}
}
while (true) {
boardCanvas.repaint();
Thread.sleep(period);
board.next(); // advance to next gen
}
}
public static class Board {
final int rows;
final int cols;
boolean[][] grid; // [row][col], grid[0][1] = grid[row=0][col=1]
Board(int rows, int cols) {
this.rows = rows;
this.cols = cols;
grid = new boolean[rows][cols];
}
void alive(int row, int col) {
set(row, col, true);
}
void set(int row, int col, boolean value) { // set(0, 1, true)
grid[row][col] = value;
}
int countLiveNeighbors(int row, int col) {
int count = 0;
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if (r >= 0 && c >= 0 && r < rows & c < cols) {
if (r != row || c != col) {
if (grid[r][c]) {
count++;
}
}
}
}
}
return count;
}
void next() {
boolean nextGrid[][] = new boolean[rows][cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
int liveNeighbors = countLiveNeighbors(r, c);
if (grid[r][c]) {
if (liveNeighbors == 2 || liveNeighbors == 3) {
nextGrid[r][c] = true;
}
} else {
if (liveNeighbors == 3) {
nextGrid[r][c] = true;
}
}
}
}
grid = nextGrid;
}
}
public static class BoardCanvas extends JPanel {
final Board board;
BoardCanvas(Board board) {
this.board = board;
}
@Override
public void paint(Graphics g) {
super.paint(g);
int boardWidth = getWidth();
int boardHeight = getHeight();
g.setColor(Color.WHITE);
g.fillRect(0, 0, boardWidth, boardHeight);
g.setColor(Color.BLACK);
int cellWidth = boardWidth / board.cols;
for (int x = 0; x <= boardWidth; x += cellWidth) {
g.drawLine(x, 0, x, boardHeight);
}
int cellHeight = boardHeight / board.rows;
for (int y = 0; y <= boardHeight; y += cellHeight) {
g.drawLine(0, y, boardWidth, y);
}
for (int r = 0; r < board.rows; r++) {
for (int c = 0; c < board.cols; c++) {
if (board.grid[r][c]) {
g.fillRect(c * cellWidth, r * cellHeight, cellWidth, cellHeight);
}
}
}
}
}
}
@ebarlas
Copy link
Author

ebarlas commented May 12, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment