Hello World!

Hello World!

By Jeff Kazmierski, Copy Editor

 

So I was planning on taking a break from writing about code this summer, but then I thought, why?  As in, I’m still taking classes; why should anyone else get a break?  So here we are, back again with another installment of Hello World!

During the spring semester we covered a lot of ground, from the basics of input and output, command and control structures, to functions and program design.  This summer we’re going to delve into some more intermediate-level concepts that will enable you to develop longer and more complex programs.  Instead of looking at a variety of different programs, we’ll be developing one program over the summer, adding features every week and taking it from initial concept to full development.

The program we’ll be developing is one of the oldest games in computer game history.  In its first version, it was called “Hunt the Wumpus.”  You can call the monster whatever you like, but the concept is simple: the player is exploring a cave that has 20 rooms.  In one of those rooms is a monster.  Another room is the exit. If the player meets the monster, he gets eaten.  If he finds the exit, he wins.  Simple, right?

In later versions, the game added a bow and arrow, which the player could shoot into nearby rooms, and in others, there were three monsters and maybe a bottomless pit. Some included rooms full of bats, which would cause the player to lose a turn, and in many versions the monster moved at each turn as well.  We’ll start simply with a basic game.

Fire up your editor, let’s write some code!

Wait a minute, though.  Before we get to work on any program, we need to make some plans first.  Some basic questions need to be asked, and even though this is Python and we like to keep things loose and free, it’s still nice to know what pieces of information we’ll need to track and what questions the program will need to answer.

For a start, we’ll use these variables:

●      player_loc – the room the player is in now, starting with room 1

●      monster_loc – the room the monster is in, chosen randomly from 2 to 20

●      exit_loc – a random number from 10 to 20

We can add others as we need them, in the usual Python way, but these are what we’ll need to track basic game information.

 

The processes that need to be coded, at least for now, are:

●      starting the game – placing the player, monster and exit

●      getting the player’s next move

●      warning the player when he’s near the monster or exit

●      ending the game

For our first version of the program we’ll just code it simply, without using functions (I’ll assume you haven’t forgotten everything you learned in the spring…).

 

For our setup, we need to initialize our three variables with starting values.  We’ll place the player in room 1, the exit in a random location from 10 to 20, and the monster anywhere other than the player or exit location.

 

#  setup

import random

player_loc = 1                          # player starts at the entrance

exit_loc = random.randint(10, 20)       # place the exit

monster_loc = exit_loc                  # place the monster

while monster_loc == exit_loc:

monster_loc = random.randint(2, 20) # but not at the exit

 

Line 2 is very important; it brings in a Python module that allows us to create random numbers.  Lines 3 and 4 are pretty self-explanatory.  In line 5, we set monster_loc the same as exit_loc; because we don’t want the monster to be in the same place as the exit, the loop at lines 6 and 7 will make sure that doesn’t happen.

 

Next, we need a routine to get the player’s next move.

 

while player_loc != exit_loc:

print(“You are in room %d” % player_loc)

move = 0

while move < 1 or move > 20:

move = int(input(“Where would you like to go (1 – 20)? “))

player_loc = move

 

This segment is pretty simple.  The game code will all be wrapped up inside the while loop at line 1, which says that as long as the player hasn’t found the exit, the game will continue.  We set a variable move to 0 in line 3, then ask for player input for as long as it takes him to type in a valid number (in this case, 1 to 20).

The second line probably needs some explanation, since I don’t think we discussed formatted output very much.

I could go into a long, technical discussion about Python’s print() statement being a function that takes a variable number of arguments, blah blah blah, but we don’t want to do that, do we?  No, we don’t (if you really want to know, you can look it up at python.org).  So in a nutshell, the %d in the print(“You are in room %d” % player_loc)) line tells Python to look for a variable name, in this case player_loc, and replace the %d with the value held by that variable.  This is common to all modern programming languages including C, Java and Perl.  There are many others, and all serve different purposes, but here are the ones we’ll be using fairly often:

%d – prints an integer value (1, 2, 3, 4…)

%f – prints a floating point value (1.02, 2.5, 3.14159…)

%c – prints a single character (A, B, c, !)

%s – prints a string of characters (“a string of characters”)

To use them in Python, just follow the format we’ve already shown you, and make sure the variable name you use a) exists, and b) has the right type of data.

 

The next part is determining how the game ends.  Either the player finds the exit, or he gets eaten:

 

if player_loc == monster_loc:

print(“You were eaten by a monster!”)

break;

if player_loc == exit_loc:

print(“You escaped!”)

break;

 

And we’re almost done.  Notice the indentation of this code.  That means it’s still inside the while loop.  Here’s what your program should look like by now:

 

#  setup

import random

player_loc = 1                          # player starts at the entrance

exit_loc = random.randint(10, 20)       # place the exit

monster_loc = exit_loc                  # place the monster

while monster_loc == exit_loc:

monster_loc = random.randint(2, 20) # but not at the exit

while player_loc != exit_loc:

print(“You are in room %d” % player_loc)

move = 0

while move < 1 or move > 20:

move = int(input(“Where would you like to go (1 – 20)? “))

player_loc = move

if player_loc == monster_loc:

print(“You were eaten by a monster!”)

break;

if player_loc == exit_loc:

print(“You escaped!”)

break;

 

If you ran it right now, it would work, but it wouldn’t be much fun.  You’d basically be blundering about in the dark until you either found the door or got eaten (by accident).  To make it more interesting, we’re going to add some code after the “You are in room %d” line:

 

if player_loc + 1 == monster_loc or player_loc – 1 == monster_loc:

print(“You smell a monster!”)

if player_loc + 1 == exit_loc or player_loc – 1 == exit_loc:

print(“You feel a breeze!”)

 

This bit adds an “early warning system” to the game letting the player know when he’s close to the exit or the monster, and making it a little less random.

Of course, if you want, you can add an introduction explaining what the game is all about.  The full code includes such an intro (see sidebar or link).

That’s the basic code for one of the oldest computer games in computing history.  Next time we’ll modify it some more and make it a little more interesting.  While you’re waiting, why not do your own mods?  Expand the warning system, or limit the player’s movement options.

Good luck and good programming!

 

Sidebar

 

#! usr/bin/python3.2

#  hunt10.py

#  A dungeon crawl program in the spirit of Wumpus, Adventure, Dungeon and

#  many other popular text-based games from the 1970’s.

 

#  imports

import random

 

# introduction

print(“tMonster Hunt 1.0″)

print(“You are exploring a cave made up of 20 rooms.  Somewhere in the cave”)

print(“is a monster.  One of the rooms is an exit.”)

print(“At each turn you can move to any of the rooms in the cave.”)

print(“nIf you get close to the monster, the game will warn you:”)

print(“t”You smell a monster!””)

print(“nIf you get close to the exit, the game will let you know:”)

print(“t”You feel a breeze!””)

print(“nIf you wander into the room with the monster, you’ll be eaten!”)

print(“t”You were eaten by a monster!””)

print(“nYou can move to any room.”)

 

#  setup

player_loc = 1                          # player starts at the entrance

exit_loc = random.randint(10, 20)       # place the exit

monster_loc = exit_loc                  # place the monster

while monster_loc == exit_loc:

monster_loc = random.randint(2, 20) # but not at the exit

 

#  the game starts

while player_loc != exit_loc:

# show the player’s current location…

print(“You are in room %d” % player_loc)

# if monster is nearby…

if player_loc + 1 == monster_loc or player_loc – 1 == monster_loc:

print(“You smell a monster!”)

# if the exit is nearby…

if player_loc + 1 == exit_loc or player_loc – 1 == exit_loc:

print(“You feel a breeze!”)

# get the player’s move…

move = 0

while move < 1 or move > 20:

move = int(input(“Where would you like to go (1 – 20)? “))

player_loc = move

# check to see if the game is finished…

if player_loc == monster_loc:

print(“You were eaten by a monster!”)

break

if player_loc == exit_loc:

print(“You escaped!”)

break

Comments

comments