Skip to content

Instantly share code, notes, and snippets.

@CorruptComputer
Last active June 6, 2017 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CorruptComputer/16a1c24dfda363609110900147c0b366 to your computer and use it in GitHub Desktop.
Save CorruptComputer/16a1c24dfda363609110900147c0b366 to your computer and use it in GitHub Desktop.
The Game of Life
package life;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
public class Life extends JFrame {
public static int W = 800;
public static int H = 500;
public static int cellW = 5;
public static int cellH = 5;
public static int n = W/cellW;
public static int m = H/cellH;
public static double fillRate = 0.92;
public static boolean stopGraphics = false;
Timer timer = new Timer(100, new TimerListener());
public static int[][] cells = new int[n][m];
public static int[][] copy = new int[n][m];
public static boolean initialize = true;
public static JSlider threshold;
public static boolean mutate = false;
public static JButton mutation = new JButton("Mutate: off");
public static boolean grid = true;
public Life(){
timer.start();
DrawingPanel p = new DrawingPanel();
JButton stop = new JButton("Start/Stop");
stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
stopGraphics = !stopGraphics;
}
});
JButton init = new JButton("init");
init.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
initialize = true;
stopGraphics = true;
}
});
threshold = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
p.add(init);
p.add(stop);
p.add(threshold);
this.add(p);
}
static class DrawingPanel extends JPanel{
public DrawingPanel(){
initializeCells();
}
public void initializeCells(){
for(int x = 0; x < n; x++){
for(int y = 0; y < m; y++){
cells[x][y] = Math.random() > fillRate ? 1 : 0;
}
}
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
if(initialize){
fillRate = threshold.getValue()/100.0;
initializeCells();
}
initialize = false;
if(grid){
drawGrid(g);
}
if(!stopGraphics){
createCopy();
updateGrid();
}
}
private void updateGrid(){
for(int x = 0; x < n; x++){
for(int y = 0; y < m; y++){
cells[x][y] = copy[x][y];
}
}
}
private void createCopy(){
for(int x = 0; x < n; x++){
for(int y = 0; y < m; y++){
copy[x][y] = cells[x][y];
int count = getNeighborCount(x, y);
if(cells[x][y] == 1 && count < 2){
copy[x][y] = 0;
}
if(cells[x][y] == 1 && (count == 2 || count == 3)){
copy[x][y] = 1;
}
if(cells[x][y] == 1 && count > 3){
copy[x][y] = 0;
}
if(cells[x][y] == 0 && count == 3){
copy[x][y] = 1;
}
}
}
}
private void drawGrid(Graphics g){
Random rand = new Random();
g.drawRect(0, 0, W, H);
for(int x = 0; x < n; x++){
for(int y = 0; y < m; y++){
int i = x * cellW;
int o = y * cellH;
if(cells[x][y] == 1){
g.setColor(Color.BLACK);
g.fillRect(i, o, cellW, cellH);
g.drawRect(i, o, cellW, cellH);
}
if(cells[x][y] == 0){
if(Math.random() < 0.0001){
cells[x][y] = 1;
}
}
}
}
}
public static int getNeighborCount(int x, int y){
int xMinus = (x - 1) % n;
if(xMinus == -1){
xMinus = n-1;
}
int xPlus = (x + 1) % n;
int yMinus = (x - 1) % m;
if(yMinus == -1){
yMinus = m-1;
}
int yPlus = (y + 1) % m;
int sum = cells[xMinus][yMinus]
+ cells[x][yMinus]
+ cells[xPlus][yMinus]
+ cells[xMinus][y]
+ cells[xPlus][y]
+ cells[xMinus][yPlus]
+ cells[x][yPlus]
+ cells[xPlus][yPlus];
return sum;
}
}
public class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
}
}
public static void main(String[] args) {
Life frame = new Life();
frame.setTitle("The Game of Life");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(W, H);
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment