Skip to main content
1 of 5

Conway's Game of Life in Java (DIFFERENT IMPLEMENTATION)

I have just finished implementing a version of Conway's Game of Life using Java. Being only a college student, I am sure that my code is no where near perfect, and was wondering if you guys could look at my code. What can I improve on? Are there faster ways to implement certain areas of my code? Where is there excess code that I can trim away? Is there a smarter way of implementing Conway's game of life? Thank you!! (Side Note: most comments have been taken out because they don't add much to the readability of the code)

CellularAutomaton.java:

package first;
public abstract class CellularAutomaton{
    public abstract String lifeCycle();
    public abstract boolean rules(int num);
}

GameOfLife.java:

package first; 
import java.util.Stack;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class GameOfLife extends CellularAutomaton {

int board[][];
int dim;
Stack<Integer> stackCells;
HashMap<Integer, Integer> hmapCells;

public gameOfLife(int d, Stack<Integer> s){
    board = new int[d][d];
    dim = d;
    stackCells = s;
    hmapCells = new HashMap<>();
}

public boolean rules(int num){
    return num == 3 || num == 12 || num == 13;
}

private void birth() {
    Iterator<Map.Entry<Integer,Integer>> it=hmapCells.entrySet().iterator();
    while(it.hasNext()) {
        Map.Entry<Integer,Integer> pair = it.next();
        int key = pair.getKey();

        if(rules(pair.getValue()))
        stackCells.add(key);

        board[key/dim][key%dim] = 0;
        it.remove();
    }
}

private void insertAlive() {
    while(!stackCells.isEmpty()) {
        int cell = stackCells.pop();

        int x = cell / dim;
        int y = cell % dim;
        int startX = (x <= 0) ? 0 : x - 1;
        int startY = (y <= 0) ? 0 : y - 1;
        int endX = (x >= dim - 1) ? x + 1 : x + 2;
        int endY = (y >= dim - 1) ? y + 1 : y + 2;

        for(int i = startX; i < endX; ++i) {
            for(int j = startY; j < endY; ++j) {
                hmapCells.put(i * dim + j, ++board[i][j]);
            }
        }
        hmapCells.put(cell, board[x][y] += 9);
    }
}

private String printBoard() {
    StringBuilder s = new StringBuilder();
    
    for(int elements[] : board) {
        for(int element : elements) {
            if(element >= 10){
                s.append("* ");
            }
            else {
                s.append("  ");
            }
        }
        s.append("\n");
    }
    
    return s.toString();
}

public String lifeCycle() {
    birth();
    insertAlive();
    return printBoard();
}

}

Simulation.java:

package first;

import java.util.Stack;

public class Simulation {
    public static void main(String args[]) throws InterruptedException{
        int dim = 70;
        Stack<Integer> init = new Stack<>();

        //all vals pushed to init is of the form: xPos * dim + yPos
        init.push(351);
        init.push(352);
        init.push(421);
        init.push(422);

        init.push(245); 
        init.push(246); 
        init.push(315); 
        init.push(316); 
        
        init.push(361); 
        init.push(431); 
        init.push(501); 
        init.push(292); 
        init.push(572);
        init.push(223); 
        init.push(643);
        init.push(224); 
        init.push(644);
        init.push(435);
        init.push(296);
        init.push(576);
        init.push(367);
        init.push(437);
        init.push(507);
        init.push(438);

        init.push(231);
        init.push(301);
        init.push(371);
        init.push(232);
        init.push(302);
        init.push(372);
        init.push(163);
        init.push(443);
        init.push(165);
        init.push(445);
        init.push(95);
        init.push(515);

        GameOfLife gOL = new GameOfLife(dim, init);

        while(true) {
            System.out.print(gOL.lifeCycle());
            Thread.sleep(100);
            System.out.print("\033[H\033[2J");  
        }
    }
}