diff --git a/国际数棋-双界面/国际数棋-双界面/Philter - Below The Surface.mp3 b/国际数棋-双界面/国际数棋-双界面/Philter - Below The Surface.mp3 new file mode 100644 index 0000000..8ded2d8 Binary files /dev/null and b/国际数棋-双界面/国际数棋-双界面/Philter - Below The Surface.mp3 differ diff --git a/国际数棋-双界面/国际数棋-双界面/R-C.jpg b/国际数棋-双界面/国际数棋-双界面/R-C.jpg new file mode 100644 index 0000000..4ca36dc Binary files /dev/null and b/国际数棋-双界面/国际数棋-双界面/R-C.jpg differ diff --git a/国际数棋-双界面/国际数棋-双界面/README.md b/国际数棋-双界面/国际数棋-双界面/README.md new file mode 100644 index 0000000..b50da14 --- /dev/null +++ b/国际数棋-双界面/国际数棋-双界面/README.md @@ -0,0 +1,5 @@ +1.使用方法 + +在cmd进入该文件夹下![image-20220825080709679](E:\程序设计课程\国际数棋-双界面\国际数棋-双界面\image-20220825080709679.png) + +键入命令: `python .\chess_main.py` 开始游戏 \ No newline at end of file diff --git a/国际数棋-双界面/国际数棋-双界面/STXINGKA.TTF b/国际数棋-双界面/国际数棋-双界面/STXINGKA.TTF new file mode 100644 index 0000000..353ba6a Binary files /dev/null and b/国际数棋-双界面/国际数棋-双界面/STXINGKA.TTF differ diff --git a/国际数棋-双界面/国际数棋-双界面/__pycache__/client.cpython-38.pyc b/国际数棋-双界面/国际数棋-双界面/__pycache__/client.cpython-38.pyc new file mode 100644 index 0000000..b0a4bc5 Binary files /dev/null and b/国际数棋-双界面/国际数棋-双界面/__pycache__/client.cpython-38.pyc differ diff --git a/国际数棋-双界面/国际数棋-双界面/__pycache__/client.cpython-39.pyc b/国际数棋-双界面/国际数棋-双界面/__pycache__/client.cpython-39.pyc new file mode 100644 index 0000000..aaf5d22 Binary files /dev/null and b/国际数棋-双界面/国际数棋-双界面/__pycache__/client.cpython-39.pyc differ diff --git a/国际数棋-双界面/国际数棋-双界面/chess_main.py b/国际数棋-双界面/国际数棋-双界面/chess_main.py new file mode 100644 index 0000000..4df20a2 --- /dev/null +++ b/国际数棋-双界面/国际数棋-双界面/chess_main.py @@ -0,0 +1,1694 @@ +# -*- coding: utf-8 -*- +""" +Created on Mon Aug 23 08:44:51 2021 + +@author: xqs +""" +import pygame +import re +import sys +import os +from tkinter import * +from pygame.locals import * +from PIL import Image, ImageTk +import time +import random +import threading +import pygame.gfxdraw +import client +from client import * + +##############模块############### +ScreenWidth=1200 #窗体大小 +ScreenHi=800 +CHESSSIZE=30 #棋子大小 +R=25 +GAP=3 #棋子间距 +XMARGIN=int((ScreenWidth-(CHESSSIZE+GAP)*28)/2) #左边距 +TOPMARGIN=int((ScreenHi-(CHESSSIZE+GAP)*18)/2) #上边距 +#定义颜色R G B +BLACK = (0,0,0) +LIGHTBLUE = (40,191,255) +LIGHTBLUE2 = (0,0,255) +BGCOLOR = (230,230,250) +MAGENTA = (255,0,255) +LIGHTYELLOW= (255,225,0) +MYCOLOR = (255,165,0) +WHITE = (255,255,255) +GREEN = (0,255,0) +RED = (255,0,0) +BLUE = (0,0,255) +Crimson=(220,20,60) +LightPink=(255,182,193) +DeepSkyBlue=(0,191,255) +Screen=pygame.display.set_mode((ScreenWidth,ScreenHi))#设置窗体 +chosenChess=None #选择的棋子(将要移动) +coverdChess=None #点击的目标位置(将被覆盖) +currentChess=None #当前点击的棋子 +index=0 #记录棋盘上的棋子数量 +#定义棋盘 +CHECKERBOARD={} +CHECKERBOARD[64] = [14,7,11] +CHECKERBOARD[63] = [13,8,20] +CHECKERBOARD[62] = [13,6,19] +CHECKERBOARD[61] = [12,9,16] +CHECKERBOARD[60] = [12,7,17] +CHECKERBOARD[59] = [12,5,18] +CHECKERBOARD[58] = [11,10,15] +CHECKERBOARD[56] = [11,8,13] +CHECKERBOARD[57] = [11,6,14] +CHECKERBOARD[55] = [11,4,12] +CHECKERBOARD[54] = [10,11,0] +CHECKERBOARD[53] = [10,9,0] +CHECKERBOARD[52] = [10,7,0] +CHECKERBOARD[51] = [10,5,0] +CHECKERBOARD[50] = [10,3,0] +CHECKERBOARD[49] = [9,12,0] +CHECKERBOARD[48] = [9,10,0] +CHECKERBOARD[47] = [9,8,0] +CHECKERBOARD[46] = [9,6,0] +CHECKERBOARD[45] = [9,4,0] +CHECKERBOARD[44] = [9,2,0] +CHECKERBOARD[43] = [8,13,0] +CHECKERBOARD[42] = [8,11,0] +CHECKERBOARD[41] = [8,9,0] +CHECKERBOARD[40] = [8,7,0] +CHECKERBOARD[39] = [8,5,0] +CHECKERBOARD[38] = [8,3,0] +CHECKERBOARD[37] = [8,1,0] +CHECKERBOARD[36] = [7,14,0] +CHECKERBOARD[35] = [7,12,0] +CHECKERBOARD[34] = [7,10,0] +CHECKERBOARD[33] = [7,8,0] +CHECKERBOARD[32] = [7,6,0] +CHECKERBOARD[31] = [7,4,0] +CHECKERBOARD[30] = [7,2,0] +CHECKERBOARD[29] = [7,0,0] +CHECKERBOARD[28] = [6,13,0] +CHECKERBOARD[27] = [6,11,0] +CHECKERBOARD[26] = [6,9,0] +CHECKERBOARD[25] = [6,7,0] +CHECKERBOARD[24] = [6,5,0] +CHECKERBOARD[23] = [6,3,0] +CHECKERBOARD[22] = [6,1,0] +CHECKERBOARD[21] = [5,12,0] +CHECKERBOARD[20] = [5,10,0] +CHECKERBOARD[19] = [5,8,0] +CHECKERBOARD[18] = [5,6,0] +CHECKERBOARD[17] = [5,4,0] +CHECKERBOARD[16] = [5,2,0] +CHECKERBOARD[15] = [4,11,0] +CHECKERBOARD[14] = [4,9,0] +CHECKERBOARD[13] = [4,7,0] +CHECKERBOARD[12] = [4,5,0] +CHECKERBOARD[11] = [4,3,0] +CHECKERBOARD[10] = [1,6,10] +CHECKERBOARD[9] = [1,8,9] +CHECKERBOARD[8] = [2,9,8] +CHECKERBOARD[7] = [2,7,7] +CHECKERBOARD[6] = [2,5,6] +CHECKERBOARD[5] = [3,4,5] +CHECKERBOARD[4] = [3,8,4] +CHECKERBOARD[3] = [3,6,3] +CHECKERBOARD[2] = [3,10,2] +CHECKERBOARD[1] = [0,7,1] + + +###################end###################### +def reProgram(): + py = sys.executable + os.execl(py, py, * sys.argv) +###################start棋盘绘制模块#################### +def draw_checkerboard(od):#绘制棋盘格子 + #cnt = 0 + ind = 0 + ynd = 7 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 2 + ynd = 5 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 1 + ynd = 6 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 3 + ynd = 4 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 4 + ynd = 3 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 5 + ynd = 2 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 6 + ynd = 1 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + + ind=14 + ynd=7 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=13 + ynd=6 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=12 + ynd=5 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=11 + ynd=4 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=10 + ynd=3 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=9 + ynd=2 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=8 + ynd=1 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + + draw_grid() + color4 = (0,0,0) + + f1= pygame.font.Font('font.TTF', 20) + f2 = pygame.font.Font('font.TTF', 30) + t_RedSoce = f1.render("红方得分", True, RED) + t_Redlose = f2.render("认输", True, RED) + t_Bluelose = f2.render("认输", True, BLUE) + t_BlueSoce = f1.render("蓝方得分", True, BLUE) + t_RedStop = f2.render("红方叫停", True, RED) + t_BlueStop = f2.render("蓝方叫停", True, BLUE) + t_QuitGame = f2.render("结束游戏", True, BLUE) + t_StepBack = f2.render("悔棋", True, RED) + + Screen.blit(t_RedSoce, (1030, 130))#红方得分 + Screen.blit(t_BlueSoce, (90, 130))#蓝方得分 + Screen.blit(t_RedStop, (1010, 20))#红方叫停 + Screen.blit(t_BlueStop, (70, 20))#蓝方叫停 + + Screen.blit(t_StepBack, (570, 700))#悔棋 + Screen.blit(t_Redlose, (1050, 550))#红方认输 + Screen.blit(t_Bluelose, (70, 550))#蓝方认输 + Screen.blit(t_QuitGame, (540, 25))#游戏结束 + + print_score(my_Scores(),his_Scores()) + my_font = pygame.font.SysFont("freesansbold.ttf", 100) + + if od==1: + t_Redlose = my_font.render("RED PLAY", True, RED) + Screen.blit(t_Redlose, (430, 600)) + else: + t_StepBack = my_font.render("BULE PLAY", True, BLUE) + Screen.blit(t_StepBack, (410, 600)) + + +def draw_checkerboard_net(od):#绘制棋盘格子$$$ + ind = 0 + ynd = 7 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 2 + ynd = 5 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 1 + ynd = 6 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 3 + ynd = 4 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 4 + ynd = 3 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 5 + ynd = 2 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + ind = 6 + ynd = 1 + while ind < 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind+1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind+1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind+=1 + ynd += 1 + + ind=14 + ynd=7 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=13 + ynd=6 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=12 + ynd=5 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=11 + ynd=4 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=10 + ynd=3 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=9 + ynd=2 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + ind=8 + ynd=1 + while ind > 7: + x1,y1=getChessXY(ind,ynd) + x2,y2=getChessXY(ind-1,ynd+1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),2) + + x3,y3=getChessXY(ind-1,ynd-1) + pygame.draw.line(Screen,BLACK,(x1+int(CHESSSIZE/2),y1+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + pygame.draw.line(Screen,BLACK,(x2+int(CHESSSIZE/2),y2+int(CHESSSIZE/2)),(x3+int(CHESSSIZE/2),y3+int(CHESSSIZE/2)),2) + ind=ind-1 + ynd += 1 + + draw_grid() + color4 = (0,0,0) + + font1 = pygame.font.Font('font.TTF', 20) + font2 = pygame.font.Font('font.TTF', 30) + text1 = font1.render("红方得分", True, RED) + text2 = font1.render("蓝方得分", True, BLUE) + #text3 = font2.render("红方叫停", True, RED) + #text4 = font2.render("蓝方叫停", True, BLUE) + text3 = font2.render("叫停",True,MYCOLOR) + + text5 = font2.render("举报", True, RED) + + text6 = font2.render("认输", True, MYCOLOR) + text7 = font2.render("结束游戏", True, BLUE) + #text8 = font2.render("认输", True, BLUE) + + Screen.blit(text1, (1030, 130))#红方得分 + Screen.blit(text2, (90, 130))#蓝方得分 + Screen.blit(text3, (1010, 20))#MY叫停 + #Screen.blit(text4, (70, 20))#蓝方叫停 + Screen.blit(text5, (150, 550))#悔棋 + Screen.blit(text6, (1050, 550))#认输 + Screen.blit(text7, (70, 20))#游戏结束 + #Screen.blit(text8, (70, 550))#蓝方认输 + + print_score(my_Scores(),his_Scores()) + my_font = pygame.font.SysFont("freesansbold.ttf", 100) + + if od==1: + text6 = my_font.render("RED PLAY", True, RED) + Screen.blit(text6, (430, 600)) + else: + text5 = my_font.render("BULE PLAY", True, BLUE) + Screen.blit(text5, (410, 600)) + + +def draw_grid():#绘制格子 + basicFont = pygame.font.Font(None, 24) + i = 1 + while i < 65: + x=CHECKERBOARD[i][0] + y=CHECKERBOARD[i][1] + x1, y1 = getChessXY(CHECKERBOARD[i][0], CHECKERBOARD[i][1]) + pygame.draw.circle(Screen,BLACK,(XMARGIN+2*x*(CHESSSIZE+GAP),TOPMARGIN+y*(CHESSSIZE+GAP)),int(R/2)-2,2)#格底黑圈 + if i <= 10: + pygame.draw.circle(Screen,LIGHTBLUE,(XMARGIN+2*x*(CHESSSIZE+GAP),TOPMARGIN+y*(CHESSSIZE+GAP)),int(R/2)-3,0)#红方格底 + k=str((i-1)%10) + Screen.blit(basicFont.render(k, True, BLACK),(x1+CHESSSIZE/3,y1+CHESSSIZE/3-1)) + elif i>10 and i<55: + #pygame.draw.circle(Screen, WHITE,(XMARGIN + 2 * x * (CHESSSIZE + GAP), TOPMARGIN + y * (CHESSSIZE + GAP)),int(R/2) - 3, 0)#空白格底 + pygame.gfxdraw.aacircle(Screen, XMARGIN + 2 * x * (CHESSSIZE + GAP), TOPMARGIN + y * (CHESSSIZE + GAP),int(R)-9, (100,100,100)) + pygame.gfxdraw.filled_circle(Screen, XMARGIN + 2 * x * (CHESSSIZE + GAP), TOPMARGIN + y * (CHESSSIZE + GAP),int(R)-10, WHITE) + + else: + pygame.draw.circle(Screen, RED,(XMARGIN + 2 * x * (CHESSSIZE + GAP), TOPMARGIN + y * (CHESSSIZE + GAP)),int(R/2) - 3, 0)#蓝方格底 + if i==64: + Screen.blit(basicFont.render("0", True, BLACK), (x1 + CHESSSIZE / 3, y1 + CHESSSIZE / 3-1)) + else: + k = str(i-54) + Screen.blit(basicFont.render(k, True, BLACK), (x1 + CHESSSIZE / 3, y1 + CHESSSIZE / 3-1)) + i+=1 + +#####################end棋盘绘制模块################### + +#####################start棋子绘制模块################# +def drawChess():#绘制棋子 + basicFont = pygame.font.Font(None, 24) + i = 1 + #for i in range(1,65): + while i < 65: + if CHECKERBOARD[i][2]>0: + x=CHECKERBOARD[i][0] + y=CHECKERBOARD[i][1] + if CHECKERBOARD[i][2]<=10: + pygame.draw.circle(Screen,LIGHTBLUE,(XMARGIN+2*x*(CHESSSIZE+GAP),TOPMARGIN+y*(CHESSSIZE+GAP)),int(CHESSSIZE),0) + pygame.draw.circle(Screen,LIGHTBLUE2,(XMARGIN+2*x*(CHESSSIZE+GAP),TOPMARGIN+y*(CHESSSIZE+GAP)),int(CHESSSIZE)-3,0) + else: + pygame.gfxdraw.filled_circle(Screen,XMARGIN+2*x*(CHESSSIZE+GAP),TOPMARGIN+y*(CHESSSIZE+GAP),int(CHESSSIZE),LightPink) + pygame.draw.circle(Screen, Crimson,(XMARGIN + 2 * x * (CHESSSIZE + GAP), TOPMARGIN + y * (CHESSSIZE + GAP)),int(CHESSSIZE ) - 3,0) + q = str((CHECKERBOARD[i][2]-1)%10) + Screen.blit(basicFont.render(q, True, BLACK), (XMARGIN+2*x*(CHESSSIZE+GAP)-6,TOPMARGIN+y*(CHESSSIZE+GAP)-7)) + i += 1 + +def getChessXY(chessX,chessY):#得到的棋子坐标 + left=XMARGIN+2*chessX*(CHESSSIZE+GAP)-int(CHESSSIZE/2) + top=TOPMARGIN+chessY*(CHESSSIZE+GAP)-int(CHESSSIZE/2) + return(left,top) + +def getChessIndex(x,y):#将的棋子的坐标转换成编号 + for i in range(1,65): + left,top=getChessXY(CHECKERBOARD[i][0],CHECKERBOARD[i][1]) + chessRect=pygame.Rect(left,top,CHESSSIZE + 10,CHESSSIZE + 10) + if chessRect.collidepoint(x,y): + return(i) + return(None) + +def drawSide(x,y):#在点击的棋子上画一个正方形 + pygame.draw.rect(Screen,MAGENTA,(x-int(CHESSSIZE/2),y-int(CHESSSIZE/2),2*R+8,2*R+8),5) + +def report_judge(report_count,side): + if report_count >= 0: + if side == 1: + for i in range(1,11): + if CHECKERBOARD[i][2]<=10 and CHECKERBOARD[i][2] > 0: + return True + else: + for i in range(55,65): + if CHECKERBOARD[i][2]>10: + return True + return False + +#################end棋子绘制模块######################## + +#################start棋子移动模块###################### +def checkNeibor(chosenChess,coverdChess):#检查欲跳向的位置是否是相临格子 + x=abs(CHECKERBOARD[chosenChess][0]-CHECKERBOARD[coverdChess][0]) + y=abs(CHECKERBOARD[chosenChess][1]-CHECKERBOARD[coverdChess][1]) + if x==y and x==1: + return True#左右斜进退(上进下退) + elif CHECKERBOARD[chosenChess][0]==CHECKERBOARD[coverdChess][0] and y==2: + return True#进退 + return False + +def check_in_line(chosenChess,coverdChess):#检查欲跳向的位置是否是在同一直线上 + x=abs(CHECKERBOARD[chosenChess][0]-CHECKERBOARD[coverdChess][0]) + y=abs(CHECKERBOARD[chosenChess][1]-CHECKERBOARD[coverdChess][1]) + if x==y: + return True + elif CHECKERBOARD[chosenChess][0]==CHECKERBOARD[coverdChess][0]:#no movement + return True + return False + + + +def canGo(chosenChess,goalChess): + '''if checkNeibor(chosenChess,goalChess) and CHECKERBOARD[chosenChess][2]>0 and CHECKERBOARD[goalChess][2]==0:#非空白格子 + #flag1 = True + return True#移 + + x=abs(CHECKERBOARD[chosenChess][0]-CHECKERBOARD[goalChess][0])#横坐标之差abs + y=abs(CHECKERBOARD[chosenChess][1]-CHECKERBOARD[goalChess][1])#纵坐标之差abs + x1=CHECKERBOARD[chosenChess][0]+CHECKERBOARD[goalChess][0]#横坐标之和 + y1=CHECKERBOARD[chosenChess][1]+CHECKERBOARD[goalChess][1]#纵坐标之和 + + if (x==y and x==2) or (x==0 and y==4):# 左右斜进退的邻 or 进退的邻 + x2,y2=getChessXY(x1/2,y1/2)#获得中间格子的图形坐标 + z1=getChessIndex(x2,y2)#将中间格子转换为编号 + if CHECKERBOARD[z1][2]>0: + #flag2 = True#查看中间是否有棋子 + return True#邻 + + + m=CHECKERBOARD[chosenChess][0]-CHECKERBOARD[goalChess][0]#横坐标之差 + u=abs(m) + n=CHECKERBOARD[chosenChess][1]-CHECKERBOARD[goalChess][1]#纵坐标之差 + v=abs(n) + if u != 0:#不在同一个竖线 + p=m/u#1(左走) or -1(右走) + q=n/u#-1(下) or 1(上) + else:#在同一竖线 + p=0# m=0 + q=n/v*2#2(下) or -2(上) 上下差至少2 + if p > 0 and q > 0:#左上 + lastx = CHECKERBOARD[goalChess][0] + 1 + lasty = CHECKERBOARD[goalChess][1] + 1 + elif p < 0 and q > 0:#R上 + lastx = CHECKERBOARD[goalChess][0] - 1 + lasty = CHECKERBOARD[goalChess][1] + 1 + elif p < 0 and q < 0:#Rxia + lastx = CHECKERBOARD[goalChess][0] - 1 + lasty = CHECKERBOARD[goalChess][1] - 1 + elif p > 0 and q < 0:#L上 + lastx = CHECKERBOARD[goalChess][0] + 1 + lasty = CHECKERBOARD[goalChess][1] - 1 + elif p == 0 and q > 0: + lastx = CHECKERBOARD[goalChess][0] + lasty = CHECKERBOARD[goalChess][1] + 2 + elif p == 0 and q < 0: + lastx = CHECKERBOARD[goalChess][0] + lasty = CHECKERBOARD[goalChess][1] - 2 + #print('lastx',lastx) + #print('lasty',lasty) + for key in CHECKERBOARD.keys(): + if lastx == CHECKERBOARD[key][0] and lasty == CHECKERBOARD[key][1]: + lasti = CHECKERBOARD[key][2] + #print('lasti',lasti) + break + rg=abs(CHECKERBOARD[chosenChess][1]-CHECKERBOARD[goalChess][1])#纵坐标之差可表示连线之间棋子个数 v + xx0=CHECKERBOARD[goalChess][0]#goal x + yy0=CHECKERBOARD[goalChess][1]#goal y + xx1,yy1=getChessXY(xx0+p,yy0+q) + zz0=getChessIndex(xx1,yy1)#判断目标位置相邻是否有棋子 + #上述语句用于判断单跨目标棋子的上一个位置 + x=[]#准备一个空列表,用于添加当前和目标间棋子的权值 + if u==0: + rg=int(rg/2)#在竖直方向时个数为纵坐标之差的一半 可优化 + if check_in_line(chosenChess,goalChess) and lasti > 0:#当目标连线在同一直线时 + for i in range(1,rg): + x0=xx0+p + y0=yy0+q + xx0=x0 + yy0=y0 + xx,yy=getChessXY(x0,y0) + z=int(getChessIndex(xx,yy))#判断目标位置相邻是否有棋子 可优化 + if CHECKERBOARD[z][2] != 0: + if CHECKERBOARD[z][2]<11: + tmp=CHECKERBOARD[z][2]-1 + else: + tmp=CHECKERBOARD[z][2]-11 + x+=[tmp]#将希望跨过的棋子加到列表中 + root = Tk()#create a window + root.minsize(200, 200) + if CHECKERBOARD[chosenChess][2]<11: + tp=CHECKERBOARD[chosenChess][2]-1 + else: + tp=CHECKERBOARD[chosenChess][2]-11 + sss=str(x) + sss2=str(tp) + Label(root, text="请输入用只含下列数字且只用一次,结果为").pack() + Label(root, text=sss2).pack() + Label(root, text="的表达式(使用括号时请都用'()')").pack() + Label(root, text=sss).pack() + l1 = Label(root, text="请输入表达式:") + root.geometry("%dx%d+%d+%d" % (300, 100, 480, (ScreenHi) / 2)) + xls_text = StringVar() + l1.pack() # 这里的side可以赋值为LEFT RTGHT TOP BOTTOM + xls = Entry(root, textvariable=xls_text) + xls_text.set("") + xls.pack() + Button(root, text="确认", command=root.destroy).pack( expand=YES) + root.mainloop() + expression=xls_text.get() + #expression=xls_text.get() + print(xls_text) + + expre=re.findall(r"\d+",expression)#将输入的字符串运算式的数字转换到为列表 + expre=list(map(int,expre))#将列表元素转换为整型 + x.sort() + print('x',x) + expre.sort() + print('ex',expre) + if x == expre: + bingo1 = True + else: + bingo1 = False + + if tp==t_Result(expression):#表示输入的算式结果对应 + bingo2=True + else: + #print('B2')$ + bingo2=False + if bingo1 and bingo2 and CHECKERBOARD[zz0][2]>0: + #flag3 = True + return True#单跨 + if bingo2==False or bingo1==False: + root3=Tk() + Label(root3,text="表达式错误").pack() + root3.geometry("%dx%d+%d+%d" % (100, 100, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + root3.mainloop() + + return False''' + return True + + '''if flag1 or flag2: + return True + return False''' + +def jumpChess(chosenChess,currentChess):#跳子 + if canGo(chosenChess,currentChess): + CHECKERBOARD[currentChess][2] = CHECKERBOARD[chosenChess][2] + CHECKERBOARD[chosenChess][2]=0 + + pygame.mixer.music.set_volume(0.4) + s= pygame.mixer.Sound('移动音效.mp3') + s.play() + return True + + return False + +def jumpChess_back(chosenChess,currentChess):#跳子(悔棋) + CHECKERBOARD[currentChess][2] = CHECKERBOARD[chosenChess][2] + CHECKERBOARD[chosenChess][2]=0 + + pygame.mixer.music.set_volume(0.4) + s= pygame.mixer.Sound('移动音效.mp3') + s.play() + + + + + + ################################################### + ################################################### +class Calculator(object): + def __init__(self): + # 操作符集合 + self.operators = ['+', '-', '*', '/', '(', ')'] + # 操作符优先级 + self.priority = { + '+': 1, + '-': 1, + '*': 2, + '/': 2, + '(': 3, + ')': 3 + } + + def generate_postfix_expression(self, expression): + """ + 生成后缀表达式 + :param expression: + :return: + """ + # 去除表达式中所有空格 + expression = expression.replace(' ', '') + # 创建list作为栈,list的append和pop方法刚好和栈的入栈和出栈方法相同 + # 操作符栈 + operator_stack = list() + # 后缀表达式是从尾部插入数据,使用后缀表达式计算结果是从头开始遍历,可以理解为先进先出,可以使用队列实现, + # 这里为了简便用list代替,不做内存释放处理 + expression_stack = list() + for element in expression: + # 如果是数字则直接入表达式栈 + if element in self.operators: + # 如果栈为空,操作符直接入操作符栈,或者为左括号,也直接入操作符栈 + if not operator_stack: + operator_stack.append(element) + else: + # 如果目标元素是右括号,操作符栈顶出栈直接遇到左括号,且出栈的操作符除了括号入到表达式队列中 + if element == ')': + for top in operator_stack[::-1]: + fg = False + if top != '(': + expression_stack.append(top) + operator_stack.pop() + else: + print(top) + fg = True + operator_stack.pop() + break + if fg == False: + return -1 + else: + for top in operator_stack[::-1]: + # 如果目标元素大于栈顶元素,则直接入栈,否则栈顶元素出栈,入到表达式队列中 + # 左括号只有遇到右括号才出栈 + if self.priority[top] >= self.priority[element] and top != '(': + expression_stack.append(top) + operator_stack.pop() + else: + operator_stack.append(element) + break + # 可能操作符栈所有的元素优先级都大于等于目标操作符的优先级,这样的话操作符全部出栈了, + # 而目标操作符需要入栈操作 + if not operator_stack: + operator_stack.append(element) + else: + expression_stack.append(element) + # 中缀表达式遍历结束,操作符栈仍有操作符,将操作符栈中的操作符入到表达式栈中 + for i in range(len(operator_stack)): + expression_stack.append(operator_stack.pop()) + return expression_stack + + def calcaulate(self, expression): + # 生成后缀表达式 + expression_result = self.generate_postfix_expression(expression) + calcalate_stack = list()# 使用list作为栈来计算 + # 遍历后缀表达式 + for element in expression_result: + # 如果为数字直接入栈 + # 遇到操作符,将栈顶的两个元素出栈 + if element not in self.operators: + calcalate_stack.append(element) + else: + number1 = calcalate_stack.pop()# 操作数 + number2 = calcalate_stack.pop()# 被操作数 + result = self.operate(number1, number2, element) # 结果 = 被操作数 操作符 操作数 (例:2 - 1) + calcalate_stack.append(result) # 计算结果入栈 + return calcalate_stack[0] + + def operate(self, number1, number2, operator): + """ + 计算结果 + :param number1: 操作数 + :param number2: 被操作数 + :param operator: 操作符 + :return: + """ + number1 = int(number1) + number2 = int(number2) + if operator == '+': + return number2 + number1 + if operator == '-': + return number2 - number1 + if operator == '*': + #print('*') + return number2 * number1 + if operator == '/': + if number2 % number1 == 0: + return number2 / number1 + else: + return -1 + +def t_Result(expression): + c = Calculator() + try: + expression_result = c.calcaulate(expression) + except(BaseException): + return -1 + return (expression_result) + +##################end棋子移动模块########################## + +####################start计算分数模块####################### +def my_Scores(): + tmp=0 + ans=0 + i = 1 + while i < 11: + #for i in range(1,11): + if CHECKERBOARD[i][2]>10: + ans=(i-1)*(CHECKERBOARD[i][2]-11)+tmp + #print(i) + tmp=ans + i += 1 + return ans + +def his_Scores(): + tmp=0 + s=0 + i = 55 + while i < 65: + #for i in range(55,65): + if CHECKERBOARD[i][2]<11 and CHECKERBOARD[i][2]>0: + #print(i) + if i==64: + s+=0 + else: + s=(i-54)*(CHECKERBOARD[i][2]-1)+tmp + tmp=s + i+=1 + return s +def print_score(a,b): + my_font = pygame.font.SysFont("arial", 30) + Screen.blit(my_font.render(str(a), True, BLACK), (1060, 90)) + Screen.blit(my_font.render(str(b), True, BLACK), (120, 90)) +####################end计算分数模块############################ +def main():#主程序 + pygame.init() + + # 初始化 + pygame.mixer.init() + # 加载音乐文件 + pygame.mixer.music.load('Philter - Below The Surface.mp3') + pygame.mixer.music.set_volume(0.2) + # 开始播放音乐流 + pygame.mixer.music.play() + + #global Screen + Screen=pygame.display.set_mode((ScreenWidth,ScreenHi)) + pygame.display.set_caption("国际数棋") + #clock = pygame.time.Clock() + mousex=0 + mousey=0 + working=True#如果叫停,则停止操作,working变为false + #order=True#每一次跳跃后order改变,执子方转换0-1 + '''Screen.fill(WHITE) + font = pygame.font.Font('font.TTF',100) + txt = font.render('点击屏幕开始游戏',True,(0,0,255),(255,255,255)) + Screen.blit(txt,(230,300))''' + #eo(order) + #drawChess() + goback_s = [] + goback_e = [] + order_stack = [] + iswantgb = 0 # 标记是否需要悔棋 + iswantl = 0 # 判断是否认输0-无人认输,1-认输:认输后比较分数,可能存在平局 + cnt = 0 + #global pic + pic = pygame.image.load('R-C.jpg') + pic = pygame.transform.scale(pic,(1200,800)) + over = False + global side + global game_id + global counterpart_name + global request + request = '' + global report_count + report_count = 0 + while True: + #print('while0') + mouseClicked=False + for event in pygame.event.get(): + if event.type==QUIT or (event.type==KEYUP and event.key==K_ESCAPE): + pygame.quit() + sys.exit() + elif event.type==MOUSEBUTTONUP: + mousex,mousey=event.pos + mouseClicked=True + if cnt == 0: + Screen.fill(WHITE) + font = pygame.font.Font('STXINGKA.TTF',100) + txt1 = font.render('单机游戏',True,(0,0,255),(255,255,255)) + txt2 = font.render('联机对战',True,(0,0,255),(255,255,255)) + Screen.blit(txt1,(380,200)) + Screen.blit(txt2,(380,500)) + + + iswantgb =0 + + if mouseClicked==True: + #if cnt == 0: + index=getChessIndex(mousex,mousey) + global chosenChess + if((3800:#棋盘刷新/移动棋子 + #if goback_s!=[]: + #goback_s .pop() + chosenChess=index + x,y=getChessXY(CHECKERBOARD[index][0],CHECKERBOARD[index][1]) + Screen.fill(BGCOLOR) + Screen.blit(pic,(0,0)) + pygame.draw.polygon(Screen, (152,245,255), [(133,334),(339,235),(339,435)], 0) + pygame.draw.polygon(Screen, (249,204,226), [(1065,335),(866,235),(866,434)], 0) + pygame.draw.polygon(Screen, (0,255,0), [(340,235),(601,105),(867,235),(866,434),(601,566),(340,435)], 0) + draw_checkerboard(order) + drawChess() + drawSide(x,y) + + elif index!=None and CHECKERBOARD[index][2]==0 and chosenChess!=None: + currentChess=index + if order==False and CHECKERBOARD[chosenChess][2]<11 and working==True: + j=jumpChess(chosenChess,currentChess) + if j: + order=True + goback_s += [currentChess] + goback_e += [chosenChess] + order_stack +=[order] + elif order==True and CHECKERBOARD[chosenChess][2]>10 and working==True: + j=jumpChess(chosenChess,currentChess) + if j: + order=False + goback_s += [currentChess] + goback_e += [chosenChess] + order_stack +=[order] + chosenChess=None + Screen.fill(BGCOLOR) + Screen.blit(pic,(0,0)) + pygame.draw.polygon(Screen, (152,245,255), [(133,334),(339,235),(339,435)], 0) + pygame.draw.polygon(Screen, (249,204,226), [(1065,335),(866,235),(866,434)], 0) + pygame.draw.polygon(Screen, (0,255,0), [(340,235),(601,105),(867,235),(866,434),(601,566),(340,435)], 0) + draw_checkerboard(order) + drawChess() + + + if ((56010 or CHECKERBOARD[i][2]==0: + tanchuang=1 + if tanchuang==1: + root1 = Tk() + Label(root1, text="未达到胜利条件").pack() + #size = '%dx%d+%d+%d' % (100, 100, (ScreenWidth - 100) / 2, (ScreenHi - 100) / 2) + root1.geometry("%dx%d+%d+%d" %(100, 100, (ScreenWidth - 50) / 2, (ScreenHi - 50) / 2)) + root1.mainloop() + else: + working=False + + elif index!=None and CHECKERBOARD[index][2]==0 and chosenChess!=None: + currentChess=index + if order==False and CHECKERBOARD[chosenChess][2]<11 and working==True: + j=jumpChess(chosenChess,currentChess) + if j: + order=True + goback_s += [currentChess] + goback_e += [chosenChess] + order_stack +=[order] + elif order==True and CHECKERBOARD[chosenChess][2]>10 and working==True: + j=jumpChess(chosenChess,currentChess) + if j: + order=False + goback_s += [currentChess] + goback_e += [chosenChess] + + + + '''if working==False:#游戏结束''' + if 520his_Scores(): + Label(root2,text="红方胜利").pack() + elif my_Scores()==his_Scores(): + Label(root2,text="旗鼓相当-平局").pack() + else: + Label(root2,text="蓝方胜利").pack() + root2.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root2, text="再来一把", command=root2.destroy).pack(side=LEFT, expand=YES) + Button(root2, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root2.mainloop() + reProgram() + if working==False:#游戏结束 + root2=Tk() + root2.minsize(300,200) + Label(root2,text="游戏结束").pack() + if my_Scores()>his_Scores(): + Label(root2,text="红方胜利").pack() + elif my_Scores()==his_Scores(): + Label(root2,text="旗鼓相当-平局").pack() + else: + Label(root2,text="蓝方胜利").pack() + root2.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root2, text="再来一把", command=root2.destroy).pack(side=LEFT, expand=YES) + Button(root2, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root2.mainloop() + reProgram() + elif cnt == 2: + client = Client_thread() + client.start() + #client.run() + while client.status != 1: + #print(client.status) + time.sleep(0.5) + cnt = 3 + + elif cnt== 3: + #print(cnt) + order = True + if client.side == 1: + #order = False + Screen.fill(BGCOLOR) + Screen.blit(pic,(0,0)) + font_peidui = pygame.font.Font('STXINGKA.TTF',100) + text_peidui = font_peidui.render("匹配到对手,您是蓝棋", True, BLUE) + Screen.blit(text_peidui, (140, 330)) + else: + #order = True + Screen.fill(BGCOLOR) + Screen.blit(pic,(0,0)) + font_peidui = pygame.font.Font('STXINGKA.TTF',100) + text_peidui = font_peidui.render("匹配到对手,您是红棋", True, BLUE) + Screen.blit(text_peidui, (140, 330)) + pygame.display.update() + time.sleep(1) + #print(order) + side = client.side + #print('ms',side) + #print(side) + game_id = client.game_id + #print(game_id) + counterpart_name = client.counterpart_name + #print(counterpart_name) + #global Screen + #global pic + Screen.fill(WHITE) + Screen.blit(pic,(0,0)) + pygame.draw.polygon(Screen, (152,245,255), [(133,334),(339,235),(339,435)], 0) + pygame.draw.polygon(Screen, (249,204,226), [(1065,335),(866,235),(866,434)], 0) + pygame.draw.polygon(Screen, (0,255,0), [(340,235),(601,105),(867,235),(866,434),(601,566),(340,435)], 0) + pygame.display.update() + #print('update') + #print('DQ') + draw_checkerboard_net(order) + drawChess() + pygame.display.update() + cnt = 4 + #print('cnt = 4') + #while True: + #mouseClick = False + elif cnt == 4: + #print('while') + for event in pygame.event.get(): + if event.type==QUIT or (event.type==KEYUP and event.key==K_ESCAPE): + pygame.quit() + client.quit() + sys.exit() + elif event.type==MOUSEBUTTONUP: + mousex,mousey=event.pos + mouseClicked=True + #print('MC') + if client.over == True: + over = True + global red_moved + + #global blue_is_moving#$$$$$$ + for event in pygame.event.get(): + if event.type == pygame.QUIT: + client.quit() + exit() + pygame.quit() + sys.exit() + if client.request == 'stop': + root1=Tk() + root1.minsize(300,200) + Label(root1,text="对方叫停").pack() + if my_Scores()>his_Scores(): + Label(root1,text="红方胜利").pack() + elif my_Scores()==his_Scores(): + Label(root1,text="平局").pack() + else: + Label(root1,text="蓝方胜利").pack() + #client.quit() + root1.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root1, text="再来一把", command=root1.destroy).pack(side=LEFT, expand=YES) + Button(root1, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root1.mainloop() + reProgram() + if client.request == 'report':#对方report + root2 = Tk() + root2.minsize(300,200) + Label(root2,text="因违规操作被对方举报").pack() + Label(root2,text = "我方自动判输").pack() + client.b_report() + root2.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root2, text="再来一把", command=root2.destroy).pack(side=LEFT, expand=YES) + Button(root2, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root2.mainloop() + reProgram() + if over == False: + + if client.inQueue.empty() == False: + rival_move = client.inQueue.get() + if "src" in rival_move: + srcx = rival_move["src"]["x"] + srcy = rival_move["src"]["y"] + dstx = rival_move["dst"]["x"] + dsty = rival_move["dst"]["y"] + num = rival_move["num"] + + #print('mm') + for key in CHECKERBOARD.keys(): + if srcx == CHECKERBOARD[key][0] and srcy == CHECKERBOARD[key][1]: + tmp = CHECKERBOARD[key][2] + #print('tmp = ',tmp) + CHECKERBOARD[key][2] = 0 + #print('CHECKERBOARD[key][2] = ',CHECKERBOARD[key][2]) + break + for i in CHECKERBOARD.keys(): + #print('FOR') + if dstx == CHECKERBOARD[i][0] and dsty == CHECKERBOARD[i][1]: + CHECKERBOARD[i][2] = tmp + #print('CHECKERBOARD[i][2]new = ',CHECKERBOARD[i][2]) + #order = True + order = False + Screen.fill(BGCOLOR) + Screen.blit(pic,(0,0)) + pygame.draw.polygon(Screen, (152,245,255), [(133,334),(339,235),(339,435)], 0) + pygame.draw.polygon(Screen, (249,204,226), [(1065,335),(866,235),(866,434)], 0) + pygame.draw.polygon(Screen, (0,255,0), [(340,235),(601,105),(867,235),(866,434),(601,566),(340,435)], 0) + pygame.display.update() + draw_checkerboard_net(order) + drawChess() + pygame.display.update() + #red_is_move = True + #修改对面的棋盘(移动对面棋子) + if side != 1: + order = bool(1-order) + #global report_count + report_count+=1 + break + '''if rival_move['request'] == 'report': + root2=Tk() + root2.minsize(300,200) + Label(root2,text="我方超时,对方胜利").pack() + client.quit() + root2.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root2, text="再来一把", command=root2.destroy).pack(side=LEFT, expand=YES) + Button(root2, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root2.mainloop()''' + if mouseClicked==True: + #print('MT') + ###############################3 + index=getChessIndex(mousex,mousey) + + if 1010his_Scores(): + Label(root1,text="红方胜利").pack() + elif my_Scores()==his_Scores(): + Label(root1,text="平局").pack() + else: + Label(root1,text="蓝方胜利").pack() + #client.quit() + root1.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root1, text="再来一把", command=root1.destroy).pack(side=LEFT, expand=YES) + Button(root1, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root1.mainloop() + reProgram() + + elif 1010his_Scores(): + Label(root1,text="红方胜利").pack() + elif my_Scores()==his_Scores(): + Label(root1,text="平局").pack() + else: + Label(root1,text="蓝方胜利").pack() + #client.quit() + root1.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root1, text="再来一把", command=root1.destroy).pack(side=LEFT, expand=YES) + Button(root1, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root1.mainloop() + reProgram() + + + + if index!=None and CHECKERBOARD[index][2]==0 and chosenChess!=None: + currentChess=index + if order==False and CHECKERBOARD[chosenChess][2]<11 and working==True and side == 1: + ssrcx = CHECKERBOARD[chosenChess][0] + ssrcy = CHECKERBOARD[chosenChess][1] + #print('ssrcy = ',ssrcy) + num = CHECKERBOARD[chosenChess][2] -1 + sdstx = CHECKERBOARD[currentChess][0] + sdsty = CHECKERBOARD[currentChess][1] + #print('sdsty = ',sdsty) + j=jumpChess(chosenChess,currentChess) + if j: + order=True + #print('order = ',order) + goback_s += [currentChess] + goback_e += [chosenChess] + order_stack +=[order]#send msg + client.outQueue.put({ + "type": 1, + "msg": { + "game_id": client.game_id, + "side": client.side, + "num": num, + "exp": iswantgb, + "src": { + "x": int(ssrcx), + "y": int(ssrcy) + }, + "dst": { + "x": int(sdstx), + "y": int(sdsty) + } + } + }) + elif order==True and CHECKERBOARD[chosenChess][2]>=11 and working==True and side != 1: + ssrcx = CHECKERBOARD[chosenChess][0] + ssrcy = CHECKERBOARD[chosenChess][1] + #print('ssrcy = ',ssrcy) + num = CHECKERBOARD[chosenChess][2] - 11 + sdstx = CHECKERBOARD[currentChess][0] + sdsty = CHECKERBOARD[currentChess][1] + #print('sdsty = ',sdsty) + j=jumpChess(chosenChess,currentChess) + if j: + order=False + #print('order = ',order) + goback_s += [currentChess] + goback_e += [chosenChess] + order_stack +=[order]#send msg + client.outQueue.put({ + "type": 1, + "msg": { + "game_id": client.game_id, + "side": client.side, + "num": num, + "exp": iswantgb, + "src": { + "x": int(ssrcx), + "y": int(ssrcy) + }, + "dst": { + "x": int(sdstx), + "y": int(sdsty) + } + } + }) + #print('msg sent') + if index!=None and CHECKERBOARD[index][2]>0:#棋盘刷新/移动棋子 + #if goback_s!=[]: + #goback_s .pop() + chosenChess=index + x,y=getChessXY(CHECKERBOARD[index][0],CHECKERBOARD[index][1]) + Screen.fill(BGCOLOR) + Screen.blit(pic,(0,0)) + pygame.draw.polygon(Screen, (152,245,255), [(133,334),(339,235),(339,435)], 0) + pygame.draw.polygon(Screen, (249,204,226), [(1065,335),(866,235),(866,434)], 0) + pygame.draw.polygon(Screen, (0,255,0), [(340,235),(601,105),(867,235),(866,434),(601,566),(340,435)], 0) + draw_checkerboard_net(order) + drawChess() + drawSide(x,y) + pygame.display.update() + #global red_moved + red_moved = 1 + #游戏结束 + if 70his_Scores(): + Label(root2,text="红方胜利").pack() + elif my_Scores()==his_Scores(): + Label(root2,text="旗鼓相当-平局").pack() + else: + Label(root2,text="蓝方胜利").pack() + client.quit() + root2.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root2, text="再来一把", command=root2.destroy).pack(side=LEFT, expand=YES) + Button(root2, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root2.mainloop() + + reProgram() + + #reProgram() + if client.winner == side and client.request == '': + print('wnn',client.winner) + print('ms',side) + #print("red.low") + root2=Tk() + root2.minsize(300,200) + Label(root2,text="我方超时,对方胜利").pack() + client.quit() + root2.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root2, text="再来一把", command=root2.destroy).pack(side=LEFT, expand=YES) + Button(root2, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root2.mainloop() + + reProgram() + elif (client.winner != side and client.request == '') and client.winner != -1: + root2=Tk() + root2.minsize(300,200) + Label(root2,text="对方超时,我方胜利").pack() + client.quit() + root2.geometry("%dx%d+%d+%d" % (200, 200, (ScreenWidth +50) / 2, (ScreenHi +50) / 2)) + Button(root2, text="再来一把", command=root2.destroy).pack(side=LEFT, expand=YES) + Button(root2, text="退出游戏", command=quit).pack(side=LEFT, expand=YES) + root2.mainloop() + + reProgram() + if((1050 p: + p = all_core[i][1] + x = i + if x != -1: + print(333) + return all_core[x] + else: + print(444) + return False + + +def only_one_chess(s): + a = 0 + num = 0 + for key in s.lanqi.keys(): + # print(s.lanqi[key]["now_weizhi"]) + if tuple(s.lanqi[key]["now_weizhi"]) not in HongPoint: + num = num + 1 + a = key + if num == 1: + print(a) + return a + else: + return False + + +def check_events(s, screen, client): + global red_is_move + for event in pygame.event.get(): + if event.type == pygame.QUIT: + client.quit() + exit() + pygame.quit() + sys.exit() + # elif event.type == MOUSEBUTTONDOWN: + # x = event.pos[0] + # y = event.pos[1] + if s.over == False: + rival_chessman_move(client, s) + if red_is_move == True: + print("ffffffffffffff") + if whowin(s, "black"): + if fenshu(s)[0] > fenshu(s)[1]: + tishitext(s, screen) + else: + tishitext(s, screen) + ai = Max_solve(s) + if ai != False: + print("FFFFFFFFFFFFFF") + s.num = ai[0] + s.sdstx = ai[2][1] + s.sdsty = ai[2][2] + # print(s.sdstx,s.sdsty) + s.ssrcx = s.lanqi[s.num]["now_weizhi"][0] + s.ssrcy = s.lanqi[s.num]["now_weizhi"][1] + sendxy(client, s) + s.order = True + s.lanqi[s.num]["now_weizhi"] = [ai[2][1], ai[2][2]] + red_is_move = False + + +def victory(s, screen, text): + """ + 红方胜利 + """ + s.over = True + victory = pygame.image.load('images/result.png') + pic_rect = victory.get_rect() + pic_rect.centerx = 730 + pic_rect.centery = 350 + screen.blit(victory, pic_rect) + font = pygame.font.Font('font.ttf', 50) + rv = font.render(text, True, [255, 0, 0]) + screen.blit(rv, (550, 310)) + + +HongPoint = [(1125, 225), (1125, 325), (1125, 425), (1125, 525), (1225, 275), (1225, 375), + (1225, 475), (1325, 325), (1325, 425), (1425, 375)] +HeiPoint = [(25, 375), (125, 325), (125, 425), (225, 275), (225, 375), (225, 475), + (325, 225), (325, 325), (325, 425), (325, 525)] + + +# 判断是否可以结束 +def whowin(s, color): + if color == "black": + for key in s.lanqi.keys(): + if tuple(s.lanqi[key]['now_weizhi']) not in HongPoint: + return False + else: + for key in s.hongqi.keys(): + if tuple(s.hongqi[key]['now_weizhi']) not in HeiPoint: + return False + return True + + +# 判断是否可以结 +def find_fenshu(x, y): + if (x == 325 and y == 525) or (x == 1125 and y == 225): + return 1 + elif (x == 325 and y == 325) or (x == 1125 and y == 425): + return 2 + elif (x == 325 and y == 425) or (x == 1125 and y == 325): + return 3 + elif (x == 325 and y == 225) or (x == 1125 and y == 525): + return 4 + elif (x == 225 and y == 275) or (x == 1225 and y == 475): + return 5 + elif (x == 225 and y == 375) or (x == 1225 and y == 375): + return 6 + elif (x == 225 and y == 475) or (x == 1225 and y == 275): + return 7 + elif (x == 125 and y == 425) or (x == 1325 and y == 325): + return 8 + elif (x == 125 and y == 325) or (x == 1325 and y == 425): + return 9 + else: + return 0 + + +# 计分板 +def fenshu(s): + red_fen = 0 + hei_fen = 0 + for key in s.hongqi.keys(): + if tuple(s.hongqi[key]['now_weizhi']) in s.HeiPoint: + red_fen += (key) * find_fenshu(s.hongqi[key]['now_weizhi'][0], s.hongqi[key]['now_weizhi'][1]) + for key in s.lanqi.keys(): + if tuple(s.lanqi[key]['now_weizhi']) in s.HongPoint: + hei_fen += (key) * find_fenshu(s.lanqi[key]['now_weizhi'][0], s.lanqi[key]['now_weizhi'][1]) + return red_fen, hei_fen + + +# 绘制红蓝提示器 +def tishitext(s, screen): + s_font = pygame.font.Font('pz.ttf', 50) + s_text = s_font.render('红方得分: %s' % str(fenshu(s)[0]), True, (255, 0, 0)) + screen.blit(s_text, (1100, 650)) + s_text = s_font.render('蓝方得分: %s' % str(fenshu(s)[1]), True, (0, 0, 0)) + screen.blit(s_text, (1100, 700)) + + +def red_order(s, screen): + """ + 红棋落子 + """ + font = pygame.font.Font('pz.ttf', 60) + ro = font.render("红棋落子", True, [255, 0, 0]) + screen.blit(ro, (1100, 580)) + + +def black_order(s, screen): + """ + 黑棋落子 + """ + font = pygame.font.Font('pz.ttf', 60) + bo = font.render("黑棋落子", True, [0, 0, 0]) + screen.blit(bo, (1100, 580)) + + +# 判断该位置有无棋子 +def weizhi_panduan(s, x, y): + for key in s.hongqi.keys(): + if [x, y] == s.hongqi[key]['now_weizhi']: + return True + for key in s.lanqi.keys(): + if [x, y] == s.lanqi[key]['now_weizhi']: + return True + return False + + +# 移 规则 +def yi_rule(chess_x, chess_y, x, y): + if abs(chess_x - x) <= 100 and abs(chess_y - y) <= 100: + if not (chess_x == x and chess_y == y): + return True + return False + + +# 邻 规则 +def lin_rule(s, chess_x, chess_y, x, y): + if weizhi_panduan(s, (chess_x + x) / 2, (chess_y + y) / 2) and abs(x - chess_x) <= 200: + if not (chess_x == x and chess_y == y): + return True + return False + + +# 弹框规则 + +def tankuang(content, wx_type): + app = wx.App() + dlg = wx.MessageDialog(None, content, "提示", wx_type) + if dlg.ShowModal() == wx.ID_NO: + return False + else: + return True + + +def find(s, x, y): + for key in s.hongqi.keys(): + if s.hongqi[key]['now_weizhi'][0] - x == 0 and s.hongqi[key]['now_weizhi'][1] - y == 0: + return (key) + for key in s.lanqi.keys(): + if s.lanqi[key]['now_weizhi'][0] - x == 0 and s.lanqi[key]['now_weizhi'][1] - y == 0: + return (key) + return -1 + + +POINTS = [(25, 375), (125, 325), (125, 425), (225, 275), (225, 375), (225, 475), + (325, 225), (325, 325), (325, 425), (325, 525), (425, 175), (425, 275), + (425, 375), (425, 475), (425, 575), (525, 125), (525, 225), (525, 325), + (525, 425), (525, 525), (525, 625), (625, 75), (625, 175), (625, 275), + (625, 375), (625, 475), (625, 575), (625, 675), (725, 25), (725, 125), + (725, 225), (725, 325), (725, 425), (725, 525), (725, 625), (725, 725), + (825, 75), (825, 175), (825, 275), (825, 375), (825, 475), (825, 575), + (825, 675), (925, 125), (925, 225), (925, 325), (925, 425), (925, 525), + (925, 625), (1025, 175), (1025, 275), (1025, 375), (1025, 475), (1025, 575), + (1125, 225), (1125, 325), (1125, 425), (1125, 525), (1225, 275), (1225, 375), + (1225, 475), (1325, 325), (1325, 425), (1425, 375)] + + +def rival_chessman_move(client, s): + """ + 对方棋子移动 + """ + global red_is_move + getxy(client, s) + # 暂存对方选中的红棋 + # print(s.order) + if s.order == True and s.get == 1: + # for key in s.hongqi.keys(): + if s.srcx == s.hongqi[s.num]['now_weizhi'][0] and s.srcy == s.hongqi[s.num]['now_weizhi'][1]: + s.hongqi[s.num]['now_weizhi'] = [s.dstx, s.dsty] + # print("gggggggggggg") + # 改为蓝棋落子 + s.order = False + # s.red_backups = [] + # 改为选子状态 + red_is_move = True + s.running = True + + +# AI计算----------------------------------------------------------------------------------------------------------- +AI_v = () # 用于存储AI单跨计算后的值 + +# 运算单跨表达式 +def find_a(a, b, l): + if l == "+": + return a + b + elif l == '-': + return a - b + elif l == '*': + return a * b + elif l == "/": + if b != 0: + return a / b + else: + return 10000000 # 当被除数为0,运算错误 + +list2 = ['+', '-', '*', '/'] + +def js(l, n): + global AI_v + if n == 1: + AI_v = AI_v + (l[0],) + else: + for i in range(4): + b = copy.deepcopy(l) + x = find_a(b[0], b[1], list2[i]) + b.remove(b[0]) + b.remove(b[0]) + b.insert(0, x) + js(b, n - 1) + + +# 搜索单个棋子左上左下的两条移动直线上的所有情况 +def line_search(s, key): + # print(1) + ''''' + if color == "red": + mm = -1 + cur_x = s.hongqi[key]["now_weizhi"][0] + cur_y = s.hongqi[key]["now_weizhi"][1] + else: + ''''' + cur_x = s.lanqi[key]["now_weizhi"][0] + cur_y = s.lanqi[key]["now_weizhi"][1] + mm = 1 + # print(cur_x, cur_y) + nochess_addup_record = [] # 往shang运动 + nochess_adddown_record = [] # 往xia运动 + v1 = v2 = False + for i in range(1, 8): + if (cur_x + mm * 100 * i, cur_y + 50 * i) in POINTS and not weizhi_panduan(s, cur_x + mm * 100 * i, + cur_y + 50 * i): + nochess_addup_record.insert(0, [cur_x + mm * 100 * i, cur_y + 50 * i]) + if (cur_x + mm * 100 * i, cur_y - 50 * i) in POINTS and not weizhi_panduan(s, cur_x + mm * 100 * i, + cur_y - 50 * i): + nochess_adddown_record.insert(0, [cur_x + mm * 100 * i, cur_y - 50 * i]) + # print(2) + if (cur_x, cur_y + 100) in POINTS and not weizhi_panduan(s, cur_x, cur_y + 100): + nochess_addup_record.append([cur_x, cur_y + 100]) + if (cur_x, cur_y - 100) in POINTS and not weizhi_panduan(s, cur_x, cur_y - 100): + nochess_adddown_record.append([cur_x, cur_y - 100]) + if (cur_x, cur_y + 50) in POINTS and not weizhi_panduan(s, cur_x, cur_y + 50): + nochess_addup_record.append([cur_x, cur_y + 50]) + if (cur_x, cur_y - 50) in POINTS and not weizhi_panduan(s, cur_x, cur_y - 50): + nochess_adddown_record.append([cur_x, cur_y - 50]) + # print(nochess_addup_record) + # print(nochess_adddown_record) + if len(nochess_addup_record): + v1 = AI_move_rule(s, key, cur_x, cur_y, nochess_addup_record) + if len(nochess_adddown_record): + v2 = AI_move_rule(s, key, cur_x, cur_y, nochess_adddown_record) + # print(7) + print(key, cur_x, cur_y, v1) + print(key, cur_x, cur_y, v2) + # ??????????????棋子为8的时候,v1=[],v2!=[],那么如果当v1=v2=[],情况未讨论!!!!!!!!! + if v1 == False and v2 != False: + bb = ((v2[1] - cur_x) ** 2 + (v2[2] - cur_y) ** 2) ** 0.5 + (1125 - cur_x) * 0.01 + return bb, v2 + elif v1 != False and v2 == False: + aa = ((v1[1] - cur_x) ** 2 + (v1[2] - cur_y) ** 2) ** 0.5 + (1125 - cur_x) * 0.01 + return aa, v1 + elif v1 == False and v2 == False: + return False, False + else: + aa = ((v1[1] - cur_x) ** 2 + (v1[2] - cur_y) ** 2) ** 0.5 + (1125 - cur_x) * 0.01 + bb = ((v2[1] - cur_x) ** 2 + (v2[2] - cur_y) ** 2) ** 0.5 + (1125 - cur_x) * 0.01 + if v1[0] >= v2[0]: + return aa, v1 + else: + return bb, v2 + + +# AI基本移动规则 +def AI_move_rule(s, key, cur_x, cur_y, nochess_add_record): + # print(3) + for i in range(len(nochess_add_record)): + if dankua_rule_AI(s, key, cur_x, cur_y, nochess_add_record[i][0], nochess_add_record[i][1]): + cores = 300 + # print(4) + return [cores, nochess_add_record[i][0], nochess_add_record[i][1]] + elif lin_rule(s, cur_x, cur_y, nochess_add_record[i][0], nochess_add_record[i][1]): + cores = 200 + # print(5) + return [cores, nochess_add_record[i][0], nochess_add_record[i][1]] + elif yi_rule(cur_x, cur_y, nochess_add_record[i][0], nochess_add_record[i][1]): + cores = 100 + # print(6) + return [cores, nochess_add_record[i][0], nochess_add_record[i][1]] + return False + + + +# AI版AI单跨规则 +def dankua_rule_AI(s, value, chess_x, chess_y, x, y): + # print(8) + global AI_v + if chess_x != x: + y1 = (y - chess_y) / abs(y - chess_y) + x1 = (x - chess_x) / abs(x - chess_x) + if weizhi_panduan(s, x - x1 * 100, y - y1 * 50) and abs(y - chess_y) / 50 == abs(x - chess_x) / 100: + # print(9) + v = [] + for i in range(abs(int((x - chess_x) / 100)) - 1): + v.append(find(s, chess_x + (i + 1) * 100 * x1, chess_y + (i + 1) * 50 * y1)) + while -1 in v: + v.remove(-1) + + # print("++++++++++++++++", v, "++++++++++++") + lis0 = list(itertools.permutations(v, len(v))) # 全排列 + if v: + for j in range(len(lis0)): + js(list(lis0[j]), len(lis0[j])) + if value in AI_v: + # print(AI_v) + AI_v = () + return True + else: + AI_v = () + return False +# -------------------------------------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/国际数棋-双界面/国际数棋-双界面/game-function/lan_settings.py b/国际数棋-双界面/国际数棋-双界面/game-function/lan_settings.py new file mode 100644 index 0000000..4661ef6 --- /dev/null +++ b/国际数棋-双界面/国际数棋-双界面/game-function/lan_settings.py @@ -0,0 +1,66 @@ +import copy + + +class BlackSettings(): + def __init__(self): + """ + 初始化游戏的设置 + """ + + # 设置屏幕参数 + self.screen_width = 1450 + self.screen_height = 800 + self.back_color = (152, 245, 245) # 背景颜色 + + # 设置棋子参数 + self.r = 25 # 棋子半径 + + # 用于记录棋子当前状态 + self.lanqi = {0: {'color': 'blue', 'now_weizhi': [25, 375]}, 1: {'color': 'blue', 'now_weizhi': [325, 525]}, + 2: {'color': 'blue', 'now_weizhi': [325, 325]}, 3: {'color': 'blue', 'now_weizhi': [325, 425]}, + 4: {'color': 'blue', 'now_weizhi': [325, 225]}, 5: {'color': 'blue', 'now_weizhi': [225, 275]}, + 6: {'color': 'blue', 'now_weizhi': [225, 375]}, 7: {'color': 'blue', 'now_weizhi': [225, 475]}, + 8: {'color': 'blue', 'now_weizhi': [125, 425]}, 9: {'color': 'blue', 'now_weizhi': [125, 325]}} + + self.hongqi = {0: {'color': 'red', 'now_weizhi': [1425, 375]}, 1: {'color': 'red', 'now_weizhi': [1125, 225]}, + 2: {'color': 'red', 'now_weizhi': [1125, 425]}, 3: {'color': 'red', 'now_weizhi': [1125, 325]}, + 4: {'color': 'red', 'now_weizhi': [1125, 525]}, 5: {'color': 'red', 'now_weizhi': [1225, 475]}, + 6: {'color': 'red', 'now_weizhi': [1225, 375]}, 7: {'color': 'red', 'now_weizhi': [1225, 275]}, + 8: {'color': 'red', 'now_weizhi': [1325, 325]}, 9: {'color': 'red', 'now_weizhi': [1325, 425]}} + + self.HongPoint = [(1125, 225), (1125, 325), (1125, 425), (1125, 525), (1225, 275), (1225, 375), + (1225, 475), (1325, 325), (1325, 425), (1425, 375)] + self.HeiPoint = [(25, 375), (125, 325), (125, 425), (225, 275), (225, 375), (225, 475), + (325, 225), (325, 325), (325, 425), (325, 525)] + # 用于暂时存储上一步棋的位置***** + self.red_backups = [] # red + self.black_backups = [] # blue + self.exp = '' + + # 记录执子落子状态********* + self.running = True + # 记录传送的棋号 + self.num = -1 + # 记录游戏结束与否****** + self.over = False + + # 记录当前落子方,红棋先手 *** + self.order = 1 + # 记录是stop还是quit*** + self.request = '' + + # 记录游戏昵称 + self.name = 'jzl' + # 记录分配方,0为红方 + self.side = 0 + self.game_id = "" + self.get = 0 + self.srcx = 0 + self.srcy = 0 + self.dstx = 0 + self.dsty = 0 + self.ssrcx = 0 + self.ssrcy = 0 + self.sdstx = 0 + self.sdsty = 0 + diff --git a/国际数棋-双界面/国际数棋-双界面/game-function/red_game_function.py b/国际数棋-双界面/国际数棋-双界面/game-function/red_game_function.py new file mode 100644 index 0000000..49ad496 --- /dev/null +++ b/国际数棋-双界面/国际数棋-双界面/game-function/red_game_function.py @@ -0,0 +1,440 @@ +import collections +import pygame +import pygame.font +import sys +import copy +from math import sqrt +from pygame.locals import * +import main +from client import * +import traceback +import json +import wx +import re + + +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +GREEN = (0, 255, 0) +RED = (255, 0, 0) +BLUE = (0, 0, 255) +def transfer(x, y): + + """ + 将像素坐标转换为棋盘坐标 + """ + + chess_x = (x-25) / 100 + chess_y = (y-25) / 50 + + return [chess_x, chess_y] + +def retransfer(chess_x, chess_y): + + """ + 将棋盘坐标转换为像素坐标 + """ + + x = (int)(100*(chess_x )+25) + y = (int)(50*(chess_y )+25) + + return [x, y] + + +def getxy(client, s): + print("lan_rec") + """ + 接收对方消息 + """ + print("--------------") + if client.inQueue.empty() == False: # to process the rival's message + rival_move = client.inQueue.get() + if "src" in rival_move: + s.srcx = rival_move["src"]["x"] + s.srcy = rival_move["src"]["y"] + s.dstx = rival_move["dst"]["x"] + s.dsty = rival_move["dst"]["y"] + s.sum = rival_move["num"] + [s.srcx, s.srcy] = retransfer(s.srcx, s.srcy) + [s.dstx, s.dsty] = retransfer(s.dstx, s.dsty) + #置收到消息信号为1 + s.get = 1 + + print("收集到的坐标是", s.srcx, s.srcy, s.dstx, s.dsty) + +def sendxy(client, s): + """ + 发送己方消息 + """ + print("lan_rec") + [s.ssrcx, s.ssrcy] = transfer(s.ssrcx, s.ssrcy) + [s.sdstx, s.sdsty] = transfer(s.sdstx, s.sdsty) + if (s.side == 0 and s.order == True ): + client.outQueue.put({ + "type": 1, + "msg": { + "game_id": client.game_id, + "side": client.side, + "num":s.num, + "src": { + "x": s.ssrcx, + "y": s.ssrcy + }, + "dst": { + "x": s.sdstx, + "y": s.sdsty + } + } + }) + if (s.side == 1 and s.order == False): + print("发送到的坐标是", s.ssrcx, s.ssrcy, s.sdstx, s.sdsty) + client.outQueue.put({ + "type": 1, + "msg": { + "game_id": client.game_id, + "side": client.side, + "num": s.num, + "src": { + "x": s.ssrcx, + "y": s.ssrcy + }, + "dst": { + "x": s.sdstx, + "y": s.sdsty + } + } + }) + +def check_events(s, screen, client): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + client.quit() + exit() + pygame.quit() + sys.exit() + elif event.type == MOUSEBUTTONDOWN: + x=event.pos[0] + y=event.pos[1] + #getstop(client,s) + if s.over == False: + #print(s.order) + rival_chessman_move(client, s) + chessman_move(event, s, screen, client) + #认输游戏 + if 25< x <225 and 600