如何调试我的Python骰子游戏?

时间:2017-10-09 19:06:22

标签: python arrays

所以我最近发布了一个简单的骰子程序的代码,我遇到了麻烦。它应该在一个数组中随机生成5个数字,然后检查是否有任何匹配值,如果存在,它会添加到MATCH中,所以一旦完成检查,MATCH + 1就是多少'同类'( match = 1表示两种类型,match = 2表示三种类型等。)

它随机生成然后正确显示数字,程序似乎没有错误检查,除非最后两个playerDice元素匹配,然后它抛出一个越界错误,为什么它这样做?此外,它实际上从来没有显示最后一行的打印行数,即使它没有错误运行,为什么会这样?

以下是代码:

import random
playerDice = [random.randint(1,6),random.randint(1,6),random.randint(1,6),random.randint(1,6),random.randint(1,6)]
compDice = [random.randint(1,6),random.randint(1,6),random.randint(1,6),random.randint(1,6),random.randint(1,6)]
match = 0
compmatch = 0

#print player dice
print("You rolled: ",end=" ")
a = 0
while a < len(playerDice):
        print(str(playerDice[a]) + ", ",end=" ")
        a = a + 1

#player check matches
i = 0
while i < len(playerDice):

        j = i + 1
        if playerDice[i] == playerDice[j]:
                match = match + 1

        while playerDice[i] != playerDice[j]:
                j = j + 1
                if playerDice[i] == playerDice[j]:
                        match = match + 1

i = i + 1

print("Player has: " + str(match + 1) + " of a kind.")

1 个答案:

答案 0 :(得分:1)

有一种更容易寻找匹配的方法:对骰子进行排序,然后寻找重复骰子的运行。您可以手动查找这些运行,但标准库具有以下功能:itertools.groupby。这是一个简短的演示。

import random
from itertools import groupby

# Seed the randomizer while testing so that the results are repeatable.
random.seed(7)

def roll_dice(num):
    return [random.randint(1,6) for _ in range(num)]

def find_matches(dice):
    matches = []
    for k, g in groupby(sorted(dice)):
        matchlen = len(list(g))
        if matchlen > 1:
            matches.append('{} of a kind: {}'.format(matchlen, k))
    return matches

for i in range(1, 6):
    print('Round', i)

    player_dice = roll_dice(5)
    #comp_dice = roll_dice(5)

    print('You rolled: ', end='')
    print(*player_dice, sep=', ')

    matches = find_matches(player_dice)
    if not matches:
        print('No matches')
    else:
        for row in matches:
            print(row)
    print()

<强>输出

Round 1
You rolled: 3, 2, 4, 6, 1
No matches

Round 2
You rolled: 1, 5, 1, 3, 5
2 of a kind: 1
2 of a kind: 5

Round 3
You rolled: 1, 5, 2, 1, 1
3 of a kind: 1

Round 4
You rolled: 4, 4, 1, 2, 1
2 of a kind: 1
2 of a kind: 4

Round 5
You rolled: 5, 4, 1, 5, 1
2 of a kind: 1
2 of a kind: 5

以下是find_matches的替代版本,不使用groupby。在纸上运行这个算法以确切了解它是如何工作的,这可能是一个好主意。

def find_matches(dice):
    matches = []
    dice = sorted(dice)

    prev = dice[0]
    matchlen = 1
    # Add a "dummy" entry so we can detect a group at the end of the list
    for d in dice[1:] + [0]:
        # Compare this die to the previous one
        if d == prev:
            # We're inside a run of matching dice
            matchlen += 1
        else:
            # The previous run has ended, so see if it's
            # long enough to add to the matches list
            if matchlen > 1:
                matches.append('{} of a kind: {}'.format(matchlen, prev))
            # Reset the match length counter
            matchlen = 1
        # This die will be the previous die on the next loop iteration
        prev = d

    return matches