summaryrefslogtreecommitdiffstats
path: root/weatbag
diff options
context:
space:
mode:
Diffstat (limited to 'weatbag')
-rw-r--r--weatbag/__init__.py52
-rw-r--r--weatbag/tiles/centre.py2
-rw-r--r--weatbag/tiles/e1.py5
-rw-r--r--weatbag/tiles/n2.py2
-rw-r--r--weatbag/utils.py23
5 files changed, 67 insertions, 17 deletions
diff --git a/weatbag/__init__.py b/weatbag/__init__.py
index 303962e..9a743e0 100644
--- a/weatbag/__init__.py
+++ b/weatbag/__init__.py
@@ -1,5 +1,6 @@
from collections import Counter
from importlib import import_module
+import re
from . import action
@@ -53,18 +54,7 @@ class World:
try:
return self.tiles[key]
except KeyError:
- e,n = key
- modname = 'weatbag.tiles.'
- if n > 0:
- modname += 'n'+str(n)
- elif n < 0:
- modname += 's'+str(abs(n))
-
- if e > 0:
- modname += 'e'+str(e)
- elif e < 0:
- modname += 'w'+str(abs(e))
-
+ modname = 'weatbag.tiles.' + coords_to_name(*key)
try:
mod = import_module(modname)
except ImportError:
@@ -74,11 +64,47 @@ class World:
self.tiles[key] = tile
return tile
+def coords_to_name(e, n):
+ modname = ''
+ if n > 0:
+ modname += 'n'+str(n)
+ elif n < 0:
+ modname += 's'+str(abs(n))
+
+ if e > 0:
+ modname += 'e'+str(e)
+ elif e < 0:
+ modname += 'w'+str(abs(e))
+
+ return modname
+
+_tile_name_re = re.compile(r'([ns]\d+)?([ew]\d+)?')
+def name_to_coords(tilename):
+ ns, es = _tile_name_re.match(tilename).groups()
+ if ns is None:
+ n = 0
+ elif ns[0] == 'n':
+ n = int(ns[1:])
+ else:
+ n = -int(ns[1:])
+
+ if es is None:
+ e = 0
+ elif es[0] == 'e':
+ e = int(es[1:])
+ else:
+ e = -int(es[1:])
+
+ return e, n
+
def main():
name = input("What is your name? ")
player = Player(name)
+ interact(player)
+
+def interact(player):
world = World()
- tile = world[0,0]
+ tile = world[player.position]
tile.describe()
while True:
do = action.get_action()
diff --git a/weatbag/tiles/centre.py b/weatbag/tiles/centre.py
index 4aa9a30..c2709d7 100644
--- a/weatbag/tiles/centre.py
+++ b/weatbag/tiles/centre.py
@@ -5,7 +5,7 @@ class Tile:
self.contents = {'rope': 1}
def describe(self):
- print("You are in a sizeable clearing in the forest. Paths lead off in"
+ print("You are in a sizeable clearing in the forest. Paths lead off in "
"four directions, which your unerring sense of direction tells you "
"correspond to the four compass points.")
if self.contents.get('rope'):
diff --git a/weatbag/tiles/e1.py b/weatbag/tiles/e1.py
index 909f1b3..486a98f 100644
--- a/weatbag/tiles/e1.py
+++ b/weatbag/tiles/e1.py
@@ -1,4 +1,5 @@
from weatbag import words
+from weatbag.utils import transfer
class Tile:
def __init__(self):
@@ -17,9 +18,7 @@ class Tile:
print("Sorry, I don't understand.")
def take_berries(self, player):
- if self.contents['berries'] > 0:
- self.contents['berries'] -= 1
- player.give('berries')
+ if transfer('berries', self.contents, player.inventory):
print("Reaching up, you pick the berries.")
else:
print("There are no berries here.")
diff --git a/weatbag/tiles/n2.py b/weatbag/tiles/n2.py
index a9bca43..32e3ce4 100644
--- a/weatbag/tiles/n2.py
+++ b/weatbag/tiles/n2.py
@@ -26,3 +26,5 @@ class Tile:
else:
print("Sorry, I don't understand")
+
+test_items = ['unlit torch']
diff --git a/weatbag/utils.py b/weatbag/utils.py
new file mode 100644
index 0000000..0f73294
--- /dev/null
+++ b/weatbag/utils.py
@@ -0,0 +1,23 @@
+"""Utility functions for the game."""
+
+def transfer(item, from_collection, to_collection, n=1):
+ """Move an item from one dictionary of objects to another.
+
+ Returns True if the item was successfully transferred, False if it's not in
+ from_collection.
+
+ For example, to have the player pick up a pointy stick::
+
+ if transfer('pointy stick', tile.contents, player.inventory):
+ print("You take the stick.")
+ else:
+ print("There's no stick here.")
+ """
+ if from_collection.get(item, 0) < n:
+ return False
+ from_collection[item] -= n
+ if item in to_collection:
+ to_collection[item] += n
+ else:
+ to_collection[item] = n
+ return True