Advertisement
Guest User

Clan Wars III

a guest
Dec 6th, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.35 KB | None | 0 0
  1. import random
  2. import operator
  3.  
  4. class Game():
  5.     def __init__(self):
  6.         self.wars = []
  7.         self.place_trophies = [25, 20, 15, 10, 5, 5, 0, 0, 0, 0,
  8.                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  9.                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  10.                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  11.                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12.                           -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  13.                           -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  14.                           -5, -5, -5, -5, -5, -10, -10, -10, -10, -10,
  15.                           -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
  16.                           -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
  17.                           -20, -20, -20, -20, -20, -25, -25, -25, -25, -25]
  18.  
  19.         for _ in range(100):
  20.             self.wars.append(War(self.place_trophies))
  21.  
  22.     def war_day(self, noprint=False):
  23.         for war in self.wars:
  24.             war.war_day(noprint)
  25.  
  26.     def sort(self, arg, listt):
  27.         listt.sort(key=operator.attrgetter(arg), reverse=True)
  28.  
  29.     def get_num_0(self):
  30.         all0 = []
  31.         for war in self.wars:
  32.             for clan in war.clans:
  33.                 if clan.trophies == 0:
  34.                     all0.append(clan)
  35.  
  36.         return all0
  37.  
  38.     def get_num_wins(self):
  39.         wins = []
  40.         for war in self.wars:
  41.             for clan in war.clans:
  42.                 all0.append(clan.total_wins)
  43.  
  44.         wins.sort()
  45.  
  46.         return wins
  47.  
  48.     def pprint(self):
  49.         all = []
  50.         for war in self.wars:
  51.             for i in range(100):
  52.                 all.append(war.clans[i])
  53.  
  54.         self.sort('trophies', all)
  55.  
  56.         print(all)
  57.  
  58. class War():
  59.     def __init__(self, place_trophies):
  60.         self.clans = []
  61.         self.place_trophies = place_trophies
  62.         self.num_wars = 0
  63.         for _ in range(100):
  64.             self.clans.append(Clan())
  65.  
  66.     def sort(self, arg):
  67.             self.clans.sort(key=operator.attrgetter(arg), reverse=True)
  68.             #return sorted(self.clans, key=operator.attrgetter(arg))
  69.  
  70.     def war_day(self, noprint):
  71.         for clan in self.clans:
  72.             clan.war_day()
  73.  
  74.         self.sort('wins')
  75.  
  76.         for i in range(100):
  77.             self.clans[i].trophies += self.clans[i].wins + self.place_trophies[i]
  78.  
  79.             if self.clans[i].trophies < 0:
  80.                 self.clans[i].trophies = 0
  81.  
  82.             self.clans[i].day_3()
  83.  
  84.         self.sort('trophies')
  85.         if not noprint:
  86.             print(self.clans, '\n\n')
  87.         self.num_wars += 1
  88.  
  89.         if self.num_wars % 10 == 0:
  90.             for clan in self.clans:
  91.                 clan.new_season()
  92.  
  93. class Clan():
  94.     def __init__(self):
  95.         self.players = []
  96.         self.trophies = 0
  97.         self.total_wins = 1
  98.         self.wins = 0
  99.         self.skill = random.triangular(.4, .7, .5)
  100.         self.war_history = []
  101.         for _ in range(int(random.triangular(35, 50, 47))):
  102.             self.players.append(Player(self.skill))
  103.  
  104.     def war_day(self):
  105.         num_left = 60
  106.         for player in self.players:
  107.             if player.battle(Player()) == 'win':
  108.                 self.wins += 1
  109.                 self.total_wins += 1
  110.                 player.wins += 1
  111.                 num_left -= 1
  112.  
  113.         sort = sorted(self.players, key=operator.attrgetter('winrate'), reverse=True)
  114.         for player in sort[:num_left]:
  115.             if player.battle(Player()) == 'win':
  116.                 self.wins += 1
  117.                 self.total_wins += 1
  118.                 player.wins += 1
  119.  
  120.     def day_3(self):
  121.         data = {'wins': self.wins, 'current trophies': self.trophies}
  122.         self.war_history.append(data)
  123.  
  124.         self.wins = 0
  125.  
  126.     def new_season(self):
  127.         if self.trophies > 1000:
  128.             self.trophies -= 1000
  129.             self.trophies //= 2
  130.             self.trophies += 1000
  131.  
  132.     def get_avg_winrate(self):
  133.         l = []
  134.         for p in self.players:
  135.             l.append(p.winrate)
  136.         return sum(l) / len(l)
  137.  
  138.     def __repr__(self):
  139.         return f"Trophies: {self.trophies}   "
  140.  
  141.  
  142. class Player():
  143.     def __init__(self, clan_skill=.5):
  144.         self.winrate = random.triangular(clan_skill - .2, clan_skill + .3, clan_skill)
  145.         self.wins = 0
  146.  
  147.     def battle(self, opponent):
  148.         if random.random() < .75: # Miss war
  149.             return 'lose'
  150.  
  151.         if self.winrate > opponent.winrate:
  152.             return 'win'
  153.         elif self.winrate < opponent.winrate:
  154.             return 'lose'
  155.         else:
  156.             if random.random() < .5:
  157.                 return 'win'
  158.             else:
  159.                 return 'lose'
  160.  
  161.  
  162.  
  163. game = Game()
  164.  
  165.  
  166. while True:
  167.     key = input()
  168.     war = game.wars[0]
  169.  
  170.     if key == '':
  171.         game.war_day('noprint')
  172.         print(war.num_wars)
  173.         continue
  174.     elif key == 'p':
  175.         game.war_day()
  176.         print(war.num_wars)
  177.         continue
  178.     elif key == 'g':
  179.         print(len(game.get_num_0()))
  180.         continue
  181.     elif key == 'w':
  182.         print(game.get_num_wins())
  183.         continue
  184.     elif key == 'a':
  185.         game.pprint()
  186.  
  187.     # try:
  188.     #     print(war.clans[int(key)].war_history)
  189.     #     print(war.clans[int(key)].skill)
  190.     #     print(len(war.clans[int(key)].players))
  191.     #     print(war.clans[int(key)].get_avg_winrate())
  192.     # except:
  193.     #     pass
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement