title: Cascading reveal in Python Minesweeper
body: <p>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. </p>
  
  <pre><code>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] == '&amp;'):
          if (m[r][c] == '*'):
              return
          elif (m[r][c] == 0):
              if (r &gt; 0) and (c &gt; 0):
                  if (mat[r-1][c-1] != '&amp;'):
                      mat[r-1][c-1] = '&amp;'
                      a = r-1
                      b = c-1
                      one = mineCascade(m,mat,a,b)
              else:
                  return m[r-1][c-1]
              if (r &gt; 0):
                  if (mat[r-1][c] != '&amp;'):
                      mat[r-1][c] = '&amp;'
                      d = r-1
                      two = mineCascade(m,mat,d,c)
              else:
                  return m[r-1][c]
              if (r &gt; 0) and (c &lt; len(m[0]-1)):
                  if (mat[r-1][c+1] != '&amp;'):
                      mat[r-1][c+1] = '&amp;'
                      e  = r-1
                      f = c+1
                      three = mineCascade(m,mat,e,f)
              else:
                  return m[r-1][c+1]
              if (c &gt; 0):
                  if (mat[r][c-1] != '&amp;'):
                      mat[r][c-1] = '&amp;'
                      g = c-1
                      four = mineCascade(m,mat,r,g)
              else:
                  return m[r][c-1]
              if (c &lt; len(m[0])-1):
                  if (mat[r][c+1] != '&amp;'):
                      mat[r][c+1] = '&amp;'
                      h = c+1
                      five = mineCascade(m,mat,r,h)
              else:
                  return m[r][c+1]
              if (r &lt; len(m)-1) and (c &gt; 0):
                  if (mat[r+1][c-1] != '&amp;'):
                      mat[r+1][c-1] = '&amp;'
                      i = r+1
                      j = c-1
                      six = mineCascade(m,mat,i,j)
              else:
                  return m[r+1][c-1]
              if (r &lt; len(m)-1):
                  if (mat[r+1][c] != '&amp;'):
                      mat[r+1][c] = '&amp;'
                      k = r+1
                      seven = mineCascade(m,mat,k,c)
              else:
                  return m[r+1][c]
              if (r &lt; len(m)-1) and (c &lt; len(m[0])-1):
                  if (mat[r+1][c+1] != '&amp;'):
                      mat[r+1][c+1] = '&amp;'
                      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]] = '&amp;'
      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 &gt; 0) and (c &gt; 0) and (m[r-1][c-1] == '*'):
          count += 1
      if (r &gt; 0) and (m[r-1][c] == '*'):
          count += 1
      if (r &gt; 0) and (c &lt; len(m[0])-1) and (m[r-1][c+1] == '*'):
          count += 1
      if (c &gt; 0) and (m[r][c-1] == '*'):
          count += 1
      if (c &lt; len(m[0])-1) and (m[r][c+1] == '*'):
          count += 1
      if (r &lt; len(m)-1) and (c &gt; 0) and (m[r+1][c-1] == '*'):
          count += 1
      if (r &lt; len(m)-1) and (m[r+1][c] == '*'):
          count += 1
      if (r &lt; len(m)-1) and (c &lt; 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 &lt; rows) and (mcol &lt; 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] == '&amp;'):
                  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] == '&amp;'):
                  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 &lt;module&gt;
      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 &gt; 0) and (c &lt; len(m[0]-1)):
  TypeError: unsupported operand type(s) for -: 'list' and 'int'
  </code></pre>