0

我刚开始编程,我正在尝试创建一个名为“verifyNeighbor”的函数,该函数使用来自另一个名为“getNeighborLabels”的函数的列表,该函数返回 boardWidth*boardHeight 板上数字“me”周围的所有邻居列表(不包括底片和重复)。但是,这会返回一个可能包含无效邻居的邻居列表(例如,在 5 x 5 的棋盘上,me=0,neighbor=4 将在列表中,但不是邻居)。这就是“verifyNeighbors”函数的作用——为“我”周围的每个邻居返回真或假。关于如何从逻辑上接近/开始的想法?

4

4 回答 4

0

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

于 2013-02-20T03:26:03.087 回答
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)
于 2013-02-20T03:26:30.547 回答
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 编程太多了。使用 // 用于评论 >_<

Edit2:添加了 wgetNeighborLabels function that passes things throughverifyNeighbor before adding to the list.getNeighborLabels` 现在应该可以为您工作。

于 2013-02-19T23:25:39.440 回答
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))

请注意,这里的索引是从 0 开始的,所以2 x 5棋盘看起来像这样:

0 1 2 3 4
5 6 7 8 9

编辑:我犯了忘记对角线邻居的错误。从这里添加它们应该不会太难。我也更喜欢另一个答案。我将把它留作另一种方式。

于 2013-02-19T23:27:08.653 回答