确定遭遇的结果
我一般不知道怎么完成下面这些要点。希望能得到帮助!!!我把目前写的代码放在下面,但正如我所说的,我不知道怎么把这些融入到我的代码里。非常感谢!
• 计算两个角色的力量属性之间的差异。
• 将这个差异除以5,然后向下取整,得到一个“力量修正值”。
• 对技能属性重复这个过程,得到一个“技能修正值”。
• 每个玩家掷一个六面骰子。
• 如果两个骰子的点数相同,就不做任何改变。
• 如果点数不同,点数高的玩家将“力量修正值”加到他们角色的力量值上,并将“技能修正值”加到技能值上。
• 点数低的玩家则从他们角色的力量和技能值中减去这些修正值。
• 如果技能值变成负数,就把它存储为零。
• 如果力量值变成零或负数,那么这个角色就死掉了。
程序应该:
• 允许用户输入两个角色的力量和技能。
• 使用上述过程显示结果。
设计一个算法来描述这个过程。编写、测试和评估代码。
import random
def character_attributes():
initial_value = 10
character1_strength = initial_value + (random.randint(1,12) // random.randint(1,4))
character1_skill = initial_value + (random.randint(1,12) // random.randint(1,4))
character2_strength = initial_value + (random.randint(1,12) // random.randint(1,4))
character2_skill = initial_value + (random.randint(1,12) // random.randint(1,4))
print("Character 1 now has a strength attribute of {0}".format(character1_strength))
print("Character 1 now has a skill attribute of {0}".format(character1_skill))
print("Character 2 now has a strength attribute of {0}".format(character2_strength))
print("Character 2 now has a skill attribute of {0}".format (character2_skill))
myfile = open('character_attribute_data.txt', 'w')
myfile.writelines('Character 1 has a strength attribute of : ')
myfile.writelines(str(character1_strength))
myfile.writelines('\n')
myfile.writelines('Character 1 has a skill attribute of: ')
myfile.writelines(str(character1_skill))
myfile.writelines('\n')
myfile.writelines('Character 2 has a strength attribute of : ')
myfile.writelines(str(character2_strength))
myfile.writelines('\n')
myfile.writelines('Character 2 has a strength attribute of : ')
myfile.writelines(str(character2_skill))
myfile.close()
character_attributes()
def dice_roll(number):
if number == 12:
number = random.randint(1,12)
print(number)
return number
elif number == 6:
number = random.randint(1,6)
print(number)
return number
else:
number == 4
number = random.randint(1,4)
print(number)
return number
print("12 sided")
print("6 sided")
print("4 sided")
rolls = {4: [], 6: [], 12: []} # dictionary to hold rolls
while True:
roll = int(input("Which dice would you like to roll? --> ")) # store die size
rolls[roll].append(dice_roll(roll)) # roll and add to dictionary
doRepeat=input("Go again? --> ")
if doRepeat == "no":
break
print(rolls)
2 个回答
1
正如所提到的,使用类可能会很好。但如果你现在还不太理解类的概念,我可以帮你修改一下你的掷骰子函数,让它更容易理解和扩展。
def dice_roll(numberOfSides):
if numberOfSides not in set([4,6,12]):
print ("Valid die size is either 4, 6, or 12")
return 'Invalid'
number = random.randint(1,numberOfSides)
print(number)
return number
然后处理返回值为空的情况。
if dice_roll == 'Invalid':
do_something
你可以这样编写代码,而不需要使用else语句,因为无论骰子的大小,执行的操作都是一样的。这样可以避免你以后困惑,为什么要为不同大小的骰子定义不同的结果。
写代码有一点我现在正在经历,就是回头再看一遍总是会更好。
1
这里有一些代码可以参考:
请注意,如果一开始的力量差距小于5(有57.4%的概率),或者在游戏进行中力量的差距变得小于5(这个概率不确定,但很可能),那么游戏将会无限进行下去(mod_strength
将始终为0)。
import random
STRENGTH = (10, 22)
SKILL = (10, 22)
class Character:
def __init__(self, name, strength=None, skill=None):
self.name = name
self.strength = strength or random.randint(*STRENGTH)
self.skill = skill or random.randint(*SKILL)
self.roll = None
def throw(self):
self.roll = random.randint(1, 6)
def mod_strength(self, amt):
self.strength = max(0, self.strength + amt)
def mod_skill(self, amt):
self.skill = max(0, self.skill + amt)
def is_dead(self):
return self.strength == 0
def __str__(self):
return(
"{}: str {} ski {}"
.format(self.name, self.strength, self.skill)
)
def calc_modifier(val1, val2, div_by):
return abs(val2 - val1) // div_by
def main():
ch1 = Character("Conan")
ch2 = Character("Xena")
while True:
ch1.throw()
ch2.throw()
print("Roll! {}, {}".format(ch1.roll, ch2.roll))
if ch1.roll > ch2.roll:
print(" {} wins".format(ch1.name))
dir = 1
elif ch1.roll == ch2.roll:
print(" Tie...")
dir = 0
else:
print(" {} wins".format(ch2.name))
dir = -1
strength_mod = calc_modifier(ch1.strength, ch2.strength, 5)
ch1.mod_strength(strength_mod * dir)
ch2.mod_strength(strength_mod * -dir)
skill_mod = calc_modifier(ch1.skill, ch2.skill, 5)
ch1.mod_skill(skill_mod * dir)
ch2.mod_skill(skill_mod * -dir)
print(" {}, {}".format(ch1, ch2))
if ch1.is_dead():
print("{} is the victor!".format(ch2.name))
break
elif ch2.is_dead():
print("{} reigns supreme!".format(ch1.name))
break
if __name__=="__main__":
main()