Thread: Nibbles, random apple generator???

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    Question 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?
    Illusion and reality become impartiality and confidence.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    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.
    Last edited by fusikon; 02-10-2003 at 01:34 PM.

  3. #3
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    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.
    Illusion and reality become impartiality and confidence.

  4. #4
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    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.

  5. #5
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Whoops maybe I should read your code first, you pretty much already did that.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    OMG! That game is awesome! but it is so insane after you get 10 points!
    Why drink and drive when you can smoke and fly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  2. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  3. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM