SHARE
TWEET

Text Adventure Listing blog 4

KeithS Jan 12th, 2013 755 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cctype>
  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. // Added code
  13. enum en_NOUNS {STORE_DOOR, MAGNET, METER, ROULETTE, MONEY, FISHROD};
  14. // ....
  15.  
  16. const int NONE = -1;
  17. const int DIRS = 4;
  18. const int ROOMS = 11;
  19. const int VERBS = 8;
  20.  
  21. // Added code
  22. const int NOUNS = 6;
  23. // ....
  24.  
  25. struct word
  26. {
  27.     string word;
  28.     int code;
  29. };
  30.  
  31. struct room
  32. {
  33.     string description;
  34.     int exits_to_room[DIRS];
  35. };
  36.  
  37. // Added code
  38. struct noun
  39. {
  40.     string word;
  41.     string description;
  42.     int code;
  43.     int location;
  44.     bool can_carry;
  45. };
  46. // ....
  47.  
  48. // -------------------------------------------------------------------------------------------------
  49.  
  50. void set_rooms(room *rms)
  51. {
  52.     rms[SPORTSHOP].description.assign("sports shop");
  53.     rms[SPORTSHOP].exits_to_room[NORTH] = NONE;
  54.     rms[SPORTSHOP].exits_to_room[EAST] = NONE;
  55.     rms[SPORTSHOP].exits_to_room[SOUTH] = CARPARK;
  56.     rms[SPORTSHOP].exits_to_room[WEST] = NONE;
  57.  
  58.     rms[CASINO].description.assign("bustling casino");
  59.     rms[CASINO].exits_to_room[NORTH] = NONE;
  60.     rms[CASINO].exits_to_room[EAST] = NONE;
  61.     rms[CASINO].exits_to_room[SOUTH] = LOBBY;
  62.     rms[CASINO].exits_to_room[WEST] = NONE;
  63.  
  64.     rms[CARPARK].description.assign("car park");
  65.     rms[CARPARK].exits_to_room[NORTH] = SPORTSHOP;
  66.     rms[CARPARK].exits_to_room[EAST] = LOBBY;
  67.     rms[CARPARK].exits_to_room[SOUTH] = NONE;
  68.     rms[CARPARK].exits_to_room[WEST] = NONE;
  69.  
  70.     rms[LOBBY].description.assign("hotel lobby");
  71.     rms[LOBBY].exits_to_room[NORTH] = CASINO;
  72.     rms[LOBBY].exits_to_room[EAST] = RESTAURANT;
  73.     rms[LOBBY].exits_to_room[SOUTH] = CORRIDOR;
  74.     rms[LOBBY].exits_to_room[WEST] = CARPARK;
  75.  
  76.     rms[RESTAURANT].description.assign("restaurant");
  77.     rms[RESTAURANT].exits_to_room[NORTH] = NONE;
  78.     rms[RESTAURANT].exits_to_room[EAST] = NONE;
  79.     rms[RESTAURANT].exits_to_room[SOUTH] = NONE;
  80.     rms[RESTAURANT].exits_to_room[WEST] = LOBBY;
  81.  
  82.     rms[CORRIDOR].description.assign("corridor");
  83.     rms[CORRIDOR].exits_to_room[NORTH] = LOBBY;
  84.     rms[CORRIDOR].exits_to_room[EAST] = NONE;
  85.     rms[CORRIDOR].exits_to_room[SOUTH] = GARDEN;
  86.     rms[CORRIDOR].exits_to_room[WEST] = NONE;
  87.  
  88.     rms[STOREROOM].description.assign("store room");
  89.     rms[STOREROOM].exits_to_room[NORTH] = NONE;
  90.     rms[STOREROOM].exits_to_room[EAST] = NONE;
  91.     rms[STOREROOM].exits_to_room[SOUTH] = NONE;
  92.     rms[STOREROOM].exits_to_room[WEST] = NONE;
  93.  
  94.     rms[POOL].description.assign("swimming pool area");
  95.     rms[POOL].exits_to_room[NORTH] = NONE;
  96.     rms[POOL].exits_to_room[EAST] = GARDEN;
  97.     rms[POOL].exits_to_room[SOUTH] = PUMPROOM;
  98.     rms[POOL].exits_to_room[WEST] = NONE;
  99.  
  100.     rms[GARDEN].description.assign("tranquil garden");
  101.     rms[GARDEN].exits_to_room[NORTH] = CORRIDOR;
  102.     rms[GARDEN].exits_to_room[EAST] = POND;
  103.     rms[GARDEN].exits_to_room[SOUTH] = NONE;
  104.     rms[GARDEN].exits_to_room[WEST] = POOL;
  105.  
  106.     rms[POND].description.assign("patio with a fish pond");
  107.     rms[POND].exits_to_room[NORTH] = NONE;
  108.     rms[POND].exits_to_room[EAST] = NONE;
  109.     rms[POND].exits_to_room[SOUTH] = NONE;
  110.     rms[POND].exits_to_room[WEST] = GARDEN;
  111.  
  112.     rms[PUMPROOM].description.assign("damp pump room");
  113.     rms[PUMPROOM].exits_to_room[NORTH] = POOL;
  114.     rms[PUMPROOM].exits_to_room[EAST] = NONE;
  115.     rms[PUMPROOM].exits_to_room[SOUTH] = NONE;
  116.     rms[PUMPROOM].exits_to_room[WEST] = NONE;
  117. }
  118.  
  119. // -------------------------------------------------------------------------------------------------
  120.  
  121. void set_directions(word *dir)
  122. {
  123.     dir[NORTH].code = NORTH;
  124.     dir[NORTH].word = "NORTH";
  125.     dir[EAST].code = EAST;
  126.     dir[EAST].word = "EAST";
  127.     dir[SOUTH].code = SOUTH;
  128.     dir[SOUTH].word = "SOUTH";
  129.     dir[WEST].code = WEST;
  130.     dir[WEST].word = "WEST";
  131. }
  132.  
  133. // -------------------------------------------------------------------------------------------------
  134.  
  135. void set_verbs(word *vbs)
  136. {
  137.     // enum en_VERBS {GET, DROP, USE, OPEN, CLOSE, EXAMINE, INVENTORY, LOOK};
  138.     vbs[GET].code = GET;
  139.     vbs[GET].word = "GET";
  140.     vbs[DROP].code = DROP;
  141.     vbs[DROP].word = "DROP";
  142.     vbs[USE].code = USE;
  143.     vbs[USE].word = "USE";
  144.     vbs[OPEN].code = OPEN;
  145.     vbs[OPEN].word = "OPEN";
  146.     vbs[CLOSE].code = CLOSE;
  147.     vbs[CLOSE].word = "CLOSE";
  148.     vbs[EXAMINE].code = EXAMINE;
  149.     vbs[EXAMINE].word = "EXAMINE";
  150.     vbs[INVENTORY].code = INVENTORY;
  151.     vbs[INVENTORY].word = "INVENTORY";
  152.     vbs[LOOK].code = LOOK;
  153.     vbs[LOOK].word = "LOOK";
  154. }
  155.  
  156. // -------------------------------------------------------------------------------------------------
  157.  
  158. // Added code
  159. void set_nouns(noun *nns)
  160. {
  161.     //enum en_NOUNS {STORE_DOOR, MAGNET, METER, ROULETTE, MONEY, FISHROD};
  162.     nns[STORE_DOOR].word = "DOOR";
  163.     nns[STORE_DOOR].code = STORE_DOOR;
  164.     nns[STORE_DOOR].description = "a closed store room door";
  165.     nns[STORE_DOOR].can_carry = false;
  166.     nns[STORE_DOOR].location = CORRIDOR;
  167.     nns[MAGNET].word = "MAGNET";
  168.     nns[MAGNET].code = MAGNET;
  169.     nns[MAGNET].description = "a magnet";
  170.     nns[MAGNET].can_carry = true;
  171.     nns[MAGNET].location = NONE;
  172.     nns[METER].word = "METER";
  173.     nns[METER].code = METER;
  174.     nns[METER].description = "a parking meter";
  175.     nns[METER].can_carry = false;
  176.     nns[METER].location = CARPARK;
  177.     nns[ROULETTE].word = "ROULETTE";
  178.     nns[ROULETTE].code = ROULETTE;
  179.     nns[ROULETTE].description = "a roulette wheel";
  180.     nns[ROULETTE].can_carry = false;
  181.     nns[ROULETTE].location = CASINO;
  182.     nns[MONEY].word = "MONEY";
  183.     nns[MONEY].code = MONEY;
  184.     nns[MONEY].description = "some money";
  185.     nns[MONEY].can_carry = true;
  186.     nns[MONEY].location = NONE;
  187.     nns[FISHROD].word = "ROD";
  188.     nns[FISHROD].code = FISHROD;
  189.     nns[FISHROD].description = "a fishing rod";
  190.     nns[FISHROD].can_carry = false;
  191.     nns[FISHROD].location = SPORTSHOP;
  192. }
  193. // ....
  194.  
  195. // -------------------------------------------------------------------------------------------------
  196.  
  197. void section_command(string Cmd, string &wd1, string &wd2)
  198. {
  199.     string sub_str;
  200.     vector<string> words;
  201.     char search = ' ';
  202.     size_t i, j;
  203.  
  204.     for(i = 0; i < Cmd.size(); i++)
  205.     {
  206.         if(Cmd.at(i) != search)
  207.         {
  208.             sub_str.insert(sub_str.end(), Cmd.at(i));
  209.         }
  210.         if(i == Cmd.size() - 1)
  211.         {
  212.             words.push_back(sub_str);
  213.             sub_str.clear();
  214.         }
  215.         if(Cmd.at(i) == search)
  216.         {
  217.             words.push_back(sub_str);
  218.             sub_str.clear();
  219.         }
  220.     }
  221.  
  222.     for(i = words.size() - 1; i > 0; i--)
  223.     {
  224.         if(words.at(i) == "")
  225.         {
  226.             words.erase(words.begin() + i);
  227.         }
  228.     }
  229.  
  230.     for(i = 0; i < words.size(); i++)
  231.     {
  232.         for(j = 0; j < words.at(i).size(); j++)
  233.         {
  234.             if(islower(words.at(i).at(j)))
  235.             {
  236.                 words.at(i).at(j) = toupper(words.at(i).at(j));
  237.             }
  238.         }
  239.     }
  240.  
  241.     if(words.size() == 0)
  242.     {
  243.         cout << "No command given" << endl;
  244.     }
  245.     if(words.size() == 1)
  246.     {
  247.         wd1 = words.at(0);
  248.     }
  249.     if(words.size() == 2)
  250.     {
  251.         wd1 = words.at(0);
  252.         wd2 = words.at(1);
  253.     }
  254.     if(words.size() > 2)
  255.     {
  256.         cout << "Command too long. Only type one or two words (direction or verb and noun)" << endl;
  257.     }
  258. }
  259.  
  260. // ----------------------------------------------------------------------------------------
  261. void look_around(int loc, room *rms, word *dir, /* Added parameter */ noun *nns)
  262. {
  263.     int i;
  264.     cout << "I am in a " << rms[loc].description << "." << endl;
  265.  
  266.     for(i = 0; i < DIRS; i++)
  267.     {
  268.         if(rms[loc].exits_to_room[i] != NONE)
  269.         {
  270.             cout << "There is an exit " << dir[i].word << " to a " << rms[rms[loc].exits_to_room[i]].description << "." << endl;
  271.         }
  272.     }
  273.  
  274.     // Added code
  275.     // The look command should check which objects (nouns) are in the current room and report them to the player.
  276.     for(i = 0; i < NOUNS; i++)
  277.     {
  278.         if(nns[i].location == loc)
  279.         {
  280.             cout << "I see " << nns[i].description << "." << endl;
  281.         }
  282.     }
  283. }
  284.  
  285. // ----------------------------------------------------------------------------------------
  286.  
  287. bool parser(int &loc, string wd1, string wd2, word *dir, word *vbs, room *rms, /* Added parameter */ noun *nns)
  288. {
  289.     // Added code
  290.     static bool door_state = false; // false is a closed door.
  291.     // ....
  292.  
  293.     int i;
  294.     for(i = 0; i < DIRS; i++)
  295.     {
  296.         if(wd1 == dir[i].word)
  297.         {
  298.             if(rms[loc].exits_to_room[dir[i].code] != NONE)
  299.             {
  300.                 loc = rms[loc].exits_to_room[dir[i].code];
  301.                 cout << "I am now in a " << rms[loc].description << "." << endl;
  302.                 // Added code. A special case for the corridor storeroom door.
  303.                 if(loc == STOREROOM || loc == CORRIDOR)
  304.                 {
  305.                     nns[STORE_DOOR].location = loc;
  306.                 }
  307.                 // ....
  308.                 return true;
  309.             }
  310.             else
  311.             {
  312.                 cout << "No exit that way." << endl;
  313.                 return true;
  314.             }
  315.         }
  316.     }
  317.  
  318.     // Added code
  319.     int NOUN_MATCH = NONE;
  320.     // ....
  321.     int VERB_ACTION = NONE;
  322.  
  323.     for(i = 0; i < VERBS; i++)
  324.     {
  325.         if(wd1 == vbs[i].word)
  326.         {
  327.             VERB_ACTION = vbs[i].code;
  328.             break;
  329.         }
  330.     }
  331.  
  332.     // Added code
  333.     if(wd2 != "")
  334.     {
  335.         for(i = 0; i < NOUNS; i++)
  336.         {
  337.             if(wd2 == nns[i].word)
  338.             {
  339.                 NOUN_MATCH = nns[i].code;
  340.                 break;
  341.             }
  342.         }
  343.     }
  344.  
  345.     if(VERB_ACTION == NONE)
  346.     {
  347.         cout << "No valid command entered." << endl;
  348.         return true;
  349.     }
  350.  
  351.     if(VERB_ACTION == LOOK)
  352.     {
  353.         look_around(loc, rms, dir, /* Added parameter nns */ nns);
  354.         return true;
  355.     }
  356.  
  357.     // Added code
  358.     // Actions for usage of VERB OPEN
  359.     if(VERB_ACTION == OPEN)
  360.     {
  361.         if(NOUN_MATCH == STORE_DOOR)
  362.         {
  363.             if(loc == CORRIDOR || loc == STOREROOM)
  364.             {
  365.                 if(door_state == false)
  366.                 {
  367.                     door_state = true;
  368.                     rms[CORRIDOR].exits_to_room[EAST] = STOREROOM;
  369.                     rms[STOREROOM].exits_to_room[WEST] = CORRIDOR;
  370.                     nns[STORE_DOOR].description.clear();
  371.                     nns[STORE_DOOR].description.assign("an open store room door");
  372.                     cout << "I have opened the door." << endl;
  373.                     return true;
  374.                 }
  375.                 else if(door_state == true)
  376.                 {
  377.                     cout << "The door is already open." << endl;
  378.                     return true;
  379.                 }
  380.             }
  381.             else
  382.             {
  383.                 cout << "There is no door to open here." << endl;
  384.                 return true;
  385.             }
  386.         }
  387.         else
  388.         {
  389.             cout << "Opening that is not possible." << endl;
  390.             return true;
  391.         }
  392.     }
  393.     // ....
  394.  
  395.     return false;
  396. }
  397.  
  398. // ----------------------------------------------------------------------------------------
  399.  
  400.  
  401.  
  402. // ----------------------------------------------------------------------------------------
  403.  
  404. int main()
  405. {
  406.     string command;
  407.     string word_1;
  408.     string word_2;
  409.  
  410.     room rooms[ROOMS];
  411.     set_rooms(rooms);
  412.  
  413.     word directions[DIRS];
  414.     set_directions(directions);
  415.  
  416.     word verbs[VERBS];
  417.     set_verbs(verbs);
  418.  
  419.     // Added code
  420.     noun nouns[NOUNS];
  421.     set_nouns(nouns);
  422.      // ....
  423.  
  424.     int location = CARPARK;
  425.  
  426.     while(word_1 != "QUIT")
  427.     {
  428.         command.clear();
  429.         cout << "What shall I do? ";
  430.         getline(cin, command);
  431.  
  432.         word_1.clear();
  433.         word_2.clear();
  434.  
  435.         section_command(command, word_1, word_2);
  436.  
  437.         if(word_1 != "QUIT")
  438.         {
  439.             parser(location, word_1, word_2, directions, verbs, rooms, /* Added parameter */ nouns);
  440.         }
  441.     }
  442.     return 0;
  443. }
RAW Paste Data
Top