Stroustrup has an exercise in which the reader writes a program for the wumpus hunt game. A first draft of my code is below. It compiles and runs partly correctly. If you choose to move to a different room the program runs as expected. If you choose to shoot at the wumpus, I get something called run-time error #3. It seems that the room I call 'current' is being perceived as a variable. The error description is that the variable 'current' is not initialized.
I don't understand that error because the 'current' room variables are not used in the lines in question.
Here is my code. It is not a final version.
#include "../../std_lib_facilities.h"
class Room {
public:
double rand_wumpus;
double rand_pit;
double rand_bat;
bool wumpus_in_room;
bool pit_in_room;
bool bat_in_room;
int room_num;
};
int main() {
cout << "You are safe and in room 100!" << endl;
for (int i = 0; i < 5; i++) {
Room adjacent1;
srand((unsigned)time(0));
adjacent1.room_num = 120;
adjacent1.rand_wumpus = 100.0 * rand()/RAND_MAX;
adjacent1.rand_pit = 100.0 * rand()/RAND_MAX;
adjacent1.rand_bat = 100.0 * rand()/RAND_MAX;
if (adjacent1.rand_wumpus > 30.1 && adjacent1.rand_wumpus < 47.8) {
adjacent1.wumpus_in_room = true;
} else {
adjacent1.wumpus_in_room = false;
}
if (adjacent1.rand_pit > 83.1 && adjacent1.rand_pit < 95.5) {
adjacent1.pit_in_room = true;
} else {
adjacent1.pit_in_room = false;
}
if (adjacent1.rand_bat > 22.7 && adjacent1.rand_bat < 40.4) {
adjacent1.bat_in_room = true;
} else {
adjacent1.bat_in_room = false;
}
Room adjacent2;
srand((unsigned)time(0));
adjacent2.room_num = 150;
adjacent2.rand_wumpus = 100.0 * rand()/RAND_MAX;
adjacent2.rand_pit = 100.0 * rand()/RAND_MAX;
adjacent2.rand_bat = 100.0 * rand()/RAND_MAX;
if (adjacent2.rand_wumpus > 56.0 && adjacent2.rand_wumpus < 68.3) {
adjacent2.wumpus_in_room = true;
} else {
adjacent2.wumpus_in_room = false;
}
if (adjacent2.rand_pit > 5.8 && adjacent2.rand_pit < 22.4) {
adjacent2.pit_in_room = true;
} else {
adjacent2.pit_in_room = false;
}
if (adjacent2.rand_bat > 28.2 && adjacent2.rand_bat < 46.5) {
adjacent2.bat_in_room = true;
} else {
adjacent2.bat_in_room = false;
}
Room adjacent3;
srand((unsigned)time(0));
adjacent3.room_num = 160;
adjacent3.rand_wumpus = 100.0 * rand()/RAND_MAX;
adjacent3.rand_pit = 100.0 * rand()/RAND_MAX;
adjacent3.rand_bat = 100.0 * rand()/RAND_MAX;
if (adjacent3.rand_wumpus > 52.3 && adjacent3.rand_wumpus < 75.7) {
adjacent3.wumpus_in_room = true;
} else {
adjacent3.wumpus_in_room = false;
}
if (adjacent3.rand_pit > 57.4 && adjacent3.rand_pit < 75.2) {
adjacent3.pit_in_room = true;
} else {
adjacent3.pit_in_room = false;
}
if (adjacent3.rand_bat > 44.9 && adjacent3.rand_bat < 60.2) {
adjacent3.bat_in_room = true;
} else {
adjacent3.bat_in_room = false;
}
Room current;
cout << "There are passage ways to rooms " << adjacent1.room_num <<", " << adjacent2.room_num <<", and " << adjacent3.room_num <<"." << endl;
if (adjacent1.wumpus_in_room == true || adjacent2.wumpus_in_room == true || adjacent3.wumpus_in_room == true) {
cout << "I smell the wumpus." << endl;
}
if (adjacent1.pit_in_room == true || adjacent2.pit_in_room == true || adjacent3.pit_in_room == true) {
cout << "I feel a breeze." << endl;
}
if (adjacent1.bat_in_room == true || adjacent2.bat_in_room == true || adjacent3.bat_in_room == true) {
cout << "I hear a bat." << endl;
}
cout << endl;
cout << "Do you wish to move or shoot?" << endl;
string play = " ";
cin >> play;
srand((unsigned)time(0));
double prob_wumpus = rand()/RAND_MAX;
if (play == "s120") {
if (adjacent1.wumpus_in_room == true) {
cout << "You shot the wumpus! YOU WIN!!!" << endl;
}
if (adjacent1.wumpus_in_room == false) {
if (prob_wumpus <= 0.70) {
cout << "You missed the wumpus. Play again." << endl;
}
if (prob_wumpus > 0.70) {
cout << "You missed the wumpus. It has come into your room and eaten you!" << endl;
}
}
}
if (play == "s150") {
if (adjacent1.wumpus_in_room == true) {
cout << "You shot the wumpus! YOU WIN!!!" << endl;
}
if (adjacent1.wumpus_in_room == false) {
if (prob_wumpus <= 0.50) {
cout << "You missed the wumpus. Play again." << endl;
}
if (prob_wumpus > 0.50) {
cout << "You missed the wumpus. It has come into your room and eaten you!" << endl;
}
}
}
if (play == "s160") {
if (adjacent1.wumpus_in_room == true) {
cout << "You shot the wumpus! YOU WIN!!!" << endl;
}
if (adjacent1.wumpus_in_room == false) {
if (prob_wumpus <= 0.60) {
cout << "You missed the wumpus. Play again." << endl;
}
if (prob_wumpus > 0.60) {
cout << "You missed the wumpus. It has come into your room and eaten you!" << endl;
}
}
}
if (play == "m120") {
current.room_num = 120;
current.wumpus_in_room = adjacent1.wumpus_in_room;
current.pit_in_room = adjacent1.pit_in_room;
current.bat_in_room = adjacent1.bat_in_room;
}
if (play == "m150") {
current.room_num = 150;
current.wumpus_in_room = adjacent2.wumpus_in_room;
current.pit_in_room = adjacent2.pit_in_room;
current.bat_in_room = adjacent2.bat_in_room;
}
if (play == "m160") {
current.room_num = 160;
current.wumpus_in_room = adjacent3.wumpus_in_room;
current.pit_in_room = adjacent3.pit_in_room;
current.bat_in_room = adjacent3.bat_in_room;
}
if (current.wumpus_in_room == true) {cout << "The wumpus is in the room. You are eaten!" << endl;}
if (current.pit_in_room == true) {cout << "You have fallen into a pit and died!" << endl;}
if (current.bat_in_room == true) {cout << "A bat has taken you to another room!" << endl;}
if (current.wumpus_in_room == false && current.pit_in_room == false && current.bat_in_room == false) {
cout << "You are safe and in room " << current.room_num <<"!" << endl;
}
}
keep_window_open();
return 0;
}
Please explain the error to me and whether I am close to or far from the right track for this game. Thanks in advance.