簡體   English   中英

為棋盤游戲編碼有效動作時,列表索引超出范圍

[英]List index out of range when coding a valid move for board game

嘿,每個人都在這里,我正在嘗試制作一款名為HiQ的游戲,現在我已經畫好了畫板,所有內容都可以單擊其中一個,但是當我這樣做時,它的確改變了顏色,並且在外殼中出現了錯誤好吧(下面列出)我不確定為什么我得到了這個,我希望你們能給我更好的見解。 我也會在下面提供我的代碼,它是用python 3編碼的,謝謝

builtins.IndexError: list index out of range

boardcirc =[[0,0,0,1,1,1,0,0,0],
            [0,0,0,1,1,1,0,0,0],
            [0,0,0,1,1,1,0,0,0],
            [1,1,1,1,1,1,1,1,1],
            [1,1,1,1,2,1,1,1,1],
            [1,1,1,1,1,1,1,1,1],
            [0,0,0,1,1,1,0,0,0],
            [0,0,0,1,1,1,0,0,0],
            [0,0,0,1,1,1,0,0,0]]


def HiQ():
    splash_screen() 
    make_board()





def make_board():
    make_sqr()
    make_circ()
    get_click()



def get_click():
    global count, boardcirc
    while 1!=0:
        count = count - 1
        displaymessage("Pieces: " + str(count))        
        where = win.getMouse()
        col = where.x//90
        row = where.y//90
        valid_move(row,col)
        make_move(row,col)




def valid_move(row,col):
    if boardcirc[row][col] == 0:
        return False
    if boardcirc[row-1][col] == 1 and boardcirc[row-2][col] == 1:
        return True
    if boardcirc[row+1][col] == 1 and boardcirc[row+2][col] == 1:
        return True
    if boardcirc[row][col-1] == 1 and boardcirc[row][col-2] == 1:
        return True
    if boardcirc[row][col+1] == 1 and boardcirc[row][col+2] == 1:
        return True



def make_move(row,col):
    while valid_move(row,col) == True:

        col = (col*85)+42
        row = (row*85)+42
        circ = Circle(Point(col,row),35)
        circ.setFill("white")
        circ.draw(win)

多數民眾贊成在所有適用於該錯誤

對於您的valid_move(row,col) ,您不能擁有所有這些if語句。 不要在初始if語句之后使用elif ,而不要忘記編寫else語句

if boardcirc[row][col] == 0:
        return False
    if boardcirc[row-1][col] == 1 and boardcirc[row-2][col] == 1:
        return True
    elif boardcirc[row+1][col] == 1 and boardcirc[row+2][col] == 1:
        return True
    elif boardcirc[row][col-1] == 1 and boardcirc[row][col-2] == 1:
        return True
    elif boardcirc[row][col+1] == 1 and boardcirc[row][col+2] == 1:
        return True
else:
    return False

暫無
暫無

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

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