Lecture notes (8-1)

Download Report

Transcript Lecture notes (8-1)

CS320n –Visual Programming
Introduction to Recursion
(Slides 8-1)
Thanks to Wanda Dann, Steve Cooper, and Susan Rodger
for slide ideas.
What We Will Do Today
• Look at the programming technique known
as recursion
Visual Programming
Introduction to Recursion
2
Repetition
• repetition
– definite, counted -> loop
– indefinite, as long as some condition is true -> while
• In some situations, we don’t know exactly how
many times a block of instructions should be
repeated.
– repeat until some condition is true
– the repetition of step may help get closer to the
condition being true
• Games with decisions such as checkers or
chess.
– don’t know how many moves it will take to reach the
end of the game or a stalemate
Visual Programming
Introduction to Recursion
3
Indefinite Repetition
• In programs where a count of repetitions is not
known (indefinite), we can use one of two
repetition control mechanisms:
– While statement, last time
– Recursion, today
Visual Programming
Introduction to Recursion
4
Recursion
• Many of the pieces we use to create a program
are identified by using special words. For
example,
–
–
–
–
Do in order
Do together
If/Else
Loop
• Recursion is not a program statement with a
special word that identifies it as part of the
programming language.
• Recursion means that a method (or a function)
calls itself.
Visual Programming
Introduction to Recursion
5
Example – horse race
• Horse race
• In repeated moves,
one horse is randomly
selected to move
forward.
• The selected horse
moves straight ahead
to the finish line.
• First horse to the
finish line wins
Visual Programming
Introduction to Recursion
6
Storyboard
race
If one of the horses has won
the winner says, “I won!!!”
Else
randomly choose one horse and move it forward a small amount
do everything again
• "do everything again" means that the
entire method should be repeated
• this is recursion
• could this be done with a while loop?
Visual Programming
Introduction to Recursion
7
Do everything again?
• How do we implement “do everything
again” ?
– Create a call to the race method itself.
race
If one of the horses has won
the winner says, “I won!!!”
Else
randomly choose one horse and move it forward a small amount
call the race method
– Recursion means that a method calls a copy
of itself.
Visual Programming
Introduction to Recursion
8
Stepwise Refinement
race
If one of the horses has won
the winner says, “I won!!!”
Else
randomly choose one horse and move it forward a small amount
call the race method
isGameOver?
whichHorseWon?
moveRandomHorseForward
Visual Programming
Introduction to Recursion
9
function is Game Over
• world level function
• returns a boolean
• is the finish line < 0.5 meters in front of
any horse?
– remember, center point not at the front
• if so, game is over
• return true
Visual Programming
Introduction to Recursion
10
which Horse Won
• world level function
• returns an object
• which horse is within 0.5 meters of finish
line?
– or which horse is closest to finish line
– or which horse is within winning distance
• return that horse object
Visual Programming
Introduction to Recursion
11
move Random Horse Forward
• world level method
• pick one of the horses at random and
move it forward between 0.05 and 0.15
meters
• how to pick a horse at random?
– use the world level function
Visual Programming
Introduction to Recursion
12
First Attempt
Visual Programming
Introduction to Recursion
13
race Method
• uses recursion
• where is the “way out?”
Visual Programming
Introduction to Recursion
14
Testing
• Testing a program that used random
numbers requires extra caution.
• In this example, we ran the program 20
times and found that
– racehorse1 won 3 times
– racehorse2 won 0 times
– racehorse3 won 17 times
• Something is wrong! Each horse
should win approximately 1/3 of the time.
Visual Programming
Introduction to Recursion
15
Removing the bug
• The bug in this code is that we have
– nested If statements, and
– we used a 33% probability for each If
statement
• What we didn't consider is that if
racehorse1 was not selected, then we
have a 50% probability of selecting
either racehorse2 or racehorse3.
Visual Programming
Introduction to Recursion
16
Probability Tree
1/3 of the time
Select race horse 1
1/3 of time
race horse 1
moves
2/3 s of the time
Don’t select race horse 1
1/3 of the time
Select race horse 2
2/9 s of time
race horse 2
moves
Visual Programming
Introduction to Recursion
2/3s of the time
Select race horse 1
4/9 s of time
race horse 2
moves
17
Correct Version of move Random
Horse Forward
Visual Programming
Introduction to Recursion
18
Retest
• Run Program 20 times
– racehorse1 won 6 times
– racehorse2 won 6 times
– racehorse3 won 11 times
• Appears problem is fixed
• Each horse should win approximately 1/3
of the time
• more than 20 tests to really check program
Visual Programming
Introduction to Recursion
19
Modifying the Horse Race
• Change the race so that all three horses
move a random distance at the same time
• easy to change with a Do Together block,
but what else needs to change?
Visual Programming
Introduction to Recursion
20