Commits

rablador committed 7b6482f

Started modulating the project and implemented first steps of json driven design (using a general engine).

Comments (0)

Files changed (6)

 # Player class
 class Player(object):
-  def __init__(self, name, location):
+  def __init__(self, name):
     self.name = name
-    self.location = location
-
-  def look(self):
-    place = self.location
-    print(place.description)
+    
+from libs.util import Util
+
 # Room class
 class Room(object):
-  def __init__(self, name, description):
-    self.name = name
-    self.description = description
+  def __init__(self, name):
+    util = Util()
+    room = util.load_room(name)
+    
+    self.story = None
+    self.exits = {}
+    self.keywords = []
+    self.verbs = []
+    self.actions = {}
+
+    self.parse_room(room)
+
+  def parse_room(self, room):
+    self.story = room['story']
+    self.exits = room['exits']
+    self.keywords = room['keywords']
+    self.verbs = room['verbs']
+    self.actions = room['actions']
+
+  def go(self, direction):
+    for exit in self.exits:
+      if direction in exit:
+        return self.exits[direction]
+
+  def do(self, action):
+    if action in self.verbs:
+      return self.actions[action]
+import json
+
+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):
+    words = command.split()
+    
+    types = {
+      'direction' : None,
+      'action' : None,
+      'item' : None
+    }
+
+    for word in words:
+      direction = self.get_direction(word)
+      if (direction):
+        types['direction'] = direction
+
+      action = self.get_action(word)
+      if (action):
+        types['action'] = action
+
+    return types
+
+  def get_direction(self, direction):
+    data = open('resources/keywords.json')
+    keywords = json.load(data)
+    data.close()
+
+    for dir in keywords['directions']:
+      if (direction in dir):
+        return dir[0]
+
+  def get_action(self, action):
+    data = open('resources/keywords.json')
+    keywords = json.load(data)
+    data.close()
+
+    if (action in keywords['actions']):
+      return action
 from libs.player import Player
 from libs.room import Room
+from libs.util import Util
 
-# Rooms
-#
-# ===================
-# | |
-# | |
-# | 1 |
-# | |
-# | 3 2 |
-# | |
-# | |
-# | |
-# | |
-# ===================
-room1 = Room('Bedroom', 'You are you in your own bedroom.\nTo the south, there is a garden past the back door.')
-room2 = Room('Garden', 'You are in a garden with many flowers and a narrow stone path. \nTo the north, you see the backdoor of your house that enters your bedroom.\nA pathway leads west.')
-room3 = Room('Pathway', 'You are in a narrow stone path with hedges on both sides of you.\nTo the east, there is a garden.')
-
-# Room coordinates
-room1.coordinates = [0, 0, room2, 0]
-room2.coordinates = [room1, 0, 0, room3]
-room3.coordinates = [0, room2, 0, 0]
-
-player = Player('Mika', room1)
-
-print(player.name + ', welcome to Text Adventure!\n')
-print('Have a "look" around... ;)')
+util = Util()
+
+current_room = Room('room1')
+
+player = Player('Mika')
+
+print(player.name + ', welcome to Text Adventure!')
+print('\nHave a "look" around... ;)')
 
 while True:
   command = input('')
-  if command == 'look':
-    player.look()
+
+  types = util.get_command_types(command)
+
+  if (types['direction']):
+    new_room = current_room.go(types['direction'])
+    if (new_room):
+      current_room = Room(new_room)
+      print('\n' + current_room.story)
+
+  if (types['action']):
+    action = current_room.do(types['action'])
+    if (action):
+      print('\n' + action)

resources/keywords.json

+{
+  "directions" : [
+    ["n", "north", "forward"],
+    ["e", "east", "right"],
+    ["s", "south", "back"],
+    ["w", "west", "left"]
+  ],
+
+  "actions" : [
+    "look",
+    "smell"
+  ]
+}

resources/rooms.json

+{
+  "room1" : {
+    "exits" : {
+      "e" : "room2"
+    },
+    "story" : "You stand in a large room with a door in front of you and some flowers to the left.",
+    "keywords" : ["door", "flowers"],
+    "verbs" : ["look", "smell"],
+    "actions" : {
+      "look" : "The room is spacious and invokes a calm feeling.",
+      "smell" : "You can smell the freshnesh from the flowers."
+    },
+    "items" : {
+      "door" : {
+        "verbs" : ["open", "knock", "look"],
+        "actions" : {
+          "open" : "You open the door and see a person standing in front of you.",
+          "knock" : "You knock on the door, but nothin happens.",
+          "look" : "It is a large door with iron hinges."
+        }
+      }
+    }
+  },
+
+  "room2" : {
+    "exits" : {
+      "w" : "room1"
+    },
+    "story" : "Dead end!",
+    "keywords" : ["door", "flowers"],
+    "verbs" : ["look", "smell"],
+    "actions" : {
+      "look" : "The room is spacious and invokes a calm feeling.",
+      "smell" : "You can smell the freshnesh from the flowers."
+    },
+    "items" : {
+      "door" : {
+        "verbs" : ["open", "knock", "look"],
+        "actions" : {
+          "open" : "You open the door and see a person standing in front of you.",
+          "knock" : "You knock on the door, but nothin happens.",
+          "look" : "It is a large door with iron hinges."
+        }
+      }
+    }
+  }
+}