-3

可能的重复:
javascript 中的康威生命游戏(最好的 sol

我正在为康威的生命游戏编写代码。我正在使用 2 个数组;一个用于老一代,一个用于第二代。当我尝试实施规则时,控制台中出现错误:

TypeError: Cannot read property '-1' of undefined

请告诉我,我应该如何更改代码以便可以删除具有 -1 值的相邻单元格?

规则是:

生命游戏的宇宙是一个由方形细胞组成的无限二维正交网格,每个细胞都处于两种可能状态中的一种,活的或死的。每个单元格都与其八个相邻单元格交互,这些相邻单元格是水平、垂直或对角相邻的单元格。在时间的每一步,都会发生以下转换:

  1. 任何少于两个活邻居的活细胞都会死亡,好像是由于人口不足造成的。

  2. 任何有两三个活邻居的活细胞都可以活到下一代。

  3. 任何有超过三个活邻居的活细胞都会死亡,就好像过度拥挤一样。

  4. 任何只有三个活邻居的死细胞都会变成活细胞,就像通过繁殖一样。

初始模式构成系统的种子。第一代是通过将上述规则同时应用于种子中的每个细胞而创建的——出生和死亡同时发生,而发生这种情况的离散时刻有时称为滴答声(换句话说,每一代都是前一个)。这些规则继续被重复应用以创造更多的世代。

这是代码:

window.conway =
    {
    };
    window.conway.maingame =
    {
    };

    conway.maingame = function(width, height)
    {

        window.a = [];
        this.width = width;
        this.height = height;
        this.map = new Array(width);
        for( i = 0; i < this.width; i++)
        {
            this.map[i] = new Array(height);
        }
        console.log(this.map, "map")
    }

    conway.maingame.prototype.randomize = function()
    {
        for( y = 0; y < this.height; y++)
        {
            console.log("enter for loop")
            for( x = 0; x < this.width; x++)
            {
                if(Math.random() > .5)
                {
                    i = true;
                }
                else
                {
                    i = false;
                }
                console.log("enter function")
                this.set(x, y, i);

            }
        }
    }

    conway.maingame.prototype.set = function(x, y, val)
    {
        x = x % this.width;
        y = y % this.height;
        this.map[x][y] = val;
        console.log(this.map, "map2");

    }


    conway.maingame.prototype.get = function(x, y)
    {
        x = x % this.width;
        y = y % this.height;
        return this.map[x][y];
    }

    conway.maingame.prototype.neighbors = function(x, y)
    {
        n = 0;
        if(this.get(x + 1, y + 1))
        {
            n++;
        }
        if(this.get(x + 1, y))
        {
            n++;
        }
        if(this.get(x + 1, y - 1))
        {
            n++;
        }
        if(this.get(x, y - 1))
        {
            n++;
        }
        if(this.get(x - 1, y - 1))
        {
            n++;
        }
        if(this.get(x - 1, y))
        {
            n++;
        }
        if(this.get(x - 1, y + 1))
        {
            n++;
        }
        if(this.get(x, y + 1))
        {
            n++;
        }
        return n;
    }

    conway.maingame.prototype.newgeneration = function()
    {

        var newMap = new Array(this.width);
        for( i = 0; i < this.width; i++)
        {
            newMap[i] = new Array(this.height);
        }

        for(var y = 0; y < this.height; y++)
        {
            for(var x = 0; x < this.width; x++)
            {
                console.log("enter all for")
                newMap[x][y] = this.get(x, y);

                if(this.neighbors(x, y) = undefined)
                {
                    for( k = 0; k < this.width+1; k++)
                    {
                        arr[k]=[];
                        for( f = 0; f < this.height+1; f++)
                        {
                                arr[j]=this.neigbors(x,y);
                                arr.pop();
                        }
                    }
                }
 //Error is in this part of the code    
                //Rule 1: any live cell with fewer than two live neighbors dies
                if(this.get(x, y) == true && this.neighbors(x, y) < 2)
                {
                    newMap[x][y] = false;
                }

                //Rule 2: Any live cell with two or three live neighbours lives on to the next generation
                if(this.get(x, y) == true && this.neighbors(x, y) == 2 || this.neighbors(x, y) == 3)
                {
                    newMap[x][y] = true

                }

                //Rule 3: any live cell with more than three live neighbors dies
                if(this.get(x, y) == true && this.neighbors(x, y) > 3)
                {
                    newMap[x][y] = false;
                }

                //Rule 4: any dead cell with exactly three live neighbors becomes a live cell
                if(this.get(x, y) == false && this.neighbors(x, y) == 3)
                {
                    newMap[x][y] = true;
                }
            }
        }

        this.map = newMap;
    }

改变带来

即使对代码进行了更改,我的回答也不正确。请告诉我为什么?

conway.maingame.prototype.neighbors = function(x, y)
{
    count = 0;

    if(x > 0 && y > 0 && this.get(x + 1, y + 1))
    {
        console.log(this.get(x + 1, y + 1), "vallue neighbor");
        count++;
        console.log(count);
    }

    if(x > 0 && y > 0 && this.get(x + 1, y))
    {
        console.log(this.get(x + 1, y), "vallue neighbor");
        count++;
        console.log(count);

    }

    if(x > 0 && y > 0 && this.get(x + 1, y - 1))
    {
        console.log(this.get(x + 1, y - 1), "vallue neighbor");
        count++;
        console.log(count);
    }

    if(x > 0 && y >=0 && this.get(x, y - 1))
    {
        console.log(this.get(x + 1, y - 1), "vallue neighbor");
        count++;
        console.log(count);

    }

    if(x > 0 && y > 0 && this.get(x - 1, y - 1))
    {

        console.log(this.get(x + 1, y - 1), "vallue neighbor");
        count++;
        console.log(count);

    }

    if(x > 0 && y > 0 && this.get(x - 1, y))
    {
        console.log(this.get(x + 1, y - 1), "vallue neighbor");
        count++;
        console.log(count);
    }

    if(x > 0 && y > 0 && this.get(x - 1, y + 1))
    {
        console.log(this.get(x + 1, y - 1), "vallue neighbor");
        count++;
        console.log(count);
    }

    if(x > 0 && y > 0 &&this.get(x, y + 1))
    {
        console.log(this.get(x + 1, y - 1), "vallue neighbor");
        count++;
        console.log(count);
    }

    return count;
}
4

2 回答 2

2

编辑:请参阅我对这个问题重复的回答。

值得通过jsHint 之类的检查工具运行您的代码:

Line 11: window.a = []; 

Missing "use strict" statement.


Line 19: console.log(this.map, "map") 

Missing semicolon.


Line 20: } 

Missing semicolon.


Line 24: for( y = 0; y < this.height; y++) 

Missing "use strict" statement.


Line 26: console.log("enter for loop") 

Missing semicolon.


Line 29: if(Math.random() > .5) 

A leading decimal point can be confused with a dot: '.5'.


Line 37: console.log("enter function") 

Missing semicolon.


Line 42: } 

Missing semicolon.


Line 46: x = x % this.width; 

Missing "use strict" statement.


Line 51: } 

Missing semicolon.


Line 56: x = x % this.width; 

Missing "use strict" statement.


Line 59: } 

Missing semicolon.


Line 63: n = 0; 

Missing "use strict" statement.


Line 97: } 

Missing semicolon.


Line 102: var newMap = new Array(this.width); 

Missing "use strict" statement.


Line 112: console.log("enter all for") 

Missing semicolon.


Line 115: if(this.neighbors(x, y) = undefined) 

Expected a conditional expression and instead saw an assignment.


Line 129: if(this.get(x, y) == true && this.neighbors(x, y) < 2) 

Expected '===' and instead saw '=='.


Line 135: if(this.get(x, y) == true && this.neighbors(x, y) == 2 || this.neighbors(x, y) == 3) 

Expected '===' and instead saw '=='.


Line 135: if(this.get(x, y) == true && this.neighbors(x, y) == 2 || this.neighbors(x, y) == 3) 

Expected '===' and instead saw '=='.


Line 135: if(this.get(x, y) == true && this.neighbors(x, y) == 2 || this.neighbors(x, y) == 3) 

Expected '===' and instead saw '=='.


Line 137: newMap[x][y] = true 

Missing semicolon.


Line 142: if(this.get(x, y) == true && this.neighbors(x, y) > 3) 

Expected '===' and instead saw '=='.


Line 148: if(this.get(x, y) == false && this.neighbors(x, y) == 3) 

Expected '===' and instead saw '=='.


Line 148: if(this.get(x, y) == false && this.neighbors(x, y) == 3) 

Expected '===' and instead saw '=='.


Line 156: } 

Missing semicolon.


Line 8: conway.maingame = function(width, height) 

'conway' is not defined.


Line 15: for( i = 0; i < this.width; i++) 

'i' is not defined.


Line 15: for( i = 0; i < this.width; i++) 

'i' is not defined.


Line 15: for( i = 0; i < this.width; i++) 

'i' is not defined.


Line 17: this.map[i] = new Array(height); 

'i' is not defined.


Line 19: console.log(this.map, "map") 

'console' is not defined.


Line 22: conway.maingame.prototype.randomize = function() 

'conway' is not defined.


Line 24: for( y = 0; y < this.height; y++) 

'y' is not defined.


Line 24: for( y = 0; y < this.height; y++) 

'y' is not defined.


Line 24: for( y = 0; y < this.height; y++) 

'y' is not defined.


Line 26: console.log("enter for loop") 

'console' is not defined.


Line 27: for( x = 0; x < this.width; x++) 

'x' is not defined.


Line 27: for( x = 0; x < this.width; x++) 

'x' is not defined.


Line 27: for( x = 0; x < this.width; x++) 

'x' is not defined.


Line 31: i = true; 

'i' is not defined.


Line 35: i = false; 

'i' is not defined.


Line 37: console.log("enter function") 

'console' is not defined.


Line 38: this.set(x, y, i); 

'x' is not defined.


Line 38: this.set(x, y, i); 

'y' is not defined.


Line 38: this.set(x, y, i); 

'i' is not defined.


Line 44: conway.maingame.prototype.set = function(x, y, val) 

'conway' is not defined.


Line 49: console.log(this.map, "map2"); 

'console' is not defined.


Line 54: conway.maingame.prototype.get = function(x, y) 

'conway' is not defined.


Line 61: conway.maingame.prototype.neighbors = function(x, y) 

'conway' is not defined.


Line 61: 

Too many errors. (38% scanned).

这里真的有太多错误,无法将其缩小到特定问题......

于 2012-06-28T12:12:46.590 回答
2

用于==比较(甚至===)。用于=分配。

这一行是错误的:

if(this.neighbors(x, y) = undefined)

应该是比较,而不是分配。

编辑:修复第二个错误

我 99% 确定您标记的行不是导致问题的行。请检查您的控制台错误并仔细验证行号。

我很确定问题是因为您试图引用一个不存在的单元格。考虑边缘周围的单元格 - 外面没有邻居。您需要修改代码以检查您是否没有超出数组的范围。

    ...

    if (y > 0 && this.get(x, y - 1)) {
        n++;
    }

    ... etc. ...

额外提示:始终将您的开口支架放在同一条线上。

于 2012-06-28T12:11:36.947 回答