how do you shade cells that will be useless when you click on them?

Iterate through all cells of the grid using nested loops and the indexes (i, j):

for i in range(len(squares)):
    for j in range(len(squares[i])):
        # [...]

Find all the adjacent index tuples (l, k) of a cell (i, j):

adjacent = []
for k in range(max(0,i-1), min(len(squares), i+2)):
    for l in range(max(0,j-1), min(len(squares[k]), j+2)):
        if i != k or j != l:
            adjacent.append((k, l))

Change the color of a cell if a cell (i, j) doesn’t have a neighbor (not any()) (l, k) where the difference is at least 2:

if not any((k, l) for (k, l) in adjacent if abs(squares[i][j].num - squares[k][l].num) > 1):
    squares[i][j].color = (140, 140, 140)

Complete nested loop

while True:
    # [...]

    for i in range(len(squares)):
        for j in range(len(squares[i])):

            adjacent = []
            for k in range(max(0,i-1), min(len(squares), i+2)):
                for l in range(max(0,j-1), min(len(squares[k]), j+2)):
                    if i != k or j != l:
                        adjacent.append((k, l))

            if not any((k, l) for (k, l) in adjacent if abs(squares[i][j].num - squares[k][l].num) > 1):
                 squares[i][j].color = (140, 140, 140)
            elif squares[i][j].color == (140, 140, 140):
                square.clear()

    for row in squares:
        for square in row:
            square.draw()

    # [...]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top