3

I am currently working on the Conway's Game of life program in Eclipse with @Test cases. All of my methods pass their tests except for the neighborCount method. I have seen posts of this method working with for loops and for some reason it does not work on my code.

I am trying to wrap around the 2D array by locating neighbor cells with only for loops. I am also having trouble with updating the new society after counting the neighbors. If someone could please take a look at my code and locate the error in my methods, it would be much appreciated. Thank you in advance. I have attached all of my code in case I have an error in another method affecting the neighborCount().

    public class GameOfLife {

    private int theRows;
    private int theCols;
    private char[][] society;


    public GameOfLife(int rows, int cols) {
        // Complete this method.
        society = new char[rows][cols];
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                society[r][c] = ' ';
            }
        }
        theRows = rows;
        theCols = cols;
    }

    public int numberOfRows() {

        return theRows;
    }


    public int numberOfColumns() {

        return theCols;
    }

    public void growCellAt(int row, int col) {
        // Complete this method
        for (int r = 0; r < society.length; r++) {
            for (int c = 0; c < society[r].length; c++) {
                society[r][c] = 'o';
            }
        }

    }

    public boolean cellAt(int row, int col) {
                if (society[row][col] == 'o') {
                    return true;
                } else { 
                    return false;
                }

        }

    @Override
    public String toString() {
        String res = "";
        for (int r = 0; r < society.length; r++) {
            for (int c = 0; c < society[r].length; c++) 
                res = res + society[r][c];

        }
        return res;
    }

    public int neighborCount(int row, int col) {

        int count = 0;
        for(int i = row - 1; i <= row + 1; i++) {
            if (i >= 0 && i >= society.length)
                for(int j = col - 1; j <= col + 1; j++) 
                    if (j >= 0 && j >= society[i].length) 
                        if (i != row || j != col) 
                            if (society[i][j] == 'o') 
                                count++;
        }

        return count;
    }

    public void update() {
        // Complete this method
        char[][] newSociety = new char[society.length][society[0].length];

        for (int r = 0; r < society.length; r++) {
            for (int c = 0; c < society[r].length; c++) 
                newSociety[r][c] = society[r][c];
        }
    }
}
user4590197
  • 35
  • 1
  • 1
  • 7
  • A note about the code: toString() method is too slow because it grows a string character by character, reallocating string with each character. Consider using StringBuilder. – Serge Rogatch Jun 19 '15 at 06:43
  • Oh okay, that makes sense! Thank you for adding that @SergeRogatch – user4590197 Jun 19 '15 at 06:53

1 Answers1

4

Looks like you have >= instead of < in two places :

public int neighborCount(int row, int col) {

    int count = 0;
    for(int i = row - 1; i <= row + 1; i++) {
        if (i >= 0 && i < society.length) // fixed here
            for(int j = col - 1; j <= col + 1; j++) 
                if (j >= 0 && j < society[i].length) // fixed here
                    if (i != row || j != col) 
                        if (society[i][j] == 'o') 
                            count++;
    }

    return count;
}

If you are going to access society[i][j], you want to make sure 0 <= i < society.length and 0 <= j < society[i].length.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thank you for replying and helping... But okay, I've just changed that in my program but for some reason it says it has counted 8 total counts when it is expected to count only 0. I'm thinking I have an error somewhere else in my program which is effecting the neighborCount method. – user4590197 Jun 19 '15 at 06:24
  • @user4590197 Perhaps you are not updating the `society` array correctly. If you got 8, it means that all the neighbors of `society[row][col]` contain 'o'. – Eran Jun 19 '15 at 06:29
  • OMG! You are absolutely correct! I was using loops for no reason in my growCellAt method. Thank you so much! The most simplest error caused me this much time lol. Thank you! – user4590197 Jun 19 '15 at 06:41