Feb 13, 2015 at 10:01pm Feb 13, 2015 at 10:01pm UTC
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 Feb 13, 2015 at 10:02pm Feb 13, 2015 at 10:02pm UTC
Feb 14, 2015 at 1:01am Feb 14, 2015 at 1:01am UTC
What specifically are you struggling with? You forgot to ask a question.
Feb 14, 2015 at 1:16am Feb 14, 2015 at 1:16am UTC
I would like to know if any foods are within 5 blocks/units from a creature. <-- How would I go about doing this.
Feb 14, 2015 at 1:19am Feb 14, 2015 at 1:19am UTC
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?
Feb 14, 2015 at 1:22am Feb 14, 2015 at 1:22am UTC
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.
Feb 14, 2015 at 1:24am Feb 14, 2015 at 1:24am UTC
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.
Feb 14, 2015 at 1:30am Feb 14, 2015 at 1:30am UTC
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 Feb 14, 2015 at 1:30am Feb 14, 2015 at 1:30am UTC
Feb 14, 2015 at 1:32am Feb 14, 2015 at 1:32am UTC
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.