Issues for finding neighbours (Conways Game of Life)

224 views Asked by At
package GameofLife;

import java.util.Scanner;

public class GameOfLife {
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        String[][] world;
        String[][] finalform;
        world = new String[20][20];
        finalform = new String[20][20];
        String alivecell = "A";
        String deadcell = "D";
        int day = 0;
        for (int a = 0; a < world.length; a++) {
            for (int a2 = 0; a2 < world.length; a2++) {
                world[a][a2] = deadcell;
                // shows every cell as dead
            }
        }
        int more = 1;// meant for if the player is wanting to add more living
                     // cells onto the game
        while (more == 1) {
            int c = 21;// column
            int r = 21;// row
            while (c > 20 || r > 20) {// since the game is in a 20x20 grid, it
                                      // will loop the code below if the column
                                      // or row is above 20
                System.out.println("Which column do you want a cell to be on?");
                c = input.nextInt();
                System.out.println("Which row do you want a cell to be on? ");
                r = input.nextInt();
                c = c - 1;
                r = r - 1;
            }
            day = 1;/// a day of living cell(s) has begun
            System.out.println("Day " + day);
            for (int a = 0; a < 20; a++) {
                for (int a2 = 0; a2 < 20; a2++) {
                    world[c][r] = alivecell;// WHICH CELLS SHOULD BE ALIVE
                    System.out.format("%3s", world[a][a2]);// output shown in
                                                           // 20x20 grid
                }
                System.out.println();
            }
            System.out.println("add more living cells (type 1) or start game (press any other button)?");
            more = input.nextInt();
            if (more != 1) {
                game(world, alivecell, deadcell, 0, finalform);
            }
        }
    }

    public static void game(String[][] world, String alivecell, String deadcell, int day,
            String[][] finalform) {/// coming from the pseudo code in the text
                                   /// book
        int cont = 1;
        day++;
        System.out.println("Day " + (day));
        while (cont == (1)) {
            for (int c = 0; c < world.length; c++) {
                for (int r = 0; r < world.length; r++) {
                    int neighbours = n(c, r, world.length, world, alivecell, deadcell); // finds
                                                                                        // neighbours
                    switch (neighbours) {
                    case 0:
                    case 1:
                        world[c][r] = deadcell;
                    case 2:
                    case 3:
                        world[c][r] = alivecell;
                    default:
                        world[c][r] = deadcell;
                    }
                    System.out.format("%3s", world[c][r]);
                }
                System.out.println();
            }
            // outputs the world with the edits
            System.out.println("continue to next day? press 1 ");
            cont = input.nextInt();
        }
    }

    public static int n(int c, int r, int length, String[][] world, String alivecell,
            String deadcell) {
        int neighbours = 0;
        for (int c2 = (c - 1); c2 <= c + 1; c2++) {
            for (int r2 = (r - 1); r2 <= r + 1; r2++) {
                if ((r > 0) && (r < world.length - 1) && (c > 0) && c < world.length - 1) {
                    if (world[c2][r2] == alivecell) {
                        neighbours++;
                    }
                }
            }
            if (r == 0 && c == 0) {// on corners
                if (world[(0 + 1)][(0 + 1)] == alivecell) {
                    neighbours++;
                }
                if (world[0 + 1][0] == alivecell) {
                    neighbours++;
                }
                if (world[0][0 + 1] == alivecell) {
                    neighbours++;
                }
            } else if (r == 0 && c == 19) {
                if (world[19][0] == alivecell) {
                    if (world[(19 - 1)][0 + 1] == alivecell) {
                        neighbours++;
                    }
                    if (world[(19)][0 + 1] == alivecell) {
                        neighbours++;
                    }
                    if (world[(19 - 1)][0] == alivecell) {
                        neighbours++;
                    }
                }
                try {
                } catch (IndexOutOfBoundsException a) {
                    if (c == 19 && r != 0) {
                        for (int cno = c - 1; cno <= c; cno++) {
                            for (int rno = r; rno <= r + 1; rno++) {
                                if (world[cno][rno] == alivecell) {
                                    neighbours++;
                                }
                            }
                        }
                    }
                    if (c == 0 && r != 19) {
                        for (int cno1 = c; cno1 <= c + 1; cno1++) {
                            for (int rno1 = r - 1; rno1 <= r; rno1++) {
                                if (world[cno1][rno1] == alivecell) {
                                    neighbours++;
                                }
                            }
                        }
                    }
                    continue;
                }
            }
        }
        return neighbours;
    }
}

OUTPUT:

What I am trying to do here is make the code count the neighbours in each cell that the player prompts to put in what area, whenever I start the game (after I choose where the cell should be), all the cells come out as dead after I start the game.

 Which column do you want a cell to be on?
 2
  Which row do you want a cell to be on? 
  2
  Day 1
   D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
   D  A  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
add more living cells (type 1) or start game (press any other button)?
1
Which column do you want a cell to be on?
2
Which row do you want a cell to be on? 
3
Day 1
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  A  A  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
add more living cells (type 1) or start game (press any other button)?
1
Which column do you want a cell to be on?
2
Which row do you want a cell to be on? 
4
Day 1
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  A  A  A  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
add more living cells (type 1) or start game (press any other button)?
2
  Day 1
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D  D
continue to next day? press 1 
1

There are 1 answers

3
Hovercraft Full Of Eels On

One problem, your switch statement is broken:

switch (neighbours) {
case 0:
case 1:
    world[c][r] = deadcell;
case 2:
case 3:
    world[c][r] = alivecell;
default:
    world[c][r] = deadcell;
}

Because there are no break statements, the default condition will always be called, meaning your cells will be dead no matter the neighbor count. Use breaks to fix this:

switch (neighbours) {
case 0:
case 1:
    world[c][r] = deadcell;
    break;
case 2:
case 3:
    world[c][r] = alivecell;
    break;
default:
    world[c][r] = deadcell;
}

Another issue is that you appear to be changing your grid as you're checking it, which will lead to false results. You always must use two grids: the one you're checking and the one that holds the next generation result, so that changes made to the 2nd grid while checking the first one, don't have side effects.

And in fact you have another 2D array present just for this purpose, finalform, but you don't appear to ever be using it, which begs the question -- why do you have it if you're not using it?