2013-02-19 44 views
0

我刚开始编程,我试图做一个名为“verifyNeighbor”的函数,它使用来自另一个名为“getNeighborLabels”的函数的列表,该函数返回boardWidth板上数字“me”周围所有邻居的列表* boardHeight(不包括负面和重复)。但是,这会返回一个可能包含无效邻居的邻居列表(例如,在5乘5的板上,me = 0,邻居= 4将在列表中,但不是邻居)。这就是“verifyNeighbors”函数为“我”周围的每个邻居返回true或false的地方。关于如何在逻辑上接近/开始这个想法?如何从董事会获得有效的邻居?

+0

你的代码是缩进不佳,因此难以阅读。请确认我没有以任何方式更改代码的含义,因为python始终可以实现这一点。 – 2013-02-19 23:15:01

+0

你可以为每一行使用一个单独的列表,这将使得很多这些测试更容易。 – Aesthete 2013-02-19 23:15:18

回答

0

那么,你的代码格式不正确,我不能很好地理解它。但这里有一段代码应该可以做你想做的事情:

# Gives the x and y coordinates on the board 
def coords(point, boardWidth): 
    x = point % boardWidth; 
    y = point // boardWidth 
    return (x,y) 

#inverse transform from above 
def point(x, y, boardWidth): 
    return x + y * boardWidth 

def verifyNeighbor(boardWidth, boardHeight, neighbor, me): 
    mx,my = coords(me, boardWidth) 
    nx,ny = coords(neighbor, boardWidth) 
    # if the generated neightbor has coords that are 
    # off the grid, then it is invalid 
    # Since x is calculated modulo boardWidth it will never 
    # be less than 0 or greater than boardWidth so we only 
    # need to check y 
    if ny < 0 or ny >= boardHeight: 
     return False 

    dx = abs(mx-nx) 
    dy = abs(my-ny) 
    # Neighbors are points whose coordinates relative to me are: 
    # (-1,-1) top left 
    # (-1, 0) left 
    # (-1, 1) bottom left 
    # (0, -1) top 
    # (0, 1) bottom 
    # (1, -1) top right 
    # (1, 0) right 
    # (1, 1) bottom right 
    # Therefore the absolute values are one of the three options 
    # below otherwise it's not a neighbor 
    return (dx,dy) in [(0,1),(1,0), (1,1)] 

def getNeighborLabels(boardWidth, boardHeight, me): 
    mx,my = coords(me, boardWidth) 
    result = [] 
    for i in range(-1,2): 
     for j in range(-1,2): 
      if i == j == 0: 
       continue 
      p = point(mx+i, mx+j, boardWidth) 
      if verifyNeighbor(boardWidth, boardHeight, p, me): 
       result.append(point(mx+i,mx+j, boardWidth)) 
    return result 

编辑:最近用C语言编程太多了。使用// for comments> _ <

Edit2:增加了一个wgetNeighborLabels function that passes things through verifyNeighbor before adding to the list. getNeighborLabels`现在应该为你工作。

0

好的,这是我认为你需要的。经过测试,它适用于我。

def getNeighborLabels(boardWidth, boardHeight, me): 
    result = [] 

    # since there's no boardHeight, the one below me is always in the resultset 

    if(me > boardWidth * boardHeight - 1): 
     # throw exception here 
     print 'error!'   
     return null 

    if(me < boardWidth * (boardHeight - 1)):  
     result += [me + boardWidth] 
    if(me > boardWidth - 1): 
     result += [me - boardWidth] 

    # if you're in the first or last column, something special happens: 
    if(not(me % boardWidth == 0)): 
     result += [me - 1] 
    if(not(me % boardWidth == boardWidth - 1)): 
     result += [me + 1] 
    return(result) 

def verifyNeighbor(boardWidth, boardHeight, me, neighbor): 
    return(neighbor in getNeighborLabels(boardWidth, boardHeight, me)) 

注意的是,指数在这里从零开始,所以2 x 5板看起来是这样的:

0 1 2 3 4 
5 6 7 8 9 

编辑:我做了忘了对角线邻域的错误。他们不应该太难从这里添加。我也更喜欢其他答案。我会把它放在另一条路上。

0

对于给定的代码,当运行在python中由熵发布给出了错误,指出coord需要3个参数只有两个给出了mx,my = coords(me,boardWidth)。任何帮助?

0

下面是一个相当简洁的版本,假设你使用的是Python 2 - 如果情况并非如此,则需要更改一些小的内容。我还将其命名为功能getNeighborOffsets()而不是getNeighborLabels(),因为这就是它的功能。

def xy(boardWidth, offset): 
    """ x,y position from offset """ 
    y = offset // boardWidth 
    x = offset - y * boardWidth 
    return x, y 

def getNeighborOffsets(boardWidth, boardHeight, posn): 
    posnX, posnY = xy(boardWidth, posn) 
    return (y * boardWidth + x 
       for x in [posnX-1, posnX, posnX+1] 
        for y in [posnY-1, posnY, posnY+1] 
         if (x != posnX or y != posnY) and 
          0 <= x < boardWidth and 0 <= y < boardHeight) 

def verifyNeighbor(boardWidth, boardHeight, cell, posn): 
    # if both cell and posn are on board, test whether cell is adjacent to posn 
    maxOffset = (boardHeight-1) * boardHeight + (boardWidth-1) 
    return (0 <= posn <= maxOffset and 0 <= cell <= maxOffset and 
      any(cell == offset for offset in getNeighborOffsets(boardWidth, boardHeight, posn))) 

if __name__ == '__main__': 
    def offset(boardWidth, x, y): # for testing 
     """ offset from x, y position """ 
     return y * boardWidth + x 

    boardWidth, boardHeight = 8, 8 

    me = offset(boardWidth, 0, 4) 
    print sorted(getNeighborOffsets(boardWidth, boardHeight, me)) 

    posn1 = offset(boardWidth, 1, 5) 
    print verifyNeighbor(boardWidth, boardHeight, posn1, me) 

    posn2 = offset(boardWidth, 1, 6) 
    print verifyNeighbor(boardWidth, boardHeight, posn2, me)