210 likes | 259 Views
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. Repetition. repetition definite, counted -> loop
E N D
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 Introduction to Recursion
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 Introduction to Recursion
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 Introduction to 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 function) calls itself. Introduction to Recursion
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 Example – horse race Introduction to Recursion
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? Introduction to Recursion
Do everything again? • How do we implement “do everything again” ? • Create a call to the race method itself. • Recursion means that a method calls a copy of 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 Introduction to Recursion
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 Introduction to Recursion
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 Introduction to Recursion
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 Introduction to Recursion
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 Introduction to Recursion
First Attempt Introduction to Recursion
race Method • uses recursion • where is the “way out?” Introduction to Recursion
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. Introduction to Recursion
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. Introduction to Recursion
Probability Tree 1/3 of the time Select race horse 1 2/3 s of the time Don’t select race horse 1 2/3s of the time Select race horse 1 1/3 of the time Select race horse 2 1/3 of timerace horse 1moves 2/9 s of timerace horse 2moves 4/9 s of timerace horse 2moves Introduction to Recursion
Correct Version of move Random Horse Forward Introduction to Recursion
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 Introduction to Recursion
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? Introduction to Recursion