Dungeon in need of refinement ! ! !

Hello all,

Have written the following code in response to the dungeon crawl exercise - it's a slight variation.

The numberpad direction keys are for direction and the field should chance after each 'turn' adding one alien and clearing one 'trap or alien'.

It seems to work 90% well - just not certain where or what the error is / might be.

Could someone run the code and give some feedback please - you don't need to run the code obviously if the error is 'glaringly' obvious!

Many thanks,

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

using namespace std;

int main ()

{
char grid [10][10];					// basic game grid size
int grid_check [10][10];			//to flag positions once something is there o=nothing 1=trap 2=player 3=goal 4=bad guy
int x = 0, y = 0;					//x-axis and y-axis position
int randx = 0, randy =0;			//random values to be passed to x and y position
int ran1;							//temp store for random number						
int rn;								//variable for number of random numbers required
int level;							//place to store level selection	
int n;
int move;							//direction of move integer


bool game_end=0;					// end of game flag
int a=0, b=0;


//  ##############################  Setting up and printing initial game grid  #####################################

srand((unsigned)time(0));			//set off random counter

// Take appropriate level input
while ((cout << string(50, '\n') <<"Please select difficulty level 1-5\n") && (!(cin >> level) || level<1 || level>5))
	{
	cin.clear();
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

// basing number of random nmbers needed on level
rn = (10*level);

// Fill check_array with 0
for (x=0; x<10; x++)				
	for (y=0; y<10; y++)
		grid_check[x][y]=0;
		
// generating the numbers and putting markers into the check array memory
for (n=0; n<=rn; n++)
	{
	randx =rand()% 11;
	randy =rand()% 11;
	
	grid_check[randx][randy]=1;
	}
	
//fill rest of array up with dots
for (x=0; x<10; x++)				
	for (y=0; y<10; y++)
		if (grid_check[x][y] !=1)
			grid[x][y]='.';
			else grid [x][y] = 'T';
	
// Placing the treasure
grid [9][9]='X';			
grid_check [9][9] =3;

// Placing the player
grid[0][0] = 'C';
grid_check [0][0] = 2;


//create space			
	cout << string(50, '\n'); 
 
//print out array
for (x=0; x<10; x++)
	{
	cout << "\n\t\t\t\t";
		for (y=0; y<10; y++)
			cout << grid[x][y] << " ";	
	}
							
// *******************************    Main game loop   ********************************************************

while (game_end == 0)
	{
	randx =rand()% 11;
	randy =rand()% 11;
	
	// Generate a random bad guy each turn
	while (grid_check[randx][randy]!=0)			// makes sure that spot is vacant
		{
		randx =rand()% 11;
		randy =rand()% 11;
		}
		grid_check[randx][randy]=4;				// flagging the spot
		grid [randx][randy]='@';				// placing bad guy
	
	// getting appropriate input
	while ( cout << "\nPlease input direction choice:" && (!(cin >> move) || move<1 || move>9))
	{
	cin.clear();
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
	
	// Move down
	if (move == 2)
		{
		grid_check[a][b]=0;		// unflag spot about to be left
		grid[a][b]='.';			// Place a point on it
		a++;					// Change value of x-axis
		if (grid_check[a][b]!=0)
			game_end=1;
				else
				{
				grid[a][b]='C';			// Put character in new spot
				grid_check[a][b]=2;}		// Flag new spot
		}
	
	// Move right
	else if (move == 6)
		{
		grid_check[a][b]=0;		// unflag spot about to be left
		grid[a][b]='.';			// Place a point on it
		b++;					// Change value of y-axis
		if (grid_check[a][b]!=0)
			game_end=1;
				else
				{
				grid[a][b]='C';			// Put character in new spot
				grid_check[a][b]=2;}		// Flag new spot			
		}
	
	// Move up
	else if (move == 8)
		{
		grid_check[a][b]=0;		// unflag spot about to be left
		grid[a][b]='.';			// Place a point on it
		a--;					// Change value of x-axis
		if (grid_check[a][b]!=0)
			game_end=1;
				else
				{
				grid[a][b]='C';			// Put character in new spot
				grid_check[a][b]=2;}		// Flag new spot
		}	
	
	// Move left
	else if (move == 4)
		{
		grid_check[a][b]=0;		// unflag spot about to be left
		grid[a][b]='.';			// Place a point on it
		b--;					// Change value of y-axis
			if (grid_check[a][b]!=0)
			game_end=1;
				else
				{
				grid[a][b]='C';			// Put character in new spot
				grid_check[a][b]=2;}		// Flag new spot
		}
	
	//create space			
	cout << string(50, '\n'); 
	
	// A little help to clear a space
	
	randx =rand()% 11;
	randy =rand()% 11;
		
		if (grid_check[randx][randy] != 0 && grid_check[randx][randy]!=2 && grid_check[randx][randy] !=3)
			{
			grid_check[randx][randy]=0;
			grid [randx][randy]='.';
			}
				
	//print out array	
	for (x=0; x<10; x++)
		{
		cout << "\n\t\t\t\t";
			for (y=0; y<10; y++)
				cout << grid[x][y] << " ";	
		}
				
	// Check for game end situations
	if (grid_check [9][9]==2 || b>9 || a>9 || a<0 || b<0)	
			{ 
			game_end=1;
			}
	}  // ######################################### End of main game loop  ##########################################
	
	
// Final messages	
if (grid_check[a][b]==3)
	cout << "\nCongratulations, you did it!\n";
	else if (grid_check[a][b]==4)
		cout << "\nSorry, Killed by an ALIEN!!!\n";
		else
			cout << "\nSorry, not this time - you're DEAD!!\n";

return 0;
}
Hi dan! :)

One thing I noticed that you need to take care of is this couple of lines:
1
2
randx =rand()% 11;
randy =rand()% 11;

It should be:
1
2
randx =rand()% 10;
randy =rand()% 10;

rand()%11 gives you a number between 0 and 10. You don't want that...
What you want is a number between 0 and 9, which is exactly what rand()%10 gives you.
Last edited on
Once again, many thanks - all my buggy errors seem to have vanished. I remembered that the ran call gave numbers up to but not including the limit given, however forgot momentarily that my array numbers were 0-9.

All's well now - not sure if I need to make the 'aliens' move in random directions as per exercise as have already gone above and beyond its scope. I think I'm going to move on to 'fun with functions' now - have been itching to get here as I know it will cut down massively on my code length - should also go some-way to satisfy my aesthetic sense of how code should look (wouldn't have thought that this would bother me at the start of all this!)

Many thanks,

Dan
Okay, couldn't help but tweak a little bit more - here is my final attempt at the game, allowed the player to 'eat' up to three 'aliens' in their quest for the treaasure. really moving on now ... honest.

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

using namespace std;

int main ()

{
char grid [10][10];					// basic game grid size
int grid_check [10][10];			//to flag positions once something is there o=nothing 1=trap 2=player 3=goal 4=bad guy
int x = 0, y = 0;					//x-axis and y-axis position
int randx = 0, randy =0;			//random values to be passed to x and y position
int ran1;							//temp store for random number						
int rn;								//variable for number of random numbers required
int level;							//place to store level selection	
int n;
int move;							//direction of move integer
int eat=0;

bool game_end=0;					// end of game flag
int a=0, b=0;


//  ################################  Setting up and printing initial game grid  ######################################

srand((unsigned)time(0));			//set off random counter
// Instructions
cout << string(20, '\n') << "##########################  Welcome to DUNGEON CRAWL  #########################\n\n\n\n\n";
cout << "C = YOU \nX = TREASURE\n";
cout << "T = TRAP \n@ = BAD GUY\n\n\n";
cout << "Use the numberpad direction arrows to move around\nYou can 'eat' three bad guys but no more!!\n\n\n";
cout << "\n\nGOOD LUCK!!!\n\n\n";

// Take appropriate level input
while ((cout <<"Please select difficulty level 1-5\n\n") && (!(cin >> level) || level<1 || level>5))
	{
	cin.clear();
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

// basing number of random nmbers needed on level
rn = (10*level);

// Fill check_array with 0
for (x=0; x<10; x++)				
	for (y=0; y<10; y++)
		grid_check[x][y]=0;
		
// generating the numbers and putting markers into the check array memory
for (n=0; n<=rn; n++)
	{
	randx =rand()% 10;
	randy =rand()% 10;
	
	grid_check[randx][randy]=1;
	}
	
//fill rest of array up with dots
for (x=0; x<10; x++)				
	for (y=0; y<10; y++)
		if (grid_check[x][y] !=1)
			grid[x][y]='.';
			else grid [x][y] = 'T';
	
// Placing the treasure
grid [9][9]='X';			
grid_check [9][9] =3;

// Placing the player
grid[0][0] = 'C';
grid_check [0][0] = 2;


//create space			
	cout << string(50, '\n'); 
 
//print out array
for (x=0; x<10; x++)
	{
	cout << "\n\t\t\t\t";
		for (y=0; y<10; y++)
			cout << grid[x][y] << " ";	
	}
							
// *******************************    Main game loop   ********************************************************

while (game_end == 0)
	{
	randx =rand()% 10;
	randy =rand()% 10;
	
	// Generate a random bad guy each turn
	while (grid_check[randx][randy]!=0)			// makes sure that spot is vacant
		{
		randx =rand()% 10;
		randy =rand()% 10;
		}
		grid_check[randx][randy]=4;				// flagging the spot
		grid [randx][randy]='@';				// placing bad guy
	
	// getting appropriate input
	while ( cout << "\nPlease input direction choice:" && (!(cin >> move) || move<1 || move>9))
	{
	cin.clear();
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
	
	// Move down
	if (move == 2)
		{
		grid_check[a][b]=0;		// unflag spot about to be left
		grid[a][b]='.';			// Place a point on it
		a++;					// Change value of x-axis
		if (eat > 2 && grid_check[a][b]!=0)
			game_end=1;
				else if (grid_check[a][b]==4 && eat <= 2)
					{
					grid[a][b]='C';
					grid_check[a][b]=2;
					eat++;
					}
					else if (grid_check[a][b]==1)
						game_end=1;
						else if (grid_check[a][b]==3)
						game_end=1;
							else
							{
							grid[a][b]='C';			// Put character in new spot
							grid_check[a][b]=2;}		// Flag new spot
		}
	
	// Move right
	else if (move == 6)
		{
		grid_check[a][b]=0;		// unflag spot about to be left
		grid[a][b]='.';			// Place a point on it
		b++;					// Change value of y-axis
		if (eat > 2 && grid_check[a][b]!=0)
			game_end=1;
				else if (grid_check[a][b]==4 && eat <= 2)
					{
					grid[a][b]='C';
					grid_check[a][b]=2;
					eat++;
					}
					else if (grid_check[a][b]==1)
						game_end=1;
						else if (grid_check[a][b]==3)
						game_end=1;
							else
							{
							grid[a][b]='C';			// Put character in new spot
							grid_check[a][b]=2;}		// Flag new spot			
		}
	
	// Move up
	else if (move == 8)
		{
		grid_check[a][b]=0;		// unflag spot about to be left
		grid[a][b]='.';			// Place a point on it
		a--;					// Change value of x-axis
		if (eat > 2 && grid_check[a][b]!=0)
			game_end=1;
				else if (grid_check[a][b]==4 && eat <= 2)
					{
					grid[a][b]='C';
					grid_check[a][b]=2;
					eat++;
					}
					else if (grid_check[a][b]==1)
						game_end=1;
						else if (grid_check[a][b]==3)
						game_end=1;
							else
							{
							grid[a][b]='C';			// Put character in new spot
							grid_check[a][b]=2;}		// Flag new spot
		}	
	
	// Move left
	else if (move == 4)
		{
		grid_check[a][b]=0;		// unflag spot about to be left
		grid[a][b]='.';			// Place a point on it
		b--;					// Change value of y-axis
			if (eat > 2 && grid_check[a][b]!=0)
			game_end=1;
				else if (grid_check[a][b]==4 && eat <= 2)
					{
					grid[a][b]='C';
					grid_check[a][b]=2;
					eat++;
					}
					else if (grid_check[a][b]==1)
						game_end=1;
						else if (grid_check[a][b]==3)
						game_end=1;
							else
							{
							grid[a][b]='C';			// Put character in new spot
							grid_check[a][b]=2;}		// Flag new spot
		}
	
	//create space			
	cout << string(50, '\n'); 
	
	// A little help to clear a space
	
	randx =rand()% 10;
	randy =rand()% 10;
		
		if (grid_check[randx][randy] != 0 && grid_check[randx][randy]!=2 && grid_check[randx][randy] !=3)
			{
			grid_check[randx][randy]=0;
			grid [randx][randy]='.';
			}
				
	//print out array	
	for (x=0; x<10; x++)
		{
		cout << "\n\t\t\t\t";
			for (y=0; y<10; y++)
				cout << grid[x][y] << " ";	
		}
				
	// Check for game end situations
	if (grid_check [9][9]==2 || b>9 || a>9 || a<0 || b<0)	
			{ 
			game_end=1;
			}
	}  // ######################################### End of main game loop  ##########################################
	
	
// Final messages	
if (grid_check[a][b]==3)
	cout << "\nCongratulations, you did it!\n";
	else if (eat >2 && grid_check[a][b]==4)
		cout << "\a\a\a\nSorry, Killed by an ALIEN!!!\n";
		else
			cout << "\a\a\a\nSorry, not this time - you're DEAD!!\n";

return 0;
}
Topic archived. No new replies allowed.