PDA

View Full Version : Cuckoo Clock Recursion


jwehrly
09-12-2013, 07:23 AM
Hello, my project is to create a cuckoo clock using recursion. Now I understand how recursion works and I have created the methods for this project but I cannot figure out how to create the IF/ELSE statement to end the loop when the user interacts with the running program. My exact assignment was written like this:

Chapter 8
Page 260, #7

On the Hour
Place a cuckoo clock (Objects) in a new world. Be sure to set the scene which means that you should select a room (look in Environments) as the backdrop for your scene as you certainly would not hang a cuckoo clock in the open air.

The idea of this animation is to have the cuckoo clock keep time (not in real time, of course). The minute hand should go around on the face of the clock (perhaps one complete revolution should take about 30 seconds in real time) and the pendulum should swing back and forth continually. When the minute hand has made one revolution (from 12 back to 12 [one revolution] on the face of the clock), then the hour hand should advance to the next hour [1/12 of a revolution], the doors should open, and the cuckoo bird (on the clock arm) should come out and chirp once. Then the bird should retreat inside the clock and the doors should close until the next hour has gone by. “All is well” as long as the clock is running—which should continue until the user stops the animation. Use a recursive method to implement the animation. Recall that ‘recursive’ means that the method calls itself. This world does not require any changes to Events but it does require a method to move the clock [cuckooClock.keepTime()] hands and another to control the cuckoo [cuckooCLock.cockoo()].

Any help is greatly appreciated.

V/r
Jwehrly

mmcham
08-17-2014, 09:07 AM
I am going to bump this back to the top because I am also stuck on this problem. I tried the original poster's solution code but it seems to actually be a loop and not a recursion. However using a loop seems to be the only way I have been able to get the pendulum to swing continuously. Maybe it is missing a variable or parameter somewhere because I do not seem to be able to drag anything into an IF/ELSE statement to recurse. It only shows IF true, do nothing (where I can drag something).

Thanks for any help or direction!

MrMoke
08-17-2014, 10:39 AM
The true can be replaced.
Click on World and then Function tab to view additional Boolean Logic, and Math, options that are available to you for making decisions. Just drag the appropriate test in over the true, drop it, and modify to your liking.

As mentioned in the text, you need to consider what condition requires a recursive call, what must be done before making the call, and what to do when that condition becomes false.

Since this project isn't a real-time simulation, the pendulum swing doesn't have to be a 1-second cycle, and the minute hand runs at twice the speed of a seconds hand.

The whole project takes only a small amount of coding, but a large amount of thought.

mmcham
08-17-2014, 03:02 PM
The true can be replaced.
Click on World and then Function tab to view additional Boolean Logic, and Math, options that are available to you for making decisions. Just drag the appropriate test in over the true, drop it, and modify to your liking.

As mentioned in the text, you need to consider what condition requires a recursive call, what must be done before making the call, and what to do when that condition becomes false.

Since this project isn't a real-time simulation, the pendulum swing doesn't have to be a 1-second cycle, and the minute hand runs at twice the speed of a seconds hand.

The whole project takes only a small amount of coding, but a large amount of thought.

Thanks. I've coded the pendulum swing, clock face movement, and cuckoo already and have them working. My hang up is the what function to make true and recursive.

mmcham
08-17-2014, 03:06 PM
Here's what I am working with. I am missing something really simple and it's driving me nuts. I have tried several functions from the world as true and can't figure it out.

MrMoke
08-17-2014, 06:29 PM
Which of the routines that you have written would be the most likely to be used as a recursive method?
Clocks chime when a specific event is reached. What's happening while it's waiting? What is the event?

Click and open the vid to view the clues:)

mmcham
08-17-2014, 07:40 PM
I was sort of in the ballpark a few times. I was trying to find a way to use the position of the hour or minute hand at the top of the hour for a recursion statement to start the animation of the cuckoo. I did not really think about the position of the cuckoo behind the clock! I think I have it now. Thanks for the help!

MrMoke
08-17-2014, 09:14 PM
It's not the position of the Cuckoo. Old-time clocks are just a bunch of sprocket wheels that control the speed and distance that the hands move with each cycle of the pendulum. 60 seconds= 1 minute, 60 minutes= 1 hour, ding-dong. In this case, I assume that seconds aren't counted, since it isn't in real-time. Watch the hands in the high-speed vid I posted to what I mean. Everything can be controlled by time.

I was sort of in the ballpark a few times. I was trying to find a way to use the position of the hour or minute hand at the top of the hour for a recursion statement to start the animation of the cuckoo. I did not really think about the position of the cuckoo behind the clock! I think I have it now. Thanks for the help!

chickentree
08-18-2014, 11:25 AM
Remember that recursion is expensive in terms of memory use. Why? Because the first call is open (taking up memory for variables and other house keeping) when it calls itself. The second call has take up memory etc when it calls itself and this continues until the recursive function either runs out of memory (blows up) or a condition is met which, instead of having the method call itself again, cause the method to return to its caller which follows the stack of opened methods until the program returns to the first call and finally ends.
Also keep in mind that recursive methods can perform actions either before calling themselves, when returning or after returning. This becomes very important as you learn more about programming later on.
So the question becomes what do you recurse on? In your case you have basically four things that you are manipulating: The pendulum, the hour hand, the minute hand and the cuckoo. But due to the wording of the problem, two of these items do not depend on the clock’s state changing, in other words they are independent of the clock, and two are dependant, the do depend on the clock being in a certain configuration. By “state” I mean anything to do with the clock, its subparts or variables. One of these independent variables is the pendulum. In this case the pendulum should swing back and forth continuously. So using recursion on the pendulum would probably result in Alice dyeing in a disastrous way.
At this point I think I might have said too much on the subject of what to recurse on. But remember that the recursive call can return a value to be used in further calculations.
So basically a recursive method might look something like this:

int recurse parameter
int result

do something before recursion // if needed
if you are not done //test if end criterion is met
result = recurse result // recurse: new result = recurse old result.
endif
do something after recursion // if needed
return result


To forget recursion you must first forget recursion!
Hopefully this is enough to get you started

Mark