23
Stepwise Refinement -- A procedure calls another procedure

Stepwise Refinement -- A procedure calls another procedure

Embed Size (px)

Citation preview

Page 1: Stepwise Refinement -- A procedure calls another procedure

Stepwise Refinement --A procedure calls another procedure

Page 2: 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.

Page 3: Stepwise Refinement -- A procedure calls another procedure

Problem solving step 1 Download and open the world:

CurupiraSoccerSetup.a3p

Page 4: Stepwise Refinement -- A procedure calls another procedure

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

Page 5: Stepwise Refinement -- A procedure calls another procedure

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…

Page 6: Stepwise Refinement -- A procedure calls another procedure

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

Page 7: Stepwise Refinement -- A procedure calls another procedure

runToObjectDo together

curupira performs a running motioncurupira moves forward distance to the <target>

Problem

There is no running motion procedure for the curupira

Page 8: Stepwise Refinement -- A procedure calls another procedure

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

Page 9: Stepwise Refinement -- A procedure calls another procedure

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

Page 10: Stepwise Refinement -- A procedure calls another procedure

Video: 14. Create a procedure

This video illustrates the steps documented in slides 11 -12.

Page 11: Stepwise Refinement -- A procedure calls another procedure

Select Curupira from the class menu in the code editor

Open Curupira class document

Page 12: Stepwise Refinement -- A procedure calls another procedure

Select Add Procedure… and name it runningMotion

Declare a new procedure

Page 13: Stepwise Refinement -- A procedure calls another procedure

Comment

Page 14: Stepwise Refinement -- A procedure calls another procedure

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

Page 15: Stepwise Refinement -- A procedure calls another procedure

This video illustrates slide 16

Video: 15. Selecting subpart joints

Page 16: Stepwise Refinement -- A procedure calls another procedure

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

Page 17: Stepwise Refinement -- A procedure calls another procedure

Now add the code Remember the Do Together!

Complete procedure

Page 18: Stepwise Refinement -- A procedure calls another 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

Page 19: Stepwise Refinement -- A procedure calls another procedure

1. Click on Curupira tab2. Click on Add Procedure… 3. Name it runToObject

Implement runToObject

1

2

3

Class panel

Page 20: Stepwise Refinement -- A procedure calls another procedure

Add comment

Call runningMotion

Page 21: Stepwise Refinement -- A procedure calls another procedure

Return to myFirstMethod in the Scene class

Test runToObject

Page 22: Stepwise Refinement -- A procedure calls another procedure

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

Page 23: Stepwise Refinement -- A procedure calls another procedure

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.