Commits

Anonymous committed bdbff1f

Made Path from teh branchign_story easily usable for other stories.

Comments (0)

Files changed (1)

branching_story.py

 }
 # TODO: Write text for all locations. 
 
-def get_description(name):
-    """Get the text for each locations name"""
-    if name in location_descriptions: 
-        return location_descriptions[name]
-    else: 
-        return name 
-
 class Path(object):
-    def __init__(self):
-        self.position = (2, 2)
-        self.target = (0, 0)
+    """Moving through a maze. """
+    def __init__(self, start=(2, 2), target = (0,0), maze = wood, descriptions = location_descriptions, story=story, ask=ask):
+        """
+        @param start: Where we begin. 
+        @type start: Tuple
+        
+        @param target: Where we want to get. 
+        @type target: Tuple
+        
+        @param maze: The maze in which we move. 
+        @type maze: List of Lists
+        
+        @param descriptions: Descriptions for the places in the maze. 
+        @type start: Dictionary
+        
+        @param story: The function for telling stories. 
+        @type story: Function
+        
+        @param ask: The function to ask the user. 
+        @type ask: Function
+        """
+        #: Where we are
+        self.position = start
+        #: Where we want to get
+        self.target = target
+        #: The terrain / maze
+        self.maze = maze
+        #: Descriptions for the places in thge terrain / maze
+        self.descriptions = descriptions
+        #: The function to use for telling stories
+        self.story = story
+        #: The function to use for talking to the user. 
+        self.ask = ask
     def pos_to_name(self, pos=(2, 2)):
         """return the location name for a position."""
-        return wood[pos[1]][pos[0]]
+        return self.maze[pos[1]][pos[0]]
+    def get_description(self, name):
+        """Get the text for each locations name"""
+        if name in self.descriptions: 
+            return self.descriptions[name]
+        else: 
+            return name 
     def move(self):
         x, y = self.position
         # specify possible movement. 
         answers = []
         # get all possible answers by checking if the ways are possible. 
         for i, j in directions.items():
-            if wood[j[1]][j[0]]:
+            if self.maze[j[1]][j[0]]:
                 answers.append(i)
         question = " ".join([question] + answers + [")"])
         description = "\n".join(['to the ' + i + ' you see ' + 
             story("", autoscroll=True)
             ans = ask(question)
             for i in answers: 
-                name = wood[directions[i][1]][directions[i][0]]
+                name = self.maze[directions[i][1]][directions[i][0]]
                 if ans.lower() in [i.lower(), i.lower()[0], name, " ".join(name.split()[1:])]: 
                     target = directions[i]
         self.position = target
         story("""You go to """ + self.pos_to_name(self.position))
-        story(get_description(self.pos_to_name(self.position)))
+        story(self.get_description(self.pos_to_name(self.position)))
 
-path = Path()
+path = Path(start=(2,2), target=(0,0), maze=wood, descriptions=location_descriptions, story=story, ask=ask)
 while not path.position == path.target:
     path.move()