Use Python to simulate an encounter between two factions in martial arts novels.
Everyone has a name, HP (HP), a series of skills and their corresponding attack output. Everyone’s attack method is the same, that is, a skill is randomly selected from their skill pack to attack. The two factions (named by themselves) are inherited
from the parent of martial arts, but each has its own skill pack and corresponding output value. The skill packs owned by everyone under the martial art are randomly assigned when the object is instantiated.
At the time of the encounter, the number of each gang was uncertain. By way of turn-fighting, i.e. the whole person A is to send a randomly selected person B in gang attack, the output value of the damage skill B A randomly selected value plus
20% of the standard normal distribution The random number generated is rounded down, that is, damage value = output value (1+0.2*N(0,1)), and the probability of a skill being selected is inversely proportional to its corresponding output value. If someone has two skills, If the output value is 10 and 30 respectively,
the probability of being selected for skill 1 is (1/10)/(1/10 + 1/30). Anyone participating in the battle of a certain school will die (that is, the HP is reduced to 0) The battle is over.
Several random number generation functions: first introduce the random package, import random; random.random() generates a 0-1 uniformly distributed random number; random.randint(a,b) generates a random number with a confidence range of (ab))
import random # Import library
import numpy as np
import math
from matplotlib import pyplot as plt
shaolin = {"Xuan Nan": 135, "Xuan Ji": 132, "Xuan Zang": 136, "Xuan Ku": 137," Xuan Du": 131} # People and their HP
carefree = {"Kang Guang Ling": 91, "Fan Bailing": 93, "薛慕华": 108} #人及HP
s_skill = {"Great King Kong Palm": 20, "Tathagata Thousand Hands": 30, "Capture Dragon Skill": 25, "King Kong Finger": 30, "Shaolin Changquan": 35} # Skill and its output value
c_skill = {"Small Phaseless Skill": 20, "Tianshan Zhuangmei Shou": 30, "Eight Wilderness and Six Combinations Only My Unique Skill": 25, "Heavenly Six Sun Palm": 30} # Skill and its output value
d_sname = {}
d_cname = {}
s_suiji = {}
c_suiji = {}
class Wulinpeople():
def __init__(self):
self.s_name = random.sample(list(s_skill),random.randrange(1,len(s_skill)+1))
self.c_name = random.sample(list(c_skill),random.randrange(1,len(c_skill)+1))
def Shaolin_Temple(self):
s_name = random.sample(list(shaolin),1)[0] # Randomly select the character of the attacker
s_hp = shaolin[s_name] # Randomly select the hp of the attacker
c_name = random.sample(list(carefree),1)[0] # Randomly select the character of the attacked party
c_hp = carefree[c_name] # randomly select the hp of the attacked person
pt = d_sname[s_name] # randomly assigned skill pack
qwq = random.random ()
for x in pt: # The output value of the randomly assigned skill pack
s_suiji[x]=s_skill[x]
a = list(s_suiji.values())
c = 0
key = 0
s_kill_name = "" #define the variable of random skill name
for i in s_suiji:
i_1 = s_skill[i]
b = 0
for j in a:
b += 1/j
key += 1/i_1 / b
# print("c value:{} key value{}".format(c,key))
if c < qwq < key:
s_kill_name = i # The skill name assigned according to the inverse probability of the output value
# print('Random value: (), skill name: (), skill output value ()'.format(qwq,i,i_1))
c = key
# s_kill_name = random.sample(pt, 1)[0] # randomly assigned skills
s_kill_hp = int(s_skill[s_kill_name]) # randomly assigned skill output value
random3 = np.random.randn() # Randomly generate a random number that obeys the normal distribution
hurt = math.floor(s_kill_hp * (1 + 0.2*random3)) #The damage value rounded down
# print("The damage is {:0.1f}".format(hurt))
print("The Shaolin monk {} used a trick {} that hurts {}".format(s_name,s_kill_hp,s_kill_name))
c_kill_hp = c_hp - hurt
if c_kill_hp <= 0:
del carefree[c_name] # delete key'shaolin'
print("Xiaoyao faction{} suffered {} damage, remaining HP{}".format(c_name, hurt, 0))
print("Xiaoyao faction{} died in action!!!".format(c_name))
else:
carefree[c_name] = c_kill_hp
print("Xiaoyao faction{} suffered {} points of damage, remaining HP{}".format(c_name, hurt, c_kill_hp))
def Carefree(self):
c_name = random.sample(list(carefree),1)[0] # randomly select the character of the attacker
c_hp = carefree[c_name] # Randomly select the hp of the attacker
s_name = random.sample(list(shaolin),1)[0] # Randomly select the character of the attacked party
s_hp = shaolin[s_name] # Randomly select the hp of the attacked person
pt = d_sname[c_name] # randomly assigned skill pack
qwq = random.random ()
for x in pt: # The output value of the randomly assigned skill pack
c_suiji[x] = c_skill[x]
a = list(c_suiji.values())
c = 0
key = 0
c_kill_name = "" #define the variable of random skill name
for i in c_suiji:
i_1 = c_skill[i]
b = 0
for j in a:
b += 1/j
key += 1/i_1 / b
# print("c value:{} key value{}".format(c,key))
if c < qwq < key:
c_kill_name = i # The skill name assigned according to the inverse probability of the output value
# print('Random value: (), skill name: (), skill output value ()'.format(qwq,i,i_1))
c = key
# s_kill_name = random.sample(pt, 1)[0] # randomly assigned skills
c_kill_hp = int(c_skill[c_kill_name]) # randomly assigned skill output value
random3 = np.random.randn() # Randomly generate a random number that obeys the normal distribution
hurt = math.floor(c_kill_hp * (1 + 0.2*random3)) #The damage value rounded down
# print("The damage is {:0.1f}".format(hurt))
print("Xiaoyao faction {} used a trick {} that hurt {}".format(c_name,c_kill_hp,c_kill_name))
s_kill_hp = s_hp - hurt
if s_kill_hp <= 0:
del shaolin[s_name] # delete key'shaolin'
print("The Shaolin monk {} suffered {} points of damage, and the remaining HP{}".format(s_name, hurt, 0))
print("The Shaolin monk{} died in action!!!".format(s_name))
else:
shaolin[s_name] = s_kill_hp
print("The Shaolin monk {} suffered {} points of damage, and the remaining HP{}".format(s_name, hurt, s_kill_hp))
pass
def Battle():
sign = True
while sign:
# Shaolin Temple
Wulinpeople.Shaolin_Temple(Wulinpeople)
if not shaolin or not carefree: #Judge whether both sides are completely killed
if shaolin:
print("The battle is over, Shaolin Temple wins! The ones who stayed on the battlefield are:")
for i in shaolin:
print("Shaolin monk{}Remaining HP{}".format(i,shaolin[i]))
sign = False
else:
print("The battle is over, the happy faction wins! The ones who stayed on the battlefield are:")
for i in carefree:
print("Xiaoyao send people{}Remaining HP{}".format(i,carefree[i]))
sign = False
if not sign: # end the program
break
# Xiaoyao faction
Wulinpeople.Carefree(Wulinpeople)
def main():
print("The battle has begun! The characters are:")
for i in range(len(shaolin)+len(carefree)):
a = Wulinpeople()
if i<len(shaolin):
kill = ",".join(a.s_name)
d_sname[list(shaolin)[i]] = a.s_name
print("The Shaolin monks of Shaolin Temple{}, HP{}, the trick is {}".format(list(shaolin)[i],shaolin[list(shaolin)[i]],kill))
else:
i = i-len(shaolin)
kill = ",".join(a.c_name)
d_sname[list(carefree)[i]] = a.c_name
print("Xiaoyao Pai of Xiaoyao Pai{}, HP{}, the trick is {}".format(list(carefree)[i],carefree[list(carefree)[i]],kill))
Battle()
main()
# 1) Generate random numbers
rdm = np.random.randn(10000) # Randomly generate 10000 random numbers that obey normal distribution
# 2) Result verification
import seaborn as sns # Use seaborn library to draw histogram to verify the result
sns.set_palette("hls") # Set the color of all pictures, use hls color space
sns.distplot(rdm,color="r",bins=1000,kde=True) # Draw a histogram, color sets the color, bins sets the number of divisions of the histogram
plt.show()
If you are idle and bored, writing and playing, this is the highest state of fishing!
You can join the chat answering exchange base of the little brother!