Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 寻路错误:给定的坐标超出数组索引_Python_Arrays_Path Finding - Fatal编程技术网

Python 寻路错误:给定的坐标超出数组索引

Python 寻路错误:给定的坐标超出数组索引,python,arrays,path-finding,Python,Arrays,Path Finding,下面是我在设计之后编写的一个简单的寻路算法。这意味着获取一个点或“单位”的位置,并给定计算机位置,将计算X值和Y值的差值,并首先移动最短的差值(由于游戏,是一个自上而下的回合制游戏,单位需要排队进行攻击) 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

下面是我在设计之后编写的一个简单的寻路算法。这意味着获取一个点或“单位”的位置,并给定计算机位置,将计算X值和Y值的差值,并首先移动最短的差值(由于游戏,是一个自上而下的回合制游戏,单位需要排队进行攻击)

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表示空白,G表示目标,C表示计算机
boardArray[3][6]=“G”
boardArray[7][7]=“C”
def打印板():
对于boardArray中的r:
对于r中的c:
打印(c,end=“”)
打印()
印制板()
行=0
对于boardArray中的i:
列=0
对于i中的k:
如果k==“G”:
打印(行、列)
goalpos=[行,列]
如果k==“C”:
打印(行、列)
computerpos=[行,列]
列=列+1
行=行+1
def更新位置(currentx、currenty、valrow、valcolumn):
打印(当前X)
打印(当前)
boardArray[valrow][valcolumn]=0
boardArray[currentx][currenty]=“C”
valrow=currentx
valcolumn=当前值
印制板()
返回valrow,valcolumn
def findPath(outscoperow,outscopecolumn):
差分X=计算机位置[0]-目标位置[0]
差异=计算机位置[1]-目标位置[1]
CurrentCompX=计算机位置[0]
CurrentCompY=计算机位置[1]+1
时间X=差异X
暂时性=差异性
如果差值x<0:
差分x=差分x*(-1)
如果差值<0:
差分Y=差分Y*(-1)
寻路=真
寻路时:
如果差值x<差值y:
如果时间X>0:
CurrentCompX=CurrentCompX-1
outscoperow,outscopecolumn=更新位置(CurrentCompX,CurrentCompY,outscoperow,outscopecolumn)
差分x=差分x-1
elif时间X<0:
CurrentCompX=CurrentCompX+1
outscoperow,outscopecolumn=更新位置(CurrentCompX,CurrentCompY,outscoperow,outscopecolumn)
差分x=差分x-1
elif差异X>差异Y:
如果时间>0:
CurrentCompY=CurrentCompY-1
outscoperow,outscopecolumn=更新位置(CurrentCompX,CurrentCompY,outscoperow,outscopecolumn)
差分Y=差分Y-1
elif暂时<0:
CurrentCompY=CurrentCompY+1
outscoperow,outscopecolumn=更新位置(CurrentCompX,CurrentCompY,outscoperow,outscopecolumn)
差分Y=差分Y-1
如果差值x==0且差值y==0:
寻路=错误
findPath(行、列)
在代码运行时,它应该实时移动计算机位置,并在运行时打印出2D数组。然而,当更改位置时,解释器还将声明当前坐标在数组的索引(或列表)之外,因为Python很奇怪

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 
零零零零零零摄氏度
3 6
7 7
7.
7.
回溯(最近一次呼叫最后一次):
文件“C:/Users/Cameron/Desktop/Basic RPG/pathfindingTest.py”,第88行,在
findPath(行、列)
文件“C:/Users/Cameron/Desktop/Basic RPG/pathfindingTest.py”,第77行,在findPath中
outscoperow,outscopecolumn=更新位置(CurrentCompX,CurrentCompY,outscoperow,outscopecolumn)
文件“C:/Users/Cameron/Desktop/Basic RPG/pathfindingTest.py”,第44行,在updatePositions中
boardArray[valrow][valcolumn]=0
索引器:列表索引超出范围

如果有人能提供最有用的帮助。

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


您可能需要重新考虑生成
的逻辑,或者在接收它们时执行
-1
findPath
接收8和8作为参数,在python中,数组以0开头

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

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