python minesweeper揭示函数

时间:2015-11-21 19:32:50

标签: python minesweeper

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)

我遇到的问题是我收到了索引错误,我已经指出了,我不知道它意味着什么或如何改变它。这是我的扫雷游戏的揭示功能,我的电路板在运行程序时不会绘制

1 个答案:

答案 0 :(得分:2)

快速猜测是这样的:

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

就像这样,它运行两个独立的循环,因此i将具有最后一个值(8),并且您只追加到最后一行。

如果您修改了缩进,它将如下所示:

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

然后,它将正常工作。

这就是为什么你应该使用超过一个空格的缩进大小,所以你实际上看到了差异!