diff options
| author | Thomas Kluyver <takowl@gmail.com> | 2013-01-07 14:05:41 +0000 |
|---|---|---|
| committer | Thomas Kluyver <takowl@gmail.com> | 2013-01-07 14:05:41 +0000 |
| commit | 4cfb49cf5fe9513ac368ad8b09e9405cacaa9f56 (patch) | |
| tree | 8ef87bba30ea2c7e7d45e419d43ebd142e144ede /weatbag/utils.py | |
| parent | cbe1bce30306133a02de664008ca552f3618302c (diff) | |
Add transfer() utility function.
Diffstat (limited to 'weatbag/utils.py')
| -rw-r--r-- | weatbag/utils.py | 23 |
1 files changed, 23 insertions, 0 deletions
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 |
