javascript中的康威生活游戏(最佳溶胶 [英] Conway game of life in javascript( best sol

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

问题描述

我正在为 conway game of life 编写代码......我正在使用 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 中浏览了您的代码并修复了所有问题,编写了一些胶水以在浏览器中对其进行测试,结果如下: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: 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天全站免登陆