【问题标题】:Trouble understanding functions难以理解函数
【发布时间】:2014-05-16 02:12:09
【问题描述】:

基本上我有一张藏宝图,用户正在尝试寻找宝藏。藏宝图应该是用二维数组打印的。我们应该调用函数来随机化宝藏和起始位置、用户的每一轮等等。

我不知道如何在我的函数中声明我的变量。每次编写函数时我都必须重新声明,但我的教授说我应该只声明一次?

我的教授写的主要函数:

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!!!\n\n\n";


   // 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';

谁能告诉我这些是否正确以及我是否应该一遍又一遍地声明我的变量?

【问题讨论】:

  • 如果你的教授写了main,那么我不明白他/她的意图。看起来TreasureCTreasureR 是全局变量或类成员,但您的教授在main 中将它们声明为局部变量,这是没有意义的。我会回到教授那里要求澄清。
  • 刚刚贴出了给定的全局变量

标签: c++ function multidimensional-array


【解决方案1】:

你的问题是你要重新声明

int TreasureR, TreasureC; 
int StartR, StartC; 
int Row, Col;   

mainRandom

只需通过引用 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 函数执行相同操作。

您应该查看understanding variable scope 上的教程。也就是说,当你声明一个变量时,它只会“存活”一段时间(通常在{} 之间)。当您在main 中声明这些变量时,它们不会神奇地存在于您的函数中。您应该声明它们一次,然后将它们传递给需要它们的函数。

您的InitMap() 函数实际上应该遍历矩阵中的所有单元格并将它们设置为某个值。 Map = 0 不这样做。您需要像在 PrintMap 中那样遍历行和列

最后,在Random() 中,虽然您确实在计算行和列,但实际上并没有在 Map 中“放置”任何东西。你做事的方式,你实际上并不需要Map 数据结构;您可以只选择一个宝藏行和列,然后按原样使用您的PrintMap()函数。

【讨论】:

  • 如果教授没有规定如何声明函数,那么这个答案是正确的。
  • 我想我无法理解如何初始化一个数组并使其完全为空。我看看如果数组里面是数字怎么办,买我的数组是字符数组。
  • @savannaalexis: Map[row][col] = '0' 请注意,我说的是'0' 而不是0,因为'0'char0int
  • 非常感谢您的帮助。我想我现在想通了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多