### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143`` ``````#include #include #include #include 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 :

 ``1234`` ``````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.
 ``12345678910111213141516171819202122`` ``````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:

 ``123`` ``````if(column == 9 && row != 9){ ++row; column = 0;}``````

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

 ``123456789`` ``````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:

 ``1234567891011121314151617181920`` ``````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.