请原谅这个庞大的代码块,但我根本无法找出哪里出了问题。据我所知这应该管用。焊接清单是随机分配的设备,然后所有匹配值的总和。但出于某种原因,它返回的是几乎随机的大数。我真的需要帮助。你知道吗
import random
from collections import Counter
def getRandomWeightedElement(**data):
rand = random.randint(1, sum(data.values()))
for key, value in data.items():
rand -= value
if rand <= 0:
return key
equipment = {
"nothing" : Counter({"physA":0}),
"woodenShield" : Counter({"physD":1,"fireD":3}),
#Physical:
#Bronze Weapons
"bronzeDag" : Counter({"physA":12}),
"bronzeSword" : Counter({"physA":23,"physD":2}),
"bronzeBAxe" : Counter({"physA":23}),
"bronze2HSword" : Counter({"physA":26}),
"bronzeMace" : Counter({"physA":26}),
"bronzeWarHammer" : Counter({"physA":26}),
#Iron Weapons
"ironDag" : Counter({"physA":15}),
"ironSword" : Counter({"physA":27,"physD":3}),
"ironBAxe" : Counter({"physA":27}),
"iron2HSword" : Counter({"physA":31}),
"ironMace" : Counter({"physA":31}),
"ironWarHammer" : Counter({"physA":31}),
#Bronze Armor
"bronzeShield" : Counter({"physD":10,"rangD":15,"magD":5}),
"bronzeMedHelm" : Counter({"physD":2,"rangD":4,"magD":1}),
"bronzeFullHelm" : Counter({"physD":4,"rangD":6,"magD":2}),
"bronzeChainbody" : Counter({"physD":6,"rangD":9,"magD":3}),
"bronzePlatebody" : Counter({"physD":7,"rangD":12,"magD":3}),
"bronzeChainlegs" : Counter({"physD":4,"rangD":6,"magD":2}),
"bronzePlatelegs" : Counter({"physD":5,"rangD":8,"magD":2}),
"bronzeBoots" : Counter({"physD":2,"rangD":3,"magD":1}),
#Iron Armor
"ironShield" : Counter({"physD":12,"rangD":18,"magD":6}),
"ironMedHelm" : Counter({"physD":3,"rangD":5,"magD":1}),
"ironFullHelm" : Counter({"physD":4,"rangD":8,"magD":2}),
"ironChainbody" : Counter({"physD":7,"rangD":11,"magD":3}),
"ironPlatebody" : Counter({"physD":9,"rangD":14,"magD":4}),
"ironChainlegs" : Counter({"physD":4,"rangD":8,"magD":2}),
"ironPlatelegs" : Counter({"physD":6,"rangD":9,"magD":3}),
"ironBoots" : Counter({"physD":2,"rangD":4,"magD":1}),
"pineSBow" : Counter({"rangA":22}),
"bronzeNecklace" : Counter({"physD":1}),
"bronzeRing" : Counter({"rangA":0}),
"silverRing" : Counter({"rangA":0})
}
def goblinGen():
"Generates a Goblin, ready to fight"
mainH = getRandomWeightedElement(**{"bronzeDag":40,"pineSBow":25,"bronzeSword":15,"bronzeBAxe":10,"ironSword":10})
offH = getRandomWeightedElement(**{"nothing":50,"woodenShield":40,"bronzeShield":10})
head = getRandomWeightedElement(**{"nothing":80,"bronzeMedHelm":15,"ironMedHelm":5})
neck = getRandomWeightedElement(**{"nothing":90,"bronzeNecklace":10})
chest = getRandomWeightedElement(**{"nothing":60,"bronzeChainbody":40})
legs = getRandomWeightedElement(**{"nothing":75,"bronzeChainlegs":20,"ironChainlegs":5})
gloves = getRandomWeightedElement(**{"nothing":95,"leatherGloves":5})
boots = getRandomWeightedElement(**{"nothing":95,"bronzeBoots":5})
ring1 = getRandomWeightedElement(**{"nothing":95,"bronzeRing":5})
ring2 = getRandomWeightedElement(**{"nothing":95,"silverRing":5})
wielding = [mainH,offH,head,neck,chest,legs,gloves,boots,ring1,ring2]
print("\n",wielding,"\n")
total = equipment["nothing"]
for item in wielding:
total += equipment[item]
print("Physical Attack:",total["physA"],"Physical Defence:",total["physD"])
print("Ranged Attack:", total["rangA"],"Ranged Defence:", total["rangD"])
print("Magic Attack:",total["magA"],"Magic Defence:",total["magD"])
print("Fire Attack Damage",total["fireAD"],"Fire Defence Protection",total["fireDP"])
print("Poison Attack Chance",total["poisAC"],"Poison Defence Chance",total["poisDC"])
print("Prayer Offence Bonus",total["prayOB"],"Prayer Defence Bonus",total["prayDB"])
goblinGen()
所以基本的问题是你对
nothing
计数器进行了变异,所以当你生成几个li'l gobbos时,结果是累积的。这是通过实例化一个新的总计数计数器来修复的。你知道吗我还修复了其他一些东西:
getRandomWeightedElement
中使用**
字典解包语法。你知道吗pprint.pprint
用于漂亮的打印。)输出示例:
相关问题 更多 >
编程相关推荐