Javascript 关于「;康威';“生命的游戏”;算法

Javascript 关于「;康威';“生命的游戏”;算法,javascript,Javascript,可能重复: 我在为康威的人生游戏写代码。我取2个数组;一个是老一代,一个是第二代。当我尝试实施规则时,控制台中出现错误: TypeError: Cannot read property '-1' of undefined 请告诉我,我应该如何更改代码,以便删除值为-1的相邻单元格 这些规则是: 生命游戏的宇宙是一个由正方形细胞组成的无限二维正交网格,每个细胞都处于两种可能状态中的一种,活着或死去。每个细胞都与其八个相邻细胞相互作用,它们是水平、垂直或对角相邻的细胞。在每个时间步骤中,都会发生

可能重复:

我在为康威的人生游戏写代码。我取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;
        }
    

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

    这一行是错误的:

    if(this.neighbors(x, y) = undefined)
    
    应该是比较,而不是赋值

    编辑:修复第二个错误

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

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

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

    额外提示:始终将开始括号放在同一行。

    编辑:查看我对这个问题的答案

    通过以下检查工具运行代码是值得的:

    第11行:window.a=[];
    缺少“使用严格”语句。
    第19行:console.log(this.map,“map”)
    缺少分号。
    第20行:}
    缺少分号。
    第24行:for(y=0;y.5)
    前导小数点可能与点“.5”混淆。
    第37行:console.log(“输入函数”)
    缺少分号。
    第42行:}
    缺少分号。
    第46行:x=x%此宽度;
    缺少“使用严格”语句。
    第51行:}
    缺少分号。
    第56行:x=x%此宽度;
    缺少“使用严格”语句。
    第59行:}
    缺少分号。
    第63行:n=0;
    缺少“使用严格”语句。
    第97行:}
    缺少分号。
    第102行:var newMap=新数组(此.width);
    缺少“使用严格”语句。
    第112行:console.log(“全部输入”)
    缺少分号。
    第115行:if(this.neights(x,y)=未定义)
    应为条件表达式,而应为赋值。
    第129行:if(this.get(x,y)=true&&this.neights(x,y)<2)
    应为“==”而不是看到“==”。
    第135行:if(this.get(x,y)=true&&this.neights(x,y)==2 | | this.neights(x,y)==3)
    应为“==”而不是看到“==”。
    第135行:if(this.get(x,y)=true&&this.neights(x,y)==2 | | this.neights(x,y)==3)
    应为“==”而不是看到“==”。
    第135行:if(this.get(x,y)=true&&this.neights(x,y)==2 | | this.neights(x,y)==3)
    应为“==”而不是看到“==”。
    第137行:newMap[x][y]=true
    缺少分号。
    第142行:if(this.get(x,y)=true&&this.neights(x,y)>3)
    应为“==”而不是看到“==”。
    第148行:if(this.get(x,y)==false&&this.neights(x,y)==3)
    应为“==”而不是看到“==”。
    第148行:if(this.get(x,y)==false&&this.neights(x,y)==3)
    应为“==”而不是看到“==”。
    第156行:}
    缺少分号。
    第8行:conway.maingame=函数(宽度、高度)
    “康威”未定义。
    第15行:for(i=0;i    ...
    
        if (y > 0 && this.get(x, y - 1)) {
            n++;
        }
    
        ... etc. ...