Python 四种骰子游戏

Python 四种骰子游戏,python,dice,Python,Dice,我为Farkle(骰子游戏)制作了一个简单的计分系统。我是Python的初学者,对如何做某些事情有一些疑问。首先,我很难找到如何让我的代码检查我的列表(dicegroup)中是否有任何类型的匹配序列。我想让它检查,特别是一种类型的四个,而不需要我进去手动设置当每个数字都有一个数字时会发生什么。最简单的方法是什么 对本规范的任何一般性反馈也将不胜感激 from __future__ import print_function from collections import Counter impo

我为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)
要检查类型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,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进行过滤。我们可以通过创建一个列表来实现这一点,该列表过滤掉所有的值,并且只保留我们想要的值,在这种情况下,如果计数等于
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的块,并检查该列表中的所有内容是否相同。如果是,则打印项目和位置。您可以更改它以适合您的程序

在一个列表上工作,然后你传入你想在列表中计数的内容。在本例中,我们要对切片列表中的第一项进行计数,因为我们要检查它是否都相同,我们要查看计数是否等于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
x=[1,2,3,4,5,6,76,5234123,4123312,3,5245123123,1,51,35123123,1,23,1,1,1,1,12,32,31,23]
i=0
而i

使用
while循环
我们可以比
for循环
更好地控制
i
的流量。如果我们在一行中找到4,我们将跳到4的末尾。

StackOverflow是一个问答网站,而不是代码审查网站。你应该问一个有明确答案的具体问题。还有其他论坛要求代码审查(例如reddit.com/r/learnprogramming)@Metropolis Fair。我稍微误解了网站的目的(尽管我认为我问的问题是相关的)。谢谢你通知我。