Commits

rablador committed 899ec82

Updated logic and changed room json structure a little bit. A room is a now an interactable item in its own.

Comments (0)

Files changed (5)

     
     self.story = None
     self.exits = {}
-    self.keywords = []
-    self.verbs = []
-    self.actions = {}
+    self.items = {}
 
     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']
+    self.items = room['items']
 
   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]
+  def interact(self, keyword, action):
+    for item in self.items:
+      if item == keyword:
+        action = self.get_interactable(item, action)
+        if (action):
+          return self.items[item]['actions'][action]
+
+  def get_interactable(self, item, action):
+    if action in self.items[item]['verbs']:
+      return action;
 import json
 
+# Utility class
 class Util(object):
-
   def load_room(self, room):
     data = open('resources/rooms.json')
     rooms = json.load(data)
     return rooms[room]
 
   def get_command_types(self, command):
-    words = command.split()
+    words = command.lower().split()
     
     types = {
       'direction' : None,
       'action' : None,
-      'item' : None
+      'keyword' : None
     }
 
     for word in words:
       if (action):
         types['action'] = action
 
+      keyword = self.get_keyword(word)
+      if (keyword):
+        types['keyword'] = keyword
+
     return types
 
-  def get_direction(self, direction):
+  def get_direction(self, word):
     data = open('resources/keywords.json')
     keywords = json.load(data)
     data.close()
 
     for dir in keywords['directions']:
-      if (direction in dir):
+      if (word in dir):
         return dir[0]
 
-  def get_action(self, action):
+  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')
+
+  def get_keyword(self, word):
     data = open('resources/keywords.json')
     keywords = json.load(data)
     data.close()
 
-    if (action in keywords['actions']):
-      return action
+    if (word in keywords['keywords']):
+      return word
+import os
+
 from libs.player import Player
 from libs.room import Room
 from libs.util import Util
 
 util = Util()
 
+clear = lambda: os.system('cls')
+clear()
+
 current_room = Room('room1')
 
 player = Player('Mika')
 
-print(player.name + ', welcome to Text Adventure!')
-print('\nHave a "look" around... ;)')
+print('\n' + player.name + ', welcome to Text Adventure!')
+print('\n' + current_room.story)
 
 while True:
   command = input('')
-
   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)
+  if (types['keyword'] and types['action']):
+    interaction = current_room.interact(types['keyword'], types['action'][0])
+    if (interaction):
+      print('\n' + interaction)
+
+  else:
+    if (types['action']):
+      print('\n' + types['action'][0].capitalize() + ' ' + types['action'][1] + '?')
+      
+      command = input('')
+      additional_types = util.get_command_types(command)
+
+      if additional_types['keyword']:
+        interaction = current_room.interact(additional_types['keyword'], types['action'][0])
+        if (interaction):
+          print('\n' + interaction)
+
+    elif (types['direction']):
+      new_room = current_room.go(types['direction'])
+      if (new_room):
+        current_room = Room(new_room)
+        print('\n' + current_room.story)

resources/keywords.json

 {
   "directions" : [
-    ["n", "north", "forward"],
-    ["e", "east", "right"],
-    ["s", "south", "back"],
-    ["w", "west", "left"]
+    ["n", "north", "f", "forward"],
+    ["e", "east", "r", "right"],
+    ["s", "south", "b", "back"],
+    ["w", "west", "l", "left"]
   ],
 
-  "actions" : [
-    "look",
-    "smell"
+  "actions" : {
+    "where" : [
+      "look"
+    ],
+    "what" : [
+      "smell",
+      "open",
+      "knock"
+    ]
+  },
+
+  "keywords" : [
+    "room",
+    "door",
+    "flowers"
   ]
 }

resources/rooms.json

     "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."
-    },
+    "story" : "You stand in a large room with some flowers beside you and a door to the right.",
     "items" : {
+      "room" : {
+        "verbs" : ["look", "smell"],
+        "actions" : {
+          "look" : "The room is spacious and invokes a feeling of calm.",
+          "smell" : "The fragrance of the flowers is quite remarkable."
+        }
+      },
       "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.",
+          "open" : "You open the door and see... nothing.",
+          "knock" : "You knock on the door, but nothing happens.",
           "look" : "It is a large door with iron hinges."
         }
+      },
+      "flowers" : {
+        "verbs" : ["look", "smell"],
+        "actions" : {
+          "look" : "Some very pretty flowers.",
+          "smell" : "Mm, smells lovely!"
+        }
       }
     }
   },
     "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."
-    },
+    "story" : "You stand in a large room with some flowers beside you and a door to the left.",
     "items" : {
+      "room" : {
+        "verbs" : ["look", "smell"],
+        "actions" : {
+          "look" : "The room is spacious and invokes a feeling of calm.",
+          "smell" : "The fragrance of the flowers is quite remarkable."
+        }
+      },
       "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.",
+          "open" : "You open the door and see... nothing.",
+          "knock" : "You knock on the door, but nothing happens.",
           "look" : "It is a large door with iron hinges."
         }
+      },
+      "flowers" : {
+        "verbs" : ["look", "smell"],
+        "actions" : {
+          "look" : "Some very pretty flowers.",
+          "smell" : "Mm, smells lovely!"
+        }
       }
     }
   }
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.