2017-10-09 38 views
1

所以我最近發佈了一個簡單的骰子程序,我遇到了麻煩。它應該在一個數組中隨機生成5個數字,然後檢查是否有任何匹配值,如果存在,它將添加到MATCH中,因此一旦檢查完畢,MATCH + 1就是多少個'有'匹配= 1意味着兩種類型,匹配= 2意味着三種類型等)如何調試我的Python骰子游戲?

它隨機生成並正確顯示數字,程序似乎檢查沒有錯誤,除非最後兩個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

我不認爲你有足夠的邊界檢查'j'。 –

+0

如果玩家在同一時間滾動某種類似'[1,1,2,2,2]'的東西,一種和三種類似的東西,你希望程序做什麼?順便說一句,有更好的方法來組織和編寫此代碼... –

+0

行'j = i + 1'將始終導致循環的最後一次迭代的索引錯誤。另外,'i = i + 1'需要在循環中。沒有這個,循環將永遠旋轉,這就是爲什麼最後一行永遠不會執行。 – ekhumoro

回答

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 
+0

這比我目前的蟒蛇知識水平還要高,但是感謝你抽出時間寫下並展示給我!我要複製這段代碼,並通過它來了解所有你使用的不同命令。 – JGoss

+0

@JGoss夠公平的。我剛添加了'find_matches'的另一個版本,它不使用'groupby'。希望評論能幫助你理解它是如何工作的。 –

+0

再次感謝這有助於相當多。 – JGoss