[Tutor] Condensing Some Massive Code

Tino Dai tinoloc at gmail.com
Wed Sep 5 23:28:09 CEST 2007


On 9/2/07, David Millar <davmillar at gmail.com> wrote:
>
> Hello. I'm working on a text adventure game right now, and I seem to be
> kind of stuck. There's a huge chunk of code called moreaction() that pulls
> scripted events for certain locations. It's ever-changing so I'm not looking
> for specifics, but can anyone suggest good ways to clean up or condense the
> code without sacrificing clarity? The latest version of the source is found
> at http://thegriddle.net/python/v006.txt Any help is much appreciated :) -
> Dave M.


I just took a look at  the code, it seems that you are in if-then hell. I
think that I have a solution for you that improve the readability of the
code. It's using multiple dictionaries to get you out of if-then hell. Let's
say we have:

sceneDict = {}
# We populate these with the difference scene numbers. In the sceneDict, we
have eventDict if there are any events.

Lets say we take your first piece of code:

    if thescene == 3:
        if event[1] == 0:
            global choins
            choins = choins - 10
            add_item("BEAN",1)
            map[2].add_opt("NORTH",4)
            map[2].set_desc("You are just outside a nasty looking
movie theatre. Shady Latin gang members have a shell game set up
nearby, and from previous experiences you know to avoid gambling like
the plague.")
            event[1] = 1
        elif event[1] == 1:
            map[3].set_desc("You walk up to the gangsters and the boss
guy says 'Get lost, fool!'")
            event[1] = 2
        elif event[1] == 3:
            map[3].set_desc("You walk up to the gangsters but they
tell you to get lost.")
            if (inventory.has_key("ARMONDO'S NOTE") == 0):
                print "\nYou walk up to the gangsters and flash a
picture of Candy in front of them. 'Woah, is that Candy?' the boss guy
asks. I ain't seen her since high school!' He scribbles something on
the back of a receipt for frozen wonton burrito meals, and you do the
math and realize that he wants you to give candy the number."
                add_item("ARMONDO'S NOTE",1)
        elif event[1] == 4:
            print "\nYou see Candy with Armondo, and they wave you
over. 'Hey, thanks for hooking us up again! And sorry Armondo took
your choins in his little game, teehee!' She hands you 5 choins. 'Uhh,
he took 10 choins from me, not fi-' 'SHUT UP RUBE!' Candy laughs at
Armondo and kisses him on the cheek. 'We're going to the back seat of
Armondo's car for coffee. See ya! They walk away and get into
Armondo's car, which starts bucking around a bit. Then it suddenly
starts up and leaves, opening the street to the south."
            choins += 5
            map[2].rem_opt("TALK")
            map[2].add_opt("SOUTH",15)
            map[1].add_opt("ORDER",16)
    elif thescene == 6:
        map[5].rem_opt("EAST")
        add_item('MAIN MAP',1)
        add_recipe('ICED COFFEE','COFFEE','ICE',1)
        add_recipe('ESPRESSO','COFFEE','BEAN',2)
        add_recipe('CAPPUCCINO','ESPRESSO','MILK',2)
        add_recipe('CREAMY COFFEE','COFFEE','MILK',1)
    elif thescene == 8:
        if event[1] >= 3 and (book.has_key("SYRUP") == 0):
            print "\n'PROTIP!' 'Huh?' you respond. 'PROTIP! CANDY and
WATER make various sugary SYRUPS to add to your drinks!' Wow.
Interesting."
            add_recipe('SYRUP','CANDY','WATER',1)
        disp_menu(0)


sceneDict = { 3:"" , 6:"",8:""}
sceneDict[3] = { 0:"",1:"",2:"",4:""}      #  The key 3 points to a
dictionary of event dictionary.

I have to research how to get the values of these dictionary to be
executeable code :). Tell me what you think (by the way, you will need one
or two if statements and perhaps an try-except block to make this code
work). More tonight or if Kent, Alan or Bob want to step in. :)

-Tino
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070905/577cfd95/attachment-0001.htm 


More information about the Tutor mailing list