Search code examples
pythonmatrix2d-gamesminesweeper

Python minesweeper matrix out of bounds looping and last row not filling


I'm creating a minesweeper game in python, still not using any kind of interface, only in ascii, while trying to implement the code by hand as i dont have alot of experience in py i'm getting a bit frustated debugging my code, i have a problem that when a "bomb(X)" is in the last row or first column it loops to the other side, and if it is on the last column, or firstcolumn and last row the code doesnt work at all

Matrix Loop Example

 ['1' 'X' '1' '0' 'X']
 ['1' '1' '1' '0' '0']
 ['0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0']
 ['1' '1' '1' '0' '0']

Matrix Last Column Bug

 ['0' '0' '0' '0' '0']
 ['0' '0' '1' '1' '1']
 ['0' '0' '1' 'X' '1']
 ['0' '0' '1' '1' '1']
 ['0' '0' '0' 'X' '0']

Relevant Code

Number Filling Algorithm

def popularNumeros(column,row,matrix):
    column -=1
    row -=1
    for col in range(column):
        for ro in range(row):
            if matrix[col][ro] == 'X':
                try:
                    matrix[col+1][ro+1] = checker(matrix[col+1][ro+1])#diagonal inferior direita
                    matrix[col+1][ro-1] = checker(matrix[col+1][ro-1])#diagonal inferior esquerda
                    matrix[col-1][ro-1] = checker(matrix[col-1][ro-1])#diagonal superior esquerda
                    matrix[col-1][ro+1] = checker(matrix[col-1][ro+1])#diagonal superior direita
                    matrix[col-1][ro] = checker(matrix[col-1][ro])#cima
                    matrix[col+1][ro] = checker(matrix[col+1][ro])#baixo
                    matrix[col][ro-1] = checker(matrix[col][ro-1])#esquerda
                    matrix[col][ro+1] = checker(matrix[col][ro+1])#direita
                except:
                    ro+=1

Bomb Checker

def checker(matrixItem):
    if matrixItem == 'X':
        return matrixItem

    else:
        return str(int(matrixItem)+1)

Solution

  • Indexing negatively into a list (like when col is 0 so col-1 = -1) wraps around to the end of the list, returning the last item in the list.

    Indexing past the last item of the list raises an IndexError exception - IndexError: list index out of range. When this occurs, Python immediately jumps to the except block, and once that finishes, it does not go back up to where it was when the exception was first raised.

    You need to make sure you don't negatively index into your matrix list, or index past the end.

    You can use lots of if statements around each of the lines in the try block, or you could use a for loop to check the surrounding cells, like so:

    def popularNumeros(column, row, matrix):
        for col in range(column):
            for ro in range(row):
                if matrix[col][ro] == 'X':
                    for offset_column in range(col-1, col+2):
                        if offset_column < 0 or offset_column >= column:
                            continue # Outside of the safe boundary - skip
    
                        for offset_row in range(ro-1, row+2):
                            if offset_row < 0 or offset_row >= row:
                                continue # Outside of the safe boundary - skip
    
                            matrix[offset_column][offset_row] = checker(matrix[offset_column][offset_row])
    

    Also, for what it's worth, you have column and row back to front. I've left it that way.