I've implemented the Conway's Game of Life in Java, including a GUI. Here's the code of the two classes I wrote:
GameOfLife.java
public class GameOfLife {
public static void main(String[] args) throws InterruptedException {
Gui gui = new Gui();
int [][] array = new int [71][71];
//Field edge is getting filled up with '2'
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array.length; j++){
array[i][j] = 2;
}
}
//Field gets filled up with '0' = dead cells
for(int i = 1; i < array.length - 1; i++){
for(int j = 1; j < array.length - 1; j++){
array[i][j] = 0;
}
}
//A glider
array[9][5]=1;
array[9][6]=1;
array[9][7]=1;
array[8][7]=1;
array[7][6]=1;
Gui.printingOut(array);
}
public static void applyRules(int[][] array) {
int[][] newArray = new int[array.length][array.length];
for(int y = 1; y < array.length - 1; y++) {
for(int x = 1; x < array.length - 1; x++) {
int neighbors = neighborsCounter(array, x, y);
if(array[x][y] == 1) {
if((neighbors < 2) || (neighbors > 3)) {
newArray[x][y] = 0;
}
if((neighbors == 2) || (neighbors == 3)) {
newArray[x][y] = 1;
}
}
else if(array[x][y] == 0){
if(neighbors == 3) {
newArray[x][y] = 1;
}
else {
newArray[x][y] = 0;
}
}
}
}
for(int i = 1; i < array.length - 1; i++){
for(int j = 1; j < array.length - 1; j++) {
array[i][j] = newArray[i][j];
}
}
}
public static int neighborsCounter(int[][] array, int x, int y) {
int neighbors = 0;
for(int i = x - 1; i <= x + 1; i++) {
for(int j = y - 1; j <= y + 1; j++) {
if(array[i][j] == 1) {
neighbors = neighbors + 1;
}
}
}
if(array[x][y] == 1) {
neighbors = neighbors - 1;
}
return neighbors;
}
}
And here's the class for the GUI:
Gui.java
import javax.swing.*;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class Gui {
static JPanel panel;
static JFrame frame;
static int count = 0;
static boolean[] test = new boolean[1];
public static void helper() {
count++;
if(count % 2 != 0) {
test[0] = false;
}
else {
test[0] = true;
}
}
public static void GraphicalInterface(int[][] array, Graphics graphic) {
int BOX_DIM = 10;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if(array[i][j] == 0) {
graphic.setColor(Color.WHITE);
}
if(array[i][j] == 1) {
graphic.setColor(Color.BLACK);
}
if(array[i][j] == 2) {
graphic.setColor(Color.RED);
}
graphic.fillRect(i * BOX_DIM, j * BOX_DIM, BOX_DIM, BOX_DIM);
}
}
}
public static void printingOut(int[][] array) throws InterruptedException {
test[0] = true;
JFrame frame = new JFrame("Conway's Game of Life");
frame.setSize(830,800);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(710, 710));
JButton button = new JButton("Start / Stop");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
helper();
}
});
frame.add(button, BorderLayout.EAST);
frame.add(panel, BorderLayout.WEST);
frame.setVisible(true);
while(true) {
//Printing out
while(Gui.test[0]) {
Graphics graphic = panel.getGraphics();
GraphicalInterface(array, graphic);
//Applying rules
GameOfLife.applyRules(array);
Thread.sleep(249);
}
Thread.sleep(1);
}
}
}
I would appreciate any suggestions to improve my code.