使用<canvas>在JavaScript中实现康威生命游戏

3
我在JavaScript中实现Conway's Game of Life时遇到了一些问题。在德语维基百科上说,这个图案会在54代后创建一个空世界,并且进化如下所示:
这意味着第二代看起来像这样:
但是当我使用我的代码时,第二代看起来像这样,世界也没有变空:
因此,我的代码显然是错误的,但我找不到我的错误。
var cellSize = 5,   // In px
    fieldSize = 70, // In cells
    canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d"),
    thisGeneration = [],
    nextGeneration = [];

// Set canvas size
canvas.width = canvas.height = cellSize * fieldSize;


// Fill the generation array
for (var i = 0; i < fieldSize; i++){

    thisGeneration.push([]);

    for (var x = 0; x < fieldSize; x++){

        // thisGeneration[thisGeneration.length-1].push( (Math.random() >  0.08)? 0 : 1);

        thisGeneration[thisGeneration.length-1].push(0);

    }

}

// Draw pattern:

thisGeneration[35][35]      =   thisGeneration[35][36]  = thisGeneration[35][37] =

thisGeneration[36][35]                                  = thisGeneration[36][37] =
thisGeneration[37][35]                                  = thisGeneration[37][37] =


thisGeneration[39][35]                                  = thisGeneration[39][37] =
thisGeneration[40][35]                                  = thisGeneration[40][37] =
thisGeneration[41][35]      =   thisGeneration[41][36]  = thisGeneration[41][37] =
1;

// "slice" causes "nextGeneration" to be a copy, not a reference
nextGeneration = thisGeneration.slice(0);

setInterval(function(){

    for (var y = 0, l = thisGeneration.length, x, l2, n; y < l; y++){

        for (x = 0, l2 = thisGeneration[y].length;  x < l2; x++){

            ctx.fillStyle = thisGeneration[y][x]? "black" : "white";
            ctx.fillRect(x*cellSize, y*cellSize, cellSize, cellSize);

            // n := Number of neighbors of the cell
            n = 0;

            if (typeof (thisGeneration[y-1]) != "undefined"){

                n += (

                    (thisGeneration     [y-1]   [x-1]   ||0)+
                    (thisGeneration     [y-1]   [ x ]   ||0)+
                    (thisGeneration     [y-1]   [x+1]   ||0)

                );

            }

            n += (

                (thisGeneration     [ y ]   [x-1]   ||0)+
                (thisGeneration     [ y ]   [x+1]   ||0)

            );

            if (typeof (thisGeneration[y+1]) != "undefined"){

                n += (

                    (thisGeneration     [y+1]   [x-1]   ||0)+
                    (thisGeneration     [y+1]   [ x ]   ||0)+
                    (thisGeneration     [y+1]   [x+1]   ||0)

                );

            }

            if (n === 3 && !thisGeneration[y][x]){
            // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

                nextGeneration[y][x] = 1;

            }

            if (n < 2 && thisGeneration[y][x]){
            // Any live cell with fewer than two live neighbours dies, as if caused by under-population.

                nextGeneration[y][x] = 0;

            }

            if ((n === 2 || n === 3) && thisGeneration[y][x]){
            // Any live cell with two or three live neighbours lives on to the next generation.

                nextGeneration[y][x] = 1;

            }

            if (n > 3 && thisGeneration[y][x]){
            // Any live cell with more than three live neighbours dies, as if by overcrowding.

                nextGeneration[y][x] = 0;

            }

        }

    }

    thisGeneration = nextGeneration.slice(0); // "slice" causes "thisGeneration" to be a copy, not a reference

}, 1000);

我还想知道是否能提高脚本的性能,因为你可以看到,我使用了一个二维数组来保存单元格的状态。

1个回答

3

我认为这一代和下一代并不像你想象的那样独立。建议您用逐个单元格复制替换切片操作,看看是否解决问题。


哦,看起来.slice(0)只做了一个浅拷贝...谢谢! - Vincent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接