井字棋 小游戏

电脑时间 发表于 2022-9-21 20:50:18
本帖最后由 电脑时间 于 2022-9-27 21:24 编辑

改进版已推出,前往
使用PyCharm制作
使用Pyinstaller打包
属性:

玩家1胜利:

平局:(玩家2就不举了,跟玩家一的一模一样)

源代码:
  1. from tkinter import *
    import tkinter.messagebox as msg

    root = Tk()
    root.title('井字棋-By 萝卜头IT论坛 电脑时间')
    # labels
    Label(root, text="玩家1 : X", font="times 15").grid(row=0, column=1)
    Label(root, text="玩家2 : O", font="times 15").grid(row=0, column=2)

    digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]

    # for player1 sign = X and for player2 sign= Y
    mark = ''

    # counting the no. of click
    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

    # Check which button clicked

    if digit == 1 and digit in digits:
    digits.remove(digit)
    ##player1 will play if the value of count is even and for odd player2 will play
    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("提示", "玩家1 胜利")
    root.destroy()
    elif (win(panels, sign) and sign == 'O'):
    msg.showinfo("提示", "玩家2 胜利")
    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("提示", "玩家1 胜利")
    root.destroy()
    elif (win(panels, sign) and sign == 'O'):
    msg.showinfo("提示", "玩家2 胜利")
    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("提示", "玩家1 胜利")
    root.destroy()
    elif (win(panels, sign) and sign == 'O'):
    msg.showinfo("提示", "玩家2 胜利")
    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("提示", "玩家1 胜利")
    root.destroy()
    elif (win(panels, sign) and sign == 'O'):
    msg.showinfo("提示", "玩家2 胜利")
    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("提示", "玩家1 胜利")
    root.destroy()
    elif (win(panels, sign) and sign == 'O'):
    msg.showinfo("提示", "玩家2 胜利")
    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("提示", "玩家1 胜利")
    root.destroy()
    elif (win(panels, sign) and sign == 'O'):
    msg.showinfo("提示", "玩家2 胜利")
    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("提示", "玩家1 胜利")
    root.destroy()
    elif (win(panels, sign) and sign == 'O'):
    msg.showinfo("提示", "玩家2 胜利")
    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("提示", "玩家1 胜利")
    root.destroy()
    elif (win(panels, sign) and sign == 'O'):
    msg.showinfo("提示", "玩家2 胜利")
    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("提示", "玩家1 胜利")
    root.destroy()
    elif (win(panels, sign) and sign == 'O'):
    msg.showinfo("提示", "玩家2 胜利")
    root.destroy()

    ###if count is greater then 8 then the match has been tied
    if (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False):
    msg.showinfo("提示", "平局")
    root.destroy()


    ####define buttons
    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()

这个代码敲了我两个小时 不知道挨了男女混合双打多少次
井字棋-By 萝卜头IT论坛 电脑时间.py
传不了EXE

Dong 发表于 2022-9-21 23:31:10
很厉害了 我都不会没那个脑子 hhh
爱电脑的昕宇 发表于 2022-9-22 06:55:03
这个真的好厉害,你是自学的吗?
虽然我的水平无法完全读懂你的代码,但有一个小建议:按钮可以用place方法打包,或者可以加一句root.resizable(0, 0),可以避免出现这种情况

电脑时间 发表于 2022-9-22 06:58:45
爱电脑的昕宇 发表于 2022-9-22 06:55
这个真的好厉害,你是自学的吗?
虽然我的水平无法完全读懂你的代码,但有一个小建议:按钮可以用place方法 ...

已经是我的最高水平了
龍在天涯 发表于 2022-9-22 08:45:28
咱们论坛的小朋友都是敲代码高手,佩服
jyssysz 发表于 2022-9-22 19:43:54
小朋友诚会玩,加油!!
来源: 萝卜头IT论坛 查看完整版本