我一直在努力地教自己一些编码方面的知识,并认为一个不错的项目是用Python 3.5制作DND 5E字符生成器供个人使用。到目前为止,我已经构建了一个简单的类,它将为您提供类,性别,种族和背景。但是我真的很想随着时间的流逝更加充实。最终,我希望它能完成字符创建中涉及的所有工作并将其键入PDF字符表中。但是我还没有那么远。我已经设法破解了到目前为止可以正常工作的类似于真实代码的东西,现在我想添加Stat Rolling,这就是我需要帮助的地方,因为我正坐在我的裤子旁边飞行,并不真正知道自己在做什么在做。
所以我喜欢为角色统计数据的方法是滚动4d6并放下最低骰子,然后将剩余的三个数字加在一起以获得统计数据。然后,我执行7次,并删除最低的数字,以使总共6个统计数字可分配给不同的统计。我不确定如何删除最低的数字并将余数加在一起,也不确定100%如何处理分配统计信息。
就目前而言,我认为应该首先自动分配它们,然后在向用户展示“完成”字符后,我可以在选项中编写代码以最终更改内容。同样,如果我先创建一个自动版本,则可以稍后再返回,并添加一个选项来执行自动或引导性角色创建,并创建一个引导性版本,提示用户在进行操作时选择每个设置。无论如何,现在我只需要真正的帮助才能正确添加统计信息并正确分配它们,因此,您所能提供的任何建议将不胜感激!到目前为止,这是我的代码:
import random
gender='female'
race='dragonborn'
klass='barbarian'
background='acolyte'
genders = [
'Female',
'God Damned',
'Male'
]
races = [
'Dragonborn',
'Dwarf',
'Elf',
'Gnome',
'Half-Elf',
'Half-Orc',
'Halfling',
'Human',
'Tiefling'
]
klasses = [
'Barbarian',
'Bard',
'Cleric',
'Druid',
'Fighter',
'Monk',
'Paladin',
'Ranger',
'Rogue',
'Sorcerer',
'Warlock',
'Wizard'
]
backgrounds = [
'Acolyte',
'Charlatan',
'Criminal',
'Entertainer',
'Folk Hero',
'Gladiator',
'Guild Artisan',
'Guild Merchant',
'Hermit',
'Knight',
'Noble',
'Outlander',
'Pirate',
'Sage',
'Sailor',
'Soldier',
'Urchin'
]
# Here I want to roll for 6 stats, for each stat I want to roll 4d6, and
# then add the three highest numbers together. Or, add all four together,
# and then subtract the lowest number.
#
# Although I personally like to roll for 7 stats like this and then ignore
# the lowest stat. I'd like to figure out how to do it both ways eventually.
#
# As I understand it individual numbers won't repeat in a set, so that may
# be useful. However if I were to make a set of four random integers between
# 1 and 6 and there were no repeats between them, adding them all together
# would then give me too high of a number. So I still need to be able to
# identify the lowest number afaik.
#
#
# stats = [
# 'Strength',
# 'Dexterity',
# 'Constitution',
# 'Intelligence',
# 'Wisdom',
# 'Charisma'
# ]
#
# strength = [
# my_set = {random.randrange(0,6), random.randrange(0,6), random.randrange(0,6), random.randrange(0,6)}
# ]
#
# ???
#
def displayDialog():
cont = True
while cont:
userInput = input("Type the command you want or help").lower()
if(userInput == 'help'):
printCommands()
else:
roll(userInput)
def roll(stat):
global gender
global race
global klass
global background
statLow = stat.lower()
if(statLow== 'gender'):
gender = genders[random.randint(0, len(genders)-1)]
if(statLow== 'race'):
race = races[random.randint(0, len(races)-1)]
if(statLow== 'klass'):
klass = klasses[random.randint(0, len(klasses)-1)]
if(statLow== 'background'):
background = backgrounds[random.randint(0, len(backgrounds)-1)]
if(statLow== 'random'):
gender = genders[random.randint(0, len(genders)-1)]
race = races[random.randint(0, len(races)-1)]
klass = klasses[random.randint(0, len(klasses)-1)]
background = backgrounds[random.randint(0, len(backgrounds)-1)]
print('You are a ' + gender + ' ' +
race + ' ' +
klass + ' ' + 'with a background as a ' +
background + '!')
def printCommands():
print('---Commands---\nGender \nRace \nKlass \nBackground \nRandom')
displayDialog()
答案 0 :(得分:2)
这里不是一个正确的选择。你确实需要所有的骰子。
这样的函数应该做:
def roll_dice_discarding_lowest(n_dice, dice_rank):
results = [ # Generate n_dice numbers between [1, dice_rank]
random.randint(1, dice_rank)
for n
in range(n_dice)
]
lowest = min(results) # Find the lowest roll among the results
results.remove(lowest) # Remove the first instance of that lowest roll
return sum(results) # Return the sum of the remaining results.
要投放5d6放弃最低成绩,请致电roll_dice_discarding_lowest(5, 6)
。