Advertisement
KeithS

text adventure listing part 2

Nov 10th, 2012
1,891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>  // For the command handling function.
  4. #include <cctype>  // Will be used to eliminate case sensitivity problems.
  5.  
  6. using namespace std;
  7.  
  8. enum en_DIRS {NORTH, EAST, SOUTH, WEST};
  9. enum en_ROOMS {SPORTSHOP, CASINO, CARPARK, LOBBY, RESTAURANT, CORRIDOR, STOREROOM, POOL, GARDEN, POND, PUMPROOM};
  10.  
  11. const int NONE = -1;
  12. const int DIRS = 4;
  13. const int ROOMS = 11;
  14.  
  15. struct word
  16. {
  17.     string word;
  18.     int code;
  19. };
  20.  
  21. struct room
  22. {
  23.     string description;
  24.     int exits_to_room[DIRS];
  25. };
  26.  
  27. // -------------------------------------------------------------------------------------------------
  28.  
  29. void set_rooms(room *rms)
  30. {
  31.     rms[SPORTSHOP].description.assign("sports shop");
  32.     rms[SPORTSHOP].exits_to_room[NORTH] = NONE;
  33.     rms[SPORTSHOP].exits_to_room[EAST] = NONE;
  34.     rms[SPORTSHOP].exits_to_room[SOUTH] = CARPARK;
  35.     rms[SPORTSHOP].exits_to_room[WEST] = NONE;
  36.  
  37.     rms[CASINO].description.assign("bustling casino");
  38.     rms[CASINO].exits_to_room[NORTH] = NONE;
  39.     rms[CASINO].exits_to_room[EAST] = NONE;
  40.     rms[CASINO].exits_to_room[SOUTH] = LOBBY;
  41.     rms[CASINO].exits_to_room[WEST] = NONE;
  42.  
  43.     rms[CARPARK].description.assign("car park");
  44.     rms[CARPARK].exits_to_room[NORTH] = SPORTSHOP;
  45.     rms[CARPARK].exits_to_room[EAST] = LOBBY;
  46.     rms[CARPARK].exits_to_room[SOUTH] = NONE;
  47.     rms[CARPARK].exits_to_room[WEST] = NONE;
  48.  
  49.     rms[LOBBY].description.assign("hotel lobby");
  50.     rms[LOBBY].exits_to_room[NORTH] = CASINO;
  51.     rms[LOBBY].exits_to_room[EAST] = RESTAURANT;
  52.     rms[LOBBY].exits_to_room[SOUTH] = CORRIDOR;
  53.     rms[LOBBY].exits_to_room[WEST] = CARPARK;
  54.  
  55.     rms[RESTAURANT].description.assign("restaurant");
  56.     rms[RESTAURANT].exits_to_room[NORTH] = NONE;
  57.     rms[RESTAURANT].exits_to_room[EAST] = NONE;
  58.     rms[RESTAURANT].exits_to_room[SOUTH] = NONE;
  59.     rms[RESTAURANT].exits_to_room[WEST] = LOBBY;
  60.  
  61.     rms[CORRIDOR].description.assign("corridor");
  62.     rms[CORRIDOR].exits_to_room[NORTH] = LOBBY;
  63.     rms[CORRIDOR].exits_to_room[EAST] = STOREROOM;
  64.     rms[CORRIDOR].exits_to_room[SOUTH] = GARDEN;
  65.     rms[CORRIDOR].exits_to_room[WEST] = NONE;
  66.  
  67.     rms[STOREROOM].description.assign("store room");
  68.     rms[STOREROOM].exits_to_room[NORTH] = NONE;
  69.     rms[STOREROOM].exits_to_room[EAST] = NONE;
  70.     rms[STOREROOM].exits_to_room[SOUTH] = NONE;
  71.     rms[STOREROOM].exits_to_room[WEST] = CORRIDOR;
  72.  
  73.     rms[POOL].description.assign("swimming pool area");
  74.     rms[POOL].exits_to_room[NORTH] = NONE;
  75.     rms[POOL].exits_to_room[EAST] = GARDEN;
  76.     rms[POOL].exits_to_room[SOUTH] = PUMPROOM;
  77.     rms[POOL].exits_to_room[WEST] = NONE;
  78.  
  79.     rms[GARDEN].description.assign("tranquil garden");
  80.     rms[GARDEN].exits_to_room[NORTH] = CORRIDOR;
  81.     rms[GARDEN].exits_to_room[EAST] = POND;
  82.     rms[GARDEN].exits_to_room[SOUTH] = NONE;
  83.     rms[GARDEN].exits_to_room[WEST] = POOL;
  84.  
  85.     rms[POND].description.assign("patio with a fish pond");
  86.     rms[POND].exits_to_room[NORTH] = NONE;
  87.     rms[POND].exits_to_room[EAST] = NONE;
  88.     rms[POND].exits_to_room[SOUTH] = NONE;
  89.     rms[POND].exits_to_room[WEST] = GARDEN;
  90.  
  91.     rms[PUMPROOM].description.assign("damp pump room");
  92.     rms[PUMPROOM].exits_to_room[NORTH] = POOL;
  93.     rms[PUMPROOM].exits_to_room[EAST] = NONE;
  94.     rms[PUMPROOM].exits_to_room[SOUTH] = NONE;
  95.     rms[PUMPROOM].exits_to_room[WEST] = NONE;
  96. }
  97.  
  98. // -------------------------------------------------------------------------------------------------
  99.  
  100. void set_directions(word *dir)
  101. {
  102.     dir[NORTH].code = NORTH;
  103.     dir[NORTH].word = "NORTH";
  104.     dir[EAST].code = EAST;
  105.     dir[EAST].word = "EAST";
  106.     dir[SOUTH].code = SOUTH;
  107.     dir[SOUTH].word = "SOUTH";
  108.     dir[WEST].code = WEST;    
  109.     dir[WEST].word = "WEST";
  110. }
  111.  
  112. // -------------------------------------------------------------------------------------------------
  113.  
  114. void section_command(string Cmd, string &wd1, string &wd2)
  115. {
  116.     string sub_str;
  117.     vector<string> words;
  118.     char search = ' ';
  119.     size_t i, j;
  120.  
  121.     // Split Command into vector
  122.     for(i = 0; i < Cmd.size(); i++)
  123.     {
  124.         if(Cmd.at(i) != search)
  125.         {
  126.             sub_str.insert(sub_str.end(), Cmd.at(i));
  127.         }
  128.         if(i == Cmd.size() - 1)
  129.         {
  130.             words.push_back(sub_str);
  131.             sub_str.clear();
  132.         }
  133.         if(Cmd.at(i) == search)
  134.         {
  135.             words.push_back(sub_str);
  136.             sub_str.clear();
  137.         }
  138.     }
  139.     // Clear out any blanks
  140.     // I work backwards through the vectors here as a cheat not to invaldate the iterator
  141.     for(i = words.size() - 1; i > 0; i--)
  142.     {
  143.         if(words.at(i) == "")
  144.         {
  145.             words.erase(words.begin() + i);
  146.         }
  147.     }
  148.     // Make words upper case
  149.     // Right here is where the functions from cctype are used
  150.     for(i = 0; i < words.size(); i++)
  151.     {
  152.         for(j = 0; j < words.at(i).size(); j++)
  153.         {
  154.             if(islower(words.at(i).at(j)))
  155.             {
  156.                 words.at(i).at(j) = toupper(words.at(i).at(j));
  157.             }
  158.         }
  159.     }
  160.     // Very simple. For the moment I only want the first to words at most (verb / noun).
  161.     if(words.size() == 0)
  162.     {
  163.         cout << "No command given" << endl;
  164.     }
  165.     if(words.size() == 1)
  166.     {
  167.         wd1 = words.at(0);
  168.     }
  169.     if(words.size() == 2)
  170.     {
  171.         wd1 = words.at(0);
  172.         wd2 = words.at(1);
  173.     }
  174.     if(words.size() > 2)
  175.     {
  176.         cout << "Command too long. Only type one or two words (direction or verb and noun)" << endl;
  177.     }
  178. }
  179.  
  180. // ----------------------------------------------------------------------------------------
  181.  
  182. bool parser(int &loc, string wd1, string wd2, word *dir, room *rms)
  183. {
  184.     int i;
  185.     for(i = 0; i < DIRS; i++)
  186.     {
  187.         if(wd1 == dir[i].word)
  188.         {
  189.             if(rms[loc].exits_to_room[dir[i].code] != NONE)
  190.             {
  191.                 loc = rms[loc].exits_to_room[dir[i].code];
  192.                 cout << "I am now in a " << rms[loc].description << "." << endl;
  193.                 return true;
  194.             }
  195.             else
  196.             {
  197.                 cout << "No exit that way." << endl;
  198.                 return true;
  199.             }
  200.         }
  201.     }
  202.     cout << "No valid command entered." << endl;
  203.     return false;
  204. }
  205.  
  206. // ----------------------------------------------------------------------------------------
  207.  
  208. int main()
  209. {
  210.     string command;
  211.     string word_1;
  212.     string word_2;
  213.  
  214.     room rooms[ROOMS];
  215.     set_rooms(rooms);
  216.  
  217.     word directions[DIRS];
  218.     set_directions(directions);
  219.  
  220.     int location = CARPARK; // using the enumerated type identifier, of course.
  221.    
  222.     while(word_1 != "QUIT")
  223.     {
  224.         command.clear();
  225.         cout << "What shall I do? ";
  226.         getline(cin, command);
  227.         //cout << "Your raw command was " << command << endl;
  228.  
  229.         word_1.clear();
  230.         word_2.clear();
  231.  
  232.         // Call the function that handles the command line format.
  233.         section_command(command, word_1, word_2);
  234.        
  235.         // Call the parser.
  236.         parser(location, word_1, word_2, directions, rooms);
  237.        
  238.     }
  239.     return 0;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement