Recursion - ComSciGate

Recursion
Alice
Repetition
In some situations, we don’t know exactly
how many times a block of instructions
should be repeated.
All we know is that repetition is needed
For example, in a board game like chess or
checkers, we don’t know exactly how many
moves it will take for a player to win or lose
the game – all we know is that several
moves will be needed.
Indefinite Repetition
In programs where a count of repetitions is not
known (indefinite), we can use one of two
repetition control mechanisms:
Recursion
While statement
This session focuses on Recursion.
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
question) calls itself.
Example – horse race
A carnival style horse race.
In repeated moves, one
horse is randomly selected
to move forward. The
selected horse “runs”
straight ahead to the finish
line.
A horse is the winner if it
gets to the finish line before
any other horse. When one
horse wins, the game ends.
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
where recursion occurs.
Question
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 itself.
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?
moveRandomHorseForward
whichHorseWon?
isGameOver
To determine whether the game is over, ask the question:
Is the finish line < 0.5 meters (arbitrary distance) in front of a horse?
moveRandomHorseForward
To randomly choose a horse and move it forward, use the built-in
random selection question.
whichHorseWon
To determine the winner, we ask the question:
"Which horse is within 0.5 meters of the finish line?"
Putting together the pieces
Recursive call.
Note: The winning horse says "I won!!!"
-- a lame ending, we know.
Demo!
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 7 times
racehorse2 won 3 times
racehorse3 won 10 times
Something is wrong! Each horse
should win approximately 1/3 of the time.
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.
Revised
moveRandomHorseForward
The corrected code:
Assignment
Read Chapter 8-1, Recursion
Lab 8-1