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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:05:17+00:00 2026-05-22T02:05:17+00:00

I am trying to write source based on the Game Of Life simulation concept.

  • 0

I am trying to write source based on the “Game Of Life” simulation concept. What I did was create a class to represent the game environment state but separated the logic of the successor function in a different class. The state of the game is saved in a simple multidimensional array of booleans.

What the successor function is supposed to do is save the current state in a new array as a clone of the original and use this as basis throughout the process. It then takes the current state of the game as a reference and modifies this using the game logic. My problem is that I cloned the original into a new variable, but even this clone gets modified by the successor function.

I’m not so sure what’s happening so I can’t debug properly. I’m more accustomed to C#’s handling of primitive types clones.

Here is my code for the successor function wrapper class:

public class Successor {

boolean[][] current_state;
public void next(WorldPopulation worldState){
    int dim_x = worldState.get_width();
    int dim_y = worldState.get_height();
    boolean[][] nextstate = worldState.get_state();
    current_state = worldState.get_state().clone();
    for(int i=0; i<dim_y; i++){
        for(int j=0; j<dim_x; j++){
            int neighbors = 0;
            neighbors = countThyNeighbors(i, j);
            if((neighbors>3 || neighbors<3) && current_state[i][j]){
                nextstate[i][j] = false;
            } else if(!current_state[i][j] && neighbors>=3) { nextstate[i][j] = true; }
        }
    }
}
private int countThyNeighbors(int y, int x) {
    int[] grid_x = { x-1, x, x+1, x-1, x+1, x-1, x, x+1};
    int[] grid_y = { y-1, y-1, y-1, y, y, y+1, y+1, y+1};
    int neighbors = 0;
    for(int i=0; i<8; i++){
        try{
        if(current_state[grid_y[i]][grid_x[i]])
            neighbors++;
        } catch(Exception e) { continue;}
    }
    return neighbors;
}

}

The get() and set() functions in the WorldPopulation class are merely assignment and return operations.

I know a bit about nuances of cloning (deep and shallow clones) from a C# background, but I am totally new in Java paradigms.

Any suggestions to improve my code is much appreciated, but I want to know what’s happening and what I’m doing wrong. Thank you very much! 😀

  • 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-22T02:05:17+00:00Added an answer on May 22, 2026 at 2:05 am

    Multi-dimensional arrays are represented as arrays of arrays.
    A clone method in Java is shallow. that means multidimentional array is not cloned deeply.

    You can implement your own deep copy method or use serialization as descibed here

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

Sidebar

Related Questions

I'm trying to write a multi-file patch for an open-source project, but the master
I'm trying create a simple audio stream server like a concept proof, but I'm
I'm trying to write a function : Input: Source range, Source value, output range,
I've been trying to write an open-source profile provider to work against PostgreSQL (I
I am trying to write a program to check that some C source code
I am trying to write the following function in C, char* modelstrdup(char* source); The
I'm trying to write a program which will pseudorandomly autogenerate (based on a seed
I am trying to write a simple physics simulation where balls with varying radii
I'm trying to write this down as concisely as possible, but it's not easy
I'm trying to write a check to redirect users based on their progress through

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.