Search code examples
pythonminesweeper

Minesweeper python, weird results when checking for contents of neighbouring cells


Full code here I am trying to check neighbours for a cell called by user. Edit:"Sorry for not being clear. The problem is when I check for example cell 46. I want to know how many of surrounding 8 cells are mines. Unfortunately it does not do that for many cells and I cannot understand why. Often it gives me 2 digit numbers for example.

def selfncheck(grid,matrix,ident): #shenanigans are present
'''check self and neighbours for being a mine. If not mined open neighbours'''

identifier=[]
identifier.append(ident)
for ident in identifier:
    cellvalue=0

    if ident==0: #top left corner +1 +11 +10
        if matrix[ident+1]=='x':
            cellvalue+=1
        if matrix[ident+10]=='x':
            cellvalue+=1
        if matrix[ident+11]=='x':
            cellvalue+=1
        if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
            identifier.append(ident+1)
            identifier.append(ident+11)
            identifier.append(ident+10)

    if ident==9: #Top right corner -1 +10 +9
        if matrix[ident-1]=='x':
            cellvalue+=1
        if matrix[ident+10]=='x':
            cellvalue+=1
        if matrix[ident+9]=='x':
            cellvalue+=1
        if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
            identifier.append(ident-1)
            identifier.append(ident+10)
            identifier.append(ident+9)

    if ident==90: #bot left corner +1 -10 -9
        if matrix[ident+1]=='x':
            cellvalue+=1
        if matrix[ident-10]=='x':
            cellvalue+=1
        if matrix[ident-9]=='x':
            cellvalue+=1
        if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
            identifier.append(ident+1)
            identifier.append(ident-9)
            identifier.append(ident+-10)

    if ident==99: #bot right corenr -10 -11 -1
        if matrix[ident-1]=='x':
            cellvalue+=1
        if matrix[ident-10]=='x':
            cellvalue+=1
        if matrix[ident-11]=='x':
            cellvalue+=1
        if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
            identifier.append(ident-1)
            identifier.append(ident-10)
            identifier.append(ident-11)

    if ident>0 and ident<9: #checking top row -1 +1 +10 +9 +11 cells only
        if matrix[ident-1]=='x':
            cellvalue+=1
        if matrix[ident+1]=='x':
            cellvalue+=1
        if matrix[ident+10]=='x':
            cellvalue+=1
        if matrix[ident+9]=='x':
            cellvalue+=1
        if matrix[ident+11]=='x':
            cellvalue+=1
        if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
            identifier.append(ident+1)
            identifier.append(ident-1)
            identifier.append(ident+10)
            identifier.append(ident+9)
            identifier.append(ident+11)

    if ident>90 and ident<99: #checking bot row +1 -1 -10 -9 -11
        if matrix[ident-1]=='x':
            cellvalue+=1
        if matrix[ident+1]=='x':
            cellvalue+=1
        if matrix[ident-10]=='x':
            cellvalue+=1
        if matrix[ident-9]=='x':
            cellvalue+=1
        if matrix[ident-11]=='x':
            cellvalue+=1
        if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
            identifier.append(ident+1)
            identifier.append(ident-1)
            identifier.append(ident-10)
            identifier.append(ident-9)
            identifier.append(ident-11)

    for i in range(1,10,1): 

        if ident==i*10: #checking left side +1 -10 +10 -9 +11
            if matrix[ident-1]=='x':
                cellvalue+=1
            if matrix[ident+10]=='x':
                cellvalue+=1
            if matrix[ident-10]=='x':
                cellvalue+=1
            if matrix[ident-9]=='x':
                cellvalue+=1
            if matrix[ident+11]=='x':
                cellvalue+=1
            if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
                identifier.append(ident+1)
                identifier.append(ident-10)
                identifier.append(ident-9)
                identifier.append(ident+10)
                identifier.append(ident+11) 

        if ident==(i+9)*10: # checking right side -1 -10 +10 +9 -11
            if matrix[ident-1]=='x':
                cellvalue+=1
            if matrix[ident-10]=='x':
                cellvalue+=1
            if matrix[ident+10]=='x':
                cellvalue+=1
            if matrix[ident+9]=='x':
                cellvalue+=1
            if matrix[ident-11]=='x':
                cellvalue+=1
            if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
                identifier.append(ident-1)
                identifier.append(ident-1)
                identifier.append(ident+10)
                identifier.append(ident+9)
                identifier.append(ident-11)

        if ident<i*10 and ident<(i+9)*10: #checking +1 -1 -9 -11 -10 +9 +10 +11
            if matrix[ident+1]=='x':
                cellvalue+=1
            if matrix[ident-1]=='x':
                cellvalue+=1
            if matrix[ident-9]=='x':
                cellvalue+=1
            if matrix[ident-11]=='x':
                cellvalue+=1
            if matrix[ident-10]=='x':
                cellvalue+=1
            if matrix[ident+9]=='x':
                cellvalue+=1
            if matrix[ident+10]=='x':
                cellvalue+=1
            if matrix[ident+11]=='x':
                cellvalue+=1
            if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
                identifier.append(ident+1)
                identifier.append(ident-1)
                identifier.append(ident-9)
                identifier.append(ident-11)
                identifier.append(ident-10)
                identifier.append(ident+9)
                identifier.append(ident+10)
                identifier.append(ident+11)

    grid[ident]=str(cellvalue)
return grid

Solution

  • The reason you get numbers larger than 9 is probably this line:

    if ident<i*10 and ident<(i+9)*10: #checking +1 -1 -9 -11 -10 +9 +10 +11
    

    to make sense you need to switch the first comparison. Otherwise the second condition is useless and that is probably not, what you intended.

    if ident>i*10 and ident<(i+9)*10: #checking +1 -1 -9 -11 -10 +9 +10 +11