プログラミングを始めたばかりで、「getNeighborLabels」と呼ばれる別の関数のリストを使用する「verifyNeighbor」と呼ばれる関数を作成しようとしています。 (ネガと重複を除く)。ただし、これは、無効なネイバーを含む可能性のあるネイバー リストを返します (たとえば、5 x 5 のボードでは、me=0、neighbor=4 がリストに含まれますが、ネイバーではありません)。ここで「verifyNeighbors」関数の出番です。「me」の周囲の各隣人に対して true または false を返します。これを論理的にアプローチ/開始する方法についてのアイデアはありますか?
4 に答える
entropy によって投稿された python で実行された特定のコードに対して、coord には 3 つの引数が必要であるというエラーが表示され、mx,my = coords(me, boardWidth) には 2 つしか与えられません。
これは、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)
あなたのコードは適切にフォーマットされておらず、あまり意味がありません。しかし、ここにあなたが望むことをするべきコードがあります:
# 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 through
verifyNeighbor getNeighborLabels` が追加されましたbefore adding to the list.
。
わかりました、これが必要だと思います。それをテストしました、そしてそれは私のために働きます。
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
編集:斜めの隣人を忘れるという間違いを犯しました。ここから追加するのは難しくありません。私は他の答えも好きです。これは別の方法として残しておきます。