python - 扫雷:显示周围的块功能冻结
问题描述
我正在尝试为我正在制作的扫雷游戏制作一个功能。此函数的目的是显示给定 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)
解决方案
问题似乎在于,当您运行revealSurroundings
inside时revealElement
,您正在创建一个永无止境的循环。
运行时revealElement
,如果元素为 0,revealSurroundings
则运行该函数。在revealSurroundings
你运行revealElement
。如果显示的新元素也是零,它会再次运行revealSurroundings
并从 的第一次迭代中检测零revealElement
。
这开始了一个永无止境的无限循环。我建议添加另一个条件 in revealSurroundings
,例如检查您是否已经通过简单的 if 语句显示了它旁边的字符。由于@gorlen 所说,我还建议完全重写代码。
推荐阅读
- spring - 带有 Spring Security 的 SpringBoot 2 执行器
- python - 从列表中添加和检索项目
- visual-studio - Reporting Services tablix 不允许我冻结列标题和行
- spring - 由于 Spring 5 中的 Access / CORS 问题,无法访问我的后端的 URL
- python - 通过根据 MultiIndex 级别更改的值过滤列
- c# - C#/Unity:Visual Studio 参数未通过
- c++ - Avro C++:如何压缩内容?
- firebase - 在 onResume() 方法中。当我输入“mFirebaseAuth.addAuthStateListener(mAuthStateListener);”时,不幸的是在安装时停止了
- google-sheets - Google表格-如何将第1列中的每个项目与第2列中的每个项目连接起来
- c# - 保持外部编辑的数据完整性