简体   繁体   English

如何检查连接四中的对角线

[英]How to Check Diagonals in Connect Four

I've checked the rows and columns for a winner, but I have zero clue on how to solve for diagonals.我已经检查了获胜者的行和列,但我对如何求解对角线的线索为零。 Here's what I have for my winner determination function:以下是我的获胜者判定 function:

def winner(board):
    """This function accepts the Connect Four board as a parameter.
    If there is no winner, the function will return the empty string "".
    If the user has won, it will return 'X', and if the computer has
    won it will return 'O'."""
    for row in range(7):
        count = 0
        last = ''
        for col in range(7):
            row_win = board[row][col]
            if row_win == " ":
                count = 0
                continue
            if row_win == last:
                count += 1
            else:
                count = 1
            if count >= 4:
                return row_win
            last = row_win

    for col in range(7):
        count = 0
        last = ''
        for row in range(7):
            col_win = board[row][col]
            if col_win == " ":
                count = 0
                continue
            if col_win == last:
                count += 1
            else:
                count = 1
            if count >= 4:
                return col_win
            last = col_win

I can't think of a way to check diagonals for a win.我想不出一种方法来检查对角线是否获胜。 Here's the rest of the code if you need any references: https://paste.pythondiscord.com/epihuvujal.py Any tips would be greatly appreciated!如果您需要任何参考,这里是代码的 rest: https://paste.pythondiscord.com/epihuvujal.py任何提示将不胜感激!

here is a simple sample, you can try it and modify to suite your needs:这是一个简单的示例,您可以尝试并修改以适应您的需求:

def winner(board, length):
    """Returns the 'mark' of the winning player"""
    W = range(len(board))     # width
    H = range(len(board[0]))  # height
    directions = [(0, 1), (1, 0), (1, 1), (1, -1)]
    
    for dx, dy in directions:
        edges = []  
        
        if dx > 0:
            edges += [(0, y) for y in H]
            
        if dy > 0:   # scanning down
            edges += [(x, 0) for x in W]
            
        if dy < 0:   # scanning up
            edges += [(x, H[-1]) for x in W]
            
        for ex, ey in edges: 
            row = 0; mark = None
            x, y = ex, ey
            
            while x in W and y in H:
                if board[x][y] == mark:
                    row += 1
                else:
                    mark = board[x][y]
                    row = 1
                if mark is not None and row >= length:
                    return mark
                x, y = x + dx, y + dy
    return None

print(winner([
    ['X', 'O', 'O', 'O'],
    ['O', 'X', 'O', 'X'],
    ['O', 'O', 'X', 'O'],
    ['O', 'X', 'X', 'X'] ], 4))  # X

To check for the diagonals you need a nested loop.要检查对角线,您需要一个嵌套循环。

A for loop may do the work but a while loop also works. for 循环可以完成工作,但 while 循环也可以。

The for loop you need is not the one in your code but the one with i and j for iteration.您需要的 for 循环不是代码中的循环,而是带有 i 和 j 用于迭代的循环。

Notice that starting from a value board[i][j] there are 4 diagonals to check.请注意,从值 board[i][j] 开始,有 4 个对角线要检查。

The one in the top left, top right, bottom left and bottom right.左上角、右上角、左下角和右下角的那个。

You can access them by adding 1 or substracting it each time from i and j.您可以通过每次从 i 和 j 中添加 1 或减去它来访问它们。

Like this board[i+1][j+1] and board[i][j] and board[i-1][j-1] are in the same diagonal.像这个 board[i+1][j+1] 和 board[i][j] 和 board[i-1][j-1] 在同一个对角线上。

You can access as much as values you want ( if it's connect 3 or 4 or K )您可以访问尽可能多的值(如果它是 connect 3 或 4 或 K )

But be careful for not exceeding the board boundaries ( maybe you can use a try - catch).但要小心不要超出棋盘边界(也许你可以使用 try - catch)。

One more thing is don't be afraid if a diagonal is checked twice because your code will end up checking new diagonals.如果对角线被检查两次,不要害怕,因为您的代码最终会检查新的对角线。

You can also add a list of diags you visited before so it doesn't repeat the work.您还可以添加您之前访问过的诊断列表,这样它就不会重复工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM