井字游戏无法识别获胜者

时间:2019-04-30 17:52:24

标签: python-3.x tkinter

尽管定义了代码,但无法告诉别人何时赢得游戏

我尝试使用if代替elif,但这只会导致大量错误。

我也尝试过缩进ttt(buttons),但它导致屏幕上没有显示任何按钮


bclick = True

# Creates the "start" button and imports the tic-tac-toe frame
def start():

    tk=Tk()
    tk.title("Tic Tac Toe")

    global bclick
    bclick = True


    def ttt(buttons):

# Handles left clicks and forces to appear a circle after an X

         global bclick
         if buttons["text"] == " " and bclick == True:
             buttons["text"] = "X"
             bclick = False
         elif buttons["text"] == " " and bclick == False:
              buttons["text"] = "O"
              bclick = True

# Defines all the squares as 1-9 so we can know when someone wins
# Tells if "X" won the game

         elif(button1['text'] == 'X' and button2['text'] == 'X' and button3['text'] == 'X'):
              tkinter.messagebox.showinfo("Player X","Winner is X !!!")
         elif(button4['text'] == 'X' and button5['text'] == 'X' and button6['text'] == 'X'):
              tkinter.messagebox.showinfo("Player X","Winner is X !!!")                                                                                                                       #789
         elif(button7['text'] =='X' and button8['text'] == 'X' and button9['text'] == 'X'):
              tkinter.messagebox.showinfo("Player X","Winner is X !!!")
         elif(button1['text'] == 'X' and button5['text'] == 'X' and button9['text'] == 'X'):
              tkinter.messagebox.showinfo("Player X","Winner is X !!!")
         elif(button3['text'] == 'X' and button5['text'] == 'X' and button7['text'] == 'X'):
              tkinter.messagebox.showinfo("Player X","Winner is X !!!")
         elif(button1['text'] == 'X' and button2['text'] == 'X' and button3['text'] == 'X'):
              tkinter.messagebox.showinfo("Player X","Winner is X !!!")
         elif(button1['text'] == 'X' and button4['text'] == 'X' and button7['text'] == 'X'):
              tkinter.messagebox.showinfo("Player X","Winner is X !!!")
         elif(button2['text'] == 'X' and button5['text'] == 'X' and button8['text'] == 'X'):
              tkinter.messagebox.showinfo("Player X","Winner is X !!!")
         elif(button7['text'] == 'X' and button6['text'] == 'X' and button9['text'] == 'X'):
              tkinter.messagebox.showinfo("Player X",'Winner is X !!!')

# Tells if "O" won the game
         elif(button1['text'] == 'O' and button2['text'] == 'O' and button3['text'] == 'O'or
              button4['text'] == 'O' and button5['text'] == 'O' and button6['text'] == 'O'or
              button7['text'] == 'O' and button8['text'] == 'O' and button9['text'] == 'O'or
              button1['text'] == 'O' and button5['text'] == 'O' and button9['text'] == 'O'or
              button3['text'] == 'O' and button5['text'] == 'O' and button7['text'] == 'O'or
              button1['text'] == 'O' and button2['text'] == 'O' and button3['text'] == 'O'or
              button1['text'] == 'O' and button4['text'] == 'O' and button7['text'] == 'O'or
              button2['text'] == 'O' and button5['text'] == 'O' and button8['text'] == 'O'or
              button7['text'] == 'O' and button6['text'] == 'O' and button9['text'] == 'O'):
              tkinter.messagebox.showinfo("Player O",'Winner is O !!!!')


    tk.mainloop()

frame.start()

1 个答案:

答案 0 :(得分:0)

如果使用列表保留按钮,则可以获得较短的代码,并且可以创建功能来测试行或列

import tkinter as tk

def check_row(y, char):
    return ((buttons[0][y]['text'] == char)
        and (buttons[1][y]['text'] == char)
        and (buttons[2][y]['text'] == char))

def check_col(x, char):
    return ((buttons[x][0]['text'] == char)
        and (buttons[x][1]['text'] == char)
        and (buttons[x][2]['text'] == char))

def check_diagonals(char):
    return (((buttons[0][0]['text'] == char)
         and (buttons[1][1]['text'] == char)
         and (buttons[2][2]['text'] == char))
           or 
            ((buttons[0][2]['text'] == char)
         and (buttons[1][1]['text'] == char)
         and (buttons[2][0]['text'] == char)))

def check_all(char):
    for i in range(3):
        if check_row(i, char):
            print('row', i)
            return True
        if check_col(i, char):        
            print('col', i)
            return True

    if check_diagonals(char):
        print('diagonals')
        return True

    return False

def count_empty():
    result = 0
    for x in range(3):
        for y in range(3):
            if buttons[x][y]['text'] == '':
                result += 1
    return result

def on_button_click(x, y):
    global player
    global gameover

    if not gameover:
        if buttons[x][y]['text'] == '':
            buttons[x][y]['text'] = player
            gameover = check_all(player)

            if gameover:
                print('win:', player)
            elif count_empty() == 0:
                gameover = True
                print('win: nobody')
            else:
                if player == 'X':
                    player = 'O'
                else:
                    player = 'X'

# --- main ---

player = 'X'
gameover = False
buttons = []

root = tk.Tk()

# create board
for x in range(3):
    row = []
    for y in range(3):
        b = tk.Button(root, text='', command=lambda bx=x, by=y: on_button_click(bx, by))
        b.grid(row=y, column=x)
        row.append(b)
    buttons.append(row)

root.mainloop()