高效读取二维数组中另一个值周围的值



我正忙着尝试用 Python 3.5 编写一个版本的扫雷,我这里的代码旨在查看我作为游戏板创建的二维数组中的每个值,并检查它周围有多少地雷(用"O"表示)。它有效,但我觉得代码太笨拙了。python 中是否有一个函数可以做到这一点,或者我可以编写一个函数来简化它?

if 语句首先检查值是否在数组中,以便不会超出范围,然后在位置进行检查。

for x in range(len(array)):
        for y in range(len(array[0])):
            if array[x][y] != "O":
                counter = 0
                if (x - 1 >= 0) and (array[x-1][y] == "O"):
                    counter += 1
                if (x + 1 < len(array)) and (array[x+1][y] == "O"):
                    counter += 1
                if (y - 1 >= 0) and (array[x][y+1] == "O"):
                    counter += 1
                if (y + 1 < len(array)) and (array[x][y+1] == "O"):
                    counter += 1
                if (x - 1 >= 0) and (y - 1 >= 0) and (array[x-1][y-1] == "O"):
                    counter += 1
                if (x - 1 >= 0) and (y + 1 < len(array)) and (array[x-1][y+1] == "O"):
                    counter += 1
                if (x + 1 < len(array)) and (y - 1 >= 0) and (array[x+1][y-1] == "O"):
                    counter += 1
                if (x + 1 < len(array)) and (y + 1 < len(array)) and (array[x+1][y+1] == "O"):
                    counter += 1
                array[x][y] = counter

在 array[x][y] 之后尝试这个函数!= 0

def checkNeighbours(x,y,matrix):
    neighbourValues = [matrix[x_][y_] for x_ in range(x-1,x+2) for y_ in range(y-1,y+2)
                       if (0 <= x_ < len(matrix) and 0 <= y_ < len(matrix) and (x_ != x or y_ != y))]
    return neighbourValues.count('O')

一个可能的基于 numpy 的解决方案是这样的。检查评论以确保您了解他们在做什么:

 import numpy as np
 m = np.random.randint(0,2,(5,5)).astype('bool')
 print(m)  # game matrix
 # Running all cels in matrix
 for i in range(m.shape[0]):
     for j in range(m.shape[1]):
         # clip will ensure you'll never have a value below 0 for indexes which
         # is important at the borders of the game.
         c = np.count_nonzero(m[np.clip(i-1,0,m.shape[0]):i+2,np.clip(j-1,0,m.shape[1]):j+2])
         if m[i,j]:
             c = c - 1 # ensure you are not couting the cell where you are in.
         print('FOR POSITION ',i,j,' FOUND ',c,' MINES.')

结果是这样的:

[[False False False  True False]
 [ True  True False  True  True]
 [ True  True  True  True  True]
 [ True  True  True False  True]
 [ True  True  True  True False]]
FOR POSITION  0 0  FOUND  2  MINES.
FOR POSITION  0 1  FOUND  2  MINES.
FOR POSITION  0 2  FOUND  3  MINES.
FOR POSITION  0 3  FOUND  3  MINES.
FOR POSITION  0 4  FOUND  3  MINES.
FOR POSITION  1 0  FOUND  4  MINES.
FOR POSITION  1 1  FOUND  5  MINES.
FOR POSITION  1 2  FOUND  6  MINES.
FOR POSITION  1 3  FOUND  6  MINES.
FOR POSITION  1 4  FOUND  5  MINES.
FOR POSITION  2 0  FOUND  6  MINES.
FOR POSITION  2 1  FOUND  8  MINES.
FOR POSITION  2 2  FOUND  7  MINES.
FOR POSITION  2 3  FOUND  7  MINES.
FOR POSITION  2 4  FOUND  5  MINES.
FOR POSITION  3 0  FOUND  6  MINES.
FOR POSITION  3 1  FOUND  9  MINES.
FOR POSITION  3 2  FOUND  8  MINES.
FOR POSITION  3 3  FOUND  7  MINES.
FOR POSITION  3 4  FOUND  4  MINES.
FOR POSITION  4 0  FOUND  4  MINES.
FOR POSITION  4 1  FOUND  6  MINES.
FOR POSITION  4 2  FOUND  5  MINES.
FOR POSITION  4 3  FOUND  4  MINES.
FOR POSITION  4 4  FOUND  2  MINES.

相关内容

最新更新