-
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
-
psuedo:
Code:
loop (3 times or until user_input == mess5)
{
user_input = get_message();
}
/* No more chances, did they get it? */
if(user_input == mess5)
you_win();
else
you_lose();
I believe something like that is what you are asking?
-
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!!
-
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.
start:
srand(time(NULL));
int MessIndex = rand()%5; // 1 of 5 random numbers.
loop while(RandomMess != 5)
{
print(Mess[MessIndex]);
MessIndex = rand()%5;
}
end:
Hope that helps.
-Justin
-
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:
/*Psuedocode!*/
int numtries = 3;
int trycount = 0;
int randnum; //replace randnum with your variable name
do
{
//get input from player
//generate randnum (0-4?)
switch(randnum)
{
case 0: //where 0 is the correct answer
//show mess5
break;
case 1:
//show mess1
break;
case 2:
//show mess2
break;
case 3:
//show mess3
break;
case 4:
//show mess4
break;
default:
//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
else
//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];