PDA

View Full Version : Where do I fit the recursion into this?


graficdes9er
03-15-2010, 09:07 PM
Here is my world. And I do not know where to place recursion. This is for an assignment, I know how recursion works, but not sure how it is going to work here. We have to click on the key and make the hare move. He is. That works.. and he moves forward.. Where would I place recursion, since that is the major thing about the assignment?

graficdes9er
03-15-2010, 10:44 PM
Anybody? I have to turn this in tonight.... ugh.

x2495iiii
03-16-2010, 12:22 AM
Some assignments have you place certain program elements where they really shouldn't be (if you wanted it to be efficient, that is) for the sake of teaching you about that element. if all you have is a rabbit moving forward, you can just have a method where the rabbit moves forward, then have that method call itself after that. Bingo, you have recursion, and it's going to move that rabbit forward forever.

Now, if you want the rabbit to move a limited distance forward, you'll need to put a stopper in there somewhere. I'll leave that up to you.

graficdes9er
03-16-2010, 01:36 AM
Ok could you look at my file. I have it move forward, but I have the event saying when you click on the key.. it starts the process

This is how it goes

EVENT - when world starts - bunny say

When clicked on key
if x>5
windupKey roll "direction" 2 revs"
hare.slide (how much)

in hare slide I used if else.. roles and then included the hare slide again..

That is recursion right?

x2495iiii
03-16-2010, 02:05 AM
Well... I always thought of recursion as something that calls itself in order to perform an action multiple times. Like this:

[PoolFill]
Pool fill with 15 gallons of water;
If pool is filled
>(Do nothing)
Else
PoolFill();

See, the method will keep calling itself over and over automatically until the pool is filled.

Now, recursion can also be something like:

[HareHop]
bunnyHops decrement by 1
Bunny move forward 1 meter
If bunnyHops > 0
>HareHop();
Else
>(Do nothing)

This is nearly the same setup, but using a number variable called bunnyHops as the condition which stops the recursion. By setting that variable to a certain number before you call the method HareHop for the first time, you can guarantee it will be called that many times and the bunny will move forward that many times.

I know for a fact that THAT'S recursion. But to make sure, let me look it up real quick...

And it looks like I'm right. To be recursive, a method or function must call itself as the very last command it executes (unless of course you put a conditional stop, as i did, in there).

graficdes9er
03-16-2010, 02:26 AM
Ok, so here is what I have - in my hare method I have it IF/ELSE...
IF "hare is within 1.5 meters of bunny" bunny say ...

ELSE - """ANOTHER if/else))
IF/World event - "which is the when mouse clicked on"
Do together - key roll -
Hare move forward random = min "" max "" dur ""

else - do nothing

under those is the hare method again placed.

in my events I have it say when you click on key - do together - key roll - hare.my user created method ELSE do nothing.

rankhornjp
03-16-2010, 10:29 AM
If I understand you correctly:

You want to click the key and then have the rabbit do "skateboardslide" until it gets to the ramp and then stop.

Is that correct?


If so, look at this file and see if it helps you out. Look at method iceSkater.closing