most recent 30 from softwareengineering.stackexchange.com 2025-05-01T12:46:33Z https://softwareengineering.stackexchange.com/feeds/tag?tagnames=data-structures python c%23 https://creativecommons.org/licenses/by-sa/4.0/rdf https://softwareengineering.stackexchange.com/q/431348 4 johnnymcmike https://softwareengineering.stackexchange.com/users/400889 2021-08-25T06:24:00Z 2021-08-25T15:47:57Z <p>so for the past several months, I've been working on a personal project. It's a comically large, highly open, handcrafted text adventure game packed with inside jokes and fun little surprises for my friends to play around with and experience. I've got a few barebones prototypes working, and they've gotten a lot of &quot;oh cools&quot; and laughs.</p> <p>I've been coding it from the ground up in Python. I recognize that using some engine or framework or language specifically built for this purpose would be far easier, but I'm dead-set on homebaking this myself. This is mainly for learning purposes, but also so that I can do increasingly cool things that nobody expects a simple little text adventure game to be able to do (such as send desktop notifications, or read the current OS username, or play music, or understand synonyms in commands, or type things out to the console gradually like a human would.)</p> <p>Recently, I've abandoned the Python version and started rewriting it in C# because its purpose as a learning tool has run out, and also because I coded it terribly. I've been using giant JSON dictionaries as essentially glorified piles of global variables, storing things like what doors the player has entered and what choices they make, so that the main script (as well as a module I created for some shortcuts involving typing and asking questions) can access them.</p> <p>What's more, the main file is one huge mess of branching <code>if</code> statements containing scenarios every possible way that the story could have unfolded. This quickly became unreadable and unmaintainable and it completely killed my motivation to work on the project for a long time.</p> <p><strong>My question is this:</strong> In general terms, what's a more elegant and structured way of doing these things, particularly the if statement part? Doing it the way that I am now feels like such a naive and primitive workaround, and I feel like there <em>has</em> to be a more sensible and scalable way to organize this kind of project.</p> <p><strong>EDIT: Some code samples to demonstrate the problem. Be warned, it's bad!</strong></p> <pre><code>while True: choices.askchoice(&quot;1-entrance&quot;, [&quot;yes=enteredCave&quot;,&quot;go=enteredCave&quot;], speed = 3) if save[&quot;enteredCave&quot;]: break else: choices.askchoice(&quot;2-rejectCave&quot;, [&quot;yes=ending-abandonment&quot;, &quot;no=enteredCave&quot;]) ..etc </code></pre> <p>As you can see, I'm using an &quot;askchoice&quot; method (static of a &quot;choices&quot; custom object created at the start of the file) to clean up the process of printing the prompt text prettily, waiting for input, and then storing the appropriate boolean to the JSON dictionary. *If you're curious, that first parameter is for the key of the prompt for the question (stored in yet another JSON dictionary), and the string list is for all of the possible outcomes. It parses it using that = sign. For example, <code>&quot;yes=enteredCave&quot;</code> means that an input of anything defined as a synonym of &quot;yes&quot; will set the enteredCave boolean in the JSON to true. These booleans then get accessed again in my <code>if</code> forests:</p> <pre><code>while True: choices.askchoice(&quot;2-interiordescription&quot;, [&quot;go&amp;1=gotoDoor1&quot;,&quot;go&amp;2=gotoSpirits&quot;,&quot;go&amp;3=gotoDoor3&quot;,&quot;go&amp;4=gotoDoor4&quot;,&quot;go&amp;5=gotoDoor5&quot;,&quot;exit=hasLeftCave&quot;], speed=3) if save[&quot;gotoDoor1&quot;]: doStuff() elif save[&quot;gotoDoor2&quot;]: doStuff()# and then more branching if statements inside this one! ...etc </code></pre> <p>...Which, as you might be able to tell, creates a total mess.</p>