简体   繁体   English

在JS中编写Conway编程的“生命游戏”

[英]Stuck programming Conway's “Game of Life” in JS

We have to program a JavaScript version of Conway's Game of Life for a school project, but we're stuck on looping the edges. 我们必须为一个学校项目编写一个JavaScript版本的Conway的生命游戏,但我们仍然坚持循环。 The whole thing works fine, but the function that calculates the number of neighbors doesn't work on the cells that are on the edges (because it has to evaluate values outside of the array, which are undefined). 整个工作正常,但计算邻居数的函数不适用于边上的单元格(因为它必须评估数组之外的值,这是未定义的)。 We've tried several options, but they all alter the functionality of the rest of the program. 我们已经尝试了几种选择,但它们都改变了程序其余部分的功能。

What should we add for it to work on the edges of the grid? 我们应该添加什么才能在网格的边缘上工作?

  var totalNeighbors = function(x, y) { var total = 0; if (x > 0 && cells[(x - 1)][y] == 1) { total++; } if (x < (width - 1) && cells[x + 1][y] == 1) { total++; } if (y > 0 && cells[x][y - 1] == 1) { total++; } if (y < (height - 1) && cells[x][y + 1] == 1) { total++; } if (y > 0 && x > 0 && cells[x - 1][y - 1] == 1) { total++; } if (y > 0 && x < (width - 1) && cells[x + 1][y - 1] == 1) { total++; } if (y < (height - 1) && x > 0 && cells[x - 1][y + 1] == 1) { total++; } if (y < (height - 1) && x < (width - 1) && cells[x + 1][y + 1] == 1) { total++; } return total; }; 

Thanks! 谢谢!

I'd go with something more like this: 我会选择更像这样的东西:
As you can see, I refactored a little bit. 如你所见,我重构了一点。

var isvalid = function(x, y) {
        /*
         * This returns 1 if cells[x][y] == 1.
         * Otherwise, we return 0.
         * NOTE: If cells[x, y] is out of bounds, we return 0.
         * GLOBALS USED: cells, width, and height.
         */

        //This returns true if (index < size && index >= 0)
        //Used to check that index is not an invalid index.
        var inbounds = function (size, index) {
                return (index >= 0 && index < size);
        };

        //given point is out of bounds
        if (!inbounds(width, x) || !inbounds(height, y)) {
                return 0;
        }

        //everything is good
        return (cells[x][y] === 1) ? 1 : 0;
    };

var totalNeighbors = function(x, y) {
    var total = 0;

    //cells[x-1][y]
    total += isvalid(x-1, y);

    //cells[x + 1][y]
    total += isvalid(x+1, y);

    //cells[x][y - 1]
    total += isvalid(x, y-1);

    //cells[x][y + 1]
    total += isvalid(x, y+1);

    //cells[x - 1][y - 1]
    total += isvalid(x-1, y-1);

    //cells[x + 1][y - 1]
    total += isvalid(x+1, y-1);

    //cells[x - 1][y + 1]
    total += isvalid(x-1, y+1);

    //cells[x + 1][y + 1]
    total += isvalid(x+1, y+1);

    return total;
};

PS: Your original code sample is 37 lines without comments. PS:您的原始代码示例是37行没有注释。 My code sample is 52 lines with comments and 33 lines without comments. 我的代码示例是带有注释的52行和没有注释的33行。

As near as I can figure, this way is cleaner and shorter. 尽可能接近我的想象,这种方式更清洁,更短。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM