IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python井字棋源代码 -> 正文阅读

[Python知识库]Python井字棋源代码

作者:recommend-item-box type_blog clearfix
我打赌大家在课堂上肯定玩过这个,想想当年和同桌玩这个废了好几本本子。程序运行截图:

在这里插入图片描述
完整程序代码

from tkinter import *
import tkinter.messagebox as msg

root = Tk()
root.title('井字棋-Python代码大全')
# 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("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 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()

更多Python源代码,请关注微信公众号:Python代码大全。
在这里插入图片描述

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-05-24 18:07:23  更:2022-05-24 18:09:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 13:24:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码