Going beyond the boundaries of a 2D array map

I read from a comment on this forum that advised a fellow new programmer to be sure that whatever objects move on the map do not move beyond the boundaries. Otherwise, the program would crash. When making a similar map, I made a Boolean field to avoid this situation but forgot to link initializers for the "walls" of the map.

Then when I tested out the map by manually moving an object around, I moved past a wall and beyond. Since the map boundaries are the same as the length of the array where the map is stored, I expected my program to abort. Instead, I discovered that there was an extra column/row past the "wall." Furthermore, by moving another step away from the "wall," I ended appearing on the other side of the map like in the game Asteroids. This worked with both vertical and horizontal "walls."

There is nothing in my code that suggests this should happen, and the instructions for moving my objects are simply position++ and position--. Does anyone know how this occurred?

~Daleth~
Does anyone know how this occurred?


Not without seeing any code.
Functions involved with manipulating the map and displaying it:

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
//...Headers...
#define LS 10
#define MAPSIZE 50
class MapGen{
             protected:
                  int floor, pcoord[2];
                  char player;
                  char map[LS][MAPSIZE][MAPSIZE];
                  bool object[LS][MAPSIZE][MAPSIZE];
                     // ...
                  MapGen(){//...}
                  ~MapGen(){}
            public:
                     //...
}
//...
class Interact: public Object, public Player, public Stairs{
                     //...
		void Move(int dir){
			Scan();
			switch(dir){
				case 1:												//Left
					if(!(object[floor][pcoord[0]][pcoord[1]-1])){
						map[floor][pcoord[0]][pcoord[1]]='\0';
						pcoord[1]--;
							map[floor][pcoord[0]][pcoord[1]]=player;
					}
					break;
				case 2:												//Up
					if(!(object[floor][pcoord[0]+1][pcoord[1]])){
						map[floor][pcoord[0]][pcoord[1]]='\0';
						pcoord[0]++;
							map[floor][pcoord[0]][pcoord[1]]=player;
					}
					break;
				case 3:												//Down
					if(!(object[floor][pcoord[0]-1][pcoord[1]])){
						map[floor][pcoord[0]][pcoord[1]]='\0';
						pcoord[0]--;
							map[floor][pcoord[0]][pcoord[1]]=player;
					}
					break;
				case 4:												//Right
					if(!(object[floor][pcoord[0]][pcoord[1]+1])){
						map[floor][pcoord[0]][pcoord[1]]='\0';
						pcoord[1]++;
							map[floor][pcoord[0]][pcoord[1]]=player;
					}
					break;
				default:
					cout << "Error in switch-case in Move function of Interact class." << endl;
					break;
			}
		}
		void DisplayMap(){		//Print out map after development
			Object::AI(objectnum,objectcoord,objectsprite); //Bunch of other objects that move independently.
			Stairs::StairsConst();
			Scan();
			for(int IN=0; IN<MAPSIZE; IN++){
				for(int ii=0; ii<MAPSIZE; ii++){
					cout << map[floor][IN][ii];
				}
				cout << endl;
			}
		}
}*ST= new Interact;
//... 
Last edited on
Topic archived. No new replies allowed.