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

标签 c++ arrays grid

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

规则:贪婪的史莱姆(由 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/

相关文章:

c++ - 从字符串 vector 中获取字符串的第一个字符

C++ 对指针 vector 进行排序,其中对象表示矩阵坐标

C++将数据读入结构数组

c# - 我如何在 c# 中的特定字段值的结构数组上进行二进制搜索?

java - 命名和初始化数组中的对象

c++ - 在 vector C++ 中查找对象的指针

c++ - C++遍历一般树

algorithm - 当我们之间有一些 block 时,如何找到最接近点的点! (在 2D 数组中 - 贪吃蛇游戏)

magento - Magento 网格列中数据库的时间戳值

html - 无法制作 2x2 图像网格 [CSS]