|
|
from tkinter import *
|
|
|
import tkinter.messagebox
|
|
|
from chess import *
|
|
|
from PIL import Image,ImageTk
|
|
|
from datetime import datetime
|
|
|
import time
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
chess = Chess()
|
|
|
crossline = []
|
|
|
verline = []
|
|
|
regretnum = 0
|
|
|
handlenum = 0
|
|
|
|
|
|
root = Tk() # 窗口对象
|
|
|
root.title('五子棋') # 窗口标题
|
|
|
root.geometry('1000x700+10+10') # 窗口大小 width x height + x + y(x,y为在当前界面的坐标)
|
|
|
# 创建一个主目录菜单,也被称为顶级菜单
|
|
|
main_menu = Menu (root)
|
|
|
gamefile = Menu (main_menu, tearoff=False)
|
|
|
#新增"文件"菜单的菜单项,并使用 accelerator 设置菜单项的快捷键
|
|
|
# gamefile.add_command (label="新建",command=menuCommand,accelerator="Ctrl+N")
|
|
|
def peocomwhite():
|
|
|
if chess.Chess_Mode == 3:
|
|
|
chess.Chess_Mode = 1
|
|
|
chess.playing(1,root,canvas,blackch,whitech,photos1,photos2)
|
|
|
else:
|
|
|
chess.Chess_Mode = 1
|
|
|
refresh()
|
|
|
def peocomblack():
|
|
|
if chess.Chess_Mode == 3:
|
|
|
chess.Chess_Mode = 2
|
|
|
chess.playing(2,root,canvas,blackch,whitech,photos1,photos2)
|
|
|
else:
|
|
|
chess.Chess_Mode = 2
|
|
|
refresh()
|
|
|
def doublepeople():
|
|
|
if chess.Chess_Mode == 3:
|
|
|
chess.Chess_Mode = 0
|
|
|
chess.playing(0,root,canvas,blackch,whitech,photos1,photos2)
|
|
|
else:
|
|
|
chess.Chess_Mode = 0
|
|
|
refresh()
|
|
|
|
|
|
def startgame():
|
|
|
win = Tk()
|
|
|
win.title("开始游戏")
|
|
|
win.geometry('500x300+100+100')
|
|
|
Label(win, text="五子棋", width=120, height=2).place(x=-175, y=50)
|
|
|
Button(win, text="人机模式(机白)", command=peocomwhite, width=20, height=2).place(x=170, y=100)
|
|
|
Button(win, text="人机模式(机黑)", command=peocomblack, width=20, height=2).place(x=170, y=150)
|
|
|
Button(win, text="双人模式", command=doublepeople, width=20, height=2).place(x=170, y=200)
|
|
|
def quitgame():
|
|
|
pass
|
|
|
def savecurrent():
|
|
|
data = [[0 for j in range(19)] for i in range(19)]
|
|
|
file = open('data.txt', 'r+')
|
|
|
file.truncate(0)
|
|
|
for i in range(0, 19):
|
|
|
for j in range(0, 19):
|
|
|
data[i][j] = chess.ChessData[i][j]['Cstate']
|
|
|
data = np.mat(data)
|
|
|
b = ''
|
|
|
for i in range(0, 19):
|
|
|
for j in range(0, 19):
|
|
|
b += str(data[i, j]) + '\t'
|
|
|
b += '\n'
|
|
|
file.writelines(b)
|
|
|
print(data)
|
|
|
data1 = [[0 for j in range(19)] for i in range(19)]
|
|
|
for i in range(0, 19):
|
|
|
for j in range(0, 19):
|
|
|
data1[i][j] = chess.ChessData[i][j]['Cstep']
|
|
|
data = np.mat(data1)
|
|
|
b = ''
|
|
|
for i in range(0, 19):
|
|
|
for j in range(0, 19):
|
|
|
b += str(data[i, j]) + '\t'
|
|
|
b += '\n'
|
|
|
file.writelines(b)
|
|
|
print(data)
|
|
|
Chess_Mode = str(chess.Chess_Mode) + '\n'
|
|
|
file.write(Chess_Mode)
|
|
|
step = str(chess.Currently_step) + '\n'
|
|
|
file.write(step)
|
|
|
WinFLAG = str(chess.WinFLAG) + '\n'
|
|
|
file.write(WinFLAG)
|
|
|
file.close()
|
|
|
|
|
|
def resetlast():
|
|
|
refresh()
|
|
|
file = open('data.txt', 'r+', encoding='utf-8')
|
|
|
data = file.readlines()
|
|
|
rtu = []
|
|
|
for i in data:
|
|
|
rtu.append(i.strip())
|
|
|
for i in range(0,19):
|
|
|
j = 0
|
|
|
rtu1 = rtu[i].split("\t")
|
|
|
for x in rtu1:
|
|
|
chess.ChessData[i][j]['Cstate']=int(x)
|
|
|
j+=1
|
|
|
for i in range(19, 38):
|
|
|
j = 0
|
|
|
rtu1 = rtu[i].split("\t")
|
|
|
for x in rtu1:
|
|
|
chess.ChessData[i-19][j]['Cstep'] = int(x)
|
|
|
j += 1
|
|
|
chess.Chess_Mode = int(rtu[len(rtu)-3])
|
|
|
chess.Currently_step = int(rtu[len(rtu) - 2])
|
|
|
chess.WinFLAG = int(rtu[len(rtu) - 1])
|
|
|
file.close()
|
|
|
for x in range(19):
|
|
|
for y in range(19):
|
|
|
if chess.ChessData[x][y]['Cstate'] == 1:
|
|
|
chess.Chessimg[x][y]=canvas.create_image(x* 36.8 + 41- 14, y* 34.6 + 21, image=whitech, anchor=W)
|
|
|
# canvas.create_oval(x*34.2+40-12.5, y*34.2+30-13.5, x*34.2+40+12.5, y*34.2+30+13.5, fill='#fff',
|
|
|
# outline="")
|
|
|
elif chess.ChessData[x][y]['Cstate'] == 2:
|
|
|
chess.Chessimg[x][y]=canvas.create_image(x* 36.8 + 41- 14, y* 34.6 + 21, image=blackch, anchor=W)
|
|
|
# canvas.create_oval(x*34.2+40-12.5, y*34.2+30-13.5, x*34.2+40+12.5, y*34.2+30+13.5, fill='#000',
|
|
|
# outline="")
|
|
|
if chess.Chess_Mode == 1 and chess.WinFLAG ==0 :
|
|
|
chess.myColor = 2
|
|
|
chess.computercolor = 1
|
|
|
if chess.Currently_step % 2 ==0:
|
|
|
chess.player = 1
|
|
|
chess.computer = 0
|
|
|
chess.curlocation(canvas,1,0,1, blackch,whitech,photos1,photos2)
|
|
|
tkinter.messagebox.showinfo(title='信息提示!', message='轮到玩家下棋了!')
|
|
|
elif chess.Currently_step % 2 !=0:
|
|
|
chess.player = 0
|
|
|
chess.computer = 1
|
|
|
chess.playgame_black(root,None,None,canvas, blackch,whitech,result,photos1,photos2)
|
|
|
elif chess.Chess_Mode == 2 and chess.WinFLAG ==0 :
|
|
|
chess.myColor = 1
|
|
|
chess.computercolor = 2
|
|
|
if chess.Currently_step % 2 == 0:
|
|
|
chess.player = 0
|
|
|
chess.computer = 1
|
|
|
chess.playgame_white(root, None, None, canvas, blackch,whitech,result,photos1,photos2)
|
|
|
elif chess.Currently_step % 2 != 0:
|
|
|
chess.player = 1
|
|
|
chess.computer = 0
|
|
|
chess.curlocation(canvas, 1, 0,2,blackch,whitech,photos1,photos2)
|
|
|
tkinter.messagebox.showinfo(title='信息提示!', message='轮到玩家下棋了!')
|
|
|
elif chess.Chess_Mode == 0 and chess.WinFLAG ==0 :
|
|
|
chess.myColor = 2
|
|
|
chess.player2color = 1
|
|
|
if chess.Currently_step % 2 == 0:
|
|
|
chess.player = 1
|
|
|
chess.player2 = 0
|
|
|
chess.curlocation(canvas, 1, 1,1,blackch,whitech,photos1,photos2)
|
|
|
# chess.playgame_black(root, None, None, canvas)
|
|
|
tkinter.messagebox.showinfo(title='信息提示!', message='轮到先手下棋了!')
|
|
|
elif chess.Currently_step % 2 != 0:
|
|
|
chess.player = 0
|
|
|
chess.player2 = 1
|
|
|
chess.curlocation(canvas, 0, 0,2,blackch,whitech,photos1,photos2)
|
|
|
tkinter.messagebox.showinfo(title='信息提示!', message='轮到后手下棋了!')
|
|
|
elif chess.WinFLAG ==1 :
|
|
|
tkinter.messagebox.showinfo(title='信息提示!', message='对局已经结束了')
|
|
|
gamefile.add_command (label="开始游戏",command=startgame)
|
|
|
# gamefile.add_command (label="暂停游戏",command=quitgame)
|
|
|
gamefile.add_command (label="保存当前局面",command=savecurrent, accelerator="Ctrl+O")
|
|
|
gamefile.add_command (label="重装以前局面",command=resetlast, accelerator="Ctrl+S")
|
|
|
main_menu.add_cascade (label="游戏",menu=gamefile)#在主目录菜单上新增"文件"选项,并通过menu参数与下拉菜单绑定
|
|
|
def helpgame():
|
|
|
msg ='''1、对局双方各执一色棋子。
|
|
|
2、空棋盘开局。
|
|
|
3、黑棋虽先行,但有禁手:黑方不能在一步之内形成两个“活三”“活四”或一步之内形成“长连”(指一步形成超过五子连珠)。白方自由,无禁手。
|
|
|
4、棋子下在棋盘的空白点上,棋子下定后,不得向其它点移动,不得从棋盘上拿掉或拿起另落别处。
|
|
|
5、黑方的第一枚棋子可下在棋盘任意交叉点上。'''
|
|
|
tkinter.messagebox.showinfo(title='游戏规则', message=msg)
|
|
|
helpfile = main_menu.add_command(label="帮助",command=helpgame)
|
|
|
root.config (menu=main_menu)
|
|
|
#棋盘背景画面
|
|
|
canvas = Canvas(root,bg='white',width=1000,height=800)
|
|
|
photoImg = Image.open("image/背景.png").resize((1000, 800))
|
|
|
imgbg = ImageTk.PhotoImage(photoImg)
|
|
|
canvas.create_image(0, 330, image = imgbg,anchor=W)
|
|
|
|
|
|
|
|
|
photoqizi=Image.open("image/棋盘-空.png").resize((730,700))
|
|
|
photoqizi=ImageTk.PhotoImage(photoqizi)
|
|
|
canvas.create_image(10,350,image=photoqizi,anchor=W)
|
|
|
def cross():
|
|
|
for i in range(19):#横线
|
|
|
point = [[40, 37], [706, 37]]
|
|
|
point[0][1] = 21 + i * 34.3
|
|
|
point[1][1] = 21 + i * 34.3
|
|
|
if i==0 or i==18:
|
|
|
canvas.create_line(point, fill="#C18B5F", width=2.5)
|
|
|
canvas.create_line(point, fill="#C18B5F", width=1.5)
|
|
|
for i in range(19):#竖线
|
|
|
point = [[41, 21], [41, 640]]
|
|
|
point[0][0] = 41 + i * 37
|
|
|
point[1][0] = 41 + i * 37
|
|
|
if i==0 or i==18:
|
|
|
canvas.create_line(point, fill="#C18B5F", width=2.5)
|
|
|
canvas.create_line(point, fill="#C18B5F", width=1.5)
|
|
|
for x in (4, 9, 14):
|
|
|
for y in (4, 9, 14):
|
|
|
if x == y == 9:
|
|
|
radius = 9
|
|
|
else:
|
|
|
radius = 6
|
|
|
canvas.create_oval(x * 36.9 + 41 - radius / 2,
|
|
|
y * 34.3 + 21 - radius / 2,
|
|
|
x * 36.9 + 41 + radius / 2,
|
|
|
y * 34.3 + 21 + radius / 2,
|
|
|
fill='#BE875E',outline="")
|
|
|
cross()
|
|
|
|
|
|
|
|
|
|
|
|
canvas.place(x=0, y=0)
|
|
|
|
|
|
|
|
|
#鼠标左击落子事件定义
|
|
|
class Player:
|
|
|
def PointNextMove(chessData):
|
|
|
global playerx,playery
|
|
|
playerx = float(format(chessData.x))
|
|
|
playery = float(format(chessData.y))
|
|
|
if chess.player == 1:
|
|
|
if chess.myColor == 2 and chess.computercolor == 1 :
|
|
|
chess.playgame_black(root,playerx,playery,canvas, blackch,whitech,result,photos1,photos2)
|
|
|
if chess.myColor == 1 and chess.computercolor == 2 :
|
|
|
chess.playgame_white(root,playerx,playery,canvas, blackch,whitech,result,photos1,photos2)
|
|
|
if chess.myColor == 2 and chess.player2color ==1 :
|
|
|
chess.doublepeople(root, playerx, playery, canvas, blackch,whitech,result,photos1,photos2)
|
|
|
print('鼠标左键单机位置(相对于父容器):{0},{1}'.format(chessData.x, chessData.y))
|
|
|
print('鼠标左键单击位置(相对于屏幕):{0},{1}'.format(chessData.x_root, chessData.y_root))
|
|
|
# canvas.bind('<Motion>',handleMotion)
|
|
|
canvas.bind('<Button-1>', PointNextMove)
|
|
|
class HumanPlayer(Player):
|
|
|
def NextMove(self):
|
|
|
chess.player_location()
|
|
|
class RobotPlayer(Player):
|
|
|
def NextMove(self):
|
|
|
chess.ai_location()
|
|
|
|
|
|
# 图片创建
|
|
|
blackch = Image.open("image/黑子-小.png").resize((40, 40))
|
|
|
blackch = ImageTk.PhotoImage(blackch)
|
|
|
whitech = Image.open("image/白子-小.png").resize((40, 40))
|
|
|
whitech = ImageTk.PhotoImage(whitech)
|
|
|
result = Image.open("image/resultshow.PNG").resize((100, 42))
|
|
|
result = ImageTk.PhotoImage(result)
|
|
|
photos1=Image.open("image/上方button-选中.png").resize((110,45))
|
|
|
photos1=ImageTk.PhotoImage(photos1)
|
|
|
photos2=Image.open("image/上方button-未选中.png").resize((110,45))
|
|
|
photos2=ImageTk.PhotoImage(photos2)
|
|
|
|
|
|
#棋盘
|
|
|
# photoqizi=Image.open("image/棋盘-空.png").resize((730,700))
|
|
|
# photoqizi=ImageTk.PhotoImage(photoqizi)
|
|
|
# canvas.create_image(10,350,image=photoqizi,anchor=W)
|
|
|
|
|
|
#当前落子方
|
|
|
# photocur = Image.open("image/当前落子方背景.png").resize((100, 30))
|
|
|
# photocur = ImageTk.PhotoImage(photocur)
|
|
|
# canvas.create_image(720, 40, image = photocur,anchor=W)
|
|
|
canvas.create_text(800,90,text='当前落子方',font='Arial,10',fill='white')
|
|
|
# lab_name = Label(root, text="当前落子方", font='Arial,10',fg='white',image=photocur,compound=CENTER)
|
|
|
# lab_name.place(x=630, y=85, width=100, height=30)
|
|
|
#当前落子方跳选框
|
|
|
canvas.create_image(750,145,image=photos2,anchor=W)
|
|
|
canvas.create_image(870,145,image=photos2,anchor=W)
|
|
|
|
|
|
#胜负判定
|
|
|
photoresult = Image.open("image/胜负判定背景.PNG").resize((100, 30))
|
|
|
photoresult = ImageTk.PhotoImage(photoresult)
|
|
|
# canvas.create_image(630, 200, image = photoresult,anchor=W)
|
|
|
cur_result = Label(root, text='胜负判定', font='Arial,10',fg='white',image=photoresult,compound=CENTER)
|
|
|
cur_result.config(bg='#8B7355')
|
|
|
cur_result.place(x=750, y=190,width=100, height=30)
|
|
|
#公告栏
|
|
|
photonotice=Image.open("image/公告栏.png").resize((230,130))
|
|
|
photonotice=ImageTk.PhotoImage(photonotice)
|
|
|
canvas.create_image(750,300,image=photonotice,anchor=W)
|
|
|
|
|
|
|
|
|
#按钮复盘
|
|
|
def review():
|
|
|
if chess.WinFLAG == 1:
|
|
|
for x in range(19):
|
|
|
for y in range(19):
|
|
|
if chess.ChessData[x][y]['Cstep'] !=0 and chess.ChessData[x][y]['Cstate'] == 2:
|
|
|
reviewnum=Label(root, text=str(chess.ChessData[x][y]['Cstep']),bg='#000',fg ='#fff', font=("黑体", 8), width=1, height=1)
|
|
|
reviewnum.place(x=x*36.8+41-7,y=y*34.6+21-9,anchor = 'nw')
|
|
|
chess.Relabel[x][y] = reviewnum
|
|
|
elif chess.ChessData[x][y]['Cstep'] !=0 and chess.ChessData[x][y]['Cstate'] == 1 :
|
|
|
reviewnum=Label(root, text=str(chess.ChessData[x][y]['Cstep']),bg='#fff',fg ='#000', font=("黑体", 8), width=1, height=1)
|
|
|
reviewnum.place(x=x*36.8+41-7,y=y*34.6+21-9,anchor = 'nw')
|
|
|
chess.Relabel[x][y] = reviewnum
|
|
|
elif chess.WinFLAG == 0:
|
|
|
tkinter.messagebox.showinfo(title='信息提示!', message='对局结束后才能复盘噢!')
|
|
|
chess.ifreview = 1
|
|
|
root.update()
|
|
|
photoreview = Image.open("image/复盘.png").resize((100, 50))
|
|
|
photoreview = ImageTk.PhotoImage(photoreview)
|
|
|
# canvas.create_image(720, 300, image = photoresult,anchor=W)
|
|
|
btn_review = Button(root, text='复盘', font='Arial,12', width=85, height=35,
|
|
|
image=photoreview, command=review,bd=0)
|
|
|
# btn_review["bg"] =
|
|
|
# btn_review["border"] = "0"
|
|
|
btn_review.place(x=750,y=400)
|
|
|
# 按钮悔棋
|
|
|
def regretch():
|
|
|
if chess.WinFLAG == 0:
|
|
|
global regretnum
|
|
|
regretnum = 1
|
|
|
if chess.Currently_step == 1 and chess.Chess_Mode ==2:
|
|
|
regretxFLAG = xregretFLAG[chess.Currently_step - regretnum]
|
|
|
regretyFLAG = yregretFLAG[chess.Currently_step - regretnum]
|
|
|
canvas.delete(chess.Chessimg[regretxFLAG][regretyFLAG])
|
|
|
chess.ChessData[regretxFLAG][regretyFLAG]['Cstate'] = 0
|
|
|
chess.ChessData[regretxFLAG][regretyFLAG]['Cstep'] = 0
|
|
|
chess.Currently_step -= 1
|
|
|
chess.playing( 2, root, canvas)
|
|
|
if chess.Chess_Mode == 1 or chess.Chess_Mode == 2 :
|
|
|
print(xregretFLAG)
|
|
|
print(yregretFLAG)
|
|
|
for i in range(2):
|
|
|
regretxFLAG = xregretFLAG[chess.Currently_step-regretnum]
|
|
|
regretyFLAG = yregretFLAG[chess.Currently_step-regretnum]
|
|
|
canvas.delete(chess.Chessimg[regretxFLAG][regretyFLAG])
|
|
|
chess.ChessData[regretxFLAG][regretyFLAG]['Cstate'] = 0
|
|
|
chess.ChessData[regretxFLAG][regretyFLAG]['Cstep'] = 0
|
|
|
chess.Currently_step -= 1
|
|
|
chess.curlocation(canvas, 1, 0,1,blackch,whitech,photos1,photos2)
|
|
|
if chess.Chess_Mode == 0:
|
|
|
regretxFLAG = xregretFLAG[chess.Currently_step - regretnum]
|
|
|
regretyFLAG = yregretFLAG[chess.Currently_step - regretnum]
|
|
|
canvas.delete(chess.Chessimg[regretxFLAG][regretyFLAG])
|
|
|
chess.ChessData[regretxFLAG][regretyFLAG]['Cstate'] = 0
|
|
|
chess.ChessData[regretxFLAG][regretyFLAG]['Cstep'] = 0
|
|
|
chess.Currently_step -= 1
|
|
|
if chess.Currently_step % 2 == 0:
|
|
|
chess.player = 1
|
|
|
chess.player2 = 0
|
|
|
chess.curlocation(canvas, 1, 1,1)
|
|
|
elif chess.Currently_step % 2 != 0:
|
|
|
chess.player = 0
|
|
|
chess.player2 = 1
|
|
|
chess.curlocation(canvas, 0, 0,2)
|
|
|
elif chess.WinFLAG == 1 :
|
|
|
tkinter.messagebox.showinfo(title='信息提示!', message='对局已经结束了!')
|
|
|
root.update()
|
|
|
# else:
|
|
|
# tkinter.messagebox.showinfo(title='信息提示!', message='只能悔一次棋!')
|
|
|
photoregretch = Image.open("image/悔棋.png").resize((100, 50))
|
|
|
photoregretch = ImageTk.PhotoImage(photoregretch)
|
|
|
btn_regret = Button(root, text='悔棋', font='Arial,12', width=85, height=35,
|
|
|
image=photoregretch, command=regretch,bd=0)
|
|
|
btn_regret.place(x=850, y=400)
|
|
|
# 按钮撤销复盘
|
|
|
def concelreview():
|
|
|
if chess.WinFLAG == 1:
|
|
|
chess.showHistory=True
|
|
|
for x in range(19):
|
|
|
for y in range(19):
|
|
|
if chess.ChessData[x][y]['Cstep'] !=0 and chess.Relabel[x][y]!=0:
|
|
|
chess.Relabel[x][y].destroy()
|
|
|
chess.Relabel[x][y]=0
|
|
|
elif chess.WinFLAG == 0:
|
|
|
chess.showHistory=False
|
|
|
tkinter.messagebox.showinfo(title='信息提示!', message='在复盘后使用噢!')
|
|
|
root.update()
|
|
|
photoconcel = Image.open("image/撤销复盘.png").resize((100, 50))
|
|
|
photoconcel = ImageTk.PhotoImage(photoconcel)
|
|
|
btn_concel = Button(root, text='撤销复盘', font='Arial,12', width=85, height=35, image=photoconcel,
|
|
|
command=concelreview,bd=0)
|
|
|
btn_concel.place(x=750, y=460)
|
|
|
# 按钮落子建议
|
|
|
def suggestion():
|
|
|
if chess.WinFLAG!=1:
|
|
|
if chess.Depth >= 9:
|
|
|
chess.Depth = 8
|
|
|
if chess.Chess_Mode ==1:
|
|
|
color = 2
|
|
|
elif chess.Chess_Mode ==2:
|
|
|
color = 1
|
|
|
elif chess.Chess_Mode ==0:
|
|
|
if chess.player == 1 and chess.player2 == 0 :
|
|
|
color = 2
|
|
|
elif chess.player == 0 and chess.player2 == 1 :
|
|
|
color = 1
|
|
|
pos = chess.return_chess(chess.Depth, color) # 调用估值函数
|
|
|
if chess.ChessData[pos[0]][pos[1]]['Cstate']==0:
|
|
|
if chess.myColor == 1 and chess.computercolor == 2:
|
|
|
Ovalone = canvas.create_oval(pos[0]* 36.8 + 41 - 12.5, pos[1] * 34.6 + 21 - 13.5,
|
|
|
pos[0]* 36.8 + 41 + 12.5, pos[1] * 34.6 + 21 + 13.5,
|
|
|
fill='#fff', outline="#000")
|
|
|
elif chess.myColor == 2 and chess.computercolor ==1:
|
|
|
Ovalone = canvas.create_oval(pos[0] * 36.8 + 41 - 12.5, pos[1] * 34.6 + 21 - 13.5,
|
|
|
pos[0]* 36.8 + 41 + 12.5, pos[1] * 34.6 + 21 + 13.5,
|
|
|
fill='#000', outline="#fff")
|
|
|
elif chess.myColor == 2 and chess.player2color ==1:
|
|
|
if chess.player == 1 and chess.player2 == 0 :
|
|
|
Ovalone = canvas.create_oval(pos[0]* 36.8 + 41-12.5, pos[1] * 34.6 + 21-13.5,pos[0]* 36.8 + 41+12.5,
|
|
|
pos[1] * 34.6 + 21+13.5,fill='#000', outline="#fff")
|
|
|
elif chess.player2 == 1 and chess.player == 0:
|
|
|
Ovalone = canvas.create_oval(pos[0]* 36.8 + 41-12.5, pos[1] * 34.6 + 21-13.5,pos[0]* 36.8 + 41+12.5,
|
|
|
pos[1] * 34.6 + 21+13.5,fill='#fff', outline="#000")
|
|
|
chess.Ovalone = Ovalone
|
|
|
root.update()
|
|
|
else:
|
|
|
pass
|
|
|
photosuggest = Image.open("image/落子.png").resize((100, 50))
|
|
|
photosuggest = ImageTk.PhotoImage(photosuggest)
|
|
|
btn_suggest = Button(root, text='落子建议', font='Arial,12', width=85, height=35,
|
|
|
image=photosuggest, command=suggestion,bd=0)
|
|
|
btn_suggest.place(x=850, y=460)
|
|
|
# 按钮局面评估
|
|
|
def curnow():
|
|
|
if chess.Depth >= 9:
|
|
|
chess.Depth = 8
|
|
|
if chess.Chess_Mode ==1:
|
|
|
color = 2
|
|
|
elif chess.Chess_Mode ==2:
|
|
|
color = 1
|
|
|
chess.return_chess(chess.Depth, color) # 调用估值函数
|
|
|
elif chess.Chess_Mode ==0:
|
|
|
if chess.player == 1 and chess.player2 == 0 :
|
|
|
color = 2
|
|
|
elif chess.player == 0 and chess.player2 == 1 :
|
|
|
color = 1
|
|
|
chess.return_chess(chess.Depth, color) # 调用估值函数
|
|
|
if chess.Counts / chess.New_count == 1:
|
|
|
OwnCounter = random.uniform(0.8, 0.85)
|
|
|
else:
|
|
|
OwnCounter = chess.Counts / chess.New_count
|
|
|
if color == 1:
|
|
|
for x in range(15):
|
|
|
for y in range(19):
|
|
|
if chess.ChessData[x][y]['Cstate'] == 2 and chess.ChessData[x+1][y]['Cstate'] == 2 and chess.ChessData[x+2][y]['Cstate'] == 2 and \
|
|
|
chess.ChessData[x+3][y]['Cstate'] == 2 and chess.ChessData[x+4][y]['Cstate'] == 0 and chess.ChessData[x-1][y]['Cstate'] == 0:
|
|
|
OwnCounter = OwnCounter-0.5
|
|
|
# 2.判断y-轴是否连续四子
|
|
|
for x in range(19):
|
|
|
for y in range(15):
|
|
|
if chess.ChessData[x][y]['Cstate'] == 2 and chess.ChessData[x ][y +1]['Cstate'] == 2 and chess.ChessData[x][y + 2]['Cstate'] == 2 and \
|
|
|
chess.ChessData[x][y + 3]['Cstate'] == 2 and chess.ChessData[x][y + 4]['Cstate'] == 0 and chess.ChessData[x][y-1]['Cstate'] == 0:
|
|
|
OwnCounter = OwnCounter-0.5
|
|
|
# 3.判断右上-左下是否连续四子
|
|
|
for x in range(15):
|
|
|
for y in range(4, 19):
|
|
|
if chess.ChessData[x][y]['Cstate'] == 2 and chess.ChessData[x + 1][y - 1]['Cstate'] == 2 and chess.ChessData[x + 2][y - 2]['Cstate'] == 2 and \
|
|
|
chess.ChessData[x + 3][y - 3]['Cstate'] == 2 and chess.ChessData[x + 4][y- 4]['Cstate'] == 0 and chess.ChessData[x-1][y+1]['Cstate'] == 0:
|
|
|
OwnCounter = OwnCounter-0.5
|
|
|
# 4.判断左上-右下是否连续四子
|
|
|
for x in range(15):
|
|
|
for y in range(15):
|
|
|
if chess.ChessData[x][y]['Cstate'] == 2 and chess.ChessData[x + 1][y + 1]['Cstate'] == 2 and chess.ChessData[x + 2][y + 2]['Cstate'] == 2 and \
|
|
|
chess.ChessData[x + 3][y + 3]['Cstate'] == 2 and chess.ChessData[x + 4][y+4]['Cstate'] == 0 and chess.ChessData[x-1][y-1]['Cstate'] == 0:
|
|
|
OwnCounter = OwnCounter-0.5
|
|
|
elif color ==2 :
|
|
|
for x in range(15):
|
|
|
for y in range(19):
|
|
|
if chess.ChessData[x][y]['Cstate'] == 1 and chess.ChessData[x + 1][y]['Cstate'] == 1 and chess.ChessData[x + 2][y]['Cstate'] == 1 and \
|
|
|
chess.ChessData[x + 3][y]['Cstate'] == 1 and chess.ChessData[x + 4][y]['Cstate'] == 0 and chess.ChessData[x-1][y]['Cstate'] == 0:
|
|
|
OwnCounter = OwnCounter - 0.5
|
|
|
# 2.判断y-轴是否连续四子
|
|
|
for x in range(19):
|
|
|
for y in range(15):
|
|
|
if chess.ChessData[x][y]['Cstate'] == 1 and chess.ChessData[x ][y +1]['Cstate'] == 1 and chess.ChessData[x][y+2]['Cstate'] == 1 and \
|
|
|
chess.ChessData[x][y+3]['Cstate'] == 1 and chess.ChessData[x][y+4]['Cstate'] == 0 and chess.ChessData[x][y-1]['Cstate'] == 0:
|
|
|
OwnCounter = OwnCounter - 0.5
|
|
|
# 3.判断右上-左下是否连续四子
|
|
|
for x in range(15):
|
|
|
for y in range(4, 15):
|
|
|
if chess.ChessData[x][y]['Cstate'] == 1 and chess.ChessData[x+1][y-1]['Cstate'] == 1 and chess.ChessData[x+2][y-2]['Cstate'] == 1 and \
|
|
|
chess.ChessData[x + 3][y - 3]['Cstate'] == 1 and chess.ChessData[x + 4][y- 4]['Cstate'] == 0 and chess.ChessData[x-1][y+1]['Cstate'] == 0:
|
|
|
OwnCounter = OwnCounter - 0.5
|
|
|
# 4.判断左上-右下是否连续四子
|
|
|
for x in range(15):
|
|
|
for y in range(15):
|
|
|
if chess.ChessData[x][y]['Cstate'] == 1 and chess.ChessData[x+1][y+1]['Cstate'] == 1 and chess.ChessData[x+2][y+2]['Cstate'] == 1 and \
|
|
|
chess.ChessData[x+3][y+3]['Cstate'] == 1 and chess.ChessData[x+4][y+4]['Cstate'] == 0 and chess.ChessData[x-1][y-1]['Cstate'] == 0:
|
|
|
OwnCounter = OwnCounter - 0.5
|
|
|
OtherCouter=1-OwnCounter
|
|
|
msg = '当前落子方的胜率为'+str(round(OwnCounter, 4) *100)+'%!\n'+ \
|
|
|
'当前对方的胜率为' + str(round(OtherCouter, 4) * 100) + '%!'
|
|
|
tkinter.messagebox.showinfo(title='信息提示!', message=msg)
|
|
|
photocurnow = Image.open("image/局面评估.png").resize((100, 50))
|
|
|
photocurnow = ImageTk.PhotoImage(photocurnow)
|
|
|
btn_elevalue = Button(root, text='局面评估', font='Arial,12', width=85, height=35, image=photocurnow,
|
|
|
command=curnow,bd=0)
|
|
|
btn_elevalue.place(x=750, y=520)
|
|
|
def start_game_black():
|
|
|
if chess.Chess_Mode == 3:
|
|
|
chess.Chess_Mode = 1
|
|
|
chess.playing(1, root, canvas, blackch, whitech, photos1, photos2)
|
|
|
else:
|
|
|
chess.Chess_Mode = 1
|
|
|
refresh()
|
|
|
root.update()
|
|
|
photostart = Image.open("image/true.jpg").resize((100, 50))
|
|
|
photostart = ImageTk.PhotoImage(photostart)
|
|
|
btn_reset = Button(root, text='人机对战', font='Arial,12', width=85, height=35,image=photostart,
|
|
|
command=peocomwhite,bg='#FFA500',bd=0)
|
|
|
btn_reset.place(x=800,y=580)
|
|
|
# 按钮重新开始
|
|
|
def refresh():
|
|
|
canvas.create_image(750, 145, image=photos2, anchor=W)
|
|
|
canvas.create_image(870, 145, image=photos2, anchor=W)
|
|
|
photoqizi = Image.open("image/棋盘-空.png").resize((730, 700))
|
|
|
photoqizi = ImageTk.PhotoImage(photoqizi)
|
|
|
canvas.create_image(10, 350, image=photoqizi, anchor=W)
|
|
|
cross()
|
|
|
if chess.Ovalone != 0:
|
|
|
canvas.delete(chess.Ovalone)
|
|
|
for x in range(19):
|
|
|
for y in range(19):
|
|
|
if chess.ChessData[x][y]['Cstate'] != 0:
|
|
|
canvas.delete(chess.Chessimg[x][y])
|
|
|
if chess.Relabel[x][y] != 0:
|
|
|
chess.Relabel[x][y].destroy()
|
|
|
chess.ChessData[x][y]['Cstate'] = 0
|
|
|
chess.ChessData[x][y]['Cstep'] = 0
|
|
|
chess.Chessimg[x][y] = 0
|
|
|
chess.Relabel[x][y] = 0
|
|
|
chess.Currently_step = 0
|
|
|
chess.Gameover = 0
|
|
|
chess.Depth = 0
|
|
|
chess.player = 0 # 轮到下棋的标志,1=下,0=不下
|
|
|
chess.computer = 0 # 轮到下棋的标志,1=下,0=不下
|
|
|
chess.myColor = 0 # 玩家选择的棋子颜色
|
|
|
chess.computercolor = 0 # 电脑的棋子颜色
|
|
|
chess.player2color = 0 # 玩家2的棋子颜色
|
|
|
if chess.WinFLAG == 1:
|
|
|
chess.resultshow(root,canvas,whitech,blackch)
|
|
|
|
|
|
if chess.Chess_Mode == 1 :
|
|
|
chess.playing(1, root, canvas,blackch,whitech,photos1,photos2)
|
|
|
# chess.curlocation(root, player=1, computer=0,Current_Player=1)
|
|
|
elif chess.Chess_Mode == 2:
|
|
|
chess.playing(2, root, canvas,blackch,whitech,photos1,photos2)
|
|
|
elif chess.Chess_Mode == 0:
|
|
|
chess.playing(0, root, canvas,blackch,whitech,photos1,photos2)
|
|
|
# chess.curlocation(root, player=1, computer=1,Current_Player=2)
|
|
|
if chess.Ovalone != 0:
|
|
|
canvas.delete(chess.Ovalone)
|
|
|
chess.WinFLAG = 0
|
|
|
root.update()
|
|
|
photorefresh = Image.open("image/重新开始.png").resize((100, 50))
|
|
|
photorefresh = ImageTk.PhotoImage(photorefresh)
|
|
|
btn_reset = Button(root, text='重新开始', font='Arial,12', width=85, height=35,
|
|
|
image=photorefresh,command=refresh,bg='#FFA500',bd=0)
|
|
|
btn_reset.place(x=850,y=520)
|
|
|
|