Thread: Time to test your C++ abilities...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    25

    Time to test your C++ abilities...

    Games like Everquest let you input your own questions, and NPC's answer to certain keywords. Aka...

    You type:
    "Where can I get this legendary sword?"

    He answers by the keyword "legendary sword"..
    "You can find it in the mountains north of here, but I warn you, do not go alone. No one has returned on this dangerous task."


    Can you do that with cin >> at all? And what method would you use?
    My work:
    http://phil.webula.net

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Use string, and the getline function:
    Code:
    #include <iostream>
    #include <string>
    
    int main( )
    {
      std::string str;
      std::getline(cin, str, '\n');
    }
    To math patterns in the string, though, you'll have to use some complex coding, or something like a regular expression library. I believe Boost has one of those ( www.boost.org ).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    25
    There's no ansi way of doing it?
    My work:
    http://phil.webula.net

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Originally posted by Darkman
    There's no ansi way of doing it?
    string and getline are in the standard namespace for a reason.

    gg

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And Boost is largely based on ANSI C++ and provides both POSIX and Windows implementations where not.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Originally posted by Zach L.
    To math patterns in the string, though, you'll have to use some complex coding, or something like a regular expression library. I believe Boost has one of those ( www.boost.org ).
    Thought that was part of your signature for some reason...

  7. #7
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    you could do it with getline and find strings.

    something like this

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    
    string  say, compared= "legendary sword";
    unsigned int comparee = say.find(compared, 0);
    
    cout << "hail" << endl;
    getline(cin,say);
    // that way the reply will automaticaly become the say
    
    comparee = say.find(compared, 0);//establish what its looking for    
         if(comparee != string::npos ) //make it look for the key word
          {
           cout << "You can find it in the mountains north of here ";
           cout << "but I warn you, do not go alone. ";
           cout << "No one has returned on this dangerous task." << endl; 
           //i seperated it like this to keep it easyer to look  
           //at the code w/o mesing with the H-scroll
          }
    
    cin.get();
    return 0;
    }
    yeah i know there is a little bit of redundance with the comparee thing, but i couldnt find a way around it.
    Last edited by Aalmaron; 01-08-2004 at 10:24 AM.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I would tokenize the string, and search word by word, and use the best match. for example, at a shop:
    Code:
    user: I want to buy a legenday sword
    merchant: you have to go to the mountains
    words found: buy legendary sword
    
    user: I want to sell this stupid legendary sword
    merchant: I will buy it for 789456786189 platinum
    words found: sell legendary sword
    of course, that won't work when somebody types like this:
    i dwaont to biy lgendarie sord
    if you really wanted to get deeply into it, you can do it in stages... liek this:
    Code:
    user: i want to buy a sword
    merchant: what kind of sword are you looking for?
    words found: buy sword
    
    user: I want to buy a sword with short range
    merchant: I have a short sword for 1564 silver
    words found: buy sword short
    it would probably be easier to set something up like this:
    Code:
    user: /merchant buy short sword
    merchant: that short sword will cost you 45619.  buy?
    user: /merchant no
    have the user whisper it to the merchant or soemthing... or just say it while in range of the merchant...
    Last edited by major_small; 01-08-2004 at 10:47 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    thats if you are going to go to a merchant, and are going to talk to them, i prefer either a list of what he has, or a user interface with a the items and words.

    Code:
    user: /buy
    merchant: i have:
    1) a rusty short sword - 1 silver
    2) a jaged short sword - 5 silver
    3) an iron short sword - 2 gold
    4) a steal short sword - 4 gold
    5) a fine alloy short sword - 5 platinum
    user: /buy 3
    merchant: you dont have 2 gold.
    user /buy 2
    merchant: thank you for your buisness
    you have 5 less silver
    you have 6 silver less
    but thats only for a text based game, UI's are better imo.

  10. #10
    Registered User
    Join Date
    Jan 2004
    Posts
    25
    Thanks, Aal. I tried your code and it worked tremendously. Thanks everyone. By your opinion, do you think this will be used in later future games? I noticed they did with a few RPG's. It seemed more real to me for some reason. Maybe the fact that I can roleplay what my character says instead of clicking "Background," "Latest Rumors," etc.
    My work:
    http://phil.webula.net

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Like I was trying to say earlier, the main thing that holds that back is that not everybody speaks (types) full english the way you want, for example: a computer doesn't know that "i want to purchase" means "Can i buy"
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    but a programer does, and can put "purchase", "buy", and even a few miss spellings for "purchase"

  13. #13
    Registered User
    Join Date
    Nov 2003
    Posts
    168
    Could it be possible to divide all the words of the string and then check them one by one with a for loop?

    (I learned the very basics of programming with a game called Graal, and you could use tokenize to take parts of strings)
    -Felix
    Rots Soft
    If the facts don't fit the theory, change the facts.
    Albert Einstein (1879 - 1955)

  14. #14
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Yes

    What you know about a phrase is that each word is seperated by a space.

    So you take this fact and use it too your advantage.
    you could use a vector or an array or another contairer to hold it. i gona use an array but i'd suggest using a vector as it solves the problem of dynmic memory allocation for you, but you could use an array and do some dynamic stuff.
    Code:
    ...
    string userIn;
    char wordArray[6][5]; // i know my inpputed phrase has 6 words and longest word is 5 letters
    int x, y, z;
    x= 0; y = 0; z = 1;
    
    // user says this "I want to buy a sword."
    cin >> userIn;
    char currentLetter = userIn[0];
    while (currentLetter) {
     if (currentLetter != "." && currentLetter !=" ") {
      wordArray[x][y] = currentLetter;
      y++;
     }
    if (currentLetter == " ") { // your moving onto another word.
      x++;
      y = 0;
      }
    currentLetter  = userIn[z];
    z++;
    }
    Alright so that would break it up into individual words, After that you jsut have to check it for what you need.

    NOte this is untested code and is realy ment jsu tto give you an idea of how you might be able to solve your problem.

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    161
    Search the web for "natural language parsers." Many MUDs have very complex input parsers nowadays that let you do things like:

    > get the first green sword in the 2nd wood crate
    > sell it to bob

    Everquest's system is actually very, very simple. It could be easily copied with simple code like the following:

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    
    class Responder
    {
    public:
      std::string sayto(std::string msg)
      {
        std::map<std::string, std::string>::iterator itr = responses.begin();
        for( ; itr != responses.end(); ++itr)
        {
          if(msg.find(itr->first) != std::string::npos)
            return itr->second;
        }
    
        return "I do not understand.";
      }
    
      void add_response(std::string key, std::string resp)
      {
        responses[key] = resp;
      }
    
    private:
      std::map<std::string, std::string> responses;
    };
    
    int main()
    {
      Responder r;
    
      r.add_response("hail", "Hello traveller! Hrm, now where did I put those [rusty swords]?");
      r.add_response("rusty sword", "Bring me rusty swords and I'll give you a [prize]!");
      r.add_response("prize", "Shh! I can't tell you abou that!");
    
      std::cout << "Type 'hail' to start and 'quit' to quit." << std::endl;
    
      std::string s;
    
      do
      {
        std::cout << "Say what? " << std::flush;
        std::getline(std::cin, s);
    
        if(s != "quit")
        {
          std::string response = r.sayto(s);
          std::cout << "Bob says, '" << response << "'" << std::endl;
        }
    
      } while(s != "quit");
    
      return 0;
    }
    And a sample run:

    $ ./a.out
    Type 'hail' to start and 'quit' to quit.
    Say what? hail
    Bob says, 'Hello traveller! Hrm, now where did I put those [rusty swords]?'
    Say what? What rusty swords are you talking about?
    Bob says, 'Bring me rusty swords and I'll give you a [prize]!'
    Say what? Wow?! What type of prize?
    Bob says, 'Shh! I can't tell you abou that!'
    Say what? quit
    $


    Hope that helps.

    -tf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  2. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. MSVC Template Constructor/Assignment Errors
    By LuckY in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:57 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM