Xaque said:
Here is what I am trying to do. I am writing a program in C++ that mimics the behaviour of a flock of birds by specifying 2 simple rules:
1. Do not get within 3 spaces of any other birds.
2. Do not get more than 7 spaces away from any birds.
The problem I am having is that I am somewhat new to C++ and I don't know an easy way for the birds to 'look' around and see what other birds are near them. Any suggestions?
It all depends on how the program is meant to run. If you decide during runtime what the number of birds shall be, then your program must allocate the space.
It is a search algorithm solution.
You can create a matrix using arrays, or use double linked lists as your data structure.
If you use an array, use: 3(mxn) < mxn = number of birds < 7(mxn)
A link list is dynamically allocated and therefore, you hace no worries.
Once your data structure is created, traverse it using any search algorithm-- under the two constraints given. With a matrix, the programming is simple, as you can calculate the distances with ease using their positions. If you use a list, you have to create a class that keeps a number.
In anycase:
for ( birds <= # of birds)
---with current location a, previous location b
if a = bird, go to right a + 3,
b = a;
else
if ( distance ab < 7)
a = bird;
number of birds ++;
end if;
end if;
....something like that,
Or use a better search algorithm and input the constraints.