[Tutor] An interesting web site for those who want to program adventure games

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun May 4 19:03:02 2003


Hi everyone,


I just ran into the following link; it's a tutorial on writing adventure
games (circa 1983):

    http://www.atariarchives.org/adventure

Would anyone be interested in "porting" the examples over from BASIC to
Python?  It might make a fun project for us here on Tutor; a few of us
appear to have been exposed to Basic in our past lives... *grin* Any
takers?



For example, in Chapter 4, the following bit of code:

###  BASIC code
10  DIM A(5,4)
20  FOR B = 1 TO 5
30  FOR C = 1 TO 4
40  READ A(B,C)
50  NEXT C
60  NEXT B
70  DATA 0,2,3,0
80  DATA 1,0,5,0
90  DATA 0,4,0,1
100 DATA 3,5,0,0
110 DATA 4,0,0,2
###

in BASIC defines an 5x4 array called 'A' to hold information about how to
move around in this adventure room.  In essence, it's defining the map of
our world.


We can do a port of this in Python:

###
"""A defines the exits from each room [N, S, E, W].
"""
A = [ [0, 2, 3, 0],   ## Room 1
      [1, 0, 5, 0],   ## Room 2
      [0, 4, 0, 1],   ## Room 3
      [3, 5, 0, 0],   ## Room 4
      [4, 0, 0, 2]    ## Room 5
    ]
###

Say that we're in room 5.  Then we can do a lookup into our 'A' list to
see what exits are available to us:

    n_room, s_room, e_room, w_room = A[4]

... but this may need some adjustment here to make things work
conveniently, since in Basic, arrays were indexed starting from 1, but in
Python, we start doing counting from 0.  Without some adjustments, we will
end up having to do error-prone conversions between 0-indexing and
1-indexing in our head.  To clarify the problem, if we want to move north
from room 5, we end up doing things like:

    current_location = 5         ## We're in room 5
    n_room, s_room, e_room, w_room = A[current_location-1]
    current_location =  n_room   ## We move north...

and that second statement, doing a subtraction of our current_location,
just looks a bit odd: it's there because the room numbers start from 1,
but our Python arrays start counting from 0, so we do the decrement to
compensate.  But it's messy.


We can fix this.  If we add an extra "sentinel room", the code ends up
looking nicer.  That is, if we have:

###
A = [ [0, 0, 0, 0]    ## Sentinel room --- no such room exists
      [0, 2, 3, 0]    ## Room 1
      [1, 0, 5, 0],   ## Room 2
      [0, 4, 0, 1],   ## Room 3
      [3, 5, 0, 0],   ## Room 4
      [4, 0, 0, 2]    ## Room 5
    ]
###

Then with the addition of that magical sentinel room, A[5] now really does
refer to Room 5, and we don't need to do anything tricky to jump from one
room to another:

    current_location = 5         ## We're in room 5
    n_room, s_room, e_room, w_room = A[current_location]
    current_location =  n_room   ## We move north...


Talk to you later!