现在我正在尝试将一个2D数组(列表列表)放入我在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)
问题似乎是你永远不会初始化你的电路板。在print_reveal
的早期,您执行:board = []
(覆盖作为参数传递的board
值),但之后永远不会填充board
。之后,当您检查玩家选择的位置时,您会从IndexError
获得if board[bombloc] != '*':
,因为board
仍为空列表。
你需要决定print_reveal
是否应该创建董事会。如果没有,只需删除board = []
行(并确保传入的参数是合适的)。否则,您应该摆脱令人困惑的参数,并使用适当的值(可能是board = ["*"] * 100
)设置电路板,而不是使用未初始化的电路。