井字棋(免费版)

#登录界面
from tkinter import *
from tkinter.messagebox import *
 
 
# 设置主界面的容器大小和位置
root = Tk()
root.title('登录页面')
root.geometry("800x400+100+150")
 
#声明组件变量
log_name = StringVar()
log_pwd = StringVar()
 
#组件的使用
def btn_login():
    if log_name.get() == '井字棋' and log_pwd.get() == '123456':
        showinfo('提示信息','登录成功~')
    else:
        showwarning('提示信息','用户名或密码输入有误~')
 
#组件的使用
Label(root,text='账号:',font=('幼圆',20)).place(x=200,y=80)
Entry(width=27,font=('幼圆',20),textvariable=log_name).place(x=280,y=88)
Label(root,text='密码:',font=('幼圆',20)).place(x=200,y=150)
Entry(width=27,font=('幼圆',20),show='*',textvariable=log_pwd).place(x=280,y=150)
 
Button(text='登录',
       background='white',   # 背景色
       foreground='black',width=15,    # 前景色
       font=('幼圆',20),       #字体  大小
       command=btn_login).place(x=150,y=220)
Button(text='退出',
       background='white',     # 背景色
       foreground='black',      # 前景色
       width=15,font=('幼圆',20),     #字体  大小
       command=lambda:root.quit()).place(x=400,y=220)
 
# 显示图形界面
root.mainloop()
#井字棋:
from tkinter import *
import tkinter.messagebox as msg
 
root = Tk()
root.title('TIC-TAC-TOE---Project Gurukul')
# labels
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]
 
# 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("一号选手获胜")
            root.destroy()
        elif (win(panels, sign) and sign == 'O'):
            msg.showinfo("二号选手获胜")
            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 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("Result", "Match Tied")
        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()

   版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

注:本文为原创,该篇为免费版。该篇无付费版!如发现付费版,请告知作者!

账号:井字棋
密码:123456

相关推荐

  1. 免费

    2024-05-12 09:26:02       9 阅读
  2. 三子/(C语言)

    2024-05-12 09:26:02       23 阅读
  3. C++EasyX之

    2024-05-12 09:26:02       42 阅读
  4. 使用Python创建游戏

    2024-05-12 09:26:02       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-12 09:26:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-12 09:26:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-12 09:26:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-12 09:26:02       18 阅读

热门阅读

  1. 【socket】 linux C++ socket 优化参数

    2024-05-12 09:26:02       7 阅读
  2. Jtti:怎么检测香港服务器的响应速度?

    2024-05-12 09:26:02       11 阅读
  3. 服务器硬件命令查看

    2024-05-12 09:26:02       9 阅读
  4. k8s部署针对外部服务器的prometheus服务

    2024-05-12 09:26:02       9 阅读
  5. LeetCode 题目 118:杨辉三角

    2024-05-12 09:26:02       10 阅读
  6. C语言经典例题-7

    2024-05-12 09:26:02       9 阅读