1 Replies - 451 Views - Last Post: 28 June 2012 - 09:57 AM

#1 sup2.1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-June 12

ERROR IN of conway game of life . u can see it through wiki.

Posted 28 June 2012 - 08:06 AM

am writing a code for conway game of life...... i am taking 2 > arrays one fr old generation. and one for 2 nd genration. when i am > trying to implement the rules there is an error which is coming > TypeError: Cannot read property '-1' of undefined ( in the console) >
please tell me .. how should i edit the code so that the neighbor cell with -1 value can be removed

here is the code
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();
                        }
                    }
                }
 [size="7"][size="7"][b]//Error is in this part of the code    [/b][/size][/size]
                //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;
    }



Is This A Good Question/Topic? 0
  • +

Replies To: ERROR IN of conway game of life . u can see it through wiki.

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,440
  • Joined: 08-June 10

Re: ERROR IN of conway game of life . u can see it through wiki.

Posted 28 June 2012 - 09:57 AM

which of the 150 lines refers the error to?

for comparison, the Game of Life I created last year (link)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1