rablador avatar rablador committed 7bcd901

Refactoring.

Comments (0)

Files changed (3)

-from libs.util import Util
+import json
+
+data = open('resources/rooms.json')
+rooms = json.load(data)
+data.close()
 
 # Room class
 class Room(object):
   def __init__(self, name):
-    util = Util()
-    room = util.load_room(name)
+    self.room = rooms[name]
+    self.story = self.room['story']
+    self.exits = self.room['exits']
+    self.items = self.room['items']
     
-    self.story = None
-    self.exits = {}
-    self.items = {}
-
-    self.parse_room(room)
-
-  def parse_room(self, room):
-    self.story = room['story']
-    self.exits = room['exits']
-    self.items = room['items']
-
   def go(self, direction):
     for exit in self.exits:
       if direction in exit:
 import json
 
+data = open('resources/keywords.json')
+keywords = json.load(data)
+data.close()
+
 # Utility class
 class Util(object):
-  def load_room(self, room):
-    data = open('resources/rooms.json')
-    rooms = json.load(data)
-    data.close()
-
-    return rooms[room]
-
   def get_command_types(self, command):
+    self.keywords = keywords;
+
     words = command.lower().split()
     
     types = {
     return types
 
   def get_direction(self, word):
-    data = open('resources/keywords.json')
-    keywords = json.load(data)
-    data.close()
-
-    for dir in keywords['directions']:
+    for dir in self.keywords['directions']:
       if (word in dir):
         return dir[0]
 
   def get_action(self, word):
-    data = open('resources/keywords.json')
-    keywords = json.load(data)
-    data.close()
-
-    if (word in keywords['actions']['where']):
-      return (word, 'where')
-    elif (word in keywords['actions']['what']):
-      return (word, 'what')
+    if (word in self.keywords['actions']['where']):
+      return {'word' : word, 'kind' : 'where'}
+    elif (word in self.keywords['actions']['what']):
+      return {'word' : word, 'kind' : 'what'}
 
   def get_keyword(self, word):
-    data = open('resources/keywords.json')
-    keywords = json.load(data)
-    data.close()
-
-    if (word in keywords['keywords']):
+    if (word in self.keywords['keywords']):
       return word
 clear()
 
 current_room = Room('room1')
-
 player = Player('Mika')
 
 print('\n' + player.name + ', welcome to Text Adventure!')
   types = util.get_command_types(command)
 
   if (types['keyword'] and types['action']):
-    interaction = current_room.interact(types['keyword'], types['action'][0])
+    interaction = current_room.interact(types['keyword'], types['action']['word'])
     if (interaction):
       print('\n' + interaction)
 
   else:
     if (types['action']):
-      print('\n' + types['action'][0].capitalize() + ' ' + types['action'][1] + '?')
+      print('\n' + types['action']['word'].capitalize() + ' ' + types['action']['kind'] + '?')
       
       command = input('')
       additional_types = util.get_command_types(command)
 
       if additional_types['keyword']:
-        interaction = current_room.interact(additional_types['keyword'], types['action'][0])
+        interaction = current_room.interact(additional_types['keyword'], types['action']['word'])
         if (interaction):
           print('\n' + interaction)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.