难以理解功能
Trouble understanding functions
本文关键字:功能 更新时间:2023-10-16
基本上我有一个藏宝图,用户正在尝试找到宝藏。藏宝图应该是用2D阵列打印的。我们应该调用函数来随机化宝藏和开始位置,对于用户的每个回合,等等。
我不知道如何在函数中声明我的变量。每次我写函数时都必须重新声明,但我的教授说我应该只声明一次?
我教授编写的主要功能:
int main()
{
char Map[ROWS][COLS];
int TreasureR, TreasureC;
int StartR, StartC;
int Row, Col;
int NumMoves = 0; // The number of player moves
bool Winner = false;
bool Quit = false;
cout << "This homework was written by Savanna Bruce.n";
cout << "You are stranded on a desert island with no idea how to survive.n";
cout << "Fortunately, there are tools to survive and a hidden Treasure!n";
cout << "Find the Treasure!!!nnn";
// Seed the random number variable
srand (time(NULL));
// Start a New Game or Continue an Old one
InitMap (Map);
// Add code to place the treasure and start
Random();
// Add code to play the game
PlayTurn();
// Print the Map, hide the Treasure
PrintMap(Map, false);
return 0;
}
到目前为止,我拥有的功能:
// Name: InitMap
// Description: Initialize the Map with all EMPTY cells
// Return: Nothing
// ---------------------------------------------------
void InitMap(char Map[][COLS])
{
Map = 0;
}
// ---------------------------------------------------
void Random()
{
int TreasureC, TreasureR;
int StartC, StartR;
int Col, Row;
// Set the location of the treasure chest
TreasureC = rand() % COLS; // set to a value in range 0..XDIM-1
TreasureR = rand() % ROWS; // set to a value in range 0..YDIM-1
// Set the starting location of the player
StartC = rand() % COLS; // set to a value in range 0..XDIM-1
StartR = rand() % ROWS; // set to a value in range 0..YDIM-1
Col = StartC;
Row = StartR;
}
void PrintMap(const char Map[][COLS], const bool showTreasure)
{
int TreasureR = 0;
int TreasureC = 0;
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
if ((row == TreasureR && col == TreasureC) && showTreasure == true)
cout << TREASURE;
else
cout << EMPTY;
}
cout << endl;
}
}
全局变量:
const int FAST = 3;
const int SLOW = 5;
const int COLS = 20; // For MAP Size
const int ROWS = 10; // For MAP Size
const int MAX_ROW = ROWS - 1; // valid locations are 0..ROWS - 1
const int MAX_COL = COLS - 1; // valid locations are 0..COLS - 1
const string FILENAME = "Map.txt"; // File to save/load Map from
// Cell types - The Map can have any of these
// characters at a location on a Map.
const char START = 'S';
const char PLAYER = 'P';
const char TREASURE = 'T';
const char EMPTY = '*';
const char VISITED = 'X';
有人可以告诉我这些是否正确,以及我是否应该一遍又一遍地声明我的变量?
你的问题是你正在重新声明
int TreasureR, TreasureC;
int StartR, StartC;
int Row, Col;
在main
和Random
只需通过引用将这些值传递给Random()
即可解决您的问题:
void Random(int& TreasureC, int& TreasureR, int& StartC, int& startR, int& Col, int& Row)
{
// Set the location of the treasure chest
TreasureC = rand() % COLS; // set to a value in range 0..XDIM-1
TreasureR = rand() % ROWS; // set to a value in range 0..YDIM-1
// Set the starting location of the player
StartC = rand() % COLS; // set to a value in range 0..XDIM-1
StartR = rand() % ROWS; // set to a value in range 0..YDIM-1
Col = StartC;
Row = StartR;
}
对PrintMap
函数执行相同的操作。
您应该查看有关了解变量范围的教程。也就是说,当你声明一个变量时,它只"存在"一定的时间(通常在{}
之间)。当你在main
中声明这些变量时,它们不会神奇地存在于你的函数中。您应该声明它们一次,然后将它们传递给需要它们的函数。
您的InitMap()
函数实际上应该遍历矩阵中的所有单元格并将它们设置为某些内容。 Map = 0
不会那样做。您需要像在PrintMap
中一样遍历行和列
最后,在 Random()
中,虽然您确实在计算行和列,但您实际上并没有在 Map 中"放置"任何东西。你做事的方式,你实际上不需要Map
的数据结构;您可以只选择宝藏行和列,然后按原样使用PrintMap()
函数。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- 这里在 Linux 中具有"CreatePipe"和"CreateProcessW"功能吗?