随机漫步计划的多维数组

时间:2015-07-12 18:31:20

标签: c++ arrays multidimensional-array

所以我正在为我的C ++类开发我的多维数组作业,老实说,我对这应该如何工作100%迷失。这是随机网格漫步,我发现了很多例子,但我仍然只是不理解它。我理解程序应该输出什么,但它是如何让我难倒,逻辑上我只是没有得到它。如果有人能向我解释代码在做什么以及我是如何真正感激它的。我可以从他们的代码中进行编码,只是试图理解它正在踢我的屁股。

  

作业的要求和常量:

  • 图书馆:iostream,cstdlib(for stand& rand),ctime(时间)
  • const int SIZE = 10; (10永远不会出现在程序中必须通过变量调用)
  • typedef char Grid [SIZE] [SIZE];
  • 功能原型必须如图所示使用,但可以添加功能。

我们给主程序的模拟看起来像这样:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

/*** Function Prototypes ***/
bool can_move_up(int i, int j, Grid walk);
bool can_move_up(int i, int j, Grid walk);
bool can_move_up(int i, int j, Grid walk);
bool can_move_up(int i, int j, Grid walk);
void init_array(Grid walk);
void generate_random_walk(Grid walk);
void print_array(Grid walk);
/***************************/



int main(void)
{
const int SIZE = 10;
typedef char Grid[SIZE][SIZE];

Grid walk; // the grid in which the random walk occurs

srand(static_cast<unsigned>(time(NULL)));

init_array(walk);
generate_random_walk(walk);
print_array(walk);

return 0;

}

这是我到目前为止提出的代码。我无法让它运行,但我认为我在正确的轨道上我只是在混淆逻辑上从哪里去。我强调我想要理解这是如何工作的,而不是有人为我做这件事。我不是CS或CE专业,但我发现这些东西很有趣。

(我正在使用类并将它分成.cpp和... .hpp文件,但是将类部分放在代码的开头,以便大家看看我的类是如何设置的,并且其中的函数设置为的工作。)

感谢您的帮助!

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


// References these two variables to the other classes through public domain with in class
class BaseClass
{
public:
    const static int SIZE = 10;
    typedef char Grid[SIZE][SIZE]; 
private:

};

// Contains the initialization, generation, and printing functions for the program
class RandomWalkClass : public BaseClass
{
public:
    // Initializes the Grid filling all 100 spaces with '.' and starting poin "0,0" with 'A'
    void init_array(Grid walk)
    {
        for (int i = 0; i < SIZE; i++)
        {
            for (int j = 0; j < SIZE; j++)
            {
                Grid[i][j] = '.'; // Xcode gives me error for '=' "Expected unqualified-id"
                Grid[0][0] = 'A'; // Xcode gives me error for '=' "Expected unqualified-id"
            }
        }
    }
    // I am honestly not sure what to do with this funciton or what should be included in it's body
    void generate_random_walk(Grid walk)
    {

    }
    // Will print the random walk to the grid
    void print_array(Grid walk)
    {
        for (int i = 0; i < SIZE; i++)
        {
            for (int j = 0; j < SIZE; j++)
            {
                cout << Grid[i][j]; // Xcode error "Unexpected type name 'Grid'"
            }
            cout << endl;
        }
   }
private:

};

// Contains the move functions for the program
class MoveDirectionClass : public BaseClass
{
public:
    bool can_move_up(int i, int j, Grid walk)
    {
        if (i > 0 && walk[i - 1][j] == '.')
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool can_move_down(int i, int j, Grid walk)
    {
        if (i < 9 && walk[i + 1][j] == '.')
        {
           return true;
        }
        else
        {
           return false;
        }
    }
    bool can_move_left(int i, int j, Grid walk)
    {
        if (j > 0 && walk[i][j - 1] == '.')
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool can_move_right(int i, int j, Grid walk)
    {
        if (j < 9 && walk[i][j + 1] == '.')
        {
            return true;
        }
        else
        {
            return false;
        }
   }
private:

};

#include "program6.hpp"

int main()
{
int i;
int j;
int walk;
int letter;
int move;

// Unsure of where this should go and what it's purpose to the program is (we've never discussed srand in class)
srand(static_cast<unsigned>(time(NULL)));

// Calls initialization of program
RandomWalkClass initialize;
initialize.init_array(<#char (*walk)[10]#>);

// randomly chooses 0,1,2,3
move = rand() % 4;

// Runs through alphabet with each move of the switch program
for (letter = 1; letter < 26; letter++)
{
    switch (move)
    {
        case 0:
            MoveDirectionClass up;
            up.can_move_up(<#int i#>, <#int j#>, <#char (*walk)[10]#>);
            break;
        case 1:
            MoveDirectionClass down;
            down.can_move_down(<#int i#>, <#int j#>, <#char (*walk)[10]#>);
            break;
        case 2:
            MoveDirectionClass left;
            left.can_move_left(<#int i#>, <#int j#>, <#char (*walk)[10]#>);
            break;
        case 3:
            MoveDirectionClass right;
            right.can_move_right(<#int i#>, <#int j#>, <#char (*walk)[10]#>);
            break;
        default: break;
    }

}

// Calls the printing of the grid with the random walk
RandomWalkClass generate;
generate.print_array(<#char (*walk)[10]#>);


return 0;
}

2 个答案:

答案 0 :(得分:0)

使用

而不是Grid[i][j] = '.'Grid[i][j] = 'A'
walk[i][j] = '.';
walk[0][0] = 'A';

答案 1 :(得分:0)

当涉及到Unexpected类型名称时,您执行了typedef Grid并且未将其放入您的班级。 好吧,只是从一个房间移动到另一个房间,如果你想要这个房间里的物品,我会做这样的事情。

#include <iostream>
#include <string>
using namespace std;
struct Room{
//insert variables here like
int object;
}
typedef struct Room room[5][5];
int main()
{
string direction;
int x;
int y;
room space; //This is the variable to access both the array and the struct
space[0][0].object = 0; //This variable represents what room you start off in and the 0 could represent the type of object or the fact that there is no object.
//Now create rooms [0][0] through [4][4] which is a 5 by 5 area. After that create this.
cout << "Enter W to go north, S for South, D for East, and A for West." << endl;
cin >> direction;
if (direction == "W")
y++;
if (direction == "S")
y--;
if (direction == "D")
x++;
if (direction == "A")
x--;
//Then repeat this code and access the different rooms using space[x][y].object or whatever the variable you want to access from the struct
}

如果您有任何问题或疑虑,请与我们联系。我还有一个游戏,有一个在房间之间移动的例子。我的电子邮件是joshoheaps@gmail.com