array program not correctly recognizing elements in array

I am writing a program that is essentially a game where a hero(represented on the grid by an H) needs to navigate through an 8 by 8 array. In the array there are various obstacles (8 ambushes and one enemy hideout) which are randomly placed when the program executes. If the user moves onto one of these places, they lose. If they figure out where the enemy is located they enter c (to call the police) and then indicate which direction the enemy is located. "w" moves up, "a" moves left "x" moves down, and "d" moves left. They cannot move diagonally. the user cannot leave the grid, and they are warned if a trap or the enemy is one square away(diagonals excluded).
So this is my problem. Sometimes when I run the program the message that a trap is near displays on the screen, yet if I move one square up, down, left or right, of the square where the message is dispayed it appears as if there really is not a trap located there and the game does not end. This only happens occasionally and usually in squares located in column 0. I think there may be a problem with either my message function, or the assign function.

Sorry I know its alot of code but any insight would be greatly appreciated!

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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

void assign(char block[8][8]);		//assigns where the villian is, and where the ambushes are
void displayWorld(int playerRow, int playerCol);	//displays the screen that the user will see with a H as to were the hero is
void message(char block[8][8], int pRow, int pCol);	//checks if the hero is near an ambush or the enemy
void Call(int pRow, int pCol, char block[8][8]);	//checks if the user correctly guesses where the enemy is

void main()
{
	srand(time(0));

	char block[8][8];
	char direction, answer;
	int m;

	cout << "Ahh, the Force is strong with you.\n"
			"You must sneak up on Darth Vader and call in the Jedis once you have found him,"
			"\nbut be sure you do not try to take him alone. Use the force to steer clear of "
			"\ntraps, for if you run into one the dark side will take you." << endl << endl;
	
	system("pause");

	cout << "\n \nPress 'w' to move up, 'a' to move left, 'x' to move down, and 'd' to move "
			"right.When you know where Vader is, press 'c' to call the Jedis" << endl << endl;

	system("pause");
	
	do
	{
		int pRow = 0, pCol = 0, i = 0;
		assign(block);
		
		do
		{
			displayWorld(pRow, pCol);
			message(block, pRow, pCol);
	
			do
			{
				cout << "What would you like to do?		";
				cin >> direction;
				m = 0;
		
				if(direction == 'w')
				{
					if(pRow - 1 == -1)
					{
						m = 1;
					}
					else
					{
						pRow -= 1;
					}
				}
				else if(direction == 'd')
				{
					if (pCol + 1 == 9)
					{
						m = 1;
					}
					else
					{
						pCol += 1;
					}
				}	
				else if(direction == 'x')
				{
					if(pRow + 1 == 9)
					{
						m = 1;
					}
					else
					{
						pRow += 1;
					}
				}
				else if(direction == 'a')
				{
					if(pCol - 1 == -1)
					{
						m = 1;
					}
					else
					{
						pCol -= 1;
					}
				}
				else if(direction != 'c')
				{
					cout << "That is not a direction!" << endl;
					m =	1;
				}
				
				if(m == 1)
				{
					cout << "You cannot go that way!" << endl;
				}
				cout << pRow << pCol << endl;
		
			}while(m == 1);

			if (block[pRow][pCol] == 'E' || block[pRow][pCol] == 'A' || direction == 'c')
			{
				i = 1;
			}

			system("cls");
		}while(i == 0);
		
		if(block[pRow][pCol] == 'E')
		{
			cout << "The Enemy has taken you! You lose!" << endl;
		}
		else if(block[pRow][pCol] == 'A')
		{
			cout << "You have run into a trap! You lose!" << endl;
		}
		else if(direction == 'c')
		{
			displayWorld(pRow, pCol);
			Call(pRow, pCol, block);
		}	

		cout << "Would you like to play again?(y/n)			";
		cin >> answer;

	}while(answer == 'y');
	cin.get();cin.get();
}

void assign(char block[8][8])
{
	int x, y, a, b;
	for(int row = 0; row < 6; row++)	//Assigns all places to e(empty). using < 6 so that the highest value reached will be 7
	{
		for(int col = 0; col < 6; col++)
		{
			block[row][col] = 'e';
		}
	}
	
	do
	{
		a = rand()%8;
		b = rand()%8;

	}while( a == 0 && b == 0);
	block[a][b] = 'E';

	for(int i = 0; i < 8; i++)		//Assigns a random 0-8 number of ambushes to random positions on the grid.
	{
		do
		{
			x = rand()%8;
			y = rand()%8;

		}while(block[x][y] == 'E' || (x == 0 && y == 0));
		block[x][y] = 'A';
	}
}

void displayWorld(int playerRow, int playerCol)
{
	for(int row = 0; row < 8; row++)
	{
		for(int col = 0; col < 8; col++)
		{
			if(playerRow == row && playerCol == col)
			{
				cout << " H ";
			}
			else
			{
				cout << " e ";
			}
		}
		cout << endl;
	}
}
void message(char block[8][8], int pRow, int pCol)
{
	if (block[pRow + 1][pCol] == 'A' || block[pRow - 1][pCol] == 'A' || block[pRow][pCol + 1] == 'A' || block[pRow][pCol - 1] == 'A')
	{
		cout << "A Trap is near!" << endl;
	}
	else if(block[pRow + 1][pCol] == 'E' || block[pRow - 1][pCol] == 'E' || block[pRow][pCol + 1] == 'E' || block[pRow][pCol - 1] == 'E')
	{
		cout << "The Enemy is near!" << endl;
	}
	else 
	{
		cout << "All is clear" << endl;
	}
}

void Call(int pRow, int pCol, char block[8][8])
{
	char way;
	cout << "Which way is the enemy?		";
	cin >> way;

	if(way == 'w')
	{
		if(block[pRow-1][pCol] == 'E')
		{
			cout << "The Enemy has been captured! CONGRATULATIONS!!" << endl;
		}
		else
		{
			cout << "He was not there! The Enemy has escaped!" << endl;
		}
	}
	else if(way == 'a')
	{
		if(block[pRow][pCol-1] == 'E')
		{
			cout << "The Enemy has been captured! CONGRATULATIONS!!" << endl;
		}
		else
		{
			cout << "He was not there! The Enemy has escaped!" << endl;
		}
	}
	else if(way == 'x')
	{
		if(block[pRow+1][pCol] == 'E')
		{
			cout << "The Enemy has been captured! CONGRATULATIONS!!" << endl;
		}
		else
		{
			cout << "He was not there! The Enemy has escaped!" << endl;
		}
	}
	else if(way == 'd')
	{
		if(block[pRow][pCol+1] == 'E')
		{
			cout << "The Enemy has been captured! CONGRATULATIONS!!" << endl;
		}
		else
		{
			cout << "He was not there! The Enemy has escaped!" << endl;
		}
	}
	else
	{
		cout << "The Police cannot go that way!" << endl;
		Call(pRow, pCol, block);
	}
}
Topic archived. No new replies allowed.