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 8619817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:25:54+00:00 2026-06-12T06:25:54+00:00

so im programming conways game of life in GUI form. the output is not

  • 0

so im programming conways game of life in GUI form. the output is not correct and i dont know why. this is the code that handles the “next generation”.

for(int i=0; i < ROW; i++) {
    for(int j=0; j < COL; j++) {
        if(i > 0 && i < ROW-1 && j > 0 && j < COL -1) {
            if(grid.getButton(i-1, j-1).getBackground() == Color.BLUE) liveNeighbor++;
            if(grid.getButton(i-1, j).getBackground() == Color.BLUE) liveNeighbor++;
            if(grid.getButton(i-1, j+1).getBackground() == Color.BLUE) liveNeighbor++;
            if(grid.getButton(i, j-1).getBackground() == Color.BLUE) liveNeighbor++;
            if(grid.getButton(i, j+1).getBackground() == Color.BLUE) liveNeighbor++;
            if(grid.getButton(i+1, j-1).getBackground() == Color.BLUE) liveNeighbor++;
            if(grid.getButton(i+1, j).getBackground() == Color.BLUE) liveNeighbor++;
            if(grid.getButton(i+1, j+1).getBackground() == Color.BLUE) liveNeighbor++;

            if(grid.getButton(i, j).getBackground() == Color.WHITE) {
                if(liveNeighbor == 3) 
                    newGrid.getButton(i, j).setBackground(Color.BLUE);
                } else {
                    if(liveNeighbor > 3 || liveNeighbor < 2) 
                        newGrid.getButton(i, j).setBackground(Color.WHITE);
            }
            liveNeighbor=0;
        }
    }
}

maybe im missing something, but im pretty sure this is right. any suggestions?

  • 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-06-12T06:25:56+00:00Added an answer on June 12, 2026 at 6:25 am

    This is the problem:

    if(grid.getButton(i, j).getBackground() == Color.WHITE) {
        if(liveNeighbor == 3) 
            newGrid.getButton(i, j).setBackground(Color.BLUE);
        } else {
            if(liveNeighbor > 3 || liveNeighbor < 2) 
                newGrid.getButton(i, j).setBackground(Color.WHITE);
    }
    

    Your code only sets the color if the current tile is dead. If it’s alive, it will never become dead. I would write this as:

    boolean currentlyAlive = grid.getButton(i, j).getBackground() == Color.BLUE;
    boolean aliveNextStep = (liveNeighbour == 3) ||
                            (currentlyAlive && liveNeighbor == 2);
    newGrid.getButton(i, j).setBackground(aliveNextStep ? Color.BLUE : Color.WHITE);
    

    I’d also separate the logical data (a grid of boolean values) from the display part – the “business logic” here shouldn’t know anything about colors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Programming is so new to me that I apologize for not knowing how to
Programming language: Java Ok, so I want to have a BufferedImage that keeps rotating
Programming language books usually explain that value types are created on the stack, and
im programming in c++ using ado, the documnet on tsql says the OUTPUT clause
I try to realize a little game project to dive deeper into OO programming
Programming Environment: Visual Studio 2010 Programming Language: VB.NET I have a tabbed web-browser that
Programming In Scala explains that tuples' _N numbers are one-based, instead of zero-based, because
Programming in C#.NET 4.0 is my latest passion, and I would like to know
Programming in Flex 4.5 I'm getting a date as a String. I don't know
(programming in iPhone objective-C) I have a class level NSString* that I create, add

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.