我不允许我的文章有缺点 今天是井字棋 明天就是我不知道
下面 正文开始:
两个程序的代码
turtle版
from tkinter.messagebox import*;from turtle import*
ht();speed(0);pensize(3);a=Screen();a.setup(width=0.5,height=0.5);j=['0','0','0','0','0','0','0','0','0']
def c(d,e):
pu();goto(b*50-d,e);pd()
if l%2==0:write('×',font=('黑体',40,'normal'));j[b-1]='1'
else:write('〇',font=('黑体',40,'normal'));j[b-1]='2'
for i in range(2):pu();goto(-50,50-i*50);pd();fd(150)
left(90)
for g in range(2):pu();goto(g*50,-50);pd();fd(150)
def k(n):showerror('锐骐牌井字棋','“'+str(n)+'”赢了')
for l in range(9):
b=int(a.numinput('锐骐牌井字棋','请输入你想要的位置',default=1,maxval=9,minval=1))
if b<4:c(100,50)
elif 3<b<7:c(250,0)
else:c(400,-50)
if j[0]+j[4]+j[8]=='111' or j[2]+j[4]+j[6]=='111' or j[0]+j[3]+j[6]=='111' or j[1]+j[4]+j[7]=='111' or j[2]+j[5]+j[8]=='111' or j[0]+j[1]+j[2]=='111' or j[3]+j[4]+j[5]=='111' or j[6]+j[7]+j[8]=='111':k('×');done()
elif j[0]+j[4]+j[8]=='222' or j[2]+j[4]+j[6]=='222' or j[0]+j[3]+j[6]=='222' or j[1]+j[4]+j[7]=='222' or j[2]+j[5]+j[8]=='222' or j[0]+j[1]+j[2]=='222' or j[3]+j[4]+j[5]=='222' or j[6]+j[7]+j[8]=='222':k('〇');done()
if '0' not in j:showerror('锐骐牌井字棋','平局')
done()
from tkinter.messagebox import*;from turtle import*
ht();speed(0);pensize(3);a=Screen();a.setup(width=0.5,height=0.5);j=['0','0','0','0','0','0','0','0','0']
def c(d,e):
pu();goto(b*50-d,e);pd()
if l%2==0:write('×',font=('黑体',40,'normal'));j[b-1]='1'
else:write('〇',font=('黑体',40,'normal'));j[b-1]='2'
for i in range(2):pu();goto(-50,50-i*50);pd();fd(150)
left(90)
for g in range(2):pu();goto(g*50,-50);pd();fd(150)
def k(n):showerror('苦瓜牌井字棋','“'+str(n)+'”赢了')
for l in range(9):
b=int(a.numinput('苦瓜牌井字棋','请输入你想要的位置',default=1,maxval=9,minval=1))
if b<4:c(100,50)
elif 3<b<7:c(250,0)
else:c(400,-50)
if j[0]+j[4]+j[8]=='111' or j[2]+j[4]+j[6]=='111' or j[0]+j[3]+j[6]=='111' or j[1]+j[4]+j[7]=='111' or j[2]+j[5]+j[8]=='111' or j[0]+j[1]+j[2]=='111' or j[3]+j[4]+j[5]=='111' or j[6]+j[7]+j[8]=='111':k('×');done()
elif j[0]+j[4]+j[8]=='222' or j[2]+j[4]+j[6]=='222' or j[0]+j[3]+j[6]=='222' or j[1]+j[4]+j[7]=='222' or j[2]+j[5]+j[8]=='222' or j[0]+j[1]+j[2]=='222' or j[3]+j[4]+j[5]=='222' or j[6]+j[7]+j[8]=='222':k('〇');done()
if '0' not in j:showerror('苦瓜牌井字棋','平局')
done()
____________________________________________________________________
tkinter版
from tkinter.messagebox import*
from tkinter import*
a=Tk();a.geometry('60x60')
x,y=['0','0','0','0','0','0','0','0','0'],0
def c(d,e,g):
global y
if y%2==0:h=Button(text='×',font=('normal',10));h.place(x=d,y=e,width=20,height=20);x[g]='1'
elif y%2==1:h=Button(text='〇',font=('normal',10));h.place(x=d,y=e,width=20,height=20);x[g]='2'
y+=1
if x[0]+x[4]+x[8]=='111' or x[2]+x[4]+x[6]=='111' or x[0]+x[3]+x[6]=='111' or x[1]+x[4]+x[7]=='111' or x[2]+x[5]+x[8]=='111' or x[0]+x[1]+x[2]=='111' or x[3]+x[4]+x[5]=='111' or x[6]+x[7]+x[8]=='111':showerror('苦瓜の井字棋','“×”赢了')
elif x[0]+x[4]+x[8]=='222' or x[2]+x[4]+x[6]=='222' or x[0]+x[3]+x[6]=='222' or x[1]+x[4]+x[7]=='222' or x[2]+x[5]+x[8]=='222' or x[0]+x[1]+x[2]=='222' or x[3]+x[4]+x[5]=='222' or x[6]+x[7]+x[8]=='222':showerror('苦瓜の井字棋','“〇”赢了')
elif '0' not in x:showerror('苦瓜牌井字棋','平局')
def b():c(0,0,0)
def f():c(0,20,1)
def q():c(0,40,2)
def r():c(20,0,3)
def s():c(20,20,4)
def t():c(20,40,5)
def u():c(40,0,6)
def v():c(40,20,7)
def w():c(40,40,8)
def i(j,k,l):h=Button(text='',font=('normal',10),command=j);h.place(x=k,y=l,width=20,height=20)
i(b,0,0);i(r,20,0);i(u,40,0);i(f,0,20);i(s,20,20);i(v,40,20);i(q,0,40);i(t,20,40);i(w,40,40)
from tkinter.messagebox import*
from tkinter import*
a=Tk();a.geometry('60x60')
x,y=['0','0','0','0','0','0','0','0','0'],0
def c(d,e,g):
global y
if y%2==0:h=Button(text='×',font=('normal',10));h.place(x=d,y=e,width=20,height=20);x[g]='1'
elif y%2==1:h=Button(text='〇',font=('normal',10));h.place(x=d,y=e,width=20,height=20);x[g]='2'
y+=1
if x[0]+x[4]+x[8]=='111' or x[2]+x[4]+x[6]=='111' or x[0]+x[3]+x[6]=='111' or x[1]+x[4]+x[7]=='111' or x[2]+x[5]+x[8]=='111' or x[0]+x[1]+x[2]=='111' or x[3]+x[4]+x[5]=='111' or x[6]+x[7]+x[8]=='111':showerror('苦瓜の井字棋','“×”赢了')
elif x[0]+x[4]+x[8]=='222' or x[2]+x[4]+x[6]=='222' or x[0]+x[3]+x[6]=='222' or x[1]+x[4]+x[7]=='222' or x[2]+x[5]+x[8]=='222' or x[0]+x[1]+x[2]=='222' or x[3]+x[4]+x[5]=='222' or x[6]+x[7]+x[8]=='222':showerror('苦瓜の井字棋','“〇”赢了')
elif '0' not in x:showerror('苦瓜牌井字棋','平局')
def b():c(0,0,0)
def f():c(0,20,1)
def q():c(0,40,2)
def r():c(20,0,3)
def s():c(20,20,4)
def t():c(20,40,5)
def u():c(40,0,6)
def v():c(40,20,7)
def w():c(40,40,8)
def i(j,k,l):h=Button(text='',font=('normal',10),command=j);h.place(x=k,y=l,width=20,height=20)
i(b,0,0);i(r,20,0);i(u,40,0);i(f,0,20);i(s,20,20);i(v,40,20);i(q,0,40);i(t,20,40);i(w,40,40)
________________________________________________________
运行效果
turtle版
输入
胜利
tkinter版
输入
胜利
________________________________________________________
turtle版
代码一览
from tkinter.messagebox import*;from turtle import*
ht();speed(0);pensize(3);a=Screen();a.setup(width=0.5,height=0.5);j=['0','0','0','0','0','0','0','0','0']
def c(d,e):
pu();goto(b*50-d,e);pd()
if l%2==0:write('×',font=('黑体',40,'normal'));j[b-1]='1'
else:write('〇',font=('黑体',40,'normal'));j[b-1]='2'
for i in range(2):pu();goto(-50,50-i*50);pd();fd(150)
left(90)
for g in range(2):pu();goto(g*50,-50);pd();fd(150)
def k(n):showerror('苦瓜牌井字棋','“'+str(n)+'”赢了')
for l in range(9):
b=int(a.numinput('苦瓜牌井字棋','请输入你想要的位置',default=1,maxval=9,minval=1))
if b<4:c(100,50)
elif 3<b<7:c(250,0)
else:c(400,-50)
if j[0]+j[4]+j[8]=='111' or j[2]+j[4]+j[6]=='111' or j[0]+j[3]+j[6]=='111' or j[1]+j[4]+j[7]=='111' or j[2]+j[5]+j[8]=='111' or j[0]+j[1]+j[2]=='111' or j[3]+j[4]+j[5]=='111' or j[6]+j[7]+j[8]=='111':k('×');done()
elif j[0]+j[4]+j[8]=='222' or j[2]+j[4]+j[6]=='222' or j[0]+j[3]+j[6]=='222' or j[1]+j[4]+j[7]=='222' or j[2]+j[5]+j[8]=='222' or j[0]+j[1]+j[2]=='222' or j[3]+j[4]+j[5]=='222' or j[6]+j[7]+j[8]=='222':k('〇');done()
if '0' not in j:showerror('苦瓜牌井字棋','平局')
done()
____________________________________________________________________
步骤
1.导入模块
from tkinter.messagebox import*#弹窗库 负责表示输赢
from turtle import*#绘图库 负责图像显示
2.基础设置
ht()#隐藏画笔
#写成hideturtle()也行
speed(0)#把速度设为最快
#接着从1到10 速度越来越快(0比较特殊)
pensize(3)#把画笔宽度设置为3像素
a=Screen()#创建弹窗 并命名为a
a.setup(width=0.5,height=0.5)#设置弹窗大小 长和宽都是电脑的一半 如果大于1 那么就是多少像素
j=['0','0','0','0','0','0','0','0','0']#棋盘列表
3.绘制棋盘
for i in range(2):#运行两次
pu()#抬起画笔
goto(-50,50-i*50)
pd()#放下画笔
fd(150)#前进150像素
left(90)#左转90°
for g in range(2):#运行两次
pu()#抬起画笔
goto(g*50,-50)
pd()#放下画笔
fd(150)#前进150像素
4.主循环
for l in range(9):#运行九次因为有九个空位可以下
b=int(a.numinput('苦瓜牌井字棋','请输入你想要的位置',default=1,maxval=9,minval=1))
'''
numinput函数有2~5个参数 前两个必须写 后面三个随意搭配
default参数是默认值
maxval参数是最大值
minval参数是最小值
'''
5.落子函数
def c(d,e):
pu()#抬起画笔
goto(b*50-d,e)
pd()#落下画笔
if l%2==0:#判断是否是偶数
write('×',font=('黑体',40,'normal'))#是偶数就打叉
j[b-1]='1'#把列表中表示棋子位置的元素改为'1'
else:#如果不是偶数
write('〇',font=('黑体',40,'normal'))#不是偶数就画圈
j[b-1]='2'#把列表中表示棋子位置的元素改为'2'
6.使用落子函数
for l in range(9):
b=int(a.numinput('苦瓜牌井字棋','请输入你想要的位置',default=1,maxval=9,minval=1))
if b<4:c(100,50)#如果在第一行
elif 3<b<7:c(250,0)#如果在第三行
else:c(400,-50)#如果在第二行
7.判断胜利
for l in range(9):
b=int(a.numinput('锐骐牌井字棋','请输入你想要的位置',default=1,maxval=9,minval=1))
if b<4:c(100,50)
elif 3<b<7:c(250,0)
else:c(400,-50)
if j[0]+j[4]+j[8]=='111' or j[2]+j[4]+j[6]=='111' or j[0]+j[3]+j[6]=='111' or j[1]+j[4]+j[7]=='111' or j[2]+j[5]+j[8]=='111' or j[0]+j[1]+j[2]=='111' or j[3]+j[4]+j[5]=='111' or j[6]+j[7]+j[8]=='111':
k('×')#×胜利
done()
elif j[0]+j[4]+j[8]=='222' or j[2]+j[4]+j[6]=='222' or j[0]+j[3]+j[6]=='222' or j[1]+j[4]+j[7]=='222' or j[2]+j[5]+j[8]=='222' or j[0]+j[1]+j[2]=='222' or j[3]+j[4]+j[5]=='222' or j[6]+j[7]+j[8]=='222':
k('〇')#○胜利
done()
这边采用了看看所有可能胜利的条件来判断是否胜利也就是最笨的办法判断胜利
8.处理平局
for l in range(9):
b=int(a.numinput('苦瓜牌井字棋','请输入你想要的位置',default=1,maxval=9,minval=1))
if b<4:c(100,50)
elif 3<b<7:c(250,0)
else:c(400,-50)
if j[0]+j[4]+j[8]=='111' or j[2]+j[4]+j[6]=='111' or j[0]+j[3]+j[6]=='111' or j[1]+j[4]+j[7]=='111' or j[2]+j[5]+j[8]=='111' or j[0]+j[1]+j[2]=='111' or j[3]+j[4]+j[5]=='111' or j[6]+j[7]+j[8]=='111':k('×');done()
elif j[0]+j[4]+j[8]=='222' or j[2]+j[4]+j[6]=='222' or j[0]+j[3]+j[6]=='222' or j[1]+j[4]+j[7]=='222' or j[2]+j[5]+j[8]=='222' or j[0]+j[1]+j[2]=='222' or j[3]+j[4]+j[5]=='222' or j[6]+j[7]+j[8]=='222':k('〇');done()
if '0' not in j:#如果没有空位了
showerror('苦瓜牌井字棋','平局')
done()
____________________________________________________________________
总览
from tkinter.messagebox import*
from turtle import*
ht()
speed(0)
pensize(3)
a=Screen()
a.setup(width=0.5,height=0.5)
j=['0','0','0','0','0','0','0','0','0']
def c(d,e):
pu()
goto(b*50-d,e)
pd()
if l%2==0:
write('×',font=('黑体',40,'normal'))
j[b-1]='1'
else:
write('〇',font=('黑体',40,'normal'))
j[b-1]='2'
for i in range(2):
pu()
goto(-50,50-i*50)
pd()
fd(150)
left(90)
for g in range(2):
pu()
goto(g*50,-50)
pd()
fd(150)
def k(n):
showerror('苦瓜牌井字棋','“'+str(n)+'”赢了')
for l in range(9):
b=int(a.numinput('苦瓜牌井字棋','请输入你想要的位置',default=1,maxval=9,minval=1))
if b<4:
c(100,50)
elif 3<b<7:
c(250,0)
else:
c(400,-50)
if j[0]+j[4]+j[8]=='111' or j[2]+j[4]+j[6]=='111' or j[0]+j[3]+j[6]=='111' or j[1]+j[4]+j[7]=='111' or j[2]+j[5]+j[8]=='111' or j[0]+j[1]+j[2]=='111' or j[3]+j[4]+j[5]=='111' or j[6]+j[7]+j[8]=='111':
k('×')
done()
elif j[0]+j[4]+j[8]=='222' or j[2]+j[4]+j[6]=='222' or j[0]+j[3]+j[6]=='222' or j[1]+j[4]+j[7]=='222' or j[2]+j[5]+j[8]=='222' or j[0]+j[1]+j[2]=='222' or j[3]+j[4]+j[5]=='222' or j[6]+j[7]+j[8]=='222':
k('〇')
done()
if '0' not in j:showerror('苦瓜牌井字棋','平局')
done()
________________________________________________________
tkinter版
代码一览
from tkinter.messagebox import*;from tkinter import*;a=Tk();a.geometry('60x60');x,y=['0','0','0','0','0','0','0','0','0'],0
def c(d,e,g):
global y
if y%2==0:h=Button(text='×',font=('normal',10));h.place(x=d,y=e,width=20,height=20);x[g]='1'
elif y%2==1:h=Button(text='〇',font=('normal',10));h.place(x=d,y=e,width=20,height=20);x[g]='2'
y+=1
if x[0]+x[4]+x[8]=='111' or x[2]+x[4]+x[6]=='111' or x[0]+x[3]+x[6]=='111' or x[1]+x[4]+x[7]=='111' or x[2]+x[5]+x[8]=='111' or x[0]+x[1]+x[2]=='111' or x[3]+x[4]+x[5]=='111' or x[6]+x[7]+x[8]=='111':showerror('苦瓜の井字棋','“×”赢了')
elif x[0]+x[4]+x[8]=='222' or x[2]+x[4]+x[6]=='222' or x[0]+x[3]+x[6]=='222' or x[1]+x[4]+x[7]=='222' or x[2]+x[5]+x[8]=='222' or x[0]+x[1]+x[2]=='222' or x[3]+x[4]+x[5]=='222' or x[6]+x[7]+x[8]=='222':showerror('苦瓜の井字棋','“〇”赢了')
elif '0' not in x:showerror('苦瓜牌井字棋','平局')
def b():c(0,0,0)
def f():c(0,20,1)
def q():c(0,40,2)
def r():c(20,0,3)
def s():c(20,20,4)
def t():c(20,40,5)
def u():c(40,0,6)
def v():c(40,20,7)
def w():c(40,40,8)
def i(j,k,l):h=Button(text='',font=('normal',10),command=j);h.place(x=k,y=l,width=20,height=20)
i(b,0,0);i(r,20,0);i(u,40,0);i(f,0,20);i(s,20,20);i(v,40,20);i(q,0,40);i(t,20,40);i(w,40,40)
____________________________________________________________________
步骤
1.基础设置
from tkinter.messagebox import*#弹窗库
from tkinter import*#窗口库
a=Tk()#创建窗口a
a.geometry('60x60')#窗口大小为60*60
x,y=['0','0','0','0','0','0','0','0','0'],0#x=棋盘 y=落子数
2.按钮函数设置
def c(d,e,g):#参数分别为 x轴 y轴 在列表x中代表的位置
global y#让函数内可以改变变量y
if y%2==0:#是偶数时运行
h=Button(text='×',font=('normal',10))#那么设立一个点击无效果的按钮 文本为×
h.place(x=d,y=e,width=20,height=20)#放到这个按钮上
x[g]='1'#把这个地方在列表x中代表的位置改为1
elif y%2==1:#如果是奇数运行
h=Button(text='〇',font=('normal',10))#那么设立一个点击无效果的按钮 文本为〇
h.place(x=d,y=e,width=20,height=20)#放到这个按钮上
x[g]='2'#把这个地方在列表x中代表的位置改为2
y+=1#y+1
if x[0]+x[4]+x[8]=='111' or x[2]+x[4]+x[6]=='111' or x[0]+x[3]+x[6]=='111' or x[1]+x[4]+x[7]=='111' or x[2]+x[5]+x[8]=='111' or x[0]+x[1]+x[2]=='111' or x[3]+x[4]+x[5]=='111' or x[6]+x[7]+x[8]=='111':
showerror('苦瓜の井字棋','“×”赢了')#如果×连成一条线
elif x[0]+x[4]+x[8]=='222' or x[2]+x[4]+x[6]=='222' or x[0]+x[3]+x[6]=='222' or x[1]+x[4]+x[7]=='222' or x[2]+x[5]+x[8]=='222' or x[0]+x[1]+x[2]=='222' or x[3]+x[4]+x[5]=='222' or x[6]+x[7]+x[8]=='222':
showerror('苦瓜の井字棋','“〇”赢了')#如果○连成一条线
elif '0' not in x:
showerror('苦瓜牌井字棋','平局')#如果没位置下了
'''
下列为设立各个位置以及在列表中位置的函数
'''
def b():c(0,0,0)
def f():c(0,20,1)
def q():c(0,40,2)
def r():c(20,0,3)
def s():c(20,20,4)
def t():c(20,40,5)
def u():c(40,0,6)
def v():c(40,20,7)
def w():c(40,40,8)
3.按钮设置
def i(j,k,l):#参数分别为 函数 x轴 y轴
h=Button(text='',font=('normal',10),command=j)#设置没文本的按钮
h.place(x=k,y=l,width=20,height=20)#放置位置
i(b,0,0)
i(r,20,0)
i(u,40,0)
i(f,0,20)
i(s,20,20)
i(v,40,20)
i(q,0,40)
i(t,20,40)
i(w,40,40)
____________________________________________________________________
总览
from tkinter.messagebox import*
from tkinter import*
a=Tk()
a.geometry('60x60')
x,y=['0','0','0','0','0','0','0','0','0'],0
def c(d,e,g):
global y
if y%2==0:
h=Button(text='×',font=('normal',10))
h.place(x=d,y=e,width=20,height=20)
x[g]='1'
elif y%2==1:
h=Button(text='〇',font=('normal',10))
h.place(x=d,y=e,width=20,height=20)
x[g]='2'
y+=1
if x[0]+x[4]+x[8]=='111' or x[2]+x[4]+x[6]=='111' or x[0]+x[3]+x[6]=='111' or x[1]+x[4]+x[7]=='111' or x[2]+x[5]+x[8]=='111' or x[0]+x[1]+x[2]=='111' or x[3]+x[4]+x[5]=='111' or x[6]+x[7]+x[8]=='111':
showerror('苦瓜の井字棋','“×”赢了')
elif x[0]+x[4]+x[8]=='222' or x[2]+x[4]+x[6]=='222' or x[0]+x[3]+x[6]=='222' or x[1]+x[4]+x[7]=='222' or x[2]+x[5]+x[8]=='222' or x[0]+x[1]+x[2]=='222' or x[3]+x[4]+x[5]=='222' or x[6]+x[7]+x[8]=='222':
showerror('苦瓜の井字棋','“〇”赢了')
elif '0' not in x:
showerror('苦瓜牌井字棋','平局')
def b():
c(0,0,0)
def f():
c(0,20,1)
def q():
c(0,40,2)
def r():
c(20,0,3)
def s():
c(20,20,4)
def t():
c(20,40,5)
def u():
c(40,0,6)
def v():
c(40,20,7)
def w():
c(40,40,8)
def i(j,k,l):
h=Button(text='',font=('normal',10),command=j)
h.place(x=k,y=l,width=20,height=20)
i(b,0,0)
i(r,20,0)
i(u,40,0)
i(f,0,20)
i(s,20,20)
i(v,40,20)
i(q,0,40)
i(t,20,40)
i(w,40,40)
_____________________________________________________________________________
MVP结算
本期字数:12782
本期质量分:94
10赞更新花絮 15赞更新Python 30赞更新c++