Bounds checking 2-D array

I'm having trouble bounds checking a 2-D array. I haven't learned pointers yet so want to do it without them. I'm making the Dungeon Crawl game as suggested here: http://www.cplusplus.com/forum/articles/12974/

I have it to where the player can not go off the by pushing up or down, and can't go off the map to the left at the starting position. But anywhere else if they move left or right the players position loops over to the other side of the map and up a row if moving left, or down a row if moving right.


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

using namespace std;

const int ROWS = 10, COLS = 10;
void showMap(char [][COLS], int);
void setTraps(char [][COLS], int, int, int);
int move(char [][COLS], int, char);

int main(){
	
	char map[ROWS][COLS];
	int row, col, gameStatus;//gameStatus=(Won, hit trap, nothing happened)
	char playerMove;
	

	//Initializing all spots to '.' blank spaces
	for (row = 0; row < ROWS; row ++){
		for (col = 0; col < COLS; col ++){
			map[row][col] = '.';
		}
	}

	//Setting player start position
	map[row = 0][col = 0] = 'P';

	//Setting finish position
	map [row = 9][col = 9] = 'F';


	//Setting the traps
	setTraps(map, ROWS, row, col);

	//Displaying the map
	showMap(map, ROWS);

	do {
		cin >> playerMove;

		while (playerMove != 'a' && playerMove != 'A' && playerMove != 's' && playerMove != 'S' && playerMove != 'd' && playerMove != 'D' && playerMove != 'w' && playerMove != 'W'){
			system("CLS");

			cout << "Sorry that was not a choice. Please try again.\n";

			showMap(map, ROWS);

			cin >> playerMove;
		}

		gameStatus = move(map, ROWS, playerMove);

		system("CLS");

		//Displaying the map
		showMap(map, ROWS);

	}while(gameStatus == 1);
	
         if (gameStatus == 2)
		cout << "You hit a trap!\nGame over!\n";
	else if (gameStatus == 3)
		cout << "You Won!\n";

	system ("pause");
	return 0;
}

int move(char map[][COLS], int rows, char playerMove){
	
	static int r = 0, c = 0;

//MOVE UP
if (playerMove == 'w' || playerMove == 'W'){
	map[r][c] = '.';
	r--;
	if (map[r][c] == '.'){
		map[r][c] = 'P';
		return 1;
	}
	else if (map[r][c] == 'T')
		return 2;
	else if (map[r][c] == 'F')
		return 3;
	else {
		map[++r][c] = 'P';
		system("CLS");
		showMap(map, ROWS);
		cout << "There's a wall there!\n";
			
		cin >> playerMove;

		while(playerMove != 'a' && playerMove != 'A' && playerMove != 's' && playerMove != 'S' && playerMove != 'd' && playerMove != 'D' && playerMove != 'w' && playerMove != 'W'){		
		system("CLS");

		cout << "Sorry that was not a choice. Please try again.\n";

		showMap(map, ROWS);

		cin >> playerMove;
		}

		move(map, ROWS, playerMove);
	}
}
//MOVE Left
else if (playerMove == 'a' || playerMove == 'A'){
	map[r][c] = '.';
	c--;

	if (map[r][c] == '.'){
		map[r][c] = 'P';
		return 1;
	}
	else if (map[r][c] == 'T')
		return 2;
	else if (map[r][c] == 'F')
		return 3;
	else {
		map[r][++c] = 'P';
		system("CLS");
		showMap(map, ROWS);
		cout << "There's a wall there!\n";
			
		cin >> playerMove;

		while(playerMove != 'a' && playerMove != 'A' && playerMove != 's' && playerMove != 'S' && playerMove != 'd' && playerMove != 'D' && playerMove != 'w' && playerMove != 'W')
                   {		
		system("CLS");

		cout << "Sorry that was not a choice. Please try again.\n";

		showMap(map, ROWS);

		cin >> playerMove;
		}

		move(map, ROWS, playerMove);
	}
}
}


Pushing left right here shows my error and does the "else" statements
P . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . F

Pushing left where the big P is makes it go to where the little p is.
. . . . . . . . . p
P . . . . . . . . .
. . . . . . . . . .
etc...

or pushing right does this
. . . . . . . . . P
p . . . . . . . . .
etc...

I left off the showMap and setTraps functions. Thanks for the help!
Something like :

1
2
3
4
if( (playerposition == gamespace[0][0]) && (playerinput == left))
    cout << "You cant move that direction" << endl;
else if(playerposition == gamespace[0][9] && playerinput == right)
         playerposition = gamespace[1][0];


I didnt really look at your code to give you a example to c&p but that should give you an idea on what to do, also it will probably be easier to read your code if you use a switch statement instead of my if else.
Last edited on
Yea what you're saying makes since, but a switch statement wouldn't work since can only take one case.

I guess I'm looking for an easier way to do it without a lot more lines of code, in case I wanted to change the dimensions of the board.

I also tried the following code to catch if the subscript for columns goes less than 0, but didn't work or even make it to it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
else if (c < 0){
               map[r][++c] = 'P';
	system("CLS");
	showMap(map, ROWS);
	cout << "There's a wall there!\n";
			
	cin >> playerMove;

	while(playerMove != 'a' && playerMove != 'A' && playerMove != 's' && playerMove != 'S' && playerMove != 'd' && playerMove != 'D' && playerMove != 'w' && playerMove != 'W')
          {		
		system("CLS");

		cout << "Sorry that was not a choice. Please try again.\n";

		showMap(map, ROWS);

		cin >> playerMove;
          }

		move(map, ROWS, playerMove);
}


For some reason it is satisfying the first IF condition so it doesn't make it to any of the IF ELSE's. Which tells me that c-- (decrement) is putting player position still on the board, but in the process changes rows number somehow also as I showed here:

Pushing left where the big P is makes it go to where the little p is.
. . . . . . . . . p
P . . . . . . . . .
. . . . . . . . . .
etc...
Keeps you going to the next array by checking if column is 9 and the row isnt 9 cause if it is they won when user chooses to go right:

1
2
3
if(column == 9 && row != 9){
  ++row;
  column = 0;}


So user can be kept in bounds of array going left.

1
2
3
4
5
6
7
8
9
if(row == 0 && column == 0){
  cout << "There is a wall there!" << endl;} //checks if user is in start position and trying to go left

if(row >= 0 && column > 0){
  --column;} // gos back one column if its not the first column of the row

if(row > 0 && column == 0){
  --row;
  column = 9;} // if its not the first row but is the first column, goto the previous row and the last column of that row 


I would do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool won = false; //declare outside main

char direction;

while(cin >> tolower(direction) && won == false){ //keep going while there is user input and one of the direction functions doesnt change "won" to true, also use tolower() to make inout lowercase so case doesnt need to be checked
	switch(direction){
		case 'a':
		left(); //make function for going left
                checktrap() //function to check if they moved to a trap
		break;

		case 'd':
		right(); //make function for going right
                checktrap() //function to check if they moved to a trap
		break;

		default:
		break;}}

cout << "congrats you won the game!" << endl;
Last edited on
That helped a lot, the tolower trick is pretty cool too!

I'm still not understanding exactly why it's moving up and to the other side of another array though.

Say the player position is on [1][0] and they push left, making me deduct 1 from columns. Why isn't it checking if (map[1][-1] == '.') and making
map[1][-1] = 'P' ?

I almost feel like I'm missing something obvious haha!
Does it check if the column is 9 because the size of the array is 10, making the subscript go from 0 to 9? I was under the impression that subtracting 1 from the 0 subscript would take it down to -1?
Topic archived. No new replies allowed.