// @topic W15-7-010 maze game
// @brief program structures
#ifndef MAZE_H_INCLUDED
#define MAZE_H_INCLUDED
#include <vector>
#include <iostream>
// The world directions are enumerated in clockwise order
// The algorithms assume that the "current direction" is
// the direction that the visitor is facing
/*
N
^
W <-|-> E
v
S
*/
enum {
/*
Mark directions clockwise
N
W <-|-> E
S
*/
DIR_NORTH, DIR_EAST, DIR_SOUTH, DIR_WEST, DIR_ILLEGAL = -1
};
const int INVALID_LOCATION = -1;
char const* direction_2_string( int dir ) {
switch ( dir ) {
case DIR_NORTH: return "NORTH";
case DIR_EAST: return "EAST";
case DIR_SOUTH: return "SOUTH";
case DIR_WEST: return "WEST";
default: return "DIR_ILLEGAL";
}
}//direction_2_string
int compute_opposite_direction( int dir ) {
switch ( dir ) {
case DIR_NORTH: return DIR_SOUTH;
case DIR_EAST: return DIR_WEST;
case DIR_SOUTH: return DIR_NORTH;
case DIR_WEST: return DIR_EAST;
default: return DIR_ILLEGAL;
}
}//compute_opposite_direction
int compute_left_direction( int dir )
{
switch (dir) {
case DIR_NORTH: return DIR_WEST;
case DIR_EAST: return DIR_NORTH;
case DIR_SOUTH: return DIR_EAST;
case DIR_WEST: return DIR_SOUTH;
default: return DIR_ILLEGAL;
}
}//compute_left_direction
int compute_right_direction(int dir) {
return compute_opposite_direction( compute_left_direction( dir ) );
}//compute_right_direction
// Forward declare structures as necessary:
struct Maze;
struct Room;
struct Wall {
//----------------------------------------------------------------------
// constructors / destructor
//----------------------------------------------------------------------
virtual ~Wall() {}
//----------------------------------------------------------------------
// operations
//----------------------------------------------------------------------
virtual int accept_move( Maze* maze, Room* from_room ) const
{
return INVALID_LOCATION;
}//accept_move
// visualize the object
virtual void print() const
{
std::cout << "wall\n";
}//print
virtual Room* get_next_room( Room* current_room ) const
{
return nullptr;
}
};//struct Wall
// Simple room behaves like a cell with 4 walls.
// Alternatively, a wall can be replaced by a wall with the door,
// which is represented by the Door structure.
struct Room {
//----------------------------------------------------------------------
// data members
//----------------------------------------------------------------------
Wall wall_north; // default room structure is made of 4 walls
Wall wall_east;
Wall wall_south;
Wall wall_west;
Wall* wall_config[ 4 ]; // room configuration includes 4 elements
//----------------------------------------------------------------------
// constructors / destructor
//----------------------------------------------------------------------
// constructor
Room() {
wall_config[ DIR_NORTH ] = &wall_north;
wall_config[ DIR_EAST ] = &wall_east;
wall_config[ DIR_SOUTH ] = &wall_south;
wall_config[ DIR_WEST ] = &wall_west;
}//Room
//----------------------------------------------------------------------
// operations
//----------------------------------------------------------------------
// note that all configuration elements must derive from the Wall:
void update_configuration( Wall* wall, int dir ) {
wall_config[ dir ] = wall;
}//update_configuration
Wall* get_face( int dir ) const
{
return wall_config[ dir ];
}//get_face
};//struct Room
struct Door : public Wall {
//----------------------------------------------------------------------
// data members
//----------------------------------------------------------------------
Room* first;
Room* second;
//----------------------------------------------------------------------
// constructors / destructor
//----------------------------------------------------------------------
// visitor_dir is the direction that the visitor
// is facing when about to move from
// room 1 (current room) to room 2 (next room)
Door( int visitor_dir, Room& room1, Room& room2 )
:
first( &room1 ),
second( &room2 )
{
first->update_configuration( this, visitor_dir );
second->update_configuration( this, compute_opposite_direction( visitor_dir ) );
}
//----------------------------------------------------------------------
// operations
//----------------------------------------------------------------------
int accept_move( Maze* maze, Room* from_room ) const;
virtual void print() const
{
std::cout << "door\n";
}
virtual Room* get_next_room( Room* current_room ) const
{
if ( current_room == first ) return second;
return first;
}
};//struct Door
struct Maze {
//----------------------------------------------------------------------
// data members
//----------------------------------------------------------------------
std::vector< Room* > rooms;
//----------------------------------------------------------------------
// operations
//----------------------------------------------------------------------
void add( Room& room ) {
rooms.push_back( &room );
}//add
Room* get_room( int room_index ) const
{
return rooms[ room_index ];
}//get_room
// Converts address of the Room object into its index in the maze layout vector:
int get_location_of( Room* room ) {
for (size_t idx = 0; idx < rooms.size(); ++idx ) {
if ( room == rooms[ idx ] ) return idx;
}
return INVALID_LOCATION;
}//get_location_of
};//struct Maze
struct Visitor {
// TODO:
// Enhance the visitor by adding a "memory":
// keep track of the directions where you came from.
// this way, the visitor may be able to navigate back
// through the doors and passages stored in the visitor's "memory"
//----------------------------------------------------------------------
// data members
//----------------------------------------------------------------------
int direction; // direction that the visitor is currently facing
int location; // the index into the Maze::rooms vector
Maze * maze; // current layout
//----------------------------------------------------------------------
// constructors / destructor
//----------------------------------------------------------------------
// Place the visitor in the first room of the Maze::rooms vector
Visitor( Maze& maze )
:
direction( DIR_NORTH ),
location( 0 ),
maze( &maze )
{
}
//----------------------------------------------------------------------
// operations
//----------------------------------------------------------------------
// moves visitor in current direction as far as possible
// returns number of steps taken by this move attempt
// returns zero if the visitor could not move
int move_forward() {
Room* current_room = maze->get_room( location );
// obtain an object that the visitor is facing:
Wall* room_face = current_room->get_face( direction );
Room* next_room = room_face->get_next_room( current_room );
if ( next_room == nullptr ) {
// TODO:
// penalize the user for an attempt to
// use wall as a door
std::cout << "\t *** bad move\n";
return 0;
}
int new_location = room_face->accept_move(
maze,
next_room
);
if ( new_location == INVALID_LOCATION ) {
//TODO: penalize the user
std::cout << "\t *** bad move\n";
return 0;
}
int steps = ( location == new_location ? 0 : 1 );
if ( steps ) {
location = new_location; // accept new location
std::cout << "\t good move!\n";
return steps;
}
//TODO: penalize the user
std::cout << "\t *** bad move\n";
return 0;
}//move_forward
// display what the visitor may be able to see in the room
void look_around() {
Room* current_room = maze->get_room( location );
std::cout
<< "Visitor facing "
<< direction_2_string( direction )
<< " at location "
<< location
<< "\n"
;
std::cout << "ahead: ";
Wall* room_face = current_room->get_face( direction );
room_face->print();
std::cout << "\n";
std::cout << " left: ";
room_face = current_room->get_face( compute_left_direction( direction ) );
room_face->print();
std::cout << "\n";
std::cout << "right: ";
room_face = current_room->get_face( compute_right_direction( direction ) );
room_face->print();
std::cout << "\n";
}//look_around
void turn_right() {
direction = compute_right_direction( direction );
}//turn_right
void turn_left() {
direction = compute_left_direction( direction );
}//turn_left
};//struct Visitor
int Door::accept_move( Maze* maze, Room* into_room ) const
{
return maze->get_location_of( into_room );
}//Door::accept_move
#endif //MAZE_H_INCLUDED