absolutely retarded q with confusing logic but ez q
def updateBoard(board, click):
row,col = len(board),len(board[0])
if board[click[0]][click[1]]=="M":
return board
moves=[[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]
def countMines(board_copy,x,y):
count=0
for move in moves:
next_x,next_y=move[0]+x,move[1]+y
if 0<=next_x<row and 0<=next_y<col and board_copy[next_x][next_y]=="M":
count+=1
return count
queue=[click]
while queue:
cur_x,cur_y = queue.pop(0)
for move in moves:
next_x,next_y=move[0]+cur_x,move[1]+cur_y
count_returned = countMines(board,next_x,next_y)
if count_returned:
board[next_x][next_y]=count_returned
else:
board[cur_x][cur_y] = "B"
if 0 <= next_x < row and 0 <= next_y < col:
queue.append([next_x,next_y])
return board
No, this code has several issues. Let me point them out:
if board[click[0]][click[1]]=="M":
return board # ❌ Should change to 'X' first!
Should be:
if board[click[0]][click[1]]=="M":
board[click[0]][click[1]] = 'X'
return board
You add click to the queue but never process it before the loop. You need to handle the initial clicked cell.
for move in moves:
next_x,next_y=move[0]+cur_x,move[1]+cur_y
count_returned = countMines(board,next_x,next_y) # ❌ Checking neighbors, not current!
You're counting mines for the neighbor cells but you should count mines for the current cell being processed.
You call countMines(board,next_x,next_y) without checking if next_x, next_y is in bounds first.
You don't check if a cell is 'E' before processing it. You might process the same cell multiple times or try to process already revealed cells.
if count_returned:
board[next_x][next_y]=count_returned # ❌ Setting neighbor
else:
board[cur_x][cur_y] = "B" # ❌ Setting current only when neighbor has 0 mines?
This logic is backwards. You should set the current cell based on its own mine count, not its neighbor's count.
def updateBoard(board, click):
row, col = len(board), len(board[0])
# Rule 1: If clicked on mine, reveal it
if board[click[0]][click[1]] == "M":
board[click[0]][click[1]] = "X"
return board
moves = [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]
def countMines(x, y):
count = 0
for move in moves:
next_x, next_y = move[0] + x, move[1] + y
if 0 <= next_x < row and 0 <= next_y < col and board[next_x][next_y] == "M":
count += 1
return count
queue = [click]
while queue:
cur_x, cur_y = queue.pop(0)
# Skip if already processed
if board[cur_x][cur_y] != "E":
continue
# Count mines for CURRENT cell
mine_count = countMines(cur_x, cur_y)
if mine_count > 0:
# Has adjacent mines: show count and stop
board[cur_x][cur_y] = str(mine_count)
else:
# No adjacent mines: mark as B and add neighbors
board[cur_x][cur_y] = "B"
for move in moves:
next_x, next_y = move[0] + cur_x, move[1] + cur_y
if 0 <= next_x < row and 0 <= next_y < col and board[next_x][next_y] == "E":
queue.append([next_x, next_y])
return board