Codefights、扫雷、Python、代码几乎能用

3
我正在完成Codefight的挑战:扫雷游戏。
游戏描述如下:
enter image description here

以下是我的代码:

def minesweeper(matrix):

    for x in range(len(matrix)):
        matrix[x].insert(0, "x")
        #matrix[x].insert(len(matrix)+2, "x")
    frame = ["x" for i in range(len(matrix[0]))]
    matrix.insert(0, frame)
    matrix.insert(len(matrix), frame)

    output_matrix = [[0 for j in range(len(matrix[0]))] for i in range(len(matrix))]

    for i in range(0,len(matrix[0])-1):
        for j in range(0,len(matrix)-1):
            if matrix[i][j] == True:
                output_matrix[i][j+1] += 1 # one right
                output_matrix[i+1][j] += 1 # one down
                output_matrix[i][j-1] += 1 # one left
                output_matrix[i-1][j] += 1 # one up
                output_matrix[i+1][j+1] += 1 # one down, one right
                output_matrix[i+1][j-1] += 1 # one down, one right
                output_matrix[i-1][j+1] += 1 # one up, one right
                output_matrix[i-1][j-1] +=1 # one up, one left

    output_matrix.pop(0)
    output_matrix.pop(len(output_matrix)-1)

    for y in range(len(output_matrix)):
        output_matrix[y].pop(0)
        #output_matrix[y].pop(len(output_matrix))
    return output_matrix

"x"所创建的边界是由codefight用户建议的,其目的是确保如果我的位置在矩阵的边缘,炸弹计数不会转移到另一侧。
这段代码在矩阵的最后一列放置炸弹时会出现问题,例如:

如果输入为:

[[False, False, True],
 [False, False, False],
 [False, False, False]]

输出结果为:
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]

有人能清楚地解释一下为什么会发生这种情况吗?
如果有人能提出更好的方法来完成这个任务,我将不胜感激。
先谢谢您了。


你在 len(matrix[0])-1 处停止以避免越界错误,但这样也会避免最后一列。你需要更细粒度的范围控制,以便在检查最后一列时仍然可以进行检查,但不会更新其右侧的任何内容。 - Silvio Mayolo
不,他没有停得太早,因为他在周围添加了一个x的边框... - OBu
我在这段代码中遇到了IndexError。看起来三边都添加了边框,但右边没有添加边框。 - Kenny Ostrom
如果我修复代码,正确添加和删除边框单元格,则该功能正常工作。您可以使用append来避免一些计算,同时pop不带参数。 - Kenny Ostrom
我在我的回答中举了一个例子,说明如何在不使用pop()的情况下去除边框。 - OBu
1个回答

1
我刚刚反转了你的逻辑:我遍历输出字段并从矩阵中添加值。请注意异常的使用(这是关于“x”的提示)。通过这种解决方案,您不必使用pop()来缩小结果。
import itertools 

def minesweeper(matrix):

    #create the output matrix first to preserve the size
    #underscored variables to prevent warnings about unused variables
    output_matrix = [[0 for _j in range(len(matrix[0]))] for _i in range(len(matrix))]

    #unchanged
    for x in range(len(matrix)):
        matrix[x].insert(0, "x")
        matrix[x].insert(len(matrix)+2, "x")
    frame = ["x" for i in range(len(matrix[0]))]
    matrix.insert(0, frame)
    matrix.insert(len(matrix), frame)

    #to the logics the other way round: count the bombs around the output fields.
    #neighyours defines the offsets of all neighouring fields 
    neighbours = [(-1, -1), (-1, 0), (-1, 1), 
                  ( 0, -1),          ( 0, 1), 
                  ( 1, -1), ( 1, 0), ( 1, 1)] 
    for i, j in itertools.product(range(len(output_matrix[0])), range(len(output_matrix))):
        #get all indices; you could use two for-loops instead of itertools.product...
        print(i, j) # just to see how it works... please remove for final version
        for offset_i, offset_j in neighbours:
            print("   ", offset_i, offset_j ) # just to see how it works... please remove for final version
            # the exceptions do the magic here: If you add an "x", a TypeError is raised.
            # So you don't do anythithing if this happens. Otherwise you'll add 0 or 1 (adding "True" adds 1, "False" adds 0)
            try:
                output_matrix[j][i] += matrix[j + offset_j + 1][i + offset_i + 1]
                print("result = ", output_matrix[j][i]) # just to see how it works... please remove for final version
            except TypeError:
                print("pass") # just to see how it works... please remove for final version
                pass

    return output_matrix

matrix = [[False, False, True], #renamed input variable since "input" is a function name...
 [False, False, False],
 [False, False, False]]

print(minesweeper(matrix))

总的来说,您的解决方案是有效的(如果取消注释#matrix[x].insert(len(matrix)+2, "x")这一行),但是您在pop()序列中犯了错误。您可以使用2D切片(参见相应的stackoverflow主题)并执行以下操作
output_matrix = [output_matrix[i][1:len(output_matrix)-1] for i in range(1, len(output_matrix)-1)]

使用 pop() 步骤的替代方法。


不行,因为[-1]是一个有效的索引(从右边开始计数)...;-) - OBu
@KennyOstrom 我认为这样做没有问题。它可以产生更易读的代码和更合理的流程,而不是每次都检查边界。它也更少容易出现错误。你对此有何不喜欢的地方? - Evan Weissburg
我添加了一条备注,指出只有pop()需要修复,并提供了2D切片作为替代方案... - OBu
为什么在 output_matrix[j][i] += matrix[j + offset_j + 1][i + offset_i + 1] 中有一个 +1? - TCB
由于矩阵周围的x。 - OBu
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接