构建 Python 骰子游戏,卡在最后一个函数上? (关闭)

Building a Python dice game, stuck on the last function? (CLOSED)

在学校,我们正在为 Python 创建一个骰子游戏,但我似乎无法弄清楚如何完成最后一个功能。在游戏中,Ezee,你有 14 个骰子,你必须掷出所有的骰子。您必须采用骰子的模式,然后重新掷所有不是模式的骰子,直到所有骰子的数字相同。一旦它们完全相同,您就赢得了比赛。这个特定的函数应该计算掷骰数,在使用命令 "game(debug=True)"、return 后,它需要掷骰子才能获胜并重新掷骰子,直到你赢了,如图所示以下:

In []: game()
Out[]: 18
In []: game(debug=True)
Out[]: [3, 3, 3, 3, 3, 5, 3, 3, 3, 2, 5, 5, 4, 4]
       [3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 6]
       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5]
       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
       4

除了我已经完成的功能之外,我还有8个功能:

import random 
def roll_die(): 
    ''' Return a random number 1-6
    ''' 
    dice = random.randint(1, 6)
    return dice

def first_roll(): 
    ''' Return a list of 14 numbers, each random 1 to 6
    '''
    rolls = [] #Initialize
    counter = 0
    while counter<14:
        #Append
        rolls.append(roll_die())
        counter = counter+1
    return rolls #Report/use

def count_frequency(dice, number):
    ''' dice is a list of fourteen ints
    number is an int
    returns the frequency of the number among elements of dice
    '''
    frequency = 0 #Initialize
    for die in dice:
        #Accumulate
        if die == number: 
            frequency=frequency+1
    return frequency #Report/use

def find_mode(dice): 
    ''' Accepts a list of numbers 1-6.
    Returns the most common number in the list.
    Returns one of the most common if there is a tie.
    '''
    #Start with a record that will be beat
    record = 0
    for number in [1, 2, 3, 4, 5, 6]: 
        # Check for the record breaker
        frequency=count_frequency(dice, number)
        if frequency>record:
           record = frequency # Set the new record
           mode = number
    return mode


def list_unmatched_dice(dice, target): 
    '''Accepts dice    ::a list of numbers.
               target  ::a number
    Returns the indices where the element != target
    '''
    #for each index in range(14) if dice[index] != mode then add index to the new list
    unmatched = []
    for index in range(14):
        if dice[index] != target:
           unmatched.append(index)
    return unmatched

def reroll_one(dice, index): 
    ''' dice is a list of 14 numbers
    index is an int 0-13
    Return the list of dice, with a number randomly chosen 
    from 1 to 6 to replace the item at index
    '''
    #reroll the dice
    dice[index]=roll_die()
    return dice

def reroll_many(dice):
    ''' accepts a list of 14 numbers
    returns resulting list of 14 numbers
    '''
    #reroll the die
    mode=find_mode(dice)
    index=list_unmatched_dice(dice, mode)
    for index in list_unmatched_dice(dice, mode):
        dice=reroll_one(dice, index)
    return dice

def won(dice): 
    '''dice is a list of 14 ints 1-6
    checks for 14 of a kind
    returns True or False
    '''
    #Start with a record that will be beat
    if dice == [dice[0]]*14:
        #Check for the record breaker
        match = True
    else:
        match = False
    return match #Set the new record

我知道所有其他功能都是正确的,因为我已经对它们进行了测试并且它们工作正常,但是有人知道我是如何完成最后一个功能的吗?这是我使用的入门代码:

def game(debug=False): 
    '''rolls 14 dice and repeats until getting an Ezee
    returns the number of rolls made.
    '''
    dice = first_roll()
    if debug:
        print dice
    # Anything else to do before you start iterating?
    while not won(dice):
        # What to do if you haven't won?
    # What to do once you've won?

这是我目前的代码:

def game(debug=False): 
    '''rolls 14 dice and repeats until getting an Ezee
    returns the number of rolls made.
    '''
    #Initialize
    dice = first_roll()
    target = find_mode(dice)
    rolls = 0
    if debug:
        print dice
    while won(dice):
        rolls=rolls+1
        if list_unmatched_dice(dice, target) == find_mode(dice):
            return rolls #Report/use
    while not won(dice):
        #Accumulate
        rolls=rolls+1
        if list_unmatched_dice(dice, target) != find_mode(dice):
            return rolls

任何人都可以帮助我并告诉我我做错了什么吗?我已经尝试了一段时间来弄清楚这一点,但我很困惑。您的回复对我帮助很大,非常感谢!

你需要这样做:

def game(debug=False): 
    '''rolls 14 dice and repeats until getting an Ezee
    returns the number of rolls made.
    '''
    dice = first_roll()
    if debug:
        print dice
    counter = 0
    while not won(dice):
        counter += 1
        reroll_many(dice)
        if debug:
            print dice

    print counter


if __name__ == '__main__':
    game()