Skip to content

Instantly share code, notes, and snippets.

@gentimouton
Created June 2, 2019 05:37
Show Gist options
  • Save gentimouton/304e121e182cf567a59a20e1f633502f to your computer and use it in GitHub Desktop.
Save gentimouton/304e121e182cf567a59a20e1f633502f to your computer and use it in GitHub Desktop.
simple battle simulator
class Battler:
def __init__(self, p, a, m, h):
self.power = p
self.armor = a
self.mhp = m
self.hp = h
def __repr__(self):
# return '{}/{}:{}/{}'.format(self.power, self.armor, self.hp, self.mhp)
return '{}/{}'.format(self.hp, self.mhp)
front1 = Battler(5,5,40,40)
back1 = Battler(10,1,20,20)
side1 = [front1, back1]
front2 = Battler(10,5,15,15)
# back2 = Battler(8,0,20,20)
side2 = [front2, None]
def is_dead(side):
if not side[0] and not side[1]:
return True
if not side[0]:
return side[1].hp <= 0
if not side[1]:
return side[0].hp <= 0
return side[0].hp <= 0 and side[1].hp <= 0
def get_target(side):
return side[0] if side[0].hp > 0 else side[1]
turn = 0
while not is_dead(side1) and not is_dead(side2):
print('----turn {}------'.format(turn))
print(side1)
print(side2)
# backliners attack first
target1 = get_target(side2)
target2 = get_target(side1)
if side1[1]:
target1.hp -= (side1[1].power - target1.armor)
if side2[1]:
target2.hp -= (side2[1].power - target2.armor)
# stop fight if needed
if is_dead(side2) or is_dead(side1):
break
# frontliners attack second
target1 = get_target(side2)
target2 = get_target(side1)
if side1[0] and target1:
target1.hp -= side1[0].power - target1.armor
if side2[0] and target2:
target2.hp -= side2[0].power - target2.armor
turn += 1
# ----turn 0------
# [40/40, 20/20]
# [15/15, None]
# ----turn 1------
# [35/40, 20/20]
# [10/15, None]
# ----turn 2------
# [30/40, 20/20]
# [5/15, None]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment