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

  • Home
  • SEARCH
  • 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 5945443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:40:56+00:00 2026-05-22T16:40:56+00:00

My board correctly detects groups with less than 3 neighbors and kills them off,

  • 0

My board correctly detects groups with less than 3 neighbors and kills them off, but doesn’t seem to detect and give birth to cells with 3 neighbors.

Any thoughts?

If I haven’t provided enough information let me know, and I can paste more of the code, but I think this is all of the relevant parts.

Thank you in advance for any advice offered.

public boolean getCell(int row, int col) {
    boolean state = board[row][col];
    int neighbors = 0;
    for (int x = row-1; x <= row+1; x++) {
        for (int y = col-1; y <= col+1; y++) {
            // don't include this
            if ((x != row || y != col) && x != -1 && y != -1 
            && x != NROWSCOLS && y != NROWSCOLS) {
                if (board[x][y] == ALIVE){
                    neighbors ++;
                }
            }
        }
    }
    if (neighbors > 3 || neighbors < 2)
        state = DEAD;
    else if(neighbors == 3)
        state = ALIVE;
    return state;
}

Here is the lifeCycle method requested.

/** Process one life cycle of the cellular automaton
 * 
 */
public void lifeCycle() {
    for (int x = 0; x < NROWSCOLS ; x++) {
        for (int y = 0; y < NROWSCOLS; y++) {
            getCell(x,y);
        }
    }

    generations ++;
}

I’ve attached the LifeGUI for reference, but this code is provided and not intended for me to change.

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


public class LifeGUI extends JPanel {
    // game instance variables
    private Life board;        // game board

    // GUI components
    private JLabel generationsLived;          
    private JButton resetButton, cycleButton;   // reset control and cycle control
    private Cell[][] cells;         // board cells for display

    /** Construct new Life game with a graphical user interface */
    public LifeGUI()    {
        // create and initialize game board and display representation
        board = new Life();
        cells = new Cell[Life.NROWSCOLS][Life.NROWSCOLS];

        // set layout for game display
        setLayout(new BorderLayout());

        // Create board cells and add to display
        JPanel boardPanel = new JPanel();
        boardPanel.setLayout(new GridLayout(Life.NROWSCOLS, Life.NROWSCOLS));
        for (int row = 0; row < Life.NROWSCOLS; row++) {
            for (int col = 0; col < Life.NROWSCOLS; col++) {
                cells[row][col] = new Cell(Life.DEAD, row, col);
                boardPanel.add(cells[row][col]);
           }
        }
        add(boardPanel, BorderLayout.CENTER);


        // Set up 2 buttons
        // a reset button so it starts a new game when clicked
        // a cycle button to tell the Life automaton to live one cycle
        resetButton = new JButton("New Game");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                board.newGame();
                updateDisplay();
            }
        });

        cycleButton = new JButton("Live One Cycle");
        cycleButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                board.lifeCycle();
                updateDisplay();
            }
        });

        // Put the buttons and the generation count display on the screen
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(resetButton);
        buttonPanel.add(cycleButton);
        generationsLived = new JLabel("     Generations Lived: " , JLabel.RIGHT);
        buttonPanel.add(generationsLived);
        add(buttonPanel, BorderLayout.SOUTH);

        // show initial display
        updateDisplay();
    }

    /** Update display to match game state. */
    public void updateDisplay() {
        // update count display
        generationsLived.setText("     Generations Lived: " + board.getGenerationCount());

        // update board display
        for (int row = 0; row < Life.NROWSCOLS; row++) {
            for (int col = 0; col < Life.NROWSCOLS; col++) {
                cells[row][col].setState(board.getCell(row,col));
            }
        }
        repaint();
    }

    /** Create new game and a window to display it */
    private static void test() {
        JFrame f = new JFrame("The Game of Life");     // top-level window
        LifeGUI l = new LifeGUI();
        f.getContentPane().add(l);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600,600);
        f.validate();
        f.setVisible(true);
        f.toFront();
    }

    public static void main(String[] args) {
        // To support stand-alone application
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                LifeGUI.test();
            }
        });
    }
}
  • 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-22T16:40:56+00:00Added an answer on May 22, 2026 at 4:40 pm
    public boolean getCell(int row, int col) {
        boolean state = board[row][col];
        int neighbors = 0;//you are summing the alive neighbours 
           //keep var declaration outside the iteration over them
    
        for (int x = Math.max(0,row-1); x < Math.min(row+2,NROWSCOLS); x++) {
            for (int y = Math.max(0,col-1); y < Math.min(col+2,NROWSCOLS); y++) {
                //using min and max to ensure x and y remain between 0 and NROWSCOLS 
                //so no IOBException
                    if (board[x][y] == ALIVE){
                        neighbors ++;
    
                    }
                }
            }
        }
        if (neighbors > 3 || neighbors < 2)//only do the check when you are finished counting the alive neighbours
            state = DEAD;
        else if(neighbors == 3)
            state = ALIVE;
        return state;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create board for simple game, looks like for chess,but user could
So I have tried to find a answer but must not be searching correctly
Thanks to this board I have managed to retrieve the name and the price
On our Scrum board, tasks start at 'To Do', go to 'In Progress', and
I have an Arduino board connected to my PC's serial port. The board sends
The Ok6410 board comes with android 2.3 source code (including the kernel), I want
programming my arduino microcontroller board in C, I noticed a strange behaviour. Because of
I want a message board thread by default visible for a role. No need
I am writing a board game in Android where the UI consists of textViews
Using Bread::Board I have an object/service A with with accessor $A->foo . Object/service B

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.