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