help with maze program C++

My program is supposed realize that it has hit a 'K', which is a key to unlock a door, and store it. I am not sure how to link the two. any suggestions on what to do. thanks in advance for your time and help

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
  #include <iostream>
   using namespace std;
     void print(char maze[16][16])
    {
       for(int i = 0; i < 16; i++)
       {
       for(int j = 0; j < 16; j++)
      {
          cout << maze[i][j];
                                  }
          cout << endl;
                                }
                                   }
     int main(){
                char maze[][16] = { {  'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X','X'},
                                    {  'X', ' ', ' ', ' ', 'X', ' ', ' ', ' ', 'X', 'X', 'X', 'X', 'X', ' ', ' ','X'},
                                    {  'X', ' ', 'X', ' ', ' ', ' ', 'X', ' ', 'X', ' ', ' ', ' ', ' ', ' ', ' ','X'},
                                    {  'X', ' ', 'X', 'X', 'X', 'X', 'X', ' ', 'X', ' ', 'X', 'X', 'X', 'X', 'X','X'},
                                    {  'X', '#', 'X', ' ', ' ', ' ', ' ', ' ', 'X', 'X', ' ', 'X', ' ', ' ', ' ','X'},
                                    {  'X', 'X', 'X', ' ', 'X', ' ', 'X', 'X', 'X', ' ', ' ', 'X', 'X', 'X', 'X','X'},
                                    {  'X', ' ', ' ', ' ', 'X', ' ', 'X', ' ', 'X', ' ', ' ', 'X', ' ', 'X', ' ','X'},
                                    {  'X', ' ', 'X', ' ', 'X', 'X', 'X', ' ', 'X', 'X', ' ', 'X', ' ', 'X', 'K','X'},
                                    {  'X', ' ', 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', ' ','X'},
                                    {  'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', ' ', ' ', ' ', 'X', ' ','X'},
                                    {  'X', ' ', ' ', 'X', ' ', ' ', ' ', 'X', ' ', 'X', ' ', ' ', 'X', 'X', ' ','X'},
                                    {  'X', 'X', ' ', 'X', ' ', 'X', ' ', 'X', ' ', ' ', 'X', ' ', ' ', ' ', ' ','X'},
                                    {  'X', 'X', ' ', ' ', ' ', 'X', ' ', ' ', ' ', 'X', 'X', ' ', 'X', 'X', 'X','X'},
                                    {  'X', 'X', 'X', 'X', 'X', ' ', ' ', 'X', 'X', ' ', ' ', ' ', 'X', ' ', 'X','X'},
                                    {  'X', '*', ' ', ' ', ' ', ' ', 'X', ' ', ' ', ' ', 'X', 'X', 'X', ' ', ' ','X'},
                                    {  'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X','X'} };
 
                 int row = 14;
                 int col = 1;
 
                 bool gameEnd = false;
                 while(!gameEnd){
                print(maze);
  char move = ' ';
                                   cout << "Move: ";
                                   while(move != 'u' && move != 'd' && move != 'l' && move != 'r' && move != 'q')
                                   {
                                   cin >> move;
                                                }
                                   if (move == 'u')
                                   {
                                   if (maze[row - 1][col] == ' ')
                                   {
                                     maze[row][col] = ' ';
                                     row--;
                                     maze[row][col] = '*';
                                    }
                                   else if (maze[row - 1][col] == '#')
                                   {
                                     gameEnd = true;
                                                     }
                                                                        }
                                   else if (move == 'd')
                                   {
                                       if (maze[row + 1][col] == ' ')
                                      {
                                          maze[row][col] = ' ';
                                         row++;
                                          maze[row][col] = '*';
                                                                }
                                   else if (maze[row + 1][col] == '#')
                                   {
                                      gameEnd = true;
                                                      }
                                                                        }
                                   else if (move == 'l')
                                   {
                                       if (maze[row][col - 1] == ' ')
                                       {
                                          maze[row][col] = ' ';
                                          col--;
                                          maze[row][col] = '*';
                                                                        }
                                  else if (maze[row][col - 1] == '#')
                                  {
  gameEnd = true;
                                                      }
                                                                        }
                                 else if (move == 'r')
                                  {
                                       if (maze[row][col + 1] == ' ')
                                       {
                                          maze[row][col] = ' ';
                                         col++;
                                          maze[row][col] = '*';
                                                                        }
                                  else if (maze[row][col + 1] == '#')
                                  {
                                     gameEnd = true;
                                                                       }
                                                                           }
                                 else if (move == 'q')
                                 {
                                     gameEnd = true;
                                                       }
                                      else
                                      {
                                           cout << "Invalid Input" << endl;
                                                                            }
                                                                              }

               return 0;
       }
                                                        

Assuming your game will have only 1 player then you could create a boolean variable called i.e. hasKey and set its default value to false.

When checking for a valid move (i.e. there's a space), add a IF to check for K and if its found then set hasKey to true, move player to position of key - you need to remove the key otherwise a player would keep collecting it again and again.

When you come to use the key, you would check your actually in front of a valid door, set hasKey to false, move player on door position, i.e. gone over locked door.

perfect! So i have figured out how to get it to notice there is a key and when the user gets to the door, the program checks for it. How do replace the 'K' space with a ' ' space. Would i use another IF statement?

If you are referring to removing the key once you have collected it then just access the map array and set it as a space, i.e.

map[row][column] = ' ';

The biggest problem with your design is that when you come to restart the game for another go you would have previously modified the map (i.e. taken away keys and doors) so each time you restart you would need to put them back.

How I have approached this in the past was to have a "current map" buffer (or in your case a array) and level map data (lookup data), then lets say I go to level one 1, I copy level 1's data to my map buffer then draw/use the map buffer during play - that way when you make changes during playing you are modifying the buffer and not the actual map data.

Hope that made sense to you.
Topic archived. No new replies allowed.