Thread: Text Game Question

  1. #1

    Unhappy Text Game Question

    Hey peoples..Im taking C++ for the first time and for my class I'm trying to make a text game. I want it so it asks you which direction you want to go and you have to enter n, s, e, or w. The thing is, it wont matter what direction you type because the outcome is random so it wont be the same game twice. I used strings and an array and ran it with the random generator so each time i get a new message. (for easier explanation, we can call them mess1 - mess5). What Im trying to do is have it give you 3 chances. If mess1 - mess4 show up then you have to try again, but if mess5 shows up then you can move on to part 2 of the game. Right now the program just gives me 1 message then ends. How can I get it to recognize mess1 - mess5 to where you get 3 chances and when mess5 appears then it will move on. Im thinking "If" and "Then" statements will be easier to work with but I dont know how to incorporate them into this thing (Ive only had 8 weeks of C++ and its been mostly debugging). If anybody can make sense of what I just posted and can help I would greatly appreciate it! Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    loop (3 times or until user_input == mess5)
      user_input = get_message();
    /* No more chances, did they get it? */
    if(user_input == mess5)
    I believe something like that is what you are asking?
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland:

  3. #3
    Not quite what I was trying for, but thanks alot for posting. The input isnt trying to match one of the messages, its kind of hard to explain. Example is "Your walking through the woods, which way do you want to go." The user types The direction, but a random message appears saying if he chose the correct path or not. The messages arent linked to any word, they are generated randomly. Only 1 of the 5 random messages will let you move on. I have it so they will pop up when you enter your input, but cant figure out how to write the code that will recognize the correct one. Example for this is "You have chosen the correct path" pops up then another question is asked, but if "You chose wrong" pops up you have to enter input again. I know, it sounds very confusing but I think I know what Im trying for...its just very hard to explain it in words. Ive heard about using case structures for it so I might try those. Anyways, thanks again for help and if you can figure out what I just typed here additional help would be great! Thanks again!!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Ah, then perhaps an array will fix your woes.

    PHP Code:
    #include <time.h>
    #include <stdlib.h>
    char Mess[5]; // 5 messages in all.
    Mess[0] = "You win!";
    Mess[1] = "You lose."// .. same for 2, 3, and 4.


    int MessIndex rand()%5// 1 of 5 random numbers.
    loop while(RandomMess != 5)
    MessIndex rand()%5;
    Hope that helps.

    Allegro precompiled Installer for Dev-C++, MSVC, and Borland:

  5. #5
    Registered User
    Join Date
    Mar 2002
    a switch/case could be an easy way to display which messege is chosen. The way you setup your loop will depend on what happens when they get the right message or if they dont get the correct message in 3 tries.

    Depending on your code:

    int numtries = 3;
    int trycount = 0;
    int randnum; //replace randnum with your variable name


    //get input from player

    //generate randnum (0-4?)

    case 0: //where 0 is the correct answer
    //show mess5
    case 1:
    //show mess1
    case 2:
    //show mess2
    case 3:
    //show mess3
    case 4:
    //show mess4
    //show an error message for debugging
    //shouldnt display if randnum is 0-4
    trycount++ //or trycount = trycount + 1;
    } //where 0 is the correct answer
    while((trycount <= numtries) && (randnum != 0) );

    if (randnum == 0)
    //they got it
    //they didnt get it

    /*end psuedocode*/

    if you are using an array for the messages then use it by:
    removing switch/case statement and
    displaying arrayname[randnum];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text based game
    By wipeout4wh in forum C Programming
    Replies: 12
    Last Post: 03-26-2009, 04:39 PM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  3. LaTeX Question: Displaying Text and Images
    By Reisswolf in forum Tech Board
    Replies: 0
    Last Post: 06-30-2006, 08:01 AM
  4. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  5. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM