Diagonal Wins没有被检测到Python

时间:2016-07-18 12:28:32

标签: python python-3.x pygame

我使用pygame在Python中创建了一个Connect 4游戏。我的董事会是:

board = []
for row in range(6):
    board.append([])
    for column in range(7):
        board[row].append(0) 

虽然当我获得对角线胜利时,它似乎没有注册。任何人都可以看到算法的问题来检测胜利者。如果是这样,你能告诉我要改变什么吗?

    # check / diagonal spaces
for x in range(7):
    for y in range(6):
        if board[x][y] == 1  and board[x+1][y-1] == 1 and board[x+2][y-2] == 1 and board[x+3][y-3] == 1:
            winner = "Red"
            redpoints = redpoints + 1 
            return True

# check \ diagonal spaces
for x in range(7):
    for y in range(6):
        if board[x][y] == 1 and board[x+1][y+1] == 1 and board[x+2][y+2] == 1 and board[x+3][y+3] == 1:
            winner = "Red"
            redpoints = redpoints + 1 
            return True

1 个答案:

答案 0 :(得分:1)

当你创建6行时,我假设这些是y位置,并且你追加到每行的7个元素是x位置。如果是这种情况,那么在对角线胜利检查的代码中的索引与填充板时的索引相反。我同意Rawing扭转指数。将x循环放在y循环中而不是反过来也可能更有意义,以保持与填充板的方式一致。