mardi 3 mars 2020

So for some reason when im working with 2 2D arrays of abstract objects in java it changes both when it should only change one

So I have two arrays grid[22][22] and save[22][22] they are made up of germs that can be either dead or alive. I am doing Conway's game of life scenario here. When I change the "Germ" in save. it for some reason changes it in gird as well.

 ++generation;
        for(int r =0;r<grid.length;++r) {
            for (int c = 0; c < grid[r].length; ++c) {
                if(!grid[r][c].getEdge()) {
                    int around = 0;
                    if (grid[r - 1][c + 1].getAlive()){
                        ++around;}
                    if (grid[r][c + 1].getAlive()){
                        ++around;}
                    if (grid[r + 1][c + 1].getAlive()){
                        ++around;}
                    if (grid[r - 1][c].getAlive()){
                        ++around;}
                    if (grid[r + 1][c].getAlive()){
                        ++around;}
                    if (grid[r - 1][c - 1].getAlive()){
                        ++around;}
                    if (grid[r][c - 1].getAlive()){
                        ++around;}
                    if (grid[r + 1][c - 1].getAlive()){
                        ++around;}
                    if (!grid[r][c].getAlive() && (around == 3)){
                        save[r][c] = new Germ(r,c,true,false); **`This changes grid for some reason`**
                    } else if ((grid[r][c].getAlive() && around < 2) || (grid[r][c].getAlive() && around > 3)){
                        save[r][c] = new Germ(r,c,false,false);     **` so does this line`**
                    }
                }
            }
        }
        grid = save;
    }

Aucun commentaire:

Enregistrer un commentaire