gpt4 book ai didi

c++ - 尝试用 C++ 编写 Conway 的生命游戏时出现问题

转载 作者:行者123 更新时间:2023-11-28 03:54:26 25 4
gpt4 key购买 nike

我正在使用在 involuntaryexercise.com 上找到的 reni2D 库构建这个程序我创建了一个由二维行列组成的网格,单击其中的每个方 block 即可打开和关闭。现在,我正在尝试制作一些遵守规则的工作算法,但在运行程序时,在程序通过相关循环后它们都会消失。我看不出这里有什么本质上的错误,所以我依靠熟练的程序员的眼睛来指导我走上正确的道路。

[更新]:该程序现在运行得更好,只是它出于某种原因不遵循 Game Of Life 规则。

if(raGetKey(VK_RETURN))
{
//occupied cells
for(int i=0; i<ROWS; i++){ //768/24 = 32
for(int j=0; j<COLS; j++){ //768/32 = 24
//Occupied cells
if(sq[i][j].isOn == true && sq[i][j].neighbors < 4)
{
//UDLR
if((j+1)<COLS && sq[i][j+1].isOn == true){
sq[i][j].neighbors += 1;
printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
}
if((j-1)>(-1) && sq[i][j-1].isOn == true){
sq[i][j].neighbors += 1;
printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
}
if((i+1)<ROWS && sq[i+1][j].isOn == true){
sq[i][j].neighbors += 1;
printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
}
if((i-1)>(-1) && sq[i-1][j].isOn == true){
sq[i][j].neighbors += 1;
printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
}

if(sq[i][j].neighbors >= 4)
break;
//Diagonal
if((i+1)<ROWS && (j+1)<COLS && sq[i+1][j+1].isOn == true){
sq[i][j].neighbors += 1;
printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
}
if((i-1)>(-1) && (j-1)> (-1) && sq[i-1][j-1].isOn == true){
sq[i][j].neighbors += 1;
printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
}
if((i-1)>(-1) && (j+1)<COLS && sq[i-1][j+1].isOn == true){
sq[i][j].neighbors += 1;
printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
}
if((i+1)<ROWS && (j-1)> (-1) && sq[i+1][j-1].isOn == true){
sq[i][j].neighbors += 1;
printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
}
}

//Empty cells
if(sq[i][j].isOn == false && sq[i][j].neighbors < 4)
{
//UDLR
/*if((i+1)<ROWS && (j+1)<COLS && (i-1)>0 && (j-1)>0)
{*/
if((j+1)<COLS && sq[i][j+1].isOn == true){
sq[i][j].neighbors += 1;
printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
}
if((j-1)>0 && sq[i][j-1].isOn == true){
sq[i][j].neighbors += 1;
printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
}
if((i+1)<ROWS && sq[i+1][j].isOn == true){
sq[i][j].neighbors += 1;
printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
}
if((i-1)>-1 && sq[i-1][j].isOn == true){
sq[i][j].neighbors += 1;
printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
}

if(sq[i][j].neighbors >= 4)
break;
//Diagonal
if((i+1)<ROWS && (j+1)<COLS && sq[i+1][j+1].isOn == true){
sq[i][j].neighbors += 1;
printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
}
if((i-1)>(-1) && (j-1)> (-1) && sq[i-1][j-1].isOn == true){
sq[i][j].neighbors += 1;
printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
}
if((i-1)>(-1) && (j+1)<COLS && sq[i-1][j+1].isOn == true){
sq[i][j].neighbors += 1;
printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
}
if((i+1)<ROWS && (j-1)> (-1) && sq[i+1][j-1].isOn == true){
sq[i][j].neighbors += 1;
printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
}
//}
}
}
}
for(int i=0; i<ROWS; i++) //768/24 = 32
{
for(int j=0; j<COLS; j++) //768/32 = 24
{

if(sq[i][j].isOn == true)
{
if(sq[i][j].neighbors <= 1){
sq[i][j].isOn = false;
sq[i][j].neighbors = 0;
printf("sq[%d][%d] is ON and has less than 1 neighbors ... Died from lonliness\n", i, j);
}
if(sq[i][j].neighbors >= 4){
sq[i][j].isOn = false;
sq[i][j].neighbors = 0;
printf("sq[%d][%d] is ON and has neighbors is greater than or equal to 4... Died from overpopulation\n", i, j);
}
if(sq[i][j].neighbors > 1 && sq[i][j].neighbors < 4){
sq[i][j].isOn = true;
printf("sq[%d][%d] is ON has 2 or 3 neighbors... Lives\n", i, j);
sq[i][j].neighbors = 0;
}
}
if(/*sq[i][j].isOn == false && */sq[i][j].neighbors == 3){
printf("sq[%d][%d] is OFF has 3 neighbors... Reproduce\n", i, j);
sq[i][j].isOn = true;
sq[i][j].neighbors = 0;
}
}
}
}

最佳答案

这里可能有几个问题,但至少其中一个可能是花括号放错地方:您的意思是要将第 3 个和第 4 个 for 循环放在第一个 for 循环中吗?

顺便说一句,许多程序员喜欢使用具有自动缩进功能的文本编辑器,这有助于捕获这些类型的错误。

关于c++ - 尝试用 C++ 编写 Conway 的生命游戏时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4181036/

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