Python 为什么我的矩阵会出现索引外错误?

Python 为什么我的矩阵会出现索引外错误?,python,numpy,matrix,Python,Numpy,Matrix,我目前正在尝试为一个游戏编写代码,这个游戏很像跳棋。我将我的董事会表示为10 x 10矩阵。在这个游戏中有两种移动方式,你可以做一个步骤,检查你旁边的所有位置,如果其中任何一个位置是空的,你可以移动到其中。然后,如果你旁边的位置有人占据,但该位置旁边的空间是空的,你可以跳到占据该位置的玩家的头顶上。到目前为止,我有三个重要的代码来完成我的游戏。我的第一段代码是棋盘,第二段代码是两个变量,每个变量包含每个玩家芯片的坐标列表,第三部分是两个函数,一个允许我跳跃,另一个允许我执行步骤 这是我的代码:

我目前正在尝试为一个游戏编写代码,这个游戏很像跳棋。我将我的董事会表示为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
将numpy导入为np
矩阵=np.数组([[1,1,1,1,0,0,0,0,0],#0这是电路板,它由10x10矩阵表示
[1,1,1,1,0,0,0,0,0],#1 0表示空格
[1,1,1,0,0,0,0,0,0],#2 1代表玩家1的筹码
[1,1,0,0,0,0,0,0,0],#3 2代表玩家2的筹码
[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‘‘‘)’)#查找包含1的所有位置
chips2=list(tuple(map)(tuple,np.fliplr(np.argwhere(matrix==2‘‘‘)’)#查找包含2的所有位置
打印芯片2
定义步骤(x,y):#为检查器查找可能的步骤
listMoves=[]
如果x>0且矩阵[x-1][y]==0:#左
listMoves.append([x-1,y])
如果x<9且矩阵[x+1][y]==0:#右
追加([x+1,y])
如果y<9:#向上
如果矩阵[x][y+1]==0:
追加([x,y+1])
如果x>0且矩阵[x-1][y+1]==0:#向上+向左
追加([x-1,y+1])
如果x<9且矩阵[x+1][y+1]==0:#向上+向右
追加([x+1,y+1])
如果y>0:#向下
如果矩阵[x][y-1]==0:
listMoves.append([x,y-1])
如果x>0且矩阵[x-1][y-1]==0:#向下+向左
listMoves.append([x-1,y-1])
如果x 1和矩阵[x-1][y]!=0和矩阵[x-2][y]==0:#左
追加([x-2,y])
如果x<8且矩阵[x+1][y]!=0和矩阵[x+2][y]==0:#右
追加([x+2,y])
如果y>1:
如果矩阵[x][y-1]!=0和矩阵[x][y-2]==0:#向下
追加([x,y-2])
如果x>1且矩阵[x-1][y-1]!=0和矩阵[x-2][y-2]==0:#向下+向左
追加([x-2,y-2])
如果x<8且矩阵[x+1][y+1]!=0和矩阵[x+2][y-2]==0:#向上+向右
追加([x+2,y-2])
如果y<8:
如果矩阵[x][y+1]!=0和矩阵[x][y+2]==0:#向上
追加([x,y+2])
如果x>1且矩阵[x-1][y+1]!=0和矩阵[x-2][y+2]==0:#向上+向左
追加([x-2,y+2])
如果x<8且矩阵[x+1][y+1]!=0和矩阵[x+2][y+2]==0:#向上+向右
追加([x+2,y+2])
listHops=listHops+步长(x,y)
返回列表
'''
def flipBoard():
全局矩阵
矩阵=np.fliplr(np.flipud(矩阵))
返回矩阵.tolist()
flipBoard()
'''
对于范围内的i(0,len(chips2)):#在位置列表中循环查找所有可能的步骤和跳数
打印料斗(芯片2[i][0],芯片2[i][1])#板上的每个棋盘格
打印矩阵
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
回溯(最近一次呼叫最后一次):
文件“”,第80行,在
打印料斗(芯片2[i][0],芯片2[i][1])#板上的每个棋盘格
文件“”,第59行,在料斗中
如果x<8且矩阵[x+1][y+1]!=0和矩阵[x+2][y-2]==0:#向上+向右
索引器:索引10超出大小为10的轴0的界限

到目前为止,我已经设法发现它只发生在跳跃时,而且只发生在你试图向右跳跃时。我已尝试将其更改为x<7和x您应该在
hopper
函数中打印
x
y
的值。由于
chips2
在矩阵中包含值为2的索引,因此索引
[9][9]
将包含在
chips2
中,这意味着
x=9
y=9

因此
矩阵[x+1][y+1]
意味着
矩阵[10][10]
不存在。你应该检查你的代码

你应该做:

if x < 8:
    if matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #up + right
         . . .
如果x<8:
如果矩阵[x+1][y+1]!=0和矩阵[x+2][y-2]==0:#向上+向右
. . .
这样,如果x<8且矩阵[x+1][y+1]!=0和矩阵[x+2][y-2]==0:#向上+向右
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
#^打字错误-这是向下的,不是向上的
显示完整的回溯。同时打印所示代码中的内容刚刚编辑了问题以添加完整的回溯。你让我打印l是什么意思?我是说打印x或y,在它失败之前看看它是什么。它可能会给你一个线索,但下面的答案可能对你来说是正确的。我试着用步骤来运行它,结果很好。是跳跃功能造成了问题。我用芯片2位置列表中的每个位置手动尝试。然而,当我用啤酒花做这件事时,我遇到了问题,在15块芯片中只有4块出现了问题。4个索引
(x,y)=(8,8),(8,9)(9,8)(9,9)