recursion - Cascading reveal in Python Minesweeper -
i working on recursive function reveal contents of spaces around zero. getting type error "typeerror: unsupported operand type(s) -: 'list' , 'int'" code. want code (when given location) reveal spaces around not mines until there number.
import sys game import * 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 minefield: ") displayboard(smatrix,status) array = [int(i) in input("enter row , 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) , (c > 0): if (mat[r-1][c-1] != '&'): mat[r-1][c-1] = '&' = r-1 b = c-1 1 = 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 2 = minecascade(m,mat,d,c) else: return m[r-1][c] if (r > 0) , (c < len(m[0]-1)): if (mat[r-1][c+1] != '&'): mat[r-1][c+1] = '&' e = r-1 f = c+1 3 = 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 4 = 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 5 = minecascade(m,mat,r,h) else: return m[r][c+1] if (r < len(m)-1) , (c > 0): if (mat[r+1][c-1] != '&'): mat[r+1][c-1] = '&' = r+1 j = c-1 6 = 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 7 = minecascade(m,mat,k,c) else: return m[r+1][c] if (r < len(m)-1) , (c < len(m[0])-1): if (mat[r+1][c+1] != '&'): mat[r+1][c+1] = '&' l = r+1 n = c+1 8 = minecascade(m,mat,l,n) else: return m[r+1][c+1] else: return def uncover(m,mat,array): m[array[0]][array[1]] = '&' 1 = int(array[0]) 2 = int(array[1]) minecascade(mat,m,one,two) return m[array[0]][array[1]] def numbermines(m,r,c): count = 0 if (r > 0) , (c > 0) , (m[r-1][c-1] == '*'): count += 1 if (r > 0) , (m[r-1][c] == '*'): count += 1 if (r > 0) , (c < len(m[0])-1) , (m[r-1][c+1] == '*'): count += 1 if (c > 0) , (m[r][c-1] == '*'): count += 1 if (c < len(m[0])-1) , (m[r][c+1] == '*'): count += 1 if (r < len(m)-1) , (c > 0) , (m[r+1][c-1] == '*'): count += 1 if (r < len(m)-1) , (m[r+1][c] == '*'): count += 1 if (r < len(m)-1) , (c < len(m[0])-1) , (m[r+1][c+1] == '*'): count += 1 return count def processmatrix(mat): row = len(mat) col = len(mat[0]) r in range(row): c in range(col): if (mat[r][c] != '*'): new = numbermines(mat,r,c) mat[r][c] = new return mat def placemines(mat): in range(mines): mrow = random.randint(0,(rows)-1) mcol = random.randint(0,(cols)-1) if (mat[mrow][mcol] != '*'): if (mrow < rows) , (mcol < cols): mat[mrow][mcol] = '*' else: return mat return mat def printmatrix(m,mat): rows = len(m) cols = len(m[0]) in range(0,rows,1): 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 = [] in range(0,rows,1): mat += [buildrow(cols)] return mat def buildrow(cols): row = [] in range(0,cols,1): row += ['-'] return row def displayboard(m,mat): rows = len(m) cols = len(m[0]) in range(0,rows,1): 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 1 = minecascade(m,mat,a,b) file line 19, in minecascade 1 = minecascade(m,mat,a,b) file line 19, in minecascade 1 = minecascade(m,mat,a,b) file line 26, in minecascade 2 = minecascade(m,mat,d,c) file line 26, in minecascade 2 = minecascade(m,mat,d,c) file line 19, in minecascade 1 = minecascade(m,mat,a,b) file line 19, in minecascade 1 = minecascade(m,mat,a,b) file line 29, in minecascade if (r > 0) , (c < len(m[0]-1)): typeerror: unsupported operand type(s) -: 'list' , 'int'
in function minecascade, you've syntax error, around line 29, traceback suggests. change
if (r > 0) , (c < len(m[0]-1)):
to
if (r > 0) , (c < len(m[0])-1):
lots of parentheses unnecessary due operator precedence, , can simplified to:
if r > 0 , c < len(m[0]) - 1:
the redundant parentheses hid error you.
Comments
Post a Comment