Why am I getting an out of index error on my matrix?
Question: I'm currently trying to make code for a game, the game is a lot like checkers.
I'm representing my board as a 10 x 10 matrix. There exist two ways of moving
in this game, you can do a step where you check all the positions adjacent to
you and if any of them is empty you can move into them. Then if the position
right next to you is occupied but the space right next to that space is empty
you can hop on top of the player occupying said spot. So far I have three
important pieces of code to finish my game. My first piece of code is the
board, the second piece of code is two variables each one contains a list of
the coordinates of each player's chips and the third part is two functions,
one that allows me to hop and the other one that allows me to do steps.

This is my code:

    
    
    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
    

The for loop is used to go through the list of all my chips and find all the
possible moves for all the chips. When I do this with the 1 chips which are at
the top left of the board it returns all the possible plays, which are 32 if
that's of any use. However when I run this same function with the 2 chips I
get the following error:

    
    
        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
    

So far I have managed to find out it only happens with hops and it only
happens when you're trying to hop to the right. I've tried changing it to x <
7 and x <=8 and it's still not working. On the left side it works perfectly
fine. Does anyone have any clue as to why this is happening? It's obviously a
logic problem on my side but I'm out of ideas as to how I can fix it.


Answer: You should print values of `x` and `y` in your `hopper` function. Since
`chips2` contains indices in the matrix with value 2, then the index `[9][9]`
will be contained in `chips2` implying `x = 9` or `y = 9` at some point.

Therefore `matrix[x+1][y+1]` would mean `matrix[10][10]` which does not exist.
You should review your code.

you should do:

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

In that way, you don't perform a check on an index that is out of range

