首页 > 解决方案 > 扫雷:显示周围的块功能冻结

问题描述

我正在尝试为我正在制作的扫雷游戏制作一个功能。此函数的目的是显示给定 x 和 y(它所在的位置)的元素。这可能不是完成它的最优雅的方式,但我正在为每个标题为newField. '-' 代表一个隐藏块(你不知道它是否是炸弹或周围有多少炸弹)。然后,我将一个元素从 更改newField为等于其对应的块listField(这是一个列表列表。其中的每个列表代表一行)。X代表炸弹,数字代表周围有多少炸弹。

在扫雷中,当一个零炸弹周围的方块被显示出来时,它周围的方块也会被显示出来。我制作了一个功能齐全的标题revealSurroundings来实现这一点。当我像下面那样revealSurroundings在函数中运行时revealElement,它会冻结我的计算机。但是,如果我revealSurroundings在函数之外运行,revealElement它工作正常。

如果有人对如何解决这个问题和/或提高效率有任何建议(因为我知道我使用的方法非常昂贵),请告诉我。

wl = 10

listField = 
    [['1', '1', '0', '0', '0', '0', '0', '0', '1', 'X'], 
    ['X', '2', '2', '2', '2', '2', '2', '1', '1', '1'], 
    ['1', '2', 'X', 'X', '2', 'X', 'X', '2', '2', '2'], 
    ['1', '2', '3', '2', '2', '2', '3', '4', 'X', 'X'], 
    ['1', 'X', '1', '0', '0', '1', '3', 'X', 'X', '3'], 
    ['1', '2', '2', '1', '0', '1', 'X', 'X', '4', '2'], 
    ['2', '3', 'X', '1', '0', '1', '2', '2', '2', 'X'], 
    ['X', 'X', '2', '1', '1', '1', '1', '0', '1', '1'], 
    ['4', '5', '4', '3', '3', 'X', '2', '1', '0', '0'], 
    ['X', 'X', 'X', 'X', 'X', '3', 'X', '1', '0', '0']]
hiddenField = ['-' for i in range(wl*wl)]

def removeN(ls, n, iterations):
  items = ls
  try:
    for i in range(iterations):
      items.remove(n)
  except:
    pass
  return items

def revealElement(wl, x, y, userField):
  yReal = y-1
  xReal = x-1
  newField = list(userField)
  removeN(newField, '\n', wl-1)
  newField[yReal*wl + xReal] = listField[yReal][xReal]
  if newField[yReal*wl + xReal] == '0':
    revealSurroundings(wl, x, y, userField)
  for i in range(wl-1, 0, -1): # go backwards
      newField.insert(wl*i, '\n')

  return "".join(newField) # make it a string

def revealSurroundings(wl, x, y, userField):
  yReal = y-1
  xReal = x-1
  newField = userField
  try:
    newField = revealElement(wl, x+1, y, newField)
  except: 
    pass
  #right
  try:
    if xReal != 0:
      newField = revealElement(wl, x-1, y, newField)
  except:
    pass
  #left
  try:
    if yReal != 0:
      newField = revealElement(wl, x, y-1, newField)
  except:
    pass
  #up
  try:
    newField = revealElement(wl, x, y+1, newField)
  except:
    pass
  #down
  try:
    if yReal != 0:
      newField = revealElement(wl, x+1, y-1, newField)
  except:
    pass
  #upper-right
  try:
    if yReal != 0 and xReal != 0:
      newField = revealElement(wl, x-1, y-1, newField)
  except:
    pass
  #upper left
  try:
    newField = revealElement(wl, x+1, y+1, newField)
  except:
    pass
  #bottom-right
  try:
    if  xReal != 0:
      newField= revealElement(wl, x-1, y+1, newField)
  except:
    pass
  #bottom-left
  return newField

print revealSurroundings(10, 7, 2, hiddenField)

标签: pythonrecursionpython-2.xminesweeper

解决方案


问题似乎在于,当您运行revealSurroundingsinside时revealElement,您正在创建一个永无止境的循环。

运行时revealElement,如果元素为 0,revealSurroundings则运行该函数。在revealSurroundings你运行revealElement。如果显示的新元素也是零,它会再次运行revealSurroundings并从 的第一次迭代中检测零revealElement

这开始了一个永无止境的无限循环。我建议添加另一个条件 in revealSurroundings,例如检查您是否已经通过简单的 if 语句显示了它旁边的字符。由于@gorlen 所说,我还建议完全重写代码。


推荐阅读