c++ - 康威的生命游戏帮助我理解这个意想不到的输出
问题描述
我想要一些帮助来理解为什么我的程序正在打印一个网格
....................
....................
....................
....................
....................
...............OOOOO
OOOOOOOOOOOOO.......
....................
....................
....................
正确的输出是这样的:
....................
....................
....................
....................
....................
....................
....................
.............O.O....
..............OO....
..............O.....
我编写它的方式是创建旧状态的副本并使用游戏规则对其进行操作。检查每个单元格后,我会存储该单元格的存活邻居数。如果计数大于 3 或小于 2,细胞就会死亡。
如果一个单元格的邻居数为 2 或 3 个,则它仍然活着。如果一个死细胞的计数为 3,它就会变成活的。这些规则直接应用于副本版本而不是旧的然后打印副本。
我试过使用调试器,但我仍然不确定如何正确使用它。到目前为止,我还没有注意到任何危险信号。这是我的代码:
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
vector<vector<bool> > world = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
void generate(const vector<vector<bool> >&g,vector<vector<bool> >&newworld)
{
int count = 0;
newworld = g;
for(size_t i = 0; i < g.size();i++) {
for(size_t j = 0; j < g[i].size();j++) {
int x = g.size(); //I rows
int y = g[i].size(); //J columns
//wrap the edges with formula (x+n)%n where n = NumOfRows or NumOfCol
if(g[(((i+1)+x)%x)][(((j-1)+y)%y)]==true){//top left
count++;
}
else if(g[(((i+1)+x)%x)][j]==true){//top middle
count++;
}
else if(g[(((i+1)+x)%x)][(((j+1)+y)%y)]==true){//top right
count++;
}
else if(g[i][(((j-1)+y)%y)]==true){//left cell
count++;
}
else if(g[i][(((j+1)+y)%y)]==true){//right cell
count++;
}
else if(g[(((i-1)+x)%x)][(((j-1)+y)%y)]==true){ //bottom left
count++;
}
else if(g[(((i-1)+x)%x)][j]==true){//bottom middle
count++;
}
else if(g[(((i-1)+x)%x)][(((j+1)+y)%y)]==true){//bottom right
count++;
}
if (g[i][j]) {
if(count > 3 || count < 2) {//if alive cell has more than 3 or less than 2, die
newworld[i][j] = false;
}
else if (count == 2 || count == 3) { //remain the same
newworld[i][j] = g[i][j];
}
}
else if (g[i][j] == false) {//dead come alive
if(count == 3) {
newworld[i][j] = true;
}
}
}
}
}
void display(vector<vector<bool> >&a)
{
for(size_t row = 0; row <a.size(); row++) {
for(size_t column = 0; column <a[row].size(); column++){
if (a[row][column]) {
cout << 'O';
}
else {
cout << '.';
}
}
cout << '\n';
}
}
int main()
{
vector<vector<bool> > newworld;
generate(world,newworld);
display(newworld);
return 0;
}
解决方案
该功能generate
有(至少)两个问题。
count
在嵌套循环之外初始化,因此它永远不会重置为零(对于每个单元格都应该如此)并继续增长。所有条件都是互斥的,因此只要满足一个条件,就会跳过其他条件。不应该有任何
else if
,而只有if
s。
保留您选择的数据结构,您可以将该函数重写为
using gof_t = std::vector<std::vector<bool>>;
void generate(gof_t& g, gof_t& newworld)
{
for(size_t i = 0, x = g.size(); i < x; i++)
{
for(size_t j = 0, y = g[i].size(); j < y; j++)
{
size_t i_prev = (i + x - 1) % x;
size_t i_next = (i + 1) % x;
size_t j_prev = (j + y - 1) % y;
size_t j_next = (j + 1) % y;
int count = g[i_prev][j_prev] + g[i_prev][j] + g[i_prev][j_next]
+ g[i ][j_prev] + g[i ][j_next]
+ g[i_next][j_prev] + g[i_next][j] + g[i_next][j_next];
newworld[i][j] = g[i][j] ? (count == 2 || count == 3) : (count == 3);
}
}
std::swap(g, newworld); // <-- Passing by non const reference, we can swap without copying
}
现场(双关语),在这里。
推荐阅读
- javascript - 第二次点击和页面刷新后如何隐藏div
- spring - 如何解决错误:HttpClientErrorException:400 null?
- javascript - Google Apps 脚本:如何在操作结束时将所有消息编译到一个弹出窗口中
- python - 以 .ark 格式保存经过训练的 pytorch 神经网络的输出
- python - SqlAlchemy Informix 状态
- python-3.x - 无法用 sympy 求解方程,在 mathcad prime 中工作正常
- java - 片段内的 RecyclerView 未正确显示
- heroku - 在 Rails 和 HerokuCI 中使用 pg_partman
- r - 在 R 中创建下三角矩阵
- c++ - 用c ++将结构变量写入文件