- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
好吧,不久前我被分配到一个实验室,我们应该在其中制作一个非常简化的康威生命游戏。
规则:贪婪的史莱姆(由 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 步没有看到...
最佳答案
快速浏览后,我会说问题出在这里:
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/
我试图通过 DexClassLoader 从 jar 文件加载类: public class AndroidClassLoader implements ClassLoader { priva
我试图了解如何为自己的类型编写trait和impl,以处理一些输入数据。我从一个简单的示例开始,在该示例中,我想使用1, 2, 3, 4处理输入的trait Processor。一个实现将跳过第一个元
生命游戏的算法就不多解释了,百度一下介绍随处可见。 因为网上大多数版本都是基于pygame,matlab等外部库实现的,二维数组大多是用numpy,使用起来学习成本比较高,所以闲暇之余写一个不用外
若凌° Provence - wirepuller 阴谋者 。 交换末日- Brave Hold on Welcome to my life 匪我思存 · L⊙ve/
我在一个网站上工作,用户输入几个名字,它在模板上以漂亮的字体呈现,以便稍后打印和跟踪到其他东西(纸、木头、金属等)。 让我们假设模板是 4"x 3"实物大小。是否可以在所有设备上显示 4"x 3"的图
这是一个有点理论性的问题。在编程作业中,约翰康威告诉我们要实现生命游戏。作为一项附加任务,我们被要求修改程序,以便它可以检测最多四代的模式重复。例如,给定这个特定的游戏“种子”,程序应该像这样: -
我目前正在使用JavaScript编写Conways生活游戏,但是遇到了一些我不知道如何解决的错误。想知道是否有人可以帮助我解决这些问题? 当前在Google Chrome浏览器中显示4个错误,具体如
我和四边形邻居一起写了经典游戏“生活”。当我在调试中运行它时,它说: 连续版本:4.2s 并行版本:1.5s 好的,很好。但是如果我在 release 中运行它,它会说: 连续版本:0.46s 并行版
我正在制作一个 Android 应用程序,它将包含两项服务,每 24 小时持续发送一次有关用户手机使用情况的数据。 用户应该执行该应用程序,切换按钮以启用手机使用记录,然后用户应该能够使用他的手机进行
我有一些可用的 Game of Life 代码。它将每个种群保存为位图。这是输出的样子(裁剪): 在清理代码时,我发现如果我注释掉或以其他方式删除第60行: cout #include #incl
难道后面函数返回的指针是不可访问的吗? char *foo(int rc) { switch (rc) { case 1: return("on
简介 细胞自动机(又称元胞自动机),名字虽然很深奥,但是它的行为却是非常美妙的。所有这些怎样实现的呢?我们可以把计算机中的宇宙想象成是一堆方格子构成的封闭空间,尺寸为N的空间就有NN个格子。而每一
Everything you want comes after you stop looking for it. 当你不再寻觅,你想要的也就来了。 I’m counting the days ti
正如主题所述,我很难想象在现实生活中的示例中何时何地使用可恢复异常,以及我们可以通过使用它们获得哪些有效优势。 我能想象的是,一个子系统已连接,比方说通过 RFC 连接到一个 session ,该 s
什么是“双生命”模块? perldelta for Perl 5.14 中有提到. 最佳答案 Dual Life 模块是存在于两个断开连接的源存储库中的模块,通常意味着在 Perl 核心(与 perl
我想自定义站点管理员角色权限,即我需要启用一项功能来创建相应站点下的站点管理用户。我怎样才能做到这一点?我需要用 Hook 来实现它还是需要创建 EXT?或者还有其他更简单的方法来实现这一目标吗?我需
我在 uminokirin.com 有一个用榆树编写的 Conway 生命游戏的基本实现。 . 来源可见here . 该程序允许用户调整环形网格的大小、单击单元格以更改其状态以及随机化世界。它适用于我
我已经研究这段代码很长时间了,但我似乎无法弄清楚。我的代码的顶部部分用于预填充网格。但由于某种原因,我无法让我的形象按照预期移动和成长。请帮助我!! import java.util.Random;
package edu.bsu.cs121.mamurphy; import java.util.*; import java.io.*; import javax.swing
我想要生命之花,就像这个:https://jsfiddle.net/5ak8P var canvas = document.getElementById('canvas'), context
我是一名优秀的程序员,十分优秀!