2012-11-17 25 views
4

我有一个看起来像这样的网格。我在网格中随机放置一个“b”,并把数字1围绕字母“b”。这似乎在任何地方都可以工作,除非1应该放在最下面一行和一列到最右边。例如,它会是这个样子Python在网格中放置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 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的的arent被放置在那里。

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() 
+0

那是什么'-1'做什么呢? 'X avishayp

+1

的OP无视1平方米的边界,因为他正在检查炸弹的存在'X + 1'协调 – inspectorG4dget

+1

在你前面的问题,你都拿到了界失误,因为当'x'到达'9'时,你的代码在'x + 1'处测试了值'10',这是超出界限的。你在这里的解决方案的尝试是绝不允许'x'到达'9'。但是如果'x'永远达不到'9',你会如何在'9'列中放置'1'?您需要为原始问题找到不同的解决方案。 – senderle

回答

2

您的嵌套循环检查每个方块,看看它是否应该有一个1。但是,在addscores()的第一个if子句中,您忽略了位于正方形边缘上的每个正方形。解决这个问题的好方法是省略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):(和等等)。

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

在范围(-1,2)上使用'itertools.product'比列表理解IMO – 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']] 
+0

更好。这与OP已有的错误输出完全相同,并且需要修复帮助。 – abarnert

+0

@abarnert:对不起,我粘贴了错误的输出。它已被修复 – inspectorG4dget