gpt4 book ai didi

c++ - 严重找不到我的 "game of life"程序中的错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:46 25 4
gpt4 key购买 nike

好吧,不久前我被分配到一个实验室,我们应该在其中制作一个非常简化的康威生命游戏。

规则:贪婪的史莱姆(由 R 指定)会吃掉任何与其相邻(直接在上方、下方或旁边)的慢速史莱姆(由 S 指定)。一旦贪婪的史莱姆“吃掉”了缓慢的史莱姆,就会出现一个新的时间步骤,其中 4 个新的缓慢史莱姆被随机放置在棋盘上。我必须代表 4 个时间步骤。

很遗憾,我的代码无法正常工作。无论出于何种原因,我的四个慢速史莱姆都不会在每个时间步后生成。我已经为代码苦苦挣扎,但我无法为这个错误提供资金。请帮忙。

代码:

#include "stdafx.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

//Struct
struct Creatures
{
public: int type;
public: int age;
public: double weight;
private: double length;

};

//Prototypes
void printLake(Creatures Sludge[10][10]);
void fourNew(Creatures Sludge [10][10], Creatures slowSlime);
void dispatch(Creatures Sludge[10][10], Creatures water);

int main()
{
//creating the slimes, the water, and the grid (called sludge)
Creatures Sludge[10][10];
Creatures slowSlime;
slowSlime.type = 1;
Creatures ravenousSlime;
ravenousSlime.type = 2;
Creatures water;
water.type = 3;

//Initialize Lake
for (int row = 0; row<10; row++)
{
for (int col = 0; col<10; col++)
{
Sludge[row][col] = water;
}
}

//Random Distribution of Slow Slimes
srand(time(0));
int high = 10;
int low = 0;
int i = 0;
while(i<15)
{
int row = rand()% (high - low + 1) + low;
int col = rand()% (high - low + 1) + low;
if (Sludge[row][col].type == 3)
{
Sludge[row][col] = slowSlime;
i++;
}
}

//Random Distribution of Ravenouse Slimes
int c = 0;
while (c<5)
{
int row = rand()% (high - low + 1) + low;
int col = rand()% (high - low + 1) + low;
if (Sludge[row][col].type ==3)
{
Sludge[row][col] = ravenousSlime;
c++;
}
}

//Time steps
cout<<"Step 1"<<endl;
printLake(Sludge);
dispatch(Sludge, water);
fourNew(Sludge, water);
cout<<endl;
cout<<"Step 2"<<endl;
printLake(Sludge);
dispatch(Sludge, water);
cout<<endl;
fourNew(Sludge, water);
cout<<"Step 3"<<endl;
printLake(Sludge);
dispatch(Sludge, water);
cout<<endl;
fourNew(Sludge, water);
cout<<"Step 4"<<endl;
printLake(Sludge);
dispatch(Sludge, water);
fourNew(Sludge, water);
cout<<endl;
int j;
cin>>j;
return 0;
}

void printLake(Creatures Sludge[10][10])
{
for (int row = 0; row<10; row++)
{
for (int col = 0; col<10; col++)
{
if (Sludge[row][col].type == 1)
{
cout<<"S ";
}
if (Sludge[row][col].type == 2)
{
cout<<"R ";
}
if (Sludge[row][col].type == 3)
{
cout<<"_ ";
}
}
cout<<endl;
}
}

void fourNew(Creatures Sludge [10][10], Creatures slowSlime)
{
srand(time(0));
int high = 10;
int low = 0;
int i = 0;
while(i<4)
{
int row = rand()% (high - low + 1) + low;
int col = rand()% (high - low + 1) + low;
if (Sludge[row][col].type == 3)
{
Sludge[row][col] = slowSlime;
i++;
}
}
}

void dispatch(Creatures Sludge[10][10], Creatures water)
{
for (int row = 0; row<10; row++)
{
for(int col = 0; col<10; col++)
{
if(Sludge[row][col].type == 2)
{
if(Sludge[row][col-1].type == 1)
{
Sludge[row][col-1] = water;
}
if(Sludge[row][col+1].type == 1)
{
Sludge[row][col+1] = water;
}
if(Sludge[row-1][col].type == 1)
{
Sludge[row-1][col] = water;
}
if(Sludge[row+1][col].type == 1)
{
Sludge[row+1][col] = water;
}
}
}
}
}

输出:

更新输出: enter image description here enter image description here

我在第 4 步看到了一些新的缓慢的史莱姆,但在第 2 步没有看到...

最佳答案

快速浏览后,我会说问题出在这里:

fourNew(Sludge, water);

你想要的可能是:

fourNew(Sludge, slowSlime);

详细说明:您的功能

void fourNew(Creatures Sludge [10][10], Creatures slowSlime)

有两个参数,分别是 Sludge 和 slowSlime。这是一个不幸的名称选择,因为在内部,编译器不使用这些名称来生成代码;它们完全是为了您的方便。您可能希望编译器自动检查是否将正确的参数传递给函数,但 C++ 没有允许这样做的工具 - 不检查变量名,只检查类型。

函数参数充当局部变量。当您在 main 中传递值时:

fourNew(Sludge, water)

这是怎么回事:

fourNew() 中的 Sludge 等于 main() 中的 Sludge

fourNew() 中的 slowSlime 变得等于 main() 中的水

因此实际上您要求创建四个水对象。

关于c++ - 严重找不到我的 "game of life"程序中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13594820/

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