Function 如何从板中获取有效的邻居?

Function 如何从板中获取有效的邻居?,function,Function,我刚开始编程,我正在尝试创建一个名为“verifyNeighbor”的函数,该函数使用另一个名为“getNeighborLabels”的函数的列表,该函数返回boardWidth*boardHeight(不包括负片和副本)板上数字“me”周围所有邻居的列表。但是,这将返回一个邻居列表,该列表可能包含无效的邻居(例如,在5乘5的电路板上,me=0,邻居=4将在列表中,但不是邻居)。这就是“verifyneights”函数的作用——为“我”周围的每个邻居返回true或false。关于如何以逻辑方式处

我刚开始编程,我正在尝试创建一个名为“verifyNeighbor”的函数,该函数使用另一个名为“getNeighborLabels”的函数的列表,该函数返回boardWidth*boardHeight(不包括负片和副本)板上数字“me”周围所有邻居的列表。但是,这将返回一个邻居列表,该列表可能包含无效的邻居(例如,在5乘5的电路板上,me=0,邻居=4将在列表中,但不是邻居)。这就是“verifyneights”函数的作用——为“我”周围的每个邻居返回true或false。关于如何以逻辑方式处理/启动此问题的想法

嗯,你的代码格式不正确,我不太明白。但这里有一段代码可以满足您的要求:

# 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
#给出电路板上的x和y坐标
def坐标(点、板宽):
x=点百分比板宽;
y=点//板宽
返回(x,y)
#从上到下的逆变换
def点(x、y、板宽):
返回x+y*板宽
def verifyNeighbor(板宽、板高、邻居、me):
mx,my=coords(me,boardWidth)
nx,ny=坐标(相邻,板宽)
#如果生成的neightbor具有
#脱离网格,则无效
#因为x是按boardWidth计算的,所以它永远不会
#小于0或大于boardWidth,因此我们仅
#我需要检查一下y
如果ny<0或ny>=板高:
返回错误
dx=abs(mx-nx)
dy=abs(我的纽约)
#相邻点是相对于我的坐标为:
#(-1,-1)左上角
#(-1,0)左
#(-1,1)左下角
#(0,-1)顶部
#(0,1)底部
#(1,-1)右上角
#(1,0)对
#(1,1)右下角
#因此,绝对值是三个选项之一
#否则它就不是邻居了
在[(0,1)、(1,0)、(1,1)中返回(dx,dy)]
def getNeighborLabels(板宽、板高、me):
mx,my=coords(me,boardWidth)
结果=[]
对于范围(-1,2)内的i:
对于范围(-1,2)内的j:
如果i==j==0:
持续
p=点(mx+i、mx+j、板宽)
如果验证邻居(板宽、板高、p、me):
结果.追加(点(mx+i、mx+j、boardWidth))
返回结果

编辑:最近用C语言编程太多了。用于//评论>\up>好的,这是我认为您需要的。经过测试,它对我有效

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))

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

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

这里有一个相当简洁的版本,它是在假设您使用python 2的情况下编写的——如果不是这样,则需要更改一些小的内容。我还将函数命名为
getneighborofsets()
,而不是
getNeighborLabels()
,因为它就是这样做的

def xy(板宽、偏移):
“从偏移量开始的x、y位置”
y=偏移量//板宽
x=偏移量-y*板宽
返回x,y
def GetNeighborofset(板宽、板高、位置):
posnX,posnY=xy(板宽,posn)
返回(y*boardWidth+x
对于[posnX-1,posnX,posnX+1]中的x
对于[posnY-1,posnY,posnY+1]中的y
如果(x!=posnX或y!=posnY)和

0您的代码缩进不良,因此很难阅读。请验证我没有以任何方式更改代码的含义,因为python总是可以这样做。您可以为每行使用单独的列表,这将使许多测试更容易。
0 1 2 3 4
5 6 7 8 9
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)