确定遭遇的结果

时间:2014-03-18 16:34:38

标签: python python-3.x

我一般不知道如何完成以下要点。帮助将不胜感激!我已经把我到目前为止的代码放到了下面,但就像我说的那样,不知道如何将它合并到我的代码中。非常感谢。

•计算两个字符的强度属性之间的差异

•将此差值除以5,然后向下舍入以创建“强度修改器”

•对技能属性重复该过程以创建“技能修饰符”

•每位玩家掷出6面骰子。

•如果两个骰子上的得分相同,则不做任何更改

•如果得分不相同,得分最高的玩家会增加'力量 修饰符'强度值'和'技能修饰符'到他们的技能值 字符

•骰子得分较低的玩家从中减去这些修饰符 角色的力量和技能值

•如果技能值变为负值,则将其存储为零

•如果强度值变为零或负值,则角色会死亡。 该计划应该:

•允许用户输入两个角色的力量和技能。

•使用上述过程显示遭遇的结果。 设计一种算法来描述这个过程。编写,测试和评估代码。"""

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 个答案:

答案 0 :(得分: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()

答案 1 :(得分:1)

如上所述,课程可能会很好。但是如果你在学习过早的课程中让我只修改你的dice_roll函数,那么它就更容易理解和扩展

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语句的情况下对其进行编码,因为无论大小如何,都会在模具上执行相同的操作。这为您节省了很多麻烦,后来为什么要为不同的芯片尺寸定义不同的结果。

关于编写代码的一件事 - 我现在正在经历的是,当你第二次回顾它时总会更好。