在为棋盘游戏编码有效移动时列出索引超出范围

时间:2014-11-30 00:39:51

标签: python python-3.x

嘿大家我是新来的,我试图制作一个名为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)

那就是适用于错误的所有内容

1 个答案:

答案 0 :(得分:-1)

对于valid_move(row,col),您无法拥有所有if语句。 而不是这样做,请在最初的elif声明后使用if,并且不要忘记撰写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