gpt4 book ai didi

c++ - Game Of Life 程序中的混淆错误

转载 作者:可可西里 更新时间:2023-11-01 17:40:26 24 4
gpt4 key购买 nike

我有一些可用的 Game of Life 代码。它将每个种群保存为位图。这是输出的样子(裁剪):

desired output

在清理代码时,我发现如果我注释掉或以其他方式删除第60行:

cout << "Survivor: " << x << ", " << y << "\n";

它完全打乱了程序,它没有像它应该的那样产生滑翔机,而是产生了这个:

faulty output

我四处寻找,试图找出可能导致这种情况的原因,但到目前为止我一直没有成功。这是我当前的代码:

//Bitmap Library from http://partow.net/programming/bitmap/
#include "bitmap_image.hpp"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

using namespace std;

#define WIDTH 160
#define HEIGHT 128

bool population[WIDTH][HEIGHT];
bool survivors[WIDTH][HEIGHT];

int check_survivors();
int check_neighbors(int x, int y);
int write_population(char* file);

int main() {
int i, populations;

cout << "Enter number of populations: ";
cin >> populations;

//Glider
survivors[28][100] = true;
survivors[29][100] = true;
survivors[29][101] = true;
survivors[30][101] = true;
survivors[28][102] = true;

//Initial image generation
write_population("population0.bmp");

//populations
for (i = 0; i < populations; i++) {
char filename[17] = "population";
char ii[3];
sprintf(ii, "%d", i+1);

strcat(filename, ii);
strcat(filename, ".bmp");

check_survivors();
write_population(filename);
}

return 0;
}

int check_survivors() {
//set x and y variables
int x, y;

for (x = 0; x < WIDTH; x++) {
for (y = 0; y < HEIGHT; y++) {
if (check_neighbors(x, y)) {
survivors[x][y] = true;
cout << "Survivor: " << x << ", " << y << "\n";
} else {
survivors[x][y] = false;
}
}
}
return 0;
}

int check_neighbors(int x, int y) {
int neighbors = 0, survives;

//I really need to rewrite this mess

//Neighbors above
if (population[x-1][y-1] == true && x != 0 && y != 0) {
neighbors++;
}
if (population[x][y-1] == true && y != 0) {
neighbors++;
}
if (population[x+1][y-1] == true && x != WIDTH-1 && y != 0) {
neighbors++;
}

//Neighbors next to
if (population[x-1][y] == true && x != 0 ) {
neighbors++;
}
if (population[x+1][y] == true && x != WIDTH-1) {
neighbors++;
}

//Neighbors below
if (population[x-1][y+1] == true && x != 0 && y != HEIGHT-1) {
neighbors++;
}
if (population[x][y+1] == true && y != HEIGHT-1) {
neighbors++;
}
if (population[x+1][y+1] == true && x != WIDTH-1 && y != HEIGHT-1) {
neighbors++;
}

//Determining life or death
if (neighbors < 2 || neighbors > 3) {
//Neighbors less than 2 or more than 3 is dead cell
survives = 0;
} else if (neighbors == 3 && population[x][y] == false) {
//Exactly 3 neighbors re-animates a cell
survives = 1;
} else if (population[x][y] == true) {
//2 or 3 neighbors is survivor
survives = 1;
}

return survives;
}

int write_population(char* file) {
//Create Image
bitmap_image image(WIDTH, HEIGHT);

//Set background to white
image_drawer draw(image);
image.set_all_channels(255,255,255);

//set x and y variables
int x, y;

//For every array point, check to see if it survives,
//and transfer survivors to population
for (x = 0; x < WIDTH; x++) {
for (y = 0; y < HEIGHT; y++) {
if (survivors[x][y] == true) {
draw.pen_width(1);
draw.pen_color(0,0,0);
draw.plot_pixel(x, y);
}
population[x][y] = survivors[x][y];
}
}

//Save image
image.save_image(file);

//return
return 1;
}

最佳答案

像这样的事情:

if (population[x-1][y-1] == true && x != 0 && y != 0)

需要重写为:

if ( x > 0 && y > 0 && population[x-1][y-1] == true )

否则,当 xy0 时,您将直接进入未定义的行为区域(因为当您从 check_survivors()) 调用 check_neighbors(),您可能会遇到像这样奇怪的、无法解释的错误。在尝试访问这些元素之前,您需要检查无效的数组索引

此外,这里:

if (neighbors < 2 || neighbors > 3) {
//Neighbors less than 2 or more than 3 is dead cell
survives = 0;
} else if (neighbors == 3 && population[x][y] == false) {
//Exactly 3 neighbors re-animates a cell
survives = 1;
} else if (population[x][y] == true) {
//2 or 3 neighbors is survivor
survives = 1;
}

如果 neighbors == 2population[x][y] == false 看起来 survives 可能会留下一个不确定的值code>,如果您要访问该值,这也会导致未定义的行为。从您的代码中并不能立即清楚地知道这种情况的组合是否永远为真,但如果您仍处于调试阶段,那么至少值得添加一个条件检查以验证它是否曾经为真。

如果您的程序像这个一样表现出未定义的行为,那么在这些问题得到解决之前几乎不可能对它进行推理。

关于c++ - Game Of Life 程序中的混淆错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26685745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com