Thread: Is it possible to generate random text?

  1. #1
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60

    Is it possible to generate random text?

    I'm making a text based game and I had an idea, but not sure how to accomplish it. It's pretty boring seeing the same thing over and over when you attack an enemy, I have it to where it always says "You miss!" or "You hit the enemy. The hit did X damage." I also have a death message that just says "You have died! For this you lose X exp." I was wandering if there was a way to randomize what text came up. Like, have more than 1 hit messsage, maybe have the "You hit the enemy." one, along with "You strike the enemy with your sword." and others like that.

    I tried making a small sample program, just to see if it would work. But I have no clue on how to do it. So I just did random guessing with the random function. I couldnt get anything to work.

    If anyone could inform me on how to do this. It would be great.(And yes I know, text based games are boring anyways. But im trying to add a little change to it, make it more interesting =) )
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    There are plenty of guides covering how to do a min and max for a random range, then just have a switch/case for each number. At first do it without a default in the switch/case so that you know you are generating numbers within your specified range... then if you want a message that will be the most common message, you set a default value and extend your range a bit.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Variations on this perhaps?
    Code:
    void strike ( string weapon, string monster, int damage ) {
      if ( damage > 100 ) {
        cout << "You hand the " << monster << " a crushing blow!!" << endl;
      } else if ( damage > 50 ) {
        switch ( rand() % 3 ) {
          case 0:
            cout << "You did " << damage << "to the " << monster << endl;
            break;
          case 1:
            cout << "You strike the " << monster << " with your " << weapon << endl;
            break;
          case 2:
            cout << "Your " << weapon " << inflicts " << damage
                << " upon the " << monster << endl;
            break;
        }
      } else if ( damage > 10 ) {
      } else {
        cout << "The " << monster << " laughs at your feeble attack" << endl;
      }
    }
    Or you could give each monster / weapon / armour it's own set of messages which you choose from.

    Killing specific monsters with specific weapons get rather unique messages for example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps something like this:
    Code:
    const char *win[] = {
        "You win!", "Congratulations, you just won!", "You seem to have beaten me . . .", "You won . . . this time"
    };
    
    cout << win[rand() % (sizeof(win)/sizeof(*win) )] << endl;
    * Sorry, Prelude.

    [edit] Too slow. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You mean Salem?

    Consider the language(text) you are using and create lists of verbs, adverbs etc from which you can make random selections following simple grammatical rules to create what appears, superficially anyway, to be distinct sentences. This is really a variation on what's been previously described, but here's an example to give you some ideas
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    namespace
    {
    std::vector<std::string> adjectives;
    std::vector<std::string> adverbs;
    std::vector<std::string> verbs;
    }
    
    void SetAdjectives();
    void SetAdverbs();
    void SetVerbs();
    
    int main()
    {
    SetAdjectives();
    SetAdverbs();
    SetVerbs();
    
    std::string monster="rat";
    
    srand(time(0));
    
    std::cout<<"You "<< 
             adverbs.at(rand()%adverbs.size())<<
             " "<<
             verbs.at(rand()%verbs.size())<<
             " the "<<
             adjectives.at(rand()%adjectives.size())<<
             " "<<monster<<std::endl;
    }
    
    void SetAdjectives()
    {
    //..or load from file
    adjectives.push_back("cursed");
    adjectives.push_back("horrible");
    adjectives.push_back("wicked");
    adjectives.push_back("rancid");
    adjectives.push_back("churlish");
    adjectives.push_back("dirty");
    }
    
    void SetAdverbs()
    {
    //..or load from file
    adverbs.push_back("deftly");
    adverbs.push_back("swiftly");
    adverbs.push_back("efficiently");
    adverbs.push_back("maniacally");
    adverbs.push_back("smugly");
    adverbs.push_back("whimsically");
    }
    
    void SetVerbs()
    {
    //..or load from file
    verbs.push_back("biffed");
    verbs.push_back("chopped");
    verbs.push_back("flensed");
    verbs.push_back("hit");
    verbs.push_back("struck");
    verbs.push_back("whacked");
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    That's overly complex to me... But it's realistic, yeah.

    You might even want to consider using different sentence structures for the messages, here's an example.

    Code:
    std::cout << "The " << badadjective() << " " << monstername << " " << action() << " as you " << goodadjective() << "  " << strike() << " it with your " << weapon << ".\n";
    
    /*
    "The filthy mountain troll flinches in pain as you forcefully strike it with your flaming sowrd of justice."
    */
    Maybe keep 5 or so of these "sentence templates" and alternate between them randomly.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, the edit was for Salem, and the * was for Prelude: http://eternallyconfuzzled.com/articles/rand.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  2. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 05:13 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. random selection of words from a text file
    By archie in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 12:59 AM