CompSci 4 - Classwork - Nov 1, 2005
5 pts

Today's classwork goes along with Chapter 7, Section 2. You will be using random variables and writing a while loop.

In today's world, you will be writing a loop to randomly move Cinderella and the handsome prince repeatedly until they finally meet and touch hands!

For the setup, they start touching hands and are in the "middle". There are actually hidden tennisballs at this position, one for each of them so you can tell when they are in the "middle". Both turn and move 10 units and turn back around. They are now 20 units from each other.

You will write code to move them both randomly forward 1, 2 or 3 units. Each can move up to 10 units forward.

Start by downloading the world willTheyMeet.a2w. Play this world.

You will write one function and one method. All the others have been written for you and do not need any modification.

  1. Write the function findRandInt. This function should return 1, 2 or 3, indicating how far a person should move. However, the person must not go past the middle! (You might want to consider modifying the function randomInt by adding a parameter to this function so that given the parameter, you can return 1, 2, ... up to the value of the parameter.)

  2. Write the method play. This method assumes the players are in the starting positions. It repeatedly moves each player forward 1 to 3 units at the same time, but not past the middle. If one reaches the middle and the other is not there, that one must start over. If they both reach the middle, then display a message and stop.

If you finish early, feel free to embelish your animations.