Key and door

Hello! im new in this of coding.
im trying to make a key and a door.
the thing here is that the door opens even if you don't have the key

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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <string>
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <thread>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

char map [20][20] = {"#########", //if you want to create a map:
                     "#       #", // # Wall, & Medkit,
                     "# ### #*#", // @ Player, * Enemy
                     "# #&  #*#", // = Exit, - Door (door only opens with a key)
                     "# #   #*#", // ~ key, H Bigger enemy,
                     "# #&  #H#", 
                     "# #   #~#",
                     "# #&  ###",
                     "#@#    &#",
                     "#### ####",
				  	 "#&     *#",
					 "#  *    #",
					 "# *    *#",
					 "#&      #",
					 "#&   *  =",
					 "####-####",
					 "#   H   #",
					 "#& & & &#",
					 "#########",};

int gmspd = 100;
int level = 1;
bool stopgame = false;
int hp = 50;
int hpmax = 50;
bool key = false;

int main (int argc, char** argv)
{
while(stopgame == false  && level == 1)
{
	system("cls");
	for (int y = 0; y < 20; y++)
	{
		cout << map[y] << endl;
	}
	cout << "HP: " << hp << " / " << hpmax << endl;
	for (int y = 0; y < 20; y++)
	{
		for (int x = 0; x < 20; x++)
		{
		switch(map[y][x])
		{
			case '#':
				{
					map[y][x] = 219;
				}break;
			case 'a':
				{
					map[y][x] = 21;
				}break;
				
			case '@':
				{
										
					
					if (GetAsyncKeyState(VK_UP) != 0)
					{
						int y2 = (y-1);
						
						switch(map[y2][x])
						{
							case ' ':
								{
									map[y][x] = ' ';
									y -= 1;
									map[y2][x] = '@';
								}break;
							case '=':
							    {
								level = 2;
							    }break;
							case '*':
								{
									hp -= 5;
									map[y][x] = ' ';
									y -= 1;
									map[y2][x] = '@';
								}break;
								case 'H':
								{
									hp -= 10;
									map[y][x] = ' ';
									y -= 1;
									map[y2][x] = '@';
								}break;
							case '&':
								{
									hp += 5;
									map[y][x] = ' ';
									y -= 1;
									map[y2][x] = '@';
								}break;
							case '~':
								{
									key = true;
									map[y][x] = ' ';
									y -= 1;
									map[y2][x] = '@';
								}break;
							
								
						}
					}
					if (GetAsyncKeyState(VK_DOWN) != 0)
					{
						int y2 = (y + 1);
						
						switch(map[y2][x])
						{
							case ' ':
								{
									map[y][x] = ' ';
									y += 1;
									map[y2][x] = '@';
									
								}break;
							case '=':
							    {
								level = 2;
							    }
							case '*':
								{
									hp -= 5;
									map[y][x] = ' ';
									y += 1;
									map[y2][x] = '@';
								}break;
								case 'H':
								{
									hp -= 20;
									map[y][x] = ' ';
									y += 1;
									map[y2][x] = '@';
								}break;
						   case '&':
							    {
									hp += 5;
									map[y][x] = ' ';
									y += 1;
									map[y2][x] = '@';
								}break;
								case '~':
								{
									key = true;
									map[y][x] = ' ';
									y += 1;
									map[y2][x] = '@';
								}break;
								case '-':
								if (key = true)
								{
									map[y][x] = ' ';
									y += 1;
									map[y2][x] = '@';
								}
								else if (key = false)
								
								
							
						}
						
					}
					if (GetAsyncKeyState(VK_RIGHT) != 0)
					{
						int x2 = (x + 1);
						
						switch(map[y][x2])
						{
							case ' ':
							{
								map[y][x] = ' ';
									x += 1;
									map[y][x2] = '@';	
							}break;
								case '=':
							    {
								level = 2;
							    }
							    case '*':
								{
									hp -= 5;
									map[y][x] = ' ';
									x += 1;
									map[y][x2] = '@';
								}break;
								case 'H':
								{
									hp -= 10;
									map[y][x] = ' ';
									x += 1;
									map[y][x2] = '@';
								}break;
								case '&':
							    {
									hp += 5;
									map[y][x] = ' ';
									x += 1;
									map[y][x2] = '@';
								}break;
								case '~':
								{
									key = true;
									map[y][x] = ' ';
									x += 1;
									map[y][x2] = '@';
								}break;
						}
					}
				    if (GetAsyncKeyState(VK_LEFT) != 0)
				    {
				    	int x2 = (x - 1);
				    	
				    	switch(map [y][x2])
						{
							case ' ':
								{
									map[y][x] = ' ';
									x -= 1;
									map[y][x2] = '@';
								}break;
									case '=':
							    {
								level = 2;
							    }
							    case '*':
								{
									hp -= 5;
									map[y][x] = ' ';
									x -= 1;
									map[y][x2] = '@';
									
								}break;
								case 'H':
								{
									hp -= 10;
									map[y][x] = ' ';
									x -= 1;
									map[y][x2] = '@';
									
								}break;
								case '&':
							    {
									hp += 5;
									map[y][x] = ' ';
									x -= 1;
									map[y][x2] = '@';
								}break;
								case '~':
								{
									key = true;
									map[y][x] = ' ';
									x -= 1;
									map[y][x2] = '@';
								}break;
						}
					}
				
				}break;
		 }
		}
	}
	}	
	
	while (stopgame == false  && level == 2)
	{
		system("cls");
		cout << "\n-YOU WIN-\n Made by kevin 'Noclip' L. from canada and \n Roberto 'B4DC0D3' de gennaro from argentina\n\n";
		system("pause");
		
	}




return 0;
}
  
Hello B4DC0D3,

Going over your program I had some compile problems.

Before I continue you did not mention what IDE/compiler you are using, so I do not know the correct way to do this. In my MSVS 2017 there is a menu option called "Tools" in the drop down menu the last choice is "Options". After choosing "Options" You will need to find something that deals wit the "Text Editor" then something like "C/C++" -> "Tabs". Whether you have "Tab Size" or "Indent Size" or both make them 4 and choose to use spaces for the tab key. This will take the exaggeration out of the indenting that happens with the tab key being defined as a tab character.

To start with:
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
#include <string>
#include <iostream>
#include <windows.h>
#include <cstdlib>
//#include <ctime>     // <--- Not used.
#include <chrono>
#include <thread>
//#include <dos.h>     // <--- Not needed and is not used in the program.
//#include <stdlib.h>  // <--- Should be <cstdlib> and redundant.
//#include <stdio.h>   // <--- Should be covered and or included in <iostream>. Also should be <cstdio>

using namespace std;

char map[20][20] =  // <--- (=) not needed with the uniform initializer {}. Also would be better defined in "main".
{ 
    "#########", //if you want to create a map:
    "#       #", // # Wall, & Medkit,
    "# ### #*#", // @ Player, * Enemy
    "# #&  #*#", // = Exit, - Door (door only opens with a key)
    "# #   #*#", // ~ key, H Bigger enemy,
    "# #&  #H#",
    "# #   #~#",
    "# #&  ###",
    "#@#    &#",
    "#### ####",
    "#&     *#",
    "#  *    #",
    "# *    *#",
    "#&      #",
    "#&   *  =",
    "####-####",
    "#   H   #",
    "#& & & &#",
    "#########"  // <--- This (,) not needed.
};

constexpr int gameSpeed = 100;
//int level = 1;
//bool stopgame = false;
//int hp = 50;
//int hpmax = 50;
//bool key = false;

int main(int argc, char** argv)
{
    constexpr int MAXSIZE{ 20 };

    int level = 1;
    bool stopgame = false;
    int hp = 50;
    int hpmax = 50;
    bool key = false;

    while (stopgame == false && level == 1)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(gameSpeed));

        system("cls");

As a global variable I changed "gameSpeed" to a constant because it should not be changed by the program. If you need to remove the "constexpr" and move into "main". The other variables are better defined in "main" where they are used.

A good practice is to avoid using global variables. As you programs grow and you start using functions it will be more difficult to track does where something went wrong and changed a global variable when you did not want it changed. The compiler does not warn or cause an error when you change a global variable.

Line 56 makes use of the header files <chrono> and <thread> along with the variable "gameSpeed". It helps make the display less jumpy with each clear screen you call each time through the loop.

Your for loops would work better as for (int y = 0; y < MAXSIZE; y++). This works with line 46 in the above code. This way you only have 1 place to change and every MAXSIZE will reflect this change. It saves time going through all of you code to make changes ans you are likely to miss 1 or 2 somewhere.

Another option is to define "MAXROWS" and "MAXCOLS" to work with your array better. As it is you have more rows and columns than you need based on what you are using.

You case statements can be written as:
1
2
3
4
5
case '#':
    map[y][x] = 219;
    break;

case 'a':

The {}s are not needed for a case unless you are defining a variable, but the drawback to that is when the closing } is reached the variable is destroyed an not usable outside of the block. The blank line is optional.

The other reason I have used this is the 219. 219 is larger than a "signed char" can hold, so you may not be getting the character you want when it cuts the 219 down to something a "signed char" can hold. I did fix the problem be defining the array as unsigned char map[20][20].

In the if statement for VK_DOWN you have:
1
2
3
4
5
6
7
8
9
10
11
case '=':
{
    level = 2;
}
case '*':
{
    hp -= 5;
    map[y][x] = ' ';
    y += 1;
    map[y2][x] = '@';
}break;

First off yo do not need the {}s. Second when "case '=':" is chosen it falls through to "case '*':" before it finds a break to leave the switch. I do not think that is what you want.

In the "case '=':" you set level, but you have no idea what the value of "key" is, so all it does is exit when it gets back to the while loop to find that "level" no longer == 1. And speaking of that the value of "stopgame" never changes in your program. So does it really have any use?

It looks like you will need to check and change all the "case '=':"s to only work if "key" is true.

Your last while loop at the end of "main" is an endless loop because neither "stopgame" or "level" have their values changed while inside the loop, so there is no way out. I did add this to the loop at the end stopgame = true;. This worked and allowed the program to end. The end result here is that the while loop is not needed. I would suggest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    }  //  End of while loop

    system("cls");

    cout <<
        "\n\n     -YOU WIN-"
        "\n     Made by kevin 'Noclip' L. from canada and"
        "\n      Roberto 'B4DC0D3' de gennaro from argentina\n\n";

    std::this_thread::sleep_for(std::chrono::seconds(5));


    return 0;  // <--- Not required, but makes a good break point.
}

The 5 is the number of whole seconds to pause, sleep or wait.

Just a thought. Your last screen would look like:


     -YOU WIN-
     Made by kevin 'Noclip' L. from canada and
      Roberto 'B4DC0D3' de gennaro from argentina






Keep or not. Change anyway you like.

Andy
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
#include <iostream>

using namespace std;

int main()
{
    const int COL = 9;
    const int ROW = 19;
    
    const char map[][ROW]
    {
        "#########",
        "#       #",
        "# ### #*#",
        "# #&  #*#",
        "# #   #*#",
        "# #&  #H#",
        "# #   #~#",
        "# #&  ###",
        "#@#    &#",
        "#### ####",
        "#&     *#",
        "#  *    #",
        "# *    *#",
        "#&      #",
        "#&   *  =",
        "####-####",
        "#   H   #",
        "#& & & &#",
        "#########",
    };
    
    // DISPLAY THE WHOLE MAZE
    for(int row = 0; row < ROW; row++)
    {
        for(int col = 0; col < COL; col++)\
        {
            cout << map[row][col];
        }
        cout << '\n';
    }
    
    // KEY HELD? (Y/N)
   // bool key = true;
    bool key = false;
    
    // GO TO A POSITION AND GET MAPPING
    int current_row{0}, current_col{0};
    char current_map;
    
    // GO TO A POSITION
    current_row = 5;
    current_col = 7;
    current_map = map[current_row][current_col];
    
    cout << current_map << '\n';
    
    // CHECK EVERYWHERE - OR 'FIND THE EXIT'
    for(int row = 0; row < ROW; row++)
    {
        for(int col = 0; col < COL; col++)\
        {
            current_map = map[row][col];
            
            if(current_map == '=')
            {
                cout << "Door @ [" << row << "][" << col << "] is ";
                if (key)
                    cout << "now open\n";
                else
                    cout << "locked\n";
            }
        }
    }
    
    return 0;
}



#########
#       #
# ### #*#
# #&  #*#
# #   #*#
# #&  #H#
# #   #~#
# #&  ###
#@#    &#
#### ####
#&     *#
#  *    #
# *    *#
#&      #
#&   *  =
####-####
#   H   #
#& & & &#
#########
H
Door @ [14][8] is locked
Program ended with exit code: 0
Thanks it worked!
Thank you so much
And i got another problem. i need to make a max health and a min health but i can't. i tried 3 ways and those did not worked and tried searching on the internet but nothing. could you do it? i can also put you in the credits if u want
ps: as you can see you can edit the map :)
make your own maps!
Last edited on
Yep, any map is possible. You can even write a map generator program and store/retrieve files of maps of varied sizes.

The idea of health is essentially the same as the key. The player has a certain amount of health, a starting amount that as time goes by drains away or is replenished. Time can be a move, or real time.

Health is replenished by arriving at a health location, a H for instance.

Have two constants h_max and h_min and they can be checked on each cycle and acted on accordingly, continue to next position or ... dead.

Since more complexity/interest is being added the way to handle all this is to start thinking about using struct’s or claases to handle the players properties like current position, key holding and now, health
Topic archived. No new replies allowed.