Advertisement
gambuzzi

rpgkata.py

May 13th, 2015
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. from random import randint
  5. import sys
  6. import os
  7. import re
  8.  
  9.  
  10. class Pawn:
  11.     def __init__(self, *data):
  12.         self.armor_name = None
  13.         self.weapon_type = self.attack_counter = self.dealed_damage = self.armor_health = 0
  14.         self.name, self.speed, self.health, self.weapon, self.damage = data[:5]
  15.         if len(data) > 5:
  16.             self.armor_name = data[5]
  17.             self.armor_health = int(data[6])
  18.         self.speed = int(self.speed)
  19.         self.health = self.max_health = int(self.health)
  20.         self.damage = map(int, self.damage.strip('()').split(','))
  21.         try:
  22.             self.weapon, self.weapon_type = self.weapon.strip('()').split(',')
  23.             self.weapon_type = int(self.weapon_type)
  24.         except ValueError:
  25.             pass
  26.  
  27.     def roll_damage(self):
  28.         ret = self.damage[0]
  29.         for random_damage in self.damage[1:]:
  30.             ret += randint(0, random_damage - ret)
  31.         # Weapon type 1
  32.         if self.weapon_type == 1:
  33.             self.health = min(self.health + ret, self.max_health)
  34.         # Weapon type 2
  35.         self.attack_counter += 1
  36.         if self.weapon_type == 2 and self.attack_counter % 2 == 0:
  37.             ret *= 2
  38.         self.dealed_damage = ret
  39.         return ret
  40.  
  41.     def receive_damage(self, damage):
  42.         if damage <= self.armor_health:
  43.             self.armor_health -= damage
  44.         else:
  45.             self.health -= damage - self.armor_health
  46.             self.armor_health = 0
  47.  
  48.  
  49. def print_damage(tick, p1, p2, ph1, ph2):
  50.     sh1 = ("(%d,%d)" % (ph1.armor_health, ph1.health)) if ph1.armor_name is not None else ("%d" % ph1.health)
  51.     sh2 = ("(%d,%d)" % (ph2.armor_health, ph2.health)) if ph2.armor_name is not None else ("%d" % ph2.health)
  52.     critical = ",critical" if len(p1.damage) > 1 and p1.dealed_damage == p1.damage[-1] else ''
  53.     print "%d,%s,%s,%s,%d,%s,%s%s" % (tick, p1.name, p2.name, p1.weapon, p1.dealed_damage, sh1, sh2, critical)
  54.  
  55.  
  56. def main(argv):
  57.     os.chdir(os.path.dirname(os.path.abspath(__file__)))
  58.     players = [Pawn(*re.split(r"\s*,(?![^(]*\))\s*", line)) for line in open('combat.rpg').read().splitlines()]
  59.  
  60.     # weapon type 3
  61.     for i, p in enumerate(players):
  62.         j = len(players) - 1 - i
  63.         if p.weapon_type == 3:
  64.             op = players[j]
  65.             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
  66.  
  67.     tick = 1
  68.     while True:
  69.         nothing = True
  70.  
  71.         for player in players:
  72.             if player.health <= 0:
  73.                 print("%d,%s,Dead" % (tick, player.name))
  74.  
  75.         if players[0].health <= 0 or players[1].health <= 0:  # vincolo a 2 giocatori
  76.             break
  77.  
  78.         for idx in xrange(len(players)):
  79.             if tick % players[idx].speed == 0:
  80.                 other_idx = len(players) - 1 - idx
  81.                 players[other_idx].receive_damage(players[idx].roll_damage())
  82.                 print_damage(tick, players[idx], players[other_idx], players[0], players[1])  # vincolo a 2 giocatori
  83.                 nothing = False
  84.  
  85.         for p in players:
  86.             p.health = max(0, p.health)
  87.  
  88.         if nothing:
  89.             print("%d,NOTHING" % tick)
  90.  
  91.         tick += 1
  92.  
  93.     for idx, player in enumerate(players):
  94.         if player.health > 0:
  95.             print(player.name)
  96.             return idx + 1
  97.  
  98.     print("TIE")
  99.     return 0
  100.  
  101.  
  102. if __name__ == "__main__":
  103.     sys.exit(main(sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement