Dungeon Crawl assignment

Pages: 12
I can't figure out why a monster is getting "stuck" on grid tiles (2,9),(3,0), and (3,1). If a monster moves into any of those three coords it will remain there and simply "spawn" new monsters in adjacent tiles. I've included my code in its entirety, but I feel the problem is in the monster movement function. Also, if there are any other errors you can see in the code, I'm all ears. Thanks.

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

char grid[10][10];
string monsters[10]{"Vampire","Werewolf","Decepticon","Troll","Ogre","Chupacabra","Mummy","Zombie","Tax Collector","Piranha"};


char dispgrid();//Displays grid
char popgrid(char grid[][10]);//Creates and populates grid
char addH(char grid[][10]);//Adds hero and exit to grid
char movM(char grid[][10]);//Moves monsters on grid
char movH(char);//Allows user to move hero

int main()
{
srand (unsigned(time(0)));
char input;
    cout << "Hello world! Let's try this dungeon shit again..." << endl;

    popgrid(grid);
    addH(grid);
    dispgrid();
    while(input!='q'){
        cin>>input;
        cin.ignore();
        if (movH(input)==1){
            dispgrid();
            return 0;
        }
        if (movM(grid)==1){
            dispgrid();
            return 0;
        }
        dispgrid();
    }
    return 0;
}
char dispgrid(){//Displays the grid

int i,j;

    for (i=0;i<10;i++){
            cout<<"\n";
        for(j=0;j<10;j++){
            cout<<grid[i][j]<<" ";
        }
    }
}
char popgrid(char grid[10][10]){//Creates and populates the grid

int i,j,r;


   for (i=0;i<10;i++){
        for (j=0;j<10;j++){
            r=rand() % 100;
            if (r<=2)
                grid[i][j]='T';//traps
            else if (r>2 && r<5)
                grid[i][j]='M';//monsters
            else
                grid[i][j]='.';//empty tile
        }
   }



}
char addH(char grid[][10]){//Adds the hero to the grid along with the exit
int x=rand()%2;
int y=rand()%2;
int a=rand()%2+8;
int b=rand()%2+8;
cout<<x<<y;
    grid[x][y]='H';//Hero
    grid[a][b]='X';//Exit


}
char movH(char in){//Allows the user to move the hero


int i=0,j=0,x,y,r;
   r=rand()%10;
    for (i=0;i<10;i++){//find hero
        for(j=0;j<10;j++){
            if (grid[i][j]=='H'){
                cout<<i<<j;
                x=i;
                y=j;
                break;
            }
        }
    }
    if (in=='w' && x>0){
        if (grid[x-1][y]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x-1][y]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
         if (grid[x-1][y]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x-1][y]='H';
    }
    else if (in=='s' && x<9){
        if (grid[x+1][y]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x+1][y]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
        if (grid[x+1][y]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x+1][y]='H';
    }
    else if (in=='a' && y>0){
         if (grid[x][y-1]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y-1]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y-1]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x][y-1]='H';
    }
    else if (in=='d' && y<9){
         if (grid[x][y+1]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y+1]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y+ 1]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x][y+1]='H';
    }
    else
        cout<<"movement error";

}
char movM(char grid[][10]){//Sets up monster movement
char Mgrid[10][10];
   int i,j,x=0,r=rand()%100;


    for (i=0;i<10;i++){//find monster
        for(j=0;j<10;j++){
                r=rand()%4+1;//////
            if (grid[i][j]=='M'){
                    x++;//monster counter

            //25% chance to move monster in one of four directions
                if(r==1 && i>0 && (grid[i-1][j] != 'M' || Mgrid[i-1][j]!='M' || grid[i-1][j]!='T' || grid[i-1][j]!='X')){//check whether there is monster,trap, or end of array in new location
                    grid[i][j]='.';
                    Mgrid[i-1][j]='M';//move monster to new location on temp grid
                }
                else if(r==2 && i<9 && (grid[i+1][j] != 'M' || Mgrid[i+1][j]!='M' || grid[i+1][j]!='T' || grid[i+1][j]!='X')){
                    grid[i][j]='.';
                    Mgrid[i+1][j]='M';
                }
                else if(r==3 && j<9 && (grid[i][j+1] != 'M' || Mgrid[i][j+1]!='M' || grid[i][j+1]!='T' || grid[i][j+1]!='X')){
                    grid[i][j]='.';
                    Mgrid[i][j+1]='M';
                }
                else if(r==4 && j>0 && (grid[i][j-1] != 'M' || Mgrid[i][j-1]!='M' || grid[i][j-1]!='T' || grid[i][j-1]!='X')){
                    grid[i][j]='.';
                    Mgrid[i][j-1]='M';
                }


            }
        }
    }
    cout<<x;
    for (i=0;i<10;i++){//move monsters back to original grid
        for(j=0;j<10;j++){
                r=rand()%10;
                if (Mgrid[i][j]=='M'){
                        if (grid[i][j]=='H'){
                            grid[i][j]='M';
                            cout<<"You've been eaten by a "<<monsters[r];
                            return 1;
                        }
                        grid[i][j]='M';
                }
        }
    }

}



//d 
Your program wasn't compiling so I updated it a little. As for the problem you were experiencing, didn't see it at all, nothing looks wrong with the code and monsters at (2,9),(3,0) or (3,1) were not stuck either. Even when spawning them on theese positions it worked just fine.

Biggest error I came across was monsters eating up the other monsters and the exit:
if(r==1 && i>0 && (grid[i-1][j] != 'M' || Mgrid[i-1][j]!='M' || grid[i-1][j]!='T' || grid[i-1][j]!='X')) changed all your ||s into &&s as previously it would only not move if the grid cell contained a monster, a trap and an exit.

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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

char grid[10][10];
string monsters[10] = {"Vampire","Werewolf","Decepticon","Troll","Ogre","Chupacabra","Mummy","Zombie","Tax Collector","Piranha"};


void dispgrid();//Displays grid
void popgrid(char grid[][10]);//Creates and populates grid
void addH(char grid[][10]);//Adds hero and exit to grid
char movM(char grid[][10]);//Moves monsters on grid
char movH(char);//Allows user to move hero

int main()
{
srand (unsigned(time(0)));
char input = 'x';
    cout << "Hello world! Let's try this dungeon shit again..." << endl;

    popgrid(grid);
    addH(grid);
    dispgrid();
    while(input!='q'){
        cin>>input;
        cin.ignore();
        if (movH(input)==1){
            dispgrid();
            return 0;
        }
        if (movM(grid)==1){
            dispgrid();
            return 0;
        }
        dispgrid();
    }
    return 0;
}
void dispgrid(){//Displays the grid

int i,j;

    for (i=0;i<10;i++){
            cout<<"\n";
        for(j=0;j<10;j++){
            cout<<grid[i][j]<<" ";
        }
    }
}
void popgrid(char grid[10][10]){//Creates and populates the grid

int i,j,r;


   for (i=0;i<10;i++){
        for (j=0;j<10;j++){
            r=rand() % 100;
            if (r<=2)
                grid[i][j]='T';//traps
            else if (r>2 && r<5)
                grid[i][j]='M';//monsters
            else
                grid[i][j]='.';//empty tile
        }
   }



}
void addH(char grid[][10]){//Adds the hero to the grid along with the exit
int x=rand()%2;
int y=rand()%2;
int a=rand()%2+8;
int b=rand()%2+8;
cout<<x<<y;
    grid[x][y]='H';//Hero
    grid[a][b]='X';//Exit


}
char movH(char in){//Allows the user to move the hero


int i=0,j=0,x,y,r;
   r=rand()%10;
    for (i=0;i<10;i++){//find hero
        for(j=0;j<10;j++){
            if (grid[i][j]=='H'){
                cout<<i<<j;
                x=i;
                y=j;
                break;
            }
        }
    }
    if (in=='w' && x>0){
        if (grid[x-1][y]=='M'){
            cout<<"You were eaten by a "<< monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x-1][y]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
         if (grid[x-1][y]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x-1][y]='H';
    }
    else if (in=='s' && x<9){
        if (grid[x+1][y]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x+1][y]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
        if (grid[x+1][y]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x+1][y]='H';
    }
    else if (in=='a' && y>0){
         if (grid[x][y-1]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y-1]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y-1]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x][y-1]='H';
    }
    else if (in=='d' && y<9){
         if (grid[x][y+1]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y+1]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y+ 1]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x][y+1]='H';
    }
    else
        cout<<"movement error";

}
char movM(char grid[][10]){//Sets up monster movement
char Mgrid[10][10];
   int i,j,x=0,r=rand()%100;


    for (i=0;i<10;i++){//find monster
        for(j=0;j<10;j++){
                r=rand()%4+1;//////
            if (grid[i][j]=='M'){
                    x++;//monster counter

            //25% chance to move monster in one of four directions
                if(r==1 && i>0 && (grid[i-1][j] != 'M' & Mgrid[i-1][j]!='M' && grid[i-1][j]!='T' && grid[i-1][j]!='X')){//check whether there is monster,trap, or end of array in new location
                    grid[i][j]='.';
                    Mgrid[i-1][j]='M';//move monster to new location on temp grid
                }
                else if(r==2 && i<9 && (grid[i+1][j] != 'M' && Mgrid[i+1][j]!='M' && grid[i+1][j]!='T' && grid[i+1][j]!='X')){
                    grid[i][j]='.';
                    Mgrid[i+1][j]='M';
                }
                else if(r==3 && j<9 && (grid[i][j+1] != 'M' && Mgrid[i][j+1]!='M' && grid[i][j+1]!='T' && grid[i][j+1]!='X')){
                    grid[i][j]='.';
                    Mgrid[i][j+1]='M';
                }
                else if(r==4 && j>0 && (grid[i][j-1] != 'M' && Mgrid[i][j-1]!='M' && grid[i][j-1]!='T' && grid[i][j-1]!='X')){
                    grid[i][j]='.';
                    Mgrid[i][j-1]='M';
                }


            }
        }
    }
    cout<<x;
    for (i=0;i<10;i++){//move monsters back to original grid
        for(j=0;j<10;j++){
                r=rand()%10;
                if (Mgrid[i][j]=='M'){
                        if (grid[i][j]=='H'){
                            grid[i][j]='M';
                            cout<<"You've been eaten by a "<<monsters[r];
                            return 1;
                        }
                        grid[i][j]='M';
                }
        }
    }

}
Other than adding the <string> header and changing the ||s to &&s, what else did you do? If nothing else then what could be the issue. I'm going to include a snippet of the output file to show what is happening. Pay attention to the monster in (3,9).

Oh, and btw, my grid axes are backward compared to conventional (x,y)...by conventional standards, watch the monster located at (9,3).


. . . . . . . . . . 
. . T . . . . . . . 
. . . . . . . . . . 
. H . . . . . . . M 
. . M . . . . . . . 
. . . . . . . . . . 
. . . T . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . X . 

. . . . . . . . . . 
. . T . . . . . . . 
. . . . . . . . . M 
H . . . . . . . . . 
. . . M . . . . . . 
. . . . . . . . . . 
. . . T . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . X . 

. . . . . . . . . . 
. . T . . . . . . . 
. . . . . . . . . M 
. . . M . . . . . M 
H . . . . . . . . . 
. . . . . . . . . . 
. . . T . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . X . 

. . . . . . . . . . 
. . T . . . . . . . 
. . . M . . . . M M 
. . . . . . . . . . 
. . . . . . . . . M 
H . . . . . . . . . 
. . . T . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . X . 

. . . . . . . . . . 
. . T . . . . . . . 
. . . . M . . . . M 
. . . . . . . . M M 
. . . . . . . . . . 
. . . . . . . . . . 
H . . T . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . X . 

. . . . . . . . . . 
. . T . . . . . . . 
. . . M . . . . . M 
. . . . . . . M . . 
. . . . . . . . . M 
. . . . . . . . . . 
. . . T . . . . . . 
H . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . X . 

. . . . . . . . . . 
. . T . . . . . . . 
. . M . . . . . . M 
. . . . . . M . . . 
. . . . . . . . . . 
. . . . . . . . . M 
. . . T . . . . . . 
. . . . . . . . . . 
H . . . . . . . . . 
. . . . . . . . X . 

. . . . . . . . . . 
. . T . . . . . . M 
. . M . . . . . . M 
. . . . . M . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . T . . . . . M 
. . . . . . . . . . 
. H . . . . . . . . 
. . . . . . . . X . 

. . . . . . . . . . 
. . T . . . . . M . 
. M . . . M . . . M 
. . . . . . . . . M 
. . . . . . . . . . 
. . . . . . . . . . 
. . . T . . . . . . 
. . . . . . . . . M 
. . H . . . . . . . 
. . . . . . . . X . 

. . . . . . . . . . 
. M T . . M . . . . 
. . . . . . . . M M 
. . . . . . . . . M 
. . . . . . . . . . 
. . . . . . . . . . 
. . . T . . . . . M 
. . . . . . . . . . 
. . . H . . . . . . 
. . . . . . . . X . 

. M . . . . . . . . 
. . T . . . M . . M 
. . . . . . . . M M 
. . . . . . . . . M 
. . . . . . . . . . 
. . . . . . . . . . 
. . . T . . . . . . 
. . . . . . . . . M 
. . . . . . . . . . 
. . . H . . . . X . 


The same thing happens when a monster shows up on (conventional axes) (0,3) and (1,3). Unless I am pushing against one of the "walls". Then they are "freed" and can move around.
That doesn't happen to me what so ever, nor are there any other monsters spawning once the game has started. I did change one more thing after I posted the code which was removing the passing of the grid into the monster move function as it's global and not passed into the hero move function.



I was having similar "phantom" bugs in my code, that others weren't able to replicate. Maybe it has something to do with different compilers. I'm using MSVC++. Here is my similar thread if you want to check: http://www.cplusplus.com/forum/beginner/94863/
Use arrow keys to move, everything is fine:

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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <conio.h>
using namespace std;

#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80

char grid[10][10];
string monsters[10] = {"Vampire","Werewolf","Decepticon","Troll","Ogre","Chupacabra","Mummy","Zombie","Tax Collector","Piranha"};


void dispgrid();//Displays grid
void popgrid(char grid[][10]);//Creates and populates grid
void addH(char grid[][10]);//Adds hero and exit to grid
char movM(char grid[][10]);//Moves monsters on grid
char movH(int);//Allows user to move hero

int main()
{
srand (unsigned(time(0)));
int input = 0;
    cout << "Hello world! Let's try this dungeon again..." << endl;
    cout << "Use arrow keys to move your character indicated by 'H'" << endl;
    cout << "Press 'q' to quit!" << endl;

    popgrid(grid);
    addH(grid);
    dispgrid();
    while(input!= 113)
    {
        input = getch();
        if (movH(input)==1){
            dispgrid();
            return 0;
        }
        if (movM(grid)==1){
            dispgrid();
            return 0;
        }
        system("CLS");
        dispgrid();
    }
    return 0;
}
void dispgrid(){//Displays the grid

int i,j;

    for (i=0;i<10;i++){
            cout<<"\n";
        for(j=0;j<10;j++){
            cout<<grid[i][j]<<" ";
        }
    }
}
void popgrid(char grid[10][10]){//Creates and populates the grid

int i,j,r;


   for (i=0;i<10;i++){
        for (j=0;j<10;j++){
            r=rand() % 100;
            if (r<=2)
                grid[i][j]='T';//traps
            else if (r>2 && r<5)
                grid[i][j]='M';//monsters
            else
                grid[i][j]='.';//empty tile
        }
   }



}
void addH(char grid[][10])
{//Adds the hero to the grid along with the exit
int x=rand()%2;
int y=rand()%2;
int a=rand()%2+8;
int b=rand()%2+8;
cout<<x<<y;
    grid[x][y]='H';//Hero
    grid[a][b]='X';//Exit


}
char movH(int in){//Allows the user to move the hero


int i=0,j=0,x,y,r;
   r=rand()%10;
    for (i=0;i<10;i++){//find hero
        for(j=0;j<10;j++){
            if (grid[i][j]=='H'){
                cout<<i<<j;
                x=i;
                y=j;
                break;
            }
        }
    }
    if (in==UP && x>0){
        if (grid[x-1][y]=='M'){
            cout<<"You were eaten by a "<< monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x-1][y]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
         if (grid[x-1][y]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x-1][y]='H';
    }
    else if (in==DOWN && x<9){
        if (grid[x+1][y]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x+1][y]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
        if (grid[x+1][y]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x+1][y]='H';
    }
    else if (in==LEFT && y>0){
         if (grid[x][y-1]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y-1]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y-1]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x][y-1]='H';
    }
    else if (in==RIGHT && y<9){
         if (grid[x][y+1]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y+1]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]='.';
            return 1;
        }
        if (grid[x][y+ 1]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]='.';
            return 1;
        }
        grid[x][y]='.';
        grid[x][y+1]='H';
    }
    else
        cout<<"movement error";

}
char movM(char grid[][10]){//Sets up monster movement
char Mgrid[10][10];
   int i,j,x=0,r=rand()%100;


    for (i=0;i<10;i++){//find monster
        for(j=0;j<10;j++){
                r=rand()%4+1;//////
            if (grid[i][j]=='M'){
                    x++;//monster counter

            //25% chance to move monster in one of four directions
                if(r==1 && i>0 && (grid[i-1][j] != 'M' & Mgrid[i-1][j]!='M' && grid[i-1][j]!='T' && grid[i-1][j]!='X')){//check whether there is monster,trap, or end of array in new location
                    grid[i][j]='.';
                    Mgrid[i-1][j]='M';//move monster to new location on temp grid
                }
                else if(r==2 && i<9 && (grid[i+1][j] != 'M' && Mgrid[i+1][j]!='M' && grid[i+1][j]!='T' && grid[i+1][j]!='X')){
                    grid[i][j]='.';
                    Mgrid[i+1][j]='M';
                }
                else if(r==3 && j<9 && (grid[i][j+1] != 'M' && Mgrid[i][j+1]!='M' && grid[i][j+1]!='T' && grid[i][j+1]!='X')){
                    grid[i][j]='.';
                    Mgrid[i][j+1]='M';
                }
                else if(r==4 && j>0 && (grid[i][j-1] != 'M' && Mgrid[i][j-1]!='M' && grid[i][j-1]!='T' && grid[i][j-1]!='X')){
                    grid[i][j]='.';
                    Mgrid[i][j-1]='M';
                }


            }
        }
    }
    cout<<x;
    for (i=0;i<10;i++){//move monsters back to original grid
        for(j=0;j<10;j++){
                r=rand()%10;
                if (Mgrid[i][j]=='M'){
                        if (grid[i][j]=='H'){
                            grid[i][j]='M';
                            cout<<"You've been eaten by a "<<monsters[r];
                            return 1;
                        }
                        grid[i][j]='M';
                }
        }
    }

}
Also , use ANSI format

BAD:

1
2
3
for(;;){

}


GOOD:

1
2
3
4
for(;;)
{

}


BAD:

 
cout<<endl;


GOOD:

 
cout << endl;


small things like that make it better to read and less confusing
INIT Mgrid[10][10] with empty value ('.') first (line 179 of your original code)

you don't init Mgrid with empty values first, so chances are that some spots in Mgrid have 'M' value which cause monsters randomly appear in the same spots.
Thank you tntxtnt. It was an issue with the Mgrid array. Also, thank you greenleaf for correcting my coding grammar. I'll make those changes. Got one more question...an easy one....
BAD:

for(;;){

}

Is it also bad form to define functions in the same way? i.e.

1
2
3
void funct(){
//code type things in this area
}


Thanks again

There is no difference in performance, it is simply better not to do that, because it makes the code look messy and hard to read.
Just do:
1
2
3
4
void funct()
{
//code
}
so the braces are in the same place on both ends. And this applies for everything - loops, functions, main, etc.

I suggest you change the code with monster movement, so that instead of telling them where they can't go, tell them where they can.
Last edited on
@Vidminas & greenleaf800073

The way that people format their braces is a personal preference, there a variety of valid styles - so don't try to force your style onto others. There are lots of people who use the style you said was bad - and there is absolutely nothing wrong with it.

The important thing is that it should be consistent, most notably as long as the closing brace lines up with the beginning opening statement - that is, the for or while or if etc.

People use different styles - so one has to get used to them all.

One advantage of having the opening brace on the same line is, hopefully it will discourage a newbie from putting a semicolon there:

1
2
3
4
5
6
7
8
9
10
11
12
//newbies do this
if (condition);
{
//code
}

//I like this

if (condition){
//code

}


There has just been a whole lot of discussion about this here:

http://www.cplusplus.com/forum/lounge/95892/


I always put braces - even of there is only 1 statement - this will save me one day when I add more code.

Sometimes a null statement in a while loop is intentional & necessary, so best practice might look like this:

1
2
3
while(condition){
      ; //null statement
}


A while ago, L B suggested that braces would be enough to signal that the null statement was intentional, I had suggested the comment. So now I am suggesting a combination of the two, to be sure, to be sure. (It's St Patrick's day 8+D )

The following is mainly directed at the OP, to present a different approach to this type of problem.

Now, one of my pet hates is conditions like this:

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
if(r==1 && i>0 && (grid[i-1][j] != 'M' & Mgrid[i-1][j]!='M' && grid[i-1][j]!='T' && grid[i-1][j]!='X')){//check whether there is monster,trap, or end of array in new location
                    grid[i][j]='.';
                    Mgrid[i-1][j]='M';//move monster to new location on temp grid
                }
                else if(r==2 && i<9 && (grid[i+1][j] != 'M' && Mgrid[i+1][j]!='M' && grid[i+1][j]!='T' && grid[i+1][j]!='X')){
                    grid[i][j]='.';
                    Mgrid[i+1][j]='M';
                }
                else if(r==3 && j<9 && (grid[i][j+1] != 'M' && Mgrid[i][j+1]!='M' && grid[i][j+1]!='T' && grid[i][j+1]!='X')){
                    grid[i][j]='.';
                    Mgrid[i][j+1]='M';
                }
                else if(r==4 && j>0 && (grid[i][j-1] != 'M' && Mgrid[i][j-1]!='M' && grid[i][j-1]!='T' && grid[i][j-1]!='X')){
                    grid[i][j]='.';
                    Mgrid[i][j-1]='M';
                }


I hate them because they are not scalable and they are just plain ugly. You only have 4 things to test here - what if you had 20 or 50?

Normally things like this are best handled with switch statements, but your case is complicated by the need for an adjacency test as well.

This would be better handled by a nested for loop that checked the contents adjacent to position [Row][Col], with a switch with possibly a function call to control what happens when a favourable direction is found.

If you did this, you could move diagonally as well - without writing any extra code.

Hope all goes well.
@TheIdeasMan

For newbies, they have to learn, they'll get it within about 3 tries. Doing
1
2
3
if(blah == 0) {

}


is bad because it is unorganized because you can't see where each logic statement starts and ends. Might be good for small projects but once you have very large scale projects, you'll be in a giant pit of bad if you need to debug.
It really doesn't matter which style you use, as long as you follow a convention and everybody who will be accessing and/or modifying your code knows and uses the same convention.
Last edited on
helps to use the more readable kind in big groups though. Not good if everyone uses different styles. Everyone will change styles back and forth. So everyone use ANSI pls.
To make it more readable for you? I can read his code fine. We can't stop everybody from using brackets in this way, so why try? Books use this convention to save space, so how should anybody know? If you look up this thread about 10 posts you'll see a link to my thread pertaining to this exercise, I use the ANSI standard but I'm not forcing it down everyone's throat.
Last edited on
...Sorry It's just the way I say things. Just a tip...
@Vidminas
Thanks for the tip on the monster movement. It looks a lot cleaner now.

As far as the braces:
I don't know if all compilers work in this way, but code::blocks allows you to collapse and expand anything that is between braces. I find it looks a lot better if my functions are:

+int main(){
+void func1(){
+void func2(){
-void func3{
//code
}
Before I started doing it this way it would look like:
int main()
+{
void func1()
+{
void func2()
+{
void func3()
-{
//code
}

If you tell me that the ability to collapse and expand braces isn't standard across other compilers than I'll change it. Anyhow, I have another question.

How do I go about changing the color of 'H' so it can be easily differentiated from the rest of the grid output. If you'd like to see the new code I have up to this point let me know. Again, thank you all very much for your assistance.
greenleaf800073 wrote:
is bad because it is unorganized because you can't see where each logic statement starts and ends. Might be good for small projects but once you have very large scale projects, you'll be in a giant pit of bad if you need to debug.


Nonsense.

One can see where the logic starts - with the if statement. And one can see where it ends - with the closing brace that lines up with the if.

In terms of the size of projects, that is irrelevant. More to the point, if you have long compound statements, then you need to make more use of functions.

If you have long compound statements in your code, then I suggest you will have more debug problems than me.

Which debugger do you use on your system?

I would be interested to hear what you consider to be a "very large scale project".

Did you read any of the topic in the link I posted above?

Again, there are various valid styles - you should get used to all of them.

For newbies, they have to learn, they'll get it within about 3 tries.


Not so, there has been many a newbie which have had exactly the problem I was talking about with semicolons, and posted on this forum because of it. Sometimes there have been quite a few replies before anyone notices the problem as well.

The other thing is that newbies are capable of all kinds of weird things. I advised one newbie to do braces as I described above - to help prevent extra semicolons. Lo & behold a different newbie did this a few days later:

1
2
3
if(condition);{

}


At least the semicolon was easier to notice with the brace.
Trying to put borders around box, 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
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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <conio.h>
using namespace std;

#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80

char grid[12][12];
string monsters[10] = {"Vampire","Werewolf","Decepticon","Troll","Ogre","Chupacabra","Mummy","Zombie","Tax Collector","Piranha"};


void dispgrid();//Displays grid
void popgrid(char grid[][12]);//Creates and populates grid
void addH(char grid[][12]);//Adds hero and exit to grid
char movM(char grid[][12]);//Moves monsters on grid
char movH(int);//Allows user to move hero

int main()
{
srand (unsigned(time(0)));
int input = 0;
    cout << "Hello world! Let's try this dungeon again..." << endl;
    cout << "Use arrow keys to move your character indicated by 'H'" << endl;
    cout << "Press 'q' to quit!" << endl;

    popgrid(grid);
    addH(grid);
    dispgrid();
    while(input!= 113)
    {
        input = getch();
        if (movH(input)==1){
            dispgrid();
            return 0;
        }
        if (movM(grid)==1){
            dispgrid();
            return 0;
        }
        system("CLS");
        dispgrid();
    }
    return 0;
}
void dispgrid()
{//Displays the grid

int i,j;

    for (i=0;i<12;i++)
    {
        cout << "\n";
        for(j=0;j<12;j++)
        {
            if(i == 0)
            {
                cout << '\xB2';
            }
            else if(i == 12)
            {
                cout << '\xB2';
            }
            else if(j == 0)
            {
                cout << '\xB2';
            }
            else if(j == 12)
            {
                cout << '\xB2';
            }
            else
            {
                cout << grid[i][j] << " ";
            }
        }
    }
}
void popgrid(char grid[12][12]){//Creates and populates the grid

int i,j,r;


   for (i=0;i<12;i++){
        for (j=0;j<12;j++){
            r=rand() % 100;
            if (r<=2)
                grid[i][j]='T';//traps
            else if (r>2 && r<5)
                grid[i][j]='M';//monsters
            else
                grid[i][j]=' ';//empty tile
        }
   }



}
void addH(char grid[][12])
{//Adds the hero to the grid along with the exit
int x=rand()%2;
int y=rand()%2;
int a=rand()%2+8;
int b=rand()%2+8;
cout<<x<<y;
    grid[x][y]='H';//Hero
    grid[a][b]='X';//Exit


}
char movH(int in){//Allows the user to move the hero


int i=0,j=0,x,y,r;
   r=rand()%12;
    for (i=0;i<12;i++){//find hero
        for(j=0;j<12;j++){
            if (grid[i][j]=='H'){
                cout<<i<<j;
                x=i;
                y=j;
                break;
            }
        }
    }
    if (in==UP && x>0){
        if (grid[x-1][y]=='M'){
            cout<<"You were eaten by a "<< monsters[r];
            grid[x][y]=' ';
            return 1;
        }
        if (grid[x-1][y]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]=' ';
            return 1;
        }
         if (grid[x-1][y]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]=' ';
            return 1;
        }
        grid[x][y]=' ';
        grid[x-1][y]='H';
    }
    else if (in==DOWN && x<9){
        if (grid[x+1][y]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]=' ';
            return 1;
        }
        if (grid[x+1][y]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]=' ';
            return 1;
        }
        if (grid[x+1][y]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]=' ';
            return 1;
        }
        grid[x][y]=' ';
        grid[x+1][y]='H';
    }
    else if (in==LEFT && y>0){
         if (grid[x][y-1]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]=' ';
            return 1;
        }
        if (grid[x][y-1]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]=' ';
            return 1;
        }
        if (grid[x][y-1]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]=' ';
            return 1;
        }
        grid[x][y]=' ';
        grid[x][y-1]='H';
    }
    else if (in==RIGHT && y<9){
         if (grid[x][y+1]=='M'){
            cout<<"You were eaten by a "<<monsters[r];
            grid[x][y]=' ';
            return 1;
        }
        if (grid[x][y+1]=='T'){
            cout<<"You fell into a trap...";
            grid[x][y]=' ';
            return 1;
        }
        if (grid[x][y+ 1]=='X'){
            cout<<"You made it out ALIVE!";
            grid[x][y]=' ';
            return 1;
        }
        grid[x][y]=' ';
        grid[x][y+1]='H';
    }
    else
        cout<<"movement error";

}

char movM(char grid[][12])
{//Sets up monster movement
char Mgrid[12][12];
   int i,j,x=0,r=rand()%100;


    for (i=0;i<12;i++){//find monster
        for(j=0;j<12;j++){
                r=rand()%4+1;//////
            if (grid[i][j]=='M'){
                    x++;//monster counter

            //25% chance to move monster in one of four directions
                if(r==1 && i>0 && (grid[i-1][j] != 'M' && Mgrid[i-1][j]!='M' && grid[i-1][j]!='T' && grid[i-1][j]!='X')){//check whether there is monster,trap, or end of array in new location
                    grid[i][j]=' ';
                    Mgrid[i-1][j]='M';//move monster to new location on temp grid
                }
                else if(r==2 && i<9 && (grid[i+1][j] != 'M' && Mgrid[i+1][j]!='M' && grid[i+1][j]!='T' && grid[i+1][j]!='X')){
                    grid[i][j]=' ';
                    Mgrid[i+1][j]='M';
                }
                else if(r==3 && j<9 && (grid[i][j+1] != 'M' && Mgrid[i][j+1]!='M' && grid[i][j+1]!='T' && grid[i][j+1]!='X')){
                    grid[i][j]=' ';
                    Mgrid[i][j+1]='M';
                }
                else if(r==4 && j>0 && (grid[i][j-1] != 'M' && Mgrid[i][j-1]!='M' && grid[i][j-1]!='T' && grid[i][j-1]!='X')){
                    grid[i][j]=' ';
                    Mgrid[i][j-1]='M';
                }


            }
        }
    }
    cout << x;
    for (i=0;i<12;i++)
    {//move monsters back to original grid
        for(j=0;j<12;j++)
        {
            r=rand()%12;
            if (Mgrid[i][j]=='M')
            {
                if (grid[i][j]=='H')
                {
                    grid[i][j]='M';
                    cout << "You've been eaten by a "<<monsters[r];
                    return 1;
                }
                grid[i][j]='M';
            }
        }
    }
}
@sometimer

My IDE (KDevelop on Linux) has code collapsing as well - but it still works when the brace is on the same line. It works for any compound statement, not just functions.

Have you tried my advice about the for loops & switch, instead of your if else statements?
Pages: 12