So I'm writing a version of the game Hunt the Wumpus in C++. The only real difference is that I'm not worried about the cave having a dodecahedron shape.
So far I've implemented the creation of the cave and the random insertion of the hero, bat, wumpus, and pit.
// Hunt the Wumpus
#include "std_lib_facilities.h"
#include "time.h"
class Room{
bool is_occupied;
bool has_wumpus;
bool has_bat;
bool has_pit;
public:
Room() // default constructor
{
is_occupied = false;
has_wumpus = false;
has_bat = false;
has_pit = false;
}
void random_insert(vector<Room>& v);
};
void Room::random_insert(vector<Room>& v)
{
srand(time(NULL));
int random_room = 0;
bool crowded = true;
random_room = rand() % 20; // insert hero
v[random_room].is_occupied = true;
while(crowded) // insert...THE WUMPUS!
{
random_room = rand() % 20;
if(v[random_room].is_occupied) ;
else if(!v[random_room].is_occupied)
{
v[random_room].has_wumpus = true;
crowded = false;
}
}
crowded = true;
while(crowded) // insert bat
{
random_room = rand() % 20;
if(v[random_room].is_occupied || v[random_room].has_wumpus) ;
else if(!v[random_room].is_occupied && !v[random_room].has_wumpus)
{
v[random_room].has_bat = true;
crowded = false;
}
}
crowded = true;
while(crowded) // insert pit
{
random_room = rand() % 20;
if(v[random_room].is_occupied || v[random_room].has_wumpus || v[random_room].has_bat) ;
else if(!v[random_room].is_occupied && !v[random_room].has_wumpus && !v[random_room].has_bat)
{
v[random_room].has_pit = true;
crowded = false;
}
}
}
vector<Room> create(Room& r)
{
vector<Room> c;
for(int i = 0; i < 20; ++i)
c.push_back(r);
return c;
}
int main()
{
Room r;
vector<Room> cave = create(r); // create cave
r.random_insert(cave); // randomly insert things
}
I've got a good idea of how I'm going to implement everything else with bats dropping the hero into random places, shooting, outputting current situation, etc.
However, I'm not sure how to deal with randomly connecting the rooms in the cave. I've considered doing some kind of random sort in the vector and then connecting the rooms to the left and right with pointers, but that is just a long hallway, not a cave. Maybe I could create some kind of coordinates system? Does anyone have any recommendations? Thanks.