On 9/2/07, <b class="gmail_sendername">David Millar</b> &lt;<a href="mailto:davmillar@gmail.com">davmillar@gmail.com</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello. I&#39;m working on a text adventure game right now, and I seem to be kind of stuck. There&#39;s a huge chunk of code called moreaction() that pulls scripted events for certain locations. It&#39;s ever-changing so I&#39;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 
<a href="http://thegriddle.net/python/v006.txt" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://thegriddle.net/python/v006.txt</a> Any help is much appreciated :) - Dave M.</blockquote><div>
<br>I just took a look at&nbsp; 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&#39;s using multiple dictionaries to get you out of if-then hell. Let&#39;s say we have:
<br><br>sceneDict = {}&nbsp;&nbsp;&nbsp; <br># We populate these with the difference scene numbers. In the sceneDict, we have eventDict if there are any events.<br></div><br>Lets say we take your first piece of code:<br><pre>    if thescene == 3:
<br>        if event[1] == 0:<br>            global choins<br>            choins = choins - 10<br>            add_item(&quot;BEAN&quot;,1)<br>            map[2].add_opt(&quot;NORTH&quot;,4)<br>            map[2].set_desc(&quot;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.&quot;)
<br>            event[1] = 1<br>        elif event[1] == 1:<br>            map[3].set_desc(&quot;You walk up to the gangsters and the boss guy says &#39;Get lost, fool!&#39;&quot;)<br>            event[1] = 2<br>        elif event[1] == 3:
<br>            map[3].set_desc(&quot;You walk up to the gangsters but they tell you to get lost.&quot;)<br>            if (inventory.has_key(&quot;ARMONDO&#39;S NOTE&quot;) == 0):<br>                print &quot;\nYou walk up to the gangsters and flash a picture of Candy in front of them. &#39;Woah, is that Candy?&#39; the boss guy asks. I ain&#39;t seen her since high school!&#39; 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.&quot;
<br>                add_item(&quot;ARMONDO&#39;S NOTE&quot;,1)<br>        elif event[1] == 4:<br>            print &quot;\nYou see Candy with Armondo, and they wave you over. &#39;Hey, thanks for hooking us up again! And sorry Armondo took your choins in his little game, teehee!&#39; She hands you 5 choins. &#39;Uhh, he took 10 choins from me, not fi-&#39; &#39;SHUT UP RUBE!&#39; Candy laughs at Armondo and kisses him on the cheek. &#39;We&#39;re going to the back seat of Armondo&#39;s car for coffee. See ya! They walk away and get into Armondo&#39;s car, which starts bucking around a bit. Then it suddenly starts up and leaves, opening the street to the south.&quot;
<br>            choins += 5<br>            map[2].rem_opt(&quot;TALK&quot;)<br>            map[2].add_opt(&quot;SOUTH&quot;,15)<br>            map[1].add_opt(&quot;ORDER&quot;,16)<br>    elif thescene == 6:<br>        map[5].rem_opt(&quot;EAST&quot;)
<br>        add_item(&#39;MAIN MAP&#39;,1)<br>        add_recipe(&#39;ICED COFFEE&#39;,&#39;COFFEE&#39;,&#39;ICE&#39;,1)<br>        add_recipe(&#39;ESPRESSO&#39;,&#39;COFFEE&#39;,&#39;BEAN&#39;,2)<br>        add_recipe(&#39;CAPPUCCINO&#39;,&#39;ESPRESSO&#39;,&#39;MILK&#39;,2)
<br>        add_recipe(&#39;CREAMY COFFEE&#39;,&#39;COFFEE&#39;,&#39;MILK&#39;,1)<br>    elif thescene == 8:<br>        if event[1] &gt;= 3 and (book.has_key(&quot;SYRUP&quot;) == 0):<br>            print &quot;\n&#39;PROTIP!&#39; &#39;Huh?&#39; you respond. &#39;PROTIP! CANDY and WATER make various sugary SYRUPS to add to your drinks!&#39; Wow. Interesting.&quot;
<br>            add_recipe(&#39;SYRUP&#39;,&#39;CANDY&#39;,&#39;WATER&#39;,1)<br>        disp_menu(0)</pre><br>sceneDict = { 3:&quot;&quot; , 6:&quot;&quot;,8:&quot;&quot;}<br>sceneDict[3] = { 0:&quot;&quot;,1:&quot;&quot;,2:&quot;&quot;,4:&quot;&quot;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #&nbsp; The key 3 points to a dictionary of event dictionary.
<br><br>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. :)
<br><br>-Tino<br></div>