Fundaments of Game Design Recursion Richard Gesick
Objectives Simple single recursion Multiple recursion Non-linear recursion Object based recursion
Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only the base case(s). Each method call divides the problem into two conceptual pieces: a piece that the method knows how to do and a recursive call, or recursion step that solves a smaller problem. A sequence of returns ensues until the original method call returns the result to the caller.
Activation Stack We use the concept of the activation stack to keep track of what memory and instructions "live" inside of each method. When a method is invoked, a new frame for it is placed atop the activation stack. When a method is finished, the frame is removed and control is returned to the next frame down.
Recursive Technique Design First: Determine the base case, ie, what is the stopping point for the recursion. It should normally be the simplest case. Second: What is the case that is just one step above it? Can it be generalized enough to fit?
Common Programming Error Either omitting the base case or writing the recursion step incorrectly so that it does not converge on the base case will cause infinite recursion, eventually exhausting memory. This error is analogous to the problem of an infinite loop in an iterative (non-recursive) solution.
Stack Overflow Avoid stack overflow. Stack overflow occurs when you recurse too many times and run out of memory. Often, it is more efficient to perform calculations via iteration (looping), but it can also be easier to express an algorithm via recursion. Recursion is especially useful for non-linear situations
Advanced Recursion Sometimes recursion involves processing a collection. While it could be done using 'foreach' or some other iterative approach, recall that recursion works best for non-linear structures. Recursion is also useful for making use of the activation stack as a "reminder" of where we've been and what remains to be done.
Non-linear Recursion Folders and files within a computer system are an excellent example of a non-linear structure that is easily processed via recursion. This is also possible using iteration, but we would have more coding work to do in handling where we've been in the file system. We can leverage the activation stack to remember for us.
Non-linear Recursion Consider the steps to count the total number of files: If we're in a folder with no files (or subfolders), then we return 0 Otherwise, we return the count of the files in the folder + all the files in the subfolders It's the second part that's tricky... but notice that's where the recursive call comes in. We just need to recursively call for each subfolder (so this is recursion wrapped inside of iteration):
solution private int CountFiles(DirectoryInfo di) { int file_count = 0; file_count += di.GetFiles().Length; foreach (DirectoryInfo sub_folder in di.GetDirectories()) file_count += CountFiles(sub_folder); return file_count; }
Is this recursion? class Box { Box[] arrayOfBoxes; int boxID; void Print () { Console.Writeline (“Box ”+ID+“ is here!”); foreach (Box b in arrayOfBoxes) { b.Print(); }