python Tic-Tac-Toe game

Teacher told us to write a little game called Tic-Tac-Toe by GUI in python,after a night of checking the information and coding,I have made the programme finally.

from tkinter import *
import tkinter.messagebox as msg

root = Tk()
root.title("井字棋")

Label(root, text="player1:X", font="times 15").grid(row=0, column=1)
Label(root, text="player2:O", font="times 15").grid(row=0, column=2)

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
mark = ''
count = 0

panels = ["panel"] * 10


def win(panels, sign):
    return ((panels[1] == panels[2] == panels[3] == sign)
            or (panels[1] == panels[4] == panels[7] == sign)
            or (panels[1] == panels[5] == panels[9] == sign)
            or (panels[2] == panels[5] == panels[8] == sign)
            or (panels[3] == panels[6] == panels[9] == sign)
            or (panels[3] == panels[5] == panels[7] == sign)
            or (panels[4] == panels[5] == panels[6] == sign)
            or (panels[7] == panels[8] == panels[9] == sign))


def checker(digit):
    global count, mark, digits

    if digit == 1 and digit in digits:
        digits.remove(digit)
        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button1.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "player2 wins")
            root.destroy()

    if digit == 2 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button2.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "player2 wins")
            root.destroy()

    if digit == 3 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button3.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "player2 wins")
            root.destroy()

    if digit == 4 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button4.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "player2 wins")
            root.destroy()

    if digit == 5 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button5.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "player2 wins")
            root.destroy()

    if digit == 6 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button6.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "player2 wins")
            root.destroy()

    if digit == 7 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button7.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "player2 wins")
            root.destroy()

    if digit == 8 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button8.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "player2 wins")
            root.destroy()

    if digit == 9 and digit in digits:
        digits.remove(digit)

        if count % 2 == 0:
            mark = 'X'
            panels[digit] = mark
        elif count % 2 != 0:
            mark = 'O'
            panels[digit] = mark

        button9.config(text=mark)
        count = count + 1
        sign = mark

        if (win(panels, sign) and sign == 'X'):
            msg.showinfo("Result", "player1 wins")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("Result", "player2 wins")
            root.destroy()

    if (count > 8 and win(panels, 'X') == False and win(panels, '0') == False):
        msg.showinfo("Result", "Match tied")
        root.destroy()


button1 = Button(root, width=15, font=("Times 16 bold"), height=7, command=lambda: checker(1))
button1.grid(row=1, column=1)
button2 = Button(root, width=15, height=7, font=("Times 16 bold"), command=lambda: checker(2))
button2.grid(row=1, column=2)
button3 = Button(root, width=15, height=7, font=("Times 16 bold"), command=lambda: checker(3))
button3.grid(row=1, column=3)
button4 = Button(root, width=15, height=7, font=("Times 16 bold"), command=lambda: checker(4))
button4.grid(row=2, column=1)
button5 = Button(root, width=15, height=7, font=("Times 16 bold"), command=lambda: checker(5))
button5.grid(row=2, column=2)
button6 = Button(root, width=15, height=7, font=("Times 16 bold"), command=lambda: checker(6))
button6.grid(row=2, column=3)
button7 = Button(root, width=15, height=7, font=("Times 16 bold"), command=lambda: checker(7))
button7.grid(row=3, column=1)
button8 = Button(root, width=15, height=7, font=("Times 16 bold"), command=lambda: checker(8))
button8.grid(row=3, column=2)
button9 = Button(root, width=15, height=7, font=("Times 16 bold"), command=lambda: checker(9))
button9.grid(row=3, column=3)

root.mainloop()

maybe there are some bugs in it,I'll deal with it in two days

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值