python实现简单的井字棋游戏(gui界面)

项目输出

项目先决条件

要使用python构建井字游戏,我们需要tkinter模块和python的基本概念

Tkinter模块是用于渲染图形的标准图形用户界面。

Tkinter.messagebox用于显示消息框

要安装tkinter模块,我们在命令提示符下使用了pip install命令:

pip install tkinter

项目文件结构

这些是使用python构建井字游戏的步骤:

  • 导入模块
  • 初始化窗口
  • 检查结果的功能
  • 检查获胜者的功能
  • 定义标签和按钮

1.导入模块

from tkinter import *
import tkinter.messagebox as msg

在此步骤中,我们导入tkinter和messsagebox模块

2.初始化窗口

root= Tk()
root.title('TIC-TAC-TOE---DataFlair')

digits = [1,2,3,4,5,6,7,8,9]
mark = '' “
count = 0
panels = ["panel"]*10
  • Tk()用于初始化窗口
  • title()用于设置窗口的标题

3.检查结果的功能

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))

在此功能中,将通过检查哪个玩家连续打出三个标记(上,下,对角或对角线)来检查结果。

4.检查获胜者的功能

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]=mar
  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
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,'O')==False):
  msg.showinfo("Result","Match Tied")
  root.destroy()

玩家总共有9次点击以玩游戏。玩家每次单击时,如果count的值大于8,则通过将count的值增加1来减少机会,则游戏结果为平局

  • 如果count的值为偶数,则玩家1将玩,否则玩家2将玩。
  • config()用于用适当的文本标记按钮
  • messagebox小部件中的showinfo()方法用于显示一些相关信息
  • destroy()停止mainloop退出程序

5.定义标签和按钮

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)

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()

Label()小部件,用于显示用户无法修改的文本。
Button()小部件显示按钮

  • root是我们引用的窗口的名称
  • 文本存储我们在标签上显示的值
  • 文字所使用的字体
  • 单击按钮时将调用命令
  • lambda()函数用于将特定数据发送到回调函数。

要运行程序时,将执行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("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 井字棋游戏的资料请关注我们其它相关文章!

(0)

相关推荐

  • python实现井字棋小游戏

    本文为大家分享了python实现井字棋小游戏,供大家参考,具体内容如下 周五晚上上了python的选修课,本来以为老师是从python的基础语法开始的,没想到是从turtle画图开始,正好补上了我以前一些不懂的地方,有人讲一下还是比啃书好一点. 之前从图书馆借了一本python游戏编程,看了前面几章后就没怎么看了,晚上突然想看看,然后跟着教程写个游戏的.最后就有了这个井字棋的诞生,其实代码并不是很长,主要是思路,需要考虑的周全一点.代码写完后就和电脑下了好久的井字棋,一局都没赢,真的是很无奈了,

  • python入门之井字棋小游戏

    引言: 刚学python好几天了,从java到python,基础学起来确实比较容易,语法掌握,基本概念上都比较容易入脑. 唯一比较郁闷的是老想着用java的语法去学python代码,这点还需要后面慢慢掌握吧,相信学多种语言的你们也有这种经历吧. start:开始上代码了,希望有更好的逻辑思维来写,自己也是用最笨拙的思路去写的,如果有可以优化的代码请各位大神指教 #!/user/bin/python # -*- coding: utf-8 -*- import os import sys #棋盘模

  • python实现井字棋游戏

    本文实例介绍了python实现井字棋游戏的方法,分享给大家,具体内容如下 windows7下python3.4.0编译运行通过.由于采用了cmd调用,所以与Linux不兼容,无法在Linux下运行. 游戏就是井字棋,小键盘上的数字位置对应棋盘位置. #本游戏python3.4.0下编写调试,只能在windows下运行. import random import subprocess import time #定义函数 def draw_board(the_board): subprocess.c

  • python实现简单井字棋小游戏

    用python实现的一个井字棋游戏,供大家参考,具体内容如下 #Tic-Tac-Toe 井字棋游戏 #全局常量 X="X" O="O" EMPTY=" " #询问是否继续 def ask_yes_no(question): response=None; while response not in("y","n"): response=input(question).lower() return respon

  • python实现简单井字棋游戏

    井字棋,英文名叫Tic-Tac-Toe,是一种在3*3格子上进行的连珠游戏,和五子棋类似,由于棋盘一般不画边框,格线排成井字故得名.游戏需要的工具仅为纸和笔,然后由分别代表O和X的两个游戏者轮流在格子里留下标记(一般来说先手者为X),任意三个标记形成一条直线,则为获胜. 游戏的难点在于,如何判断连接成了一条线:横.竖.斜三个方向: 游戏的代码: #!/usr/bin/env python3 # -*- coding:utf-8 -*- u''' Created on 2019年4月13日 @au

  • Python实现的井字棋(Tic Tac Toe)游戏示例

    本文实例讲述了Python实现的井字棋(Tic Tac Toe)游戏.分享给大家供大家参考,具体如下: 说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选择人人.人机.机人.机机四种对战模式之一 电脑玩家的AI使用了minimax算法,带apha-beta剪枝 电脑玩家在思考时,时时刻刻都有一个"假想敌".以便使得minimax算法运转起

  • 基于python纯函数实现井字棋游戏

    1.定义全局变 '''全局变量: X 和 O 表示两方的棋子; EMPTY 表示棋位为空: TIE 表示平局: NUM_SQUARES 表示有 9 个棋位 ''' X = "X" O = "O" EMPTY = " " TIE = "TIE" NUM_SQUARES = 9 2.定义调用到的函数 def ask_yes_no(question): '''问一个是或否的问题,用 y 或 n 回答.''' response = N

  • python实现简单的井字棋游戏(gui界面)

    项目输出 项目先决条件 要使用python构建井字游戏,我们需要tkinter模块和python的基本概念 Tkinter模块是用于渲染图形的标准图形用户界面. Tkinter.messagebox用于显示消息框 要安装tkinter模块,我们在命令提示符下使用了pip install命令: pip install tkinter 项目文件结构 这些是使用python构建井字游戏的步骤: 导入模块 初始化窗口 检查结果的功能 检查获胜者的功能 定义标签和按钮 1.导入模块 from tkinte

  • python实现简单的井字棋

    本文实例为大家分享了python实现简单的井字棋的具体代码,供大家参考,具体内容如下 使用python实现井字棋游戏,没有具体算法,只是用随机下棋简单实现: import random board = [['+','+','+'],['+','+','+'],['+','+','+']] def ma(board): if isempty(board): a = random.randint(0, 2) b = random.randint(0, 2) if board[a][b] != 'X'

  • C语言实现简单的井字棋游戏

    本文实例为大家分享了C语言实现简单井字棋游戏的具体代码,供大家参考,具体内容如下 1.什么是井字棋 井字棋相信大部分人都玩过 规则:双方轮流放子,当某一方的三个子连成一线(行,列,对角)时,该方获胜. 2.游戏前的准备 1. 菜单 游戏正式开始前我们可以为用户提供一个选择菜单,笔者做了一个最简单的游戏开始菜单: 代码: /*菜单*/ int menu() { printf("###########################\n"); printf("## 1.Play

  • python实现简单的井字棋小游戏

    Python做三子棋游戏,这个是我刚开始了解做Python小游戏的时候第一个项目,因为简单好入手,实现它的过程是我开始摸索Python的GUI界面的入门之路.这个设计也都是按照自己对于这个游戏的理解,一步一步去实现它. 窗口 万能的窗口,实现窗口都可以进行简单的修改进行使用: from tkinter import * root = Tk()         #窗口名称 root.title("憨憨制作的三子棋") f1=Frame(root) f1.pack() w1 = Canva

  • C语言实现简易井字棋游戏

    井子棋承载了每个人孩童时的美好时光,小到书本.纸张,大到课桌.墙壁,总能找到井字棋盘的痕迹.今天我们就来实际操作一番,用C语言完成一个简单的井字棋游戏,让我们一起重温美好. 棋盘如下: **功能描述:**棋盘共分为九个格子,一方执"O"为棋,一方执"X"为棋,双方依次选择格子.己方棋子率先连成三子的获胜,若棋盘占满仍未分胜负,则打成平局. 具体功能实现: 1.在页面选择玩家vs玩家,或玩家vs电脑 2.玩家下棋时,输入对应格子的坐标 3.电脑下棋时,使用随机值选择坐

  • python实现人机对战的井字棋游戏

    本文实例为大家分享了python实现人机对战井字棋的具体代码,供大家参考,具体内容如下 游戏简介:在九宫格内进行,如果一方抢先于另一方向(横.竖.斜)连成3子,则获得胜利.游戏中输入方格位置代号的形式如下: 设计前的思路: 游戏中,board棋盘存储玩家.计算机的落子信息,未落子处未EMPTY.由于人机对战,需要实现计算机智能性,下面是为这个计算机机器人设计的简单策略:如果有一步棋可以让计算机机器人在本轮获胜,那就选那一步走.否则,如果有一步棋可以让玩家在本轮获胜,那就选那一步走.否则,计算机机

  • C语言实现简单井字棋游戏

    本文实例为大家分享了C语言实现简单井字棋游戏的具体代码,供大家参考,具体内容如下 游戏截图 源代码 person.h //玩家对战 void person() { int i,j; initMap(map); //打印棋局 displayMap(map); //未分出胜负且棋局未落满子前无限循环 while(1) { //获取玩家下子位置 getXY(&i,&j); //玩家落子 setPiece(map,i,j); //清屏 system("cls"); //打印棋局

  • Python+Tkinter实现经典井字棋小游戏

    目录 演示 介绍 官方文档 tkinter.messagebox 源码 演示 介绍 首先来介绍一下GUI库Tkinter 主要模块: tkinter Main Tkinter module. tkinter.colorchooser 让用户选择颜色的对话框. tkinter.commondialog 本文其他模块定义的对话框的基类. tkinter.filedialog 允许用户指定文件的通用对话框,用于打开或保存文件. tkinter.font 帮助操作字体的工具. tkinter.messa

随机推荐