def check(y,x):
if ((y < 0) or (y >= len(mat1)) or (x < 0) or (x >= len(mat1))):
return (False)
else:
return mat1[y][x]
def addscores():
for x in range(len(mat1)):
for y in range(len(mat1)):
if mat1[y][x] != "b":
if check(y-1,x-1) == "b" or check(y,x-1) == "b" or check(y+1,x-1) == "b" or check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) =="b":
mat1[y][x] = 1
if check(y-1,x-1) == "b":
if check(y,x-1) == "b" or check(y+1,x-1) == "b" or check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y,x-1) == "b":
if check(y+1,x-1) == "b" or check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y+1,x-1) == "b":
if check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y+1,x) == "b":
if check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y+1,x+1) == "b":
if check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y,x+1) == "b":
if check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y-1,x+1) == "b":
if check(y-1,x) == "b":
mat1[y][x] = 2
if check(y-1,x-1) == "b":
if check(y,x-1) == "b":
if check(y+1,x-1) == "b" or check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y+1,x-1) == "b":
if check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y+1,x) == "b":
if check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y+1,x+1) == "b":
if check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y,x+1) == "b":
if check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y-1,x-1) == "b":
if check(y,x-1) == "b":
if check(y+1,x-1) == "b":
if check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 4
if check(y+1,x-1) == "b":
if check(y+1,x) == "b":
if check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 4
if check(y+1,x) == "b":
if check(y+1,x+1) == "b":
if check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 4
#ETC
答案 0 :(得分:2)
def check(y,x):
if ((y < 0) or (x < 0) or (y >= len(mat1)) or (x >= len(mat1))):
return False
else:
if mat1[y][x] == 'b':
return 1
else:
return 0
def check_all(y,x):
if mat1[y][x] != 'b':
return sum([check(y + yy, x + xx) for xx in range(-1,2) for yy in range(-1,2)])
else:
return 'b'
def addscores():
for x in range(len(mat1)):
for y in range(len(mat1)):
mat1[y][x] = check_all(y,x)
如果我已经明白你正在做什么,这段代码 - 尤其是check_all
函数 - 应该解决你的问题。你是对的,肯定有一个更短的方法,使用循环(或列表推导,在这种情况下),而不是必须单独写出每个检查。
我尽可能保留了您的代码,因为我没有足够的上下文来知道更改是否会破坏任何内容。
答案 1 :(得分:1)
您可以使用以下内容:
def add_scores():
def bounded_range(i):
return range(max(0, i - 1), min(len(mat1) - 1, x + 1))
for x in range(len(mat1)):
for y in range(len(mat1)):
Xs = bounded_range(x)
Ys = bounded_range(y)
mat1[x][y] = len([0 for _x in Xs for _y in Ys
if not (x == _x and y == _x) and mat1[_x][_y] == 'b'])
答案 2 :(得分:0)
如果你想弄清楚如何编写扫雷程序,你可以查看Python Cookbook上的MineSweep,它显示了开发游戏GUI版本的12个步骤。