连接四个检查获胜功能仅适用于某些情况

时间:2017-04-21 02:45:58

标签: python python-3.x

我检查连接四赢的功能仅适用于第一行,但不适用于任何其他行。在我的代码的某处,x和y也混淆了,是我的循环吗?

def check_win(it):
    #right diag - works
    for x in range (0, 6):
        for y in range (0, 5):
            if board[x][y] == it and board[x-1][y+1] == it and board[x-2][y+2] == it and board[x-3][y+3] == it:
                return True
    #left diag- works
    for x in range (0, 6):
        for y in range (0, 5):
            if board[x][y] == it and board[x-1][y-1] == it and board[x-2][y-2] == it and board[x-3][y-3] == it:
                return True
    #right- works
    for y in range(0, 6):
        if board[x][y] == it and board[x][y+1] == it and board[x][y+2] == it and board[x][y+3] == it:
            return True
    #down - works
    for y in range(0, 6):
         if board[x][y] == it and board[x-1][y] == it and board[x-2][y] == it and board[x-3][y] == it:
             return True
    return False

例如,此处代码可以正常工作,但仅限于第一行

    .  .  .  .  .  .  .
    .  .  .  .  .  .  .
    R  .  .  .  .  .  .
    R  .  .  .  .  .  .
    R  .  .  .  .  .  .
    B  B  B  B  .  .  .
    1  2  3  4  5  6  7
    Game Over!
    player 1  wins!

当第一行上方的任何一行有四连接时,如下所示,它不起作用

    .  .  .  .  .  .  .
    B  .  .  .  .  .  .
    B  .  .  .  .  .  .
    B  .  .  .  .  .  .
    R  R  R  R  .  .  .
    B  R  B  R  B  .  .
    1  2  3  4  5  6  7

0 个答案:

没有答案