Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6831591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:45:19+00:00 2026-05-26T22:45:19+00:00

I’m working on a Java implementation of Conway’s game of life as a personal

  • 0

I’m working on a Java implementation of Conway’s game of life as a personal project for myself. So far it works but the rules are coming out wrong. The expected patterns aren’t showing up as well as it should. Is there something wrong with my code?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Cell extends JComponent implements MouseListener {

  private int row, col;
  private boolean isLiving;

  public Cell(int r, int c) {
    this.row = r;
    this.col = c;
    this.addMouseListener(this);
  }

  public void isAlive(int neighbors) {
    if (this.isLiving) {
      if (neighbors < 2) {
        this.isLiving = false;
      } else if (neighbors == 2 || neighbors == 3) {
        this.isLiving = true;
      } else if (neighbors > 3) {
        this.isLiving = false;
      }
    } else {
      if (neighbors == 3) {
        this.isLiving = true;
      }
    }
  }

  public boolean isLiving() {
    return this.isLiving;
  }

  public void paintComponent(Graphics g) {
    if (this.isLiving) {
      g.fillRect(0, 0, 10, 10);
    } else {
      g.drawRect(0, 0, 10, 10);
    }
  }

  public void mouseClicked(MouseEvent e) {
    this.isLiving = !this.isLiving;
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:45:19+00:00Added an answer on May 26, 2026 at 10:45 pm

    I suspect1 the problem with your code is that it is setting the cells to living or dead as soon as the neighbors are checked. That caused my early variants of this code to fail. That change of state has to be delayed until the entire grid (biosphere) has been checked.

    This example shows typical Game of Life behavior.

    1. Note that “suspicions ain’t answers”, so it is best to post an SSCCE.

    Game of Life

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
    
    public class GameOfLife extends JPanel {
    
        private final int row, col;
        private boolean isLiving;
    
        public static Random random = new Random();
    
        public GameOfLife(int r, int c) {
            this.row = r;
            this.col = c;
            MouseListener listener = new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    isLiving = !isLiving;
                    repaint();
                }
            };
            this.addMouseListener(listener);
            isLiving = random.nextBoolean();
        }
    
        public boolean isAlive(int neighbors) {
            boolean alive = false;
            if (this.isLiving) {
                if (neighbors < 2) {
                    alive = false;
                } else if (neighbors == 2 || neighbors == 3) {
                    alive = true;
                } else if (neighbors > 3) {
                    alive = false;
                }
            } else {
                if (neighbors == 3) {
                    alive = true;
                }
            }
            return alive;
        }
    
        public void setAlive(boolean alive) {
            isLiving = alive;
        }
    
        public boolean isLiving() {
            return this.isLiving;
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (this.isLiving) {
                g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
            } else {
                g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
            }
        }
    
        public static void main(String[] args) {
            final int s = 40;
            final GameOfLife[][] biosphere = new GameOfLife[s][s];
            final JPanel gui = new JPanel(new GridLayout(s, s, 2, 2));
            for (int ii = 0; ii < s; ii++) {
                for (int jj = 0; jj < s; jj++) {
                    GameOfLife cell = new GameOfLife(ii, jj);
                    cell.setPreferredSize(new Dimension(10, 10));
                    gui.add(cell);
                    biosphere[ii][jj] = cell;
                }
            }
    
            ActionListener al = (ActionEvent ae) -> {
                boolean[][] living = new boolean[s][s];
                for (int ii = 0; ii < s; ii++) {
                    for (int jj = 0; jj < s; jj++) {
                        int top = (jj > 0 ? jj - 1 : s - 1);
                        int btm = (jj < s - 1 ? jj + 1 : 0);
                        int lft = (ii > 0 ? ii - 1 : s - 1);
                        int rgt = (ii < s - 1 ? ii + 1 : 0);
                        int neighbors = 0;
                        if (biosphere[ii][top].isLiving()) {
                            neighbors++;
                        }
                        if (biosphere[ii][btm].isLiving()) {
                            neighbors++;
                        }
                        if (biosphere[lft][top].isLiving()) {
                            neighbors++;
                        }
                        if (biosphere[lft][btm].isLiving()) {
                            neighbors++;
                        }
                        if (biosphere[lft][jj].isLiving()) {
                            neighbors++;
                        }
                        if (biosphere[rgt][jj].isLiving()) {
                            neighbors++;
                        }
                        if (biosphere[rgt][top].isLiving()) {
                            neighbors++;
                        }
                        if (biosphere[rgt][btm].isLiving()) {
                            neighbors++;
                        }
                        living[ii][jj] = biosphere[ii][jj].isAlive(neighbors);
                    }
                }
                for (int ii = 0; ii < s; ii++) {
                    for (int jj = 0; jj < s; jj++) {
                        biosphere[ii][jj].setAlive(living[ii][jj]);
                    }
                }
                gui.repaint();
            };
    
            Timer timer = new Timer(50, al);
            timer.start();
    
            JOptionPane.showMessageDialog(null, gui);
            timer.stop();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.