查询“康威的生命游戏”算法[重复]

loading...


-3

可能重复:康威生活中的游戏javascript(最好的解决方案

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

TypeError: Cannot read property '-1' of undefined

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

规则是:

生命博弈的宇宙是方形单元的无限二维正交网格,每个方格单元处于两种可能状态中的一种,即活着或死亡 . 每个细胞与其八个邻居相互作用,这八个邻居是水平,垂直或对角相邻的细胞 . 在每个步骤中,发生以下转换:

  • 任何活动邻居少于两个的活细胞都会死亡,就像人口不足一样 .

  • 任何有两三个活邻居的活细胞都会生活在下一代 .

  • 任何有三个以上活着的邻居的活细胞都会死亡,就像过度拥挤一样 .

  • 具有正好三个活邻居的任何死细胞变成活细胞,就好像通过繁殖一样 .

初始模式构成系统的种子 . 通过将上述规则同时应用于种子出生中的每个细胞并且同时发生死亡来创建第一代,并且发生这种情况的离散时刻有时被称为刻度(换句话说,每一代是纯粹的函数 . 前一个) . 这些规则继续反复应用以创造更多代 .

这是代码:

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;
}
2回答

  • 2

    使用 == 进行比较(甚至 === ) . 使用 = 进行分配 .

    这条线错了:

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

    应该是一个比较,而不是一个assignemnt .

    EDIT: To fix the second error

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

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

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

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


  • 2

    Edit :看到我对这个问题的回答duplicate .

    值得通过像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).
    

    这里真的有太多错误将它缩小到一个特定的问题......

loading...

评论

暂时没有评论!