Python在网格中将“1”放在“b”附近

时间:2012-11-17 00:09:52

标签: python grid placement minesweeper

我有一个看起来像这样的网格。我在网格中随机放置一个“b”并将数字1环绕在字母“b”上。这似乎无处不在,除非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()

3 个答案:

答案 0 :(得分:2)

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

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 mat1[y - 1][x - 1]:,你可以做if checksqu(y - 1, x - 1):(等等)。

答案 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

答案 2 :(得分: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']]