简体   繁体   中英

Cascading reveal in Python Minesweeper

I am working on a recursive function to reveal the contents of any spaces around a zero. I am getting the type error "TypeError: unsupported operand type(s) for -: 'list' and 'int'" for my code. I want this code (when given a location) to reveal any spaces around it that are not mines up until there is a number.

import sys
from game import * 
from gameio import *
import random 

rows = int(sys.argv[1])
cols = int(sys.argv[2])
mines = int(sys.argv[3])

def main():
    mat = buildMatrix(rows,cols)
    status = buildMatrix(rows,cols)
    minematrix = placeMines(mat)
    smatrix = processMatrix(minematrix)
    print("Here is the minefield: ")
    displayBoard(smatrix,status)
    array = [int(i) for i in input("Enter a row and column: ").split()]
    first = unCover(status,smatrix,array)
    displayBoard(smatrix,status)
    return

main()

def mineCascade(m,mat,r,c):
    if (mat[r][c] == '&'):
        if (m[r][c] == '*'):
            return
        elif (m[r][c] == 0):
            if (r > 0) and (c > 0):
                if (mat[r-1][c-1] != '&'):
                    mat[r-1][c-1] = '&'
                    a = r-1
                    b = c-1
                    one = mineCascade(m,mat,a,b)
            else:
                return m[r-1][c-1]
            if (r > 0):
                if (mat[r-1][c] != '&'):
                    mat[r-1][c] = '&'
                    d = r-1
                    two = mineCascade(m,mat,d,c)
            else:
                return m[r-1][c]
            if (r > 0) and (c < len(m[0]-1)):
                if (mat[r-1][c+1] != '&'):
                    mat[r-1][c+1] = '&'
                    e  = r-1
                    f = c+1
                    three = mineCascade(m,mat,e,f)
            else:
                return m[r-1][c+1]
            if (c > 0):
                if (mat[r][c-1] != '&'):
                    mat[r][c-1] = '&'
                    g = c-1
                    four = mineCascade(m,mat,r,g)
            else:
                return m[r][c-1]
            if (c < len(m[0])-1):
                if (mat[r][c+1] != '&'):
                    mat[r][c+1] = '&'
                    h = c+1
                    five = mineCascade(m,mat,r,h)
            else:
                return m[r][c+1]
            if (r < len(m)-1) and (c > 0):
                if (mat[r+1][c-1] != '&'):
                    mat[r+1][c-1] = '&'
                    i = r+1
                    j = c-1
                    six = mineCascade(m,mat,i,j)
            else:
                return m[r+1][c-1]
            if (r < len(m)-1):
                if (mat[r+1][c] != '&'):
                    mat[r+1][c] = '&'
                    k = r+1
                    seven = mineCascade(m,mat,k,c)
            else:
                return m[r+1][c]
            if (r < len(m)-1) and (c < len(m[0])-1):
                if (mat[r+1][c+1] != '&'):
                    mat[r+1][c+1] = '&'
                    l = r+1
                    n = c+1
                    eight = mineCascade(m,mat,l,n)
            else:
                return m[r+1][c+1]
    else:
        return

def unCover(m,mat,array):
    m[array[0]][array[1]] = '&'
    one = int(array[0])
    two = int(array[1])
    mineCascade(mat,m,one,two)
    return m[array[0]][array[1]]

def numberMines(m,r,c):
    count = 0
    if (r > 0) and (c > 0) and (m[r-1][c-1] == '*'):
        count += 1
    if (r > 0) and (m[r-1][c] == '*'):
        count += 1
    if (r > 0) and (c < len(m[0])-1) and (m[r-1][c+1] == '*'):
        count += 1
    if (c > 0) and (m[r][c-1] == '*'):
        count += 1
    if (c < len(m[0])-1) and (m[r][c+1] == '*'):
        count += 1
    if (r < len(m)-1) and (c > 0) and (m[r+1][c-1] == '*'):
        count += 1
    if (r < len(m)-1) and (m[r+1][c] == '*'):
        count += 1
    if (r < len(m)-1) and (c < len(m[0])-1) and (m[r+1][c+1] == '*'):
        count += 1
    return count

def processMatrix(mat):
    row = len(mat)
    col = len(mat[0])
    for r in range(row):
        for c in range(col):
           if (mat[r][c] != '*'):
                new = numberMines(mat,r,c)
                mat[r][c] = new
    return mat

def placeMines(mat):
    for i in range(mines):
        mrow = random.randint(0,(rows)-1)
        mcol = random.randint(0,(cols)-1)
        if (mat[mrow][mcol] != '*'):
            if (mrow < rows) and (mcol < cols):
                 mat[mrow][mcol] = '*'
        else:
            return mat
    return mat

def printMatrix(m,mat):
    rows = len(m)
    cols = len(m[0])
    for i in range(0,rows,1):
        for j in range(0,cols,1):
            if (mat[i][j] == '&'):
                if (m[i][j] == 0):
                    print('[',' ',']',sep = "", end = " ")
                else:
                    print('[',m[i][j],']',sep = "", end = " ")
            else:
                print('[',mat[i][j],']',sep = "", end = " ")
        print()
    return

def buildMatrix(rows,cols):
    mat = []
    for i in range(0,rows,1):
        mat += [buildRow(cols)]
    return mat

def buildRow(cols):
    row = []
    for i in range(0,cols,1):
        row += ['-']
    return row

def displayBoard(m,mat):
    rows = len(m)
    cols = len(m[0])
    for i in range(0,rows,1):
        for j in range(0,cols,1):
            if (mat[i][j] == '&'):
                if (m[i][j] == 0):
                    print('[',' ',']',sep = "", end = " ")
                else:
                    print('[',m[i][j],']',sep = "", end = " ")
            else:
                print('[',mat[i][j],']',sep = "", end = " ")
        print()
    return

Error:
Traceback (most recent call last):
  File "level3.py", line 21, in <module>
    main()    
  File "level3.py", line 17, in main
    first = unCover(status,smatrix,array)
  File line 81, in unCover
    mineCascade(mat,m,one,two)
  File line 19, in mineCascade
    one = mineCascade(m,mat,a,b)
  File line 19, in mineCascade
    one = mineCascade(m,mat,a,b)
  File line 19, in mineCascade
    one = mineCascade(m,mat,a,b)
  File line 26, in mineCascade
    two = mineCascade(m,mat,d,c)
  File line 26, in mineCascade
    two = mineCascade(m,mat,d,c)
  File line 19, in mineCascade
    one = mineCascade(m,mat,a,b)
  File line 19, in mineCascade
    one = mineCascade(m,mat,a,b)
  File line 29, in mineCascade
    if (r > 0) and (c < len(m[0]-1)):
TypeError: unsupported operand type(s) for -: 'list' and 'int'

In function mineCascade , you've a syntax error, probably around line 29, as the traceback suggests. Change

if (r > 0) and (c < len(m[0]-1)):

to

if (r > 0) and (c < len(m[0])-1):

Lots of the parentheses are unnecessary due to operator precedence , and can be simplified to:

if r > 0 and c < len(m[0]) - 1:

The redundant parentheses are probably what hid the error from you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM