easiest way to make text based game?

This is a discussion on easiest way to make text based game? within the C++ Programming forums, part of the General Programming Boards category; I am very new to programing. Just learned about classes today and I think those might help somwere. All I ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    7

    easiest way to make text based game?

    I am very new to programing. Just learned about classes today and I think those might help somwere.

    All I want to do is write a program that prints out a message, gives three choices and then the user chooses among three choices and then it will print out the message determined by what choice they enter.

    The screen would look like this:
    .................................................. .................................................. .....
    you are being attacked by a giant toad!
    its size and strength are menacing,
    the party is unsure about this
    encounter.

    1) fight
    2) defend
    3) flee

    Press a number for your choice and hit ENTER
    .................................................. .................................................. .....

    All I would want right now is a way to create this and also have a way so if the wrong number was pressed it would ask again for a correct choice.

    I can almost write this program EXCEPT for the part of (looping?) when a valid choice is not entered. And I guess the next step after that would be making it more than a one encounter adventure. lol

    anyone one have any sugestions???

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Have you any code to post so we can have a look?

    You could use a do while loop to print the menu like you have above, and the test part would be to see that the number entered is valid (ie, 1-3) - If its valid, print the text message corresponding to the choice. If its not valid, print the menu again, until the user enters an appropriate choice.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    7
    Code:
    #include <iostream>
    
    class GiantToad
    {
    public:
    void encounter();
    void toadChoice();
    void fightToad();
    void defendToad();
    void fleeToad();
    };
    void GiantToad::encounter()
    {
      std::cout << "\n\nThe party climbs through a thicket of marsh, suddenly you hear\n"
                << "what sounds like a giant belch. It is actually a giant toad.\n"
                << "It attacks!!\n\n";
      std::cout << "What will you do faced with this overgrown reptilian monster?\n\n";
    }
    
    void GiantToad::toadChoice()
    {
    std::cout << "1) Skewer the toad, Giant frog legs are good eating.\n"
              << "2) Defend yourselves, that long tounge looks like trouble\n"
              << "3) Run away, forget the toad!\n";
    }
    
    void GiantToad::fightToad()
    {
      std::cout << "* You try and attack the toad but it swallows one of your party members *\n";
    }
    
    void GiantToad::defendToad()
    {
      std::cout << "* You block the attack, but your confidence in victory is low *\n";
    }
    
    void GiantToad::fleeToad()
    {
      std::cout << "* Runing away is the right choice, you survive the ordeal *\n";
    }
    
    int main()
    {
    GiantToad text;
    text.encounter();
    text.toadChoice();
    int choice;
    std::cout << "\n\nEnter your choice 1-3 and hit ENTER..... ";
    std::cin >> choice;
    while (choice < 1 || choice > 3)
    {
      std::cout << "Please enter a valid selection ";
      std::cin >> choice;
    }
    
    switch (choice)
    {
      case 1:
      text.fightToad();
      break;
    
      case 2:
      text.defendToad();
      break;
    
      case 3:
      text.fleeToad();
    }
    
    return 0;
    }
    Well I tried this and it works? were should I go from here? any tips on an easier way to do this? or a way to add more monster encounters?

  4. #4
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    Your class needs a little work done, it's good to have a class not only list documentation about an enemy, but also keep status of them and make sure they function properly. Classes are made for this type of stuff, having an enemy class as a base and then inheriting from it to make all sorts of smaller enemies using polymorphism and stuff is really good for what it can do (that probably confused you, but don't worry, you seem ambitious enough to be able to figure it out simply, because what I said is really fairly simple (polymorphism is a very big word for a very simple concept really, one that may take a little time to grasp, but simple none the less.))

    Make some methods for attacking, magic, dodging, private data members for health and magic and stuff, ect ect. Before you do this however, I suggest you get into the habit of object oriented programming, which there are many excellent tutorials over (The C++ Programming Language by Bjarne Stroustrup is one of my favorites.)

    As for the infinate loop, try this:

    Code:
    enum choicesEnemy = {RUN=1,ATTK,DFND}; //Run attack and defend
    
    ...
    int choice;
    printf("What type of move shall you take?\n");
    for(;;) {
     scanf("%1d",choice);
     switch(choice) {
      case RUN:
      ... //Do stuff here
      break;
      case ATTK:
      ... //Do stuff here
      break;
      case DFND:
      ... //Do stuff here
      break;
      default:
       printf("Incorrect type of manuver to execute!");
     }
    }
    Of course you'll have to break from the loop later on and such in each statement.
    Last edited by Mad_guy; 08-13-2005 at 02:27 AM.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    7
    ...Yea the stuff about classes I just learned today. It seems like a whole new world of possibilities. The different kind of attacks is a good sugestion, will try and do this.

    The section on object oriented programing design is at the end of this book im studying, so it might be awhile before I stop writing frankenstein code

  6. #6
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    That class is the equivalent of using plain functions, if you only need one instance of data you don't need to put it in a class; you can just use plain data and functions. However a monster is the kind of thing you would put in a class so you’re on the right track. There is likely to be more than one monster so you'll need multiply instances. I would put the enemy encounter logic separate from the actual enemy itself. There is no one correct way to make a game though -just find what works for you.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    oceanrats, to develop a good object oriented design, you need to think more about indivisible objects (which will be implemented in terms of classes), not just using classes to keep functions out of the global namespace (you could, however, use namespaces for this). And while I'm guessing you have not made it to this point, class member functions that do not modify member data are best a) not being part of the class, if possible [hence, you're class is not really an object], or b) being static member functions.

    Mad_guy, out of mild curiosity, why are you using C I/O in a C++ program?

    Cheers
    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.

  8. #8
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    Quote Originally Posted by Zach L.
    Mad_guy, out of mild curiosity, why are you using C I/O in a C++ program?
    It's kind of a personal thang, when I do IO it's more natural for me to do scanf and printf instead of std::cout and std::cin, this 'natural' feeling came after long hard years of drug abuse.

    I don't think syntax like I listed will bother him much though, it should have given him the basic concept of what he might want to try and do, which more or less was my goal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. text based mmo type game.....
    By SONAR in forum Game Programming
    Replies: 0
    Last Post: 12-09-2008, 04:17 AM
  2. OpenGL - 2d text in 3d game
    By mikeb1986 in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2006, 12:24 PM
  3. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-25-2006, 11:14 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21