在学校我们正在为Python创建一个骰子游戏,我似乎无法弄清楚如何完成最后一个功能。在游戏中,Ezee,你有14个骰子,你必须滚动所有这些。你必须采取骰子的模式,你重新滚动所有不是模式的骰子,直到所有骰子都是相同的数字。一旦他们都一样,你就赢了比赛。这个特定的功能应该计算滚动次数,并且在使用命令“game(debug = True)”之后,返回获胜的滚动次数以及重新掷骰子直到你赢了,如下所示:
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
有没有人可以帮助我,告诉我我做错了什么?我已经尝试了一段时间来解决这个问题,我很困惑。你的回复对我很有帮助,非常感谢你!
答案 0 :(得分:1)
你需要这样做:
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()