在扫雷游戏的方格中,在“b”周围放置数字1

4

我有一个类似这样的网格。我随机地将一个 "b" 放在网格中,并在字母 "b" 周围放置数字 1。这似乎在除了当一个 1 应该被放置在底行和整个右侧列时出现问题。例如,它看起来像这样:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 b
0 0 0 0 0 0 0 0 0 0

应该看起来像什么

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 1 b
0 0 0 0 0 0 0 0 1 1

这是我正在使用的代码,但我无法弄清楚为什么那些1没有被放置在那里。

from random import*
mat1 = []
mat2 = []

def makemat(x):
    for y in range(x):
        list1 = []
        list2 = []
        for z in range(x):
            list1.append(0)
            list2.append("-")
        mat1.append(list1)
        mat2.append(list2)
makemat(10)


def printmat(mat):
    for a in range(len(mat)):
        for b in range(len(mat)):
            print(str(mat[a][b]) + "\t",end="")
        print("\t")



def addmines(z):
    count = 0
    while (count < z):
        x = randrange(0,len(mat1))       
        y = randrange(0,len(mat1))      
        if mat1[y][x] == "b":
            count -= 1
        else:
            mat1[y][x] = "b"
        count += 1
addmines(1)


    

def addscores():
    for x in range(len(mat1)):
        for y in range(len(mat1)):
            if ((y < len(mat1)-1) and (x < len(mat1)-1)) and ((y >= 0) and (x >= 0))):
                if mat1[y+1][x] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x] == "b":
                    mat1[y][x] = 1
                if mat1[y][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y][x-1] == "b":
                    mat1[y][x] = 1
                if mat1[y+1][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y+1][x-1] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x-1] == "b":
                    mat1[y][x] = 1
    printmat(mat1)
addscores()

那个 -1 是干什么的?x < len(mat1)-1 - avishayp
1
OP正在忽略一个1个方块的边框,因为他正在检查x+1坐标处是否存在炸弹。 - inspectorG4dget
1
在你之前的问题中,出现了越界错误,因为当 x 达到 9 时,你的代码会测试 x + 1 的值,即 10,超出了边界。你在这里尝试解决的方法是永远不允许 x 达到 9。但是如果 x 永远不达到 9,那么你如何在第 9 列中放置一个 1 呢?你需要找到一个不同的解决方案来解决原始问题。 - senderle
3个回答

2

您的嵌套循环检查每个正方形,以确定它是否应该有1。然而,在addscores()中的第一个if子句中,您省略了每个位于正方形边缘上的正方形。解决这个问题的好方法是省略if子句,而是添加一个函数来检查一个自动检查边界的正方形。例如:

def checksqu(y, x):
    if y < 0 or y >= len(mat1) or x < 0 or x >= len(mat1):
        return False
    return mat1[y][x] == 'b'

那么,你可以使用if checksqu(y - 1, x - 1):(等等)代替if mat1[y - 1][x - 1]:


0
你可以简化这部分代码。
def addscores():
    for x in range(len(mat1)):
        for y in range(len(mat1)):
            if ((y < len(mat1)-1) and (x < len(mat1)-1)) and ((y >= 0) and (x >= 0))):
                if mat1[y+1][x] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x] == "b":
                    mat1[y][x] = 1
                if mat1[y][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y][x-1] == "b":
                    mat1[y][x] = 1
                if mat1[y+1][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y+1][x-1] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x-1] == "b":
                    mat1[y][x] = 1

通过使用这段代码:

def addscores():
    size = len(mat1)
    directions = [(dx, dy) for dx in [-1,0,1] for dy in [-1,0,1] if (dy!=0 or dx!=0)]
    for x in range(size):
        for y in range(size):
            for dx, dy in directions:
                try:
                    if mat1[y+dy][x+dx] == "b":
                        mat1[y][x] = 1
                except:
                    pass

1
在我的看法中,使用 itertools.productrange(-1,2) 上会比列表推导式更好。 - inspectorG4dget

0

这似乎可以解决问题:

def addscores(mat):
    for y in range(len(mat)):
        for x in range(len(mat[y])):
            if mat[y][x] == 'b':
                mat = pad(mat, x, y, '1')
    return mat

def pad(mat, x, y, n):
    for i, (x,y) in enumerate(itertools.product(range(x-1, x+2), range(y-1, y+2))):
        if i != 4: # the coordinate at index 4 is where the bomb is
            if 0<=y<len(mat) and 0<=x<len(mat[y]):
                mat[y][x] = n
    return mat

测试:

In [127]: mat
Out[127]: 
[['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '1', '1', '1'],
 ['0', '0', '0', '0', '0', '0', '0', '1', '0', '1']]

In [129]: addscores(mat)
Out[129]: 
[['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '1', '1'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '1', 'b'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '1', '1']]

这正是OP已经拥有并请求帮助修复的完全相同的错误输出。 - abarnert
@abarnert:抱歉,我粘贴了错误的输出。已经修复了。 - inspectorG4dget

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