c++ console rpg

Pages: 12
umm so this is my code but before you read it this is my list of priorities. First can you explain why no matter what command i input it says its invalid? second critique. Thank you.
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
61
62
63
64
65
66
67
68
69
70
71
72
#include "rpghead.h"

int state = 1;

void help();
void room1();

int main()
{
    while(state != -1)
    {
        switch(state)
        {
            case 1:
                room1();
        }
    }
}

void room1()
{
    string action;
    
    cout<<"\nPlease do all typing in lowercase.\n\nYou wake up with no recollection of who you are. You sit up and look around. It apears to be a small, square room about 20 ft. by 20 ft. by 20 ft. Besides the thin cot you are sleeping on, there is a round, wooden table and a chest at the foot of the cot. There is a door on the west wall. There are no windows. The four walls are made completely out of stone. What do you want to do (type \"help\" for a list of commands)?\n>";
    getline(cin, action);
    
    if(action != "help" || action != "open door" || action != "open chest")
    {
        while(action != "help" || action != "open door" || action != "open chest")
        {
            cout<<"\nThat is not valid. What do you want to do?\n>";
            getline(cin, action);
        }
    }
    
    if(action == "help")
    {
        help();
    }
    
    if(action == "open door")
    {
        cout<<"\nYou open it and find yourself staring at a brick wall. There must be another way out. What do you want to do?\n>";
        getline(cin, action);
        
        if(action != "help" || action != " open chest")
        {
            while(action != "help" || action != " open chest")
            {
                cout<<"\nThat is invalid. What do you want to do?\n>";
                getline(cin, action);
            }
        }
    }
    
    if(action == "help")
    {
        help();
    }
    
    if(action == "open chest")
    {
        cout<<"\nInside the chest you find:\n-50 gold\n-a rusty short sword\n-a backpack\n\nSuddenly, a false door on the east wall is opened. An orc with what appears to be an axe in his hand, steps into the room with an intent to kill you.";
    }
}

void help()
{
    cout<<"\nCommands:\n-open door\n-open chest\n";
    
    return;
}
Your logic is wrong. action != "help" || action != "open door" || action != "open chest" means "if action is not "help" or action is not "open door" or ... do something". For this condition to be false, action should be "help", "open door" and "open chest" at the same time, which, of course, is not possible. Change || to && and it'll be fine.

Your code is not designed for repetition (what happens if you go from room1 to room2 (when you make one) and then back?). It's ok if you intend to destroy room1 after you leave it, but if you don't, you could try this:
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
int state = 0;

int main() {
   while (state != -1)
      switch (state) {
         case 0: introduction(); break; //a separate state so that you can repeat  room1 without repeating the intro.
         case 1: room1(); break;
      }
}

void introduction() {
   cout << "\nPlease do all typing in lowercase.\n\nYou wake up with no recollection of who you are, etc.\n";
   state = 1; //continue to the first room.
}

void room1() {
   string action;
   cout << "It's a small square room, etc.\n";

   static bool door_open = false, monster_appeared = false; //static variables keep their state
   //between several calls to the same function (they act like globals, but only appear in local scope).
   //your room is very complex, so a single state integer is not enough.

   if(monster_appeared) cout << "There's a huge orc threatening to kill you! and a new door in the east";
   else {//I'm assuming that if there's an orc, you don't care about doors and chests..
      cout << "There is a chest...";
      if(!door_open) cout << " There is a door on the west wall.";
   }
   
   getline(cin, action);

   if(!monster_appeared && !door_open && action == "open door") {
      door_open = true;
      cout << "you see a brick wall";//after this, the function ends and (due to it being in a while loop)
      //it starts again, with a slightly different message
   }else if(!monster_appeared && action == "open chest") {
      cout << "inside the chest you find ...";
      monster_appeared = true;
   }else if(action == "run") {
      cout << "you escape through the east door. the orc is too slow to catch you.";
      state = 2;
   }else{
      cout << "I don't know how to do that..";
      //since the set of instructions for this room changes often, an useful help() would be hard to write. 
   }
}
(not tested)
Note that when you have more experience with C++, you'll be able to automate or clean up many tasks (such as keeping a list of available instructions). For now, it's a bit more dirty.
yeah i changed it alot. I do plan to destroy room 1 I haven't started on room 2 yet because ive been restructuring.
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "rpghead.h"

int state = 1;

void help();
void room1();
void roomdes1();
void roomdes2();
void room2();

int main()
{
    cout<<"\nPlease do all typing in lowercase.\n\nYou wake up with no recollection of who you are. You sit up and look around.";
    
    while(state != -1)
    {
        switch(state)
        {
            case 1:
                roomdes1();
                break;
            case 2:
                room1();
                break;
            case 3:
                roomdes2();
                break;
            case 4:
                room2();
                break;
        }
    }
}

void roomdes1()
{
    cout<<"\nThe room apears to be a small, square room about 20 ft. by 20 ft. by 20 ft. Besides the thin cot you are sleeping on, there is a round, wooden table and a chest at the foot of the cot. There is a door on the west wall. There are no windows. The four walls are made completely out of stone.\n";
    
    state = 2;
}

void room1()
{
    string action, door;
    
    cout<<"\nWhat do you want to do (type \"help\" for a list of commands)?\n>";
    getline(cin, action);
    
    if(action != "help" && action != "open door" && action != "open chest" && action != "describe room" && action != "leave room")
    {
        while(action != "help" && action != "open door" && action != "open chest" && action != "describe room")
        {
            cout<<"\nThat is not valid. What do you want to do?\n>";
            getline(cin, action);
        }
    }
    
    if(action == "open door")
    {
        door = "open";
        
        cout<<"\nYou open it and find yourself staring at a brick wall. There must be another way out. What do you want to do?\n>";
        getline(cin, action);
        
        if(action != "help" && action != "open chest" && action != "describe room")
        {
            while(action != "help" && action != "open chest" && action != "describe room")
            {
                cout<<"\nThat is invalid. What do you want to do?\n>";
                getline(cin, action);
            }
        }
    }
    
    if(action == "describe room")
    {
        roomdes1();
    }
    
    if(action == "help")
    {
        help();
    }
    
    if(action == "leave room")
    {
        if(door == "open")
        {
            cout<<"\nAs you leave, you feel compelled to turn around. When you do, you see a strange man with a red cloak and white beard saying something you can't understand. There is a sword at his feet. Before you can go to pick it up, he finishes and the door closes and disappears, leaving behind a stone wall. This seems vaguely familiar.You turn around and leave the room.\n";
        }
        
        state = 3;
    }
    
    if(action == "open chest")
    {
        cout<<"\nInside the chest you find:\n-50 gold\n-a rusty short sword\n-a backpack\nYou put the gold in the backpack and carry it on your back. You put the sword in your hand.\nSuddenly, a false door on the east wall is opened. It must have been triggered by the chest. What do you want to do?\n>";
        getline(cin, action);
        
        if(action != "describe room" && action != "leave room" && action != "help")
        {
            while(action != "describe room" && action != "leave room" && action != "help")
            {
                cout<<"\nThat is not valid. What do you want to do?\n>";
                getline(cin, action);
            }
        }
        
        if(action == "describe room")
        {
            roomdes1();
        }
        
        if(action == "help")
        {
            help();
        }
        
        if(action == "leave room")
        {
            state = 3;
            
            if(door == "open")
            {
                cout<<"\nAs you leave, you feel compelled to turn around. When you do, you see a strange man with a red cloak and white beard saying something you can't understand. There is a sword at his feet. Before you can go to pick it up, he finishes and the door closes and disappears, leaving behind a stone wall. This seems vaguely familiar. You turn around and exit the room.\n";
            }
        }
    }
}

void roomdes2()
{
    cout<<"\nAs soon as you enter the door shuts behind you and seals magically.\n";
}

void help()
{
    cout<<"\nCommands:\n-open door\n-leave room\n-open chest\n-loot [insert name of monster here]\n-equip [item name]\n-describe [object]\n";
}
also I don't really understand bool and can't find a good tutorial on it could you explain it?
boolean is a variable that can only be true or false (1 or 0). All conditions in your ifs are booleans (like 'action == "open"' or 'myint < 5 && myint > 2') so a boolean can be used as a condition.
so what about lines 20, 24, and 27
Consider an example. With any operation, you can store intermediate values in a variable. That is, a = b+c; is the same as int temp = b+c; a = temp;.
Likewise
if(x>5)...; is the same as bool temp = x>5; if(temp)...;.
What I did was assign false to temp. false = (1>2) or etc.
If you still don't understand, play around with it. I don't know what else can be explained about it.
thank you i think I get it that makes a lot more sense now.
i remade it and this is the new version:
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//*******************************************
//*                                         *
//*     Mirrors of ELixia                   *
//*     Start Date: 12/02/11 @ 08:31 pm     *
//*     End Date: ??/??/?? @ ??:?? ??       *
//*     Creator: Aramil Daern of Elixia     *
//*                                         *
//*******************************************

//header file collection
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;


//
int state = 1;

void room1des();
void room1();
void help();

int main()
{
    cout<<"Please do all typing in lowercase.\nYou wake up with no memory of who or where you are. You decide to look around.\n";
    
    while(state != -1)
    {
        switch(state)
        {
                case 1:
                room1des();
                break;
                
                case 2:
                room1();
                break;
        }
    }
}

void room1des()
{
    cout<<"\nThis room is small and cube-like. There are four walls made out of what looks like pure stone with not a single crack. There is a chest in the center of the room. On the west wall there is a simple wooden door. Besides that there is the cot you were sleeping on.\n\n";
    state = 2;
}

void room1()
{
    string action;
    static bool look_under_cot = false, key_found = false, key_used = false, chest_opened = false, door1_opened = false, panel_discovered = false, panel_moved = false, false_door_opened =false;
    
    cout<<"What do you want to do (type \"help\" for a list of commands)?\n>";
    getline (cin, action);
    
    if(action == "help")
    {
        help();
    }
    
    else if(action == "describe room")
    {
        state = 1;
    }
    
    else if(!look_under_cot && action == "look under cot")
    {
        look_under_cot = true;
        cout<<"You look under the cot and see something shiny. You reach for it and realize it's a key. You pick it up.\n";
        key_found = true;
    }
    
    else if(look_under_cot && action == "look under cot")
    {
        cout<<"You look under the cot again but find nothing.\n";
    }
    
    else if(key_found && !key_used && !door1_opened && action == "unlock chest")
    {
        cout<<"You try to unlock the chest but there is something jamming the key hole.\n";
    }
    
    else if(!door1_opened && action == "open door")
    {
        door1_opened = true;
        cout<<"You open the door and find yourself staring at a blank wall. There must be another way out.\n";
    }
    
    else if(door1_opened && action == "close door")
    {
        cout<<"You close the door.\n";
        door1_opened = false;
    }
    
    //This will eventually take you to the boss room
    //All good games need easter eggs
    else if(door1_opened && action == "leave through stone wall")
    {
        cout<<"You see through the illusion of the wall and step through.\n";
        exit(1);
    }
    
    else if(door1_opened && key_found && !key_used && !chest_opened && action == "unlock chest")
    {
        cout<<"You unlock the chest and see the outline of a false panel appear at the bottom of the chest.\n";
        chest_opened = true;
        panel_discovered = true;
        key_used = true;
    }
    
    else if(chest_opened && action == "close chest")
    {
        cout<<"You shut the chest and pull out the key.\n";
        chest_opened = false;
        key_used = false;
    }
    
    else if(chest_opened && panel_discovered && action == "move panel")
    {
        cout<<"You move the panel and discover a button under it.\n";
        panel_moved = true;
    }
    
    else if(panel_moved && chest_opened && action == "push button")
    {
        cout<<"You push the button and see a false door open up on the east wall.\n";
        false_door_opened = true;
    }
    
    //This will eventually lead to to room two description
    else if(false_door_opened && action == "leave room")
    {
        cout<<"You leave the room.\n";
        exit(1);
    }
    
    else
    {
        cout<<"That is not a valid command.\n";
    }
}

void help()
{
    cout<<"Commands:\n\t-look under [object]\n\t-unlock [object]\n\t-open [object]\n\t-close [object]\n\t-leave [exit]\n";
}
Topic archived. No new replies allowed.
Pages: 12