user5561413 user5561413 - 6 months ago 23
Python Question

python minesweeper reveal function

def reveal(board, row, col):
board[row][col] = "C" + board[row][col][1]
count = 0
board =[]
rowcount = 0
for i in board:
if count in mines:
i = 'C*'
print("| ", i, end=" ")
if rowcount == 9:
print("|")
rowcount = 0
else:
rowcount += 1
count += 1

board = []
mines = []
for i in range(9):
board.append([])
for j in range(12):
board[i].append('C*')

for i in range(9):
for j in range(12):
#Error Below it says Index Not in Range
print("|", board[i][j], end=" ")
print("|")
mines = []
for i in range(9):

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


The issue I'm having is that I'm getting an Index Error where I've indicated and I don't know what it means or how to go about changing it. This is the reveal function of my minesweeper game and my board won't draw when running the program

Answer

Quick guess is this:

for i in range(9):
 board.append([])
for j in range(12):
  board[i].append('C*')

Intended like that, it runs two independent loops, so i will have the last value (8) and you only append to the last row.

If you fix your indentation it would look like this:

for i in range(9):
    board.append([])
    for j in range(12):
        board[i].append('C*')

Then, it will work correctly.

That’s why you should use an indentation size of more than one space, so you actually see the difference!