The (Unofficial) Official HW#4A Walkthrough. Abridged.

Oh wow! It's a bootleg copy of the HW#4A walkthrough! Man.. this will really help with that homework assignment. I'll just follow along with the walkthrough. If I do everything step by step with it (and think about what I'm doing as I go), I should understand the code way better.

Playing Around

I'll kick off by ignoring the code altogether and just running the skeleton code. It compiles... it runs... it doesn't seem to do much. Wait, let me hit a key.

Aha! It started a level after a couple of key presses. OK, I have a square with a little red dot. When I type on the numeric keypad (the 1 2 3 and 4 5 6 and 7 8 9 thing), the square moves around. Plus, the little red dot seems to point wherever I'm moving (although it seems to start off in a wierd spot.. why is that?).

Hey! I can probably keep running the program as I build my solution as long as I'm careful to do it piece by piece! That should make debugging way easier!

Getting Started

The Main Vein

OK, let's look at some more of this main. For now, though, we'll just assume any function that's called does about what we expect it to do if we can.

The call graph

You should go and make a static call graph at this point. I'll do mine and then come back. It's pretty easy: it just involves looking in the code and seeing which function calls which other function.

Filling in one step

Hey, when I was making that static call graph, I found out that a bunch of functions aren't implemented! They just say "TODO". That must be (some of?) the stuff that we need to do on this assignment. Plus, there's some functions that aren't called at all. I bet some of those are things that I can use to implement the TODO stuff!

I'm going to try filling in the very first TODO I found: startLevel.

According to the directions:

At the start of each level, [I should] clear the screen, place the hero in the middle of the screen, and randomly place 1 enemy on the screen.

From my call graph I know that's called from three different places! handleInput calls it (looks like when the level ends), startGame calls it, and updateScreen calls it (when the player is hit by an enemy? I'm not sure I get that.. later!). Anyway, the good news is as long as I do what the function descriptions says I need to do, I shouldn't have to worry about where it's called from or how it's used! It'll just work because it keeps its contract. I love functional decomposition!

OK, I've read the function comment, and it makes sense. (Although.. why does it single out the level number as important?) The TODO comment in the function repeats everything the instructions said to do and also says to "set the bullet's status appropriately". I'll ignore that for now, but I'll make sure not to delete that part of the TODO so that I know I haven't done it! OK, here goes nothing!

I finished that. So, I'll take "the player's position" out of the TODO comment.

A few more steps

OK, let's polish this function off (except maybe that "bullet status" part). OK, I'll just create the enemy now. I checked out all the functions around here when I made my call graph.. so, I know there's a terribly useful function for this just a few lines down from startLevel. I'll call that to create my enemy. Then, I'll rerun it to see what happens.

That is so cool! The game actually works now (sort of). And, I've taken care of everything in startLevel's TODO list except setting the bullet status. I bet I can take that to.

Warning: descending into madness. Hey, I can finish this. I can do it right now! I'll do Part B today, too. And, I think I'll go ahead and ace the midterm but take only ten minutes of the period on it as well. Plus, I think I will polish Steve's bike!


P.S. I (Steve) wrote this walkthrough just as I did it. The steps were really the steps I took (although I admit I threw in the playerX = 0 error just for fun). Having done so, I want to say that the code is some of the most tear-jerkingly beautiful stuff I've ever seen. Congrats to Luke, David Du., Matt, and Hannah for producing such a masterwork!

P.P.S. If you're curious about the answers to any of the questions in this walkthrough, maybe you should post to the newsgroup about them. Remember to know what's cheating and what isn't first, though! No assignment code on the newsgroup!