好的,不久前我被分配了一个实验室,我们应该在其中制作一个非常简化版的康威人生游戏。
规则:贪婪的史莱姆(由 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;
}
}
}
}
}
输出:
更新的输出:
我在第 4 步看到了几个新的慢史莱姆,但在第 2 步没有看到...