康威的生命游戏可视化Java

时间:2014-06-23 11:11:14

标签: java swing conways-game-of-life

我试图在Java eclipse中撰写Conway的生命游戏。算法有效,但我在可视化方面存在问题。我可以绘制一些在阵列中存活的小矩形但它在动画中不起作用。这是我的代码。我怎么能看到它?感谢。


public class ConwaysGameOfLife extends JPanel {

    private int[][] grid;
    private static final Random rnd = new Random();

    public ConwaysGameOfLife(int width, int height) {
        this.grid = new int[width][height];
        setupGrid();
    }

    private void setupGrid() {
        for (int[] row : grid) {
            for (int j = 0; j < row.length; j++) {
                if (rnd.nextDouble() < 0.92)
                    continue;
                row[j] = rnd.nextInt(2);
            }
        }
    }

    public void updateGrid() {
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                countAndDecideDeadOrAlive(i, j);
            }
        }
    }

    private void countAndDecideDeadOrAlive(int rowIndex, int columnIndex) {
        int counter = 0;

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {

                counter = 0;

                if ((i > 0 && j > 0) && grid[i - 1][j - 1] == '1')
                    counter++;
                if (i > 0 && grid[i - 1][j] == '1')
                    counter++;
                if ((i > 0 && j < grid[i].length - 1)
                        && grid[i - 1][j + 1] == '1')
                    counter++;
                if (j > 0 && grid[i][j - 1] == '1')
                    counter++;
                if (j < grid[i].length - 1 && grid[i][j + 1] == '1')
                    counter++;
                if ((j > 0 && i < grid.length - 1) && grid[i + 1][j - 1] == '1')
                    counter++;
                if (i < grid.length - 1 && grid[i + 1][j] == '1')
                    counter++;
                if ((i < grid.length - 1 && j < grid[i].length - 1)
                        && grid[i + 1][j + 1] == '1')
                    counter++;

                if (grid[i][j] == '1') {

                    if (counter > 3 || counter < 2)
                        grid[i][j] = '0';

                } else {
                    if (counter == 3)
                        grid[i][j] = '1';
                }

            }
        }
    }

    public Dimension getPreferredSize() {
        return new Dimension(grid.length * 4, grid[0].length * 4);
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Color gColor = g.getColor();

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 1) {
                    g.setColor(Color.black);
                    g.fillRect(j * 4, i * 4, 4, 4);
                }
            }
        }

        g.setColor(gColor);
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Bir row giriniz");

        int row = input.nextInt();
        System.out.println("Bir column giriniz");

        int column = input.nextInt();
        final ConwaysGameOfLife c = new ConwaysGameOfLife(row, column);
        JFrame frame = new JFrame();
        frame.getContentPane().add(c);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        new Timer(10, new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                c.updateGrid();
                c.repaint();
            }
        }).start();
    }
}

1 个答案:

答案 0 :(得分:1)

问题是您在算法中将int值与char'1'或'0'进行比较。

初始化int值:

private void setupGrid() {
    for (int[] row : grid) {
        for (int j = 0; j < row.length; j++) {
            if (rnd.nextDouble() < 0.92)
                continue;
            row[j] = rnd.nextInt(2);
        }
    }
}

与char值比较:

if (grid[i][j] == '1') {
    if (counter > 3 || counter < 2)
       grid[i][j] = '0';
} else {
    if (counter == 3)
       grid[i][j] = '1';
}