Placing Python 1 around "b" in the grid

I have a grid that looks like this. I put one “b” randomly in the grid and put the number 1 around the letter “b”. It seems to work everywhere except when 1 should be placed on the bottom row and the column to the right. For example, it will look something like this.

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 1 b 0 0 0 0 0 0 0 0 0 0 

Where should he look like

 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 1 0 0 0 0 0 0 0 0 1 b 0 0 0 0 0 0 0 0 1 1 

Here is the code I use and I can not understand why these 1 arent posted there.

 from random import* mat1 = [] mat2 = [] def makemat(x): for y in range(x): list1 = [] list2 = [] for z in range(x): list1.append(0) list2.append("-") mat1.append(list1) mat2.append(list2) makemat(10) def printmat(mat): for a in range(len(mat)): for b in range(len(mat)): print(str(mat[a][b]) + "\t",end="") print("\t") def addmines(z): count = 0 while (count < z): x = randrange(0,len(mat1)) y = randrange(0,len(mat1)) if mat1[y][x] == "b": count -= 1 else: mat1[y][x] = "b" count += 1 addmines(1) def addscores(): for x in range(len(mat1)): for y in range(len(mat1)): if ((y < len(mat1)-1) and (x < len(mat1)-1)) and ((y >= 0) and (x >= 0))): if mat1[y+1][x] == "b": mat1[y][x] = 1 if mat1[y-1][x] == "b": mat1[y][x] = 1 if mat1[y][x+1] == "b": mat1[y][x] = 1 if mat1[y][x-1] == "b": mat1[y][x] = 1 if mat1[y+1][x+1] == "b": mat1[y][x] = 1 if mat1[y+1][x-1] == "b": mat1[y][x] = 1 if mat1[y-1][x+1] == "b": mat1[y][x] = 1 if mat1[y-1][x-1] == "b": mat1[y][x] = 1 printmat(mat1) addscores() 
+4
source share
3 answers

Your nested loop checks each square to see if it should have 1 in it. However, in the first if clause in addscores() you omit every square that lies on the edge of the square. A good way to solve this problem would be to omit claase if and instead add a function to check the square, which automatically checks the borders. For instance:

 def checksqu(y, x): if y < 0 or y >= len(mat1) or x < 0 or x >= len(mat1): return False return mat1[y][x] == 'b' 

Then instead of if mat1[y - 1][x - 1]: you can do if checksqu(y - 1, x - 1): (etc.).

+2
source

You can simplify this part:

 def addscores(): for x in range(len(mat1)): for y in range(len(mat1)): if ((y < len(mat1)-1) and (x < len(mat1)-1)) and ((y >= 0) and (x >= 0))): if mat1[y+1][x] == "b": mat1[y][x] = 1 if mat1[y-1][x] == "b": mat1[y][x] = 1 if mat1[y][x+1] == "b": mat1[y][x] = 1 if mat1[y][x-1] == "b": mat1[y][x] = 1 if mat1[y+1][x+1] == "b": mat1[y][x] = 1 if mat1[y+1][x-1] == "b": mat1[y][x] = 1 if mat1[y-1][x+1] == "b": mat1[y][x] = 1 if mat1[y-1][x-1] == "b": mat1[y][x] = 1 

Using this code:

 def addscores(): size = len(mat1) directions = [(dx, dy) for dx in [-1,0,1] for dy in [-1,0,1] if (dy!=0 or dx!=0)] for x in range(size): for y in range(size): for dx, dy in directions: try: if mat1[y+dy][x+dx] == "b": mat1[y][x] = 1 except: pass 
0
source

This seems like a trick:

 def addscores(mat): for y in range(len(mat)): for x in range(len(mat[y])): if mat[y][x] == 'b': mat = pad(mat, x, y, '1') return mat def pad(mat, x, y, n): for i, (x,y) in enumerate(itertools.product(range(x-1, x+2), range(y-1, y+2))): if i != 4: # the coordinate at index 4 is where the bomb is if 0<=y<len(mat) and 0<=x<len(mat[y]): mat[y][x] = n return mat 

Testing:

 In [127]: mat Out[127]: [['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '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', '1', '1'], ['0', '0', '0', '0', '0', '0', '0', '1', '0', '1']] In [129]: addscores(mat) Out[129]: [['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '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', '1'], ['0', '0', '0', '0', '0', '0', '0', '0', '1', 'b'], ['0', '0', '0', '0', '0', '0', '0', '0', '1', '1']] 
0
source

Source: https://habr.com/ru/post/1446576/


All Articles