Morning all, I've got a grid of rooms that I need to navigate round using directions in main that's working fine, in main I've put a random starting location that also works.
Ive also got a program with two functions in it, random starting location and random direction facing to implement at start up. What I'd like to do is use the functions but I have no idea how to implement this into one peice of code. I've tried to figure it out but I don't know whether I need to return the function by value or try to do it by reference, one function returns a string (facing) while the other (start room) returns a random int, any suggestions or help? Both bits of code below.

#include <iostream>
//#include <fstream.h>
using namespace std;

void start_direction (){
time_t now;
time(&now);
srand(now);
    string start_facing;
    string str7 ("North");
    string str8 ("East");
    string str9 ("South");
    string str10 ("West");
    int facing; 

facing = rand() % 4 + 1;

    if (facing == 1)
        start_facing = str7;
        else if (facing == 2)
        start_facing = str8;                
        else if (facing == 3)
        start_facing = str9;
        else 
        start_facing = str10;
        
        cout << "Facing " << start_facing << endl; // starting direction
} 
void start_room (){
time_t now;
time(&now);
srand(now);
    int room_location;
    room_location = rand() % 27;        //0 to 26
  cout << "starting room " << room_location << endl;
}

int main (){

  start_direction ();
  start_room ();
//  cout << "starting room " << room_location << endl;
  system ("Pause");
  return 0;
}
#include <iostream>
#include <stdlib.h>

using namespace std;
//int location;

void printInstructions ()
{
   cout << "You awake to find yourself locked in a grid of rooms" << endl;
   cout << "\n" << "Directions of travel are 2 for N, 6 for E, 8 for South, 4 for West," << endl;
   cout << "1 for UP, 3 for DOWN" << "\n" << endl;
   cout << "Enter your direction to travel?" << endl;
}

int main()
{
//random start location bit
srand((unsigned)time(0));           //random time generator
int start = rand () % 27;           //Placement location (start off)
//   int facing;
//   facing = rand() % 4 + 1;
   int direction;
   int location, old_location;

   int map[3][9][10] =
      {
      {
     //  0,  1,  2,  3,  4,  5,  6,  7,  8,  9

       {99,  9,  3, 99, 99, 99,  1, 99, 99, 99},  // 0
       {99, 10,  4, 99,  0, 99,  2, 99, 99, 99},  // 1
       {99, 11,  5, 99,  1, 99, 99, 99, 99, 99},  // 2
       {99, 12,  6, 99, 99, 99,  4, 99,  0, 99},  // 3
       {99, 13,  7, 99,  3, 99,  5, 99,  1, 99},  // 4
       {99, 14,  8, 99,  4, 99, 99, 99,  2, 99},  // 5
       {99, 15, 99, 99, 99, 99,  7, 99,  3, 99},  // 6
       {99, 16, 99, 99,  6, 99,  8, 99,  4, 99},  // 7
       {99, 17, 99, 99,  7, 99,  9, 99,  5, 99}   // 8
      },
      {
       {99, 18, 12,  0, 99, 99, 10, 99, 99, 99},   // 9
       {99, 19, 13,  1,  9, 99, 11, 99, 99, 99},  // 10
       {99, 20, 14,  2, 10, 99, 99, 99, 99, 99},  // 11
       {99, 21, 15,  3, 99, 99, 13, 99,  9, 99},  // 12
       {99, 22, 16,  4, 12, 99, 14, 99, 10, 99},  // 13
       {99, 23, 17,  5, 13, 99, 99, 99, 11, 99},  // 14
       {99, 24, 99,  6, 99, 99, 16, 99, 12, 99},  // 15
       {99, 25, 99,  7, 15, 99, 17, 99, 13, 99},  // 16
       {99, 26, 99,  8, 16, 99, 99, 99, 14, 99}   // 17
      },     
      {     
       {99, 99, 21,  9, 99, 99, 19, 99, 99, 99},  // 18
       {99, 99, 22, 10, 18, 99, 20, 99, 99, 99},  // 19
       {99, 99, 23, 11, 19, 99, 99, 99, 99, 99},  // 20
       {99, 99, 24, 12, 99, 99, 22, 99, 18, 99},  // 21
       {99, 99, 25, 13, 21, 99, 23, 99, 19, 99},  // 22
       {99, 99, 26, 14, 22, 99, 99, 99, 20, 99},  // 23
       {99, 99, 99, 15, 99, 99, 25, 99, 21, 99},  // 24
       {99, 99, 99, 16, 24, 99, 26, 99, 22, 99},  // 25
       {99, 99, 99, 17, 25, 99, 99, 99, 23, 99}   // 26
       }
       };

   location = start;
   old_location = location;
   printInstructions ();  //call to void function = no return arguement
   cout << "starting location = room " <<  location << "\n" << endl;

   while (location !=26)
   {
      cin >> direction;

      location = map[0][location][direction];

      if(location == 99)
      {
         cout<<"Incorrect move...!!!... Enter direction again."<< endl;
         location = old_location;
      }
      else
      {
         cout << "Your at location " << location << endl;
         old_location = location;

         if (location == 26)
         cout << "You've found the exit" << endl;

      }
   }
   return 0;
}

Thanks in advance

Leppie

Recommended Answers

All 5 Replies

Both functions should simply return an int (by value). Simply change void FunctionName() to int FunctionName() and at the end of the function, after you cout << something; now you need to return something;

Thanks

In start_direction though my return value is a string not an int? Have I got to allocate memory for this return value, as I read this somewhere? Start_room I've done returning an int but start_direction doessn't compile with either string or int?

What to return:

  • Option one: Instead of returning "East" or "South", return an index into an array of direction strings. That index is an int.
  • Option two: You are using std::string, so just return it by value. The library takes care of dealing with storage space.

The considerations are:

  1. Is it easier to understand the program one way or the other?
  2. If your random direction function needs to be compatible with the rest of the game, then numbers may be easier to compare.
  3. If you add new directions ("Up", or "North by North West"), how is your program going to cope? Would returning a new number be easier, or a new string?
  4. What would happen if you had to translate your game to some other language? Are you using code that says ... if(direction == "East") ... somewhere?

Doesn't compile: Step back two steps and think about a function. You declare it to return something (maybe void) and to take parameters. You also define it to do the work. The declaration (often in a header file) must match the body (often in a cpp file. When you call a function that returns other than void, you may assign the returned value to a variable (you can also just ignore it if you want to). The variable that holds the returned value must have a type (the same as, usually) that is compatible with the type of the returned value.

You could also use an 'enum'. This lets you see words rather than numbers when writing the code, but everything is translated to an int at compile time.

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.