用Java开发的Conway生活游戏(最好的sol
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了用Java开发的Conway生活游戏(最好的sol,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4296字,纯文字阅读大概需要7分钟。
内容图文
![用Java开发的Conway生活游戏(最好的sol](/upload/InfoBanner/zyjiaocheng/695/5944f67eb27c4a5faa064b34bed19c05.jpg)
我正在为生活的康威游戏编写代码…我正在使用2>阵列一老一代.第二代.
**规则是:生命游戏的宇宙是一个无限的二维正交方格,每个方格处于两个可能的状态,即活着的或死的.每个单元都与其八个邻居进行交互,这八个邻居在水平,垂直或对角线相邻.在每个时间步骤中,都会发生以下过??渡:** 1.任何具有少于两个活邻居的活细胞都会死亡,好像是由人口不足造成的.
2.任何有两个或三个活邻居的活细胞都可以存活到下一代.
3.具有三个以上活邻居的任何活细胞都会死亡,好像人满为患.
4,任何有三个活邻居的死细胞都变成活细胞,仿佛是通过繁殖**
初始模式构成了系统的种子.第一代是通过将上述规则同时应用于种子中的每个细胞而创建的-生与死同时发生,有时发生这种情况的离散时刻称为滴答声(换句话说,每一代都是纯种的功能)前一个).该规则将继续重复应用以创造更多的后代.**
这是代码
我得到一个解决方案,但是我猜它没有给我正确的解决方案,因为它没有检查角落的邻居.我已经标记了那部分
**
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.
看来它对我有用,因此我认为问题一定是由于JSHint标记了数十种警告之一.
回复:您断言问题归结于角落,这就是这些行的目的:
x = x % this.width;
y = y % this.height;
在我的测试用例中,我使用的是10 x 10,因此当检查(9,9)的邻居时,它看起来是(10,10),而(10%10,10%10)是(0,0 ),从而避免查看数组外部.我相信这就是所谓的环形阵列.
我们从中学到的教训是什么?紧紧关注小问题,大问题将自理.
内容总结
以上是互联网集市为您收集整理的用Java开发的Conway生活游戏(最好的sol全部内容,希望文章能够帮你解决用Java开发的Conway生活游戏(最好的sol所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。