Advertisement
luckytyphlosion

Abstract Pokemon Simulator.

May 20th, 2019
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.81 KB | None | 0 0
  1. import collections
  2. import copy
  3. import string
  4. import random
  5.  
  6. # switch by inputting in the mon to switch to
  7. # use moves by inputting 1, 2, 3, 4 for each move respectively
  8.  
  9. """
  10. team A:
  11. A: hp=140, speed=2
  12. B: hp=120, speed=10
  13. team B:
  14. a: hp=180, speed=1
  15. b: hp=260, speed=5
  16.  
  17. Aa - 100
  18. Ab - 100
  19. Ba - 80
  20. Bb - 60
  21. aA - 100
  22. aB - 80
  23. bA - 50, 90
  24. bB - 60, 50
  25. """
  26.  
  27. TEAM_1_SIZE = 3
  28. TEAM_2_SIZE = 3
  29.  
  30. MAX_SPEED = 10
  31.  
  32. class Team:
  33.     __slots__ = ("mons", "cur_member")
  34.     def __init__(self, mons, cur_member):
  35.         self.mons = mons
  36.         self.cur_member = cur_member
  37.  
  38.     @property
  39.     def cur_mon(self):
  40.         return self.mons[self.cur_member]
  41.  
  42. class Member:
  43.     __slots__ = ("hp", "speed")
  44.     def __init__(self, hp, speed):
  45.         self.hp = hp
  46.         self.speed = speed
  47.  
  48. """
  49. team_a_mons_original = collections.OrderedDict({
  50.    "A": Member(hp=140, speed=2),
  51.    "B": Member(hp=120, speed=10),
  52. })
  53.  
  54. team_b_mons_original = collections.OrderedDict({
  55.    "a": Member(hp=180, speed=1),
  56.    "b": Member(hp=260, speed=5),
  57. })
  58. """
  59.  
  60. # Aa: [60, 10, 100]
  61. # Ab: [60, 100, 50]
  62. # Ba: [70, 30]
  63. # Bb: [70, 90]
  64. # aA: [60, 90, 90, 100]
  65. # aB: [20, 60, 70, 90]
  66. # bA: [50, 30, 20, 70]
  67. # bB: [80, 10, 80, 50]
  68.  
  69. def generate_random_teams():
  70.     team_1_mons = collections.OrderedDict()
  71.     team_2_mons = collections.OrderedDict()
  72.     damages = {}
  73.  
  74.     for team_1_member in string.ascii_uppercase[:TEAM_1_SIZE]:
  75.         for team_2_member in string.ascii_lowercase[:TEAM_2_SIZE]:
  76.             damages[team_1_member + team_2_member] = []
  77.             damages[team_2_member + team_1_member] = []
  78.             for move_index in range(4):
  79.                 damages[team_1_member + team_2_member].append(random.randint(1, 10) * 10)
  80.                 damages[team_2_member + team_1_member].append(random.randint(1, 10) * 10)
  81.  
  82.     team_1_speeds = []
  83.     team_2_speeds = []
  84.     speeds = set(i for i in range(MAX_SPEED + 1))
  85.  
  86.     if random.randint(1, 2) == 1:
  87.         first_team_speeds = team_1_speeds
  88.         second_team_speeds = team_2_speeds
  89.         first_team_len = TEAM_1_SIZE
  90.         second_team_len = TEAM_2_SIZE
  91.     else:
  92.         first_team_speeds = team_2_speeds
  93.         second_team_speeds = team_1_speeds
  94.         first_team_len = TEAM_2_SIZE
  95.         second_team_len = TEAM_1_SIZE
  96.  
  97.     for i in range(first_team_len):
  98.         speed = random.randint(1, MAX_SPEED)
  99.         speeds.discard(speed)
  100.         first_team_speeds.append(speed)
  101.  
  102.     speeds = tuple(speeds)
  103.  
  104.     for i in range(second_team_len):
  105.         speed = random.choice(speeds)
  106.         second_team_speeds.append(speed)
  107.  
  108.     for team_1_member in string.ascii_uppercase[:TEAM_1_SIZE]:
  109.         team_1_speed = team_1_speeds.pop(0)
  110.         team_1_mons[team_1_member] = Member(hp=random.randint(10, 30) * 10, speed=team_1_speed)
  111.  
  112.     for team_2_member in string.ascii_lowercase[:TEAM_2_SIZE]:
  113.         team_2_speed = team_2_speeds.pop(0)
  114.         team_2_mons[team_2_member] = Member(hp=random.randint(10, 30) * 10, speed=team_2_speed)
  115.  
  116.     return Team(team_1_mons, "A"), Team(team_2_mons, "a"), damages
  117.  
  118. def print_damages(damages):
  119.     output = ""
  120.     for matchup, damage_values in damages.items():
  121.         output += "%s: %s\n" % (matchup, damage_values)
  122.  
  123.     print(output)
  124.  
  125. def print_team(team):
  126.     output = "cur member: %s\n" % team.cur_member
  127.     for team_member in team.mons:
  128.         output += "%s: hp=%s, speed=%s\n" % (team_member, team.mons[team_member].hp, team.mons[team_member].speed)
  129.  
  130.     print(output)
  131.  
  132. def print_teams_info(team_1, team_2, damages):
  133.     print_damages(damages)
  134.     print("Team 1:")
  135.     print_team(team_1)
  136.     print("Team 2:")
  137.     print_team(team_2)
  138.  
  139. def can_switch(team, member):
  140.     if not member in team.mons:
  141.         return False
  142.     if member == team.cur_member:
  143.         return False
  144.     if team.mons[member].hp <= 0:
  145.         return False
  146.     return True
  147.  
  148. def decide_opponent_action(team_1, team_2, damages):
  149.     # greedy
  150.     damage_list = damages[team_2.cur_member + team_1.cur_member]
  151.     return "%s" % (damage_list.index(max(damage_list)) + 1)
  152.  
  153. def decide_player_action(team_1):
  154.     move_actions = set(("1", "2", "3", "4"))
  155.     while True:
  156.         action = input("Choose move or switch: ")
  157.         if action in team_1.mons and can_switch(team_1, action):
  158.             return action
  159.         elif action in move_actions:
  160.             return action
  161.         else:
  162.             print("Invalid action!")
  163.  
  164. def get_opponent_switch_in(team_1, team_2, damages):
  165.     max_damage = 0
  166.     max_damage_team_member = None
  167.  
  168.     for team_member in team_2.mons:
  169.         this_max_damage = max(damages[team_member + team_1.cur_member])
  170.         if this_max_damage > max_damage:
  171.             max_damage = this_max_damage
  172.             max_damage_team_member = team_member
  173.  
  174.     return team_member
  175.  
  176. def get_player_switch_in(team_1):
  177.     while True:
  178.         action = input("Choose switch: ")
  179.         if action in team_1.mons and can_switch(team_1, action):
  180.             return action
  181.         else:
  182.             print("Invalid switch!")
  183.  
  184. def remove_cur_mon(team, damages):
  185.     del team.mons[team.cur_member]
  186.     new_damages = dict(damages)
  187.     for matchup, damage_value in damages.items():
  188.         if team.cur_member in matchup:
  189.             del new_damages[matchup]
  190.     return new_damages
  191.  
  192. def do_battle_until_ko():
  193.     team_1_original, team_2_original, damages_original = generate_random_teams()
  194.  
  195.     while True:
  196.         damages = dict(damages_original)
  197.         team_1 = copy.deepcopy(team_1_original)
  198.         team_2 = copy.deepcopy(team_2_original)
  199.  
  200.         while True:
  201.             print_teams_info(team_1, team_2, damages)
  202.             opponent_action = decide_opponent_action(team_1, team_2, damages)
  203.             player_action = decide_player_action(team_1)
  204.             player_switched = False
  205.             opponent_switched = False
  206.            
  207.             if player_action in team_1.mons:
  208.                 team_1.cur_member = player_action
  209.                 player_switched = True
  210.             if opponent_action in team_2.mons:
  211.                 team_2.cur_member = opponent_action
  212.                 opponent_switched = True
  213.    
  214.             if not player_switched:
  215.                 team_1_damage = damages[team_1.cur_member + team_2.cur_member][int(player_action) - 1]
  216.             if not opponent_switched:
  217.                 team_2_damage = damages[team_2.cur_member + team_1.cur_member][int(opponent_action) - 1]
  218.    
  219.             if not player_switched and not opponent_switched:
  220.                 if team_1.cur_mon.speed > team_2.cur_mon.speed:
  221.                     team_2.cur_mon.hp -= team_1_damage
  222.                     if team_2.cur_mon.hp > 0:
  223.                         team_1.cur_mon.hp -= team_2_damage
  224.                 else:
  225.                     team_1.cur_mon.hp -= team_2_damage
  226.                     if team_1.cur_mon.hp > 0:
  227.                         team_2.cur_mon.hp -= team_1_damage
  228.             else:
  229.                 if not player_switched:
  230.                     team_2.cur_mon.hp -= team_1_damage
  231.                 if not opponent_switched:
  232.                     team_1.cur_mon.hp -= team_2_damage
  233.    
  234.             if team_1.cur_mon.hp <= 0:
  235.                 damages = remove_cur_mon(team_1, damages)
  236.                 if len(team_1.mons) == 0:
  237.                     break
  238.                 print_teams_info(team_1, team_2, damages)
  239.                 team_1.cur_member = get_player_switch_in(team_1)
  240.             elif team_2.cur_mon.hp <= 0:
  241.                 damages = remove_cur_mon(team_2, damages)
  242.                 if len(team_2.mons) == 0:
  243.                     break
  244.                 team_2.cur_member = get_opponent_switch_in(team_1, team_2, damages)
  245.    
  246.         print_teams_info(team_1, team_2, damages)
  247.         continue_answer = input("Continue? ")
  248.         if continue_answer != "yes" and continue_answer != "y":
  249.             break
  250.  
  251. do_battle_until_ko()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement