#include <iostream>



/* Set up the travel table. */

int travel_table[3][6] = { 0, 0, 0, 0, 0, 0,
                           0, 5, 3, 0, 2, 1,
                           0, 0, 1, 0, 0, 0,
                           0, 4, 0, 0, 1, 0,
                           0, 0, 0, 3, 0, 0,
                           0,-7, 0, 1, 6, 0,
                           0, 0, 5, 0, 0, 0,
                           0, 0, 0, 5, 0, 0 };



void displayRoom( int room, bool hasTorch )
  {
  /* ----- Prints out a description of _room_, depending on whether
           the player has a torch or not.

     If the player doesn't have the torch, and the room has no torch,
       Say that the player can't see anything.
     Otherwise,
       Print a description for each room.
     If there are any items in this room,
       Print a description of the item.
  */

  /* If the player doesn't have the torch, and the room has no torch, */
  if( !hasTorch  &amp;&amp;  travel_table[room][5] != 1 )
    /* Say that the player can't see anything. */
    cout &lt;&lt; "It's very dark in here." &lt;&lt; endl;
  /* Otherwise,
       Print a description for each room. */
  else if( room == 1 )
    cout &lt;&lt; "You are standing in what was once a grand foyer.  Dust and cobwebs cover" &lt;&lt; endl &lt;&lt;
            "everything, from the richly-panelled oak walls to the faded russet carpet." &lt;&lt; endl &lt;&lt;
            "There are exits in all directions, except for the south door, which is now" &lt;&lt; endl &lt;&lt;
            "completely impassable." &lt;&lt; endl;
  else if( room == 2 )
    cout &lt;&lt; "This is a small antechamber, probably used as a small study.  A lopsided desk" &lt;&lt; endl &lt;&lt;
            "crouches forlornly under an aged, cracked window.  Nearby, a small collection" &lt;&lt; endl &lt;&lt;
            "of books are slowly crumbling into dust.  The only door leads east." &lt;&lt; endl;
  else if( room == 3 )
    cout &lt;&lt; "You stand in a small, wood-panelled hallway that runs north-south.  A glance" &lt;&lt; endl &lt;&lt;
            "at the doors on the east shows that they are firmly rusted shut; the only exits" &lt;&lt; endl &lt;&lt;
            "are to the east (back to the foyer), or north." &lt;&lt; endl;
  else if( room == 4 )
    cout &lt;&lt; "This bathroom is in a worse state than the rest of the house.  The bath tub" &lt;&lt; endl &lt;&lt;
            "is covered in dust, the sink is cracked, the wallpaper has long since peeled" &lt;&lt; endl &lt;&lt;
            "away, and most of the tiles are split." &lt;&lt; endl;
  else if( room == 5 )
    cout &lt;&lt; "This must have been a comfortable study once.  All of the furniture has been" &lt;&lt; endl &lt;&lt;
            "removed, though, leaving only bare bookcases and a hard wooden floor.  Large" &lt;&lt; endl &lt;&lt;
            "doors lead to the east and south, while a curious metal door has been set into" &lt;&lt; endl &lt;&lt;
            "the wall to the north." &lt;&lt; endl;
  else if( room == 6 )
    cout &lt;&lt; "This cramped, dusty pantry contains nothing of interest." &lt;&lt; endl;
  else if( room == 7 )
    cout &lt;&lt; "You are in a tiny elevator that looks like it was made many decades ago.  But" &lt;&lt; endl &lt;&lt;
            "more importantly, the cage has been broken open to the north.  You can see" &lt;&lt; endl &lt;&lt;
            "daylight pouring in from that direction!" &lt;&lt; endl;

  /* If there are any items in this room,
       Print a description of the item. */
  if( travel_table[room][5] == 1 )
    cout &lt;&lt; "There is a torch here." &lt;&lt; endl;
  else if( travel_table[room][5] == 2 )
    cout &lt;&lt; "You can see a small key lying on the ground here." &lt;&lt; endl;
  }

bool canMoveFromRoom( int room, int direction )
  {
  /* ----- Returns TRUE if the player can move out of the room, in
           the desired direction.

     If the way is 0,
       Say that you can't go there, and return false.
     If thet way is negative,
       Say that that way is locked, and return false.
     Otherwise,
       Return true.
  */

  /* If the way is 0, */
  if( travel_table[room][direction] == 0 )
    {
    /* Say that you can't go there, and return false. */
    cout &lt;&lt; "You can't go that way." &lt;&lt; endl;
    return false;
    }
  /* If thet way is negative, */
  else if( travel_table[room][direction] &lt; 0 )
    {
    /* Say that that way is locked, and return false. */
    cout &lt;&lt; "That door is locked." &lt;&lt; endl;
    return false;
    }
  /* Otherwise, */
  else
    {
    /* Return true. */
    return true;
    }
  }

int main( void )
  {
  bool boolHasTorch = false;
  bool boolHasKey   = false;
  int iRoom = 1;
  int iQuit = 0;
  char cCommand;

  /* Loop until we want to quit. */
  while( iQuit == 0 &amp;&amp; iRoom != 8 )
    {
    /* Print out the room description. */
    cout &lt;&lt; "--------------------" &lt;&lt; endl;
    displayRoom( iRoom, boolHasTorch || travel_table[iRoom][5] == 1 );
    /* Inform the player about what the player is carrying. */
    cout &lt;&lt; "You are carrying";
    if( boolHasTorch )
      cout &lt;&lt; " a torch";
    if( boolHasKey )
      cout &lt;&lt; " a key";
    if( !boolHasTorch &amp;&amp; !boolHasKey )
      cout &lt;&lt; " nothing";
    cout &lt;&lt; "." &lt;&lt; endl &lt;&lt; endl;

    /* Ask for a command. */
    cout &lt;&lt; "What do you want to do? ";
    cin &gt;&gt; cCommand;
    /* If the player wants to move in some direction,
         If that is possible,
           Move in that direction. */
    if( cCommand == 'n'  ||  cCommand == 'N' )
      {
      if( canMoveFromRoom( iRoom, 1 ) )
        iRoom = travel_table[iRoom][1];
      }
    if( cCommand == 'e'  ||  cCommand == 'E' )
      {
      if( canMoveFromRoom( iRoom, 2 ) )
        iRoom = travel_table[iRoom][2];
      }
    if( cCommand == 's'  ||  cCommand == 'S' )
      {
      if( canMoveFromRoom( iRoom, 3 ) )
        iRoom = travel_table[iRoom][3];
      }
    if( cCommand == 'w'  ||  cCommand == 'W' )
      {
      if( canMoveFromRoom( iRoom, 4 ) )
        iRoom = travel_table[iRoom][4];
      }
    /* If the player wants to pick up something, */
    if( cCommand == 'p'  ||  cCommand == 'P' )
      {
      /* If the room has a torch in it, */
      if( travel_table[iRoom][5] == 1 )
        {
        /* Pick up the torch. */
        boolHasTorch = true;
        cout &lt;&lt; "You have picked up a torch." &lt;&lt; endl;
        travel_table[iRoom][5] = 0; /* Remove the torch from the room */
        }
      /* If the room has a key in it, */
      else if( travel_table[iRoom][5] == 2 )
        {
        /* Pick up the key. */
        boolHasKey = true;
        cout &lt;&lt; "You have picked up a small gold key." &lt;&lt; endl;
        travel_table[iRoom][5] = 0;
        }
      /* Otherwise, */
      else
        {
        /* Report that there's nothing to pick up. */
        cout &lt;&lt; "There is nothing here to pick up." &lt;&lt; endl;
        }
      }
    /* If the player wants help, */
    else if( cCommand == 'h'  ||  cCommand == 'H' )
      {
      /* Print out a list of commands. */
      cout &lt;&lt; "Valid commands are:" &lt;&lt; endl
           &lt;&lt; "n -- Move north" &lt;&lt; endl
           &lt;&lt; "e -- Move east" &lt;&lt; endl
           &lt;&lt; "s -- Move south" &lt;&lt; endl
           &lt;&lt; "w -- Move west" &lt;&lt; endl
           &lt;&lt; "p -- Pick up anything in the room" &lt;&lt; endl
           &lt;&lt; "q -- Quit the game" &lt;&lt; endl;
      }
    /* If the player wants to quit, */
    if( cCommand == 'q'  ||  cCommand == 'Q' )
      {
      /* Quit. */
      iQuit = 1;
      }
    cout &lt;&lt; endl;

    if( iRoom == 8 )
      {
      cout &lt;&lt; "Congratulations!  You made it out of the mansion!" &lt;&lt; endl;
      cout &lt;&lt; "Well done!" &lt;&lt; endl;
      }
    }

  cout &lt;&lt; "Thanks for playing!" &lt;&lt; endl;

  return 0;
  }
