2014-04-14 70 views
0

現在我正試圖在Python中將二維數組(列表清單)放入我的掃雷程序中。然而,當我運行它,它給了我38行錯誤掃雷問題

Traceback (most recent call last): 

    File "F:\MINESWEEPER2.py", line 112, in <module> 

    check_neighbours(reveal,board,bombs) 

    File "F:\MINESWEEPER2.py", line 38, in check_neighbours 

    board[index] = bombcount #copy the number of neighbouring bombs 

    IndexError: list assignment index out of range). 

代碼:

import string 
import os 
import random 
def check_neighbours(index, board, bombs): 

#get the last digit in the index to check for left or right column positions 
if index > 9: 
    strindex = str(index) 
    second = strindex[1] 
else: 
    second = 'a' 

if index == 0: #top left corner 
    offsets=[1,10,11] 
elif index == 9: #top right corner 
    offsets =[-1,9,10] 
elif index == 90: #bottom left corner 
    offsets = [-10,-9, 1] 
elif index ==99: #bottom right corner 
    offsets=[-11,-10,-1] 
elif index > 0 and index < 9: #top row 
    offsets = [-1,1,9,10,11] 
elif index >90 and index < 99: #bottom row 
    offsets = [-1,1,-9,-10,-11] 
elif second == '9':  #Right side column 
    offsets=[-11,-10,-1,9,10] 
elif second == '0':  #Left side column 
    offsets = [-10,-9,1,10,11] 

else: 
    offsets=[-11,-10,-9,-1,1,9,10,11] 
bombcount = 0; 
for i in offsets: #check for bombs in all neighbour positions 
    if index+i in bombs: 
     bombcount+=1 

board[index] = bombcount #copy the number of neighbouring bombs 
if bombcount == 0: 
    board[i][j] = ' ' 
    for i in offsets: #Recursive function call for all neighbours 
     if board[index + i] == '*': 
      check_neighbours(index+i, board, bombs) 


else: 
    return 



def print_reveal(board, bombs): 
count = 0 

rowcount = 0 
for i in board: 
    if count in bombs: 
     i = 'B' 
    print("| ", i, end=" ") 
    if rowcount == 9: 
     print("|") 
     rowcount = 0 
    else: 
     rowcount += 1 
    count += 1 

board = [] 
bombs = [] 
for i in range(10): 
board.append([]) 
    for j in range(10): 
    board[i].append('*') 

for i in range(10): 
for j in range(10): 
    print("|", board[i][j], end=" ") 
print("|") 
bombs = [] 
for i in range(10): 


loc = random.randint(0, 99) 
while loc in bombs: 
    loc = random.randint(0, 99) 
#board[loc] = 'B' 
bombs.append(loc) 


gameon=True 
while(gameon): 

choice = input("Do you want to pick a spot or flag a bomb? (pick or flag)") 
if choice == 'flag': 
    bombloc = int(input("Enter your guess for a bomb location: ")) 
    if bombloc >= 0 and bombloc < 100: 
     if board[bombloc] != '*': 
      print("already chosen!") 
     else: 
      board[bombloc] = 'F' 



else: 

    reveal = int(input("Enter your guess for an open spot: ")) 
    if reveal >= 0 and reveal < 100: 
     if board[i][j] != '*' and board[i][j] != 'F': 
      print("already chosen!") 
     elif reveal in bombs: 
      print("BOOOM!!!") 
      gameon = False 
     else: 
      check_neighbours(reveal,board,bombs) 



if '*' not in board: 
    print("You WIN!") 
    gameon = False 

if(gameon): 
    print_reveal(board,bombs) 
print(bombs) 

這是輸出:

| * | * | * | * | * | * | * | * | * | * | 
    | * | * | * | * | * | * | * | * | * | * | 
    | * | * | * | * | * | * | * | * | * | * | 
    | * | * | * | * | * | * | * | * | * | * | 
    | * | * | * | * | * | * | * | * | * | * | 
    | * | * | * | * | * | * | * | * | * | * | 
    | * | * | * | * | * | * | * | * | * | * | 
    | * | * | * | * | * | * | * | * | * | * | 
    | * | * | * | * | * | * | * | * | * | * | 
    | * | * | * | * | * | * | * | * | * | * | 
    Do you want to pick a spot or flag a bomb? (pick or flag)pick 

    Enter your guess for an open spot: 30 

    Traceback (most recent call last): 

    File "F:\MINESWEEPER2.py", line 112, in <module> 

    check_neighbours(reveal,board,bombs) 

    File "F:\MINESWEEPER2.py", line 38, in check_neighbours 

    board[index] = bombcount #copy the number of neighbouring bombs 

    IndexError: list assignment index out of range 

我不知道該怎麼做解決這個問題

我真的很感謝這個幫助

+0

錯誤在哪裏? –

+0

如果board [reveal]!='*'和board [reveal]!='F',則文件「G:\ MINESWEEPER2.py」,行114,位於 IndexError:列表索引超出範圍 – user3533914

+0

如果您有包含錯誤的行號,指定哪一行在問題中有錯誤。 –

回答

0

這個問題似乎是你永遠不會初始化你的主板。在print_reveal的早期部分,您可以:board = [](覆蓋作爲參數傳遞的board值),但從未填充board的任何內容。稍後,當您檢查玩家選中的位置時,您會從if board[bombloc] != '*':獲得IndexError,因爲board仍然是一個空列表。

您需要決定print_reveal是否應該創建董事會。如果沒有,只需擺脫board = []行(並確保傳入的參數是合適的)。否則,您應該擺脫混淆的參數,並設置適當的值(可能是board = ["*"] * 100),而不是使用未初始化的值。

+0

我在print_reveal中取出了board = []部分。但是,我怎樣才能使論點合適呢? – user3533914

+0

@ user3533914:我不確定該程序的最佳結構是什麼。你需要考慮董事會來自哪裏以及哪裏通過。你的問題的一部分可能是你似乎正在做你的程序的大部分工作在兩個函數中,名字以「print_」開頭,這通常意味着他們只是做輸出,而不是做所有事情。看看你是否可以將實際的遊戲部分重構成其他功能,你可能會有更好的運氣。 – Blckknght