Text adventure

It is a text adventure in which a player has 4 doors to choose from (N/E/S/W). But there are two alignments (i.e., N = monster for alignment 0, and N = exit for alignment 1). How would I allow the player to keep guessing until they get the exit?

I have tested my functions, they work fine. I just can't get it to loop. Also, when I ran the function, the first door input never goes through, I'm not sure why.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    cout << "Pick a door to enter by typing the direction it is in "
    << "(N/E/S/W): ";
    cin >> door;

    while (!(door == 'N' || door == 'E' ||  door == 'S' || door == 'W'));
    {
        cout << "Pick a door to enter by typing the direction it is in "
        << "(N/E/S/W): ";
        cin >> door;
        
        alignment = rand() % 2;
    
        if (alignment == 0)
        {
            if (door == 'N')
            {
                monsterRoom(bananas, oranges, monster);
            }
            if (door == 'S')
            {
                genieRoom(bananas, oranges);
            }
            if (door == 'E')
            {
                pictureRoom();
            }
            if (door == 'W')
            {
                cout << "You found the exit!";
                cout << endl;
            }
        }
    
        if (alignment == 1)
        {
            if (door == 'N')
            {
                cout << "You found the exit!";
                cout << endl;
            }
            if (door == 'S')
            {
                monsterRoom(bananas, oranges, monster);
            }
            if (door == 'E')

                genieRoom(bananas,oranges);
            }
            if (door == 'W')
            {
                pictureRoom();
        }
    }
@cuponooodles

Your main problem lies on this line

while (!(door == 'N' || door == 'E' || door == 'S' || door == 'W'));

Don't use the OR symbols, ||, use AND, &&. Also, remove the semicolon at the end of that line.

Hope this helps you..
Oops, I changed from a do-while loop and forgot the semi-colon.

Hmm, what would I put for the while loop? If the user inputs something not N/E/S/W I'm supposed to ask again until they input a valid one, and it's supposed to keep looping until they find the exit
Last edited on
I saw a couple of problems, 1, you don't have enough ending braces on your last if, 2, I would use else if, it's more readable and here it's important because you can end it with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (alignment == 1)
        {
        alignment1:
            if (door == 'N')
            {
                cout << "You found the exit!";
                cout << endl;
           } else if (door == 'S')
            {
                monsterRoom(bananas, oranges, monster);
           } else if (door == 'E')

                genieRoom(bananas,oranges);
            } else if (door == 'W')
            {
                pictureRoom();
            } else {
            cout<<"That is not a valid response." << endl;
            goto alignment1;
           }
        }

I haven't tested it, but it should work.
Last edited on
Better yet, use a switch statement and do not use goto at all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while(door!='N' /*...*/){
   //...
   switch(alignment){
      case 0:
         switch(door){
            case 'N':
               //...
               break;
            //...
         }
         break;
      default:
         break;
   }
}

If you want your code to be extra readable, consider sectioning off code into functions.

Can you post your updated code?
Also, I realized my code doesn't go back far enough, if you wanna use goto, put it before the original cout.
The thing is... we did not learn switch, goto, or cases. My professor doesn't really "allow" us to use things not taught in class as it would be unfair to those that have no prior knowledge of it.


There are specifications we are to follow which is where most of the points go. So @dunnmifflsys, I wouldn't be able to use that else statement.


Updated my code a bit. It still doesn't register my first door input (a valid one)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
cout << "Pick a door to enter by typing the direction it is in "
    << "(N/E/S/W): ";
    cin >> door;
    while (door == 'N' || door == 'E' ||  door == 'S' || door == 'W')
    {
        cout << "Pick a door to enter by typing the direction it is in "
        << "(N/E/S/W): ";
        cin >> door;
        cout << endl;

        alignment = rand() % 2;
    
        if (alignment == 0)
        {
            if (door == 'N')
            {
                monsterRoom(bananas, oranges, monster);
                
                cout << endl;
                cout << userName << ", you are in a room with 4 doors." << endl;
                cout << "you are carrying " << bananas << " bananas and " << 
                oranges << " oranges." << endl;
                cout << endl;
            }
            else if (door == 'S')
            {
                genieRoom(bananas, oranges);
                
                cout << endl;
                cout << userName << ", you are in a room with 4 doors." << endl;
                cout << "you are carrying " << bananas << " bananas and " << 
                oranges << " oranges." << endl;
                cout << endl;
            }
            else if (door == 'E')
            {
                pictureRoom();
                
                cout << endl;
                cout << userName << ", you are in a room with 4 doors." << endl;
                cout << "you are carrying " << bananas << " bananas and " << 
                oranges << " oranges." << endl;
                cout << endl;
            }
            else if (door == 'W')
            {
                cout << "You found the exit!" << endl;
                cout << "Your score is " << bananas + oranges << " ("
                << bananas << " bananas and " << oranges << " oranges).";
                cout << endl;
                cout << "Bye bye!!!" << endl;
                break;
            }
            else
            {
                cout << "Pick a door to enter by typing the direction it is in "
                << "(N/E/S/W): ";
                cin >> door;
            }
        }
Last edited on
Take a look at the top of your code.

-Prompt user for a choice
-Store choice in door
-enter while loop
-Prompt user for choice


You do nothing with the first choice before asking the user for another. There are two simple fixes to this:
1) Move lines 6-9 to the end of the while block, or
2) Delete lines 1-3

Also, do not use goto unless required by your instructor. goto statements obfuscate your code and allow you to do things the compiler won't catch unless you know what you're doing.
Topic archived. No new replies allowed.