Pathfinding error: Coordinates given outside of array index

Pathfinding error: Coordinates given outside of array index

下面是我设计后编写的一个简单的寻路算法。意思是取一个点或'Unit'的位置,给定Computer位置,会计算X值和Y值的差值,先走最短的差值(由于游戏原因,是top基于回合的游戏,单位需要排队进行攻击)。

boardArray = [[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 shows an empty space, G is the goal, and C is the computer


boardArray[3][6] = "G"
boardArray[7][7] = "C"


def printBoard():
    for r in boardArray:
        for c in r:
            print(c,end = " ")
        print()


printBoard()

row = 0
for i in boardArray:
    column = 0
    for k in i:
        if k == "G":
            print(row, column)
            goalpos = [row, column]
        if k == "C":
            print(row, column)
            computerpos = [row, column]
        column = column + 1
    row = row + 1


def updatePositions(currentx, currenty, valrow, valcolumn):
    print(currentx)
    print(currenty)
    boardArray[valrow][valcolumn] = 0
    boardArray[currentx][currenty] = "C"
    valrow = currentx
    valcolumn = currenty
    printBoard()
    return valrow, valcolumn


def findPath(outscoperow, outscopecolumn):
    DifferenceX = computerpos[0] - goalpos[0]
    DifferenceY = computerpos[1] - goalpos[1]
    CurrentCompX = computerpos[0]
    CurrentCompY = computerpos[1] + 1
    TemporaryX = DifferenceX
    TemporaryY = DifferenceY
    if DifferenceX < 0:
        DifferenceX = DifferenceX *(-1)
    if DifferenceY < 0:
        DifferenceY = DifferenceY *(-1)
    pathfinding = True
    while pathfinding:
        if DifferenceX < DifferenceY:
            if TemporaryX > 0:
                CurrentCompX = CurrentCompX - 1
                outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
                DifferenceX = DifferenceX - 1
            elif TemporaryX < 0:
                CurrentCompX = CurrentCompX + 1
                outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
                DifferenceX = DifferenceX - 1
        elif DifferenceX > DifferenceY:
            if TemporaryY > 0:
                CurrentCompY = CurrentCompY - 1
                outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
                DifferenceY = DifferenceY - 1
            elif TemporaryY < 0:
                CurrentCompY = CurrentCompY + 1
                outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
                DifferenceY = DifferenceY - 1
        if DifferenceX == 0 and DifferenceY == 0:
            pathfinding = False



findPath(row, column)

随着代码的运行,它应该实时移动计算机位置,打印出二维数组,但是,当改变位置时,解释器也会声明当前坐标在外面数组的索引(或列表,因为 Python 很奇怪)。

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 G 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 C 
3 6
7 7
7
7
Traceback (most recent call last):
  File "C:/Users/Cameron/Desktop/Basic RPG/pathfindingTest.py", line 88, in <module>
    findPath(row, column)
  File "C:/Users/Cameron/Desktop/Basic RPG/pathfindingTest.py", line 77, in findPath
    outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
  File "C:/Users/Cameron/Desktop/Basic RPG/pathfindingTest.py", line 44, in updatePositions
    boardArray[valrow][valcolumn] = 0
IndexError: list index out of range

如果有人能伸出援手那将是最有用的。

findPath 接收 8 和 8 作为参数,在 python 中,数组以 0 开头。

您可能需要重新考虑生成 rowcolumn 的逻辑,或者只是在接收它们时执行 -1