Question

I am making a basic version of "The Game of Life" in Java, but I cannot seem to get the cells to render correctly. Below are the only two classes in the package.

This is the first class:

package gameLogics;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Game extends Canvas implements Runnable {

    public static final int width = 128;
    public static final int height = 96;
    public static final String name = "The Game of Life";
    public static final int cellWidth = 6;

    private JFrame frame;

    public static boolean running = false;

    public static ObjCell[] objCell = new ObjCell[width * height];

    public Game() {

        setPreferredSize(new Dimension(width * cellWidth, height * cellWidth));

        frame = new JFrame(name);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(this, BorderLayout.CENTER);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        // addKeyListener(this);

        for (int i = 0; i < objCell.length; i++) {
            objCell[i] = new ObjCell(i % Game.height, (int) Math.floor(i
                    / Game.height));
            objCell[i].on = Math.random() < 0.5;
        }

    }

    public static void main(String[] args) {

        new Game().start();

    }

    private void start() {

        running = true;
        new Thread(this).start();

    }

    public void run() {

        while (running) {

            // for (int i = 0; i < objCell.length; i++) {
            // if (objCell[i] == null) {
            // System.out.println("objCell[" + i
            // + "] was the first null value.");
            // break;
            // }
            // }

            for (int i = 0; i < objCell.length; i++)
                objCell[i].countNeighbours();
            for (int i = 0; i < objCell.length; i++)
                objCell[i].changeState();

            render();

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

    public void render() {

        BufferStrategy bs = getBufferStrategy();

        if (bs == null) {
            createBufferStrategy(3);
            bs = getBufferStrategy();
        }

        Graphics2D g = (Graphics2D) bs.getDrawGraphics();

        // Draw background
        g.setPaint(Color.white);
        g.fill(new Rectangle2D.Double(0, 0, width * cellWidth, height
                * cellWidth));

        // Draw cells
        g.setPaint(Color.black);
        for (int i = 0; i < objCell.length; i++) {
            if (objCell[i].on)
                g.fill(new Rectangle2D.Double(objCell[i].x * cellWidth,
                        objCell[i].y * cellWidth, cellWidth, cellWidth));
        }

        g.dispose();

        bs.show();

    }

}

This is the second class:

package gameLogics;

public class ObjCell {

    public int x, y;
    public boolean on = false;

    private short neighbours;

    public ObjCell(int x, int y) {

        this.x = x;
        this.y = y;

    }

    public void countNeighbours() {

        neighbours = 0;
        if (inScreen(x - 1, y - 1)
                && Game.objCell[x - 1 + (y - 1) * Game.height].on)
            neighbours++;
        if (inScreen(x, y - 1) && Game.objCell[x + (y - 1) * Game.height].on)
            neighbours++;
        if (inScreen(x + 1, y - 1)
                && Game.objCell[x + 1 + (y - 1) * Game.height].on)
            neighbours++;
        if (inScreen(x + 1, y) && Game.objCell[x + 1 + y * Game.height].on)
            neighbours++;
        if (inScreen(x + 1, y + 1)
                && Game.objCell[x + 1 + (y + 1) * Game.height].on)
            neighbours++;
        if (inScreen(x, y + 1) && Game.objCell[x + (y + 1) * Game.height].on)
            neighbours++;
        if (inScreen(x - 1, y + 1)
                && Game.objCell[x - 1 + (y + 1) * Game.height].on)
            neighbours++;
        if (inScreen(x - 1, y) && Game.objCell[x - 1 + y * Game.height].on)
            neighbours++;

    }

    public void changeState() {

        on = neighbours == 2 && on || neighbours == 3;

    }

    private boolean inScreen(int x, int y) {

        return x >= 0 && x < Game.width && y >= 0 && y < Game.height;

    }

}

I'm sorry that I can't provide more information, but I don't really have any idea what is wrong.

Thanks in advance.

Was it helpful?

Solution

How do you mean you cannot get the cells to render correctly?

A few possible issues could be (However without fully understanding your code, I would not be able to give a clear answer - This would actually be more suited as a comment however I cannot comment):

  • Are you setting all the "on" values to be false / True before (I would assume there are initial seeds you need to populate?)
  • You are defining a 1D array... ObjCell[]... you could possibly use a 2D Array (ObjCell[width][height] or similar) and change your check neighbours logic?

This link might help: Game of Life in Java, Overpopulation but can't figure out why

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top