簡體   English   中英

為什么我的矩陣出現索引錯誤?

[英]Why am I getting an out of index error on my matrix?

我目前正在嘗試為游戲編寫代碼,該游戲很像跳棋。 我將我的電路板表示為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 <7和x <= 8,但仍然無法正常工作。 在左側,它工作正常。 有誰知道為什么會這樣嗎? 這顯然是我的邏輯問題,但是我對如何解決它沒有想法。

您應該在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
         . . .

這樣,您就不會對超出范圍的索引執行檢查

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM