You may think that you have already been debugging and in a way you have, but you could have gotten through the previous two chapters without setting a single breakpoint because the programs are simple enough that it is unnecessarry. Games on the other hand consist of thousands of lines of code so it is easy to write a program that is complex enough that debugging without breakpoints becomes nearly impossible.

We are only one section away from learning how to create a computer game. At this point you have learned essentially everything you need to in order to create a game. You may be tempted to skip this section and start writing games right away. However, if you ever try to write your own game you will need some serious debugging skills. Complex programs can easily become nightmares to debug if you don't really know what you are doing. For this reason, each problem in this section contains a description of a single program that is riddled with errors. Some programs won't even compile, some fail during runtime, and some run fine but don't do what they are supposed to do. You can find the programs for each problem in the folder Unit1/Debugging.
Excercise 5-1.
  1. Open the project DebugProject1 in Unit1/Debugging. This program should print the key that is pressed and held to the screen for all A..Z. Compile this program. If it won't compile, then fix any errors.
  2. Now run this program and test it by pressing different letter keys (A, B, C, etc). The first step is to identify the bug before trying to fix it in code. What is the bug in this program?
  3. Now find the bug in the code and fix it.
Excercise 5-2.
  1. Open the project DebugProject2 in Unit1/Debugging. This program should be identical to the flower program we did in Section 3.10. Try compiling it. If it won't compile, then fix any errors.
  2. When you run the program it clearly does not draw flowers. There are actually 2 bugs preventing this. Since you know the problem is in drawing. Start by looking for errors in the DrawFlower subroutine and any subroutines it calls. These 2 bugs will be difficult to find, but really examine the logic of the code before looking at the solution. Although breakpoints probably won't help at this point, it might be useful to comment out the code in OnEnterFrame that draws flowers and just call some of the subroutines like PolarToXY to verify whether or not they are working as they should (i.e., try drawing a circle with it). Remember that (* *) comments can be used to block comment out multiple lines of code.
Excercise 5-3.
  1. Open the project DebugProject3 in Unit1/Debugging. This program should be similar to the fireworks program we did in Section 4.6. Try running it and identify what the error is.
  2. Fix the bug identified in the previous problem by setting a breakpoint and stepping through the code (F8).
  3. Fixing the previous problem introduces another (rather cool) bug. What is it?
  4. Fix the new bug by considering the code near the creation of new flares after the detonation of a flare.
Excercise 5-4.
  1. Open the project DebugProject4 in Unit1/Debugging. This program should be similar to the prime number exercise we did in Section 3.6. Try running it and identify what the error is.
  2. Set a breakpoint at the beginning of the main while statement. Step through the code and see what happens. Remember that F8 steps line by line and F7 steps into a subroutine. For example, try stepping until the following line is highlighted:
    if IsPrime(CandidateInteger) then
    Then step into the IsPrime subroutine using F7. Step through each line of the subroutine and observe the value in each variable. You can also use Run -> Evaluate/Modify from the main menu. This will allow you to evaluate expressions such as K mod X to see what the result is. Continue doing this until you identify the first problem.
  3. Once the previous bug is fixed run the program and see if everything works (i.e. whether or not the first 20 primes are listed).
  4. Repeat the process of setting a breakpoint as done previously and step through the while loop until 4 comes up as a CandidateInteger. Now step into IsPrime and try to determine what the problem is.
Excercise 5-5.
  1. Open the project DebugProject5 in Unit1/Debugging. This program is similar to the one that relates triangles, circles, and waves from Section 3.1. Try running it and identify what the error is.
  2. Remember the first step in debugging. Test the subroutines to make sure they work. Try commenting everything out (using parenthesis asterisk comments (* *) and playing with PolarToXY with values you know should work. PolarToXY, as it is written, takes a Theta value and Radius and outputs X and Y coordinates to the last two arguments passed to it. Since you know that 0 degrees is the X axis, passing a radius of 5 and a theta of 0 should return the coordinates X = 5 and Y = 0. Try it and see what the result is (use breakpoints to evaluate the contents of the output variables after the subroutine has finished running).
  3. After setting a breakpoint and examining OutX and OutY as suggested in the previous problem, can you identify the bug?
  4. Now that the first bug is fixed, uncomment the original code and try to run the program. You will notice that it still doesn't work. Try examining the call to PolarToXY to see if there are any obvious problems.
  5. After fixing the previous bug, you will notice that the program still does not work. Is there something else wrong with PolarToXY?
Excercise 5-6.
  1. Open the project DebugProject6 in Unit1/Debugging. This program simply creates a list of shapes and then writes their areas to the screen. This program has a number of compiler and design errors. Try compiling it to find the compiler errors. Attempt to fix all the compiler errors before looking at the solution.
  2. Although the program now compiles, it crashes with a runtime error (that in some cases even can crash the Lazarus IDE). Any ideas? Hint: Since the IDE does not help us out by locating the line on which the runtime error occurs, the best way to fix this is to comment out all the code between the begin-end block (using (* *) or { } comments) and then uncomment each line to see if it causes an error.
  3. Assuming you are still looking for the error from the previous problem, do what the solution says to do before looking at the actual solution.
  4. Fix one of the runtime errors by modifying the bounds of the for loop.
  5. After fixing the last runtime error, the program will work if both Shapes.Add(S) lines are commented out. Thus, we should look in the Square unit for the source of the original error.