Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查“周围的值”;b";在矩阵中_Python_Matrix_Minesweeper - Fatal编程技术网

Python 检查“周围的值”;b";在矩阵中

Python 检查“周围的值”;b";在矩阵中,python,matrix,minesweeper,Python,Matrix,Minesweeper,扫雷舰,还是。 我找到了一种方法,但我知道必须有一种简化的方法。我必须在矩阵中放置一个数字来表示它周围有多少炸弹(“b”)。这就是我所拥有的,我知道必须有一条更短的路 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 addsc

扫雷舰,还是。 我找到了一种方法,但我知道必须有一种简化的方法。我必须在矩阵中放置一个数字来表示它周围有多少炸弹(“b”)。这就是我所拥有的,我知道必须有一条更短的路

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
def检查(y,x):
如果((y<0)或(y>=len(mat1))或(x<0)或(x>=len(mat1)):
返回(假)
其他:
返回mat1[y][x]
def addscores():
对于范围内的x(len(mat1)):
对于范围内的y(len(mat1)):
如果mat1[y][x]!=“b”:
如果检查(y-1,x-1)=“b”或检查(y,x-1)=“b”或检查(y+1,x-1)=“b”或检查(y+1,x)=“b”或检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x+1)=“b”:
mat1[y][x]=1
如果检查(y-1,x-1)=“b”:
如果检查(y,x-1)=“b”或检查(y+1,x-1)=“b”或检查(y+1,x)=“b”或检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=2
如果检查(y,x-1)=“b”:
如果检查(y+1,x-1)=“b”或检查(y+1,x)=“b”或检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=2
如果检查(y+1,x-1)=“b”:
如果检查(y+1,x)=“b”或检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=2
如果检查(y+1,x)=“b”:
如果检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=2
如果检查(y+1,x+1)=“b”:
如果检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=2
如果检查(y,x+1)=“b”:
如果检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=2
如果检查(y-1,x+1)=“b”:
如果检查(y-1,x)=“b”:
mat1[y][x]=2
如果检查(y-1,x-1)=“b”:
如果检查(y,x-1)=“b”:
如果检查(y+1,x-1)=“b”或检查(y+1,x)=“b”或检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=3
如果检查(y+1,x-1)=“b”:
如果检查(y+1,x)=“b”或检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=3
如果检查(y+1,x)=“b”:
如果检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=3
如果检查(y+1,x+1)=“b”:
如果检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=3
如果检查(y,x+1)=“b”:
如果检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=3
如果检查(y-1,x-1)=“b”:
如果检查(y,x-1)=“b”:
如果检查(y+1,x-1)=“b”:
如果检查(y+1,x)=“b”或检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=4
如果检查(y+1,x-1)=“b”:
如果检查(y+1,x)=“b”:
如果检查(y+1,x+1)=“b”或检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=4
如果检查(y+1,x)=“b”:
如果检查(y+1,x+1)=“b”:
如果检查(y,x+1)=“b”或检查(y-1,x+1)=“b”或检查(y-1,x)=“b”:
mat1[y][x]=4
#等

您可以使用以下内容:

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'])
def检查(y,x):
如果((y<0)或(x<0)或(y>=len(mat1))或(x>=len(mat1)):
返回错误
其他:
如果mat1[y][x]=“b”:
返回1
其他:
返回0
def检查全部(y,x):
如果mat1[y][x]!='b':
返回和([检查范围(-1,2)内xx的(y+yy,x+xx)和范围(-1,2)内yy的(y+yy,x+xx)])
其他:
返回“b”
def addscores():
对于范围内的x(len(mat1)):
对于范围内的y(len(mat1)):
mat1[y][x]=全部选中(y,x)
如果我正确理解了您的操作,那么这段代码——特别是
check\u all
函数——应该会解决您的问题。你是对的,肯定有一种更短的方法,使用循环(在本例中是列表理解),而不是单独写出每个检查


我尽可能地保留了您的代码,因为我没有足够的上下文来知道进行更改是否会破坏任何东西。

如果您试图了解如何编写“扫雷者”,您可以查看Python Cookbook,它显示了开发游戏GUI版本的十二步过程。

您应该认真研究一下这一点。另外,您在
for
循环中声明了一个函数,这是一种非常糟糕的做法。我帮你搬出去了。顺便说一句,我注意到你到目前为止已经问了5个问题,到目前为止你还没有接受任何一个问题的答案。如果您想让人们继续回答您的问题,您应该回顾此问题和其他问题,并接受最佳答案。为什么
False
1
之和等于
1
?请参阅。Python
bool
子类
int
,而
False
的值为0
True
的值为1。
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)