使用python的简单扫雷。我的邻近功能

时间:2014-11-17 18:26:58

标签: python function dictionary

我正在努力改进我的代码,以便在python上的一个扫雷“游戏”中找到近处的地雷。 它有效,但我想知道是否还有改进它仍在使用词典。 我个人认为代码可能更好,但不能真正看到如何更好地做到这一点。 谢谢:D

grid = {}
grid[(0, 7)] = "Mine"
grid[(1, 5)] = "Mine"
grid[(1, 6)] = "Mine"
grid[(1, 8)] = "Mine"
grid[(2, 4)] = "Mine"
grid[(3, 4)] = "Mine"
grid[(5, 5)] = "Mine"
grid[(7, 0)] = "Mine"
grid[(7, 5)] = "Mine"


def mine_in_proximity(position, grid):
    death = False
    count = 0
    i, j = position[0], position[1]
    for n in range(i-1, i+2):
        for m in range(j-1, j+2):
            if (n, m) != (i, j):
                mine = grid.get((n, m), 0)
                if mine == "Mine":
                    count += 1
                elif mine == 0:
                    grid[position] = count
            else:
                deadly = grid.get((n, m), 0)
                if deadly == "Mine":
                    death = True
    return count, grid, death

0 个答案:

没有答案