Commits

Anonymous committed c8d99b8

made Char.attack() testable.

Comments (0)

Files changed (1)

         """
         win, diff = self.compete(other, "skill", myskill, "skill", otherskill, bonus=bonus)
 
+        # damage and wounds
+        damage, wound = self._attackresults(win, diff, myweapon, myarmor, self.woundthreshold, otherweapon, otherarmor, other.woundthreshold)
+
+        # apply damage and wounds
+        if win: hurt = other
+        else: hurt = self
+
+        hurt.damage += damage
+        if wound:
+            hurt.wounds[wound-1] += 1
+
+        # return the info for reporting
+        return win, damage, wound
+
+    def _attackresults(self, win, diff, myweapon, myarmor, mywoundthreshold, otherweapon, otherarmor, otherwoundthreshold):
+        """Calculate the results of an attack roll.
+
+        >>> me = Char()
+        >>> other = Char()
+        >>> myweapon, myarmor = 3, 2
+        >>> otherweapon, otherarmor = 3, 2
+        >>> win, diff = True, 3
+        >>> me._attackresults(win, diff, myweapon, myarmor, me.woundthreshold, otherweapon, otherarmor, other.woundthreshold)
+        (4, 0)
+
+        @return damage, wound? # wound: 0 = no, 1 = normal, 2 = critical
+        """
         if win:
             weapon = myweapon
             armor = otherarmor
-            woundthreshold = other.woundthreshold
-            hurt = other
+            woundthreshold = otherwoundthreshold
         else:
             weapon = otherweapon
             armor = myarmor
-            woundthreshold = self.woundthreshold
-            hurt = self
+            woundthreshold = mywoundthreshold
 
         damage = max(diff + weapon - armor, 0) # can’t be negative
 
         if damage >= 3 * woundthreshold:
             wound = 2 # critical
-            hurt.wounds[1] += 1
         elif damage >= woundthreshold:
             wound = 1 # normal
-            hurt.wounds[0] += 1
         else: wound = 0
 
-        hurt.damage += damage
-
-        return win, damage, wound
+        return damage, wound
 
 
 ### Self-Test ###