1
votes

Excluding the boundary zero values, is it possible to group the coordinates (as tuples) of remaining zero values into different lists in this numpy array?

[[ 0  0  0  0  0  0  0  0  0  0  0]
 [ 0  1  1  1  0  0  0  1 10  2  0]
 [ 0  2 10  2  1  0  0  1  2 10  0]
 [ 0 10  3 10  1  0  0  0  1  1  0]
 [ 0  1  2  1  1  0  0  0  0  0  0]
 [ 0  1  2  1  2  2  2  1  0  0  0]
 [ 0 10  2 10  2 10 10  1  0  0  0]
 [ 0  1  2  1  2  2  2  1  1  1  0]
 [ 0  0  0  0  0  0  0  0  1 10  0]
 [ 0  0  0  0  0  0  0  0  1  1  0]
 [ 0  0  0  0  0  0  0  0  0  0  0]]

for ex. in above grid, there are two 'groups' of zeros, one in lower left corner and other in upper right corner. can these be put into separate lists, for every such matrix generated? Below is the code for creating matrix 'sol_mat' :-

import numpy as np
import random

bomb_mat = np.zeros((11,11), dtype = int)
for i in range(10):
    a = random.randint(1,9)
    b = random.randint(1,9)
    bomb_mat[a,b] = 1

sol_mat = np.zeros(11,11), dtype = int)
for j in range(1,10):
    for k in range(1,y-1):
        if bomb_mat[j,k] == 1:
            sol_mat[j,k] = 10
        else:
            sol_mat[j,k] = bomb_mat[j-1,k-1] + bomb_mat[j,k-1] + bomb_mat[j+1,k-1]+ bomb_mat[j-1,k] + bomb_mat[j+1,k] + bomb_mat[j-1,k+1] + bomb_mat[j,k+1] + bomb_mat[j+1,k+1]

Trying to create minesweeper

1
Can you provide the example desired output for your exmaple input?Itamar Mushkin

1 Answers

1
votes

I made some adjustments on your code. Mainly I tried to avoid for loops and I used scipys convolve2d() to create sol_mat. The main advantage of this method is that you don't have to worry about the edge cases of the image. Using a 3x3 kernel of ones on the boolean array of bombs gives you exactly the number of neighbouring bombs (the flags in minesweeper).

import numpy as np
from scipy.signal import convolve2d

grid_size = (7, 7)
n_bombs = 5

bomb_mat = np.zeros(grid_size, dtype=int)
bomb_mat[np.random.randint(low=1, high=grid_size[0]-1, size=n_bombs),
         np.random.randint(low=1, high=grid_size[1]-1, size=n_bombs)] = 1
# array([[0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 1, 0, 0],
#        [0, 0, 1, 0, 0, 0, 0],
#        [0, 1, 1, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 1, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0]])

sol_mat = convolve2d(bomb_mat, np.ones((3, 3)), mode='same').astype(int)
sol_mat[bomb_mat.astype(bool)] = 10
# array([[ 0,  0,  0,  1,  1,  1,  0],
#        [ 0,  1,  1,  2, 10,  1,  0],
#        [ 1,  3, 10,  3,  1,  1,  0],
#        [ 1, 10, 10,  2,  0,  0,  0],
#        [ 1,  3,  3,  2,  0,  0,  0],
#        [ 0,  1, 10,  1,  0,  0,  0],
#        [ 0,  1,  1,  1,  0,  0,  0]])

You can use np.tril() and np.triu() to get the lower and upper triangle of an array. By building the intersection of boolean triangles with the condition sol_mat == 0 you get the wanted indices:

lower0 = np.logical_and(np.tril(np.ones(grid_size)), sol_mat == 0)
# lower0.astype(int)
# array([[1, 0, 0, 0, 0, 0, 0],
#        [1, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 1, 0, 0],
#        [1, 0, 0, 0, 1, 1, 0],
#        [1, 0, 0, 0, 1, 1, 1]])
upper0 = np.logical_and(np.triu(np.ones(grid_size)), sol_mat == 0)
# upper0.astype(int)
# array([[1, 1, 1, 0, 0, 0, 1],
#        [0, 0, 0, 0, 0, 0, 1],
#        [0, 0, 0, 0, 0, 0, 1],
#        [0, 0, 0, 0, 1, 1, 1],
#        [0, 0, 0, 0, 1, 1, 1],
#        [0, 0, 0, 0, 0, 1, 1],
#        [0, 0, 0, 0, 0, 0, 1]])

You can get the indices of these arrays via np.nonzero():

lower0_idx = np.array(np.nonzero(lower0))
# array([[0, 1, 4, 5, 5, 5, 6, 6, 6, 6],
#        [0, 0, 4, 0, 4, 5, 0, 4, 5, 6]])
upper0_idx = np.array(np.nonzero(upper0))
# array([[0, 0, 0, 0, 1, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6],
#        [0, 1, 2, 6, 6, 6, 4, 5, 6, 4, 5, 6, 5, 6, 6]])