SHARE
TWEET

text adventure listing part 3

KeithS Nov 20th, 2012 1,106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. enum en_VERBS {GET, DROP, USE, OPEN, CLOSE, EXAMINE, INVENTORY, LOOK};
  11.  
  12. const int NONE = -1;
  13. const int DIRS = 4;
  14. const int ROOMS = 11;
  15. const int VERBS = 8;
  16.  
  17. struct word
  18. {
  19.     string word;
  20.     int code;
  21. };
  22.  
  23. struct room
  24. {
  25.     string description;
  26.     int exits_to_room[DIRS];
  27. };
  28.  
  29. // -------------------------------------------------------------------------------------------------
  30.  
  31. void set_rooms(room *rms)
  32. {
  33.     rms[SPORTSHOP].description.assign("sports shop");
  34.     rms[SPORTSHOP].exits_to_room[NORTH] = NONE;
  35.     rms[SPORTSHOP].exits_to_room[EAST] = NONE;
  36.     rms[SPORTSHOP].exits_to_room[SOUTH] = CARPARK;
  37.     rms[SPORTSHOP].exits_to_room[WEST] = NONE;
  38.  
  39.     rms[CASINO].description.assign("bustling casino");
  40.     rms[CASINO].exits_to_room[NORTH] = NONE;
  41.     rms[CASINO].exits_to_room[EAST] = NONE;
  42.     rms[CASINO].exits_to_room[SOUTH] = LOBBY;
  43.     rms[CASINO].exits_to_room[WEST] = NONE;
  44.  
  45.     rms[CARPARK].description.assign("car park");
  46.     rms[CARPARK].exits_to_room[NORTH] = SPORTSHOP;
  47.     rms[CARPARK].exits_to_room[EAST] = LOBBY;
  48.     rms[CARPARK].exits_to_room[SOUTH] = NONE;
  49.     rms[CARPARK].exits_to_room[WEST] = NONE;
  50.  
  51.     rms[LOBBY].description.assign("hotel lobby");
  52.     rms[LOBBY].exits_to_room[NORTH] = CASINO;
  53.     rms[LOBBY].exits_to_room[EAST] = RESTAURANT;
  54.     rms[LOBBY].exits_to_room[SOUTH] = CORRIDOR;
  55.     rms[LOBBY].exits_to_room[WEST] = CARPARK;
  56.  
  57.     rms[RESTAURANT].description.assign("restaurant");
  58.     rms[RESTAURANT].exits_to_room[NORTH] = NONE;
  59.     rms[RESTAURANT].exits_to_room[EAST] = NONE;
  60.     rms[RESTAURANT].exits_to_room[SOUTH] = NONE;
  61.     rms[RESTAURANT].exits_to_room[WEST] = LOBBY;
  62.  
  63.     rms[CORRIDOR].description.assign("corridor");
  64.     rms[CORRIDOR].exits_to_room[NORTH] = LOBBY;
  65.     rms[CORRIDOR].exits_to_room[EAST] = STOREROOM;
  66.     rms[CORRIDOR].exits_to_room[SOUTH] = GARDEN;
  67.     rms[CORRIDOR].exits_to_room[WEST] = NONE;
  68.  
  69.     rms[STOREROOM].description.assign("store room");
  70.     rms[STOREROOM].exits_to_room[NORTH] = NONE;
  71.     rms[STOREROOM].exits_to_room[EAST] = NONE;
  72.     rms[STOREROOM].exits_to_room[SOUTH] = NONE;
  73.     rms[STOREROOM].exits_to_room[WEST] = CORRIDOR;
  74.  
  75.     rms[POOL].description.assign("swimming pool area");
  76.     rms[POOL].exits_to_room[NORTH] = NONE;
  77.     rms[POOL].exits_to_room[EAST] = GARDEN;
  78.     rms[POOL].exits_to_room[SOUTH] = PUMPROOM;
  79.     rms[POOL].exits_to_room[WEST] = NONE;
  80.  
  81.     rms[GARDEN].description.assign("tranquil garden");
  82.     rms[GARDEN].exits_to_room[NORTH] = CORRIDOR;
  83.     rms[GARDEN].exits_to_room[EAST] = POND;
  84.     rms[GARDEN].exits_to_room[SOUTH] = NONE;
  85.     rms[GARDEN].exits_to_room[WEST] = POOL;
  86.  
  87.     rms[POND].description.assign("patio with a fish pond");
  88.     rms[POND].exits_to_room[NORTH] = NONE;
  89.     rms[POND].exits_to_room[EAST] = NONE;
  90.     rms[POND].exits_to_room[SOUTH] = NONE;
  91.     rms[POND].exits_to_room[WEST] = GARDEN;
  92.  
  93.     rms[PUMPROOM].description.assign("damp pump room");
  94.     rms[PUMPROOM].exits_to_room[NORTH] = POOL;
  95.     rms[PUMPROOM].exits_to_room[EAST] = NONE;
  96.     rms[PUMPROOM].exits_to_room[SOUTH] = NONE;
  97.     rms[PUMPROOM].exits_to_room[WEST] = NONE;
  98. }
  99.  
  100. // -------------------------------------------------------------------------------------------------
  101.  
  102. void set_directions(word *dir)
  103. {
  104.     dir[NORTH].code = NORTH;
  105.     dir[NORTH].word = "NORTH";
  106.     dir[EAST].code = EAST;
  107.     dir[EAST].word = "EAST";
  108.     dir[SOUTH].code = SOUTH;
  109.     dir[SOUTH].word = "SOUTH";
  110.     dir[WEST].code = WEST;
  111.     dir[WEST].word = "WEST";
  112. }
  113.  
  114. // -------------------------------------------------------------------------------------------------
  115.  
  116. void set_verbs(word *vbs)
  117. {
  118.     // enum en_VERBS {GET, DROP, USE, OPEN, CLOSE, EXAMINE, INVENTORY, LOOK};
  119.     vbs[GET].code = GET;
  120.     vbs[GET].word = "GET";
  121.     vbs[DROP].code = DROP;
  122.     vbs[DROP].word = "DROP";
  123.     vbs[USE].code = USE;
  124.     vbs[USE].word = "USE";
  125.     vbs[OPEN].code = OPEN;
  126.     vbs[OPEN].word = "OPEN";
  127.     vbs[CLOSE].code = CLOSE;
  128.     vbs[CLOSE].word = "CLOSE";
  129.     vbs[EXAMINE].code = EXAMINE;
  130.     vbs[EXAMINE].word = "EXAMINE";
  131.     vbs[INVENTORY].code = INVENTORY;
  132.     vbs[INVENTORY].word = "INVENTORY";
  133.     vbs[LOOK].code = LOOK;
  134.     vbs[LOOK].word = "LOOK";
  135. }
  136.  
  137. // -------------------------------------------------------------------------------------------------
  138.  
  139. void section_command(string Cmd, string &wd1, string &wd2)
  140. {
  141.     string sub_str;
  142.     vector<string> words;
  143.     char search = ' ';
  144.     size_t i, j;
  145.  
  146.     // Split Command into vector
  147.     for(i = 0; i < Cmd.size(); i++)
  148.     {
  149.         if(Cmd.at(i) != search)
  150.         {
  151.             sub_str.insert(sub_str.end(), Cmd.at(i));
  152.         }
  153.         if(i == Cmd.size() - 1)
  154.         {
  155.             words.push_back(sub_str);
  156.             sub_str.clear();
  157.         }
  158.         if(Cmd.at(i) == search)
  159.         {
  160.             words.push_back(sub_str);
  161.             sub_str.clear();
  162.         }
  163.     }
  164.     // Clear out any blanks
  165.     // I work backwards through the vectors here as a cheat not to invaldate the iterator
  166.     for(i = words.size() - 1; i > 0; i--)
  167.     {
  168.         if(words.at(i) == "")
  169.         {
  170.             words.erase(words.begin() + i);
  171.         }
  172.     }
  173.     // Make words upper case
  174.     // Right here is where the functions from cctype are used
  175.     for(i = 0; i < words.size(); i++)
  176.     {
  177.         for(j = 0; j < words.at(i).size(); j++)
  178.         {
  179.             if(islower(words.at(i).at(j)))
  180.             {
  181.                 words.at(i).at(j) = toupper(words.at(i).at(j));
  182.             }
  183.         }
  184.     }
  185.     // Very simple. For the moment I only want the first to words at most (verb / noun).
  186.     if(words.size() == 0)
  187.     {
  188.         cout << "No command given" << endl;
  189.     }
  190.     if(words.size() == 1)
  191.     {
  192.         wd1 = words.at(0);
  193.     }
  194.     if(words.size() == 2)
  195.     {
  196.         wd1 = words.at(0);
  197.         wd2 = words.at(1);
  198.     }
  199.     if(words.size() > 2)
  200.     {
  201.         cout << "Command too long. Only type one or two words (direction or verb and noun)" << endl;
  202.     }
  203. }
  204.  
  205. // ----------------------------------------------------------------------------------------
  206. void look_around(int loc, room *rms, word *dir)
  207. {
  208.     int i;
  209.     cout << "I am in a " << rms[loc].description << "." << endl;
  210.    
  211.     // LOOK should also allow the player to see what exits exist from the current room.
  212.     for(i = 0; i < DIRS; i++)
  213.     {
  214.         if(rms[loc].exits_to_room[i] != NONE)
  215.         {
  216.             cout << "There is an exit " << dir[i].word << " to a " << rms[rms[loc].exits_to_room[i]].description << "." << endl;
  217.         }
  218.     }
  219. }
  220.  
  221. // ----------------------------------------------------------------------------------------
  222.  
  223. bool parser(int &loc, string wd1, string wd2, word *dir, word *vbs, room *rms)
  224. {
  225.     int i;
  226.     for(i = 0; i < DIRS; i++)
  227.     {
  228.         if(wd1 == dir[i].word)
  229.         {
  230.             if(rms[loc].exits_to_room[dir[i].code] != NONE)
  231.             {
  232.                 loc = rms[loc].exits_to_room[dir[i].code];
  233.                 cout << "I am now in a " << rms[loc].description << "." << endl;
  234.                 return true;
  235.             }
  236.             else
  237.             {
  238.                 cout << "No exit that way." << endl;
  239.                 return true;
  240.             }
  241.         }
  242.     }
  243.  
  244.     // Handle verbs. As a reference, here are the verbs I am using in the game.
  245.     // enum en_VERBS {GET, DROP, USE, OPEN, CLOSE, EXAMINE, INVENTORY, LOOK};
  246.     int VERB_ACTION = NONE;
  247.    
  248.     for(i = 0; i < VERBS; i++)
  249.     {
  250.         if(wd1 == vbs[i].word)
  251.         {
  252.             VERB_ACTION = vbs[i].code;
  253.             break;
  254.         }
  255.     }
  256.    
  257.     if(VERB_ACTION == LOOK)
  258.     {
  259.         // This is an example of sub proceduralizing a function from the parser.
  260.         look_around(loc, rms, dir);
  261.         return true;
  262.     }
  263.  
  264.     if(VERB_ACTION == NONE)
  265.     {
  266.         cout << "No valid command entered." << endl;
  267.         return true;
  268.     }
  269.     return false;
  270. }
  271.  
  272. // ----------------------------------------------------------------------------------------
  273.  
  274.  
  275.  
  276. // ----------------------------------------------------------------------------------------
  277.  
  278. int main()
  279. {
  280.     string command;
  281.     string word_1;
  282.     string word_2;
  283.  
  284.     room rooms[ROOMS];
  285.     set_rooms(rooms);
  286.  
  287.     word directions[DIRS];
  288.     set_directions(directions);
  289.  
  290.     word verbs[VERBS];
  291.     set_verbs(verbs);
  292.  
  293.     int location = CARPARK; // using the enumerated type identifier, of course.
  294.  
  295.     while(word_1 != "QUIT")
  296.     {
  297.         command.clear();
  298.         cout << "What shall I do? ";
  299.         getline(cin, command);
  300.         //cout << "Your raw command was " << command << endl;
  301.  
  302.         word_1.clear();
  303.         word_2.clear();
  304.  
  305.         // Call the function that handles the command line format.
  306.         section_command(command, word_1, word_2);
  307.  
  308.         // Call the parser.
  309.         if(word_1 != "QUIT")
  310.         {
  311.             parser(location, word_1, word_2, directions, verbs, rooms);
  312.         }
  313.     }
  314.     return 0;
  315. }
RAW Paste Data
Want to get better at C++?
Learn to code C++ in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top