Python Pygame TikTacToe玩家获胜问题如何解决?

Python Pygame TikTacToe玩家获胜问题如何解决?,python,pygame,Python,Pygame,我正在做一个练习,我对我的玩家像游戏一样获胜有一个问题,即使我没有连续获得3分,我只得到我的建议是在你的按钮()类中使用.result的状态 class button(): def __init__(self, color, x,y,width,height, text=''): # ... self.result = '' # empty def getResult( self ): return self.result

我正在做一个练习,我对我的玩家像游戏一样获胜有一个问题,即使我没有连续获得3分,我只得到我的建议是在你的
按钮()
类中使用
.result
的状态

class button():
    def __init__(self, color, x,y,width,height, text=''):
        # ...
        self.result = ''    # empty

    def getResult( self ):
        return self.result

    def setResult( self, value ):
        self.result = value

    def reset( self ):
        self.result = ''   # back to empty state
然后,当您创建
Parti
Parti
时,告诉按钮记住结果:

if score == 2:
    if greenbutton4.isOver(pos):
        greenbutton4.setResult( 'x' )
        partics.append(Partic(71,215,100,100,white))

#  ... 

if score == 3:
    if greenbutton4.isOver(pos):
        greenbutton4.setResult( 'o' )
        parts.append(Parti(71,215,100,100,white))
只有8种方式可以在tic tac toe/noughts和CROSS中获胜:3个水平、3个垂直和2个对角线。这是一个相当简单的检查:

def winner( b1, b2, b3, b4, b5, b6, b7, b8, b8 ):
    """ Given the buttons 1-9 in order from top-left to bottom right
        return whether 'x' or 'o' has won the game, or None and which
        line/row/vertical the win was made on """
    winner = ( None, None )
    # make a grid of the board-state for iteration
    board = [ [ b1.getResult(), b2.getResult(), b3.getResult() ],
              [ b4.getResult(), b5.getResult(), b6.getResult() ],
              [ b7.getResult(), b8.getResult(), b9.getResult() ] ]

    # EDIT: some debug code
    print( "BOARD IS: ")
    print( " %3s | %3s | %3s " % ( b1.getResult(), b2.getResult(), b3.getResult() ) )
    print( "-----+-----+-----" )
    print( " %3s | %3s | %3s " % ( b4.getResult(), b5.getResult(), b6.getResult() ) )
    print( "-----+-----+-----" )
    print( " %3s | %3s | %3s " % ( b7.getResult(), b8.getResult(), b9.getResult() ) )
    print( "" )


    # check the horizontals
    for row in range( 3 ):
        if ( board[row][0] != '' and
             board[row][0] == board[row][1] and board[row][0] == board[row][2] ):
            winner = ( board[row][0], 'h'+str( row ) )
            break
    # check the verticals
    for col in range( 3 ):
        if ( board[0][col] != '' and
             board[0][col] == board[1][col] and board[0][col] == board[2][col] ):
            winner = ( board[col][0], 'v'+str( col ) )
            break
    # diagonals
    if ( board[1][1] != '' ):
        if ( board[0][0] == board[1][1] and board[2][2] == board[1][1] ):
            winner = ( board[1][1], 'd1' )
        elif ( board[0][2] == board[1][1] and board[2][0] == board[1][1] ):
            winner = ( board[1][1], 'd2' )
    return winner
代码似乎在创建按钮时将
button2
放在中间(基于视频),因此对该函数的调用类似于:

player_wins, row = winner( greenbutton8,  greenbutton9,  greenbutton10,   
                           greenbutton4,  greenbutton2,  greenbutton3,  
                           greenbutton5,  greenbutton6,  greenbutton7 )
if ( player_wins != None ):
    print( "Player "+ player_wins + " has won on " + row )

对不起,但我认为你根本错了。[215,215,215]中的
partic.y这样的条件毫无意义。
partic.y
是215,或者不是。它与
partic.y==215
相同。我建议您调查一下您前面一个问题的答案:
部分与
partics
之间有什么不同?你能解释一下为什么算法在
turns()
函数中检查
score
。我不明白。它检查我的分数是否为2,我悬停在我的任何按钮上,然后玩家X应该去,如果我的分数为3,我的O在任何按钮上,那么玩家2应该去,我得到了一切工作,希望部分检查玩家是否赢了部件列表是玩家2,部件列表是玩家1我有两个不同的类来显示X和O的不同图像,这就是为什么我在那里使用不同的列表来查看您所使用的方法,但我正在尝试想出一种不同的方法来实现这一点,我尝试检查碰撞,如[partic.rect.collide rect(greenbutton2.rect)和partic.rect.collide rect(greenbutton3.rect)和partic.rect.collide rect(greenbutton4.rect)for partics in partics]:lines.append(liner(0,0,0,0,white))
但此方法不起作用,它只检查我是否单击其中一个,然后单击,然后我赢我得到此错误
player_wins,row=winner(greenbutton8,greenbutton9,greenbutton10,TypeError:\uu init_uuuuuuuuu()接受6个位置参数,但给出了10个
>>>>您提供的代码是否也适用于这两个参数,因为什么时候玩家X轮到它将成为玩家O轮到它检查玩家O轮到了吗?您是否粘贴了错误的内容?错误指的是
\uuuuu init\uuuuo()
不是
winner()
。这里有点问题。该函数不关心谁是赢家,它返回哪个符号填充赢家模式中的单元格。它可能是
o
x
(或胡萝卜和香蕉)。是的,我粘贴正确,但仍然会出现错误更新你的粘贴箱,我会快速查看
if score == 2:
    if greenbutton4.isOver(pos):
        greenbutton4.setResult( 'x' )
        partics.append(Partic(71,215,100,100,white))

#  ... 

if score == 3:
    if greenbutton4.isOver(pos):
        greenbutton4.setResult( 'o' )
        parts.append(Parti(71,215,100,100,white))
def winner( b1, b2, b3, b4, b5, b6, b7, b8, b8 ):
    """ Given the buttons 1-9 in order from top-left to bottom right
        return whether 'x' or 'o' has won the game, or None and which
        line/row/vertical the win was made on """
    winner = ( None, None )
    # make a grid of the board-state for iteration
    board = [ [ b1.getResult(), b2.getResult(), b3.getResult() ],
              [ b4.getResult(), b5.getResult(), b6.getResult() ],
              [ b7.getResult(), b8.getResult(), b9.getResult() ] ]

    # EDIT: some debug code
    print( "BOARD IS: ")
    print( " %3s | %3s | %3s " % ( b1.getResult(), b2.getResult(), b3.getResult() ) )
    print( "-----+-----+-----" )
    print( " %3s | %3s | %3s " % ( b4.getResult(), b5.getResult(), b6.getResult() ) )
    print( "-----+-----+-----" )
    print( " %3s | %3s | %3s " % ( b7.getResult(), b8.getResult(), b9.getResult() ) )
    print( "" )


    # check the horizontals
    for row in range( 3 ):
        if ( board[row][0] != '' and
             board[row][0] == board[row][1] and board[row][0] == board[row][2] ):
            winner = ( board[row][0], 'h'+str( row ) )
            break
    # check the verticals
    for col in range( 3 ):
        if ( board[0][col] != '' and
             board[0][col] == board[1][col] and board[0][col] == board[2][col] ):
            winner = ( board[col][0], 'v'+str( col ) )
            break
    # diagonals
    if ( board[1][1] != '' ):
        if ( board[0][0] == board[1][1] and board[2][2] == board[1][1] ):
            winner = ( board[1][1], 'd1' )
        elif ( board[0][2] == board[1][1] and board[2][0] == board[1][1] ):
            winner = ( board[1][1], 'd2' )
    return winner
player_wins, row = winner( greenbutton8,  greenbutton9,  greenbutton10,   
                           greenbutton4,  greenbutton2,  greenbutton3,  
                           greenbutton5,  greenbutton6,  greenbutton7 )
if ( player_wins != None ):
    print( "Player "+ player_wins + " has won on " + row )