# Thread: Nibbles, random apple generator???

1. ## Nibbles, random apple generator???

I'm working on a nibbles-type game (available here) and I've hit a problem with the function that places apples randomly on the screen without placing them off-screen or underneath the worm.

The basic structure of the game is a worm (arrays of worm.x and worm.y moving at worm.speed, with circle of radius worm.speed at each point) and an apple (circle at apple.x, apple.y with radius apple.size). Here's the function that I came up with:

Code:
```bool appleok = false;
while (!appleok) {
apple.x = rand()%(SCREEN_W - (apple.size * 4)) + (apple.size * 2);
apple.y = rand()%(SCREEN_H - (apple.size * 4)) + (apple.size * 2);
for (i = 1; i <= worm.length; ++i) {
dist = sqrt(pow(worm.x[i]-apple.x,2)+pow(worm.y[i]-apple.y,2));
if (dist > worm.speed*2 + apple.size*2)
appleok = true;
}
}```
I figure that that should work, but the apple still appears underneath the worm, any ideas?

2. I'm not sure I experienced your problem, but once the apple did seem to appear over the tail of the worm.

The problem with your for(...) loop test that I see is that you'll get "true" based on the apple's distance from a worm-circle even if later worm-circles would cover it up. You're doing this:

0 0 0 0 0 0 0 : the worm
1 2 3 4 5 6 7 : the number (e.g. 'i')

for i = 1 to 7

1: far enough from this? No. Appleok false.

2: far enough from this? No. Appleok false.

3: far enough from this? Yes. Appleok true.

4: far enought from this? NO -- but appleok is still true because we set it true last time.

5: far enough from this? NO -- but appleok is still true because we set it to true during loop 3...

etc.

That means that appleok will be true if it's far enough from just one of the circles that make up the worm, never mind the other ones.

Try this:

Code:
```...
if (dist > worm.speed*2 + apple.size*2)
appleok = true;
else appleok = false; // add this line.```

Hope that helps. I can't compile it myself 'cause I don't have allegro.

BTW post alleg40.dll too!

EDIT: Actually what you really want to do is reverse the test, so that your condition is testing if the distance is less, not greater. You set appleok to true before entering the loop, and then during the loop, if the condition tests true (i.e. the distance is too short) you trip appleok to false, otherwise you do nothing. That way, if the apple is too close to just one of the worm-circles, the loop will end with appleok false, and the while() loop will continue.

e.g.

while(!appleok)
{
appleok=true;

get random apple x, y

for ...
if distance too short
appleok=false

}

yeah? Should work.

3. Works just fine, thanks for your help.

BTW post alleg40.dll too!
it was too big to attach so I put it here. I hope that helps.

4. I would say make the board a 2d array, then set all of the empty spaces to 0, all of the places with worm to 1 and an apple to 2, or whatever. Then have a loop run through for a random amount of time and place an apple in a 0 spot. Once the apple is placed set like a boolean value to true, and then after the loop is done check to see if the value is true, and if it's not search the board for a 0 place again with a bigger value, if it still doesn't work well, do it again I guess. I hope that made sense and I think it will work.