dahvheed
dahvheed

Reputation: 1

There are no updates among different iterations of the code (Conway's Game of Life)

This program is attempting to recreate Conway's Game of Life.

The rules that this code is trying to apply:

The issue is the my code outputs no change among the different iterations, even though there is obviously supposed to be.

Any help or ideas with some of the logic being used in the portion that updates the cells would be greatly appreciated.

I have tried printing out different variables and cells that are filled, but everything (in that regard) seems to be working properly.

Apologizes for not being more in-depth, I am honestly unsure what the error with the code is. Thanks for help in advance.

import java.util.*;
import java.io.*;

public class Game_Of_Life {

    public static void main(String[] args) throws IOException {

        final int runs = 5;
        int organisms;
        String[][] real = new String[20][20];
        String[][] test = new String[20][20];
        Scanner reader = new Scanner(new File("life100.txt"));

        for(int i = 0; i < real.length; i++) {
            for(int g = 0; g < real.length; g++) {
                real[i][g] = test[i][g] = " ";
            }
        }

        while(reader.hasNext()) {
            real[reader.nextInt()-1][reader.nextInt()-1] = "*";
            test[reader.nextInt()-1][reader.nextInt()-1] = "*";
        }
        reader.close();

        for(int j=0; j<runs; j++) {

            for(int i = 0; i < real.length; i++) {
                for(int g = 0; g < real.length; g++) {

                    int neigh = neighbors(real, i, g);
                    if(test[i][g].equals("*")) {
                        if(neigh<2 || neigh>3) {
                            real[i][g] = " ";
                        }
                    }
                    else {
                        if(neigh == 3) {
                            real[i][g] = "*";
                        }
                    }
                }
            }

            for(int i = 0; i < real.length; i++) {
                for(int g = 0; g < real.length; g++) {
                    real[i][g] = test[i][g];
                }
            }

            System.out.println("     1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20\n");
            for(int i = 0; i < real.length; i++) {
                System.out.print((i+1) + "   ");
                for(int g = 0; g < real.length; g++) {
                    System.out.print(" " + test[i][g] + " ");
                }
                System.out.println();
            }

        }

    }

    public static boolean able(int row, int col, int N) {
        if (row >= 0 && col >= 0 && row < N && col < N) {
            return true;
        }
        else {
            return false;
        }
    }

    public static int neighbors(String[][] ray, int row, int col) {

        int neighbor=0;
        int[] rows = {row-1, row-1, row-1, row, row, row+1, row+1, row+1};
        int[] cols = {col-1, col, col+1, col-1, col+1, col-1, col, col+1};

        for(int i=0; i<8; i++) {
            if(able(rows[i], cols[i], 20) && ray[rows[i]][cols[i]].equals("*")) {
                neighbor++;
            }
        }

        return neighbor;
    }

}

Actual Results: Cells do not become alive or dead after the five iterations I have it running.

Upvotes: 0

Views: 61

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39142

Seems to me like all of your checks should be against the "test" array, so change to:

int neigh = neighbors(test, i, g);

Then, once all checks have been made and changes to "real" are done, copy everything from "real" back to "test".

So change:

real[i][g] = test[i][g];

To:

test[i][g] = real[i][g];

Upvotes: 0

Related Questions