Top College News Subscribe to the Newsletter

Hello World! Function Junction

Copy Editor

Published: Wednesday, July 18, 2012

Updated: Wednesday, July 18, 2012 19:07


Hello World! Function Junction

Jeff Kazmierski

Copy Editor

Sunburn and heatstroke and dehydration, oh my! Sunburn and heatstroke and dehydration, oh my! With temperatures soaring above 100, and no relief in sight, that's what you have to look forward to if you're brave or crazy enough to venture forth from your air-conditioned hovel this summer. I'm back with another installment of Hello World!, designed especially to keep you safe and healthy. Put away the suntan lotion; what you really need is to open up a well-chilled bottle of your favorite libation, fire up the development environment of your choice, crank up the AC and start coding. I hope you didn't lose any fingers during your Independence Day revelries, 'cause you're going to need them!

Last week we expanded our Monster Hunt program a little, by limiting the player's movements and giving the monster the ability to move. This week is all about preparation. Our program is going to get a lot more complicated, as we'll see in the future columns, so we're going to get ready for that by encoding all of its major functions in, well, functions.

Recall from Spring that in Python, functions are identified by the keyword 'def,' the function name, and a colon. All the code indented after the function definition is part of the function. In C-type languages like Java, Perl and C++, functions or methods are encased in curly braces ( {} ). Python, on the other hand, relies on formatting and indentation. This makes the code much easier to read.

But you already know that. Moving along...

To convert our program from its linear format to a function-based one requires we take a look at what it's doing and think about where it can be broken apart. If we look at last week's version, we can see easily that there are six basic functions:

1. it tells the player where he can move

2. it tells the player if the monster is near

3. it tells the player if the exit is near

4. it gets the player's move

5. it gets the monster's moves, then moves the monster

6. it determines if the game is over

We won't spend a lot of time going over the code line by line and explaining it. Instead, we'll look closely at a few specific sections and examine how they can be converted to modular code. For example, if we look at last week's program, specifically the code that enables the player and monster to move, we see they're pretty much the same:

# show available player moves

player_moves = []

for a in range(-2, 3):

next_room = player_loc + a

if next_room < 1 or next_room > 20:

continue

else:

player_moves.append(next_room)

# get the monster's moves...

monster_moves = []

for a in range(-2, 3):

next_room = monster_loc + a

if next_room < 1 or next_room > 20:

Recommended: Articles that may interest you

Be the first to comment on this article!





log out