0

I'm currently working on a version of Conway's Game of Life with Netbeans IDE and I wanted to store cells in a matrix. For the operation of going to the Next generation of cells, I would return a new matrix of cells which is calculated from the inputting matrix.

The Code is the following:

public static Cell[][] nextGen(Cell[][] CellList)
        { 
            Cell[][] Copy = CellList.clone();
            
            for (int i = 0; i<Copy.length; i++)
            {
                for(int n = 0; n<Copy[i].length; n++)
                {
                    if (Copy[i][n].isAlive())
                    {
                        if (Cell.count(Copy, i, n) <= 1 || Cell.count(Copy, i, n) >= 4 )
                        {
                            CellList[i][n].kill();
                        }
                    }else
                    {
                        if (Cell.count(Copy, i, n) == 3)
                        {
                            CellList[i][n].born();
                        }
                    }
                }
            }
            
            return CellList;
        }

The Class is called "Cell" it has a private boolean property "alive" which can be set to false with the public method kill() or true with the public method born(). Everything except the method for counting alive cells surrounding a specific cell and the method for calculating the new generation is nonstatic.

The Problem why it isn't working is that if I make any changes to the input matrix "CellList", the same thing happens in the copy of this matrix.

How can I let the copy have the same Values but only make changes in the input matrix? Thanks for the helping!

2
  • Does this answer your question? Java: Copy array of non-primitive type Commented Jul 4, 2021 at 19:11
  • It's been awhile since I've used Java, but clone() is surely a shallow copy; meaning the inside rows aren't copied. You'd need to make a deepcopy of it instead. The way I do it though is just to create two arrays at the very start, then swap them once per tick. They each take turns being the reading/writing matrix. Commented Jul 4, 2021 at 19:11

1 Answer 1

0

What you are doing is shallow copy, what you need is deep copy. Try this

public class Cell {

boolean alive = false;

protected Cell clone() {
    return new Cell(this);
}

public Cell() {

}

public Cell(Cell cell) {
    this.alive = cell.alive;
}

boolean isAlive() {
    return alive;
}

void kill() {
    alive = false;
}

void born() {
    alive = true;
}

static int count(Cell[][] cell, int j, int k) {
    return 1;
}

public static void main(String[] args) {
    Cell[][] CellList = new Cell[2][3];
    CellList[0][1] = new Cell();
    nextGen(CellList);
}

public static Cell[][] nextGen(Cell[][] CellList) {

    Cell[][] Copy = deepArrayCopy(CellList);

    for (int i = 0; i < Copy.length; i++) {
        for (int n = 0; n < Copy[i].length; n++) {
            if (Copy[i][n].isAlive()) {
                if (Cell.count(Copy, i, n) <= 1 || Cell.count(Copy, i, n) >= 4) {
                    CellList[i][n].kill();
                }
            } else {
                if (Cell.count(Copy, i, n) == 3) {
                    CellList[i][n].born();
                }
            }
        }
    }

    return CellList;
}

public static Cell[][] deepArrayCopy(Cell[][] celllist) {
    Cell[][] copy = new Cell[celllist.length][celllist[0].length];
    for (int i = 0; i < celllist.length; i++) {
        for (int k = 0; k < celllist[i].length; k++) {
            if (celllist[i][k] != null)
                copy[i][k] = celllist[i][k].clone();
        }
    }
    return copy;
}

}

Sign up to request clarification or add additional context in comments.

1 Comment

I'll repeat what I said in an above comment though: while this works, there are better ways. Creating an entire deep copy once per tick is extremely inefficient, and will likely be the bottleneck of the program. Just create two separate nested arrays at the start of the program and swap them after each tick. Write to one, read from the other, then swap roles after the generation is done. I can say from experience that this is much better in the long run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.