11""" Player class file """
22
3+ from collections import defaultdict
4+
35class Player (object ):
46 """Describe the player, his inventory, traits, position and history"""
57
68 def __init__ (self , state = None ):
79 self .state = state
8- self .items = {}
9- self .traits = {}
10- self .history = {}
10+ self .items = defaultdict ( lambda : 0 )
11+ self .traits = defaultdict ( lambda : 0 )
12+ self .history = defaultdict ( lambda : False )
1113
1214
1315 def save_to (self , filename ):
@@ -35,7 +37,7 @@ def set_traits(self, traits):
3537 def update_traits (self , traits ):
3638 """Update the players traits to reflect the parameter traits. (Adds to the previous value)"""
3739 for trait , value in traits .items ():
38- self .traits [trait ] = self . traits . get ( trait , 0 ) + value
40+ self .traits [trait ] += value
3941
4042
4143 def has_traits (self , traits ):
@@ -48,7 +50,7 @@ def has_traits(self, traits):
4850 def update_items (self , items : dict ):
4951 """Update the players inventory to reflect the parameter items. (Adds to the previous value)"""
5052 for item , count in items .items ():
51- self .items [item ] = max (self .items . get ( item , 0 ) + count , 0 ) # clamps value to zero if result is negative
53+ self .items [item ] = max (self .items [ item ] + count , 0 ) # clamps value to zero if result is negative
5254
5355
5456 def has_items (self , items : dict ):
0 commit comments