【问题标题】:Why am I getting an out of index error on my matrix?为什么我的矩阵出现索引不足错误?
【发布时间】:2016-05-27 23:14:14
【问题描述】:

我目前正在尝试为游戏编写代码,该游戏很像跳棋。我将我的电路板表示为 10 x 10 矩阵。这个游戏有两种移动方式,你可以做一个步骤,检查你附近的所有位置,如果其中任何一个位置是空的,你就可以进入它们。然后,如果您旁边的位置被占用,但该位置旁边的空间是空的,您可以跳到占据所述位置的玩家的顶部。到目前为止,我已经完成了我的游戏的三段重要代码。我的第一段代码是棋盘,第二段代码是两个变量,每个变量包含每个玩家筹码的坐标列表,第三部分是两个函数,一个允许我跳,另一个允许我跳做步骤。

这是我的代码:

import numpy as np

matrix = np.array([[1,1,1,1,1,0,0,0,0,0],  #0 this is the board, it's represented by a 10x10 matrix
                   [1,1,1,1,0,0,0,0,0,0],  #1 the 0s represent empty spaces
                   [1,1,1,0,0,0,0,0,0,0],  #2 the 1s represent player 1's chips
                   [1,1,0,0,0,0,0,0,0,0],  #3 the 2s represetn player 2's chips
                   [1,0,0,0,0,0,0,0,0,0],  #4 
                   [0,0,0,0,0,0,0,0,0,2],  #5
                   [0,0,0,0,0,0,0,0,2,2],  #6
                   [0,0,0,0,0,0,0,2,2,2],  #7
                   [0,0,0,0,0,0,2,2,2,2],  #8
                   [0,0,0,0,0,2,2,2,2,2]]) #9

chips1 = list(tuple(map(tuple,np.fliplr(np.argwhere(matrix == 1))))) #Finds all the positions that contain a 1
chips2 = list(tuple(map(tuple,np.fliplr(np.argwhere(matrix == 2))))) #Finds all the positions that contain a 2

print chips2

def step(x,y): #Finds the possible steps for a checker
    listMoves = []
    if x > 0 and matrix[x-1][y] == 0: #left
        listMoves.append([x-1,y])
    if x < 9 and matrix[x+1][y] == 0: #right
        listMoves.append([x+1,y])
    if y < 9: #up
        if matrix[x][y+1] == 0:
            listMoves.append([x,y+1])
        if x>0 and matrix[x-1][y+1] == 0: #up + left
            listMoves.append([x-1,y+1])
        if x < 9 and matrix[x+1][y+1] == 0: #up + right
            listMoves.append([x+1,y+1])
    if y > 0: #down
        if matrix[x][y-1] == 0:
            listMoves.append([x,y-1])
        if x > 0 and matrix[x-1][y-1] == 0: #down + left
            listMoves.append([x-1,y-1])
        if x<9 and matrix[x+1][y-1] == 0:
            listMoves.append([x+1,y-1])
    return listMoves

def hopper(x,y): #Finds the possible hops for a checker
    listHops = []
    if x > 1 and matrix[x-1][y] != 0 and matrix[x-2][y] == 0: #left
        listHops.append([x-2,y])
    if x < 8 and matrix[x+1][y] != 0 and matrix[x+2][y] == 0: #right
        listHops.append([x+2,y])
    if y > 1:
        if matrix[x][y-1] != 0 and matrix[x][y-2] == 0: #down
            listHops.append([x,y-2])
        if x>1 and matrix[x-1][y-1] != 0 and matrix[x-2][y-2] == 0: #down + left
            listHops.append([x-2,y-2])
        if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #up + right
            listHops.append([x+2,y-2])
    if y < 8:
        if matrix[x][y+1] != 0 and matrix[x][y+2] == 0: #up
            listHops.append([x,y+2])
        if x > 1 and matrix[x-1][y+1] != 0 and matrix[x-2][y+2] == 0: #up + left
            listHops.append([x-2,y+2])
        if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y+2] == 0: #up + right
            listHops.append([x+2,y+2])
    listHops = listHops + step(x,y)
    return listHops
'''
def flipBoard():
    global matrix 
    matrix = np.fliplr(np.flipud(matrix))
    return matrix.tolist() 
flipBoard()
'''
for i in range(0,len(chips2)): #loops through the list of positions to find all the possible steps and hops for 
    print hopper(chips2[i][0], chips2[i][1])#every single checker on the board
print matrix

for 循环用于遍历我所有筹码的列表并找到所有筹码的所有可能移动。当我用棋盘左上角的 1 个筹码执行此操作时,它会返回所有可能的玩法,如果有任何用处,则为 32。但是,当我使用 2 个芯片运行相同的功能时,出现以下错误:

    Traceback (most recent call last):

  File "<ipython-input-21-3429014a2777>", line 80, in <module>
    print hopper(chips2[i][0], chips2[i][1])#every single checker on the board

  File "<ipython-input-21-3429014a2777>", line 59, in hopper
    if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #up + right

IndexError: index 10 is out of bounds for axis 0 with size 10

到目前为止,我已经设法发现它仅发生在跳跃时,并且仅在您尝试向右跳跃时发生。我尝试将其更改为 x

【问题讨论】:

  • 显示完整的回溯。还打印我在指定代码中的内容
  • 刚刚编辑了问题以添加完整的回溯。你让我打印 l 是什么意思?
  • 我的意思是打印 x 或 y,在失败之前查看它是什么。它可能会给你一个线索,但下面的答案可能适合你

标签: python numpy matrix


【解决方案1】:

您应该在hopper 函数中打印xy 的值。由于chips2 包含矩阵中值为2 的索引,那么索引[9][9] 将包含在chips2 中,暗示x = 9y = 9 在某些时候。

因此matrix[x+1][y+1] 表示matrix[10][10] 不存在。您应该检查您的代码。

你应该这样做:

if x < 8:
    if matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #up + right
         . . .

这样,您就不会检查超出范围的索引

【讨论】:

  • 我尝试使用步骤运行它,它可以工作。导致问题的是啤酒花功能。我在我的chips2位置列表中的每个位置手动尝试了它。然而,当我用啤酒花做时,我遇到了问题,它只发生在 15 个筹码中的 4 个。
  • (x, y) = (8,8), (8,9) (9,8) (9,9) 的 4 个索引将在 matrix[x+1][y]matrix[x+2][y] 上失败。请参阅我关于如何测试矩阵中的项目的更新
  • 我这样运行它: step(chips2[8][0],chips2[8][1]) 它确实返回值。我希望我没有误解你或者你误解了我,但如果我是正确的,你正在尝试修复我的阶跃函数,当我真正需要修复的是跳跃函数时,这并没有给我带来任何问题.如果我错了,请纠正我?也感谢您花时间帮助我。
  • 对不起,我看错了。我仍然认为在right 上级联您的if 条件会有所帮助。查看我的新更新。
  • 刚试了一下,我一直遇到同样的问题。 :(
【解决方案2】:
if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #up + right
#                         ^ typo - that's down, not up

【讨论】:

  • 感谢您的回答。不仅符号是错字,而且评论也是错字,共同点应该是#up + down。感谢您的帮助。
猜你喜欢
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多