def execute(self): """ Equips player with item in inventory. """ #Create variables inventory = self._player.getInventory() equipped = self._player.getEquipped() equippable = ItemSet() #Create list of equippable items for item in inventory: if (isinstance(item, Weapon) or isinstance(item, Armor) or isinstance(item, Charm)) and item not in equipped: equippable.addItem(item) #If no equippable items if equippable.count() == 0: print "No equippable items in inventory." return #User prompt print "%s may equip:" % self._player.getName() for item in equippable: print "\t%s" % item.getName() print "" itemToEquip = raw_input("Which item do you want to equip? ") #Attempt to equip item item = inventory.getItemByName(itemToEquip) if item: statement = self._player.equip(item) print statement else: print "Item not in inventory."
def execute(self): """ Uses potion in inventory to heal player. """ #Check that potions in inventory inventory = self._player.getInventory() potions = ItemSet() for item in inventory: if isinstance(item, Potion): potions.addItem(item) if potions.count() == 0: print "%s has no potions." % self._player.getName() return #User prompt print "%s currently has:" % self._player.getName() for potion in potions: print "\t%s with %s healing power." % (potion.getName(), potion.getHealing()) print "" choice = None while True: choice = raw_input("Which potion would you like to use? ") if potions.containsItemWithName(choice): break else: print "%s does not have that potion." % self._player.getName() print "" #Healing mechanics potionChoice = potions.getItemByName(choice) healing = potionChoice.getHealing() preHealedHealth = self._player.getHp() self._player.heal(healing) postHealedHealth = self._player.getHp() healed = postHealedHealth - preHealedHealth inventory.removeItem(potionChoice) print "%s was healed by %s! %s's health is now %s." \ % (self._player.getName(), healed, self._player.getName(), self._player.getHp())
class ItemSetTest(unittest.TestCase): """ Tests ItemSet class. """ INITIAL_COUNT = 3 INITIAL_WEIGHT = 4 def setUp(self): from items.item import Item from items.item_set import ItemSet sword = Item("sword", "made by elves", 2) helmet = Item("helmet", "made by men", 1) potion = Item("potion", "restores health", 1) self._itemList = [sword, helmet, potion] self._items = ItemSet([sword, helmet, potion]) def tearDown(self): self._itemList = self._items = None def testInitItemSet(self): errorMsg = "ItemSet object has more objects than it was given " \ "during initialization." self.assertEqual(len(self._items._items), ItemSetTest.INITIAL_COUNT, errorMsg) errorMsg = "ItemSet object does not include all objects given " \ "during initialization." for item in self._itemList: self.assertTrue(item in self._items._items, errorMsg) def testCountItems(self): expectedCount = ItemSetTest.INITIAL_COUNT actualCount = self._items.count() errorMsg = "Actual count and expected count different for ItemSet object." self.assertEqual(expectedCount, actualCount, errorMsg) def testAddRemoveContainsItems(self): from items.item import Item antidote = Item("antidote", "cures poison", 1) #Verify item not included in collection errorMsg = "ItemSet.containsItem() claimed to contain item not present." self.assertFalse(self._items.containsItem(antidote), errorMsg) #Add item self._items.addItem(antidote) errorMsg = "ItemSet.containsItem() failed to identify existing item." self.assertTrue(self._items.containsItem(antidote), errorMsg) #Remove item self._items.removeItem(antidote) errorMsg = "ItemSet.containsItem() claimed to contain item not present." self.assertFalse(self._items.containsItem(antidote), errorMsg) def testItemsWeight(self): from items.item import Item errorMsg = "Initial weight of ItemSet object incorrect." expectedWeight = ItemSetTest.INITIAL_WEIGHT actualWeight = self._items.weight() self.assertEqual(expectedWeight, actualWeight, errorMsg) heavyRock = Item("heavy rock", "weighs a ton", 2000) #Add item self._items.addItem(heavyRock) errorMsg = "ItemSet.weight() reported incorrect weight." expectedWeight += 2000 actualWeight = self._items.weight() self.assertEqual(expectedWeight, actualWeight, errorMsg) #Remove item self._items.removeItem(heavyRock) expectedWeight -= 2000 actualWeight = self._items.weight() self.assertEqual(expectedWeight, actualWeight, errorMsg) def testItemSetIter(self): #Verify iterator returns by ItemSet object visits the exact #collection of objects added to ItemSet #(Implicitly) use iterator in for loop for item in self._items: #Verify item returned is recognized errorMsg = "ItemSet iterator returned unrecognized object." self.assertTrue(item in self._itemList, errorMsg) #Remove item from original list of items self._itemList.remove(item) #Assert all items are accounted for errorMsg = "ItemSet object contained Item not added during initialization." self.assertEqual(len(self._itemList), 0, errorMsg)