gpt4 book ai didi

python - Pygame TikTacToe Player 获胜问题如何解决?

转载 作者:行者123 更新时间:2023-12-04 08:32:30 27 4
gpt4 key购买 nike

我正在制作一个 tiktactoe,我的玩家像游戏一样获胜,即使我没有连续获得 3 个,我也只是获得了 VIDEO << 我没有连续获得 3 个事件,它只是为玩家 2 赢得了比赛
我的按钮位置

                   
white = (250,250,250)
greenbutton2 = button((0,255,0),190,215,100,100, '2')

greenbutton3 = button((0,255,0),335,215,100,100, '3')

greenbutton4 = button((0,255,0),71,215,100,100, '4')

greenbutton5 = button((0,255,0),71,350,100,100, '5')

greenbutton6 = button((0,255,0),190,350,100,100, '6')

greenbutton7 = button((0,255,0),335,350,100,100, '7')


greenbutton8 = button((0,255,0),70,90,100,100, '8')

greenbutton9 = button((0,255,0),190,90,100,100, '9')

greenbutton10 = button((0,255,0),335,90,100,100, '10')

greenbutton4 = button((0,255,0),71,215,100,100, '4')

例如,我为第一个所做的是 215,215,215 如果我的部分超过那些它应该闪烁获胜图像但它会闪烁另一个图像 IMAGE但它不是 blitting 正确的图像,而是它 blitting 这个 VIDEO << 对于所有人来说都是一样的,它会不断地传输错误的图像,有时即使我没有为 X 玩家或 O 敌人连续获得 3 个,它也会说我赢了有没有办法解决这个问题?
这是给我的 X 播放器的
    # player 2 winning for the rows part
count = sum([1 if partic.y in [215, 215, 215] else 0 for partic in partics])
if count == 3:
lines.append(line(0,0,0,0,white))



count = sum([1 if partic.y in [90, 90, 90] else 0 for partic in partics])
if count == 3:
lines.append(lin(0,0,0,0,white))


count = sum([1 if partic.y in [350, 350, 350] else 0 for partic in partics])
if count == 3:
lines.append(liner(0,0,0,0,white))



count = sum([1 if partic.x in [335, 335, 335] else 0 for partic in partics])
if count == 3:
lines.append(low(0,0,0,0,white))


count = sum([1 if partic.x in [190, 190, 190] else 0 for partic in partics])
if count == 3:
lines.append(lowe(0,0,0,0,white))


count = sum([1 if partic.x in [71, 71, 70] else 0 for partic in partics])
if count == 3:
lines.append(lower(0,0,0,0,white))


count = sum([1 if partic.y in [90, 215, 350] else 0 for partic in partics])
if count == 3:
lines.append(win(0,0,0,0,white))

count = sum([1 if partic.x in [335, 71, 190] else 0 for partic in partics])
if count == 3:
lines.append(winner(0,0,0,0,white))




这是给我的 O 播放器的
    # player 1 winning for the rows part
count = sum([1 if parti.y in [215, 215, 215] else 0 for parti in parts])
if count == 3:
lines.append(line(0,0,0,0,white))



count = sum([1 if parti.y in [90, 90, 90] else 0 for parti in parts])
if count == 3:
lines.append(lin(0,0,0,0,white))


count = sum([1 if parti.y in [350, 350, 350] else 0 for parti in parts])
if count == 3:
lines.append(liner(0,0,0,0,white))



count = sum([1 if parti.x in [335, 335, 335] else 0 for parti in parts])
if count == 3:
lines.append(low(0,0,0,0,white))


count = sum([1 if parti.x in [190, 190, 190] else 0 for parti in parts])
if count == 3:
lines.append(lowe(0,0,0,0,white))


count = sum([1 if parti.x in [71, 71, 70] else 0 for parti in parts])
if count == 3:
lines.append(lower(0,0,0,0,white))


count = sum([1 if parti.y in [90, 215, 350] else 0 for parti in parts])
if count == 3:
lines.append(win(0,0,0,0,white))

count = sum([1 if parti.x in [335, 71, 190] else 0 for parti in parts])
if count == 3:
lines.append(winner(0,0,0,0,white))

我的完整代码
pastebin

最佳答案

我的建议是简单地使用 .result 的状态在您的 button()类(class)。

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
然后当你创建一个 PartiPartic ,告诉按钮记住结果:
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 种方法可以在井字棋/零点游戏中获胜:3x 水平、3x 垂直和 2x 对角线。这是一个相当简单的检查:
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 )

关于python - Pygame TikTacToe Player 获胜问题如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64959745/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com