for t in b.down
unsink t
else
- while b.flips > 0 and b.down.length > 0
+ down_count = b.down.length
+ while b.flips > 0 and down_count
b.flips -= 1
- flipper = Math.floor(Math.random() * b.down.length)
+ flipper = Math.floor(Math.random() * down_count)
unsink b.down[flipper]
- b.down = [b.down[0...flipper]..., b.down[flipper+1...b.down.length]...]
+ down_count -= 1
+ # move the last tile back into range
+ b.down[flipper] = b.down[down_count]
if b.force > 0
for t in b.up
t.new_hp = t.hp + b.force