gpt4 book ai didi

python - 扫雷:显示周围方 block 功能卡住

转载 作者:行者123 更新时间:2023-12-02 01:44:55 26 4
gpt4 key购买 nike

我正在尝试为我正在制作的扫雷游戏制作一个函数。该函数的目的是根据给定的 x 和 y(它所在的位置)显示一个元素。这可能不是实现它的最优雅的方法,但我正在为每个标题为 newField 的图 block 创建一个 ['-'] 列表。 “-”代表一个隐藏 block (您不知道它是否是炸弹或它周围有多少炸弹)。然后,我将 newField 中的一个元素更改为等于 listField 中对应的 block (这是一个列表列表。其中的每个列表代表一行)。 X代表一颗炸弹,数字代表周围有多少颗炸弹。

在扫雷中,当周围有零枚炸弹的方 block 被揭露时,它周围的方 block 也会被揭露。我制作了一个名为 revealSurroundings 的函数来完成此任务。当我像下面那样在函数 revealElement 中运行 revealSurroundings 时,它卡住了我的计算机。但是,如果我在函数 revealElement 之外运行 revealSurroundings ,它就可以正常工作。

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

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)

最佳答案

问题似乎是,当您在 revealElement 中运行 revealSurroundings 时,您正在创建一个永无休止的循环。

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

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

关于python - 扫雷:显示周围方 block 功能卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com