如何改进Python中Tic-Tac-Toe-Check的代码

如何改进Python中Tic-Tac-Toe-Check的代码,python,tic-tac-toe,Python,Tic Tac Toe,我正在编写一个python代码来制作一个tic-tac-toe游戏。我需要写一个函数,它接受三个输入,board,x和y。线路板是线路板的当前显示,然后x和y是0、1或2的值。游戏设置为询问用户坐标 def CheckVictory(board, x, y): #check if previous move was on vertical line and caused a win if board[0][y] == ('X') and board[1][y] == ('X')

我正在编写一个python代码来制作一个tic-tac-toe游戏。我需要写一个函数,它接受三个输入,board,x和y。线路板是线路板的当前显示,然后x和y是0、1或2的值。游戏设置为询问用户坐标

def CheckVictory(board, x, y):

    #check if previous move was on vertical line and caused a win
    if board[0][y] == ('X') and board[1][y] == ('X') and board [2][y] == ('X'):
        return True
    if board[0][y] == ('O') and board[1][y] == ('O') and board [2][y] == ('O'):
        return True

    #check if previous move was on horizontal line and caused a win
    if board[x][0] == ('X') and board[x][1] == ('X') and board [x][2] == ('X'):
        return True
    if board[x][0] == ('O') and board[x][1] == ('O') and board [x][2] == ('O'):
        return True

    #check if previous move was on the main diagonal and caused a win
    if board[0][0] == ('X') and board[1][1] == ('X') and board [2][2] == ('X'):
        return True
    if board[0][0] == ('O') and board[1][1] == ('O') and board [2][2] == ('O'):
        return True
    #check if previous move was on the secondary diagonal and caused a win
    if board[0][2] == ('X') and board[1][1] == ('X') and board [2][0] == ('X'):
        return True
    if board[0][2] == ('O') and board[1][1] == ('O') and board [2][0] == ('O'):
        return True

    return False 
#end of CheckVictory function
该函数在游戏循环中被调用,如下所示

p_x, p_y = playerTurn(board)    #let player take turn
displayBoard(board)             #show board after move has been made
if CheckVictory(board, p_x, p_y):   #see if user has won
    print("CONGRATULATIONS, you win!")
    newGame(board)  #game over start new one
    continue
对于电脑来说也是如此


我觉得有更好的方法来编写这个函数。我觉得我应该更多地使用x和y,或者有更好的方法来检查,而不是写下所有的可能性。那么,写这篇文章的更好方法是什么?简明扼要。

我不明白为什么需要
x
y
参数,应该检查一行中是否有三个x字母或三个O字母,不需要坐标。 而是先编辑棋盘,使其更新玩家输入的坐标,然后检查胜利是否发生

我会这样做,但如果你想使用你的方法,请随意。你仍然可以从我的版本中学到一些东西

def check_victory(board):
    combinations = [
        # horizontal
        ((0,0), (1,0), (2,0)),
        ((0,1), (1,1), (2,1)),
        ((0,2), (1,2), (2,2)),
        # vertical
        ((0,0), (0,1), (0,2)),
        ((1,0), (1,1), (1,2)),
        ((2,0), (2,1), (2,2)),
        # crossed
        ((0,0), (1,1), (2,2)),
        ((2,0), (1,1), (0,2))
    ]

    for coordinates in combinations:
        letters = [board[y][x] for x,y in coordinates]
        if len(set(letters)) == 1:
            return letters[0] # returns corresponding letter for winner (X/O)

    return False

请注意,它使用列表理解和集合。如果您不熟悉这些参数,我建议在使用此解决方案之前先学习它们。

我不明白为什么需要
x
y
参数,您应该检查一行中是否有三个x字母或三个O字母,您不需要坐标。 而是先编辑棋盘,使其更新玩家输入的坐标,然后检查胜利是否发生

我会这样做,但如果你想使用你的方法,请随意。你仍然可以从我的版本中学到一些东西

def check_victory(board):
    combinations = [
        # horizontal
        ((0,0), (1,0), (2,0)),
        ((0,1), (1,1), (2,1)),
        ((0,2), (1,2), (2,2)),
        # vertical
        ((0,0), (0,1), (0,2)),
        ((1,0), (1,1), (1,2)),
        ((2,0), (2,1), (2,2)),
        # crossed
        ((0,0), (1,1), (2,2)),
        ((2,0), (1,1), (0,2))
    ]

    for coordinates in combinations:
        letters = [board[y][x] for x,y in coordinates]
        if len(set(letters)) == 1:
            return letters[0] # returns corresponding letter for winner (X/O)

    return False

请注意,它使用列表理解和集合。如果您不熟悉这些,我建议您在使用此解决方案之前先了解它们。

如果您的代码有效,那么codereview.stackexchange.com可能是一个更好的询问方式。(虽然我会先检查关于Python tic tac toe实现的现有问题)它可以工作,但是我后面的函数中包含此函数时将无法正常工作,因为我认为我编写此函数的能力很差。1。函数不应该知道最后一步。由于您可以访问
线路板
,您可以评估当前状态,以了解on party是否获胜。2.您可以使用for循环检查一行中是否有3个水平或垂直循环。现在只需添加对角线的检查。3.将要检查的对象作为参数(X或O)传递@cah:StackOverflow更适合于有客观、明确答案的问题。如何“更好地编写函数”是主观的。你想快点吗?更易于维护?不那么内聚?这些目标可能相互冲突。我建议您遵循Wooble的建议,将此内容带到codereview,codereview正是针对这类问题而设计的。如果您的代码有效,那么codereview.stackexchange.com可能是一个更好的提问场所。(虽然我会先检查关于Python tic tac toe实现的现有问题)它可以工作,但是我后面的函数中包含此函数时将无法正常工作,因为我认为我编写此函数的能力很差。1。函数不应该知道最后一步。由于您可以访问
线路板
,您可以评估当前状态,以了解on party是否获胜。2.您可以使用for循环检查一行中是否有3个水平或垂直循环。现在只需添加对角线的检查。3.将要检查的对象作为参数(X或O)传递@cah:StackOverflow更适合于有客观、明确答案的问题。如何“更好地编写函数”是主观的。你想快点吗?更易于维护?不那么内聚?这些目标可能相互冲突。我建议您遵循Wooble的建议,将此带到codereview,codereview正是针对这类问题设计的。