我为Farkle(一个骰子游戏)制作了一个简单的评分系统。我是Python的初学者,对如何做某些事情有一些疑问。首先,我无法找到如何让我的代码检查我的列表(dicegroup)以查找任何类型的匹配序列。我特别想要检查四种类型,而不必我进去并手动设置当每个号码有一个数字时会发生什么。最简单的方法是什么?
对此代码的任何一般反馈也表示赞赏。
from __future__ import print_function
from collections import Counter
import random
onescore = 0
twoscore = 0
threescore = 0
fourscore = 0
fivescore = 0
fivesinglesscore = 0
sixscore = 0
fourofakind = 0
scorelist = []
##ROLLING THE DICE##
def roll():
dice = (1,2,3,4,5,6)
dice1 = random.choice(dice)
dice2 = random.choice(dice)
dice3 = random.choice(dice)
dice4 = random.choice(dice)
dice5 = random.choice(dice)
dice6 = random.choice(dice)
global onescore,twoscore,threescore,fourscore,fivescore,fivesinglesscore,sixscore,fourofakind
onescore = 0
twoscore = 0
threescore = 0
fourscore = 0
fivescore = 0
fivesinglesscore = 0
sixscore = 0
fourofakind = 0
dicegroup = [dice1,dice2,dice3,dice4,dice5,dice6]
print ('Your rolls are',dicegroup)
dicenum = Counter(dicegroup)
onescore = dicenum[1] * 100
print ('There are',dicenum[1],'ones. This gives you', onescore, 'points.')
fivesinglesscore = dicenum[5] * 50
print ('There are',dicenum[5],'fives. This gives you', fivesinglesscore, 'points.')
if dicenum[2] == 3:
twoscore = 200
print ('There are',dicenum[2],'twos. This gives you', twoscore, 'points.')
if dicenum[3] == 3:
threescore = 300
print ('There are',dicenum[3],'threes. This gives you', threescore, 'points.')
if dicenum[4] == 3:
fourscore = 400
print ('There are',dicenum[4],'fours. This gives you', fourscore, 'points.')
if dicenum[5] == 3:
fivescore = 500
print ('There are',dicenum[5],'fives. This gives you', fivescore, 'points.')
if dicenum[6] == 3:
sixscore = 600
print ('There are',dicenum[6],'sixes. This gives you', sixscore, 'points.')
if dicenum[any] == 4:
fourofakind = 1000
print ('You have four of a kind. This is worth', fourofakind, 'points.')
roll()
##SCORE PROCESSING
finalscore = onescore + twoscore + threescore + fivescore + fivesinglesscore + sixscore + fourofakind
print (finalscore)
scorelist.append(finalscore)
print ('')
print ('Score list is:')
print (scorelist)
答案 0 :(得分:0)
要检查4种:
使用collections
模块中的Counter
,我们可以创建一个字典,在列表中为我们计算所有唯一项目:
from collections import Counter
x = [1,2,3,4,5,6,76,5,234,123,4,123,312,3,5,245,123,123,1,51,35,123,123,1,23,1,1,1,1,1,12,32,31,23]
print(Counter(x))
#Counter({1: 8, 123: 6, 5: 3, 3: 2, 4: 2, 23: 2, 32: 1, 2: 1, 6: 1, 31: 1, 12: 1, 234: 1, 76: 1, 51: 1, 245: 1, 312: 1, 35: 1})
现在我们需要做的就是按照您想要的许多x of a kind
进行过滤。我们可以通过创建一个列表推导来实现这一点,该列表推导过滤掉所有值并且只保留我们想要的值,在这种情况下,如果计数等于4
from collections import Counter
x = [1,2,3,4,5,6,76,5,234,123,4,123,312,3,5,245,123,123,1,51,35,123,123,5,1,23,1,1,1,1,1,12,32,31,23]
answer = [a for a,b in Counter(x).items() if b == 4]
print(answer)
#[5]
现在我们返回一个包含4个项目的所有项目的列表。
要连续检查4个:
x = [1,2,3,4,5,6,76,5,234,123,4,123,312,3,5,245,123,123,1,51,35,123,123,1,23,1,1,1,1,1,12,32,31,23]
for i in range(len(x)):
to_check = x[i:i+4]
if to_check.count(to_check[0]) == 4:
print('found it {0}, position {1} to position {2}'.format(to_check[0], i, i+4))
这将在列表中连续检查4个。它一次从列表中获取大小为4的块,并检查该列表中的所有内容是否相同。如果是,则打印出项目和位置。您可以更改它以适合您的计划。
count()
适用于列表,您可以在列表中传入您想要计算的内容。在这种情况下,我们想要计算切片列表中的第一项,因为我们要检查它是否完全相同,我们看看计数是否等于4.
注意:这会将[1,2,2,2,2,2]
视为2,连续四项,因为它是第一组2和第二组2。如果您只想将其视为一组4连续,则可以按如下方式修改代码:
x = [1,2,3,4,5,6,76,5,234,123,4,123,312,3,5,245,123,123,1,51,35,123,123,1,23,1,1,1,1,1,12,32,31,23]
i = 0
while i < len(x):
to_check = x[i:i+4]
if to_check.count(to_check[0]) == 4:
print('found it {0}, position {1} to position {2}'.format(to_check[0], i, i+4))
i += 4
else:
i += 1
使用while loop
,我们可以比i
更好地控制for loop
的流量。如果我们连续找到4,我们会跳到连续4的末尾。