Last active
October 11, 2016 10:47
-
-
Save ornirus/7c0e0a0dcd073446aa67 to your computer and use it in GitHub Desktop.
GOL
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 com.company; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.event.*; | |
import java.util.*; | |
public class GameOfLife extends JFrame | |
{ | |
private JButton button; | |
private JButton next; | |
int gridX = 100, gridY = 100, alive = 0; | |
int[][] grid; | |
int[][] prevGrid; | |
private JPanel panel; | |
private Random random; | |
private javax.swing.Timer timer; | |
public void createGUI() { | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
Container window = getContentPane(); | |
window.setLayout(new FlowLayout()); | |
random = new Random(); | |
panel = new JPanel(); | |
panel.setPreferredSize(new Dimension(701, 701)); | |
panel.setBackground(Color.lightGray); | |
window.add(panel); | |
// timer = new javax.swing.Timer(10, this); | |
// timer.start(); | |
button = new JButton("Randomize"); | |
window.add(button); | |
button.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
populate(); | |
render(); | |
} | |
}); | |
next = new JButton("Next"); | |
window.add(next); | |
next.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
updatePrevGrid(); | |
} | |
}); | |
} | |
public void populate() { | |
grid = new int[gridX][gridY]; | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
grid[i][j] = random.nextInt(2); | |
} | |
} | |
} | |
public void render(){ | |
Graphics paper = panel.getGraphics(); | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
if (grid[i][j] == 1) { | |
paper.setColor(Color.black); | |
} | |
else { | |
paper.setColor(Color.white); | |
} | |
paper.fillRect(1 + 7 * i,1 + 7 * j, 6, 6); | |
} | |
} | |
} | |
public void updatePrevGrid() { | |
prevGrid = new int[gridX][gridY]; | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
prevGrid[n][m] = grid[n][m]; | |
} | |
} | |
update(); | |
} | |
public void update() { | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
alive = 0; | |
for (int x = (n + 99) % (100); x < (n + 99 + 2) % (100); x = (x + 1) % 100) { | |
for (int y = (m + 99) % (100); y < (m + 99 + 2) % (100); y = (y + 1) % 100) { | |
alive = alive + prevGrid[x][y]; | |
} | |
} | |
if (prevGrid[n][m] == 1) { | |
alive = alive - 1; | |
if (alive < 2) { | |
grid[n][m] = 0; | |
} | |
if (alive == 3 || alive == 2) { | |
grid[n][m] = 1; | |
} | |
if (alive > 3) { | |
grid[n][m] = 0; | |
} | |
} | |
else { | |
if (alive == 3) { | |
grid[n][m] = 1; | |
} | |
} | |
} | |
} | |
render(); | |
} | |
} |
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 com.company; | |
import sun.jvm.hotspot.utilities.BitMap; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.event.*; | |
import java.awt.image.BufferedImage; | |
import java.util.*; | |
public class GameOfLife extends JFrame | |
{ | |
private JButton button; | |
private JButton next; | |
int gridX = 100, gridY = 100, alive = 0; | |
boolean run = false; | |
int[][] grid; | |
int[][] prevGrid; | |
private JPanel panel; | |
private Random random; | |
private javax.swing.Timer timer; | |
public void createGUI() { | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
Container window = getContentPane(); | |
window.setLayout(new FlowLayout()); | |
random = new Random(); | |
panel = new JPanel(); | |
panel.setPreferredSize(new Dimension(701, 701)); | |
panel.setBackground(Color.lightGray); | |
window.add(panel); | |
//timer = new javax.swing.Timer(100, this); | |
//timer.start(); | |
button = new JButton("Randomize"); | |
window.add(button); | |
button.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
populate(); | |
render(); | |
} | |
}); | |
next = new JButton("Next"); | |
window.add(next); | |
next.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
if (run == false) { | |
run = true; | |
} | |
else { | |
run = false; | |
} | |
while (run == true) { | |
updatePrevGrid(); | |
try { | |
Thread.sleep(1000 / 60); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}).start(); | |
} | |
}); | |
} | |
public void populate() { | |
grid = new int[gridX][gridY]; | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
grid[i][j] = random.nextInt(2); | |
} | |
} | |
} | |
public void render(){ | |
BufferedImage img = new BufferedImage(701, 701, BufferedImage.TYPE_INT_ARGB); | |
Graphics paper = img.getGraphics(); | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
if (grid[i][j] == 1) { | |
paper.setColor(Color.black); | |
} | |
else { | |
paper.setColor(Color.white); | |
} | |
paper.fillRect(1 + 7 * i,1 + 7 * j, 6, 6); | |
} | |
} | |
panel.getGraphics().drawImage(img, 0, 0, this); | |
} | |
public void updatePrevGrid() { | |
prevGrid = new int[gridX][gridY]; | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
prevGrid[n][m] = grid[n][m]; | |
} | |
} | |
update(); | |
} | |
public void update() { | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
alive = 0; | |
if (prevGrid[(n - 1 + 100) % 100][(m - 1 + 100) % 100] == 1) { | |
alive = alive + 1; | |
} | |
if (prevGrid[(n - 1 + 100) % 100][(m + 100) % 100] == 1) { | |
alive = alive + 1; | |
} | |
if (prevGrid[(n - 1 + 100) % 100][(m + 1 + 100) % 100] == 1) { | |
alive = alive + 1; | |
} | |
if (prevGrid[(n + 100) % 100][(m - 1 + 100) % 100] == 1) { | |
alive = alive + 1; | |
} | |
if (prevGrid[(n + 1 + 100) % 100][(m - 1 + 100) % 100] == 1) { | |
alive = alive + 1; | |
} | |
if (prevGrid[(n + 1 + 100) % 100][(m + 100) % 100] == 1) { | |
alive = alive + 1; | |
} | |
if (prevGrid[(n + 100) % 100][(m + 1 + 100) % 100] == 1) { | |
alive = alive + 1; | |
} | |
if (prevGrid[(n + 1 + 100) % 100][(m + 1 + 100) % 100] == 1) { | |
alive = alive + 1; | |
} | |
if (prevGrid[n][m] == 1) { | |
if (alive < 2) { | |
grid[n][m] = 0; | |
} | |
if (alive == 3 || alive == 2) { | |
grid[n][m] = 1; | |
} | |
if (alive > 3) { | |
grid[n][m] = 0; | |
} | |
} | |
else { | |
if (alive == 3) { | |
grid[n][m] = 1; | |
} | |
} | |
} | |
} | |
render(); | |
} | |
} |
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 com.company; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.event.*; | |
import java.util.*; | |
public class GameOfLife extends JFrame | |
{ | |
private JButton button; | |
private JButton next; | |
int gridX = 100, gridY = 100, alive = 0; | |
int[][] grid; | |
int[][] prevGrid; | |
private JPanel panel; | |
private Random random; | |
private javax.swing.Timer timer; | |
public void createGUI() { | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
Container window = getContentPane(); | |
window.setLayout(new FlowLayout()); | |
random = new Random(); | |
panel = new JPanel(); | |
panel.setPreferredSize(new Dimension(701, 701)); | |
panel.setBackground(Color.lightGray); | |
window.add(panel); | |
// timer = new javax.swing.Timer(10, this); | |
// timer.start(); | |
button = new JButton("Randomize"); | |
window.add(button); | |
button.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
populate(); | |
render(); | |
} | |
}); | |
next = new JButton("Next"); | |
window.add(next); | |
next.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
updatePrevGrid(); | |
} | |
}); | |
} | |
public void populate() { | |
grid = new int[gridX][gridY]; | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
grid[i][j] = random.nextInt(2); | |
} | |
} | |
} | |
public void render(){ | |
Graphics paper = panel.getGraphics(); | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
if (grid[i][j] == 1) { | |
paper.setColor(Color.black); | |
} | |
else { | |
paper.setColor(Color.white); | |
} | |
paper.fillRect(1 + 7 * i,1 + 7 * j, 6, 6); | |
} | |
} | |
} | |
public void updatePrevGrid() { | |
prevGrid = new int[gridX][gridY]; | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
prevGrid[n][m] = grid[n][m]; | |
} | |
} | |
update(); | |
} | |
public void update() { | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
alive = 0; | |
for (int x = (n + prevGrid.length) % (prevGrid.length + 1); x < (n + prevGrid.length + 2) % (prevGrid.length + 1); x = (x + (n + prevGrid.length) % (prevGrid.length + 1)) + 1) { | |
for (int y = (m + prevGrid[0].length) % (prevGrid[0].length + 1); y < m + 1; y++) { | |
alive = alive + prevGrid[x][y]; | |
} | |
} | |
if (prevGrid[n][m] == 1) { | |
if (alive != 1 || alive != 2){ | |
grid[n][m] = 0; | |
} | |
} | |
else { | |
if (alive == 2) { | |
grid[n][m] = 1; | |
} | |
} | |
} | |
} | |
render(); | |
} | |
} |
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 com.company; | |
/** | |
* Created by User on 1/14/15. | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
GameOfLife frame = new GameOfLife(); | |
frame.setSize(800, 800); | |
frame.createGUI(); | |
frame.setVisible(true); | |
} | |
} |
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 com.company; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.event.*; | |
import java.util.*; | |
public class GameOfLife extends JFrame | |
{ | |
private JButton button; | |
private JButton next; | |
int gridX = 100, gridY = 100, alive = 0; | |
int[][] grid; | |
int[][] prevGrid; | |
private JPanel panel; | |
private Random random; | |
private javax.swing.Timer timer; | |
public void createGUI() { | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
Container window = getContentPane(); | |
window.setLayout(new FlowLayout()); | |
random = new Random(); | |
panel = new JPanel(); | |
panel.setPreferredSize(new Dimension(701, 701)); | |
panel.setBackground(Color.lightGray); | |
window.add(panel); | |
// timer = new javax.swing.Timer(10, this); | |
// timer.start(); | |
button = new JButton("Randomize"); | |
window.add(button); | |
button.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
populate(); | |
render(); | |
} | |
}); | |
next = new JButton("Next"); | |
window.add(next); | |
next.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
updatePrevGrid(); | |
} | |
}); | |
} | |
public void populate() { | |
grid = new int[gridX][gridY]; | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
grid[i][j] = random.nextInt(2); | |
} | |
} | |
} | |
public void render(){ | |
Graphics paper = panel.getGraphics(); | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
if (grid[i][j] == 1) { | |
paper.setColor(Color.black); | |
} | |
else { | |
paper.setColor(Color.white); | |
} | |
paper.fillRect(1 + 7 * i,1 + 7 * j, 6, 6); | |
} | |
} | |
} | |
public void updatePrevGrid() { | |
prevGrid = new int[gridX][gridY]; | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
prevGrid[n][m] = grid[n][m]; | |
} | |
} | |
update(); | |
} | |
public void update() { | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
alive = 0; | |
for (int x = (n + prevGrid.length) % (prevGrid.length + 1); x < n + 1; x++) { | |
for (int y = (m + prevGrid[0].length) % (prevGrid[0].length + 1); y < m + 1; y++) { | |
alive = alive + prevGrid[x][y]; | |
} | |
} | |
if (prevGrid[n][m] == 1) { | |
alive = alive - 1; | |
} | |
if (alive == 2 || alive == 3) { | |
grid[n][m] = 1; | |
} | |
else { | |
//if (alive == 2) { | |
grid[n][m] = 0; | |
//} | |
} | |
} | |
} | |
render(); | |
} | |
} |
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 com.company; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.event.*; | |
import java.util.*; | |
public class GameOfLife extends JFrame | |
{ | |
private JButton button; | |
private JButton next; | |
int gridX = 100, gridY = 100, alive = 0; | |
int[][] grid; | |
int[][] prevGrid; | |
private JPanel panel; | |
private Random random; | |
private javax.swing.Timer timer; | |
public void createGUI() { | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
Container window = getContentPane(); | |
window.setLayout(new FlowLayout()); | |
random = new Random(); | |
panel = new JPanel(); | |
panel.setPreferredSize(new Dimension(701, 701)); | |
panel.setBackground(Color.lightGray); | |
window.add(panel); | |
// timer = new javax.swing.Timer(10, this); | |
// timer.start(); | |
button = new JButton("Randomize"); | |
window.add(button); | |
button.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
populate(); | |
render(); | |
} | |
}); | |
next = new JButton("Next"); | |
window.add(next); | |
next.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
updatePrevGrid(); | |
} | |
}); | |
} | |
public void populate() { | |
grid = new int[gridX][gridY]; | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
grid[i][j] = random.nextInt(2); | |
} | |
} | |
} | |
public void render(){ | |
Graphics paper = panel.getGraphics(); | |
for (int i = 0; i < grid.length; i++) { | |
for (int j = 0; j < grid[0].length; j++) { | |
if (grid[i][j] == 1) { | |
paper.setColor(Color.black); | |
} | |
else { | |
paper.setColor(Color.white); | |
} | |
paper.fillRect(1 + 7 * i,1 + 7 * j, 6, 6); | |
} | |
} | |
} | |
public void updatePrevGrid() { | |
prevGrid = new int[gridX][gridY]; | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
prevGrid[n][m] = grid[n][m]; | |
} | |
} | |
update(); | |
} | |
public void update() { | |
for (int n = 0; n < grid.length; n++) { | |
for (int m = 0; m < grid[0].length; m++) { | |
alive = 0; | |
for (int x = (n + 99) % (100); x < (n + 99 + 2) % (100); x = (x + 1) % 100) { | |
for (int y = (m + 99) % (100); y < (m + 99 + 2) % (100); y = (y + 1) % 100) { | |
alive = alive + prevGrid[x][y]; | |
} | |
} | |
if (prevGrid[n][m] == 1) { | |
alive = alive - 1; | |
if (alive != 2 || alive != 3){ | |
grid[n][m] = 0; | |
} | |
} | |
else { | |
if (alive == 3) { | |
grid[n][m] = 1; | |
} | |
} | |
} | |
} | |
render(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suggest you separate the populate code and the render code....
https://gist.github.com/ornirus/7c0e0a0dcd073446aa67#file-gameoflife-L50