Advertisement
zCool

Where Am I?

May 11th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. std::vector<std::string> SplitString( const std::string& text, char delim );
  9. bool StringContains( const std::string& haystack, const std::string& needle );
  10. void examine(string itm = "");
  11. void smell(string itm2 = "");
  12. void poke(string itm3 = "");
  13. void WaitForEnter();
  14. string items[4] = {"Sword" , "Shield" , "Watch" , "Ground"};
  15. string userCommand = "";
  16. string action = "";
  17. string item = "";
  18.  
  19.  
  20. /* User Will be able to enter these commands
  21.     1. Examine
  22.     2. Examine [itemname]
  23.     3. Smell
  24.     4. Smell [itemname]
  25.     5. Poke [itemname]
  26.     */
  27.  
  28. cout << "Enter a command: ";
  29.  
  30. getline(cin, userCommand);
  31.  
  32. //validate - check if input matches a command
  33. //if not loop until correct input is entered
  34.  
  35. vector<string> lstWords  = SplitString(userCommand , ' ');
  36.  
  37. if (lstWords.size() == 2)
  38. {
  39.     action = lstWords[0];
  40.     item = lstWords[1];
  41. }
  42. else if (lstWords.size() == 1)
  43. {
  44.     action = lstWords[0];
  45. }
  46.  
  47. while( action.compare("Examine") != 0
  48.     && action.compare("Smell") != 0
  49.     && action.compare("Poke") != 0 )
  50. {
  51.     cout << "Enter a command: ";
  52.     getline(cin, userCommand);
  53.     vector<string> lstWords  = SplitString(userCommand , ' ');
  54.  
  55. if (lstWords.size() == 2)
  56. {
  57.     action = lstWords[0];
  58.     item = lstWords[1];
  59. }
  60. else if (lstWords.size() == 1)
  61. {
  62.     action = lstWords[0];
  63. }
  64.  
  65. }
  66. if (action.compare("Examine") == 0)
  67. {
  68.     examine(item);
  69. }
  70.  
  71. if (action.compare("Poke") == 0)
  72. {
  73.     poke(item);
  74. }
  75.  
  76. if (action.compare("Smell") == 0)
  77. {
  78.     smell(item);
  79. }
  80.  
  81.  
  82. cout << "Press Enter To Continue...";
  83. WaitForEnter();
  84.  
  85. }
  86.  
  87. //Moosader Utility Functions
  88. //Probably want to understand how these work
  89. //at some point...
  90. bool StringContains( const std::string& haystack, const std::string& needle )
  91. {
  92.     size_t notFoundLen = std::string::npos;
  93.     return ( haystack.find( needle ) != notFoundLen );
  94.  
  95. }
  96.  
  97. //vector<string> lstWords = SplitString( "examine potato", ' ' );
  98. //for ( int i = 0; i < lstWords.size(); i++ )
  99. //{
  100. //   cout << i << ": " << lstWords[i] << endl;
  101. //}
  102.  
  103.  
  104. std::vector<std::string> SplitString( const std::string& text, char delim )
  105. {
  106.     std::vector<std::string> lstStrings;
  107.  
  108.     unsigned int begin = 0;
  109.     for ( unsigned int i = 0; i < text.size(); i++ )
  110.     {
  111.         if ( text[i] == delim && i == begin )
  112.         {
  113.             begin++;
  114.         }
  115.         else if ( text[i] == delim )
  116.         {
  117.             lstStrings.push_back( text.substr( begin, i - begin ) );
  118.  
  119.             begin = i+1; // Start after this delimiter
  120.         }
  121.  
  122.         if ( i == text.size() - 1 && text[i] != delim )
  123.         {
  124.             lstStrings.push_back( text.substr( begin, text.size() - begin ) );
  125.         }
  126.     }
  127.  
  128.     return lstStrings;
  129. }
  130.  
  131. void examine(string item = "")
  132. {
  133.     if(item.compare("") == 0)
  134.     {
  135.         cout << "You look around and admire the kingdom of HighRule, a totally original kingdom\n";
  136.  
  137.     }
  138.  
  139.     if(item.compare("Sword") == 0)
  140.     {
  141.         cout << "It's a sword, be careful, it's sharp.\n";
  142.     }
  143.  
  144.     if(item.compare("Shield") == 0)
  145.     {
  146.         cout << "It's a shield.\n";
  147.     }
  148.  
  149.     else if(item.compare("Watch") == 0)
  150.     {
  151.         cout << "It's a watch, the time is frozen at 10:10, seems pretty useless.\n";
  152.     }
  153.  
  154.     else if(item.compare("Ground") == 0)
  155.     {
  156.         cout << "It's the ground, it's firm and dependable.\n";
  157.     }
  158.     else
  159.     {
  160.         cout << "Sorry didn't recognize that item.\n";
  161.     }
  162. }
  163.  
  164. void smell(string item = "")
  165. {
  166.     if(item.compare("") == 0)
  167.     {
  168.         cout << "You take a deep breath, ahh the Kingdom of HighRule, it has an unusual but pleasant odor.\n";
  169.  
  170.     }
  171.  
  172.     else if(item.compare("Sword") == 0)
  173.     {
  174.         cout << "You sniff your sword, the sword gives you a puzzled look.\n";
  175.     }
  176.  
  177.     else if(item.compare("Shield") == 0)
  178.     {
  179.         cout << "You sniff your shield, it smells of copper.\n";
  180.     }
  181.  
  182.     else if(item.compare("Watch") == 0)
  183.     {
  184.         cout << "You sniff your watch, it doesn't appear to have a distinct odor.\n";
  185.     }
  186.  
  187.     else if(item.compare("Ground") == 0)
  188.     {
  189.         cout << "You kneel down and sniff the ground, it smells of dirt.\n";
  190.     }
  191.     else
  192.     {
  193.         cout << "Sorry didn't recognize that item.\n";
  194.     }
  195. }
  196.  
  197. void poke(string item = "")
  198. {
  199.     if(item.compare("") == 0)
  200.     {
  201.         cout << "Nothing to Poke.\n";
  202.  
  203.     }
  204.  
  205.     else if(item.compare("Sword") == 0)
  206.     {
  207.         cout << "You poke the sword, ouch, well what did you expect?.\n";
  208.     }
  209.  
  210.     else if(item.compare("Shield") == 0)
  211.     {
  212.         cout << "You poke the shield, unsurprisingly the shield is unresponsive.\n";
  213.     }
  214.  
  215.     else if(item.compare("Watch") == 0)
  216.     {
  217.         cout << "You poke the watch, unsurprisingly the watch is unresponsive.\n";
  218.     }
  219.  
  220.     else if(item.compare("Ground") == 0)
  221.     {
  222.         cout << "You poke the ground, unsurprisingly the ground is unresponsive.\n";
  223.     }
  224.  
  225.     else
  226.     {
  227.         cout << "Sorry didn't recognize that item.\n";
  228.     }
  229. }
  230.  
  231. void WaitForEnter()
  232. {
  233.     while(1)
  234.     {
  235.         if( '\n' == getchar() )
  236.         {
  237.             break;
  238.         }
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement