From 46afe8e6d15c00a94d177a7eeef46f00694e6e2b Mon Sep 17 00:00:00 2001 From: bettleChen <2207153529@qq.com> Date: Thu, 11 Jan 2024 13:57:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AE=9E=E8=AE=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chess.py | 653 ++++++++++++++++++++++--------- chessboard.py | 1021 ++++++++++++++++++++++++++++++++++++++----------- data/1.txt | 41 ++ data/2.txt | 41 ++ data/3.txt | 41 ++ data/4.txt | 41 ++ data/5.txt | 41 ++ data/7.txt | 41 ++ main.sh | 1 + 9 files changed, 1505 insertions(+), 416 deletions(-) create mode 100644 data/1.txt create mode 100644 data/2.txt create mode 100644 data/3.txt create mode 100644 data/4.txt create mode 100644 data/5.txt create mode 100644 data/7.txt create mode 100644 main.sh diff --git a/chess.py b/chess.py index 7133569..bc187b5 100644 --- a/chess.py +++ b/chess.py @@ -1,49 +1,56 @@ import random - -from chessboard import * -from PIL import Image,ImageTk +from PIL import Image, ImageTk import math import random import time +from tkinter import W -Chessimgx=0 #玩家落子的横坐标 -Chessimgy=0 #玩家落子的纵坐标 -xregretFLAG = [0 for x in range(365)] #悔棋的数组x坐标 -yregretFLAG = [0 for y in range(365)] #悔棋的数组y坐标 - - +Chessimgx = 0 # 玩家落子的横坐标 +Chessimgy = 0 # 玩家落子的纵坐标 +xregretFLAG = [0 for x in range(365)] # 悔棋的数组x坐标 +yregretFLAG = [0 for y in range(365)] # 悔棋的数组y坐标 class Chess: def __init__(self): - self.ChessData = [[{"Cstate": 0, "Cstep": 0} for j in range(19)] for i in range(19)]#Cstate=0无棋子,1有黑棋子,2有白棋子;Cstep为该棋子落子的步骤 - #self.chess_state = [[0 for y in range(19)] for x in range(19)] # 15x15的大小,存储棋子的状态,0为没有棋子,1为白色棋子,2为黑色棋子 - #self.chess_step = [[0 for y in range(19)] for x in range(19)] # 15x15的大小,存储当前棋子的步骤 - self.Currently_step = 0 #棋子当前进行的步数 + self.ChessData = [[{"Cstate": 0, "Cstep": 0} for j in range(19)] for i in + range(19)] # Cstate=0无棋子,1有黑棋子,2有白棋子;Cstep为该棋子落子的步骤 + # self.chess_state = [[0 for y in range(19)] for x in range(19)] # 15x15的大小,存储棋子的状态,0为没有棋子,1为白色棋子,2为黑色棋子 + # self.chess_step = [[0 for y in range(19)] for x in range(19)] # 15x15的大小,存储当前棋子的步骤 + self.Currently_step = 0 # 棋子当前进行的步数 self.Chessimg = [[0 for y in range(19)] for x in range(19)] - self.Relabel = [[0 for y in range(19)] for x in range(19)] #复盘的标签 + self.Relabel = [[0 for y in range(19)] for x in range(19)] # 复盘的标签 # self.promptBoxFlag = [[[0 for z in range(8)] for y in range(15)] for x in range(15)] self.Score = [[0 for y in range(19)] for x in range(19)] # 初始化计分数组 self.Max_Score = 0 self.Counts = 0 self.New_count = 0 - self.Ovalone = 0 #代表棋子 - self.WinFLAG = 0 #判断是否取得胜利 - self.Gameover = 0 #判断游戏是否结束 - self.Depth = 0 #搜索的深度 - self.player = 0 #轮到下棋的标志,1=下,0=不下 - self.computer = 0 #轮到下棋的标志,1=下,0=不下 - self.Current_Player = 1 #(=1,黑方;=2,白方) + self.Ovalone = 0 # 代表棋子 + self.Ovalone_new = [] + self.WinFLAG = 0 # 判断是否取得胜利 + self.Gameover = 0 # 判断游戏是否结束 + self.Depth = 0 # 搜索的深度 + self.player = 0 # 轮到下棋的标志,1=下,0=不下 + self.computer = 0 # 轮到下棋的标志,1=下,0=不下 + self.Current_Player = 1 # (=1,黑方;=2,白方) self.myColor = 0 # 玩家选择的棋子颜色 self.computercolor = 0 # 电脑的棋子颜色 - self.Chess_Mode = 3#对弈方式标志 Chess_Mode(=0,人-人对弈; =1, 人-机白对弈; =2,机黑-人对弈) - self.player2color = 0 #玩家2的棋子颜色 - self.showHistory=False #当为False时,不展示下棋的步骤,当为True时展示下棋的步骤 + self.Chess_Mode = 3 # 对弈方式标志 Chess_Mode(=0,人-人对弈; =1, 人-机白对弈; =2,机黑-人对弈, =4,测试模式) + self.player2color = 0 # 玩家2的棋子颜色 + self.showHistory = False # 当为False时,不展示下棋的步骤,当为True时展示下棋的步骤 + self.Max_Score_pos = () # self.choice = 0 + def game_end(self): + if self.Gameover == 1 or self.WinFLAG == 1: + return True + else: + return False - def count_Score(self,x, y, computerColor): + # 计算某个位置得分 + def count_Score(self, x, y, computerColor): # global sum,count,value + global s sum = 0 # count = 0 value = [0, 0, 0, 0] @@ -51,10 +58,11 @@ class Chess: downcount = [0, 0, 0, 0] # 右、下、右斜向下、右斜向上 棋子数 upflag = [0, 0, 0, 0] # 左、上、左斜上、左斜下 空格数 downflag = [0, 0, 0, 0] # 右、下、右斜向下、右斜向上 空格数 + message = '' for color in range(1, 3): self.ChessData[x][y]['Cstate'] = color # 假设该点为白色棋子 - for i in range(x-1, -1, -1): # 计算左边棋子数量 upcount[0]表示左侧同类棋子 用upflag[0] 记录 + for i in range(x - 1, -1, -1): # 计算左边棋子数量 upcount[0]表示左侧同类棋子 用upflag[0] 记录 if self.ChessData[i][y]['Cstate'] == color: upcount[0] += 1 # 左侧有自己棋子 elif self.ChessData[i][y]['Cstate'] != 0 and self.ChessData[i][y]['Cstate'] != color: # 左侧有对方棋子 @@ -62,11 +70,11 @@ class Chess: break elif self.ChessData[i][y]['Cstate'] == 0: # 左侧没有棋子 upflag[0] = 1 - if i - 1 >= 0 and self.ChessData[i-1][y]['Cstate'] == 0: + if i - 1 >= 0 and self.ChessData[i - 1][y]['Cstate'] == 0: upflag[0] = 2 # 表示有两个空格 else: break - if i - 2 >= 0 and self.ChessData[i-2][y]['Cstate'] == 0: + if i - 2 >= 0 and self.ChessData[i - 2][y]['Cstate'] == 0: upflag[0] = 3 # 表示有三个空格 else: break @@ -79,11 +87,11 @@ class Chess: break elif self.ChessData[i][y]['Cstate'] == 0: # 右侧没有棋子 downflag[0] = 1 - if i + 1 < 19 and self.ChessData[i+1][y]['Cstate'] == 0: + if i + 1 < 19 and self.ChessData[i + 1][y]['Cstate'] == 0: downflag[0] = 2 # 表示有两个空格 else: break - if i + 2 < 19 and self.ChessData[i+2][y]['Cstate'] == 0: + if i + 2 < 19 and self.ChessData[i + 2][y]['Cstate'] == 0: downflag[0] = 3 # 表示有三个空格 else: break @@ -96,11 +104,11 @@ class Chess: break elif self.ChessData[x][i]['Cstate'] == 0: # 上没有棋子 upflag[1] = 1 - if i - 1 >= 0 and self.ChessData[x][i-1]['Cstate'] == 0: + if i - 1 >= 0 and self.ChessData[x][i - 1]['Cstate'] == 0: upflag[1] = 2 # 表示上有两个空格 else: break - if i - 2 >= 0 and self.ChessData[x][i-2]['Cstate'] == 0: + if i - 2 >= 0 and self.ChessData[x][i - 2]['Cstate'] == 0: upflag[1] = 3 # 表示上有三个空格 else: break @@ -113,34 +121,35 @@ class Chess: break elif self.ChessData[x][i]['Cstate'] == 0: # 下侧没有棋子 downflag[1] = 1 - if i + 1 <19 and self.ChessData[x][i+1]['Cstate'] == 0: + if i + 1 < 19 and self.ChessData[x][i + 1]['Cstate'] == 0: downflag[1] = 2 # 表示下有两个空格 else: break - if i + 2 <19 and self.ChessData[x][i+2]['Cstate'] == 0: + if i + 2 < 19 and self.ChessData[x][i + 2]['Cstate'] == 0: downflag[1] = 3 # 表示下有三个空格 else: break break - j=1 + j = 1 for i in range(x - 1, -1, -1): # // 计算左斜向上 upcount[2]表示左斜向上同类棋子 用upflag[2]记录 # for j in range(y - 1, -1, -1): - if y-j>=0 and self.ChessData[i][y-j]['Cstate'] == color: + if y - j >= 0 and self.ChessData[i][y - j]['Cstate'] == color: upcount[2] += 1 # 左斜向上有自己棋子 - j+=1 - elif y-j>=0 and self.ChessData[i][y-j]['Cstate'] != 0 and self.ChessData[i][y-j]['Cstate'] != color: # 左斜向上有对方棋子 + j += 1 + elif y - j >= 0 and self.ChessData[i][y - j]['Cstate'] != 0 and self.ChessData[i][y - j][ + 'Cstate'] != color: # 左斜向上有对方棋子 upflag[2] = -1 j += 1 break - elif y-j>=0 and self.ChessData[i][y-j]['Cstate'] == 0: # 左斜向上没有棋子 + elif y - j >= 0 and self.ChessData[i][y - j]['Cstate'] == 0: # 左斜向上没有棋子 upflag[2] = 1 - if i - 1 >= 0 and y-j-1 >= 0 and self.ChessData[i-1][y-j-1]['Cstate'] == 0: + if i - 1 >= 0 and y - j - 1 >= 0 and self.ChessData[i - 1][y - j - 1]['Cstate'] == 0: upflag[2] = 2 # 表示有两个空格 j += 1 else: j += 1 break - if i - 2 >= 0 and y-j- 2 >= 0 and self.ChessData[i-2][y-j-2]['Cstate'] == 0: + if i - 2 >= 0 and y - j - 2 >= 0 and self.ChessData[i - 2][y - j - 2]['Cstate'] == 0: upflag[2] = 3 # 表示有三个空格 j += 1 else: @@ -150,22 +159,23 @@ class Chess: break j = 1 for i in range(x + 1, 19): # // 计算右斜向下 downcount[2]表示右斜向下同类棋子 用downflag[2]记录 - if y+j<19 and self.ChessData[i][y+j]['Cstate'] == color: + if y + j < 19 and self.ChessData[i][y + j]['Cstate'] == color: downcount[2] += 1 # 右斜向下有自己棋子 j += 1 - elif y+j<19 and self.ChessData[i][y+j]['Cstate'] != 0 and self.ChessData[i][y+j]['Cstate'] != color: # 右斜向下有对方棋子 + elif y + j < 19 and self.ChessData[i][y + j]['Cstate'] != 0 and self.ChessData[i][y + j][ + 'Cstate'] != color: # 右斜向下有对方棋子 downflag[2] = -1 j += 1 break - elif y+j<19 and self.ChessData[i][y+j]['Cstate'] == 0: # 右斜向下没有棋子 + elif y + j < 19 and self.ChessData[i][y + j]['Cstate'] == 0: # 右斜向下没有棋子 downflag[2] = 1 - if i + 1 < 19 and y+j + 1 < 19 and self.ChessData[i+1][y+j+1]['Cstate'] == 0: + if i + 1 < 19 and y + j + 1 < 19 and self.ChessData[i + 1][y + j + 1]['Cstate'] == 0: downflag[2] = 2 # 表示有两个空格 j += 1 else: j += 1 break - if i + 2 < 19 and y+j + 2 < 19 and self.ChessData[i+2][y+j+2]['Cstate'] == 0: + if i + 2 < 19 and y + j + 2 < 19 and self.ChessData[i + 2][y + j + 2]['Cstate'] == 0: downflag[2] = 3 # 表示有三个空格 j += 1 else: @@ -173,25 +183,26 @@ class Chess: break j += 1 break - j=1 + j = 1 for i in range(x + 1, 19): # // 计算右斜向上 downcount[3]表示右斜向上同类棋子 用downflag[3]记录 # for j in range(y - 1, -1, -1): - if y-j>=0 and self.ChessData[i][y-j]['Cstate'] == color: + if y - j >= 0 and self.ChessData[i][y - j]['Cstate'] == color: downcount[3] += 1 # 右斜向上有自己棋子 - j +=1 - elif y-j>=0 and self.ChessData[i][y-j]['Cstate'] != 0 and self.ChessData[i][y-j]['Cstate'] != color: # 右斜向上有对方棋子 + j += 1 + elif y - j >= 0 and self.ChessData[i][y - j]['Cstate'] != 0 and self.ChessData[i][y - j][ + 'Cstate'] != color: # 右斜向上有对方棋子 downflag[3] = -1 j += 1 break - elif y-j>=0 and self.ChessData[i][y-j]['Cstate'] == 0: # 右斜向上没有棋子 + elif y - j >= 0 and self.ChessData[i][y - j]['Cstate'] == 0: # 右斜向上没有棋子 downflag[3] = 1 - if i + 1 < 19 and y-j-1 >= 0 and self.ChessData[i][y-j]['Cstate'] == 0: + if i + 1 < 19 and y - j - 1 >= 0 and self.ChessData[i][y - j]['Cstate'] == 0: downflag[3] = 2 # 表示有两个空格 j += 1 else: j += 1 break - if i + 2 < 19 and y-j-2 >= 0 and self.ChessData[i][y-j]['Cstate'] == 0: + if i + 2 < 19 and y - j - 2 >= 0 and self.ChessData[i][y - j]['Cstate'] == 0: downflag[3] = 3 # 表示有三个空格 j += 1 else: @@ -199,25 +210,26 @@ class Chess: break j += 1 break - j=1 + j = 1 for i in range(x - 1, -1, -1): # 计算左斜向下 upcount[3]表示左斜向下同类棋子 用upflag[3]记录 # for j in range(y + 1, 15): - if y+j<19 and self.ChessData[i][j+y]['Cstate'] == color: + if y + j < 19 and self.ChessData[i][j + y]['Cstate'] == color: upcount[3] += 1 # 左斜向下有自己棋子 - j+=1 - elif y+j<19 and self.ChessData[i][j+y]['Cstate'] != 0 and self.ChessData[i][j+y]['Cstate'] != color: # 左斜向下有对方棋子 + j += 1 + elif y + j < 19 and self.ChessData[i][j + y]['Cstate'] != 0 and self.ChessData[i][j + y][ + 'Cstate'] != color: # 左斜向下有对方棋子 upflag[3] = -1 - j+=1 + j += 1 break - elif y+j<19 and self.ChessData[i][j+y]['Cstate'] == 0: # 左斜向下没有棋子 + elif y + j < 19 and self.ChessData[i][j + y]['Cstate'] == 0: # 左斜向下没有棋子 upflag[3] = 1 - if i - 1 >= 0 and j+y+1 < 19 and self.ChessData[i][j+y]['Cstate'] == 0: + if i - 1 >= 0 and j + y + 1 < 19 and self.ChessData[i][j + y]['Cstate'] == 0: upflag[3] = 2 # 表示有两个空格 j += 1 else: j += 1 break - if i - 2 >= 0 and j+y+2 < 19 and self.ChessData[i-1][j+y+1]['Cstate'] == 0: + if i - 2 >= 0 and j + y + 2 < 19 and self.ChessData[i - 1][j + y + 1]['Cstate'] == 0: upflag[3] = 3 # 表示有三个空格 j += 1 else: @@ -225,82 +237,94 @@ class Chess: break j += 1 break + + if computerColor == self.ChessData[x][y]['Cstate']: # 数据处理,如果是电脑方的话分数要高一点 for i in range(4): count = upcount[i] + downcount[i] + 1 if count == 5: # 成五 - value[i] = 40000 + message += '五子连珠,得分加4000!\n' + value[i] = 4000 elif count == 4: if upflag[i] >= 1 and downflag[i] >= 1: # 活四 - value[i] = 19000 + message += '四子连珠且有两端未被对方封堵,得分加1900!\n' + value[i] = 1900 if (upflag[i] >= 1 and downflag[i] == -1) or (upflag[i] == -1 and downflag[i] >= 1): # 眠四 - value[i] = 3000 + message += '四子连珠且有一端未被对方封堵,得分加300!\n' + value[i] = 300 if upflag[i] == -1 and downflag[i] == -1: # 死四 - value[i] = -50 + message += '四子连珠但两端都被封堵,得分减5!\n' + value[i] = -5 elif count == 3: if (upflag[i] >= 2 and downflag[i] >= 1) or (upflag[i] >= 1 and downflag[i] >= 2): # 活三 - value[i] = 4000 + message += '三子连珠且有两端未被对方封堵,得分加400!\n' + value[i] = 400 if (upflag[i] >= 2 and downflag[i] == -1) or (upflag[i] == -1 and downflag[i] >= 2) or ( upflag[i] == 1 and downflag[i] == 1): # 眠三 - value[i] = 800 + message += '三子连珠且有有一端未被对方封堵,得分加80!\n' + value[i] = 80 if upflag[i] == -1 and downflag[i] == -1: # 死三 - value[i] = -50 + message += '三子连珠但两端都被封堵,得分减5!\n' + value[i] = -5 elif count == 2: if (upflag[i] >= 3 and downflag[i] >= 1) or (upflag[i] >= 1 and downflag[i] >= 3) or ( upflag[i] >= 2 and downflag[i] >= 2): # 活二 - value[i] = 1050 + message += '二子连珠且有两端未被对方封堵,得分加105!\n' + value[i] = 105 if (upflag[i] >= 3 and downflag[i] == -1) or (upflag[i] == -1 and downflag[i] >= 3) or ( upflag[i] == 2 and downflag[i] == 1) or (upflag[i] == 1 and downflag[i] == 2): # 眠三 - value[i] = 350 + message += '二子连珠且有有一端未被对方封堵,得分加35!\n' + value[i] = 35 if upflag[i] == -1 and downflag[i] == -1: # 死二 - value[i] = -50 + message += '二子连珠但两端都被封堵,得分减5!\n' + value[i] = -5 else: if (upflag[i] >= 3 and downflag[i] >= 2) or (upflag[i] >= 2 and downflag[i] >= 3): # 活一 - value[i] = 80 + value[i] = 8 if (upflag[i] == 2 and downflag[i] == 2) or (upflag[i] == 1 and downflag[i] == 3) or ( upflag[i] == 3 and downflag[i] == 1): # 眠一 - value[i] = 20 + value[i] = 2 if (upflag[i] <= 1 and downflag[i] <= 2) or (upflag[i] <= 2 and downflag[i] <= 1): # 死一 - value[i] = -50 + value[i] = -5 else: for i in range(4): count = upcount[i] + downcount[i] + 1 if count == 5: # 成五 - value[i] = 30000 + value[i] = 3000 elif count == 4: if upflag[i] >= 1 and downflag[i] >= 1: # 活四 - value[i] = 15000 + value[i] = 1500 if (upflag[i] >= 1 and downflag[i] == -1) or (upflag[i] == -1 and downflag[i] >= 1): # 眠四 - value[i] = 2500 + value[i] = 250 if upflag[i] == -1 and downflag[i] == -1: # 死四 - value[i] = -50 + value[i] = -5 elif count == 3: if (upflag[i] >= 2 and downflag[i] >= 1) or (upflag[i] >= 1 and downflag[i] >= 2): # 活三 - value[i] = 3000 + value[i] = 300 if (upflag[i] >= 2 and downflag[i] == -1) or (upflag[i] == -1 and downflag[i] >= 2) or ( upflag[i] == 1 and downflag[i] == 1): # 眠三 - value[i] = 500 + value[i] = 50 if upflag[i] == -1 and downflag[i] == -1: # 死三 - value[i] = -50 + value[i] = -5 elif count == 2: if (upflag[i] >= 3 and downflag[i] >= 1) or (upflag[i] >= 1 and downflag[i] >= 3) or ( upflag[i] >= 2 and downflag[i] >= 2): # 活二 - value[i] = 650 + value[i] = 65 if (upflag[i] >= 3 and downflag[i] == -1) or (upflag[i] == -1 and downflag[i] >= 3) or ( upflag[i] == 2 and downflag[i] == 1) or (upflag[i] == 1 and downflag[i] == 2): # 眠二 - value[i] = 150 + value[i] = 15 if (upflag[i] == -1 and downflag[i] == -1) or (upflag[i] == 1 and downflag[i] == 1) or ( upflag[i] == -1 and downflag[i] == 2) or (upflag[i] == 2 and downflag[i] == -1): # 死二 - value[i] = -50 + value[i] = -5 else: if (upflag[i] >= 3 and downflag[i] >= 2) or (upflag[i] >= 2 and downflag[i] >= 3): # 活一 - value[i] = 50 + value[i] = 5 if (upflag[i] == 2 and downflag[i] == 2) or (upflag[i] == 1 and downflag[i] == 3) or ( upflag[i] == 3 and downflag[i] == 1): # 眠一 - value[i] = 10 + value[i] = 1 if (upflag[i] <= 1 and downflag[i] <= 2) or (upflag[i] <= 2 and downflag[i] <= 1) or ( upflag[i] <= 3 and downflag[i] == -1) or (upflag[i] == -1 and downflag[i] <= 3): # 死三 - value[i] = -50 + value[i] = -5 for i in range(4): sum += value[i] value[i] = 0 @@ -309,9 +333,9 @@ class Chess: upflag[i] = 0 downflag[i] = 0 self.ChessData[x][y]['Cstate'] = 0 - return sum + return sum, message - def return_chess(self,Depth, computercolor): + def return_chess(self, Depth, computercolor): time.sleep(0.5) self.New_count = 0 self.Max_Score = 0 @@ -319,27 +343,63 @@ class Chess: pos = [[0 for y in range(2)] for x in range(10)] evaFLAG = 0 position = [0, 0] # 初始化位置坐标数组 - for i in range(8 - Depth,11+Depth): - for j in range(8 - Depth, 11+Depth): - if 0<=i<19 and 0<=j<19: + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if 0 <= i < 19 and 0 <= j < 19: + if self.ChessData[i][j]['Cstate'] == 0: + self.Score[i][j],s = self.count_Score(i, j, computercolor) + self.New_count += 1 + if self.Score[i][j] > 0: + self.Counts += 1 + if self.Max_Score < self.Score[i][j]: + self.Max_Score = self.Score[i][j] # 记录当前棋盘分数的最大值 + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if self.ChessData[i][j]['Cstate'] != 0: + self.Score[i][j] = self.ChessData[i][j]['Cstate'] + if i < 19 and j < 19 and self.Score[i][j] == self.Max_Score and self.ChessData[i][j]['Cstate'] == 0: + pos[evaFLAG][0] = i + pos[evaFLAG][1] = j + evaFLAG += 1 + m = random.randint(0, evaFLAG - 1) + position[0] = pos[m][0] + position[1] = pos[m][1] + return position + + # 落子建议下一步, + def return_chess_new(self, Depth, computercolor): + time.sleep(0.5) + self.New_count = 0 + self.Max_Score = 0 + self.Counts = 0 + pos = [[0 for y in range(2)] for x in range(10)] + evaFLAG = 0 + position = [0, 0] # 初始化位置坐标数组 + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if 0 <= i < 19 and 0 <= j < 19: if self.ChessData[i][j]['Cstate'] == 0: self.Score[i][j] = self.count_Score(i, j, computercolor) self.New_count += 1 - if self.Score[i][j]>0: + if self.Score[i][j] > 0: self.Counts += 1 if self.Max_Score < self.Score[i][j]: self.Max_Score = self.Score[i][j] # 记录当前棋盘分数的最大值 - for i in range(8 - Depth,11+Depth): - for j in range(8 - Depth,11+Depth): - if i<19 and j<19 and self.Score[i][j] == self.Max_Score and self.ChessData[i][j]['Cstate'] == 0: + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if self.ChessData[i][j]['Cstate'] != 0: + self.Score[i][j] = self.ChessData[i][j]['Cstate'] + if i < 19 and j < 19 and self.Score[i][j] >= self.Max_Score - 5000 and self.ChessData[i][j][ + 'Cstate'] == 0: pos[evaFLAG][0] = i pos[evaFLAG][1] = j - evaFLAG +=1 - m = random.randint(0, evaFLAG-1) + evaFLAG += 1 + m = random.randint(0, evaFLAG - 1) position[0] = pos[m][0] position[1] = pos[m][1] return position + # 判断玩家点击的位置是否可以落子,如果可以落子,则进行self.make_move()移动,返回self.make_move()的值,否则返回False def player_location(self, playerx, playery, canvas, blackch, whitech): if 0 <= playerx <= 706 and 0 <= playery <= 640: m = (playerx - 40) / 37 @@ -354,13 +414,35 @@ class Chess: pointj = intn + 1 else: pointj = intn - print(pointi, pointj) + # print(pointi, pointj) if 0 <= pointi <= 18 and 0 <= pointj <= 18: - print("1:%d" % self.Currently_step) + # print("1:%d" % self.Currently_step) return self.make_move(pointi, pointj, canvas, blackch, whitech) - print("2:%d" % self.Currently_step) + # print("2:%d" % self.Currently_step) return False + def get_player_move_location_xy(self, playerx, playery, canvas, blackch, whitech): + if 0 <= playerx <= 706 and 0 <= playery <= 640: + m = (playerx - 40) / 37 + n = (playery - 21) / 34 + intm = int(m) + intn = int(n) + if m - intm >= 0.5 or m - intm <= -0.5: + pointi = intm + 1 + else: + pointi = intm + if n - intn >= 0.5 or n - intn <= -0.5: + pointj = intn + 1 + else: + pointj = intn + # print(pointi, pointj) + if 0 <= pointi <= 18 and 0 <= pointj <= 18: + # print("1:%d" % self.Currently_step) + return self.make_move(pointi, pointj, canvas, blackch, whitech),pointi,pointj + # print("2:%d" % self.Currently_step) + return self.make_move(pointi, pointj, canvas, blackch, whitech),pointi,pointj + + # def make_move(self, pointi, pointj, canvas, blackch, whitech): if self.ChessData[pointi][pointj]['Cstate'] == 0 and self.myColor == 2 and self.Currently_step % 2 == 0: Chessimgx = pointi * 36.8 + 41 @@ -415,11 +497,13 @@ class Chess: y += y_offset if 0 <= x < 18 and 0 <= y < 18: if Cflag == 2: - if self.ChessData[y][x]['Cstate'] == 2 and self.myColor == 2: #) or (self.ChessData[y][x]['Cstate'] == 1 and self.myColor == 1) + if self.ChessData[y][x][ + 'Cstate'] == 2 and self.myColor == 2: # ) or (self.ChessData[y][x]['Cstate'] == 1 and self.myColor == 1) nowcount += 1 if space is False: space = True - elif self.ChessData[y][x]['Cstate'] == 1 and self.myColor == 2: #) or (self.ChessData[y][x]['Cstate'] == 1 and self.myColor == 1) + elif self.ChessData[y][x][ + 'Cstate'] == 1 and self.myColor == 2: # ) or (self.ChessData[y][x]['Cstate'] == 1 and self.myColor == 1) _both += 1 break else: @@ -460,11 +544,11 @@ class Chess: y -= y_offset if 0 <= x < 18 and 0 <= y < 18: if _flag == 2: - if self.ChessData[y][x]['Cstate'] == 2 and self.myColor ==2: + if self.ChessData[y][x]['Cstate'] == 2 and self.myColor == 2: nowcount += 1 if space is False: space = True - elif self.ChessData[y][x]['Cstate'] == 1 and self.myColor ==2: + elif self.ChessData[y][x]['Cstate'] == 1 and self.myColor == 2: _both += 1 break else: @@ -473,10 +557,10 @@ class Chess: else: break # 遇到第二个空格退出 elif _flag == 1: - if self.ChessData[y][x]['Cstate'] == 2 and self.myColor ==1: + if self.ChessData[y][x]['Cstate'] == 2 and self.myColor == 1: _both += 1 break - elif self.ChessData[y][x]['Cstate'] == 1 and self.myColor ==1: + elif self.ChessData[y][x]['Cstate'] == 1 and self.myColor == 1: _nowcount += 1 if _space is False: _space = True @@ -547,7 +631,7 @@ class Chess: for i in range(0, 18): for j in range(0, 18): if self.ChessData[i][j]['Cstate'] == 0: - Score = self.get_point_Score(i,j) + Score = self.get_point_Score(i, j) if Score > _Score: position[0] = i position[1] = j @@ -559,67 +643,86 @@ class Chess: position[1] = j return position - def ai_location(self, canvas, blackch,whitech): + # 设置的策略ai落子 + def ai_location(self, canvas, blackch, whitech): if self.Depth >= 9: self.Depth = 8 - position = self.return_chess(self.Depth,self.computercolor) #调用估值函数 + position = self.return_chess(self.Depth, self.computercolor) # 调用估值函数 # position = self.atuoactive() if self.ChessData[position[0]][position[1]]['Cstate'] == 0: - print("3:%d"%self.Currently_step) - if self.computercolor == 2 and self.Currently_step %2 ==0: + # print("3:%d"%self.Currently_step) + if self.computercolor == 2 and self.Currently_step % 2 == 0: self.ChessData[position[0]][position[1]]['Cstate'] = 2 - oval = canvas.create_image(position[0]* 36.8 + 41 - 14, position[1]* 34.6 + 20, image=blackch, anchor=W) + oval = canvas.create_image(position[0] * 36.8 + 41 - 14, position[1] * 34.6 + 20, image=blackch, + anchor=W) self.Chessimg[position[0]][position[1]] = oval - self.ChessData[position[0]][position[1]]['Cstep']= self.Currently_step - elif self.computercolor==1 and self.Currently_step %2 !=0: + self.ChessData[position[0]][position[1]]['Cstep'] = self.Currently_step + elif self.computercolor == 1 and self.Currently_step % 2 != 0: self.ChessData[position[0]][position[1]]['Cstate'] = 1 - oval = canvas.create_image(position[0]* 36.8 + 41 - 14, position[1]* 34.6 + 20, image=whitech, anchor=W) + oval = canvas.create_image(position[0] * 36.8 + 41 - 14, position[1] * 34.6 + 20, image=whitech, + anchor=W) self.Chessimg[position[0]][position[1]] = oval - self.ChessData[position[0]][position[1]]['Cstep']= self.Currently_step + self.ChessData[position[0]][position[1]]['Cstep'] = self.Currently_step xregretFLAG[self.Currently_step] = position[0] yregretFLAG[self.Currently_step] = position[1] self.player = 1 # 玩家下棋标志置0 self.computer = 0 # 电脑下棋标志置1 self.Currently_step += 1 - self.ChessData[position[0]][position[1]]['Cstep']= self.Currently_step - a = max(abs(position[0] - 9), abs(position[1] - 9)) #计算该点到中心的最大的距离 - self.Depth = max(self.Depth, a) #不断更新Depth的值 - print("4:%d" % self.Currently_step) + self.ChessData[position[0]][position[1]]['Cstep'] = self.Currently_step + a = max(abs(position[0] - 9), abs(position[1] - 9)) # 计算该点到中心的最大的距离 + self.Depth = max(self.Depth, a) # 不断更新Depth的值 + # print("4:%d" % self.Currently_step) def ChessCheck(self): - for x in range(15):# 1.判断x-轴是否连续五子 + for x in range(15): # 1.判断x-轴是否连续五子 for y in range(19): - if self.ChessData[x][y]['Cstate'] == 1 and self.ChessData[x + 1][y]['Cstate'] == 1 and self.ChessData[x + 2][y]['Cstate'] == 1 and self.ChessData[x + 3][y]['Cstate'] == 1 and self.ChessData[x + 4][y]['Cstate'] == 1: + if self.ChessData[x][y]['Cstate'] == 1 and self.ChessData[x + 1][y]['Cstate'] == 1 and \ + self.ChessData[x + 2][y]['Cstate'] == 1 and self.ChessData[x + 3][y]['Cstate'] == 1 and \ + self.ChessData[x + 4][y]['Cstate'] == 1: self.WinFLAG = 1 return 1 - if self.ChessData[x][y]['Cstate'] == 2 and self.ChessData[x + 1][y]['Cstate'] == 2 and self.ChessData[x + 2][y]['Cstate'] == 2 and self.ChessData[x + 3][y]['Cstate'] == 2 and self.ChessData[x + 4][y]['Cstate'] == 2: + if self.ChessData[x][y]['Cstate'] == 2 and self.ChessData[x + 1][y]['Cstate'] == 2 and \ + self.ChessData[x + 2][y]['Cstate'] == 2 and self.ChessData[x + 3][y]['Cstate'] == 2 and \ + self.ChessData[x + 4][y]['Cstate'] == 2: self.WinFLAG = 1 return 2 # 2.判断y-轴是否连续五子 for x in range(19): for y in range(15): - if self.ChessData[x][y]['Cstate'] == 1 and self.ChessData[x][y + 1]['Cstate'] == 1 and self.ChessData[x][y + 2]['Cstate'] == 1 and self.ChessData[x][y + 3]['Cstate'] == 1 and self.ChessData[x][y + 4]['Cstate'] == 1: + if self.ChessData[x][y]['Cstate'] == 1 and self.ChessData[x][y + 1]['Cstate'] == 1 and \ + self.ChessData[x][y + 2]['Cstate'] == 1 and self.ChessData[x][y + 3]['Cstate'] == 1 and \ + self.ChessData[x][y + 4]['Cstate'] == 1: self.WinFLAG = 1 return 1 - if self.ChessData[x][y]['Cstate'] == 2 and self.ChessData[x][y + 1]['Cstate'] == 2 and self.ChessData[x][y + 2]['Cstate'] == 2 and self.ChessData[x][y + 3]['Cstate'] == 2 and self.ChessData[x][y + 4]['Cstate'] == 2: + if self.ChessData[x][y]['Cstate'] == 2 and self.ChessData[x][y + 1]['Cstate'] == 2 and \ + self.ChessData[x][y + 2]['Cstate'] == 2 and self.ChessData[x][y + 3]['Cstate'] == 2 and \ + self.ChessData[x][y + 4]['Cstate'] == 2: self.WinFLAG = 1 return 2 # 3.判断右上-左下是否连续五子 for x in range(15): - for y in range(4,19): - if self.ChessData[x][y]['Cstate'] == 1 and self.ChessData[x + 1][y - 1]['Cstate'] == 1 and self.ChessData[x + 2][y - 2]['Cstate'] == 1 and self.ChessData[x + 3][y - 3]['Cstate'] == 1 and self.ChessData[x + 4][y - 4]['Cstate'] == 1: + for y in range(4, 19): + if self.ChessData[x][y]['Cstate'] == 1 and self.ChessData[x + 1][y - 1]['Cstate'] == 1 and \ + self.ChessData[x + 2][y - 2]['Cstate'] == 1 and self.ChessData[x + 3][y - 3]['Cstate'] == 1 and \ + self.ChessData[x + 4][y - 4]['Cstate'] == 1: self.WinFLAG = 1 return 1 - if self.ChessData[x][y]['Cstate'] == 2 and self.ChessData[x + 1][y - 1]['Cstate'] == 2 and self.ChessData[x + 2][y - 2]['Cstate']== 2 and self.ChessData[x + 3][y - 3]['Cstate'] == 2 and self.ChessData[x + 4][y - 4]['Cstate'] == 2: + if self.ChessData[x][y]['Cstate'] == 2 and self.ChessData[x + 1][y - 1]['Cstate'] == 2 and \ + self.ChessData[x + 2][y - 2]['Cstate'] == 2 and self.ChessData[x + 3][y - 3]['Cstate'] == 2 and \ + self.ChessData[x + 4][y - 4]['Cstate'] == 2: self.WinFLAG = 1 return 2 # 4.判断左上-右下是否连续五子 for x in range(15): for y in range(15): - if self.ChessData[x][y]['Cstate'] == 1 and self.ChessData[x + 1][y + 1]['Cstate'] == 1 and self.ChessData[x + 2][y + 2]['Cstate'] == 1 and self.ChessData[x + 3][y + 3]['Cstate'] == 1 and self.ChessData[x + 4][y + 4]['Cstate'] == 1: + if self.ChessData[x][y]['Cstate'] == 1 and self.ChessData[x + 1][y + 1]['Cstate'] == 1 and \ + self.ChessData[x + 2][y + 2]['Cstate'] == 1 and self.ChessData[x + 3][y + 3]['Cstate'] == 1 and \ + self.ChessData[x + 4][y + 4]['Cstate'] == 1: self.WinFLAG = 1 return 1 - if self.ChessData[x][y]['Cstate'] == 2 and self.ChessData[x + 1][y + 1]['Cstate'] == 2 and self.ChessData[x + 2][y + 2]['Cstate'] == 2 and self.ChessData[x + 3][y + 3]['Cstate'] == 2 and self.ChessData[x + 4][y + 4]['Cstate'] == 2: + if self.ChessData[x][y]['Cstate'] == 2 and self.ChessData[x + 1][y + 1]['Cstate'] == 2 and \ + self.ChessData[x + 2][y + 2]['Cstate'] == 2 and self.ChessData[x + 3][y + 3]['Cstate'] == 2 and \ + self.ChessData[x + 4][y + 4]['Cstate'] == 2: self.WinFLAG = 1 return 2 # 5.判断是否是平局 @@ -629,60 +732,61 @@ class Chess: if self.ChessData[x][y]['Cstate'] == 0: # 判断棋盘中是否有空位,有空位则非平局 break elif self.ChessData[x][y]['Cstate'] != 0: - avanum +=1 + avanum += 1 if avanum == 365: return 3 - def resultshow(self,root,canvas,whitech,blackch): - global photo,text + def resultshow(self, root, canvas, whitech, blackch): + global photo, text if self.WinFLAG == 1: if self.myColor == 1 and self.Gameover == 1 and self.computercolor == 2: - photo=canvas.create_image(800, 300, image=whitech, anchor=W) - text=canvas.create_text(850, 300, text='玩家 胜利', font='Arial,10', fill='#AE0000',anchor=W) + photo = canvas.create_image(800, 300, image=whitech, anchor=W) + text = canvas.create_text(850, 300, text='玩家 胜利', font='Arial,10', fill='#AE0000', anchor=W) elif self.myColor == 2 and self.Gameover == 2 and self.computercolor == 1: photo = canvas.create_image(800, 300, image=blackch, anchor=W) text = canvas.create_text(850, 300, text='玩家 胜利', font='Arial,10', fill='#AE0000', anchor=W) elif self.myColor == 2 and self.Gameover == 1 and self.computercolor == 1: - photo=canvas.create_image(800, 300, image=whitech, anchor=W) - text=canvas.create_text(850, 300, text='电脑 胜利', font='Arial,10', fill='#AE0000',anchor=W) + photo = canvas.create_image(800, 300, image=whitech, anchor=W) + text = canvas.create_text(850, 300, text='电脑 胜利', font='Arial,10', fill='#AE0000', anchor=W) elif self.myColor == 1 and self.Gameover == 2 and self.computercolor == 2: photo = canvas.create_image(800, 300, image=blackch, anchor=W) text = canvas.create_text(850, 300, text='电脑 胜利', font='Arial,10', fill='#AE0000', anchor=W) elif self.myColor == 2 and self.player2color == 1 and self.Gameover == 2: - photo=canvas.create_image(800, 300, image=blackch, anchor=W) - text=canvas.create_text(850, 300, text='玩家1 胜利', font='Arial,10', fill='#AE0000', anchor=W) - elif self.myColor == 1 and self.player2color ==2 and self.Gameover == 2: + photo = canvas.create_image(800, 300, image=blackch, anchor=W) + text = canvas.create_text(850, 300, text='玩家1 胜利', font='Arial,10', fill='#AE0000', anchor=W) + elif self.myColor == 1 and self.player2color == 2 and self.Gameover == 2: photo = canvas.create_image(800, 300, image=whitech, anchor=W) text = canvas.create_text(850, 300, text='玩家1 胜利', font='Arial,10', fill='#AE0000', anchor=W) elif self.myColor == 2 and self.player2color == 1 and self.Gameover == 1: - photo=canvas.create_image(800, 300, image=whitech, anchor=W) - text=canvas.create_text(850, 300, text='玩家2 胜利', font='Arial,10', fill='#AE0000',anchor=W) - elif self.myColor == 1 and self.player2color ==2 and self.Gameover == 1: + photo = canvas.create_image(800, 300, image=whitech, anchor=W) + text = canvas.create_text(850, 300, text='玩家2 胜利', font='Arial,10', fill='#AE0000', anchor=W) + elif self.myColor == 1 and self.player2color == 2 and self.Gameover == 1: photo = canvas.create_image(800, 300, image=blackch, anchor=W) text = canvas.create_text(850, 300, text='玩家2 胜利', font='Arial,10', fill='#AE0000', anchor=W) - elif self.Gameover != 1 or self.Gameover != 2 or self.Gameover != 3 : + elif self.Gameover != 1 or self.Gameover != 2 or self.Gameover != 3: canvas.delete(photo) canvas.delete(text) - elif self.Gameover == 3 and self.WinFLAG == 0 : - canvas.create_text(850, 300, text='平 局', font='Arial,10', fill='#AE0000',anchor=W) + elif self.Gameover == 3 and self.WinFLAG == 0: + canvas.create_text(850, 300, text='平 局', font='Arial,10', fill='#AE0000', anchor=W) + # def curlocation(self, canvas, player, computer, Current_Player, blackch, whitech, photos1, photos2): text_options = {'fill': 'white', 'font': 'Arial,9', 'anchor': W} image_positions = [(750, 145), (870, 145)] if player == 1 and computer == 0: if Current_Player == 1: - image_data = [(blackch, '玩家'),(whitech, '电脑')] + image_data = [(blackch, '玩家'), (whitech, '电脑')] else: - image_data = [(whitech, '玩家'),(blackch, '电脑')] + image_data = [(whitech, '玩家'), (blackch, '电脑')] elif player == 0 and computer == 1: if Current_Player == 1: - image_data = [(blackch, '玩家'),(whitech, '电脑')] + image_data = [(blackch, '玩家'), (whitech, '电脑')] else: - image_data = [(whitech, '玩家'),(blackch, '电脑')] + image_data = [(whitech, '玩家'), (blackch, '电脑')] elif player == 1 and computer == 1: - image_data = [(blackch, '玩家1'),(whitech, '玩家2')] + image_data = [(blackch, '玩家1'), (whitech, '玩家2')] else: - image_data = [(blackch, '玩家1'),(whitech, '玩家2')] + image_data = [(blackch, '玩家1'), (whitech, '玩家2')] for i, (image, text) in enumerate(image_data): canvas.create_image(*image_positions[i], image=photos1, anchor=W) canvas.create_image(*image_positions[i], image=image, anchor=W) @@ -698,15 +802,16 @@ class Chess: canvas.delete(canvas.find_closest(position[0] + 50, 150)) canvas.update() - def playing(self,Chess_Mode,root,canvas,blackch,whitech,photos1,photos2): + + def playing(self, Chess_Mode, root, canvas, blackch, whitech, photos1, photos2): if Chess_Mode == 1: self.myColor = 2 # 玩家选择的棋子颜色 黑 self.computercolor = 1 # 电脑的棋子颜色 白 self.player = 1 self.computer = 0 self.WinFLAG = 0 - self.curlocation(canvas, 1, 0,1,blackch,whitech,photos1,photos2) - elif Chess_Mode ==2: + self.curlocation(canvas, 1, 0, 1, blackch, whitech, photos1, photos2) + elif Chess_Mode == 2: self.myColor = 1 # 玩家选择的棋子颜色 self.computercolor = 2 # 电脑的棋子颜色 self.ChessData[9][9]['Cstate'] = 2 @@ -719,73 +824,239 @@ class Chess: self.Currently_step += 1 self.WinFLAG = 0 self.ChessData[9][9]['Cstep'] = self.Currently_step - a = max(abs(9 - 9), abs(9 - 9)) # 计算该点到中心的最大的距离 - self.Depth = max(self.Depth, a) # 不断更新Depth的值 - self.curlocation(canvas, 1, 0,2,blackch,whitech,photos1,photos2) - elif Chess_Mode ==0: + a = max(abs(9 - 9), abs(9 - 9)) # 计算该点到中心的最大的距离 + self.Depth = max(self.Depth, a) # 不断更新Depth的值 + self.curlocation(canvas, 1, 0, 2, blackch, whitech, photos1, photos2) + elif Chess_Mode == 0: self.myColor = 2 # 先手玩家选择的棋子颜色 self.player2color = 1 # 后手玩家选择的棋子颜色 self.player = 1 self.player2 = 0 self.WinFLAG = 0 - self.curlocation(canvas, 1, 1,1,blackch,whitech,photos1,photos2) + self.curlocation(canvas, 1, 1, 1, blackch, whitech, photos1, photos2) + # 由于在存储的棋局中都是Mode1,这里先按照Mode1实现功能 + elif Chess_Mode == 4: + self.myColor = 2 # 玩家选择的棋子颜色 黑 + self.computercolor = 1 # 电脑的棋子颜色 白 + self.player = 1 + self.computer = 0 + self.WinFLAG = 0 + self.curlocation(canvas, 1, 0, 1, blackch, whitech, photos1, photos2) - def playgame_black(self,root,playerx,playery,canvas, blackch,whitech,result,photos1,photos2): - if self.WinFLAG == 0 and self.player == 1 and self.computer == 0 and self.Currently_step % 2==0: - if self.player_location(playerx,playery,canvas, blackch,whitech): - if self.Ovalone != 0: - canvas.delete(self.Ovalone) - self.curlocation(canvas, 0, 1,2,blackch,whitech,photos1,photos2) + def playgame_black(self, root, playerx, playery, canvas, blackch, whitech, result, photos1, photos2): + if self.WinFLAG == 0 and self.player == 1 and self.computer == 0 and self.Currently_step % 2 == 0: + if self.player_location(playerx, playery, canvas, blackch, whitech): + if self.Ovalone_new != 0: + for i in self.Ovalone_new: + canvas.delete(i) + self.curlocation(canvas, 0, 1, 2, blackch, whitech, photos1, photos2) self.Gameover = self.ChessCheck() root.update() - if self.WinFLAG == 0 and self.player == 0 and self.computer == 1 and self.Currently_step % 2!=0 : + if self.WinFLAG == 0 and self.player == 0 and self.computer == 1 and self.Currently_step % 2 != 0: root.update() - self.ai_location(canvas, blackch,whitech) + self.ai_location(canvas, blackch, whitech) self.Gameover = self.ChessCheck() - self.curlocation(canvas,1,0,1, blackch,whitech,photos1,photos2) + self.curlocation(canvas, 1, 0, 1, blackch, whitech, photos1, photos2) root.update() if self.WinFLAG == 1: - self.resultshow(root,canvas,whitech,blackch) + self.resultshow(root, canvas, whitech, blackch) root.update() - def playgame_white(self,root,playerx,playery,canvas, blackch,whitech,result,photos1,photos2): - if self.WinFLAG == 0 and self.player == 1 and self.computer == 0: - if self.player_location(playerx, playery, canvas, blackch,whitech) : - if self.Ovalone != 0: - canvas.delete(self.Ovalone) - self.curlocation(canvas, 0, 1,1, blackch,whitech,photos1,photos2) + def playgame_white(self, root, playerx, playery, canvas, blackch, whitech, result, photos1, photos2): + if self.WinFLAG == 0 and self.player == 1 and self.computer == 0 and self.Currently_step % 2 == 1: + if self.player_location(playerx, playery, canvas, blackch, whitech): + if self.Ovalone_new != 0: + # print(self.Ovalone) + for i in self.Ovalone_new: + canvas.delete(i) + self.curlocation(canvas, 0, 1, 1, blackch, whitech, photos1, photos2) self.Gameover = self.ChessCheck() if self.WinFLAG == 0 and self.player == 0 and self.computer == 1: root.update() - self.ai_location(canvas,blackch,whitech) - self.curlocation(canvas, 1, 0,2, blackch,whitech,photos1,photos2) + self.ai_location(canvas, blackch, whitech) + self.curlocation(canvas, 1, 0, 2, blackch, whitech, photos1, photos2) self.Gameover = self.ChessCheck() if self.WinFLAG == 1: - self.resultshow(root,canvas,whitech,blackch) + self.resultshow(root, canvas, whitech, blackch) root.update() - def doublepeople(self,root,playerx,playery,canvas,blackch,whitech,result,photos1,photos2): - if self.WinFLAG ==0: - if self.player == 1 and self.player2 == 0 and self.Currently_step % 2 ==0 : - if self.player_location(playerx, playery, canvas, blackch,whitech) : + def doublepeople(self, root, playerx, playery, canvas, blackch, whitech, result, photos1, photos2): + if self.WinFLAG == 0: + if self.player == 1 and self.player2 == 0 and self.Currently_step % 2 == 0: + if self.player_location(playerx, playery, canvas, blackch, whitech): if self.Ovalone != 0: canvas.delete(self.Ovalone) self.player2 = 1 self.Gameover = self.ChessCheck() - self.curlocation(canvas, 0, 0,2, blackch,whitech,photos1,photos2) + self.curlocation(canvas, 0, 0, 2, blackch, whitech, photos1, photos2) if self.WinFLAG == 1: - self.resultshow(root,canvas,whitech,blackch) + self.resultshow(root, canvas, whitech, blackch) root.update() - elif self.player2 == 1 and self.player == 0 and self.Currently_step % 2 !=0 : - if self.player_location(playerx, playery, canvas, blackch,whitech): + elif self.player2 == 1 and self.player == 0 and self.Currently_step % 2 != 0: + if self.player_location(playerx, playery, canvas, blackch, whitech): if self.Ovalone != 0: canvas.delete(self.Ovalone) self.player = 1 self.player2 = 0 - self.curlocation(canvas, 1, 1,1, blackch,whitech,photos1,photos2) + self.curlocation(canvas, 1, 1, 1, blackch, whitech, photos1, photos2) self.Gameover = self.ChessCheck() - if self.WinFLAG ==1: - self.resultshow(root,canvas,whitech,blackch) + if self.WinFLAG == 1: + self.resultshow(root, canvas, whitech, blackch) root.update() + + def studenttest_playgame_white(self, root, playerx, playery, canvas, blackch, whitech, result, photos1, photos2): + if self.WinFLAG == 0 and self.player == 1 and self.computer == 0 and self.Currently_step % 2 == 1: + flag,x,y = self.get_player_move_location_xy(playerx, playery, canvas, blackch, whitech) + if flag: + if self.Ovalone_new != 0: + # print(self.Ovalone) + for i in self.Ovalone_new: + canvas.delete(i) + self.curlocation(canvas, 0, 1, 1, blackch, whitech, photos1, photos2) + self.Gameover = self.ChessCheck() + if self.WinFLAG == 0 and self.player == 0 and self.computer == 1: + root.update() + self.ai_location(canvas, blackch, whitech) + self.curlocation(canvas, 1, 0, 2, blackch, whitech, photos1, photos2) + self.Gameover = self.ChessCheck() + if self.WinFLAG == 1: + self.resultshow(root, canvas, whitech, blackch) + root.update() + # 返回xy坐标 + + return x,y + + def studenttest_playgame_black(self, root, playerx, playery, canvas, blackch, whitech, result, photos1, photos2): + if self.WinFLAG == 0 and self.player == 1 and self.computer == 0 and self.Currently_step % 2 == 0: + flag,x,y = self.get_player_move_location_xy(playerx, playery, canvas, blackch, whitech) + # print("get_player_move_location_xy:flag,x,y",flag,x,y) + if flag: + if self.Ovalone_new != 0: + for i in self.Ovalone_new: + canvas.delete(i) + self.curlocation(canvas, 0, 1, 2, blackch, whitech, photos1, photos2) + self.Gameover = self.ChessCheck() + root.update() + if self.WinFLAG == 0 and self.player == 0 and self.computer == 1 and self.Currently_step % 2 != 0: + root.update() + self.ai_location(canvas, blackch, whitech) + self.Gameover = self.ChessCheck() + self.curlocation(canvas, 1, 0, 1, blackch, whitech, photos1, photos2) + + root.update() + if self.WinFLAG == 1: + self.resultshow(root, canvas, whitech, blackch) + root.update() + return x,y + + + # 返回一个位置数组 + def return_postion(self, Depth, computercolor): + time.sleep(0.5) + self.New_count = 0 + self.Max_Score = 0 + self.Counts = 0 + pos = [[0 for y in range(2)] for x in range(10)] + evaFLAG = 0 + position = [0, 0] # 初始化位置坐标数组 + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if 0 <= i < 19 and 0 <= j < 19: + if self.ChessData[i][j]['Cstate'] == 0: + self.Score[i][j] = self.count_Score(i, j, computercolor) + self.New_count += 1 + if self.Score[i][j] > 0: + self.Counts += 1 + if self.Max_Score < self.Score[i][j]: + self.Max_Score = self.Score[i][j] # 记录当前棋盘分数的最大值 + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if self.ChessData[i][j]['Cstate'] != 0: + self.Score[i][j] = self.ChessData[i][j]['Cstate'] + if i < 19 and j < 19 and self.Score[i][j] == self.Max_Score and self.ChessData[i][j]['Cstate'] == 0: + pos[evaFLAG][0] = i + pos[evaFLAG][1] = j + evaFLAG += 1 + return pos + + def return_postion_new(self, Depth, computercolor): + Depth = 8 + # Depth = 2 + time.sleep(0.5) + self.New_count = 0 + self.Max_Score = 0 + self.Counts = 0 + pos = [[0 for y in range(2)] for x in range(100)] + evaFLAG = 0 + position = [0, 0] # 初始化位置坐标数组 + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if 0 <= i < 19 and 0 <= j < 19: + if self.ChessData[i][j]['Cstate'] == 0: + self.Score[i][j], message = self.count_Score(i, j, computercolor) # 计算点的得分 + self.New_count += 1 + if self.Score[i][j] > 0: + self.Counts += 1 + if self.Max_Score < self.Score[i][j]: + self.Max_Score = self.Score[i][j] # 记录当前棋盘分数的最大值 + self.Max_Score_pos = (i, j) + + score, message = self.count_Score(self.Max_Score_pos[0], self.Max_Score_pos[1], computercolor) # 计算点的得分 + # print(self.Max_Score_pos) + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if self.ChessData[i][j]['Cstate'] != 0: + self.Score[i][j] = self.ChessData[i][j]['Cstate'] + if i < 19 and j < 19 and self.Score[i][j] >= self.Max_Score - 100 and self.ChessData[i][j][ + 'Cstate'] == 0: + pos[evaFLAG][0] = i + pos[evaFLAG][1] = j + evaFLAG += 1 + # print('\n'.join([' '.join([f'{item:4}' for item in row]) for row in self.Score])) + return pos, message + + # 获得当前一步的最大估值落子点和落子原因以及除了最大值以外估值最高的4个落子点 + def return_postion_test_module(self, Depth, computercolor): + Depth = 8 + # Depth = 2 + time.sleep(0.5) + self.New_count = 0 + self.Max_Score = 0 + self.Counts = 0 + tempPos = [] + tempVal = [] + Pos_get = [] + pos = [[0 for y in range(2)] for x in range(361)] + evaFLAG = 0 + position = [0, 0] # 初始化位置坐标数组 + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if 0 <= i < 19 and 0 <= j < 19: + if self.ChessData[i][j]['Cstate'] == 0: + self.Score[i][j], message = self.count_Score(i, j, computercolor) # 计算点的得分 + self.New_count += 1 + if self.Score[i][j] > 0: + self.Counts += 1 + if self.Max_Score < self.Score[i][j]: + self.Max_Score = self.Score[i][j] # 记录当前棋盘分数的最大值 + self.Max_Score_pos = (i, j) + + score, message = self.count_Score(self.Max_Score_pos[0], self.Max_Score_pos[1], computercolor) # 计算点的得分 + # print(self.Max_Score_pos) + for i in range(8 - Depth, 11 + Depth): + for j in range(8 - Depth, 11 + Depth): + if self.ChessData[i][j]['Cstate'] != 0: + self.Score[i][j] = self.ChessData[i][j]['Cstate'] + if i < 19 and j < 19 and self.Score[i][j] >= 36 and self.ChessData[i][j]['Cstate'] == 0: + pos[evaFLAG][0] = i + pos[evaFLAG][1] = j + tempPos.append([i, j]) + tempVal.append(self.Score[i][j]) + evaFLAG += 1 + # print('\n'.join([' '.join([f'{item:4}' for item in row]) for row in self.Score])) + sorted_id = sorted(range(len(tempVal)), key=lambda k: tempVal[k], reverse=True) + for i in range(5): + Pos_get.append(tempPos[sorted_id[i]]) + return Pos_get, message diff --git a/chessboard.py b/chessboard.py index 6dcb7b0..a6be132 100644 --- a/chessboard.py +++ b/chessboard.py @@ -1,49 +1,87 @@ +import os from tkinter import * import tkinter.messagebox +import tkinter.scrolledtext as st from chess import * -from PIL import Image,ImageTk +from PIL import Image, ImageTk from datetime import datetime import time import numpy as np - - chess = Chess() crossline = [] verline = [] regretnum = 0 handlenum = 0 - +flag = 0 root = Tk() # 窗口对象 root.title('五子棋') # 窗口标题 -root.geometry('1000x700+10+10') # 窗口大小 width x height + x + y(x,y为在当前界面的坐标) + +x = 10 +y = 10 +root.geometry('1000x700+'+str(x)+'+'+str(y)) # 窗口大小 width x height + x + y(x,y为在当前界面的坐标) # 创建一个主目录菜单,也被称为顶级菜单 -main_menu = Menu (root) -gamefile = Menu (main_menu, tearoff=False) -#新增"文件"菜单的菜单项,并使用 accelerator 设置菜单项的快捷键 +main_menu = Menu(root) +gamefile = Menu(main_menu, tearoff=False) + +# ---zhuozhuo +Text_test = None +accuracy = 0 +chooseright = 0 +choosetime = 0 + +# local = True +local = False +if local == True: + path = "" +else: + path = "/data/workspace/myshixun/finalchess/" +# path = "" + +# 新增"文件"菜单的菜单项,并使用 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) + 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) + 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) + chess.playing(0, root, canvas, blackch, whitech, photos1, photos2) else: chess.Chess_Mode = 0 refresh() +# 测试模式 +def studenttest(): + if chess.Chess_Mode == 3: + chess.Chess_Mode = 4 + chess.playing(4, root, canvas, blackch, whitech, photos1, photos2) + else: + chess.Chess_Mode = 4 + refresh() + + +# 开始游戏 def startgame(): win = Tk() win.title("开始游戏") @@ -52,11 +90,17 @@ def startgame(): 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 = open(path + 'data.txt', 'r+') file.truncate(0) for i in range(0, 19): for j in range(0, 19): @@ -68,7 +112,7 @@ def savecurrent(): b += str(data[i, j]) + '\t' b += '\n' file.writelines(b) - print(data) + # 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): @@ -80,7 +124,7 @@ def savecurrent(): b += str(data[i, j]) + '\t' b += '\n' file.writelines(b) - print(data) + # print(data) Chess_Mode = str(chess.Chess_Mode) + '\n' file.write(Chess_Mode) step = str(chess.Currently_step) + '\n' @@ -89,116 +133,442 @@ def savecurrent(): file.write(WinFLAG) file.close() + +# 重装棋局 def resetlast(): refresh() - file = open('data.txt', 'r+', encoding='utf-8') + # 读取保存的数据文件 + file = open(path + '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='对局已经结束了') + + +# 随机棋局 +def random_chessboard(): + refresh() + if local: + folder_path = "./data" + else: + folder_path = path + "data" + txt_files = [f for f in os.listdir(folder_path) if f.endswith('.txt')] + if txt_files: + random_file = random.choice(txt_files) + file_path = os.path.join(folder_path, random_file) + file = open(file_path, '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='对局已经结束了') + + +# 测试模式随机棋局 +def random_chessboard_test_module(): + refresh() + if local: + folder_path = "./data" + else: + folder_path = path + "data" + txt_files = [f for f in os.listdir(folder_path) if f.endswith('.txt')] + if txt_files: + random_file = random.choice(txt_files) + file_path = os.path.join(folder_path, random_file) + file = open(file_path, 'r+', encoding='utf-8') data = file.readlines() rtu = [] for i in data: rtu.append(i.strip()) - for i in range(0,19): + 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 + 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) + chess.ChessData[i - 19][j]['Cstep'] = int(x) j += 1 - chess.Chess_Mode = int(rtu[len(rtu)-3]) + # chess.Chess_Mode = int(rtu[len(rtu) - 3]) + chess.Chess_Mode = 4 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="") + 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.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: + if chess.Currently_step % 2 == 0: chess.player = 1 chess.computer = 0 - chess.curlocation(canvas,1,0,1, blackch,whitech,photos1,photos2) + chess.curlocation(canvas, 1, 0, 1, blackch, whitech, photos1, photos2) tkinter.messagebox.showinfo(title='信息提示!', message='轮到玩家下棋了!') - elif chess.Currently_step % 2 !=0: + 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.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) + 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) + chess.curlocation(canvas, 1, 0, 2, blackch, whitech, photos1, photos2) tkinter.messagebox.showinfo(title='信息提示!', message='轮到玩家下棋了!') - elif chess.Chess_Mode == 0 and chess.WinFLAG ==0 : + 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.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) + chess.curlocation(canvas, 0, 0, 2, blackch, whitech, photos1, photos2) tkinter.messagebox.showinfo(title='信息提示!', message='轮到后手下棋了!') - elif chess.WinFLAG ==1 : + elif chess.Chess_Mode == 4 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.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参数与下拉菜单绑定 + + +# 这个方法为了分开得到建议函数suggestion_test_module中的获得落子点和画图的部分 +# 这个方法为了获得落子点 +def test_suggestion_location(): + # 滚动框 + x = root.winfo_x() + y = root.winfo_y() + # root.geometry('1000x800+' + str(x) + '+' + str(y)) + # Text = st.ScrolledText(root, bg='#522418', font=('Arial', 12), bd=2, relief='groove') # 创建一个带滚动条的文本框,用于展示计算过程 + # Text.place(x=0, y=685, height=100, width=1000) + # Text.configure(foreground='white') + # Text_test.delete('1.0', 'end') + # Text_test.insert('2.0', '测试模式:请在估值函数中最大的分数处落子\n') + + # print("suggestion_test_module Mode ?",chess.Chess_Mode) + # print("suggestion_test_module Text_test",Text_test) + + # 判断是否结束以及所持颜色 + 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 + elif chess.Chess_Mode == 4: + color = 2 + pos,message = chess.return_postion_test_module(chess.Depth, color) # 调用估值函数 + return pos,message + + +# 这个方法为了分开得到建议函数suggestion_test_module中的获得落子点和画图的部分 +# 这个方法根据落子点的建议在画布上画图 +def test_suggestion_drawloc(pos): + for i in pos: + if chess.ChessData[i[0]][i[1]]['Cstate'] == 0 and (i[0] != 0 and i[1] != 0): + if chess.myColor == 1 and chess.computercolor == 2: + Ovalone = canvas.create_oval(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, i[1] * 34.6 + 21 + 13.5, + fill='#fff', outline="#000") + elif chess.myColor == 2 and chess.computercolor == 1: + Ovalone = canvas.create_oval(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, i[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(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, + i[1] * 34.6 + 21 + 13.5, fill='#000', outline="#fff") + elif chess.player2 == 1 and chess.player == 0: + Ovalone = canvas.create_oval(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, + i[1] * 34.6 + 21 + 13.5, fill='#fff', outline="#000") + chess.Ovalone_new.append(Ovalone) + root.update() + return pos[0] + + +# 最新的得到建议的函数 +def suggestion_test_module(): + # 滚动框 + global Text_test + # x = root.winfo_x() + # y = root.winfo_y() + x = 10 + y = 10 + root.geometry('1000x800+' + str(x) + '+' + str(y)) + Text_test = st.ScrolledText(root, bg='#522418', font=('Arial', 12), bd=2, relief='groove') # 创建一个带滚动条的文本框,用于展示计算过程 + Text_test.place(x=0, y=685, height=100, width=1000) + Text_test.configure(foreground='white') + Text_test.delete('1.0', 'end') + Text_test.insert('2.0', '测试模式:请在估值函数中最大的分数处落子\n') + + # print("suggestion_test_module Mode ?",chess.Chess_Mode) + + # 判断是否结束以及所持颜色 + 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 + elif chess.Chess_Mode == 4: + color = 2 + pos,message = chess.return_postion_test_module(chess.Depth, color) # 调用估值函数 + # Text.insert('end', '最佳落子点为:{}\n'.format(chess.Max_Score_pos)) + # Text.insert('end',message) + # print(pos) + # 画点 + + for i in pos: + if chess.ChessData[i[0]][i[1]]['Cstate'] == 0 and (i[0] != 0 and i[1] != 0): + # print("chess.myColor,chess.computercolor:",chess.myColor == 1 , chess.computercolor) + if chess.myColor == 1 and chess.computercolor == 2: + Ovalone = canvas.create_oval(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, i[1] * 34.6 + 21 + 13.5, + fill='#fff', outline="#000") + elif chess.myColor == 2 and chess.computercolor == 1: + Ovalone = canvas.create_oval(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, i[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(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, + i[1] * 34.6 + 21 + 13.5, fill='#000', outline="#fff") + elif chess.player2 == 1 and chess.player == 0: + Ovalone = canvas.create_oval(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, + i[1] * 34.6 + 21 + 13.5, fill='#fff', outline="#000") + chess.Ovalone_new.append(Ovalone) + root.update() + return pos,message + + +# 学生测试模式 +def test_module(): + random_chessboard_test_module() + answer_pos,answer_message = suggestion_test_module() + # test_answer_x = answer_pos[0] + # test_answer_y = answer_pos[1] + + +gamefile.add_command(label="开始游戏", command=startgame) +gamefile.add_command(label="保存当前局面", command=savecurrent, accelerator="Ctrl+O") +gamefile.add_command(label="重装以前局面", command=resetlast, accelerator="Ctrl+S") +gamefile.add_command(label="随机棋局", command=random_chessboard) +gamefile.add_command(label="测试模式", command=test_module) +main_menu.add_cascade(label="游戏", menu=gamefile) # 在主目录菜单上新增"文件"选项,并通过menu参数与下拉菜单绑定 + + +# 游戏规则 def helpgame(): - msg ='''1、对局双方各执一色棋子。 + 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) +main_menu.add_command(label="帮助", command=helpgame) +root.config(menu=main_menu) + +# 棋盘背景画面 +canvas = Canvas(root, bg='white', width=1000, height=800) +photoImg = Image.open(path + "image/背景.png").resize((1000, 800)) +imgbg = ImageTk.PhotoImage(photoImg) +canvas.create_image(0, 330, image=imgbg, anchor=W) +photoqizi = Image.open(path + "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):#横线 + 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: + 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):#竖线 + 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: + 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): @@ -211,128 +581,223 @@ def cross(): y * 34.3 + 21 - radius / 2, x * 36.9 + 41 + radius / 2, y * 34.3 + 21 + radius / 2, - fill='#BE875E',outline="") -cross() - + 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)) + global Text_test, choosetime, chooseright + + if chess.Chess_Mode != 4: + try: + # x = root.winfo_x() + x = 10 + # y = root.winfo_y() + y = 10 + root.geometry('1000x700+' + str(x) + '+' + str(y)) + Text.destroy() + except: + pass + 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) + elif chess.Chess_Mode == 4: + # 由于在点击后需要重新给出下一步落子都选点,在五子棋结束后不能使用这个,因此需要判断是否结束 + if chess.game_end(): + pass + else: + try: + # x = root.winfo_x() + x = 10 + # y = root.winfo_y() + y = 10 + root.geometry('1000x800+' + str(x) + '+' + str(y)) + except: + pass + playerx = float(format(chessData.x)) + playery = float(format(chessData.y)) + + # print("Mode 4") + loc_x = -1 + loc_y = -1 + + answer_pos,answer_message = test_suggestion_location() + test_answer_x = answer_pos[0][0] + test_answer_y = answer_pos[0][1] + + if chess.player == 1: + if chess.myColor == 2 and chess.computercolor == 1: + loc_x,loc_y = chess.studenttest_playgame_black(root, playerx, playery, canvas, blackch, whitech, result, photos1, photos2) + if chess.myColor == 1 and chess.computercolor == 2: + loc_x,loc_y = chess.studenttest_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) + + # 准确率判断 + if loc_x == test_answer_x and loc_y == test_answer_y: + # print("print(Text_test)",Text_test) + # print("answer right!",test_answer_x,test_answer_y) + Text_test.insert('end', "answer right!") + Text_test.insert('end', '最佳落子点为:{}\n'.format(chess.Max_Score_pos)) + Text_test.insert('end',answer_message) + + # 添加正确率 + chooseright += 1 + choosetime += 1 + string_acc = "当前正确率:" + "{:.2f}%".format(chooseright/choosetime*100) + "\n" + Text_test.insert('end', string_acc) + + elif loc_x != test_answer_x or loc_y != test_answer_y: + # print("print(Text_test)",Text_test) + # print("answer wrong!", test_answer_x, test_answer_y) + Text_test.insert('end', "answer wrong!") + Text_test.insert('end', '最佳落子点为:{}\n'.format(chess.Max_Score_pos)) + Text_test.insert('end', answer_message) + + # 添加正确率 + choosetime += 1 + string_acc = "当前正确率:" + "{:.2f}%".format(chooseright/choosetime*100) + "\n" + Text_test.insert('end', string_acc) + + # 继续获取位置 + if chess.game_end(): + pass + else: + pos,message = test_suggestion_location() + test_suggestion_drawloc(pos) + + # print('鼠标左键单机位置(相对于父容器):{0},{1}'.format(chessData.x, chessData.y)) + # print('鼠标左键单击位置(相对于屏幕):{0},{1}'.format(chessData.x_root, chessData.y_root)) + # canvas.bind('',handleMotion) canvas.bind('', 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 = Image.open(path + "image/黑子-小.png").resize((40, 40)) blackch = ImageTk.PhotoImage(blackch) -whitech = Image.open("image/白子-小.png").resize((40, 40)) +whitech = Image.open(path + "image/白子-小.png").resize((40, 40)) whitech = ImageTk.PhotoImage(whitech) -result = Image.open("image/resultshow.PNG").resize((100, 42)) +result = Image.open(path + "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) +photos1 = Image.open(path + "image/上方button-选中.png").resize((110, 45)) +photos1 = ImageTk.PhotoImage(photos1) +photos2 = Image.open(path + "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') +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) +# 当前落子方跳选框 +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 = Image.open(path + "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 = 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) +cur_result.place(x=750, y=190, width=100, height=30) +# 公告栏 +photonotice = Image.open(path + "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') + 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') + 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 = Image.open(path + "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) + image=photoreview, command=review, bd=0) # btn_review["bg"] = # btn_review["border"] = "0" -btn_review.place(x=750,y=400) +btn_review.place(x=750, y=400) + + # 按钮悔棋 def regretch(): + + if chess.Chess_Mode == 4: + tkinter.messagebox.showinfo(title='信息提示!', message='测试模式中不可使用!') + return + if chess.WinFLAG == 0: global regretnum regretnum = 1 - if chess.Currently_step == 1 and chess.Chess_Mode ==2: + 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) + chess.playing(2, root, canvas) + if chess.Chess_Mode == 1 or chess.Chess_Mode == 2 or chess.Chess_Mode == 4: + # print(xregretFLAG) + # print(yregretFLAG) for i in range(2): - regretxFLAG = xregretFLAG[chess.Currently_step-regretnum] - regretyFLAG = yregretFLAG[chess.Currently_step-regretnum] + 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) + 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] @@ -343,173 +808,273 @@ def regretch(): if chess.Currently_step % 2 == 0: chess.player = 1 chess.player2 = 0 - chess.curlocation(canvas, 1, 1,1) + 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 : + 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 = Image.open(path + "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) + image=photoregretch, command=regretch, bd=0) btn_regret.place(x=850, y=400) + + # 按钮撤销复盘 def concelreview(): if chess.WinFLAG == 1: - chess.showHistory=True + 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: + if chess.ChessData[x][y]['Cstep'] != 0 and chess.Relabel[x][y] != 0: chess.Relabel[x][y].destroy() - chess.Relabel[x][y]=0 + chess.Relabel[x][y] = 0 elif chess.WinFLAG == 0: - chess.showHistory=False + chess.showHistory = False tkinter.messagebox.showinfo(title='信息提示!', message='在复盘后使用噢!') root.update() -photoconcel = Image.open("image/撤销复盘.png").resize((100, 50)) + + +photoconcel = Image.open(path + "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) + command=concelreview, bd=0) btn_concel.place(x=750, y=460) + + # 按钮落子建议 -def suggestion(): - if chess.WinFLAG!=1: +# 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 + + +# 最新的得到建议的函数 +def suggestion_new(): + + if chess.game_end(): + tkinter.messagebox.showinfo(title='信息提示!', message='请在对局中使用!') + return + if chess.Chess_Mode == 4: + tkinter.messagebox.showinfo(title='信息提示!', message='测试模式中不可使用!') + return + + # 滚动框 + global Text + # x = root.winfo_x() + # y = root.winfo_y() + x = 10 + y = 10 + root.geometry('1000x800+' + str(x) + '+' + str(y)) + Text = st.ScrolledText(root, bg='#522418', font=('Arial', 12), bd=2, relief='groove') # 创建一个带滚动条的文本框,用于展示计算过程 + Text.place(x=0, y=685, height=100, width=1000) + Text.configure(foreground='white') + Text.delete('1.0', 'end') + Text.insert('2.0', '落子建议步骤:\n') + if chess.WinFLAG != 1: if chess.Depth >= 9: chess.Depth = 8 - if chess.Chess_Mode ==1: + if chess.Chess_Mode == 1: color = 2 - elif chess.Chess_Mode ==2: + elif chess.Chess_Mode == 2: color = 1 - elif chess.Chess_Mode ==0: - if chess.player == 1 and chess.player2 == 0 : + elif chess.Chess_Mode == 0: + if chess.player == 1 and chess.player2 == 0: color = 2 - elif chess.player == 0 and chess.player2 == 1 : + 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 + pos,message = chess.return_postion_new(chess.Depth, color) # 调用估值函数 + Text.insert('end', '最佳落子点为:{}\n'.format(chess.Max_Score_pos)) + Text.insert('end',message) + # print("pos:",pos) + # print(pos) + # 画点 + for i in pos: + if chess.ChessData[i[0]][i[1]]['Cstate'] == 0 and (i[0] != 0 and i[1] != 0): + if chess.myColor == 1 and chess.computercolor == 2: + Ovalone = canvas.create_oval(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, i[1] * 34.6 + 21 + 13.5, + fill='#fff', outline="#000") + elif chess.myColor == 2 and chess.computercolor == 1: + Ovalone = canvas.create_oval(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, i[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(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, + i[1] * 34.6 + 21 + 13.5, fill='#000', outline="#fff") + elif chess.player2 == 1 and chess.player == 0: + Ovalone = canvas.create_oval(i[0] * 36.8 + 41 - 12.5, i[1] * 34.6 + 21 - 13.5, + i[0] * 36.8 + 41 + 12.5, + i[1] * 34.6 + 21 + 13.5, fill='#fff', outline="#000") + chess.Ovalone_new.append(Ovalone) root.update() - else: - pass -photosuggest = Image.open("image/落子.png").resize((100, 50)) + + +photosuggest = Image.open(path + "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) + image=photosuggest, command=suggestion_new, bd=0) btn_suggest.place(x=850, y=460) + + # 按钮局面评估 def curnow(): + + # if chess.game_end(): + + if chess.Depth >= 9: chess.Depth = 8 - if chess.Chess_Mode ==1: + if chess.Chess_Mode == 1: color = 2 - elif chess.Chess_Mode ==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 : + elif chess.Chess_Mode == 0: + if chess.player == 1 and chess.player2 == 0: color = 2 - elif chess.player == 0 and chess.player2 == 1 : + elif chess.player == 0 and chess.player2 == 1: color = 1 + elif chess.Chess_Mode == 4: + color = 2 chess.return_chess(chess.Depth, color) # 调用估值函数 if chess.Counts / chess.New_count == 1: - OwnCounter = random.uniform(0.8, 0.85) + OwnCounter = random.uniform(0.5, 0.6) else: OwnCounter = chess.Counts / chess.New_count - if color == 1: + if color == 2: + # 1.判断x-轴是否活四或者连五 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-轴是否连续四子 + 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) or \ + (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'] == 1): + OwnCounter = OwnCounter - OwnCounter + # 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.判断右上-左下是否连续四子 + 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) or ( + 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'] == 1): + OwnCounter = OwnCounter - OwnCounter + # 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.判断左上-右下是否连续四子 + 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) or \ + (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'] == 1): + OwnCounter = OwnCounter - OwnCounter + # 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'+ \ + 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) or ( + 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'] == 1): + OwnCounter = OwnCounter - OwnCounter + OwnCounter = OwnCounter - OwnCounter + + 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 = Image.open(path + "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) + 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)) + +# 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(path +"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) +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(): + + # 这里需要重新添加一个刷新的东西类似于geometry这种,因为刷新有时候没有将画的建议位置删除 + # x = root.winfo_x() + # y = root.winfo_y() + x = 10 + y = 10 + root.geometry('1000x700+' + str(x) + '+' + str(y)) + 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 = Image.open(path + "image/棋盘-空.png").resize((730, 700)) photoqizi = ImageTk.PhotoImage(photoqizi) canvas.create_image(10, 350, image=photoqizi, anchor=W) cross() @@ -534,23 +1099,29 @@ def refresh(): chess.computercolor = 0 # 电脑的棋子颜色 chess.player2color = 0 # 玩家2的棋子颜色 if chess.WinFLAG == 1: - chess.resultshow(root,canvas,whitech,blackch) + chess.resultshow(root, canvas, whitech, blackch) - if chess.Chess_Mode == 1 : - chess.playing(1, root, canvas,blackch,whitech,photos1,photos2) + 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) + chess.playing(2, root, canvas, blackch, whitech, photos1, photos2) elif chess.Chess_Mode == 0: - chess.playing(0, root, canvas,blackch,whitech,photos1,photos2) + chess.playing(0, root, canvas, blackch, whitech, photos1, photos2) + elif chess.Chess_Mode == 4: + chess.playing(4, 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 = Image.open(path + "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) + image=photorefresh, command=refresh, bg='#FFA500', bd=0) +btn_reset.place(x=850, y=520) +canvas.pack() +root.mainloop() diff --git a/data/1.txt b/data/1.txt new file mode 100644 index 0000000..d40ef4c --- /dev/null +++ b/data/1.txt @@ -0,0 +1,41 @@ +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 2 2 1 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 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