寻找邻居的问题(Conways Game of Life)

时间:2017-01-08 04:01:50

标签: java conways-game-of-life

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;
    }
}

输出:

我在这里尝试做的是让代码计算每个单元格中的邻居,玩家提示放入哪个区域,每当我开始游戏时(在我选择单元格所在的位置之后),所有单元格都来了我开始比赛后就已经死了。

 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 个答案:

答案 0 :(得分:0)

有一个问题,你的switch语句坏了:

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

由于没有break语句,默认条件将始终被调用,这意味着无论邻居计数如何,您的单元格都将无效。使用休息来解决这个问题:

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;
}

另一个问题是,当您检查网格时,您似乎正在更改网格,这会导致错误的结果。您总是必须使用两个网格:您正在检查的网格和保存下一代结果的网格,以便在检查第一个网格时对第二个网格进行的更改没有副作用。

事实上你有另一个2D阵列就是为了这个目的而存在,finalform,但你似乎并没有使用它,这引出了一个问题 - 为什么你有它如果你是不使用它?