Search code examples
pythonminesweeper

Program does not count numbers of a field rigth


I tried to program Minesweeper in python. Counting the numbers of an field surrounded by bombs, I get an worse problem - no error...

enter image description here

My code is:

for i in range(len(bombs)):
    numbers[bombs[i]] = -1

    if (bombs[i][0] > 0):
        if (numbers[(bombs[i][0]-step, bombs[i][1]+0)] != -1):
            numbers[(bombs[i][0]-step, bombs[i][1]+0)] = numbers[(bombs[i][0]-step, bombs[i][1]+0)] + 1
            if (bombs[i][1] > 0):
                if (numbers[(bombs[i][0]-step, bombs[i][1]-step)] != -1):
                    numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1
            if (bombs[i][1] < heigth-step-1):
                if (numbers[(bombs[i][0]-step, bombs[i][1]+step)] != -1):
                    numbers[(bombs[i][0]-step, bombs[i][1]+step)] = numbers[(bombs[i][0]-step, bombs[i][1]+step)] + 1

    if (bombs[i][0] < width-step-1):
        if (numbers[(bombs[i][0]+step, bombs[i][1]+0)] != -1):
            numbers[(bombs[i][0]+step, bombs[i][1]+0)] = numbers[(bombs[i][0]+step, bombs[i][1]+0)] + 1
            if (bombs[i][1] > 0):
                if (numbers[(bombs[i][0]+step, bombs[i][1]-step)] != -1):
                    numbers[(bombs[i][0]+step, bombs[i][1]-step)] = numbers[(bombs[i][0]+step, bombs[i][1]-step)] + 1
            if (bombs[i][1] < heigth-step-1):
                if (numbers[(bombs[i][0]+step, bombs[i][1]+step)] != -1):
                    numbers[(bombs[i][0]+step, bombs[i][1]+step)] = numbers[(bombs[i][0]+step, bombs[i][1]+step)] + 1

    if (bombs[i][1] > 0):
        if (numbers[(bombs[i][0]+0, bombs[i][1]-step)] != -1):
            numbers[(bombs[i][0]+0, bombs[i][1]-step)] = numbers[(bombs[i][0]+0, bombs[i][1]-step)] + 1

    if (bombs[i][1] < heigth-step-1):
        if (numbers[(bombs[i][0]+0, bombs[i][1]+step)] != -1):
            numbers[(bombs[i][0]+0, bombs[i][1]+step)] = numbers[(bombs[i][0]+0, bombs[i][1]+step)] + 1

step = size of the fields

bombs = all bombs in an array


Solution

  • I think a problem is in this section of code:

                if (bombs[i][0] > 0):
                    # This bomb is not in the leftmost column of the grid
            #        print(numbers[(bombs[i][0]-step, bombs[i][1]+0)])
                    if (numbers[(bombs[i][0]-step, bombs[i][1]+0)] != -1):
                        # The cell to the left is not a bomb, so add one to its count
                        numbers[(bombs[i][0]-step, bombs[i][1]+0)] = numbers[(bombs[i][0]-step, bombs[i][1]+0)] + 1
    
                        if (bombs[i][1] > 0):
                            # This bomb is not in the top row of the grid.
                            if (numbers[(bombs[i][0]-step, bombs[i][1]-step)] != -1):
                                # The cell above and to the left is not a bomb, so add one to its count
                                numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1
    

    Note that we only look at the cell above and to the left of the bomb and add one to it if it isn't a bomb if the cell to the left is also not a bomb. This is incorrect: for any bomb, we need to check the cell above and to the left of it whether or not the cell to the left of it is also a bomb.

    What you want to do is to remove a level of indentation from the third if statement in the above:

                if (bombs[i][0] > 0):
                    # This bomb is not in the leftmost column of the grid
            #        print(numbers[(bombs[i][0]-step, bombs[i][1]+0)])
                    if (numbers[(bombs[i][0]-step, bombs[i][1]+0)] != -1):
                        # The cell to the left is not a bomb, so add one to its count
                        numbers[(bombs[i][0]-step, bombs[i][1]+0)] = numbers[(bombs[i][0]-step, bombs[i][1]+0)] + 1
    
                    # This line will now be reached if there is another bomb to the left of bombs[i].
                    if (bombs[i][1] > 0):
                        # This bomb is not in the top row of the grid.
                        if (numbers[(bombs[i][0]-step, bombs[i][1]-step)] != -1):
                            # The cell above and to the left is not a bomb, so add one to its count
                            numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1
    

    You would also make the same changes to the other three if statements that handle adding one to the counts for diagonally-adjacent squares in other directions.


    Looking at your code, I'm not sure you need the -1 in conditions such as if (bombs[i][1] < heigth-step-1):. Clearly these checks are trying to stop you going off the edge of the grid, but the extra -1 is unnecessary. I'm also a bit concerned about step: is this equal to 1? You are including step in your checks for falling off the right-hand side or the bottom of the grid, but not when falling off the top or left-hand side, so if step is greater than 1 you may run into problems. (For example, if step is 3 and bombs[i][0] is 1, bombs[i][0] - step will be -2.)

    Also, can I make a couple of suggestions about your code from a readability point of view? Firstly, you are repeating bombs[i][0] and bombs[i][1] a lot: if you add lines x = bombs[i][0] and y = bombs[i][1] you could write x and y instead of bombs[i][0] and bombs[i][1] in the rest of the loop. Secondly, instead of writing some_expression = some_expression + 1 you can write some_expression += 1. Making these changes would shorten a line such as

    numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1
    

    to

    numbers[(x-step, y-step)] += 1