#!/usr/bin/python ## ## Simple web game by William D. Colburn ## http://infohost.nmt.edu/~schlake/ ## 505 480 9406 ## schlake@nmt.edu ## import cgi import cgitb import os import sys import random def getField( field ): if form.has_key( field ): try: return int(form[field].value) except: return form[field].value return None cgitb.enable() print 'Content-Type: text/html\n' form = cgi.FieldStorage() cmd = getField( 'cmd' ) loc = getField( 'loc' ) from rooms import rooms def showRoom( loc ): print 'showRoom()' print rooms[loc].description print '
' def getCommand( loc ): print """
""" % loc def parseCommand( loc, cmd ): dirs = { 'north':['n', 'north'], 'south': ['s', 'south'], 'east':['e', 'easy'], 'west':['w', 'west'], 'up':['u', 'up'], 'down':['d','down'] } cmd = cmd.lower() for dir in dirs: if cmd in dirs[dir]: if dir in rooms[loc].exits: print 'You go %s.' % dir return rooms[loc].exits[dir] else: print 'You cannot go %s from here.
' % dir return None print 'I did not understand "%s".
' % cmd return None print 'cmd', cmd, cmd == None, '
' print 'loc', loc, loc == None, '
' if cmd == None or loc == None: print 'New Game because', cmd, loc, '
' loc = 'origin' elif cmd != None and loc != None: print 'Move because', cmd, loc, '
' else: print 'Internal Error', cmd, loc sys.exit( 1 ) showRoom( loc ) if cmd: newloc = parseCommand(loc, cmd ) if newloc: loc = newloc showRoom( loc ) getCommand( loc )