blob: 0f73294f55fe921616cb78e90c8e682fe1ea8740 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
|