Created
June 8, 2022 05:24
-
-
Save ebarlas/93be96be7511a8a03ed333eb8e3ea5fb to your computer and use it in GitHub Desktop.
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
package interview; | |
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 pattern = """ | |
........................*........... | |
......................*.*........... | |
............**......**............** | |
...........*...*....**............** | |
**........*.....*...**.............. | |
**........*...*.**....*.*........... | |
..........*.....*.......*........... | |
...........*...*.................... | |
............**...................... | |
"""; | |
/* | |
String pattern = """ | |
.***......*.....*......***. | |
*..*.....***...***.....*..* | |
...*....**.*...*.**....*... | |
...*...................*... | |
...*..*.............*..*... | |
...*..**...........**..*... | |
..*...**...........**...*.. | |
"""; | |
*/ | |
String[] lines = pattern.split("\n"); | |
int patternRows = lines.length; | |
int patternCols = lines[0].length(); | |
int left = 5; | |
int top = 5; | |
int right = 10; | |
int bottom = 20; | |
int period = 25; | |
int rows = patternRows + top + bottom; | |
int cols = patternCols + left + right; | |
int width = cols * 20; | |
int height = rows * 20; | |
Board board = new Board(rows, cols); | |
for (int r=0; r<lines.length; r++) { | |
for (int c=0; c<lines[r].length(); c++) { | |
if (lines[r].charAt(c) == '*') { | |
board.grid[top + r][left + c] = true; | |
} | |
} | |
} | |
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); | |
while (true) { | |
boardCanvas.repaint(); | |
Thread.sleep(period); | |
board.nextGeneration(); | |
} | |
} | |
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]; | |
} | |
boolean isValid(int row, int col) { | |
return row >= 0 && row < rows && col >= 0 && col < cols; | |
} | |
int numberOfLiveNeighbors(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 (isValid(r, c)) { | |
if (r != row || c != col) { | |
if (grid[r][c]) { | |
count++; | |
} | |
} | |
} | |
} | |
} | |
return count; | |
} | |
boolean isAliveInNextGen(int row, int col) { | |
int numLiveNeighbors = numberOfLiveNeighbors(row, col); | |
if (grid[row][col]) { | |
return numLiveNeighbors == 2 || numLiveNeighbors == 3; | |
} | |
return numLiveNeighbors == 3; | |
} | |
void nextGeneration() { | |
boolean[][] next = new boolean[rows][cols]; | |
for (int r = 0; r < rows; r++) { | |
for (int c = 0; c < cols; c++){ | |
if (isAliveInNextGen(r, c)) { | |
next[r][c] = true; | |
} | |
} | |
} | |
grid = next; | |
} | |
} | |
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 windowWidth = getWidth(); | |
int windowHeight = getHeight(); | |
g.setColor(Color.WHITE); | |
g.fillRect(0, 0, windowWidth, windowHeight); | |
g.setColor(Color.BLACK); | |
int cellWidth = windowWidth / board.cols; // 300 / 3 = 100 | |
int cellHeight = windowHeight / board.rows; | |
for (int col = 1; col <= board.cols - 1; col++) { | |
g.drawLine(col * cellWidth, 0, col * cellWidth, windowHeight); | |
} | |
for (int row = 1; row <= board.rows - 1; row++) { | |
g.drawLine(0, row * cellHeight, windowWidth, row * cellHeight); | |
} | |
for (int row = 0; row < board.rows; row++) { | |
for (int col = 0; col < board.cols; col++) { | |
if (board.grid[row][col]) { | |
g.fillRect(col * cellWidth, row * cellHeight, cellWidth, cellHeight); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment