rpg code

Could you please critique my code
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
151
152
//*******************************************
//*                                         *
//*     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";
        state = 3;
    }
    
    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";
}
Last edited on
Please change line 46 to this to make the page less wide:
46
47
48
49
50
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";
String literals concatenate when they have nothing between them, making them easy to split apart.

As for your mini rpg, I'd recommend a 2D array of function pointers and have another function specifically for understanding which command was entered and reacting to it.
could you please explain that more clearer and yes l will change it.
Topic archived. No new replies allowed.