Pygame 做的中国象棋,一直以来喜欢下象棋,写了 python 就拿来做一个试试,水平有限,电脑走法水平低,需要在下次版本中更新电脑走法,希望源码能帮助大家更好的学习 python。总共分为四个文件,chinachess.py 为主文件,constants.py 数据常量,pieces.py 棋子类,走法,computer.py 电脑走法计算。 链接:pan.baidu.com/s/1e1lzDiLT… 提取码:cskj

chinachess.py 为主文件

  1. import pygame
  2. import time
  3. import constants
  4. import pieces
  5. import computer
  6.  
  7. class MainGame():
  8. window = None
  9. Start_X = constants.Start_X
  10. Start_Y = constants.Start_Y
  11. Line_Span = constants.Line_Span
  12. Max_X = Start_X + * Line_Span
  13. Max_Y = Start_Y + * Line_Span
  14.  
  15. player1Color = constants.player1Color
  16. player2Color = constants.player2Color
  17. Putdownflag = player1Color
  18. piecesSelected = None
  19.  
  20. button_go = None
  21. piecesList = []
  22.  
  23. def start_game(self):
  24. MainGame.window = pygame.display.set_mode([constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT])
  25. pygame.display.set_caption("天青-中国象棋")
  26. MainGame.button_go = Button(MainGame.window, "重新开始", constants.SCREEN_WIDTH - , ) # 创建开始按钮
  27. self.piecesInit()
  28.  
  29. while True:
  30. time.sleep(0.1)
  31. # 获取事件
  32. MainGame.window.fill(constants.BG_COLOR)
  33. self.drawChessboard()
  34. #MainGame.button_go.draw_button()
  35. self.piecesDisplay()
  36. self.VictoryOrDefeat()
  37. self.Computerplay()
  38. self.getEvent()
  39. pygame.display.update()
  40. pygame.display.flip()
  41.  
  42. def drawChessboard(self):
  43. mid_end_y = MainGame.Start_Y + * MainGame.Line_Span
  44. min_start_y = MainGame.Start_Y + * MainGame.Line_Span
  45. for i in range(, ):
  46. x = MainGame.Start_X + i * MainGame.Line_Span
  47. if i== or i ==:
  48. y = MainGame.Start_Y + i * MainGame.Line_Span
  49. pygame.draw.line(MainGame.window, constants.BLACK, [x, MainGame.Start_Y], [x, MainGame.Max_Y], )
  50. else:
  51. pygame.draw.line(MainGame.window, constants.BLACK, [x, MainGame.Start_Y], [x, mid_end_y], )
  52. pygame.draw.line(MainGame.window, constants.BLACK, [x, min_start_y], [x, MainGame.Max_Y], )
  53.  
  54. for i in range(, ):
  55. x = MainGame.Start_X + i * MainGame.Line_Span
  56. y = MainGame.Start_Y + i * MainGame.Line_Span
  57. pygame.draw.line(MainGame.window, constants.BLACK, [MainGame.Start_X, y], [MainGame.Max_X, y], )
  58.  
  59. speed_dial_start_x = MainGame.Start_X + * MainGame.Line_Span
  60. speed_dial_end_x = MainGame.Start_X + * MainGame.Line_Span
  61. speed_dial_y1 = MainGame.Start_Y + * MainGame.Line_Span
  62. speed_dial_y2 = MainGame.Start_Y + * MainGame.Line_Span
  63. speed_dial_y3 = MainGame.Start_Y + * MainGame.Line_Span
  64. speed_dial_y4 = MainGame.Start_Y + * MainGame.Line_Span
  65.  
  66. pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y1], [speed_dial_end_x, speed_dial_y2], )
  67. pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y2],
  68. [speed_dial_end_x, speed_dial_y1], )
  69. pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y3],
  70. [speed_dial_end_x, speed_dial_y4], )
  71. pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y4],
  72. [speed_dial_end_x, speed_dial_y3], )
  73.  
  74. def piecesInit(self):
  75. MainGame.piecesList.append(pieces.Rooks(MainGame.player2Color, ,))
  76. MainGame.piecesList.append(pieces.Rooks(MainGame.player2Color, , ))
  77. MainGame.piecesList.append(pieces.Elephants(MainGame.player2Color, , ))
  78. MainGame.piecesList.append(pieces.Elephants(MainGame.player2Color, , ))
  79. MainGame.piecesList.append(pieces.King(MainGame.player2Color, , ))
  80. MainGame.piecesList.append(pieces.Knighs(MainGame.player2Color, , ))
  81. MainGame.piecesList.append(pieces.Knighs(MainGame.player2Color, , ))
  82. MainGame.piecesList.append(pieces.Cannons(MainGame.player2Color, , ))
  83. MainGame.piecesList.append(pieces.Cannons(MainGame.player2Color, , ))
  84. MainGame.piecesList.append(pieces.Mandarins(MainGame.player2Color, , ))
  85. MainGame.piecesList.append(pieces.Mandarins(MainGame.player2Color, , ))
  86. MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, , ))
  87. MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, , ))
  88. MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, , ))
  89. MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, , ))
  90. MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, , ))
  91.  
  92. MainGame.piecesList.append(pieces.Rooks(MainGame.player1Color, , ))
  93. MainGame.piecesList.append(pieces.Rooks(MainGame.player1Color, , ))
  94. MainGame.piecesList.append(pieces.Elephants(MainGame.player1Color, , ))
  95. MainGame.piecesList.append(pieces.Elephants(MainGame.player1Color, , ))
  96. MainGame.piecesList.append(pieces.King(MainGame.player1Color, , ))
  97. MainGame.piecesList.append(pieces.Knighs(MainGame.player1Color, , ))
  98. MainGame.piecesList.append(pieces.Knighs(MainGame.player1Color, , ))
  99. MainGame.piecesList.append(pieces.Cannons(MainGame.player1Color, , ))
  100. MainGame.piecesList.append(pieces.Cannons(MainGame.player1Color, , ))
  101. MainGame.piecesList.append(pieces.Mandarins(MainGame.player1Color, , ))
  102. MainGame.piecesList.append(pieces.Mandarins(MainGame.player1Color, , ))
  103. MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, , ))
  104. MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, , ))
  105. MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, , ))
  106. MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, , ))
  107. MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, , ))
  108.  
  109. def piecesDisplay(self):
  110. for item in MainGame.piecesList:
  111. item.displaypieces(MainGame.window)
  112. #MainGame.window.blit(item.image, item.rect)
  113.  
  114. def getEvent(self):
  115. # 获取所有的事件
  116. eventList = pygame.event.get()
  117. for event in eventList:
  118. if event.type == pygame.QUIT:
  119. self.endGame()
  120. elif event.type == pygame.MOUSEBUTTONDOWN:
  121. pos = pygame.mouse.get_pos()
  122. mouse_x = pos[]
  123. mouse_y = pos[]
  124. if (
  125. mouse_x > MainGame.Start_X - MainGame.Line_Span / and mouse_x < MainGame.Max_X + MainGame.Line_Span / ) and (
  126. mouse_y > MainGame.Start_Y - MainGame.Line_Span / and mouse_y < MainGame.Max_Y + MainGame.Line_Span / ):
  127. # print( str(mouse_x) + "" + str(mouse_y))
  128. # print(str(MainGame.Putdownflag))
  129. if MainGame.Putdownflag != MainGame.player1Color:
  130. return
  131.  
  132. click_x = round((mouse_x - MainGame.Start_X) / MainGame.Line_Span)
  133. click_y = round((mouse_y - MainGame.Start_Y) / MainGame.Line_Span)
  134. click_mod_x = (mouse_x - MainGame.Start_X) % MainGame.Line_Span
  135. click_mod_y = (mouse_y - MainGame.Start_Y) % MainGame.Line_Span
  136. if abs(click_mod_x - MainGame.Line_Span / ) >= and abs(
  137. click_mod_y - MainGame.Line_Span / ) >= :
  138. # print("有效点:x="+str(click_x)+" y="+str(click_y))
  139. # 有效点击点
  140. self.PutdownPieces(MainGame.player1Color, click_x, click_y)
  141. else:
  142. print("out")
  143. if MainGame.button_go.is_click():
  144. #self.restart()
  145. print("button_go click")
  146. else:
  147. print("button_go click out")
  148.  
  149. def PutdownPieces(self, t, x, y):
  150. selectfilter=list(filter(lambda cm: cm.x == x and cm.y == y and cm.player == MainGame.player1Color,MainGame.piecesList))
  151. if len(selectfilter):
  152. MainGame.piecesSelected = selectfilter[]
  153. return
  154.  
  155. if MainGame.piecesSelected :
  156. #print("")
  157.  
  158. arr = pieces.listPiecestoArr(MainGame.piecesList)
  159. if MainGame.piecesSelected.canmove(arr, x, y):
  160. self.PiecesMove(MainGame.piecesSelected, x, y)
  161. MainGame.Putdownflag = MainGame.player2Color
  162. else:
  163. fi = filter(lambda p: p.x == x and p.y == y, MainGame.piecesList)
  164. listfi = list(fi)
  165. if len(listfi) != :
  166. MainGame.piecesSelected = listfi[]
  167.  
  168. def PiecesMove(self,pieces, x , y):
  169. for item in MainGame.piecesList:
  170. if item.x ==x and item.y == y:
  171. MainGame.piecesList.remove(item)
  172. pieces.x = x
  173. pieces.y = y
  174. print("move to " +str(x) +" "+str(y))
  175. return True
  176.  
  177. def Computerplay(self):
  178. if MainGame.Putdownflag == MainGame.player2Color:
  179. print("轮到电脑了")
  180. computermove = computer.getPlayInfo(MainGame.piecesList)
  181. #if computer==None:
  182. #return
  183. piecemove = None
  184. for item in MainGame.piecesList:
  185. if item.x == computermove[] and item.y == computermove[]:
  186. piecemove= item
  187.  
  188. self.PiecesMove(piecemove, computermove[], computermove[])
  189. MainGame.Putdownflag = MainGame.player1Color
  190.  
  191. #判断游戏胜利
  192. def VictoryOrDefeat(self):
  193. txt =""
  194. result = [MainGame.player1Color,MainGame.player2Color]
  195. for item in MainGame.piecesList:
  196. if type(item) ==pieces.King:
  197. if item.player == MainGame.player1Color:
  198. result.remove(MainGame.player1Color)
  199. if item.player == MainGame.player2Color:
  200. result.remove(MainGame.player2Color)
  201.  
  202. if len(result)==:
  203. return
  204. if result[] == MainGame.player1Color :
  205. txt = "失败!"
  206. else:
  207. txt = "胜利!"
  208. MainGame.window.blit(self.getTextSuface("%s" % txt), (constants.SCREEN_WIDTH - , ))
  209. MainGame.Putdownflag = constants.overColor
  210.  
  211. def getTextSuface(self, text):
  212. pygame.font.init()
  213. # print(pygame.font.get_fonts())
  214. font = pygame.font.SysFont('kaiti', )
  215. txt = font.render(text, True, constants.TEXT_COLOR)
  216. return txt
  217.  
  218. def endGame(self):
  219. print("exit")
  220. exit()
  221.  
  222. if __name__ == '__main__':
  223. MainGame().start_game()
constants.py 数据常量
  1. import pygame
  2.  
  3. SCREEN_WIDTH=
  4. SCREEN_HEIGHT=
  5. Start_X =
  6. Start_Y =
  7. Line_Span =
  8.  
  9. player1Color =
  10. player2Color =
  11. overColor =
  12.  
  13. BG_COLOR=pygame.Color(, , )
  14. Line_COLOR=pygame.Color(, , )
  15. TEXT_COLOR=pygame.Color(, , )
  16.  
  17. # 定义颜色
  18. BLACK = ( , , )
  19. WHITE = (, , )
  20. RED = (, , )
  21. GREEN = ( , , )
  22. BLUE = ( , , )
  23.  
  24. repeat =
  25.  
  26. pieces_images = {
  27. 'b_rook': pygame.image.load("imgs/s2/b_c.gif"),
  28. 'b_elephant': pygame.image.load("imgs/s2/b_x.gif"),
  29. 'b_king': pygame.image.load("imgs/s2/b_j.gif"),
  30. 'b_knigh': pygame.image.load("imgs/s2/b_m.gif"),
  31. 'b_mandarin': pygame.image.load("imgs/s2/b_s.gif"),
  32. 'b_cannon': pygame.image.load("imgs/s2/b_p.gif"),
  33. 'b_pawn': pygame.image.load("imgs/s2/b_z.gif"),
  34.  
  35. 'r_rook': pygame.image.load("imgs/s2/r_c.gif"),
  36. 'r_elephant': pygame.image.load("imgs/s2/r_x.gif"),
  37. 'r_king': pygame.image.load("imgs/s2/r_j.gif"),
  38. 'r_knigh': pygame.image.load("imgs/s2/r_m.gif"),
  39. 'r_mandarin': pygame.image.load("imgs/s2/r_s.gif"),
  40. 'r_cannon': pygame.image.load("imgs/s2/r_p.gif"),
  41. 'r_pawn': pygame.image.load("imgs/s2/r_z.gif"),
  42. }
pieces.py 棋子类,走法,
  1. import pygame
  2. import constants
  3.  
  4. class Pieces():
  5. def __init__(self, player, x, y):
  6. self.imagskey = self.getImagekey()
  7. self.image = constants.pieces_images[self.imagskey]
  8. self.x = x
  9. self.y = y
  10. self.player = player
  11. self.rect = self.image.get_rect()
  12. self.rect.left = constants.Start_X + x * constants.Line_Span - self.image.get_rect().width /
  13. self.rect.top = constants.Start_Y + y * constants.Line_Span - self.image.get_rect().height /
  14.  
  15. def displaypieces(self,screen):
  16. #print(str(self.rect.left))
  17. self.rect.left = constants.Start_X + self.x * constants.Line_Span - self.image.get_rect().width /
  18. self.rect.top = constants.Start_Y + self.y * constants.Line_Span - self.image.get_rect().height /
  19. screen.blit(self.image,self.rect);
  20. #self.image = self.images
  21. #MainGame.window.blit(self.image,self.rect)
  22.  
  23. def canmove(self, arr, moveto_x, moveto_y):
  24. pass
  25. def getImagekey(self):
  26. return None
  27. def getScoreWeight(self,listpieces):
  28. return None
  29.  
  30. class Rooks(Pieces):
  31. def __init__(self, player, x, y):
  32. self.player = player
  33. super().__init__(player, x, y)
  34.  
  35. def getImagekey(self):
  36. if self.player == constants.player1Color:
  37. return "r_rook"
  38. else:
  39. return "b_rook"
  40.  
  41. def canmove(self, arr, moveto_x, moveto_y):
  42. if self.x == moveto_x and self.y == moveto_y:
  43. return False
  44. if arr[moveto_x][moveto_y] ==self.player :
  45. return False
  46. if self.x == moveto_x:
  47. step = - if self.y > moveto_y else
  48. for i in range(self.y +step, moveto_y, step):
  49. if arr[self.x][i] != :
  50. return False
  51. #print(" move y")
  52. return True
  53.  
  54. if self.y == moveto_y:
  55. step = - if self.x > moveto_x else
  56. for i in range(self.x + step, moveto_x, step):
  57. if arr[i][self.y] != :
  58. return False
  59. return True
  60.  
  61. def getScoreWeight(self, listpieces):
  62. score =
  63. return score
  64.  
  65. class Knighs(Pieces):
  66. def __init__(self, player, x, y):
  67. self.player = player
  68. super().__init__(player, x, y)
  69. def getImagekey(self):
  70. if self.player == constants.player1Color:
  71. return "r_knigh"
  72. else:
  73. return "b_knigh"
  74. def canmove(self, arr, moveto_x, moveto_y):
  75. if self.x == moveto_x and self.y == moveto_y:
  76. return False
  77. if arr[moveto_x][moveto_y] == self.player:
  78. return False
  79. #print(str(self.x) +""+str(self.y))
  80. move_x = moveto_x-self.x
  81. move_y = moveto_y - self.y
  82. if abs(move_x) == and abs(move_y) == :
  83. step = if move_y > else -
  84. if arr[self.x][self.y + step] == :
  85. return True
  86. if abs(move_x) == and abs(move_y) == :
  87. step = if move_x > else -
  88. if arr[self.x +step][self.y] == :
  89. return True
  90.  
  91. def getScoreWeight(self, listpieces):
  92. score =
  93. return score
  94.  
  95. class Elephants(Pieces):
  96. def __init__(self, player, x, y):
  97. self.player = player
  98. super().__init__(player, x, y)
  99. def getImagekey(self):
  100. if self.player == constants.player1Color:
  101. return "r_elephant"
  102. else:
  103. return "b_elephant"
  104. def canmove(self, arr, moveto_x, moveto_y):
  105. if self.x == moveto_x and self.y == moveto_y:
  106. return False
  107. if arr[moveto_x][moveto_y] == self.player:
  108. return False
  109. if self.y <= and moveto_y >= or self.y >= and moveto_y <=:
  110. return False
  111. move_x = moveto_x - self.x
  112. move_y = moveto_y - self.y
  113. if abs(move_x) == and abs(move_y) == :
  114. step_x = if move_x > else -
  115. step_y = if move_y > else -
  116. if arr[self.x + step_x][self.y + step_y] == :
  117. return True
  118.  
  119. def getScoreWeight(self, listpieces):
  120. score =
  121. return score
  122. class Mandarins(Pieces):
  123.  
  124. def __init__(self, player, x, y):
  125. self.player = player
  126. super().__init__(player, x, y)
  127.  
  128. def getImagekey(self):
  129. if self.player == constants.player1Color:
  130. return "r_mandarin"
  131. else:
  132. return "b_mandarin"
  133. def canmove(self, arr, moveto_x, moveto_y):
  134. if self.x == moveto_x and self.y == moveto_y:
  135. return False
  136. if arr[moveto_x][moveto_y] == self.player:
  137. return False
  138. if moveto_x < or moveto_x >:
  139. return False
  140. if moveto_y > and moveto_y < :
  141. return False
  142. move_x = moveto_x - self.x
  143. move_y = moveto_y - self.y
  144. if abs(move_x) == and abs(move_y) == :
  145. return True
  146. def getScoreWeight(self, listpieces):
  147. score =
  148. return score
  149.  
  150. class King(Pieces):
  151. def __init__(self, player, x, y):
  152. self.player = player
  153. super().__init__(player, x, y)
  154. def getImagekey(self):
  155. if self.player == constants.player1Color:
  156. return "r_king"
  157. else:
  158. return "b_king"
  159.  
  160. def canmove(self, arr, moveto_x, moveto_y):
  161. if self.x == moveto_x and self.y == moveto_y:
  162. return False
  163. if arr[moveto_x][moveto_y] == self.player:
  164. return False
  165. if moveto_x < or moveto_x > :
  166. return False
  167. if moveto_y > and moveto_y < :
  168. return False
  169. move_x = moveto_x - self.x
  170. move_y = moveto_y - self.y
  171. if abs(move_x) + abs(move_y) == :
  172. return True
  173. def getScoreWeight(self, listpieces):
  174. score =
  175. return score
  176. class Cannons(Pieces):
  177. def __init__(self, player, x, y):
  178. self.player = player
  179. super().__init__(player, x, y)
  180. def getImagekey(self):
  181. if self.player == constants.player1Color:
  182. return "r_cannon"
  183. else:
  184. return "b_cannon"
  185.  
  186. def canmove(self, arr, moveto_x, moveto_y):
  187. if self.x == moveto_x and self.y == moveto_y:
  188. return False
  189. if arr[moveto_x][moveto_y] == self.player:
  190. return False
  191. overflag = False
  192. if self.x == moveto_x:
  193. step = - if self.y > moveto_y else
  194. for i in range(self.y + step, moveto_y, step):
  195. if arr[self.x][i] != :
  196. if overflag:
  197. return False
  198. else:
  199. overflag = True
  200.  
  201. if overflag and arr[moveto_x][moveto_y] == :
  202. return False
  203. if not overflag and arr[self.x][moveto_y] != :
  204. return False
  205.  
  206. return True
  207.  
  208. if self.y == moveto_y:
  209. step = - if self.x > moveto_x else
  210. for i in range(self.x + step, moveto_x, step):
  211. if arr[i][self.y] != :
  212. if overflag:
  213. return False
  214. else:
  215. overflag = True
  216.  
  217. if overflag and arr[moveto_x][moveto_y] == :
  218. return False
  219. if not overflag and arr[moveto_x][self.y] != :
  220. return False
  221. return True
  222. def getScoreWeight(self, listpieces):
  223. score =
  224. return score
  225.  
  226. class Pawns(Pieces):
  227. def __init__(self, player, x, y):
  228. self.player = player
  229. super().__init__(player, x, y)
  230. def getImagekey(self):
  231. if self.player == constants.player1Color:
  232. return "r_pawn"
  233. else:
  234. return "b_pawn"
  235.  
  236. def canmove(self, arr, moveto_x, moveto_y):
  237. if self.x == moveto_x and self.y == moveto_y:
  238. return False
  239. if arr[moveto_x][moveto_y] == self.player:
  240. return False
  241. move_x = moveto_x - self.x
  242. move_y = moveto_y - self.y
  243.  
  244. if self.player == constants.player1Color:
  245. if self.y > and move_x != :
  246. return False
  247. if move_y > :
  248. return False
  249. elif self.player == constants.player2Color:
  250. if self.y <= and move_x != :
  251. return False
  252. if move_y < :
  253. return False
  254.  
  255. if abs(move_x) + abs(move_y) == :
  256. return True
  257. def getScoreWeight(self, listpieces):
  258. score =
  259. return score
  260.  
  261. def listPiecestoArr(piecesList):
  262. arr = [[ for i in range()] for j in range()]
  263. for i in range(, ):
  264. for j in range(, ):
  265. if len(list(filter(lambda cm: cm.x == i and cm.y == j and cm.player == constants.player1Color,
  266. piecesList))):
  267. arr[i][j] = constants.player1Color
  268. elif len(list(filter(lambda cm: cm.x == i and cm.y == j and cm.player == constants.player2Color,
  269. piecesList))):
  270. arr[i][j] = constants.player2Color
  271.  
  272. return arr
computer.py 电脑走法计算
  1. import constants
  2. #import time
  3. from pieces import listPiecestoArr
  4.  
  5. def getPlayInfo(listpieces):
  6. pieces = movedeep(listpieces , ,constants.player2Color)
  7. return [pieces[].x,pieces[].y, pieces[], pieces[]]
  8.  
  9. def movedeep(listpieces, deepstep, player):
  10. arr = listPiecestoArr(listpieces)
  11. listMoveEnabel = []
  12. for i in range(, ):
  13. for j in range(, ):
  14. for item in listpieces:
  15. if item.player == player and item.canmove(arr, i, j):
  16. #标记是否有子被吃 如果被吃 在下次循环时需要补会
  17. piecesremove = None
  18. for itembefore in listpieces:
  19. if itembefore.x == i and itembefore.y == j:
  20. piecesremove= itembefore
  21. break
  22. if piecesremove != None:
  23. listpieces.remove(piecesremove)
  24.  
  25. #记录移动之前的位置
  26. move_x = item.x
  27. move_y = item.y
  28. item.x = i
  29. item.y = j
  30.  
  31. #print(str(move_x) + "," + str(move_y) + "," + str(item.x) + " , " + str(item.y))
  32. scoreplayer1 =
  33. scoreplayer2 =
  34. for itemafter in listpieces:
  35. if itemafter.player == constants.player1Color:
  36. scoreplayer1 += itemafter.getScoreWeight(listpieces)
  37. elif itemafter.player == constants.player2Color:
  38. scoreplayer2 += itemafter.getScoreWeight(listpieces)
  39.  
  40. #print("得分:"+item.imagskey +", "+str(len(moveAfterListpieces))+","+str(i)+","+str(j)+"," +str(scoreplayer1) +" , "+ str(scoreplayer2) )
  41. #print(str(deepstep))
  42. #如果得子 判断对面是否可以杀过来,如果又被杀,而且子力评分低,则不干
  43. arrkill = listPiecestoArr(listpieces)
  44.  
  45. if scoreplayer2 > scoreplayer1 :
  46. for itemkill in listpieces:
  47. if itemkill.player == constants.player1Color and itemkill.canmove(arrkill, i, j):
  48. scoreplayer2=scoreplayer1
  49.  
  50. if deepstep > :
  51. nextplayer = constants.player1Color if player == constants.player2Color else constants.player2Color
  52. nextpiecesbest= movedeep(listpieces, deepstep -, nextplayer)
  53. listMoveEnabel.append([item, i, j, nextpiecesbest[], nextpiecesbest[], nextpiecesbest[]])
  54. else:
  55. #print(str(len(listpieces)))
  56. #print("得分:" + item.imagskey + ", " + str(len(listpieces)) + "," + str(move_x) + "," + str(move_y) + "," + str(i) + " , " + str(j))
  57. if player == constants.player2Color:
  58. listMoveEnabel.append([item, i, j, scoreplayer1, scoreplayer2, scoreplayer1 - scoreplayer2])
  59. else:
  60. listMoveEnabel.append([item, i, j, scoreplayer1, scoreplayer2, scoreplayer2 - scoreplayer1])
  61. #print("得分:"+str(scoreplayer1))
  62. item.x = move_x
  63. item.y = move_y
  64. if piecesremove != None:
  65. listpieces.append(piecesremove)
  66.  
  67. list_scorepalyer1 = sorted(listMoveEnabel, key=lambda tm: tm[], reverse=True)
  68. piecesbest = list_scorepalyer1[]
  69. if deepstep == :
  70. print(list_scorepalyer1)
  71. return piecesbest

关注微信公众号“ python社区营 ”分享技术干货,提供学习资源

Python 中国象棋源码 V1的更多相关文章

  1. android 在线升级借助开源中国App源码

    android 在线升级借助开源中国App源码 http://www.cnblogs.com/luomingui/p/3949429.html android 在线升级借助开源中国App源码分析如下: ...

  2. python的paramiko源码修改了一下,写了个操作命令的日志审计 bug修改

    python的paramiko源码修改了一下,写了个操作命令的日志审计,但是记录的日志中也将backspace删除键记录成^H这个了,于是改了一下代码,用字符串的特性. 字符串具有列表的特性 > ...

  3. Python解析器源码加密系列之(二):一次使用标准c的FILE*访问内存块的尝试

    摘要:由于近期打算修改Python解释器以实现pyc文件的加密/解密,出于保密的要求,解密之后的数据只能放在内存中,不能写入到文件中.但是后续的解析pyc文件的代码又只能接受FILE*作为入参,所以就 ...

  4. Python 3.5源码编译安装

    系统环境:CentOS 6.8-Minimal 安装Python依赖包: [root@Python src]# yum install zlib-devel bzip2-devel openssl-d ...

  5. Python:Sqlmap源码精读之解析xml

    XML <?xml version="1.0" encoding="UTF-8"?> <root> <!-- MySQL --&g ...

  6. 读懂掌握 Python logging 模块源码 (附带一些 example)

    搜了一下自己的 Blog 一直缺乏一篇 Python logging 模块的深度使用的文章.其实这个模块非常常用,也有非常多的滥用.所以看看源码来详细记录一篇属于 logging 模块的文章. 整个 ...

  7. Python paramiko 修改源码实现用户命令抓取

    paramiko 源码修改 paramiko主要用来实现ssh客户端.服务端链接,上一节我们说到了堡垒机,堡垒机内有一个需求是“用户行为审计”,在这里我们就可以通过修改paramiko内文件的源码来实 ...

  8. 【Python】Webpy 源码学习

    那么webpy是什么呢? 阅读它的源码我们又能学到什么呢? 简单说webpy就是一个开源的web应用框架(官方首页:http://webpy.org/) 它的源代码非常整洁精干,学习它一方面可以让我们 ...

  9. Python Web Flask源码解读(一)——启动流程

    关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...

随机推荐

  1. django初始化

    Django 版本 安装 pip安装 pip install django 安装最新版本的 pip install django==1.11.11 安装指定版本的 验证安装 直接去代码中调用djang ...

  2. JavaScript新手经常遇到的问题(二)

    1.Form表单只提交数据而不进行页面跳转的方法 <script type="text/javascript" src="js/jquery/jquery-1.8. ...

  3. HOOK的类型

  4. 华为ARM64服务器上手体验--不吹不黑,用实际应用来看看TaiShan鲲鹏的表现

    背景 中美贸易冲突以来,相信最大的感受,并不是我对你加多少关税,而是我有,可我不卖给你."禁售"成了市场经济中最大的竞争力. 相信也是因为这个原因,华为"备胎转正&quo ...

  5. 关于SQL Server 中日期格式化若干问题

    select CONVERT(varchar, getdate(), 120 )2004-09-12 11:06:08 select replace(replace(replace(CONVERT(v ...

  6. elasticSerach 知识学习

    一 介绍: ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java语言开发的, ...

  7. SpringBoot-多环境切换相关(六)

    多环境切换 profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境: 方式一:多配置文件 我们在主配置文件编写的时候,文件名可以是 applicat ...

  8. 2019牛客全国多校第八场A题 All-one Matrices(单调栈)

    题意:让你找最大不可扩展全1子矩阵的数量: 题解:考虑枚举每一行为全1子矩阵的的底,然后从左到右枚举:up[i][j]:表示(i,j)这个位置向上可扩展多少,同时还有记录每个位置(i,j)向左最多可扩 ...

  9. 前端flex布局学习笔记

    flex布局,即为弹性布局,其为盒模型提供最大的灵活性,任何一个容器都可以指定为flex布局. eg:.box{ display:flex: } 行内元素也可以使用flex布局. 注意:设置flex布 ...

  10. BX谷 2019年最新所有人都能学会的数据分析课视频教程

    第一章 数据分析师职业概览 1-1 数据分析师的职业概览免费试学 数据分析师的"钱"景如何 什么人适合学数据分析 数据分析师的临界知识 数据分析师的主要职责 第二章 数据分析和数据 ...