Guest User

boxer evolution

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