Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- from random import randint
- import sys
- import os
- import re
- class Pawn:
- def __init__(self, *data):
- self.armor_name = None
- self.weapon_type = self.attack_counter = self.dealed_damage = self.armor_health = 0
- self.name, self.speed, self.health, self.weapon, self.damage = data[:5]
- if len(data) > 5:
- self.armor_name = data[5]
- self.armor_health = int(data[6])
- self.speed = int(self.speed)
- self.health = self.max_health = int(self.health)
- self.damage = map(int, self.damage.strip('()').split(','))
- try:
- self.weapon, self.weapon_type = self.weapon.strip('()').split(',')
- self.weapon_type = int(self.weapon_type)
- except ValueError:
- pass
- def roll_damage(self):
- ret = self.damage[0]
- for random_damage in self.damage[1:]:
- ret += randint(0, random_damage - ret)
- # Weapon type 1
- if self.weapon_type == 1:
- self.health = min(self.health + ret, self.max_health)
- # Weapon type 2
- self.attack_counter += 1
- if self.weapon_type == 2 and self.attack_counter % 2 == 0:
- ret *= 2
- self.dealed_damage = ret
- return ret
- def receive_damage(self, damage):
- if damage <= self.armor_health:
- self.armor_health -= damage
- else:
- self.health -= damage - self.armor_health
- self.armor_health = 0
- def print_damage(tick, p1, p2, ph1, ph2):
- sh1 = ("(%d,%d)" % (ph1.armor_health, ph1.health)) if ph1.armor_name is not None else ("%d" % ph1.health)
- sh2 = ("(%d,%d)" % (ph2.armor_health, ph2.health)) if ph2.armor_name is not None else ("%d" % ph2.health)
- critical = ",critical" if len(p1.damage) > 1 and p1.dealed_damage == p1.damage[-1] else ''
- print "%d,%s,%s,%s,%d,%s,%s%s" % (tick, p1.name, p2.name, p1.weapon, p1.dealed_damage, sh1, sh2, critical)
- def main(argv):
- os.chdir(os.path.dirname(os.path.abspath(__file__)))
- players = [Pawn(*re.split(r"\s*,(?![^(]*\))\s*", line)) for line in open('combat.rpg').read().splitlines()]
- # weapon type 3
- for i, p in enumerate(players):
- j = len(players) - 1 - i
- if p.weapon_type == 3:
- op = players[j]
- p.weapon_type, p.weapon, p.damage, op.weapon_type, op.weapon, op.damage = op.weapon_type, op.weapon, op.damage, p.weapon_type, p.weapon, p.damage
- tick = 1
- while True:
- nothing = True
- for player in players:
- if player.health <= 0:
- print("%d,%s,Dead" % (tick, player.name))
- if players[0].health <= 0 or players[1].health <= 0: # vincolo a 2 giocatori
- break
- for idx in xrange(len(players)):
- if tick % players[idx].speed == 0:
- other_idx = len(players) - 1 - idx
- players[other_idx].receive_damage(players[idx].roll_damage())
- print_damage(tick, players[idx], players[other_idx], players[0], players[1]) # vincolo a 2 giocatori
- nothing = False
- for p in players:
- p.health = max(0, p.health)
- if nothing:
- print("%d,NOTHING" % tick)
- tick += 1
- for idx, player in enumerate(players):
- if player.health > 0:
- print(player.name)
- return idx + 1
- print("TIE")
- return 0
- if __name__ == "__main__":
- sys.exit(main(sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement