【问题标题】:Game of Life Processing生命处理游戏
【发布时间】:2016-11-15 07:56:27
【问题描述】:
    import processing.core.PApplet;

    public class gl extends PApplet {

    static int neighborCount;
    static int screenRows;
    int tNC; // Temporary Neighbor Count
    int newState;

    int columns = 960;
    int rows = 477;

    int[][] cells = new int[columns][rows];
    int[][] newGen = new int[columns][rows];

    public static void main(String[] args) {
        PApplet.main("gl");
    }

    public void settings() {
        size(1920, 955);
    }

    public void setup() {
        // Set background white and all of cells[][] to 0 or 1
        screenRows = 0;
        background(255);
        for (int j = 0; j < (rows / 2); j++) {
            for (int i = 0; i < (columns / 2); i++) {
                cells[i][j] = (int) random(0, 2);
            }
        }
    }

    public void draw() {
        // If program has finished generating this frame, reset everything and set cells[][] equal to newGen[][]
        if (screenRows > (height / 2)) {
            screenRows = 0;
            System.out.println("End of generation reached");
            background(255);
            cells = newGen.clone();
            for (int i = 0; i < columns; i++) {
                for (int j = 0; j < rows; j++) {
                    newGen[i][j] = 0;
                }
            }
        }
        // Go through every element in cells[][], determine it's value, and display it
        for (int x = 1; x < (width / 2) - 1; x++) {
            for (int y = 1; y < (height / 2) - 1; y++) {

                printCell(x, y);
            }
        }
        screenRows++;

    }

    public void printCell(int x, int y) {
        setCellState(x, y);

        if (newGen[x][y] == 0) {
            stroke(255);
            fill(255);

        } else if (newGen[x][y] == 1) {
            stroke(0);
            fill(0);

        }
        System.out.println(x + ", " + y);
        rect(x, y, 2, 2);
    }

    public void setCellState(int x, int y) {
        tNC = getNeighborCount(x, y);
        neighborCount = 0;
        System.out.println(tNC);

        if (tNC < 2) { // If less than 2 neighbors, cell dead
            newGen[x][y] = 0;

        } else if (tNC > 3) { // If more than 3 neighbors, cell dead
            newGen[x][y] = 0;

        } else if ((tNC == 2 || tNC == 3) && cells[x][y] == 1) { // If 2 or 3 neighbors and cell is alive, do nothing (unnecessary statement but makes visualizing easier)

        } else if (tNC == 3 && cells[x][y] == 0) { // If 3 neighbors and cell is dead, cell is alive
            newGen[x][y] = 1;

        } else if (tNC == 2 && cells[x][y] == 0) { // If 2 neighbors and cel is dead, do nothing (also unnecessary)

        } else {
            System.out.println("Error in setCellState(int, int);"); // In event of none of the conditions being met
        }
        tNC = 0; // Reset variable (probably unnecessary but might as well)
    }

    public int getNeighborCount(int x, int y) {
        // Go through each cell adjacent or diagonal to the cell and add it's value (0 or 1) to neighborCount
        for (int i = -1; i < 2; i++) {
            for (int j = -1; j < 2; j++) {
                neighborCount += cells[i + x][j + y];
            }
        }
        // Subtract the value of the cell being evaluated from neighborCount as that is not a factor in the sum of the neighbors
        neighborCount -= cells[x][y];
        return neighborCount;
    }
}

Pastebin

我现在只追求功能而不是速度。

我正在尝试使用 Eclipse 中的处理来编写 Conway 的生命游戏。上面的代码在多个方面都无法正常工作:

在窗口中显示的世代比我想要的要小得多。尽管我努力通过使每个单元格为 2x2 像素并且行和列的数量是窗口高和宽的一半来平衡这一点,但它只占用了窗口的一小部分。

此外,几秒钟后显示第一代后,该代在窗口中似乎没有更新。

我注意到变量 tNC 通常等于 0,而它应该等于 0 到 7 之间的任何数字。

【问题讨论】:

    标签: multidimensional-array processing conways-game-of-life cellular-automata


    【解决方案1】:

    你有三个主要问题。

    问题 1: 您似乎在渲染单元格时正在生成下一代,这可能没问题...但是您在用 screenRows 逻辑做什么(if draw() 函数中的声明)?

    如果我是你,我会将你的逻辑分成两部分:编写一个绘制你的棋盘的函数,以及另一个基于当前棋盘返回新棋盘的函数。在绘制当前一代时停止尝试计算下一代,因为那只会让您头疼。

    我也不认为您在数组之间切换的逻辑是正确的。哪个数组保存当前一代,哪个保存下一代?你确定吗?

    问题 2: 您似乎在像素大小和数组坐标之间切换。例如,您在其数组索引坐标处绘制每个单元格,但您将它们绘制为2x2 矩形。这没有多大意义,因为无论如何你都会用下一个单元格在它上面绘制。再次分离您的逻辑:创建一个函数,该函数根据窗口 widthheight、数组位置和数组长度绘制单元格。

    问题 3: 您的打印语句正在扼杀您的帧速率。打印语句的速度非常慢。由于您正在进行的所有计算,您的帧速率已经很慢,但是当您每帧打印 (960*477*2) 东西时,它会变得更慢。这并不是真正的逻辑错误,但它会让您更难准确地看到您的程序在做什么。

    解决方案:要解决您的问题,我建议您对代码进行大量重构。如果我是你,我会从一个新程序重新开始。那么:

    第 1 步:将绘图逻辑与计算下一代的逻辑分开。创建两个函数:一个用于绘图,另一个根据当前函数返回一个新数组。

    第 2 步:在您的绘图代码中,确保将数组索引和像素位置分开。也许写另一个函数来获取一个单元格位置并根据窗口大小和数组大小绘制一个矩形。

    PS:你和this person同班吗?你也在使用 Daniel Shiffman 的代码吗?

    【讨论】:

    • 我已经尝试了您的建议并重新开始,它确实更整洁、更紧凑、更易读,但仍然无法正常工作。在可能回到这里寻求进一步建议之前,我将继续尝试一段时间。我不是那个人的那个班级,但是我确实阅读了 Daniel Shiffman 书中的一章作为指导(但我认为我的大部分代码都是原创的,或者至少是我个人认为的)。
    • 如果您再次遇到困难,我建议您使用新代码发布新帖子。我很乐意继续努力提供帮助。哦,我并没有指责你窃取代码或任何东西,我只是认为这是一个有趣的巧合!
    • 我相信我已经取得了进步,但我的 Game of Life 功能并不完善。它似乎以某种方式错误地计算了邻域,或者错误地解释了总和,或者类似的东西。你能帮忙吗?谢谢stackoverflow.com/questions/38382356/…
    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多