vector value changes unexpected

I am making a little game (just console) and my vectors don't act like I think they have to. One Value of the vector just changes and I don't know why. The Code shown below is part where this bug come from, I have deleted the rest of the code where these 2 vectors show up and the bug still appears and I DONT KNOW WHY!!
This part of the code is responsible for letting the Enemy spread to a random direction.
Oh, and I don't have much experience because I program for only 5 months know :P

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
/**in this part the first value of the 2 vectors are created (only once, I've tested it)**/
  if(moves == 0){
		int randomNum1 = (rand() % HEIGHT)+1;
		int randomNum2 = (rand() % WIDTH)+1;
		_EnemysY.push_back(randomNum1);
		_EnemysX.push_back(randomNum2);
	}
/**_Enemy vectors have normal values. For instance: _EnemysX[0] is 23 and _EnemysY[0] is 12**/
/**In this part, the Enemy spreads in a random direction**/
	if (moves > 3){
		//save Enemys at the border here (those who can move)
		std::vector<int> topX;
		std::vector<int> topY;
		std::vector<int> botX;
		std::vector<int> botY;
		std::vector<int> rigX;
		std::vector<int> rigY;
		std::vector<int> lefX;
		std::vector<int> lefY;
/**here, I wanna save all Fields of the Enemy where it can spread to:**/
		for (Uint it = 0; it < _EnemysY.size(); it++){
/**_EnemysY is still normal, but _EnemysX is like: 86BF163E0**/			
			if (_map[_EnemysY[it]-1][_EnemysX[it]] == _Grenade || _map[_EnemysY[it]-1][_EnemysX[it]] == _Field){
				topY.push_back(_EnemysY[it]);
				topX.push_back(_EnemysX[it]);
			}
			if (_map[_EnemysY[it]+1][_EnemysX[it]] == _Grenade || _map[_EnemysY[it]+1][_EnemysX[it]] == _Field){
				botY.push_back(_EnemysY[it]);
				botX.push_back(_EnemysX[it]);
			}
			if (_map[_EnemysY[it]][_EnemysX[it]-1] == _Grenade || _map[_EnemysY[it]][_EnemysX[it]-1] == _Field){
				lefX.push_back(_EnemysX[it]);
				lefY.push_back(_EnemysY[it]);
			}
			if (_map[_EnemysY[it]][_EnemysX[it]+1] == _Grenade || _map[_EnemysY[it]][_EnemysX[it]+1] == _Field){
				rigX.push_back(_EnemysX[it]);
				rigY.push_back(_EnemysY[it]);
			}
		}

/**and here is a random direction created and the programm chooses which Field it will spread to: **/
		for (;;){
			int ranDir = (rand() % 4)+1;
			if (ranDir == 1 && !topY.empty()){
				int temp = (rand() % topY.size())+1;
				_EnemysY.push_back(topY[temp]);
				_EnemysX.push_back(topX[temp]);
				return true;
			}
			if (ranDir == 2 && !botY.empty()){
				int temp = (rand() % botY.size())+1;
				_EnemysY.push_back(botY[temp]);
				_EnemysX.push_back(botX[temp]);
				return true;
			}
			if (ranDir == 3 && !lefY.empty()){
				int temp = (rand() % lefY.size())+1;
				_EnemysY.push_back(lefY[temp]);
				_EnemysX.push_back(lefX[temp]);
				return true;
			}
			if (ranDir == 4 && !rigY.empty()){
				int temp = (rand() % rigY.size())+1;
				_EnemysY.push_back(rigY[temp]);
				_EnemysX.push_back(rigX[temp]);
				return true;
			}
		}
	}


PS: sorry if my english isn't the best, I'm German ^^
Last edited on
You have a lot of vectors in your code, which one is changing?
I cludn't really test all, but so far just _EnemysX, and for some reason topY, topX, botY etc. also have pretty strange values. :(
It looks like you have precisely the same problem as here:
http://www.cplusplus.com/forum/beginner/128048/
Topic archived. No new replies allowed.