康威生活游戏javascript(最好的解决方案 [英] Conway game of life in javascript( best sol

查看:47
本文介绍了康威生活游戏javascript(最好的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为康威生命游戏编写一个代码......我正在采用2代阵列。和一个2代。

i am writing a code for conway game of life...... i am taking 2> arrays one fr old generation. and one for 2 nd genration.

**规则是:生命游戏的世界是方形单元的无限二维正交网格,每个网格都处于两种可能状态之一,活着还是死了。每个细胞与其八个邻居相互作用,这八个邻居是水平,垂直或对角相邻的细胞。在每个步骤中,发生以下转换:** 1.具有少于两个活邻居的任何活细胞死亡,好像由人口不足引起。

**rules are : The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: **1.Any live cell with fewer than two live neighbours dies, as if caused by under-population.

2任何有两三个活着的邻居的活细胞都会生活在下一代。

2.Any live cell with two or three live neighbours lives on to the next generation.

3.有三个以上活着的邻居的活细胞就像过度拥挤一样死亡。

3.Any live cell with more than three live neighbours dies, as if by overcrowding.

4.正好有三个活着的邻居的任何死亡单元都会变成一个活细胞,好像通过复制一样。**

4.Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.**

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

The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.**

这里是代码

我是得到一个溶解,但我想它没有给我正确的解决方案,因为它没有检查角落的邻居。我已标记该部分

I am getting a soln but i guess its not giving me the correct solution becuase its not checking the neighbors of the corners. i have marked that part

**

    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)
    {
        count = 0;
        if(x > 0 && y > 0 && this.get(x + 1, y + 1))
        {
            console.log(this.get(x + 1, y + 1), "value 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;
    }***
    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);
                console.log(newMap, "newarray");
                //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;
                    console.log("rule1");
                }
                //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
                    console.log("rule2");
                }
                //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;
                    console.log("rule3");
                }
                //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;
                    console.log("rule4");
                }
            }
        }
        this.map = newMap;
    console.log(this.map,"new generation")
    }

**


推荐答案

我在JSHint中搜索了你的代码并解决了所有问题,写了一些胶水在浏览器中测试它,结果如下:< a href =http://jsfiddle.net/RichardTowers/ujmba/ =nofollow> jsfiddle 。

I trawled through your code in JSHint and fixed all of the issues, wrote some glue to test it in the browser and here's the result: jsfiddle.

看起来它正在发挥作用我,所以我认为这个问题肯定是由于JSHint提出的数十个警告中的一个。

It looks like it's working to me, so I think the problem must have been due to one of the dozens of warnings that JSHint flagged up.

Re:你断言这个问题是由角落引起的,这就是这些行的用途:

Re: your assertion that the issue was due to the corners, that's what these lines are for:

  x = x % this.width;
  y = y % this.height;

在我的测试用例中,我使用的是10 x 10,所以在检查邻居时(9,9)它查看(10,10)(10%) 10,10%10)(0,0),因此避免在数组外查找。我相信这就是所谓的环形阵列。

In my test case I'm using a 10 x 10, so when it comes to check the neighbours of (9,9) it looks at (10, 10) and (10 % 10, 10 % 10) is (0, 0), thus avoiding looking outside the array. I believe this is what's known as a toroidal array.

我们从中吸取的教训是什么?继续处理小问题,重大问题将自行处理。

The lesson we learn from this? Keep on top of the small issues, and the big issues will take care of themselves.

这篇关于康威生活游戏javascript(最好的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆