在Python Minesweeper中层叠显示

时间:2014-04-20 03:37:20

标签: python recursion minesweeper

我正在处理一个递归函数,以显示零周围任何空格的内容。我收到类型错误" TypeError:不支持的操作数类型 - :' list'和' int'"为我的代码。我希望这个代码(当给定一个位置时)显示它周围的任何空间,直到有一个数字,这些空间不会被挖掘。

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'

1 个答案:

答案 0 :(得分:1)

在函数 mineCascade 中,您可能会出现语法错误,可能是第29行,如回溯所示。变化

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

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

由于operator precedence,很多括号都是不必要的,可以简化为:

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

多余的括号可能是你隐藏错误的原因。