Texted Room Game Help

I have to write a code that allows "x" to move between 4 rooms to find a treasure. I think I have the code done except that I initilize the variable room = 1 (Line 114) to allow the program to work correctly. After the first function the result gets overwritten again by the 1 and I don't know how to fix this. Yes, this is a homework assignment. I am just asking for help on this one problem. I have looked everywhere and can't figure it out. The only thing that works is the ones when room = 1 so the function returns the return and then it is overwritten by the room = 1 statement (Line 114)(I think). I don't know how to solve this problem.

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
#include <iostream>
using namespace std;


 float render(int x, int room)
{
    if ( room >=1 && room <= 4)
    {
        if(x == 4 && room == 2 )
        {
            room = 1;

            cout << "---------" << endl;
            cout << "| X |   |" << endl;
            cout << "---------" << endl;
            cout << "|   | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 8 && room == 3)
        {
            room = 1;

            cout << "---------" << endl;
            cout << "| X |   |" << endl;
            cout << "---------" << endl;
            cout << "|   | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 6 && room == 1)
        {
            room = 2;

            cout << "---------" << endl;
            cout << "|   | X |" << endl;
            cout << "---------" << endl;
            cout << "|   | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 8 && room == 4)
        {
            room = 2;

            cout << "---------" << endl;
            cout << "|   | X |" << endl;
            cout << "---------" << endl;
            cout << "|   | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 2 && room == 1)
        {
            room = 3;

            cout << "---------" << endl;
            cout << "|   |   |" << endl;
            cout << "---------" << endl;
            cout << "| X | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 4 && room == 4)
        {
            room = 3;

            cout << "---------" << endl;
            cout << "|   |   |" << endl;
            cout << "---------" << endl;
            cout << "| X | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 6 && room == 3)
        {
            room = 4;

            cout << "---------" << endl;
            cout << "|   |   |" << endl;
            cout << "---------" << endl;
            cout << "|   | X |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
            cout << endl;
            cout << "You have found the treasure!  You win!" << endl;
        }
        else if (x == 2 && room == 2)
        {
            room = 4;

            cout << "---------" << endl;
            cout << "|   |   |" << endl;
            cout << "---------" << endl;
            cout << "|   | X |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
            cout<< endl;
            cout << "You have found the treasure!  You win!" << endl;
        }

    }
    else
    {
        cout << "Cannot draw map. Invalid room number: " << room << endl;
    }
    return(room);
}


int main()
{
    int x;
    int room = 1;

    do
    {
        cout << "Which direction would you like to move?" << endl;
        cout << "[8] North" << endl;
        cout << "[4] West" << endl;
        cout << "[2] South" << endl;
        cout << "[6] East" << endl;
        cout << "[0] Exit" << endl;
        cin >> x;

        render(x, room);

    }while(x !=0);
}
Last edited on
Because you need to pass room by reference, not by value. Simply, you need to tell the compiler that you want to all the variable, room, to be modified by the function, and also modify the local variable. This is quite simple in your program. In your render function definition, change this line:
float render(int x, int room)

To:
float render(int x, int &room)

Also, why are you defining render as a float function? You're not returning any values from the function. Would void not work?
Thank you so much. I have been working on this for over a week. I just started programming and only started the class 3 weeks ago. I tried to use void but it would not allow me to since I was returning a reference. I tried float and it worked so I left it. Thanks again.
Remove your return statement from the function since you're using room as a reference. That will allow you to use void render.

Another alternative is to leave your code exactly how it was but change float render to int render, since room is an int, and assign room to render(x, room) like so
room = render(x, room);

That would replace your regular call to render in main. The function returns the value of room, and assigns it to the variable room.
Thanks for the help. You have no idea how much I appreciate it. I had another problem with my code but with your last reply I was able to solve it. Thanks again.
I have revised the code but I had to add some features. I thought I had it solved until I had to add the new features and then it has me stumped again. When you in a certain room and try to move to another room that as you cannot move to it outputs a code but when I added that code I can no longer enter room 4 where the treasure is.

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
#include <iostream>
using namespace std;


void render(int &x, int &room)
{
            if (room >=1 && room <= 4)
        {
            if( x == 4 || x == 8 )
            {
                if(room == 2 || room == 3)
                {
                    room = 1;

                    cout << "---------" << endl;
                    cout << "| X |   |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
                else
                {

                    cout << "Cannot move in this direction from this room." << endl;
                    cout << "---------" << endl;
                    cout << "| X |   |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }

            }
            else if (x == 6 || x == 8)
            {
                if(room == 1 || room == 4)
                {
                    room = 2;

                    cout << "---------" << endl;
                    cout << "|   | X |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
                else
                {
                    cout << "Cannot move in this direction from this room." << endl;
                    cout << "---------" << endl;
                    cout << "|   | X |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }

            }
            else if (x == 2 || x == 4)
            {
                if(room == 1 || room == 4)
                {
                    room = 3;

                    cout << "---------" << endl;
                    cout << "|   |   |" << endl;
                    cout << "---------" << endl;
                    cout << "| X | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
                else
                {
                    cout << "Cannot move in this direction from this room." << endl;
                    cout << "---------" << endl;
                    cout << "|   |   |" << endl;
                    cout << "---------" << endl;
                    cout << "| X | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }

            }
            else if (x == 6 || x == 2)
            {
                if(room == 2 || room == 3)
                {
                    room = 4;

                    cout << "---------" << endl;
                    cout << "|   |   |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | X |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                    cout << "You have found the treasure!  You win!" << endl;

                    x=0;
                }

            }
        }
}


int main()
{
    int x;
    int room = 1;

    cout << "== Super Text Adventure ==" << endl;
    cout << "---------" << endl;
    cout << "| X |   |" << endl;
    cout << "---------" << endl;
    cout << "|   | T |" << endl;
    cout << "---------" << endl;
    cout << "You are in room 1." << endl;
    cout << endl;

    do
    {
        cout << "Which direction would you like to move?" << endl;
        cout << "[8] North" << endl;
        cout << "[4] West" << endl;
        cout << "[2] South" << endl;
        cout << "[6] East" << endl;
        cout << "[0] Exit" << endl;
        cout << "<===========>" << endl;
        cin >> x;

        render(x, room);


    }while(x !=0);
}
I'll walk you through step by step at what is wrong with it. Your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
            // Room must be 1-4
            if (room >=1 && room <= 4)
        {
            // User wants to go either to the west or north
            if( x == 4 || x == 8 )
            {
                // User is in room 2 or 3 and wants to go either west or north
                if(room == 2 || room == 3)
                {
                    // If the user is in room 2 and goes north
                    // How do they end back up in room 1?
                    room = 1;

                    cout << "---------" << endl;
                    cout << "| X |   |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }


Read my comments to understand your logic better.
I understand what your saying. I also have this code that everything works except that after it says "you cannot move in that direction" it needs to cout the last room they were in. Thats when I started getting messed up.

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
#include <iostream>
using namespace std;


void render(int &x, int &room)
{
    if ( room >=1 && room <= 4)
    {
        if(x == 4 && room == 2 )
        {
            room = 1;

            cout << "---------" << endl;
            cout << "| X |   |" << endl;
            cout << "---------" << endl;
            cout << "|   | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 8 && room == 3)
        {
            room = 1;

            cout << "---------" << endl;
            cout << "| X |   |" << endl;
            cout << "---------" << endl;
            cout << "|   | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 6 && room == 1)
        {
            room = 2;

            cout << "---------" << endl;
            cout << "|   | X |" << endl;
            cout << "---------" << endl;
            cout << "|   | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 8 && room == 4)
        {
            room = 2;

            cout << "---------" << endl;
            cout << "|   | X |" << endl;
            cout << "---------" << endl;
            cout << "|   | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 2 && room == 1)
        {
            room = 3;

            cout << "---------" << endl;
            cout << "|   |   |" << endl;
            cout << "---------" << endl;
            cout << "| X | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 4 && room == 4)
        {
            room = 3;

            cout << "---------" << endl;
            cout << "|   |   |" << endl;
            cout << "---------" << endl;
            cout << "| X | T |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
        }
        else if (x == 6 && room == 3)
        {
            room = 4;

            cout << "---------" << endl;
            cout << "|   |   |" << endl;
            cout << "---------" << endl;
            cout << "|   | X |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
            cout << endl;
            cout << "You have found the treasure!  You win!" << endl;
        }
        else if (x == 2 && room == 2)
        {
            room = 4;

            cout << "---------" << endl;
            cout << "|   |   |" << endl;
            cout << "---------" << endl;
            cout << "|   | X |" << endl;
            cout << "---------" << endl;
            cout << "You are in room " << room << endl;
            cout<< endl;
            cout << "You have found the treasure!  You win!" << endl;
        }
        else
        {
            cout << "Cannot move in that direction" << endl;
        }

    }
}


int main()
{
    int x;
    int room = 1;

    do
    {
        cout << "Which direction would you like to move?" << endl;
        cout << "[8] North" << endl;
        cout << "[4] West" << endl;
        cout << "[2] South" << endl;
        cout << "[6] East" << endl;
        cout << "[0] Exit" << endl;
        cin >> x;

        render(x, room);

    }while(x !=0);
}


Is it better to not try and combine everything to shorten the code?
I would suggest creating a function that displays the room, it will help shorten up your code for render.

1
2
3
4
void DisplayRooms(int room, int treasure) {
   // Display the room the user is in
   // If the room and treasure are the same number, display that the user found the treasure
}


This saves from having to display each room multiple times.
Thank you so much for your help. After about a week an half of struggling with this you have finally helped me solve this problem and I submitted it and it passed all the tests. Thanks again. Here is the finally code that I used.

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
using namespace std;

void render(int &x, int &room)
{
            if (room >=1 && room <= 4)
        {
            if(x == 8)
            {
                if(room == 3)
                {
                    room = 1;

                    cout << "---------" << endl;
                    cout << "| X |   |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
                else if(room == 1)
                {
                    cout << "Cannot move in this direction from this room." << endl;
                    cout << "---------" << endl;
                    cout << "| X |   |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
                else
                {
                    cout << "Cannot move in this direction from this room." << endl;
                    cout << "---------" << endl;
                    cout << "|   | X |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }

            }
            else if (x == 4)
            {
                if(room == 2)
                {
                     room = 1;

                    cout << "---------" << endl;
                    cout << "| X |   |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
                else if(room == 1)
                {
                    cout << "Cannot move in this direction from this room." << endl;
                    cout << "---------" << endl;
                    cout << "| X |   |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
                else
                {
                    cout << "Cannot move in this direction from this room." << endl;
                    cout << "---------" << endl;
                    cout << "|   |   |" << endl;
                    cout << "---------" << endl;
                    cout << "| X | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
            }
            else if (x == 6)
            {
                if(room == 1)
                {
                    room = 2;

                    cout << "---------" << endl;
                    cout << "|   | X |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
                else if(room == 3)
                {
                    return;
                }
                else
                {
                    cout << "Cannot move in this direction from this room." << endl;
                    cout << "---------" << endl;
                    cout << "|   | X |" << endl;
                    cout << "---------" << endl;
                    cout << "|   | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }

            }
            else if (x == 2)
            {
                if(room == 1)
                {
                    room = 3;

                    cout << "---------" << endl;
                    cout << "|   |   |" << endl;
                    cout << "---------" << endl;
                    cout << "| X | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }
                else if(room == 2)
                {
                     return;
                }
                else
                {
                    cout << "Cannot move in this direction from this room." << endl;
                    cout << "---------" << endl;
                    cout << "|   |   |" << endl;
                    cout << "---------" << endl;
                    cout << "| X | T |" << endl;
                    cout << "---------" << endl;
                    cout << "You are in room " << room << "." << endl;
                    cout << endl;
                }

            }

        }
}


void treasure(int &x, int &room)
{
    room = 4;
    cout << "---------" << endl;
    cout << "|   |   |" << endl;
    cout << "---------" << endl;
    cout << "|   | X |" << endl;
    cout << "---------" << endl;
    cout << "You are in room " << room << "." << endl;
    cout << endl;
    cout << "You have found the treasure!  You win!" << endl;

    x=0;
}


int main()
{
    int x;
    int room = 1;

    cout << "== Super Text Adventure ==" << endl;
    cout << "---------" << endl;
    cout << "| X |   |" << endl;
    cout << "---------" << endl;
    cout << "|   | T |" << endl;
    cout << "---------" << endl;
    cout << "You are in room 1." << endl;
    cout << endl;

    do
    {
        cout << "Which direction would you like to move?" << endl;
        cout << "[8] North" << endl;
        cout << "[4] West" << endl;
        cout << "[2] South" << endl;
        cout << "[6] East" << endl;
        cout << "[0] Exit" << endl;
        cout << "<===========>" << endl;
        cin >> x;


        render(x, room);

        if( x == 2 )
        {
            if(room == 2)
            {
                treasure(x, room);
            }
        }
        else if(x == 6)
        {
            if(room == 3)
            {
                treasure(x, room);
            }
        }

    }while(x !=0);
}
Topic archived. No new replies allowed.