HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Cactus (Prototype) - A game engine for text-based adventure games

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
enginecactustextadventuregamesgameprototypeforbased

Problem

@Annonomus Penguin, @QPaysTaxes, and I are building an engine for easy creation of text-based adventure games, like Zork. The engine is still in its early stages of development, and the following code is my prototype. Here's the library code.

cactus.py

```
class MapPosition(object):
"""
Describes data about a position on in the world
map. Contains the following data attributes.

name - The name of the choice.
desc - A description of the choice.
choices - A dictionary of possible choices.
function - An (optional) function to be run.
"""
def __init__(self, name, desc, choices, function=None):
self.name = name
self.desc = desc
self.choices = choices
self.function = function

def print_choice(self):
"""
Outputs certain data about the choice, e.g, description,
it's name, possible choices, etc.
"""
if self.choices != {}:
print "{0}: {1} Choices: {2}".format(
self.name,
self.desc,
', '.join([key for key, value in self.choices.iteritems()])
)
else:
print "{0}: {1}".format(
self.name,
self.desc
)

def run_function(self):
"""
Run the attached function when the choice is chosen
by the user.
"""
if self.function is not None:
self.function()

class GameMap(object):
"""
Describes data about a world map. Contains the
following data attributes.

map_data - A list containing MapPosition's
"""
def __init__(self, map_data):
self.map_data = map_data

def find_start(self):
"""
This iterates over the self.map_data list and finds
the starting choice.
"""
for index, choice in enumerate(self.map_data):
if choice.name.lower() == "start":
return index

Solution

else:
    continue


Does nothing: remove it.

lower or not lower: you must decide

I suggest you decide: either all input is lowercased or it is kept as the original, the following is not a clean solution (you can understand it is not clean because you spelled it out in the docs).

if user_input.lower() in possible_choices:
    self.map_position = possible_choices[user_input.lower()]
elif user_input in possible_choices:
    self.map_position = possible_choices[user_input]

Code Snippets

else:
    continue
if user_input.lower() in possible_choices:
    self.map_position = possible_choices[user_input.lower()]
elif user_input in possible_choices:
    self.map_position = possible_choices[user_input]

Context

StackExchange Code Review Q#92318, answer score: 5

Revisions (0)

No revisions yet.