Help with comparing classes

Hi, my end goal is to create a "creature" donated as 'C' on a 30x30 grid in terminal, and "food" as 'F'. The creature and food x and y's are randomly generated. However, I would like to know if any foods are within 5 blocks/units from a creature. So far I have:
main.cpp
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
int main()
{
	const int BSIZE = 30;
	const int CREATNO = 5;
	const int FOODNO = 20;

	RandomNumber rand;
	rand.initialiseSeed();

	Console board;
	
	for (int i = 0; i < CREATNO; i++)
	{
		board.setColour(board.WHITE, board.BLUE);
		creature creati;
		creati.setpos(BSIZE);
		board.gotoXY(creati.getX(), creati.getY());
		cout << 'C';
	}
	
	for (int i = 0; i < FOODNO; i++)
	{
		board.setColour(board.WHITE, board.RED);
		food foodi;
		foodi.setpos(BSIZE);
		board.gotoXY(foodi.getX(), foodi.getY());
		cout << 'F';
	}

	Sleep(2000);

	board.gotoXY(0, 35);

	return 0;
}

creature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
creature::creature()
{
	energy = 5;
}

void creature::setpos(int BSIZE)
{
	RandomNumber rand;
	
	xpos = rand.random(0, BSIZE);
	ypos = rand.random(0, BSIZE);
}

int creature::getX()
{
	return xpos;
}

int creature::getY()
{
	return ypos;
}

food.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void food::setpos(int BSIZE)
{
	RandomNumber rand;

	xpos = rand.random(0, BSIZE);
	ypos = rand.random(0, BSIZE);
}

int food::getX()
{
	return xpos;
}

int food::getY()
{
	return ypos;
}
Last edited on
What specifically are you struggling with? You forgot to ask a question.
I would like to know if any foods are within 5 blocks/units from a creature. <-- How would I go about doing this.
What have you tried?

If you are standing at the center of a 5x5 grid, how many cells across do you have to move to get to the edge?
I was going to try and make a loop for each creature, return its x and y positions and compare them with each foods x and ys, but I wasnt sure if that was the easiest way.
If all you have is a single collection of all food, then yes, you must go through each food and ask it where it lives so the creature can decide if it is within eating distance.

If you have a grid that is occupied by food and creatures, then that's a different story.

Looking at your code I think you have the first implementation.
1
2
3
4
5
6
7
8
9
10
	for (int i = 0; i < FOODNO; i++)
	{
		creature creat0;
		food foodi;
		if ((creat0.getX() < foodi.getX + 5) && (creat0.getX() > foodi.getX() - 5) && (creat0.getY() < foodi.getY + 5) && (creat0.getY() > foodi.getY() - 5))
		{
			creat0.closestx = foodi.getX;
			creat0.closesty = foodi.getY;
		}
	}

did this. not sure what i would do if there was another food closer
Last edited on
As you examine food, keep track of how far the food was (use the distance formula) and if you find closer food, you should change your mind.

This is basically a slight alteration of the min/max array problem.
okay thanks
Topic archived. No new replies allowed.