如何调试我的Python骰子游戏?
所以我最近发布了一个简单的骰子程序的代码,我遇到了麻烦。假设它在一个数组中随机生成5个数字,然后检查是否有匹配值,如果有,它将添加到匹配中,因此一旦完成检查,MATCH+1表示有多少个“同类”(MATCH=1表示一类中的两个,MATCH=2表示一类中的三个等) 它随机生成并正确显示数字,并且程序似乎检查无误,除非最后两个playerDice元素匹配,然后抛出越界错误,为什么这样做?而且,它从来没有实际显示最后一行的打印行,其中有多少种打印行,即使它运行时没有错误,这是为什么 代码如下:如何调试我的Python骰子游戏?,python,arrays,Python,Arrays,所以我最近发布了一个简单的骰子程序的代码,我遇到了麻烦。假设它在一个数组中随机生成5个数字,然后检查是否有匹配值,如果有,它将添加到匹配中,因此一旦完成检查,MATCH+1表示有多少个“同类”(MATCH=1表示一类中的两个,MATCH=2表示一类中的三个等) 它随机生成并正确显示数字,并且程序似乎检查无误,除非最后两个playerDice元素匹配,然后抛出越界错误,为什么这样做?而且,它从来没有实际显示最后一行的打印行,其中有多少种打印行,即使它运行时没有错误,这是为什么 代码如下: impo
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.")
随机导入
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)]
匹配=0
compmatch=0
#打印玩家骰子
打印(“您滚动:”,end=“”)
a=0
而a
有一种更简单的方法来查找匹配项:对骰子进行排序,然后查找重复的骰子。您可以手动查找这些运行,但标准库有一个函数:。这里有一个简短的演示
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
我认为你没有对
j
进行足够的边界检查。如果玩家同时滚动类似[1,1,2,2,2]
,两个一类和三个一类的东西,你希望程序做什么?顺便说一句,有更好的方法来组织和编写此代码…行j=i+1
在循环的最后一次迭代中总是会导致索引错误。而且,i=i+1
需要在循环内。如果没有这一点,循环将永远旋转,这就是为什么最后一行永远不会执行的原因。这比我目前掌握的python知识水平要高很多,但感谢您花时间编写并演示!我将复制这段代码并仔细检查,以便了解您使用的所有不同命令。@JGoss公平。我刚刚添加了另一个版本的find\u matches
,它不使用groupby
。希望这些评论能帮助你理解它是如何工作的。