I have problems with creating the classic game Wumpus. the file: http://esnips.c

mensanator at aol.com mensanator at aol.com
Wed Apr 26 20:32:59 EDT 2006


conny.ledin at gmail.com wrote:
> i just mailed it to you. Thanks for looking at it.

Returning.

Comments added by me start #m.

I tried to keep all your original lines commented out and
added my fixes with #m on either side, but there were
so many errors I had to give up. I tried to preserve the
code of the first mistake and simply deleted multiple
repetitions of the same mistake.

Note I made a couple structural changes, move some
variables and deleted others. This in addition to fixing
the goofy errors you made trying to move to the
next room.

Movement now works correctly (I think, did not verify
the map, it doesn't allow you to go back the way you
came). In addition to now correctly printing the room
number, I added the room contents in square brackets.
You'll need to know that in order to handle the traps.

You've got a lot of code still commented out, better
get moving.

# -*- coding: cp1252 -*-
#Belzebub
#Conny Ledin

import random
from Tkinter import *

#creates a room class
class room(object):#,trap):
    def __init__(self, trap):
        self.trap = trap
    #inserts content in rooms
    #m
    #m why do you need get_trap()?
    #m room_list[current_room].trap will return the trap content
    #m
    def get_trap():
        return trap

#creates a room list with content
room_list = []
#0=Nothing, 1=fire trap, 2=acid trap, 3=teleporter, 4=belzebub
trap_list = [0,0,0,0,0,0,0,0,0,1,1,2,2,3,3,3,3,3,3,4]
random.shuffle(trap_list)
for i in range(0,20):
    r = room(trap_list[i]) #crates a room object whith content (trap)
    room_list.append(r)

#creates a list with rooms
nextto_list1 = range(0,20)

#shuffles the list with rooms for movement in east/west direction
random.shuffle(nextto_list1)

#shuffles another list with rooms for movement in north/south direction
#m
#m this line doesn't work, nextt0_list2==None
#m nextto_list2 = random.shuffle (nextto_list1)
#m
nextto_list2 = range(0,20)
random.shuffle(nextto_list2)
#m


#m placed here, current_room is a global variable
current_room = 0
#m

root = Tk()

#creates GUI
class application(Frame):

    def __init__(self, master):
        #creates frame
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        Label(self,text = "Whats your next move?").grid(row = 0, column
= 0, sticky = W)
        Label(self, text = "Choose one: ").grid(row = 1, column = 0,
sticky = W)
        self.choice = StringVar()
        #creates text box
        self.results_txt = Text(self, width = 60, height = 10, wrap =
WORD)
        self.results_txt.grid(row = 7, column = 0, columnspan = 4)
        #creates radiobutton for north
        Radiobutton(self, text = "Go North",
                    variable = self.choice,
                    value = "north",
                    command = self.move
                    ).grid(row = 2, column = 0, sticky = W)
        #creates radiobutton for east
        Radiobutton(self, text = "Go East",
                    variable = self.choice,
                    value = "east",
                    command = self.move
                    ).grid(row = 3, column = 0, sticky = W)
        #creates radiobutton for south
        Radiobutton(self, text = "Go South",
                    variable = self.choice,
                    value = "south",
                    command = self.move
                    ).grid(row = 4, column = 0, sticky = W)
        #creates radiobutton for west
        Radiobutton(self, text = "Go West",
                    variable = self.choice,
                    value = "west",
                    command = self.move
                    ).grid(row = 5, column = 0, sticky = W)
        #creates radiobutton for shooting
        Radiobutton(self, text = "Deploy bomb",
                    variable = self.choice,
                    value = "shoot",
                    command = self.shoot
                    ).grid(row = 6, column = 0, sticky = W)

    #creates a function for movement
    def move(self):
#m tell move() that current_room is a global and not local
#m  to this block
        global current_room
#m
        message = ""
        message += self.choice.get()
        print message
        if message == "north":
#m I modified next_room to pass the direction, you don't need it saved
#m          direction = 2
#m
#m next_room() returns the new room index, but you aren't saving it,
#m  so it's being lost
#m          next_room()
#m
#m ok, that's better, you're trying to save it, but what you
#m  forgot the (), so you're saving the location of the function,
#m  not calling it. note we can't make that mistake if we have to pass
#m  parameters to the function
#m          current_room = next_room
            current_room = next_room(current_room,2)
#m
            message = "You enter the next room to the north "
            message += str(current_room)
#m show what's in the room
            message += ' [' + str(room_list[current_room].trap) + ']'
#m
        if message == "south":
#m
            current_room = next_room(current_room,-2)
#m
            message = "You enter the next room to the south "
            message += str(current_room)
#m show what's in the room
            message += ' [' + str(room_list[current_room].trap) + ']'
#m
        elif message == "east":
#m
            current_room = next_room(current_room,1)
#m
            message = "You enter the next room to the east "
            message += str(current_room)
#m show what's in the room
            message += ' [' + str(room_list[current_room].trap) + ']'
#m
        elif message == "west":
#m
            current_room = next_room(current_room,-1)
#m
            message = "You enter the next room to the west "
            message += str(current_room)
#m show what's in the room
            message += ' [' + str(room_list[current_room].trap) + ']'
#m
        self.results_txt.delete(0.0, END)
        self.results_txt.insert(0.0, message)
        root.title("Chasing Belzebub")
    #creates a function for shooting
    def shoot(self):
        message = ""
        message += self.choice.get()
        print message
##      if message == "shoot":
##          belzebub.die()

#creates a randomized grid with rooms that the player can move trough
#m
#m changed to pass function current_room and desired direction
#m
def next_room(cr,d):#direction, current_room):
    #east/west
#m these need to be global?
#m   current_room = 1
#m   direction = 1      # no need for this at all
#m
#m
    if cr == 0 and d == -1:
#m next_room is the name of the function, use a different name for
variable
#m  otherwise you're returning the function address, not the result
#m  if current_room == 0 and direction == -1:
#m       next_room = nextto_list1[19]
         nr = nextto_list1[19]
    elif cr == 19 and d == 1:
         nr = nextto_list1[0]
    elif d == 1 or d == -1:
         nr = nextto_list1[cr + d]
    #north/south
    elif cr == 0 and d == -2:
         nr = nextto_list2[19]
    elif cr == 19 and d == 2:
         nr = nextto_list2[0]
    elif d == 2:
         nr = nextto_list2[cr + 1]
    elif d == -2:
         nr = nextto_list2[cr - 1]
#    return next_room
    return nr

###creates a player class
##class player(object):
##    def __init__(self):
##
##    #all the ways the player can die and GAME OVER displays
##    def die(self, fire_trap, acid_trap, belzebub):
##        if acid_trap.kill:
##            print "Im sorry to say that you have melted./n"
##            "Better luck next time/n"
##            "GAME OVER!"
##        elif fire_trap.kill:
##            print "Oh my! Your hair is on fire./n"
##            "GAME OVER!"
##        elif belzebub.kill:
##            print "Belzebub got you, have a lovely time in HELL!/n"
##            "GAME OVER"
##
###creates a fire trap class
##class fire_trap(object):
##    def __init__(self):
##    #ser till att spelaren dör
##    def kill(self, player):
##        player.die()
##
###creates a fire trap class
##class acid_trap(object):
##    def __init__(self):
##    #ser till att spelaren dör
##    def kill(self, player):
##        player.die()
##
###creates a Belzebub class
##class belzebub(object):
##    def __init__(self):
##    #ser till att spelaren dör
##    def kill(self, player):
##        player.die()
##    ##ser till att belzebub dör
##    def die(self, player):
##        if bomb
####
def main():

    app = application(root)
    root.mainloop()

##    for i in nextto_list1 or nextto_list2:
##        #skapa ett rum
        
main()




More information about the Python-list mailing list