Commits

Anonymous committed 236743d

asciibattle: map instead of for-loop → 40% faster.

  • Participants
  • Parent commits a7f1636

Comments (0)

Files changed (1)

     else:
         enemy.improve(1)
 
+def fighterstep(me):
+    """One Round for One Fighter."""
+    if me.wounds[1]:
+        return
+    if me.enemy is None or not me.enemy.pos in battlefield:
+        findenemy(me)
+    if me.enemy is None:
+        return
+    forwards = stepforward(me, me.enemy)
+    sidewards = stepsideward(me, me.enemy)
+    odd = oddstep(me)
+    for step in (forwards, sidewards, odd):
+        if step in battlefield:
+            isenemy = battlefield[step].team != me.team
+            if isenemy:
+                me.enemy = battlefield[step]
+                enemies = len([battlefield[pos] for pos in adjadent(me.pos) if battlefield.get(pos, None) is not None and battlefield[pos].team != me.team])
+                allies = len([battlefield[pos] for pos in adjadent(me.enemy.pos) if battlefield.get(pos, None) is not None and battlefield[pos].team == me.team])
+                mod = (allies - enemies) * 3
+                attack(me, me.enemy, mod)
+                break
+        else: # move there
+            del battlefield[me.pos]
+            me.pos = step
+            battlefield[me.pos] = me
+            break
+    if me.wounds[0]:
+        me.letter = me.let
 
 
 def battleround():
     """a single round of battle"""
-    for me in fighters[:]:
-        if me.wounds[1]:
-            continue
-        if me.enemy is None or not me.enemy.pos in battlefield:
-            findenemy(me)
-        if me.enemy is None:
-            continue
-        forwards = stepforward(me, me.enemy)
-        sidewards = stepsideward(me, me.enemy)
-        odd = oddstep(me)
-        for step in (forwards, sidewards, odd):
-            if step in battlefield:
-                isenemy = battlefield[step].team != me.team
-                if isenemy:
-                    me.enemy = battlefield[step]
-                    enemies = len([battlefield[pos] for pos in adjadent(me.pos) if battlefield.get(pos, None) is not None and battlefield[pos].team != me.team])
-                    allies = len([battlefield[pos] for pos in adjadent(me.enemy.pos) if battlefield.get(pos, None) is not None and battlefield[pos].team == me.team])
-                    mod = (allies - enemies) * 3
-                    attack(me, me.enemy, mod)
-                    break
-            else: # move there
-                del battlefield[me.pos]
-                me.pos = step
-                battlefield[me.pos] = me
-                break
-        if me.wounds[0]:
-            me.letter = me.let
+    [i for i in map(fighterstep, fighters)]
 
 def meancost(team):
     return int(sum((c.cost for c in team))/max(1, len(team)))