2015-07-12 61 views
-2

所以我正在为我的C++类做多维数组作业,老实说我完全失去了应该如何工作。这是随机的网格走,我发现了很多的例子,但我仍然只是不理解它。我明白该节目应该输出什么,但这是我如何让自己难倒,逻辑上我只是不明白。如果任何人都可以向我解释代码在做什么以及我会如何欣赏它。我可以从他们的代码,它只是试图理解它是踢我的屁股。随机漫步程序的多维数组

要求和常数的分配:

  • 库:的iostream,cstdlib(对于支架& 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; 
} 
+1

'Grid [i] [j] ='。';'是错误的 - 您将'Grid'定义为类型,而不是变量。 –

+0

欢迎来到Stack Overflow。当你编写代码时,你应该从小而简单的工作开始,这个工作是完美的,然后每次增加一点复杂性。 *永远不要添加到不起作用的代码*您是否尝试过编码一维随机游走?如果你真的想要特定问题的帮助,那么请提出你的问题;一点点背景材料或背景是没问题的,但不要发布你所拥有的一切,并且要问十几个关于它的具体问题。 – Beta

回答

0

当轮到你做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 
} 

如果您有任何问题或疑虑,请让我知道。我也有一个在房间间移动的例子。我的电子邮件地址是:[email protected]

0

代替Grid[i][j] = '.'Grid[i][j] = 'A',使用

walk[i][j] = '.'; 
walk[0][0] = 'A'; 
+0

哦,我觉得那人真的很蠢!我知道这是一个简单的错误,我只是一直在盯着这个,并且已经认为它太多了。谢啦! –