所以我正在为我的c++类做多维数组作业,老实说,我100%失去了这应该是如何工作的。这是随机网格行走,我已经找到了很多例子,但我仍然不理解它。我明白程序应该输出什么,但是它是如何把我难住的,逻辑上我就是不明白。如果有人能给我解释一下代码是怎么做的,我真的很感激。我可以从他们的代码,它只是试图理解它踢我的屁股。
赋值的要求和常量:
- Libraries: iostream, cstdlib (for stand &rand), ctime(表示时间) const int SIZE = 10;(10永远不能出现在程序中必须通过调用的变量)
- typepedef 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;
}
这是我到目前为止写出来的代码。我还没能让它运行,但我认为我在正确的轨道上,我只是对从这里到哪里逻辑困惑。我强调的是,我想从逻辑上理解这是如何工作的,而不是让别人为我做。我不是计算机科学或计算机工程专业的,但我确实觉得这些东西很有趣。
(我使用类,并将其分离到。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;
}
当涉及到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
用
代替Grid[i][j] = '.'
和Grid[i][j] = 'A'
walk[i][j] = '.';
walk[0][0] = 'A';