Guest User

Untitled

a guest
Mar 31st, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. import time
  3. import sys
  4. import math
  5.  
  6. female_names = ["Tonya","Jane","Jessica","Amy","Susan","Tammy","Erica", "Joselyn","Veronica","Laura","Betsy"]
  7. male_names = ["James","Jason","Michael","Christoper","Eric","Cameron","Ralph","Tony","John","Timothy","Craig"]
  8. family_names = ["Jones","Smith","Malloy","Potter","Miller","Doe","Simmons","Johnson","Conner","Monk","Simpson","Mars"]
  9.  
  10. def replenish(fighter):
  11. fighter.fight_endurance = fighter.base_endurance
  12. fighter.fight_strength = fighter.base_strength
  13. fighter.fight_speed = fighter.base_speed
  14. fighter.fight_constitution = fighter.base_constitution
  15. fighter.health = fighter.base_health
  16.  
  17. def chance(end):
  18. try:
  19. chance_ = random.randrange(1,end+1)
  20. if chance_ == end/2:
  21. return True
  22. return False
  23. except:
  24. print "int must be 2 or higher"
  25. sys.exit()
  26.  
  27. class simpleBoxer: # The base boxer, only used in the beginning to spawn initial set of boxers.
  28. def __init__(self,sex):
  29. self.sex = sex
  30. self.base_endurance = 10
  31. self.base_strength = 10
  32. self.base_speed = 10
  33. self.base_aggression = 10
  34. self.base_defense = 10
  35. self.base_constitution = 10
  36. self.base_health = 100.0
  37. self.health = self.base_health
  38. self.tournaments = 0
  39.  
  40. if sex == "male":
  41. self.name = (male_names[random.randrange(0,len(male_names)-1)],family_names[random.randrange(0,len(family_names)-1)])
  42. else:
  43. self.name =(female_names[random.randrange(0,len(female_names)-1)],family_names[random.randrange(0,len(family_names)-1)])
  44.  
  45. replenish(self)
  46.  
  47. def __str__(self):
  48. return str(self.name[0]) + ' ' + str(self.name[1]) +":\nendurance is %s\nstrength is %s \nspeed is %s\n\
  49. aggression is %s\ndefense is %s\nconstitution is %s\n" % \
  50. (self.base_endurance, self.base_strength, self.base_speed, self.base_aggression, self.base_defense, self.base_constitution)
  51.  
  52.  
  53. class Boxer: # spawns with avg of stats from each parent and has 10% chance for either a positive or negative mutation with each stat.
  54. def __init__(self,parent1,parent2):
  55. self.base_endurance = (int)(math.ceil((parent1.base_endurance + parent2.base_endurance)/2.))
  56. self.base_strength = (int)(math.ceil((parent1.base_strength + parent2.base_strength)/2.))
  57. self.base_speed = (int)(math.ceil((parent1.base_speed + parent2.base_speed)/2.))
  58. self.base_aggression = (int)(math.ceil((parent1.base_aggression + parent2.base_aggression)/2.))
  59. self.base_defense = (int)(math.ceil((parent1.base_defense + parent2.base_defense)/2.))
  60. self.base_constitution = (int)(math.ceil((parent1.base_constitution + parent2.base_constitution)/2.))
  61.  
  62. if chance(10):
  63. self.base_endurance = random.randrange(self.base_endurance -2, self.base_endurance +3)
  64. if chance(10):
  65. self.base_strength = random.randrange(self.base_strength -2, self.base_strength +3)
  66. if chance(10):
  67. self.base_speed = random.randrange(self.base_speed -2, self.base_speed +3)
  68. if chance(10):
  69. self.base_aggression = random.randrange(self.base_aggression -2, self.base_aggression +3)
  70. if chance(10):
  71. self.base_defense = random.randrange(self.base_defense -2, self.base_defense +3)
  72. if chance(10):
  73. self.base_constitution = random.randrange(self.base_constitution -2, self.base_constitution +3)
  74. self.base_health = 100.0
  75. self.health = self.base_health
  76. sex = chance(2)
  77. if parent1.sex == "male":
  78. last = parent1.name[1]
  79. else:
  80. last = parent2.name[1]
  81. if sex:
  82. self.sex = "male"
  83. self.name = (male_names[random.randrange(0,len(male_names)-1)],last)
  84. else:
  85. self.sex = "female"
  86. self.name =(female_names[random.randrange(0,len(female_names)-1)],last)
  87. self.tournaments = 0
  88.  
  89. replenish(self)
  90.  
  91.  
  92. def __str__(self):
  93. return str(self.name[0]) + ' ' + str(self.name[1]) +":\nendurance is %s\nstrength is %s \nspeed is %s\n\
  94. aggression is %s\ndefense is %s\nconstitution is %s\n" % \
  95. (self.base_endurance, self.base_strength, self.base_speed, self.base_aggression, self.base_defense, self.base_constitution)
  96.  
  97.  
  98.  
  99. males = []
  100. females = []
  101. main_pool = []
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. def fight(fighter1,fighter2):
  110. def attack_resolution(attack,attacker,defender): #how every single attack is resolved
  111. if attacker.fight_endurance > 0.0:
  112. attacker.fight_endurance -= attack/20.0
  113. if attack == 1:
  114. speed = attacker.fight_speed
  115. elif attack == 2:
  116. speed = attacker.fight_speed/2.0
  117. else:
  118. speed = attacker.fight_speed/3.0
  119. attack_roll = (random.randrange(0,75) + speed)
  120. defend_roll = (random.randrange(0,75) + defender.base_defense)
  121. if attack_roll > defend_roll:
  122. defender.health -= attack+(attacker.fight_strength/5.0)
  123. defender.fight_endurance -= attack/10.0
  124. else:
  125. attacker.fight_endurance -= attack/20.0
  126. def winner(fighter1,fighter2):
  127. if fighter1.health > fighter2.health:
  128. return fighter1,fighter2
  129. else:
  130. return fighter2,fighter1
  131.  
  132. def health_check(fighter1,fighter2):
  133. if fighter1.health >0 and fighter2.health > 0:
  134. return True
  135. return False
  136.  
  137.  
  138.  
  139. def Round(fighter1,fighter2): # generates the amount of attacks and the their strength based on both RNG and boxer stats. Keeps resolving attacks until both fighters are out of attacks for that round. Runs basic maintenance at the end of the round.
  140. number_strikes_f1 = random.randrange(10,20) + fighter1.base_aggression
  141. number_strikes_f2 = random.randrange(10,20) + fighter2.base_aggression
  142. strikelist1 = []
  143. strikelist2 = []
  144. gassed_1 = False
  145. gassed_2= False
  146. for i in range(number_strikes_f1):
  147. strikelist1.append(random.randrange(1,4))
  148. for i in range(number_strikes_f2):
  149. strikelist2.append(random.randrange(1,4))
  150. count = 0
  151. while gassed_1 == False and gassed_2 == False:
  152. if count < len(strikelist1):
  153. attack_resolution(strikelist1[count],fighter1,fighter2)
  154. else:
  155. gassed_1 = True
  156. if count < len(strikelist2):
  157. attack_resolution(strikelist2[count],fighter2,fighter1)
  158. else:
  159. gassed_2 = True
  160. if fighter1.health <=0 or fighter2.health <=0:
  161. break
  162. count+=1
  163.  
  164. if fighter1.health > 0 and fighter2.health > 0:
  165. f1_ratio = (fighter1.health/fighter1.base_health)
  166. fighter1.fight_constitution = fighter1.base_constitution*f1_ratio
  167. f2_ratio = (fighter2.health/fighter2.base_health)
  168. fighter2.fight_constitution = fighter2.base_constitution*f2_ratio
  169. fighter1.health += fighter1.fight_constitution/4.0
  170. fighter1.fight_endurance += fighter1.fight_constitution/10.0
  171. fighter2.health += fighter2.fight_constitution/4.0
  172. fighter2.fight_endurance += fighter2.fight_constitution/10.0
  173.  
  174. for bell in range(10): # each fight is 10 rounds, so simply rounds the round function 10 times and then returns the results.
  175. if health_check(fighter1,fighter2):
  176. Round(fighter1,fighter2)
  177. else:
  178. winner,loser = winner(fighter1,fighter2)
  179. replenish(fighter1)
  180. replenish(fighter2)
  181. return winner,loser
  182. winner,loser = winner(fighter1,fighter2)
  183. replenish(fighter1)
  184. replenish(fighter2)
  185. return winner,loser
  186.  
  187. def Tournament(fighter_list): # Basically has every fighter fight at least once allowing only the winners to progress to the next round. Continues until there's only 1 winner.
  188.  
  189. if len(fighter_list)%2 !=0 or len(fighter_list) < 2:
  190. print "Number of fighters must be even to make a fair card."
  191. return
  192. def bracket(starting_list):
  193. new_list = starting_list
  194. winners = []
  195. losers = []
  196. adding = False
  197. if len(new_list)%2 !=0:
  198. lucky = new_list.pop(random.randrange(0,len(new_list)-1))
  199. adding = True
  200.  
  201. count = 0
  202. while count < len(new_list):
  203. results = fight(new_list[count],new_list[count+1])
  204.  
  205. try:
  206. winners.append(results[0])
  207. losers.append(results[1])
  208. count +=2
  209. except:
  210. #print results[0]
  211. #print results[1]
  212. print count, len(new_list)
  213. sys.exit()
  214. if adding:
  215. winners.append(lucky)
  216. return (winners,losers)
  217.  
  218.  
  219. results = bracket(fighter_list)
  220. winner_list = results[0]
  221. loser_list = results[1]
  222. if len(winner_list) > 1:
  223. results = bracket(winner_list)
  224. progression_list = results[0]
  225. else:
  226. print "too small a pool of fighters"
  227. return
  228. while len(progression_list) >1:
  229. results = bracket(progression_list)
  230. progression_list = results[0]
  231. return progression_list[0], winner_list,loser_list
  232.  
  233. def cleanup(losers): # Removes boxers slated for deletion from several lists.
  234. for each in losers:
  235. try:
  236. males.remove(each)
  237. except:
  238. pass
  239. try:
  240. females.remove(each)
  241. except:
  242. pass
  243. try:
  244. main_pool.remove(each)
  245. except:
  246. pass
  247.  
  248. def baby_making(champ,loser_count): #creates new fighters to offset those recently deleted due to poor performance.
  249. new_males = []
  250. new_females = []
  251. count = loser_count
  252. if champ.sex == "male":
  253. boxer = Boxer(champ,females[random.randrange(0,len(females)-1)])
  254. else:
  255. boxer = Boxer(champ,males[random.randrange(0,len(males)-1)])
  256. if boxer.sex == 'male':
  257. males.append(boxer)
  258. else:
  259. females.append(boxer)
  260.  
  261. main_pool.append(boxer)
  262. count -=1
  263. while count > 0:
  264. boxer = Boxer(males[random.randrange(0,len(males)-1)],females[random.randrange(0,len(females)-1)])
  265. if boxer.sex == 'male':
  266. males.append(boxer)
  267. else:
  268. females.append(boxer)
  269.  
  270. main_pool.append(boxer)
  271. count -=1
  272.  
  273. def loser_tourney(fighter_list): # Gives the tournament first round losers a second chance. If they win they don't create offspring this tournament but they're allowed to fight in the next tournament.
  274. def bracket(starting_list):
  275. new_list = starting_list
  276. winners = []
  277. losers = []
  278. adding = False
  279. if len(new_list)%2 !=0:
  280. lucky = new_list.pop(random.randrange(0,len(new_list)-1))
  281. adding = True
  282.  
  283. count = 0
  284. while count < len(new_list):
  285. results = fight(new_list[count],new_list[count+1])
  286.  
  287. try:
  288. winners.append(results[0])
  289. losers.append(results[1])
  290. count +=2
  291. except:
  292. #print results[0]
  293. #print results[1]
  294. print count, len(new_list)
  295. sys.exit()
  296. if adding:
  297. winners.append(lucky)
  298. return winners,losers
  299.  
  300.  
  301. return bracket(fighter_list)
  302.  
  303.  
  304. def generational_tourney(iterations,participant_count,spam): # allows you to run as many consecutive tournaments as you'd like.
  305. if participant_count %2 == 0 and participant_count == int(participant_count):
  306. for i in range (participant_count/2):
  307. female_boxer = simpleBoxer("female")
  308. females.append(female_boxer)
  309. main_pool.append(female_boxer)
  310.  
  311. for i in range(participant_count/2):
  312. male_boxer = simpleBoxer("male")
  313. main_pool.append(male_boxer)
  314. males.append(male_boxer)
  315. for i in range(iterations):
  316. champion,winners,losers = Tournament(main_pool)
  317. if spam:
  318. print "\nTournament #"+str(i+1)
  319. print champion
  320. print "-------------------"
  321. else:
  322. print "Tournament #"+str(i+1) + " complete."
  323. live,die = loser_tourney(losers) # runs the loser bracket once. Essentially if you lose the tournament fight you get put into this bracket, then all the losers are put into a fight against another loser. If they lose they get removed from the program and the "baby_making" function replaces them with new boxers.
  324. cleanup(die)
  325. baby_making(champion,len(die))
  326. print "\n-_-_-_-_-_-_-_-_-_-\n"
  327. print "Final tournament champion after " + str(iterations) + " tournaments and competing against " + str(participant_count) + " other boxers is:"
  328. print champion
  329. else:
  330. print "Number of participants must be an even whole number for fair cards"
  331. def information_acquisition():
  332. participants = int(raw_input("How many boxers would you like to have fight it out? (Even/whole numbers only)\n"))
  333. iterations = int(raw_input("How many tournaments shall we have?\n"))
  334. spam = raw_input("Would you like to see every tournament winner?(Y/N)\n")
  335. spam = spam.lower()
  336. if spam == "y":
  337. spam = True
  338. else:
  339. spam = False
  340.  
  341. generational_tourney(iterations,participants,spam)
  342.  
  343. if __name__ == "__main__":
  344. while True:
  345. x = raw_input("\nWould you like to run the simulation?(Y/N)\n")
  346. x = x.lower()
  347. if x == 'y':
  348. x = True
  349. else:
  350. x = False
  351. if x:
  352. information_acquisition()
  353. else:
  354. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment