2.10完成落子各个方向的判断
现在我们要完成同色棋子在不同方向,是否三子连成一线,如果是,提示赢了。结合我们上一节讲的内容,下面是我们要做的事情。
1、判断所有横向,两种颜色棋子是否三子成一线。
2、判断所有竖向,两种颜色棋子是否三子成一线。。
3、判断所有斜向,两种颜色棋子是否三子成一线。。
实现代码如下:
if position[0]==position[1] and position[1]==position[2]:
if position[0]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[0]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[0]==position[3] and position[3]==position[6]:
if position[0]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[0]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[0]==position[4] and position[4]==position[8]:
if position[0]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[0]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[8]==position[7] and position[7]==position[6]:
if position[8]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[8]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[8]==position[5] and position[5]==position[2]:
if position[8]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[8]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[6]==position[4] and position[4]==position[2]:
if position[4]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[4]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[3]==position[4] and position[4]==position[5]:
if position[3]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[3]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[1]==position[4] and position[4]==position[7]:
if position[1]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[1]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
总计是8中可能。三横三竖,两斜向。
将代码整合起来,整合后完整代码如下:
#-*- coding:GBK -*-
from tkinter import *
import time
import tkinter.messagebox #messagebox
tk = Tk()
tk.title("雷雷的井字游戏")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=800, height=800, bd=0, highlightthickness=0)
canvas.pack()
tk.update()
redorgreen=0
colorx="green"
position=[0,0,0,0,0,0,0,0,0]
#画棋盘
canvas.create_line(100,100,700,100)
canvas.create_line(100,300,700,300)
canvas.create_line(100,500,700,500)
canvas.create_line(100,700,700,700)
canvas.create_line(100,100,100,700)
canvas.create_line(300,100,300,700)
canvas.create_line(500,100,500,700)
canvas.create_line(700,100,700,700)
def action(event):
global redorgreen
global colorx
#if redorgreen==0 and event.x>100 and event.x<300 and event.y<300 and event.y>100:
# canvas.create_oval(100, 100, 300, 300, fill =colorx )
if redorgreen==0:
redorgreen=1
colorx="red"
else :
redorgreen=0
colorx="green"
#tkinter.messagebox.showinfo('提示',redorgreen)
if event.x>100 and event.x<300 and event.y<300 and event.y>100:
if position[0]==0:
canvas.create_oval(100, 100, 300, 300, fill =colorx )
if colorx=="red":
position[0]=1
else:
position[0]=2
else:
tkinter.messagebox.showinfo('提示',"已有棋子了!")
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return
#winyesorno()
if event.x>300 and event.x<500 and event.y<300 and event.y>100:
if position[1]==0:
canvas.create_oval(300, 100, 500, 300, fill =colorx )
if colorx=="red":
position[1]=1
else:
position[1]=2
else:
tkinter.messagebox.showinfo('提示',"已有棋子了")
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return
if event.x>500 and event.x<700 and event.y<300 and event.y>100:
if position[2]==0:
canvas.create_oval(500, 100, 700, 300, fill =colorx )
if colorx=="red":
position[2]=1
else:
position[2]=2
else:
tkinter.messagebox.showinfo('提示',"已有棋子了")
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return
if event.x>100 and event.x<300 and event.y<500 and event.y>300:
if position[3]==0:
canvas.create_oval(100, 300, 300, 500, fill =colorx )
if colorx=="red":
position[3]=1
else:
position[3]=2
else:
tkinter.messagebox.showinfo('提示',"已有棋子了")
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return
if event.x>300 and event.x<500 and event.y<500 and event.y>300:
if position[4]==0:
canvas.create_oval(300, 300, 500, 500, fill =colorx )
if colorx=="red":
position[4]=1
else:
position[4]=2
else:
tkinter.messagebox.showinfo('提示',"已有棋子了")
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return
if event.x>500 and event.x<700 and event.y<500 and event.y>300:
if position[5]==0:
canvas.create_oval(500, 300, 700, 500, fill =colorx )
if colorx=="red":
position[5]=1
else:
position[5]=2
else:
tkinter.messagebox.showinfo('提示',"已有棋子了")
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return
if event.x>100 and event.x<300 and event.y<700 and event.y>500:
if position[6]==0:
canvas.create_oval(100, 500, 300, 700, fill =colorx )
if colorx=="red":
position[6]=1
else:
position[6]=2
else:
tkinter.messagebox.showinfo('提示',"已有棋子了")
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return
if event.x>300 and event.x<500 and event.y<700 and event.y>500:
if position[7]==0:
canvas.create_oval(300, 500, 500, 700, fill =colorx )
if colorx=="red":
position[7]=1
else:
position[7]=2
else:
tkinter.messagebox.showinfo('提示',"已有棋子了")
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return
if event.x>500 and event.x<700 and event.y<700 and event.y>500:
if position[8]==0:
canvas.create_oval(500, 500, 700, 700, fill =colorx )
if colorx=="red":
position[8]=1
else:
position[8]=2
else:
tkinter.messagebox.showinfo('提示',"已有棋子了")
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return
if position[0]==position[1] and position[1]==position[2]:
if position[0]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[0]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[0]==position[3] and position[3]==position[6]:
if position[0]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[0]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[0]==position[4] and position[4]==position[8]:
if position[0]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[0]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[8]==position[7] and position[7]==position[6]:
if position[8]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[8]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[8]==position[5] and position[5]==position[2]:
if position[8]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[8]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[6]==position[4] and position[4]==position[2]:
if position[4]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[4]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[3]==position[4] and position[4]==position[5]:
if position[3]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[3]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
if position[1]==position[4] and position[4]==position[7]:
if position[1]==1:
tkinter.messagebox.showinfo('提示',"红方获胜")
if position[1]==2:
tkinter.messagebox.showinfo('提示',"绿方获胜")
canvas.bind('<Button-1>', action)
while 1:
tk.update_idletasks()
tk.update()
time.sleep(0.01)
运行这段代码,我们测试一下各个方向的判断结果(我们以左侧斜上方向为例)。见图2-16
图2-16