diff --git a/chess.py b/chess.py new file mode 100644 index 0000000..b7b629e --- /dev/null +++ b/chess.py @@ -0,0 +1,1064 @@ +import random +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坐标 + + +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.currentStep = 0 # 棋子当前进行的步数 + self.Chessimg = [[0 for y in range(19)] for x in range(19)] + self.Reviewflag = 0 # 复盘的标签flag 0为没有复盘 1为已经复盘,防止重复复盘导致无法消除新建的标签 + self.Reviewlabel = [[0 for y in range(19)] for x in range(19)] # 复盘的数字标签 + self.score = [[0 for y in range(19)] for x in range(19)] # 初始化计分数组 + self.maxScore = 0 # 落子分数 + self.counts = 0 + self.newCount = 0 + self.OvaloneNew = [] # 棋子对象列表 + self.winFlag = 0 # 判断是否取得胜利 + self.Gameover = 0 # 判断游戏是否结束,1 黑方胜,2 白方胜利,3 平局 + self.depth = 0 # 搜索的深度 + self.player = 0 # 轮到下棋的标志,1=下,0=不下 + self.player2 = 0 # 玩家2 + self.currentPlayer = 1 # (=1,黑方;=2,白方) + self.myColor = 0 # 玩家选择的棋子颜色 + self.chessMode = 3 # 对弈方式标志 chessMode(=0,人-人对弈; =1, 人-机白对弈; =2,机黑-人对弈, =4,测试模式) + self.player2Color = 0 # 玩家2的棋子颜色 + self.maxScorePos = () + + # 结果展示的图片,以用于删除 + self.PicLocIds = [] + + def gameEnd(self): + if self.Gameover == 1 or self.winFlag == 1: + return True + else: + return False + + # 计算某个位置得分 + def countScore(self, x, y, computerColor): + # global sum,count,value + global s + sum = 0 + # count = 0 + value = [0, 0, 0, 0] + upcount = [0, 0, 0, 0] # 左、上、左斜上、左斜下 棋子数 + 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] 记录 + if self.chessData[i][y]['Cstate'] == color: + upcount[0] += 1 # 左侧有自己棋子 + elif self.chessData[i][y]['Cstate'] != 0 and self.chessData[i][y]['Cstate'] != color: # 左侧有对方棋子 + upflag[0] = -1 + break + elif self.chessData[i][y]['Cstate'] == 0: # 左侧没有棋子 + upflag[0] = 1 + 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: + upflag[0] = 3 # 表示有三个空格 + else: + break + break + for i in range(x + 1, 19): # 计算右边棋子数量 downcount[0]表示右侧同类棋子 用downflag[0] 记录 + if self.chessData[i][y]['Cstate'] == color: + downcount[0] += 1 # 右侧有自己棋子 + elif self.chessData[i][y]['Cstate'] != 0 and self.chessData[i][y]['Cstate'] != color: # 右侧有对方棋子 + downflag[0] = -1 + break + elif self.chessData[i][y]['Cstate'] == 0: # 右侧没有棋子 + downflag[0] = 1 + 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: + downflag[0] = 3 # 表示有三个空格 + else: + break + break + for i in range(y - 1, -1, -1): # 计算方向向上 upcount[1]表示上方同类棋子 用upflag[1] 记录 + if self.chessData[x][i]['Cstate'] == color: + upcount[1] += 1 # 上有自己棋子 + elif self.chessData[x][i]['Cstate'] != 0 and self.chessData[x][i]['Cstate'] != color: # 上有对方棋子 + upflag[1] = -1 + break + elif self.chessData[x][i]['Cstate'] == 0: # 上没有棋子 + upflag[1] = 1 + 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: + upflag[1] = 3 # 表示上有三个空格 + else: + break + break + for i in range(y + 1, 19): # 计算下棋子数量 downcount[0]表示下同类棋子 用downflag[0] 记录 + if self.chessData[x][i]['Cstate'] == color: + downcount[1] += 1 # 下有自己棋子 + elif self.chessData[x][i]['Cstate'] != 0 and self.chessData[x][i]['Cstate'] != color: # 下有对方棋子 + downflag[1] = -1 + break + elif self.chessData[x][i]['Cstate'] == 0: # 下侧没有棋子 + downflag[1] = 1 + 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: + downflag[1] = 3 # 表示下有三个空格 + else: + break + break + 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: + 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: # 左斜向上有对方棋子 + upflag[2] = -1 + j += 1 + break + 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: + 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: + upflag[2] = 3 # 表示有三个空格 + j += 1 + else: + j += 1 + break + j += 1 + 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: + 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: # 右斜向下有对方棋子 + downflag[2] = -1 + j += 1 + break + 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: + 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: + downflag[2] = 3 # 表示有三个空格 + j += 1 + else: + j += 1 + break + j += 1 + break + 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: + 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: # 右斜向上有对方棋子 + downflag[3] = -1 + j += 1 + break + 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: + 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: + downflag[3] = 3 # 表示有三个空格 + j += 1 + else: + j += 1 + break + j += 1 + break + 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: + 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: # 左斜向下有对方棋子 + upflag[3] = -1 + j += 1 + break + 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: + 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: + upflag[3] = 3 # 表示有三个空格 + j += 1 + else: + j += 1 + 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: # 成五 + message += '五子连珠,得分加4000!\n' + value[i] = 4000 + elif count == 4: + if upflag[i] >= 1 and downflag[i] >= 1: # 活四 + message += '四子连珠且有两端未被对方封堵,得分加1900!\n' + value[i] = 1900 + if (upflag[i] >= 1 and downflag[i] == -1) or (upflag[i] == -1 and downflag[i] >= 1): # 眠四 + message += '四子连珠且有一端未被对方封堵,得分加300!\n' + value[i] = 300 + if upflag[i] == -1 and downflag[i] == -1: # 死四 + message += '四子连珠但两端都被封堵,得分减5!\n' + value[i] = -5 + # 其他3子2子情况类似 + elif count == 3: + if (upflag[i] >= 2 and downflag[i] >= 1) or (upflag[i] >= 1 and downflag[i] >= 2): # 活三 + 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): # 眠三 + message += '三子连珠且有有一端未被对方封堵,得分加80!\n' + value[i] = 80 + if upflag[i] == -1 and downflag[i] == -1: # 死三 + 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): # 活二 + 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): # 眠三 + message += '二子连珠且有有一端未被对方封堵,得分加35!\n' + value[i] = 35 + if upflag[i] == -1 and downflag[i] == -1: # 死二 + 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] = 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] = 2 + if (upflag[i] <= 1 and downflag[i] <= 2) or (upflag[i] <= 2 and downflag[i] <= 1): # 死一 + value[i] = -5 + else:# 如果是玩家方的棋子 + # 同上,但得分标准可能有所不同 + for i in range(4): + count = upcount[i] + downcount[i] + 1 + if count == 5: # 成五 + value[i] = 3000 + elif count == 4: + if upflag[i] >= 1 and downflag[i] >= 1: # 活四 + value[i] = 1500 + if (upflag[i] >= 1 and downflag[i] == -1) or (upflag[i] == -1 and downflag[i] >= 1): # 眠四 + value[i] = 250 + if upflag[i] == -1 and downflag[i] == -1: # 死四 + value[i] = -5 + elif count == 3: + if (upflag[i] >= 2 and downflag[i] >= 1) or (upflag[i] >= 1 and downflag[i] >= 2): # 活三 + 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] = 50 + if upflag[i] == -1 and downflag[i] == -1: # 死三 + 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] = 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] = 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] = -5 + else: + if (upflag[i] >= 3 and downflag[i] >= 2) or (upflag[i] >= 2 and downflag[i] >= 3): # 活一 + 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] = 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] = -5 + # 汇总得分,并重置变量以便下一轮计算 + for i in range(4): + sum += value[i] + value[i] = 0 + upcount[i] = 0 + downcount[i] = 0 + upflag[i] = 0 + downflag[i] = 0 + # 恢复棋盘状态并返回得分及得分信息 + self.chessData[x][y]['Cstate'] = 0 + return sum, message + + # 根据估值函数计算最佳落子点 + def returnChess(self, Depth, computercolor): + time.sleep(0.5) + self.newCount = 0 + self.maxScore = 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], s = self.countScore(i, j, computercolor) + self.newCount += 1 + if self.score[i][j] > 0: + self.counts += 1 + if self.maxScore < self.score[i][j]: + self.maxScore = 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.maxScore 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 returnChessNew(self, Depth, computercolor): + time.sleep(0.5) + self.newCount = 0 + self.maxScore = 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.countScore(i, j, computercolor) + self.newCount += 1 + if self.score[i][j] > 0: + self.counts += 1 + if self.maxScore < self.score[i][j]: + self.maxScore = 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.maxScore - 5000 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 + + # 判断玩家点击的位置是否可以落子,如果可以落子,则进行self.make_move()移动,返回self.make_move()的值,否则返回False + def playerLocation(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.currentStep) + print("准备makeMove") + return self.makeMove(pointi, pointj, canvas, blackch, whitech) + # print("2:%d" % self.currentStep) + return False + + def getPlayerMoveLocation_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.currentStep) + return self.makeMove(pointi, pointj, canvas, blackch, whitech), pointi, pointj + # print("2:%d" % self.currentStep) + return self.makeMove(pointi, pointj, canvas, blackch, whitech), pointi, pointj + + # # 在pointi, pointj位置进行落子,返回落子是否成功的标记 + # def makeMove(self, pointi, pointj, canvas, blackch, whitech): + # + # print("self.chessData[pointi][pointj]['Cstate']", self.chessData[pointi][pointj]['Cstate']) + # print("self.myColor",self.myColor) + # print("self.currentStep % 2",self.currentStep % 2) + # print("self.player2Color",self.player2Color) + # + # # 如果pointi, pointj位置没有棋子,且当前执白子 + # if self.chessData[pointi][pointj]['Cstate'] == 0 and self.myColor == 2 and self.currentStep % 2 == 0: + # # 计算得到需要落子画图位置,并画棋子 + # Chessimgx = pointi*36.8 + 41 + # Chessimgy = pointj*34.3 + 20 + # oval = canvas.create_image(Chessimgx - 14, Chessimgy, image=blackch, anchor=W) + # self.Chessimg[pointi][pointj] = oval + # self.chessData[pointi][pointj]['Cstate'] = 2 + # xregretFLAG[self.currentStep] = pointi + # yregretFLAG[self.currentStep] = pointj + # self.player = 0 # 玩家下棋标志置0 + # self.player2 = 1 # 玩家2下棋标志置1 + # self.currentStep += 1 + # self.chessData[pointi][pointj]['Cstep'] = self.currentStep + # a = max(abs(pointi - 9), abs(pointj - 9)) + # self.depth = max(self.depth, a) + # print("makeMove True") + # return True + # # 如果pointi, pointj位置没有棋子,且当前执黑子 + # elif (self.chessData[pointi][pointj]['Cstate'] == 0 and self.myColor == 1 and self.currentStep % 2 != 0) or \ + # (self.chessData[pointi][pointj]['Cstate'] == 0 and self.player2Color == 1 and self.currentStep % 2 != 0): + # # 计算得到需要落子画图位置,并画棋子 + # Chessimgx = pointi*36.8 + 41 + # Chessimgy = pointj*34.6 + 20 + # oval = canvas.create_image(Chessimgx - 14, Chessimgy, image=whitech, anchor=W) + # self.Chessimg[pointi][pointj] = oval + # self.chessData[pointi][pointj]['Cstate'] = 1 + # xregretFLAG[self.currentStep] = pointi + # yregretFLAG[self.currentStep] = pointj + # self.currentStep += 1 + # self.player = 0 + # self.player2 = 1 # 玩家2下棋标志置1 + # # self.computer = 1 + # self.chessData[pointi][pointj]['Cstep'] = self.currentStep + # a = max(abs(pointi - 9), abs(pointj - 9)) + # self.depth = max(self.depth, a) + # print("makeMove True") + # return True + # # 如果pointi, pointj位置有棋子,无法落子 + # elif self.chessData[pointi][pointj]['Cstate'] != 0: + # print("makeMove False") + # return False + + # 在pointi, pointj位置进行落子,返回落子是否成功的标记 + def makeMove(self, pointi, pointj, canvas, blackch, whitech): + print("self.chessData[pointi][pointj]['Cstate']", self.chessData[pointi][pointj]['Cstate']) + print("self.myColor", self.myColor) + print("self.currentStep % 2", self.currentStep % 2) + print("self.player2Color", self.player2Color) + + # 检查是否可以在pointi, pointj位置落子 + if self.chessData[pointi][pointj]['Cstate'] != 0: + print("makeMove False") + return False + + # 检查是否轮到当前玩家落子 + is_player_turn = (self.myColor == 2 and self.currentStep % 2 == 0) or \ + (self.myColor == 1 and self.currentStep % 2 != 0) + is_player2_turn = (self.player2Color == 1 and self.currentStep % 2 != 0) + + # 如果不是当前玩家的轮次,则不能落子,函数返回False + if not is_player_turn and not is_player2_turn: + print("makeMove False") + return False + + # 根据当前执棋方选择棋子图 + image = blackch if self.currentStep % 2 == 0 else whitech + # 计算得到需要落子画图位置,并画棋子 + Chessimgx = pointi * 36.8 + 41 + Chessimgy = pointj * 34.3 + 20 + # Chessimgy = pointj * 34.3 + 20 if self.myColor == 2 else pointj * 34.6 + 20 + oval = canvas.create_image(Chessimgx - 14, Chessimgy, image=image, anchor=W) + # 更新棋盘数据,记录棋子信息 + self.Chessimg[pointi][pointj] = oval + self.chessData[pointi][pointj]['Cstate'] = self.myColor + xregretFLAG[self.currentStep] = pointi + yregretFLAG[self.currentStep] = pointj + self.currentStep += 1 + self.player = 0 + self.player2 = 1 + self.chessData[pointi][pointj]['Cstep'] = self.currentStep + a = max(abs(pointi - 9), abs(pointj - 9))# 计算落子距离棋盘中心的距离 + self.depth = max(self.depth, a) # 更新搜索深度,用于AI决策等场景 + print("makeMove True") + return True + + def getDirectionScore(self, y, x, y_offset, x_offset): + nowcount = 0 # 落子处我方连续子数 + _nowcount = 0 # 落子处对方连续子数 + space = None # 我方连续子中有无空格 + _space = None # 对方连续子中有无空格 + both = 0 # 我方连续子两端有无阻挡 + _both = 0 # 对方连续子两端有无阻挡 + tmp_x = x + tmp_y = y + # 如果是 2 表示是边上是我方子,1 表示敌方子 + if 0 <= y + y_offset <= 18 and 0 <= x + x_offset <= 18: + Cflag = self.chessData[y + y_offset][x + x_offset]['Cstate'] + if Cflag != 0: + for step in range(1, 6): + x += x_offset + 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) + 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) + _both += 1 + break + else: + if space is None: + space = False + else: + break # 遇到第二个空格退出 + elif Cflag == 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: + _nowcount += 1 + if _space is False: + _space = True + else: + if _space is None: + _space = False + else: + break + else: + # 遇到边也就是阻挡 + if Cflag == 2: + both += 1 + elif Cflag == 1: + _both += 1 + if space is False: + space = None + if _space is False: + _space = None + x = tmp_x + y = tmp_y + if 0 <= y - y_offset <= 18 and 0 <= x - x_offset <= 18: + _flag = self.chessData[y - y_offset][x - x_offset]['Cstate'] + if _flag != 0: + for step in range(1, 6): + x -= x_offset + 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: + nowcount += 1 + if space is False: + space = True + elif self.chessData[y][x]['Cstate'] == 1 and self.myColor == 2: + _both += 1 + break + else: + if space is None: + space = False + else: + break # 遇到第二个空格退出 + elif _flag == 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: + _nowcount += 1 + if _space is False: + _space = True + else: + if _space is None: + _space = False + else: + break + else: + # 遇到边也就是阻挡 + if _flag == 1: + both += 1 + elif _flag == 2: + _both += 1 + Score = 0 + if nowcount == 4: + Score = 10000 + elif _nowcount == 4: + Score = 9000 + elif nowcount == 3: + if both == 0: + Score = 1000 + elif both == 1: + Score = 100 + else: + Score = 0 + elif _nowcount == 3: + if _both == 0: + Score = 900 + elif _both == 1: + Score = 90 + else: + Score = 0 + elif nowcount == 2: + if both == 0: + Score = 100 + elif both == 1: + Score = 10 + else: + Score = 0 + elif _nowcount == 2: + if _both == 0: + Score = 90 + elif _both == 1: + Score = 9 + else: + Score = 0 + elif nowcount == 1: + Score = 10 + elif _nowcount == 1: + Score = 9 + else: + Score = 0 + if space or _space: + Score /= 2 + return Score + + def getPointScore(self, y, x): + Score = 0 + offset = [(1, 0), (0, 1), (1, 1), (1, -1)] + for os in offset: + Score += self.getDirectionScore(y, x, os[0], os[1]) + return Score + + def atuoActive(self): + _Score = 0 + position = [-1 for x in range(2)] + for i in range(0, 18): + for j in range(0, 18): + if self.chessData[i][j]['Cstate'] == 0: + Score = self.getPointScore(i, j) + if Score > _Score: + position[0] = i + position[1] = j + _Score = Score + elif Score == _Score: + r = random.randint(0, 100) + if r % 2 == 0: + position[0] = i + position[1] = j + return position + + # 设置的策略ai落子 + def aiLocation(self, canvas, blackChPieceImg, whiteChPieceImg): + print("do aiLocation") + if self.depth >= 9: + self.depth = 8 + position = self.returnChess(self.depth, self.player2Color) # 调用估值函数 + # position = self.atuoactive() + if self.chessData[position[0]][position[1]]['Cstate'] == 0: + # print("3:%d"%self.currentStep) + if self.player2Color == 2 and self.currentStep % 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=blackChPieceImg, anchor=W) + self.Chessimg[position[0]][position[1]] = oval + self.chessData[position[0]][position[1]]['Cstep'] = self.currentStep + elif self.player2Color == 1 and self.currentStep % 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=whiteChPieceImg, anchor=W) + self.Chessimg[position[0]][position[1]] = oval + self.chessData[position[0]][position[1]]['Cstep'] = self.currentStep + xregretFLAG[self.currentStep] = position[0] + yregretFLAG[self.currentStep] = position[1] + self.player = 1 # 玩家下棋标志置0 + self.player2 = 0 # 玩家2下棋标志置1 + self.currentStep += 1 + self.chessData[position[0]][position[1]]['Cstep'] = self.currentStep + a = max(abs(position[0] - 9), abs(position[1] - 9)) # 计算该点到中心的最大的距离 + self.depth = max(self.depth, a) # 不断更新Depth的值 + # print("4:%d" % self.currentStep) + return True + + + # 判断对局是否结束,如果结束则返回胜负信息 + def chessCheck(self): + 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: + 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: + 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: + 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: + 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: + 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: + 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: + 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: + self.winFlag = 1 + return 2 + # 5.判断是否是平局 + avanum = 0 + for x in range(19): + for y in range(19): + if self.chessData[x][y]['Cstate'] == 0: # 判断棋盘中是否有空位,有空位则非平局 + break + elif self.chessData[x][y]['Cstate'] != 0: + avanum += 1 + if avanum == 365: + return 3 + + # 展示结果 + def resultshow(self, root, canvas, whitech, blackch): + global photo, text + if self.winFlag == 1: + print("resultshow") + # 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) + # 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) + # 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) + if 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=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=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: + 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) + + + def clearLocation(self, canvas): + image_positions = [(750, 145), (870, 145)] + # Delete images and texts at specified positions + for position in image_positions: + canvas.delete(canvas.find_closest(*position)) + canvas.delete(canvas.find_closest(*position)) + canvas.delete(canvas.find_closest(*position)) + canvas.delete(canvas.find_closest(*position)) + # canvas.delete(canvas.find_closest(position[0] + 50, 150)) + canvas.update() + + # 设置右上方当前落子方状态显示 + def curLocation(self, canvas, player, player2, ChessMode, blackch, whitech, photos1, photos2): + print("curLocation !!") + + # 删除原来的图片,以便于更新展示现在的状态 + for item_id in self.PicLocIds: + canvas.delete(item_id) + # 清空ID列表以避免重复删除 + self.PicLocIds.clear() + + text_options = {'fill': 'white', 'font': 'Arial,9', 'anchor': W} + image_positions = [(750, 145), (870, 145)] + + image_data = [] + + if ChessMode == 0 or ChessMode == 1: + print("player and player2", player,player2) + if player == 1 and player2 == 0: + image_data = [(blackch, '玩家1'), (None, '玩家2')] + else: + image_data = [(None, '玩家1'), (whitech, '玩家2')] + elif ChessMode == 2: + if player == 1 and player2 == 0: + image_data = [(whitech, '玩家1'), (None, '玩家2')] + else: + image_data = [(None, '玩家1'), (blackch, '玩家2')] + + # 创建图像 + for i, (image, text) in enumerate(image_data): + id = canvas.create_image(*image_positions[i], image=photos1, anchor=W) + self.PicLocIds.append(id) # 保存图像id + id = canvas.create_image(*image_positions[i], image=image, anchor=W) + self.PicLocIds.append(id) + id = canvas.create_text(image_positions[i][0] + 50, 150, text=text, **text_options) + self.PicLocIds.append(id) + canvas.update() + + def playing(self, Chess_Mode, root, canvas, blackch, whitech, photos1, photos2): + if Chess_Mode == 1: + self.myColor = 2 # 先手玩家选择的棋子颜色 + self.player2Color = 1 # 后手玩家选择的棋子颜色 + # print("self.player2Color set", self.player2Color) + self.player = 1 + self.player2 = 0 + self.winFlag = 0 + self.curLocation(canvas, 1, 1, 1, blackch, whitech, photos1, photos2) + + elif Chess_Mode == 2: + self.myColor = 1 # 先手玩家选择的棋子颜色 + self.player2Color = 2 # 后手玩家选择的棋子颜色 + self.chessData[9][9]['Cstate'] = 2 + oval = canvas.create_image(9*36.8 + 41 - 14, 9*34.6 + 20, image=blackch, anchor=W) + self.Chessimg[9][9] = oval + xregretFLAG[self.currentStep] = 9 + yregretFLAG[self.currentStep] = 9 + # print("self.player2Color set", self.player2Color) + self.player = 1 # 玩家下棋标志置0 + self.player2 = 0 # 电脑下棋标志置1 + self.currentStep += 1 + self.winFlag = 0 + self.chessData[9][9]['Cstep'] = self.currentStep + 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 # 后手玩家选择的棋子颜色 + print("self.player2Color set", self.player2Color) + self.player = 1 + self.player2 = 0 + self.winFlag = 0 + self.curLocation(canvas, 1, 1, 1, blackch, whitech, photos1, photos2) + + + + # def playGameBlack(self, root, playerx, playery, canvas, blackch, whitech, result, photos1, photos2): + # if self.winFlag == 0 and self.player == 1 and self.computer == 0 and self.currentStep % 2 == 0: + # # 判断玩家点击的位置是否可以落子,并落子 + # if self.playerLocation(playerx, playery, canvas, blackch, whitech): + # # 如果使用了落子建议,则删除掉所画的落子点 + # for i in self.OvaloneNew: + # 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.currentStep % 2 != 0: + # root.update() + # self.aiLocation(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() + # + # def playGameWhite(self, root, playerx, playery, canvas, blackch, whitech, result, photos1, photos2): + # if self.winFlag == 0 and self.player == 1 and self.computer == 0 and self.currentStep % 2 == 1: + # if self.playerLocation(playerx, playery, canvas, blackch, whitech): + # # if self.OvaloneNew != 0: + # # print(self.Ovalone) + # # 如果使用了落子建议,则删除掉所画的落子点 + # for i in self.OvaloneNew: + # 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.aiLocation(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() + # + # + # 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.currentStep % 2 == 0: + # if self.playerLocation(playerx, playery, canvas, blackch, whitech): + # for i in self.OvaloneNew: + # canvas.delete(i) + # self.player2 = 1 + # self.Gameover = self.chessCheck() + # self.curLocation(canvas, 0, 0, 2, blackch, whitech, photos1, photos2) + # if self.winFlag == 1: + # self.resultshow(root, canvas, whitech, blackch) + # root.update() + # elif self.player2 == 1 and self.player == 0 and self.currentStep % 2 != 0: + # if self.playerLocation(playerx, playery, canvas, blackch, whitech): + # for i in self.OvaloneNew: + # canvas.delete(i) + # self.player = 1 + # self.player2 = 0 + # self.curLocation(canvas, 1, 1, 1, blackch, whitech, photos1, photos2) + # self.Gameover = self.chessCheck() + # if self.winFlag == 1: + # self.resultshow(root, canvas, whitech, blackch) + # root.update() + + # 返回一个位置数组 + def returnPostion(self, Depth, computercolor): + time.sleep(0.5) + self.newCount = 0 + self.maxScore = 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.countScore(i, j, computercolor) + self.newCount += 1 + if self.score[i][j] > 0: + self.counts += 1 + if self.maxScore < self.score[i][j]: + self.maxScore = 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.maxScore and self.chessData[i][j]['Cstate'] == 0: + pos[evaFLAG][0] = i + pos[evaFLAG][1] = j + evaFLAG += 1 + return pos + + # 返回建议落子点的位置和原因 + def returnPostionNew(self, Depth, computercolor): + + Depth = 8 + # depth = 2 + time.sleep(0.5) + self.newCount = 0 + self.maxScore = 0 + self.counts = 0 + tempPos = [] + tempVal = [] + Pos_get = [] + 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.countScore(i, j, computercolor) # 计算点的得分 + self.newCount += 1 + if self.score[i][j] > 0: + self.counts += 1 + if self.maxScore < self.score[i][j]: + self.maxScore = self.score[i][j] # 记录当前棋盘分数的最大值 + self.maxScorePos = (i, j) + + score, message = self.countScore(self.maxScorePos[0], self.maxScorePos[1], computercolor) # 计算点的得分 + # print(self.maxScorePos) + 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.maxScore - 100 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])) + # print(pos) + sorted_id = sorted(range(len(tempVal)), key=lambda k: tempVal[k], reverse=True) + for i in range(min(5,evaFLAG)): + Pos_get.append(tempPos[sorted_id[i]]) + return Pos_get, message + # return pos[:5], message diff --git a/chessboard.py b/chessboard.py new file mode 100644 index 0000000..35a71cf --- /dev/null +++ b/chessboard.py @@ -0,0 +1,868 @@ +import os +from tkinter import * +import tkinter.messagebox +from utils import * +import tkinter.scrolledtext as st +from chess import * +from PIL import Image, ImageTk +from datetime import datetime +import time +import numpy as np + +chess = Chess() +crossline = [] +verline = [] +regretnum = 0 +handlenum = 0 +flag = 0 +root = Tk() # 窗口对象 +root.title('五子棋') # 窗口标题 + +x = 10 +y = 10 +root.geometry('1000x700+'+str(x)+'+'+str(y)) # 窗口大小 width x height + x + y(x,y为在当前界面的坐标) +# 创建一个主目录菜单,也被称为顶级菜单 +main_menu = Menu(root) + +# menu_font2 = ("Arial", 14) +# main_menu.config(font=menu_font2) + +gamefile = Menu(main_menu, tearoff=False) + + +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 peoCompWhite(): + refresh() + chess.chessMode = 1 + mainCon.switch_mode(chess) + print(chess.chessMode) + chess.playing(1, root, canvas, blackChPieceImg, whiteChPieceImg, photos1, photos2) + + +# 人-机黑对弈 +def peoCompBlack(): + refresh() + chess.chessMode = 2 + mainCon.switch_mode(chess) + print(chess.chessMode) + chess.playing(2, root, canvas, blackChPieceImg, whiteChPieceImg, photos1, photos2) + + +# 双人模式 +def doublePeople(): + refresh() + chess.chessMode = 0 + mainCon.switch_mode(chess) + print(chess.chessMode) + chess.playing(0, root, canvas, blackChPieceImg, whiteChPieceImg, photos1, photos2) + + +# 开始游戏 +def startGame(): + win = Tk() + win.title("开始游戏") + win.geometry('500x300+100+100') + Label(win, text="五子棋", width=120, height=2).place(x=-175, y=50) + Button(win, text="人机模式(机白)", command=peoCompWhite, width=20, height=2).place(x=170, y=100) + Button(win, text="人机模式(机黑)", command=peoCompBlack, 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 saveCurrentBoard(): + # 创建一个空的二维数组data来保存当前棋盘状态 + data = [[0 for j in range(19)] for i in range(19)] + + file = open(path +'data.txt', 'w') + file.truncate(0) + for i in range(0, 19): + for j in range(0, 19): + data[i][j] = chess.chessData[i][j]['Cstate'] + data = np.mat(data) + b = '' + for i in range(0, 19): + for j in range(0, 19): + b += str(data[i, j]) + '\t' + b += '\n' + file.writelines(b) + # print(data) + # 创建一个空的二维数组data来保存当前棋盘落子步骤 + data1 = [[0 for j in range(19)] for i in range(19)] + for i in range(0, 19): + for j in range(0, 19): + data1[i][j] = chess.chessData[i][j]['Cstep'] + data = np.mat(data1) + b = '' + for i in range(0, 19): + for j in range(0, 19): + b += str(data[i, j]) + '\t' + b += '\n' + file.writelines(b) + # 将游戏模式、当前步数和胜利状态转换为字符串,并写入文件 + Chess_Mode = str(chess.chessMode) + '\n' + file.write(Chess_Mode) + step = str(chess.currentStep) + '\n' + file.write(step) + WinFLAG = str(chess.winFlag) + '\n' + file.write(WinFLAG) + file.close() + + +# 重装棋局 +def resetLastBoard(): + refresh() + # 读取保存的数据文件 + 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.chessMode = int(rtu[len(rtu) - 3]) + chess.currentStep = int(rtu[len(rtu) - 2]) + chess.winFlag = int(rtu[len(rtu) - 1]) + file.close() + + # 设置为记录中的模式 + mainCon.switch_mode(chess) + + 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=whiteChPieceImg, anchor=W) + elif chess.chessData[x][y]['Cstate'] == 2: + chess.Chessimg[x][y] = canvas.create_image(x * 36.8 + 41 - 14, y * 34.6 + 21, image=blackChPieceImg, anchor=W) + if chess.chessMode == 1 and chess.winFlag == 0: + chess.myColor = 2 + chess.player2Color = 1 + if chess.currentStep % 2 == 0: + chess.player = 1 + chess.player2 = 0 + chess.curLocation(canvas, 0, 1, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + tkinter.messagebox.showinfo(title='信息提示!', message='轮到玩家下棋了!') + elif chess.currentStep % 2 != 0: + pass # 正常不会出现电脑不落子的情况 + # chess.player = 0 + # chess.player2 = 1 + # chess.playGameBlack(root, None, None, canvas, blackChPieceImg, whiteChPieceImg, result, photos1, photos2) + elif chess.chessMode == 2 and chess.winFlag == 0: + chess.myColor = 1 + chess.player2Color = 2 + if chess.currentStep % 2 == 0: + pass # 正常不会出现电脑不落子的情况 + # chess.player = 0 + # chess.player2 = 1 + # chess.playGameWhite(root, None, None, canvas, blackChPieceImg, whiteChPieceImg, result, photos1, photos2) + if chess.currentStep % 2 != 0: + chess.player = 1 + chess.player2 = 0 + chess.curLocation(canvas, 0, 1, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + tkinter.messagebox.showinfo(title='信息提示!', message='轮到玩家下棋了!') + elif chess.chessMode == 0 and chess.winFlag == 0: + chess.myColor = 2 + chess.player2Color = 1 + if chess.currentStep % 2 == 0: + chess.player = 1 + chess.player2 = 0 + chess.curLocation(canvas, 1, 1, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + tkinter.messagebox.showinfo(title='信息提示!', message='轮到先手下棋了!') + elif chess.currentStep % 2 != 0: + chess.player = 0 + chess.player2 = 1 + chess.curLocation(canvas, 1, 0, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + tkinter.messagebox.showinfo(title='信息提示!', message='轮到后手下棋了!') + elif chess.winFlag == 1: + tkinter.messagebox.showinfo(title='信息提示!', message='对局已经结束了') + + +# 随机棋局 +def randomChessboard(): + 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.chessMode = int(rtu[len(rtu) - 3]) + chess.currentStep = 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=whiteChPieceImg, anchor=W) + elif chess.chessData[x][y]['Cstate'] == 2: + chess.Chessimg[x][y] = canvas.create_image(x * 36.8 + 41 - 14, y * 34.6 + 21, image=blackChPieceImg, anchor=W) + + # 设置为记录中的模式 + mainCon.switch_mode(chess) + + 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=whiteChPieceImg, anchor=W) + elif chess.chessData[x][y]['Cstate'] == 2: + chess.Chessimg[x][y] = canvas.create_image(x * 36.8 + 41 - 14, y * 34.6 + 21, image=blackChPieceImg, anchor=W) + if chess.chessMode == 1 and chess.winFlag == 0: + chess.myColor = 2 + chess.player2Color = 1 + if chess.currentStep % 2 == 0: + chess.player = 1 + chess.player2 = 0 + chess.curLocation(canvas, 1, 0, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + tkinter.messagebox.showinfo(title='信息提示!', message='轮到玩家下棋了!') + elif chess.currentStep % 2 != 0: + pass # 正常不会出现电脑不落子的情况 + # chess.player = 0 + # chess.player2 = 1 + # chess.playGameBlack(root, None, None, canvas, blackChPieceImg, whiteChPieceImg, result, photos1, photos2) + elif chess.chessMode == 2 and chess.winFlag == 0: + chess.myColor = 1 + chess.player2Color = 2 + if chess.currentStep % 2 == 0: + pass # 正常不会出现电脑不落子的情况 + # chess.player = 0 + # chess.player2 = 1 + # chess.playGameWhite(root, None, None, canvas, blackChPieceImg, whiteChPieceImg, result, photos1, photos2) + if chess.currentStep % 2 != 0: + chess.player = 1 + chess.player2 = 0 + chess.curLocation(canvas, 1, 0, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + tkinter.messagebox.showinfo(title='信息提示!', message='轮到玩家下棋了!') + elif chess.chessMode == 0 and chess.winFlag == 0: + chess.myColor = 2 + chess.player2Color = 1 + if chess.currentStep % 2 == 0: + chess.player = 1 + chess.player2 = 0 + chess.curLocation(canvas, 0, 1, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + tkinter.messagebox.showinfo(title='信息提示!', message='轮到先手下棋了!') + elif chess.currentStep % 2 != 0: + chess.player = 0 + chess.player2 = 1 + chess.curLocation(canvas, 1, 0, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + tkinter.messagebox.showinfo(title='信息提示!', message='轮到后手下棋了!') + elif chess.winFlag == 1: + tkinter.messagebox.showinfo(title='信息提示!', message='对局已经结束了') + + +menu_font2 = ("Microsoft YaHei UI", 12) + +gamefile.add_command(label="开始游戏", command=startGame, font=menu_font2) +gamefile.add_command(label="保存当前局面", command=saveCurrentBoard, accelerator="Ctrl+O", font=menu_font2) +gamefile.add_command(label="重装以前局面", command=resetLastBoard, accelerator="Ctrl+S", font=menu_font2) +gamefile.add_command(label="随机棋局", command=randomChessboard, font=menu_font2) +main_menu.add_cascade(label="游戏", menu=gamefile) # 在主目录菜单上新增"文件"选项,并通过menu参数与下拉菜单绑定 + + +# 游戏规则 +def helpGame(): + msg = ''' + 1、对局双方各执一色棋子。 + 2、空棋盘开局。 + 3、黑棋虽先行,但有禁手:黑方不能在一步之内形成两个“活三”“活四”或一步之内形成“长连”(指一步形成超过五子连珠)。白方自由,无禁手。 + 4、棋子下在棋盘的空白点上,棋子下定后,不得向其它点移动,不得从棋盘上拿掉或拿起另落别处。 + 5、黑方的第一枚棋子可下在棋盘任意交叉点上。 + ''' + tkinter.messagebox.showinfo(title='游戏规则', message=msg) + + +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) +photoQipan = Image.open(path + "image/棋盘-空.png").resize((730, 700)) +photoQipan = ImageTk.PhotoImage(photoQipan) +canvas.create_image(10, 350, image=photoQipan, anchor=W) + + +# # 绘制棋盘 +cross(canvas) +canvas.place(x=0, y=0) + + +class Player: + def NextMove(self, chess, blackch, whitech): + pass + def Suggestion(self): + pass + + +class HumanPlayer(Player): + + def NextMove(self, chessData, chess, blackch, whitech): + playerx = float(format(chessData.x)) + playery = float(format(chessData.y)) + print("playerx",playerx) + print("playery",playery) + if chess.winFlag == 0: + print("chess.player", chess.player) + print("chess.player2", chess.player2) + print("chess.currentStep % 2 == 0", chess.currentStep % 2 == 0) + # if chess.player == 1 and chess.player2 == 0 and chess.currentStep % 2 == 0: + if chess.player == 1 and chess.player2 == 0: + # 判断玩家点击的位置是否可以落子,并落子 + print("human 落子") + if chess.playerLocation(playerx, playery, canvas, blackch, whitech): + # 如果使用了落子建议,则删除掉所画的落子点 + for i in chess.OvaloneNew: + canvas.delete(i) + # 控制玩家下棋状态 + chess.player = 0 + chess.player2 = 1 + # 判断此次落子是否结束游戏 + chess.Gameover = chess.chessCheck() + # 设置右上方面板当前落子方状态显示 + chess.curLocation(canvas, 1, 0, chess.chessMode, blackch, whitech, photos1, photos2) + if chess.winFlag == 1: + chess.resultshow(root, canvas, whitech, blackch) + root.update() + # elif chess.player2 == 1 and chess.player == 0 and chess.currentStep % 2 != 0: + elif chess.player2 == 1 and chess.player == 0: + print("human 落子") + if chess.playerLocation(playerx, playery, canvas, blackch, whitech): + for i in chess.OvaloneNew: + canvas.delete(i) + chess.player = 1 + chess.player2 = 0 + chess.curLocation(canvas, 1, 0, chess.chessMode, blackch, whitech, photos1, photos2) + chess.Gameover = chess.chessCheck() + if chess.winFlag == 1: + chess.resultshow(root, canvas, whitech, blackch) + root.update() + + # 最新的得到建议的函数 + def Suggestion(self): + if chess.gameEnd() or chess.currentStep == 0: + 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.chessMode == 1: + color = 2 + elif chess.chessMode == 2: + color = 1 + elif chess.chessMode == 0: + if chess.player == 1 and chess.player2 == 0: + color = 2 + elif chess.player == 0 and chess.player2 == 1: + color = 1 + pos, message = chess.returnPostionNew(chess.depth, color) # 调用估值函数 + Text.insert('end', '最佳落子点为:{}\n'.format(chess.maxScorePos)) + Text.insert('end', message) + # print("pos:",pos) + print(pos) + # 画点 + drawChess(canvas, pos, chess) + root.update() + + +class RobotPlayer(Player): + + def NextMove(self, chessData, chess, blackch, whitech): + playerx = float(format(chessData.x)) + playery = float(format(chessData.y)) + print("playerx",playerx) + print("playery",playery) + if chess.winFlag == 0: + print("chess.player", chess.player) + print("chess.player2", chess.player2) + # print("chess.currentStep % 2 == 0", chess.currentStep % 2 == 0) + if chess.player == 1 and chess.player2 == 0: + # if chess.player == 1 and chess.player2 == 0 and chess.currentStep % 2 == 0: + # ai落子 + print("ai 落子") + if chess.aiLocation(canvas, blackch, whitech): + # 如果使用了落子建议,则删除掉所画的落子点 + for i in chess.OvaloneNew: + canvas.delete(i) + chess.player = 0 + chess.player2 = 1 + # 判断此次落子是否结束游戏 + chess.Gameover = chess.chessCheck() + # 设置右上方面板当前落子方状态显示 + chess.curLocation(canvas, 1, 0, chess.chessMode, blackch, whitech, photos1, photos2) + if chess.winFlag == 1: + chess.resultshow(root, canvas, whitech, blackch) + root.update() + elif chess.player2 == 1 and chess.player == 0: + # elif chess.player2 == 1 and chess.player == 0 and chess.currentStep % 2 != 0: + # if chess.playerLocation(playerx, playery, canvas, blackch, whitech): + print("ai 落子") + if chess.aiLocation(canvas, blackch, whitech): + print("ai 落子成功") + for i in chess.OvaloneNew: + canvas.delete(i) + chess.player = 1 + chess.player2 = 0 + chess.curLocation(canvas, 0, 1, chess.chessMode, blackch, whitech, photos1, photos2) + chess.Gameover = chess.chessCheck() + print("chess.Gameover", chess.Gameover) + if chess.winFlag == 1: + chess.resultshow(root, canvas, whitech, blackch) + root.update() + + # 最新的得到建议的函数 + def Suggestion(self): + if chess.gameEnd() or chess.currentStep == 0: + 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.chessMode == 1: + color = 2 + elif chess.chessMode == 2: + color = 1 + elif chess.chessMode == 0: + if chess.player == 1 and chess.player2 == 0: + color = 2 + elif chess.player == 0 and chess.player2 == 1: + color = 1 + pos, message = chess.returnPostionNew(chess.depth, color) # 调用估值函数 + Text.insert('end', '最佳落子点为:{}\n'.format(chess.maxScorePos)) + Text.insert('end', message) + print("pos:", pos) + # 画点 + drawChess(canvas, pos, chess) + root.update() + + +class mainControl(): + def __init__(self): + self.player1 = HumanPlayer() + self.player2 = RobotPlayer() + self.current_player = self.player1 + + def switch_mode(self, chess): + # 切换当前模式 + if chess.chessMode == 1: + self.player1 = HumanPlayer() + self.player2 = RobotPlayer() + self.current_player = self.player1 + elif chess.chessMode == 2: + # 先落一个黑子,然后与chess.chessMode == 1相同 + self.player1 = RobotPlayer() + self.player2 = HumanPlayer() + self.current_player = self.player2 + elif chess.chessMode == 0: + self.player1 = HumanPlayer() + self.player2 = HumanPlayer() + self.current_player = self.player1 + print(type(self.player1)) + print(type(self.player2)) + + + def switch_player(self): + # 切换当前玩家 + if self.current_player == self.player1: + self.current_player = self.player2 + else: + self.current_player = self.player1 + print(self.current_player) + + def on_mouse_click(self, chessdata): + # 如果是人机对弈 + if type(self.player1) is not type(self.player2): + # 处理鼠标点击事件 + self.current_player.NextMove(chessdata, chess, blackChPieceImg, whiteChPieceImg) + # 落子后切换玩家 + self.switch_player() + # print("on_mouse_click 换!") + self.current_player.NextMove(chessdata, chess, blackChPieceImg, whiteChPieceImg) + # 落子后切换玩家 + self.switch_player() + # print("on_mouse_click 换!") + # 如果是玩家对弈或者机器人对弈 + elif type(self.player1) is type(self.player2): + # 处理鼠标点击事件 + self.current_player.NextMove(chessdata, chess, blackChPieceImg, whiteChPieceImg) + # 落子后切换玩家 + self.switch_player() + + def suggestion(self): + # 处理落子推荐 + self.current_player.Suggestion() + +mainCon = mainControl() +canvas.bind('', mainCon.on_mouse_click) + + +# 图片创建 +blackChPieceImg = Image.open(path + "image/黑子-小.png").resize((40, 40)) +blackChPieceImg = ImageTk.PhotoImage(blackChPieceImg) +whiteChPieceImg = Image.open(path + "image/白子-小.png").resize((40, 40)) +whiteChPieceImg = ImageTk.PhotoImage(whiteChPieceImg) +result = Image.open(path + "image/resultshow.PNG").resize((100, 42)) +result = ImageTk.PhotoImage(result) +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) + +# 棋盘 +# photoQipan=Image.open("image/棋盘-空.png").resize((730,700)) +# photoQipan=ImageTk.PhotoImage(photoQipan) +# canvas.create_image(10,350,image=photoQipan,anchor=W) + +# 当前落子方 +# photocur = Image.open("image/当前落子方背景.png").resize((100, 30)) +# photocur = ImageTk.PhotoImage(photocur) +# canvas.create_image(720, 40, image = photocur,anchor=W) +canvas.create_text(800, 90, text='当前落子方', font='Arial,10', fill='white') +# lab_name = Label(root, text="当前落子方", font='Arial,10',fg='white',image=photocur,compound=CENTER) +# lab_name.place(x=630, y=85, width=100, height=30) +# 当前落子方跳选框 +canvas.create_image(750, 145, image=photos2, anchor=W) +canvas.create_image(870, 145, image=photos2, anchor=W) + +# 胜负判定 +photoresult = Image.open(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.config(bg='#8B7355') +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.Reviewflag != 1: + chess.Reviewflag = 1 + for x in range(19): + for y in range(19): + if chess.chessData[x][y]['Cstep'] != 0 and chess.chessData[x][y]['Cstate'] == 2: + reviewnum = Label(root, text=str(chess.chessData[x][y]['Cstep']), bg='#000', fg='#fff', font=("黑体", 8), width=1, height=1) + reviewnum.place(x=x*36.8 + 41 - 7, y=y*34.6 + 21 - 9, anchor='nw') + chess.Reviewlabel[x][y] = reviewnum + elif chess.chessData[x][y]['Cstep'] != 0 and chess.chessData[x][y]['Cstate'] == 1: + reviewnum = Label(root, text=str(chess.chessData[x][y]['Cstep']), bg='#fff', fg='#000', font=("黑体", 8), width=1, height=1) + reviewnum.place(x=x*36.8 + 41 - 7, y=y*34.6 + 21 - 9, anchor='nw') + chess.Reviewlabel[x][y] = reviewnum + root.update() + + +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) + +btn_review.place(x=750, y=400) + + +# 按钮悔棋 +def regretChess(): + if chess.winFlag == 0: + # 首先撤销复盘,防止删不掉 + concelReview() + global regretnum + regretnum = 1 + if chess.currentStep == 1 and chess.chessMode == 2: + regretxFLAG = xregretFLAG[chess.currentStep - regretnum] + regretyFLAG = yregretFLAG[chess.currentStep - regretnum] + canvas.delete(chess.Chessimg[regretxFLAG][regretyFLAG]) + chess.chessData[regretxFLAG][regretyFLAG]['Cstate'] = 0 + chess.chessData[regretxFLAG][regretyFLAG]['Cstep'] = 0 + chess.currentStep -= 1 + chess.playing(2, root, canvas) + if chess.chessMode == 1 or chess.chessMode == 2 or chess.chessMode == 4: + # print(xregretFLAG) + # print(yregretFLAG) + for i in range(2): + regretxFLAG = xregretFLAG[chess.currentStep - regretnum] + regretyFLAG = yregretFLAG[chess.currentStep - regretnum] + canvas.delete(chess.Chessimg[regretxFLAG][regretyFLAG]) + chess.chessData[regretxFLAG][regretyFLAG]['Cstate'] = 0 + chess.chessData[regretxFLAG][regretyFLAG]['Cstep'] = 0 + chess.currentStep -= 1 + chess.curLocation(canvas, 1, 0, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + if chess.chessMode == 0: + regretxFLAG = xregretFLAG[chess.currentStep - regretnum] + regretyFLAG = yregretFLAG[chess.currentStep - regretnum] + canvas.delete(chess.Chessimg[regretxFLAG][regretyFLAG]) + chess.chessData[regretxFLAG][regretyFLAG]['Cstate'] = 0 + chess.chessData[regretxFLAG][regretyFLAG]['Cstep'] = 0 + chess.currentStep -= 1 + if chess.currentStep % 2 == 0: + chess.player = 1 + chess.player2 = 0 + chess.curLocation(canvas, 0, 1, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + elif chess.currentStep % 2 != 0: + chess.player = 0 + chess.player2 = 1 + chess.curLocation(canvas, 1, 0, chess.chessMode, blackChPieceImg, whiteChPieceImg, photos1, photos2) + elif chess.winFlag == 1: + tkinter.messagebox.showinfo(title='信息提示!', message='对局已经结束了!') + root.update() + + +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=regretChess, bd=0) +btn_regret.place(x=850, y=400) + + +# 按钮撤销复盘 +def concelReview(): + if chess.Reviewflag == 1: + chess.Reviewflag = 0 + for x in range(19): + for y in range(19): + if chess.chessData[x][y]['Cstep'] != 0 and chess.Reviewlabel[x][y] != 0: + chess.Reviewlabel[x][y].destroy() + chess.Reviewlabel[x][y] = 0 + root.update() + + +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) +btn_concel.place(x=750, y=460) + + +photoSuggest = Image.open(path + "image/落子建议按钮.png").resize((100, 50)) +photoSuggest = ImageTk.PhotoImage(photoSuggest) +btnSuggest = Button(root, text='落子建议', font='Arial,12', width=85, height=35, + image=photoSuggest, command=mainCon.suggestion, bd=0) +btnSuggest.place(x=850, y=460) + + +# 按钮局面评估 +def curNow(): + + if chess.depth >= 9: + chess.depth = 8 + if chess.chessMode == 1: + color = 2 + elif chess.chessMode == 2: + color = 1 + chess.returnChess(chess.depth, color) # 调用估值函数 + elif chess.chessMode == 0: + if chess.player == 1 and chess.player2 == 0: + color = 2 + elif chess.player == 0 and chess.player2 == 1: + color = 1 + chess.returnChess(chess.depth, color) # 调用估值函数 + if chess.counts / chess.newCount == 1: + OwnCounter = random.uniform(0.5, 0.6) + else: + OwnCounter = chess.counts / chess.newCount + if color == 2: + # 1.判断x-轴是否活四或者连五 + 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) 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'] == 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) 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) 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(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) +btn_elevalue.place(x=750, y=520) + + +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=peoCompWhite, bg='#FFA500', bd=0) +btn_reset.place(x=800, y=580) + + +# 按钮重新开始 +def refresh(): + # 删除棋盘上可能的复盘数字 + concelReview() + + # 删除棋子 + for x in range(19): + for y in range(19): + if chess.chessData[x][y]['Cstate'] != 0: + canvas.delete(chess.Chessimg[x][y]) + if chess.Reviewlabel[x][y] != 0: + chess.Reviewlabel[x][y].destroy() + chess.chessData[x][y]['Cstate'] = 0 + chess.chessData[x][y]['Cstep'] = 0 + chess.Chessimg[x][y] = 0 + chess.Reviewlabel[x][y] = 0 + + # 删除棋盘上可能留有的建议落子图 + for i in chess.OvaloneNew: + canvas.delete(i) + + # 这里需要重新添加一个刷新的东西类似于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(path + "image/棋盘-空.png").resize((730, 700)) + photoqizi = ImageTk.PhotoImage(photoqizi) + canvas.create_image(10, 350, image=photoqizi, anchor=W) + cross(canvas) + + # 重置棋盘的变量 + chess.currentStep = 0 # 当前落子步数 + chess.Gameover = 0 # 对局是否结束 + chess.depth = 0 # 在棋盘搜索广度 + chess.player = 0 # 轮到下棋的标志,1=下,0=不下 + chess.myColor = 0 # 玩家选择的棋子颜色 + chess.player2Color = 0 # 玩家2的棋子颜色 + chess.winFlag = 0 # 胜利标志 + + # 清空结果展示 + if chess.winFlag == 1: + chess.resultshow(root, canvas, whiteChPieceImg, blackChPieceImg) + + # 重置玩家和变量 + mainCon.switch_mode(chess) + chess.playing(chess.chessMode, root, canvas, blackChPieceImg, whiteChPieceImg, photos1, photos2) + + root.update() + + +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) + +canvas.pack() +root.mainloop() diff --git a/data.txt b/data.txt new file mode 100644 index 0000000..9178516 --- /dev/null +++ b/data.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 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0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 2 1 8 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 3 4 10 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 12 9 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 7 0 11 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 13 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 +1 +14 +0 diff --git a/image/blackchess.png b/image/blackchess.png new file mode 100644 index 0000000..f13fdcb Binary files /dev/null and b/image/blackchess.png differ diff --git a/image/chessbdbg.png b/image/chessbdbg.png new file mode 100644 index 0000000..1b10d62 Binary files /dev/null and b/image/chessbdbg.png differ diff --git a/image/chessbdg.jpg b/image/chessbdg.jpg new file mode 100644 index 0000000..134d90c Binary files /dev/null and b/image/chessbdg.jpg differ diff --git a/image/concel.png b/image/concel.png new file mode 100644 index 0000000..de18c29 Binary files /dev/null and b/image/concel.png differ diff --git a/image/curnow.png b/image/curnow.png new file mode 100644 index 0000000..0046ddf Binary files /dev/null and b/image/curnow.png differ diff --git a/image/currentlocation.png b/image/currentlocation.png new file mode 100644 index 0000000..e7d2d7a Binary files /dev/null and b/image/currentlocation.png differ diff --git a/image/currentlocationd.png b/image/currentlocationd.png new file mode 100644 index 0000000..e5742d5 Binary files /dev/null and b/image/currentlocationd.png differ diff --git a/image/gameresult.png b/image/gameresult.png new file mode 100644 index 0000000..ef4a132 Binary files /dev/null and b/image/gameresult.png differ diff --git a/image/refresh.png b/image/refresh.png new file mode 100644 index 0000000..4a7975c Binary files /dev/null and b/image/refresh.png differ diff --git a/image/regretch.png b/image/regretch.png new file mode 100644 index 0000000..f3a7bfe Binary files /dev/null and b/image/regretch.png differ diff --git a/image/result.jpg b/image/result.jpg new file mode 100644 index 0000000..09cf09a Binary files /dev/null and b/image/result.jpg differ diff --git a/image/resultshow.PNG b/image/resultshow.PNG new file mode 100644 index 0000000..6bc8db5 Binary files /dev/null and b/image/resultshow.PNG differ diff --git a/image/review.png b/image/review.png new file mode 100644 index 0000000..758aaf4 Binary files /dev/null and b/image/review.png differ diff --git a/image/suggestion.png b/image/suggestion.png new file mode 100644 index 0000000..fffc13c Binary files /dev/null and b/image/suggestion.png differ diff --git a/image/true.jpg b/image/true.jpg new file mode 100644 index 0000000..d8e1e41 Binary files /dev/null and b/image/true.jpg differ diff --git a/image/whitechess.png b/image/whitechess.png new file mode 100644 index 0000000..1fd868e Binary files /dev/null and b/image/whitechess.png differ diff --git a/image/上方button-未选中.png b/image/上方button-未选中.png new file mode 100644 index 0000000..bcefe78 Binary files /dev/null and b/image/上方button-未选中.png differ diff --git a/image/上方button-选中.png b/image/上方button-选中.png new file mode 100644 index 0000000..cfba87f Binary files /dev/null and b/image/上方button-选中.png differ diff --git a/image/信息框.jpg b/image/信息框.jpg new file mode 100644 index 0000000..6b27b94 Binary files /dev/null and b/image/信息框.jpg differ diff --git a/image/公告栏.png b/image/公告栏.png new file mode 100644 index 0000000..d12cb7f Binary files /dev/null and b/image/公告栏.png differ diff --git a/image/复盘.png b/image/复盘.png new file mode 100644 index 0000000..c10c591 Binary files /dev/null and b/image/复盘.png differ diff --git a/image/局面评估.png b/image/局面评估.png new file mode 100644 index 0000000..5ebd68d Binary files /dev/null and b/image/局面评估.png differ diff --git a/image/当前落子方背景.PNG b/image/当前落子方背景.PNG new file mode 100644 index 0000000..18d19b9 Binary files /dev/null and b/image/当前落子方背景.PNG differ diff --git a/image/悔棋.png b/image/悔棋.png new file mode 100644 index 0000000..493a1ef Binary files /dev/null and b/image/悔棋.png differ diff --git a/image/撤销复盘.png b/image/撤销复盘.png new file mode 100644 index 0000000..05860a6 Binary files /dev/null and b/image/撤销复盘.png differ diff --git a/image/未选中.PNG b/image/未选中.PNG new file mode 100644 index 0000000..605a974 Binary files /dev/null and b/image/未选中.PNG differ diff --git a/image/棋盘-空.png b/image/棋盘-空.png new file mode 100644 index 0000000..8c5843c Binary files /dev/null and b/image/棋盘-空.png differ diff --git a/image/棋盘图.png b/image/棋盘图.png new file mode 100644 index 0000000..736a31b Binary files /dev/null and b/image/棋盘图.png differ diff --git a/image/白子-大.png b/image/白子-大.png new file mode 100644 index 0000000..e8748d8 Binary files /dev/null and b/image/白子-大.png differ diff --git a/image/白子-小.png b/image/白子-小.png new file mode 100644 index 0000000..40f8bbe Binary files /dev/null and b/image/白子-小.png differ diff --git a/image/背景.png b/image/背景.png new file mode 100644 index 0000000..7af92c3 Binary files /dev/null and b/image/背景.png differ diff --git a/image/胜负判定背景.PNG b/image/胜负判定背景.PNG new file mode 100644 index 0000000..78655ba Binary files /dev/null and b/image/胜负判定背景.PNG differ diff --git a/image/落子建议按钮.png b/image/落子建议按钮.png new file mode 100644 index 0000000..336fcee Binary files /dev/null and b/image/落子建议按钮.png differ diff --git a/image/重新开始按钮.png b/image/重新开始按钮.png new file mode 100644 index 0000000..b334062 Binary files /dev/null and b/image/重新开始按钮.png differ diff --git a/image/黑子-大.png b/image/黑子-大.png new file mode 100644 index 0000000..4cd5a78 Binary files /dev/null and b/image/黑子-大.png differ diff --git a/image/黑子-小.png b/image/黑子-小.png new file mode 100644 index 0000000..16e3bf1 Binary files /dev/null and b/image/黑子-小.png differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..70711c3 --- /dev/null +++ b/main.py @@ -0,0 +1,24 @@ +# This is a sample Python script. +import chessboard +from chessboard import * +# +# Press Shift+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + root.mainloop() + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ + + + + + + + + + + + + diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..278b76c --- /dev/null +++ b/utils.py @@ -0,0 +1,104 @@ +from PIL import Image, ImageTk +from tkinter import * + +local = True +# local = False +if local == True: + path = "" +else: + path = "/data/workspace/myshixun/finalchess/" + + +# 绘制棋盘 +def cross(canvas): + for i in range(19): # 横线 + point = [[40, 37], [706, 37]] + point[0][1] = 21 + i*34.3 + point[1][1] = 21 + i*34.3 + if i == 0 or i == 18: + canvas.create_line(point, fill="#C18B5F", width=2.5) + canvas.create_line(point, fill="#C18B5F", width=1.5) + for i in range(19): # 竖线 + point = [[41, 21], [41, 640]] + point[0][0] = 41 + i*37 + point[1][0] = 41 + i*37 + if i == 0 or i == 18: + canvas.create_line(point, fill="#C18B5F", width=2.5) + canvas.create_line(point, fill="#C18B5F", width=1.5) + for x in (4, 9, 14): + for y in (4, 9, 14): + if x == y == 9: + radius = 9 + else: + radius = 6 + canvas.create_oval(x*36.9 + 41 - radius / 2, + y*34.3 + 21 - radius / 2, + x*36.9 + 41 + radius / 2, + y*34.3 + 21 + radius / 2, + fill='#BE875E', outline="") + +# 画棋子 +def drawChess(canvas, pos, chess): + 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.player2Color == 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.player2Color == 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.OvaloneNew.append(Ovalone) + + +# def pics_set(canvas, root): +# # 图片创建 +# blackChPieceImg = Image.open(path + "image/黑子-小.png").resize((40, 40)) +# blackChPieceImg = ImageTk.PhotoImage(blackChPieceImg) +# whiteChPieceImg = Image.open(path + "image/白子-小.png").resize((40, 40)) +# whiteChPieceImg = ImageTk.PhotoImage(whiteChPieceImg) +# result = Image.open(path + "image/resultshow.PNG").resize((100, 42)) +# result = ImageTk.PhotoImage(result) +# 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) +# +# # 棋盘 +# # photoQipan=Image.open("image/棋盘-空.png").resize((730,700)) +# # photoQipan=ImageTk.PhotoImage(photoQipan) +# # canvas.create_image(10,350,image=photoQipan,anchor=W) +# +# # 当前落子方 +# # photocur = Image.open("image/当前落子方背景.png").resize((100, 30)) +# # photocur = ImageTk.PhotoImage(photocur) +# # canvas.create_image(720, 40, image = photocur,anchor=W) +# canvas.create_text(800, 90, text='当前落子方', font='Arial,10', fill='white') +# # lab_name = Label(root, text="当前落子方", font='Arial,10',fg='white',image=photocur,compound=CENTER) +# # lab_name.place(x=630, y=85, width=100, height=30) +# # 当前落子方跳选框 +# canvas.create_image(750, 145, image=photos2, anchor=W) +# canvas.create_image(870, 145, image=photos2, anchor=W) +# +# # 胜负判定 +# photoresult = Image.open(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.config(bg='#8B7355') +# 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) \ No newline at end of file