Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>On a more fundamental level, the object-oriented paradigm elegantly solves the problem of repetitive code like this:</p> <pre><code>global gold = 0 def cave(): print "You are in a cave." print "You have %i gold." % gold direction = input() if direction = 'N': stream() elif direction == 'S': house() elif direction == 'W': mountain() elif direction == 'directions': print "You can go North, West, or South." else: print "You cannot go there." def stream(): print "A small stream flows out of the building and down a gully." print "You have %i gold." % gold direction = input() if direction == 'N': tree() elif direction == 'S': cave() elif direction == 'directions': print "You can go North or South." else: print "You cannot go there." def main(): cave() </code></pre> <p>by turning it into something like this:</p> <pre><code>class Location: map = { 'cave': { 'description': 'You are in a cave.', 'directions': { 'N': 'stream', 'S': 'house', 'W': 'mountain' } }, 'stream': { 'description': 'A small stream flows out the building and down a gully.', 'directions': { 'N': 'tree', 'S': 'cave' } } #... } def __init__ (self): self.location = 'cave' def enter (self, direction): self.location = self.map[self.location]["directions"][direction] print self.map[self.location]["description"] def directions(self): return self.map[self.location]["directions"].keys() def readable(self, dirs): readable = { 'S': 'South', 'N': 'North', 'W': 'West', 'E': 'East' } return [readable[d] for d in dirs] class Inventory: def __init__ (self): self.inventory = { 'gold': 0 } def query (self): print "You have %i gold." % self.inventory['gold'] def main: loc = Location() inv = Inventory() while True: directions = loc.directions() action = raw_input() if action in directions: loc.enter(action) inv.query() elif action == 'directions': where = loc.readable(directions) print "You can go " + ", ".join(where[:-1])\ + ", or " + where[-1] else: print "You cannot go there." </code></pre> <p>You will notice that the more modular code is also easier to extend. For example, the inventory can now hold more things than gold, and it's easy to add new commands to query for weapons, potions, etc. Furthermore, it somewhat separates the code from the data, making it less cumbersome and error-prone to add new locations and actions.</p> <p>Next up, define <code>class Object</code> with subclasses for animate objects, objects you can pick up, immobile objects, etc; and populate the locations with instances of these. Different subclasses can have different interactions defined, and inherit from more basic superclasses which implement fundamentals like <code>take</code>, <code>drop</code>, <code>kill</code>, etc.</p> <p>What to map into objects is a broad topic, but a few simple guidelines would be to isolate and encapsulate unrelated code into their own classes, and make them as decoupled as possible (code implementing "location" should not need to know pretty much anything about code in "inventory", and vice versa).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload