Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- import sys
- female_names = ["Tonya","Jane","Jessica","Amy","Susan","Tammy","Erica", "Joselyn","Veronica","Laura","Betsy"]
- male_names = ["James","Jason","Michael","Christoper","Eric","Cameron","Ralph","Tony","John","Timothy","Craig"]
- family_names = ["Jones","Smith","Malloy","Potter","Miller","Doe","Simmons","Johnson","Conner","Monk","Simpson","Mars"]
- def replenish(fighter):
- fighter.fight_endurance = fighter.base_endurance
- fighter.fight_strength = fighter.base_strength
- fighter.fight_speed = fighter.base_speed
- fighter.fight_constitution = fighter.base_constitution
- fighter.health = fighter.base_health
- def chance(end):
- try:
- chance_ = random.randrange(1,end+1)
- if chance_ == end/2:
- return True
- return False
- except:
- print "int must be 2 or higher"
- sys.exit()
- class simpleBoxer: # The base boxer, only used in the beginning to spawn initial set of boxers.
- def __init__(self,sex):
- self.sex = sex
- self.base_endurance = 10
- self.base_strength = 10
- self.base_speed = 10
- self.base_aggression = 10
- self.base_defense = 10
- self.base_constitution = 10
- self.base_health = 100.0
- self.health = self.base_health
- self.tournaments = 0
- if sex == "male":
- self.name = (male_names[random.randrange(0,len(male_names)-1)],family_names[random.randrange(0,len(family_names)-1)])
- else:
- self.name =(female_names[random.randrange(0,len(female_names)-1)],family_names[random.randrange(0,len(family_names)-1)])
- replenish(self)
- def __str__(self):
- return str(self.name[0]) + ' ' + str(self.name[1]) +":\nendurance is %s\nstrength is %s \nspeed is %s\n\
- aggression is %s\ndefense is %s\nconstitution is %s\n" % \
- (self.base_endurance, self.base_strength, self.base_speed, self.base_aggression, self.base_defense, self.base_constitution)
- class Boxer: # spawns with avg of stats from each parent and has 10% chance for either a positive or negative mutation with each stat.
- def __init__(self,parent1,parent2):
- self.base_endurance = (parent1.base_endurance + parent2.base_endurance)/2
- self.base_strength = (parent1.base_strength + parent2.base_strength)/2
- self.base_speed = (parent1.base_speed + parent2.base_speed)/2
- self.base_aggression = (parent1.base_aggression + parent2.base_aggression)/2
- self.base_defense = (parent1.base_defense + parent2.base_defense)/2
- self.base_constitution = (parent1.base_constitution + parent2.base_constitution)/2
- if chance(10):
- self.base_endurance = random.randrange(self.base_endurance -2, self.base_endurance +3)
- if chance(10):
- self.base_strength = random.randrange(self.base_strength -2, self.base_strength +3)
- if chance(10):
- self.base_speed = random.randrange(self.base_speed -2, self.base_speed +3)
- if chance(10):
- self.base_aggression = random.randrange(self.base_aggression -2, self.base_aggression +3)
- if chance(10):
- self.base_defense = random.randrange(self.base_defense -2, self.base_defense +3)
- if chance(10):
- self.base_constitution = random.randrange(self.base_constitution -2, self.base_constitution +3)
- self.base_health = 100.0
- self.health = self.base_health
- sex = chance(2)
- if parent1.sex == "male":
- last = parent1.name[1]
- else:
- last = parent2.name[1]
- if sex:
- self.sex = "male"
- self.name = (male_names[random.randrange(0,len(male_names)-1)],last)
- else:
- self.sex = "female"
- self.name =(female_names[random.randrange(0,len(female_names)-1)],last)
- self.tournaments = 0
- replenish(self)
- def __str__(self):
- return str(self.name[0]) + ' ' + str(self.name[1]) +":\nendurance is %s\nstrength is %s \nspeed is %s\n\
- aggression is %s\ndefense is %s\nconstitution is %s\n" % \
- (self.base_endurance, self.base_strength, self.base_speed, self.base_aggression, self.base_defense, self.base_constitution)
- males = []
- females = []
- main_pool = []
- def fight(fighter1,fighter2):
- def attack_resolution(attack,attacker,defender): #how every single attack is resolved
- if attacker.fight_endurance > 0.0:
- attacker.fight_endurance -= attack/20.0
- if attack == 1:
- speed = attacker.fight_speed
- elif attack == 2:
- speed = attacker.fight_speed/2.0
- else:
- speed = attacker.fight_speed/3.0
- attack_roll = (random.randrange(0,75) + speed)
- defend_roll = (random.randrange(0,75) + defender.base_defense)
- if attack_roll > defend_roll:
- defender.health -= attack+(attacker.fight_strength/5.0)
- defender.fight_endurance -= attack/10.0
- else:
- attacker.fight_endurance -= attack/20.0
- def winner(fighter1,fighter2):
- if fighter1.health > fighter2.health:
- return fighter1,fighter2
- else:
- return fighter2,fighter1
- def health_check(fighter1,fighter2):
- if fighter1.health >0 and fighter2.health > 0:
- return True
- return False
- 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.
- number_strikes_f1 = random.randrange(10,20) + fighter1.base_aggression
- number_strikes_f2 = random.randrange(10,20) + fighter2.base_aggression
- strikelist1 = []
- strikelist2 = []
- gassed_1 = False
- gassed_2= False
- for i in range(number_strikes_f1):
- strikelist1.append(random.randrange(1,4))
- for i in range(number_strikes_f2):
- strikelist2.append(random.randrange(1,4))
- count = 0
- while gassed_1 == False and gassed_2 == False:
- if count < len(strikelist1):
- attack_resolution(strikelist1[count],fighter1,fighter2)
- else:
- gassed_1 = True
- if count < len(strikelist2):
- attack_resolution(strikelist2[count],fighter2,fighter1)
- else:
- gassed_2 = True
- if fighter1.health <=0 or fighter2.health <=0:
- break
- count+=1
- if fighter1.health > 0 and fighter2.health > 0:
- f1_ratio = (fighter1.health/fighter1.base_health)
- fighter1.fight_constitution = fighter1.base_constitution*f1_ratio
- f2_ratio = (fighter2.health/fighter2.base_health)
- fighter2.fight_constitution = fighter2.base_constitution*f2_ratio
- fighter1.health += fighter1.fight_constitution/4.0
- fighter1.fight_endurance += fighter1.fight_constitution/10.0
- fighter2.health += fighter2.fight_constitution/4.0
- fighter2.fight_endurance += fighter2.fight_constitution/10.0
- for bell in range(10): # each fight is 10 rounds, so simply rounds the round function 10 times and then returns the results.
- if health_check(fighter1,fighter2):
- Round(fighter1,fighter2)
- else:
- winner,loser = winner(fighter1,fighter2)
- replenish(fighter1)
- replenish(fighter2)
- return winner,loser
- winner,loser = winner(fighter1,fighter2)
- replenish(fighter1)
- replenish(fighter2)
- return winner,loser
- 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.
- if len(fighter_list)%2 !=0 or len(fighter_list) < 2:
- print "Number of fighters must be even to make a fair card."
- return
- def bracket(starting_list):
- new_list = starting_list
- winners = []
- losers = []
- adding = False
- if len(new_list)%2 !=0:
- lucky = new_list.pop(random.randrange(0,len(new_list)-1))
- adding = True
- count = 0
- while count < len(new_list):
- results = fight(new_list[count],new_list[count+1])
- try:
- winners.append(results[0])
- losers.append(results[1])
- count +=2
- except:
- #print results[0]
- #print results[1]
- print count, len(new_list)
- sys.exit()
- if adding:
- winners.append(lucky)
- return (winners,losers)
- results = bracket(fighter_list)
- winner_list = results[0]
- loser_list = results[1]
- if len(winner_list) > 1:
- results = bracket(winner_list)
- progression_list = results[0]
- else:
- print "too small a pool of fighters"
- return
- while len(progression_list) >1:
- results = bracket(progression_list)
- progression_list = results[0]
- return progression_list[0], winner_list,loser_list
- def cleanup(losers): # Removes boxers slated for deletion from several lists.
- for each in losers:
- try:
- males.remove(each)
- except:
- pass
- try:
- females.remove(each)
- except:
- pass
- try:
- main_pool.remove(each)
- except:
- pass
- def baby_making(champ,loser_count): #creates new fighters to offset those recently deleted due to poor performance.
- new_males = []
- new_females = []
- count = loser_count
- if champ.sex == "male":
- boxer = Boxer(champ,females[random.randrange(0,len(females)-1)])
- else:
- boxer = Boxer(champ,males[random.randrange(0,len(males)-1)])
- if boxer.sex == 'male':
- males.append(boxer)
- else:
- females.append(boxer)
- main_pool.append(boxer)
- count -=1
- while count > 0:
- boxer = Boxer(males[random.randrange(0,len(males)-1)],females[random.randrange(0,len(females)-1)])
- if boxer.sex == 'male':
- males.append(boxer)
- else:
- females.append(boxer)
- main_pool.append(boxer)
- count -=1
- 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.
- def bracket(starting_list):
- new_list = starting_list
- winners = []
- losers = []
- adding = False
- if len(new_list)%2 !=0:
- lucky = new_list.pop(random.randrange(0,len(new_list)-1))
- adding = True
- count = 0
- while count < len(new_list):
- results = fight(new_list[count],new_list[count+1])
- try:
- winners.append(results[0])
- losers.append(results[1])
- count +=2
- except:
- #print results[0]
- #print results[1]
- print count, len(new_list)
- sys.exit()
- if adding:
- winners.append(lucky)
- return winners,losers
- return bracket(fighter_list)
- def generational_tourney(iterations,participant_count,spam): # allows you to run as many consecutive tournaments as you'd like.
- if participant_count %2 == 0 and participant_count == int(participant_count):
- for i in range (participant_count/2):
- female_boxer = simpleBoxer("female")
- females.append(female_boxer)
- main_pool.append(female_boxer)
- for i in range(participant_count/2):
- male_boxer = simpleBoxer("male")
- main_pool.append(male_boxer)
- males.append(male_boxer)
- for i in range(iterations):
- champion,winners,losers = Tournament(main_pool)
- if spam:
- print "\nTournament #"+str(i+1)
- print champion
- print "-------------------"
- else:
- print "Tournament #"+str(i+1) + " complete."
- 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.
- cleanup(die)
- baby_making(champion,len(die))
- print "\n-_-_-_-_-_-_-_-_-_-\n"
- print "Final tournament champion after " + str(iterations) + " tournaments and competing against " + str(participant_count) + " other boxers is:"
- print champion
- else:
- print "Number of participants must be an even whole number for fair cards"
- def information_acquisition():
- participants = int(raw_input("How many boxers would you like to have fight it out? (Even/whole numbers only)\n"))
- iterations = int(raw_input("How many tournaments shall we have?\n"))
- spam = raw_input("Would you like to see every tournament winner?(Y/N)\n")
- spam = spam.lower()
- if spam == "y":
- spam = True
- else:
- spam = False
- generational_tourney(iterations,participants,spam)
- if __name__ == "__main__":
- while True:
- x = raw_input("\nWould you like to run the simulation?(Y/N)\n")
- x = x.lower()
- if x == 'y':
- x = True
- else:
- x = False
- if x:
- information_acquisition()
- else:
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment