Access

Good afternoon fellas! I have a question. This is a snippet from a program. In this snippet i draw bunnies from my vector into a screen. It works great. However, later on I need to check if a square next to it is empty or is it taken is there a way that i could to that directly?

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
void Draw()
{
	system("cls");
	for (unsigned int i = 0; i < height; i++)
	{
		for (unsigned int j = 0; j < width; j++)
		{
			int x = 0;
			for (unsigned int z = 0; z < package.size(); z++)
			{
				if (package[z].getAge() > 1 && package[z].getSex() == "Male" && package[z].getPositionX() == i && package[z].getPositionY() == j)
				{
					std::cout << "M";
					break;
					
				}
				else if (package[z].getAge() == 1 && package[z].getSex() == "Male" && package[z].getPositionX() == i && package[z].getPositionY() == j)
				{
					std::cout << "m";
					break;
				}
				else if (package[z].getAge() > 1 && package[z].getSex() == "Female" && package[z].getPositionX() == i && package[z].getPositionY() == j)
				{
					std::cout << "F";
					break;
				}
				else if (package[z].getAge() == 1 && package[z].getSex() == "Female" && package[z].getPositionX() == i && package[z].getPositionY() == j)
				{
					std::cout << "f";
					break;
				}
				else if (package[z].getSex() == "BADASS" && package[z].getPositionX() == i && package[z].getPositionY() == j)
				{
					std::cout << "X";
					break;
				}
				x++;
				
			}
			if (x == package.size())
				std::cout << ".";

		}

		std::cout << std::endl;
	}

	
}
void Trap()
{
	for (unsigned int i = 0; i < package.size(); i++)
	{
		int move = getRandomNumberObjectless(0, 4);;
		switch (move)
		{
		case 0:
			package[i].increasePositionX();
			break;
		case 1:
			package[i].increasePositionY();
			break;
		case 2:
			package[i].decreasePositionX();
			break;
		case 3:
			package[i].decreasePositionX();
			break;
		}

		if (package[i].getPositionX() > 39)
			package[i].setPositionX(0);
		else if (package[i].getPositionY() > 39)
			package[i].setPositionY(0);
		else if (package[i].getPositionX() < 0)
			package[i].setPositionX(39);
		else if (package[i].getPositionY() < 0)
			package[i].setPositionX(39);
	}
}


This is how i tried it kind of works however if i need to check if it is empty or not it gets difficult.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (unsigned int i = 0; i < package.size(); i++)
		{
			if (package[i].getSex() == "BADASS")
			{
				RadioBunnyGenerator xD;
				int x = getRandomNumberObjectless(0, package.size() - 1);
				if ((package[x].getSex() != "BADASS") && ((package[i].getPositionX() == (package[x].getPositionX()+1) || (package[x].getPositionX()-1)) 
					&& (package[i].getPositionY()) == (package[x].getPositionY()+1) || (package[x].getPositionY()-1)))
				{
					package[x] = xD.generateRadioBunny();
				}
			}

		}
You may consider to use a 2d array.

In that case x must be set before the loop. Within the loop you may use an array to store the index of the (8?) found positions and then check the status.
Topic archived. No new replies allowed.