Java生活游戏没有给出理想的输出?

时间:2015-10-22 05:00:20

标签: java algorithm logic conways-game-of-life

我试图在Java中创造康威的生命游戏,但出于某种原因,这些细胞并没有栩栩如生,并且应该死亡?我想知道我的比较算法是不正确的?在要求多次运行evolve函数之后,它只是一遍又一遍地打印相同的东西....

项目4:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Project4 {

    private static void printGame() {
        for (char[] row : GameOfLife.grid) {
            for (char c : row) {
                System.out.print(c);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in); // Created a scanner
        System.out.println("Enter the file name you would like to use");
        File file = new File(input.nextLine()); // Takes file name to find file
        BufferedReader br = null;
        String line;
        int i = 0;

        try {
            br = new BufferedReader(new FileReader(file));
            while ((line = br.readLine()) != null) {
                for (int col = 0; col < line.length(); col++) {
                    GameOfLife.grid[i][col] = line.charAt(col);
                }
                i++;
                if (i == 25) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                br.close();
            }
        }

        // Prints the initial environment
        System.out.println("Initial set: ");
        printGame();

        while (true) {
            System.out.println("Do you want to see the next generation? Y/N?");
            String q = input.nextLine();
            if (q.equalsIgnoreCase("y")) {
                GameOfLife.evolve();
                printGame();
            } else {
                System.exit(0);
            }
        }
    }
}

GameOfLife:

import java.util.Arrays;

public class GameOfLife {

    static final int m = 25; // number of rows
    static final int n = 75; // number of columns
    static char[][] grid = new char[m][n]; // Creates an empty (no dots or
                                            // X's)grid of rows and columns.

    static int count_neighbors(int i, int j) {
        int nn = 0; // number of neighbors of cell(i,j)

        if (i > 0 && j > 0 && grid[i - 1][j - 1] == 'X') {
            nn++;
        }
        ;
        if (i > 0 && grid[i - 1][j] == 'X') {
            nn++;
        }
        ;
        if (i > 0 && j < 72 && grid[i - 1][j + 1] == 'X') {
            nn++;
        }
        ;
        if (j > 0 && grid[i][j - 1] == 'X') {
            nn++;
        }
        ;
        if (j < 72 && grid[i][j + 1] == 'X') {
            nn++;
        }
        ;
        if (j > 0 && i < 22 && grid[i + 1][j - 1] == 'X') {
            nn++;
        }
        ;
        if (i < 22 && grid[i + 1][j] == 'X') {
            nn++;
        }
        ;
        if (i < 22 && j < 72 && grid[i + 1][j + 1] == 'X') {
            nn++;
        }

        return nn;
    }

    static void evolve() {
        for (int i = 0; i <= 25 - 1; i++) {
            for (int j = 0; j <= 75 - 1; j++) {
                int s = count_neighbors(i, j);
                if (s < 2 || s > 3) {
                    grid[i][j] = '.';
                }
                if ((s == 2 || s == 3) && grid[i][j] == 'X') {
                    grid[i][j] = 'X';
                }
                if (s == 3 && grid[i][j] == '.') {
                    grid[i][j] = 'X';
                }
            }

        }

    }

}

2 个答案:

答案 0 :(得分:1)

当我实现这个时,我有一个条件检查当前单元格是否有值...不记得确切的规则,但这是我的版本使用@janos提到的“快照”。

right

答案 1 :(得分:0)

evolve中,当您遍历单元格时,您将修改单元格内容(活动或死亡)。这会影响下一个单元的邻居计数。游戏无法以这种方式运作。您需要在进化阶段开始时对邻居进行计数,并在阶段结束时立即更新单元格。

一种方法是在阶段开始时拍摄网格的快照(完美副本),并使用快照进行计数,而不进行更新。另一种方法是将更改放在列表中(迭代时不进行更新),并在阶段结束时应用更改。