Upload
meredith-craig
View
226
Download
4
Tags:
Embed Size (px)
Citation preview
Stepwise Refinement --A procedure calls another procedure
A curupira is playing soccer in the rainforest. The curupira has a soccer ball and will kick the soccer ball toward a pond in the forest. Each kick causes the soccer ball to move forward a random distance.
If the curupira gets the soccer ball in the water, the curupira does a dance, otherwise he disappears, shocked that he didn’t succeed.
Problem solving step 1 Download and open the world:
CurupiraSoccerSetup.a3p
Problem Solving step 2 Design an algorithm
Do in ordercurupira runs to soccer ballcurupira kicks the soccer ball a random distanceIf soccer ball is in the water,
curupira danceselse
curupira disappears
Implementation • translate algorithm into code, one line at a
time
Problem Solving step 3
Do in ordercurupira runs to the soccer ballcurupira kicks the soccer ball a random distance…
Do in ordercurupira runs to the soccer ballcurupira kicks the soccer ball a random distance…
Decomposition
runToObjectDo together
curupira performs running motioncurupira moves forward to the ball
Recall Decomposition -- take a complex procedure and break into smaller, simpler actions
runToObjectDo together
curupira performs a running motioncurupira moves forward distance to the <target>
Problem
There is no running motion procedure for the curupira
runToObjectDo together
curupira performs running motioncurupira moves forward to the ball
Stepwise refinement: create another procedure & call it from this procedure
Stepwise refinement
myFirstMethodDo in order
curupira runs to the soccer ballcurupira kicks the soccer ball a random distance…
runningMotion
runningMotion
Algorithm
Do in order Do together
curupira right leg turns forward
curupira left leg turns backward
Do together
curupira right leg turns backward
curupira left leg turns forward
Do together
curupira right leg turns forward
curupira left leg turns backward
Video: 14. Create a procedure
This video illustrates the steps documented in slides 11 -12.
Select Curupira from the class menu in the code editor
Open Curupira class document
Select Add Procedure… and name it runningMotion
Declare a new procedure
Comment
By default, actions are for this (entire) curupira
Translating algorithm to code
Do in order Do together
curupira right leg turns forward
curupira left leg turns backward
Do together
curupira right leg turns backward
curupira left leg turns forward
Do together
curupira right leg turns forward
curupira left leg turns backward
This video illustrates slide 16
Video: 15. Selecting subpart joints
Click on the instance menu
Select an appropriate joint for the subpart In this example, select the hip because the leg
subpart turns at the hip
Select a joint in the skeleton
Now add the code Remember the Do Together!
Complete procedure
Working our way back up…
myFirstMethodDo in order
curupira runs to the soccer ballcurupira kicks the soccer ball a random
distance…
runToObject
Do togethercurupira performs running motioncurupira moves forward distance to the <target>
runningMotionDo together
curupira right leg turns forwardcurupira left leg turns backward
…
We have completed
runningMotion procedure
Create runToObject
procedure and add call to
runningMotion
1. Click on Curupira tab2. Click on Add Procedure… 3. Name it runToObject
Implement runToObject
1
2
3
Class panel
Add comment
Call runningMotion
Return to myFirstMethod in the Scene class
Test runToObject
Test – call from myFirstMethod
myFirstMethodDo in order
curupira runs to the soccer ballcurupira kicks the soccer ball a random
distance…
runToObject
Do togethercurupira performs running motioncurupira moves forward distance to the <target>
runningMotionDo together
curupira right leg turns forwardcurupira left leg turns backward
…
Testing: Call runToObject from myFirstMethod
Call runToObject in myFirstMethod
… we need to call it somewhere in our program1. Select curupira in the instance menu2. Drag the runToObject tile into myFirstMethod in the
editor3. Click on Run
After we have defined the new procedure, runToObject…
1. 2.
3.