人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

Python實(shí)現(xiàn)人機(jī)中國(guó)象棋游戲

發(fā)布日期:2022-02-02 15:49 | 文章來(lái)源:站長(zhǎng)之家

導(dǎo)語(yǔ)

哈嘍!哈嘍!我是木木子!今日游戲更新——中國(guó)象棋上線啦!

中國(guó)象棋是一種古老的棋類游戲,大約有兩千年的歷史。

是中華文明非物質(zhì)文化經(jīng)典產(chǎn)物,藝術(shù)價(jià)值泛屬于整個(gè)人類文明進(jìn)化史的一個(gè)分枝。

在中國(guó),可以隨處在大街上、小公園兒里等地方經(jīng)??吹揭欢讶藝谝黄鹣缕?,這就足以說(shuō)明中國(guó)象棋的流行性以及普遍性有多高!

早前曾有統(tǒng)計(jì),14、15個(gè)中國(guó)人當(dāng)中,就有1個(gè)會(huì)下中國(guó)象棋。中國(guó)象棋的受眾,可能數(shù)以億計(jì)!

今天教大家制作一款中國(guó)象棋and想學(xué)象棋的話也可以來(lái)看看當(dāng)作新手村吧~

1.游戲規(guī)則&基本玩法

1.1 基本玩法

中國(guó)象棋的游戲用具由棋盤(pán)和棋子組成,對(duì)局時(shí),由執(zhí)紅棋的一方先走,雙方輪流各走一招,直至分出勝、負(fù)、和,對(duì)局即終了。輪到走棋的一方,將某個(gè)棋子從一個(gè)交叉點(diǎn)走到另一個(gè)交叉點(diǎn),或者吃掉對(duì)方的棋子而占領(lǐng)其交叉點(diǎn),都算走了一著。雙方各走一著,稱為一個(gè)回合。

1.2 行棋規(guī)則

2.素材文件

3.主要代碼

chinachess.py 為主文件;constants.py 數(shù)據(jù)常量;pieces.py 棋子類,走法;computer.py 電腦走法計(jì)算;button.py按鈕定義。

目前電腦走法比較傻,有興趣的朋友可以對(duì)computer.py 進(jìn)行升級(jí)!不過(guò)這針對(duì)大部分的新手剛開(kāi)始學(xué)象棋的話完全夠用了哈~哈哈哈 如果你新手入門(mén)玩兒的過(guò)電腦就說(shuō)明你入門(mén)了!

3.1 Chinachess.py 為主文件

import pygame
import time
import Xiangqi.constants as constants
from Xiangqi.button import Button
import Xiangqi.pieces as pieces
import Xiangqi.computer as computer
 
 
class MainGame():
 window = None
 Start_X = constants.Start_X
 Start_Y = constants.Start_Y
 Line_Span = constants.Line_Span
 Max_X = Start_X + 8 * Line_Span
 Max_Y = Start_Y + 9 * Line_Span
 
 
 player1Color = constants.player1Color
 player2Color = constants.player2Color
 Putdownflag = player1Color
 piecesSelected = None
 
 
 button_go = None
 piecesList = []
 
 
 def start_game(self):
  MainGame.window = pygame.display.set_mode([constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT])
  pygame.display.set_caption("Python代碼大全-中國(guó)象棋")
  MainGame.button_go = Button(MainGame.window, "重新開(kāi)始", constants.SCREEN_WIDTH - 100, 300)  # 創(chuàng)建開(kāi)始按鈕
  self.piecesInit()
 
 
  while True:
time.sleep(0.1)
# 獲取事件
MainGame.window.fill(constants.BG_COLOR)
self.drawChessboard()
#MainGame.button_go.draw_button()
self.piecesDisplay()
self.VictoryOrDefeat()
self.Computerplay()
self.getEvent()
pygame.display.update()
pygame.display.flip()
 
 
 def drawChessboard(self): #畫(huà)象棋盤(pán)
  mid_end_y = MainGame.Start_Y + 4 * MainGame.Line_Span
  min_start_y = MainGame.Start_Y + 5 * MainGame.Line_Span
  for i in range(0, 9):
x = MainGame.Start_X + i * MainGame.Line_Span
if i==0 or i ==8:
 y = MainGame.Start_Y + i * MainGame.Line_Span
 pygame.draw.line(MainGame.window, constants.BLACK, [x, MainGame.Start_Y], [x, MainGame.Max_Y], 1)
else:
 pygame.draw.line(MainGame.window, constants.BLACK, [x, MainGame.Start_Y], [x, mid_end_y], 1)
 pygame.draw.line(MainGame.window, constants.BLACK, [x, min_start_y], [x, MainGame.Max_Y], 1)
 
 
  for i in range(0, 10):
x = MainGame.Start_X + i * MainGame.Line_Span
y = MainGame.Start_Y + i * MainGame.Line_Span
pygame.draw.line(MainGame.window, constants.BLACK, [MainGame.Start_X, y], [MainGame.Max_X, y], 1)
 
 
  speed_dial_start_x =  MainGame.Start_X + 3 * MainGame.Line_Span
  speed_dial_end_x =  MainGame.Start_X + 5 * MainGame.Line_Span
  speed_dial_y1 = MainGame.Start_Y + 0 * MainGame.Line_Span
  speed_dial_y2 = MainGame.Start_Y + 2 * MainGame.Line_Span
  speed_dial_y3 = MainGame.Start_Y + 7 * MainGame.Line_Span
  speed_dial_y4 = MainGame.Start_Y + 9 * MainGame.Line_Span
 
 
  pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y1], [speed_dial_end_x, speed_dial_y2], 1)
  pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y2], [speed_dial_end_x, speed_dial_y1], 1)
  pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y3], [speed_dial_end_x, speed_dial_y4], 1)
  pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y4], [speed_dial_end_x, speed_dial_y3], 1)
 
 
 def piecesInit(self):  #加載棋子
  MainGame.piecesList.append(pieces.Rooks(MainGame.player2Color, 0,0))
  MainGame.piecesList.append(pieces.Rooks(MainGame.player2Color,  8, 0))
  MainGame.piecesList.append(pieces.Elephants(MainGame.player2Color,  2, 0))
  MainGame.piecesList.append(pieces.Elephants(MainGame.player2Color,  6, 0))
  MainGame.piecesList.append(pieces.King(MainGame.player2Color, 4, 0))
  MainGame.piecesList.append(pieces.Knighs(MainGame.player2Color,  1, 0))
  MainGame.piecesList.append(pieces.Knighs(MainGame.player2Color,  7, 0))
  MainGame.piecesList.append(pieces.Cannons(MainGame.player2Color,  1, 2))
  MainGame.piecesList.append(pieces.Cannons(MainGame.player2Color, 7, 2))
  MainGame.piecesList.append(pieces.Mandarins(MainGame.player2Color,  3, 0))
  MainGame.piecesList.append(pieces.Mandarins(MainGame.player2Color, 5, 0))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 0, 3))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 2, 3))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 4, 3))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 6, 3))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 8, 3))
 
 
  MainGame.piecesList.append(pieces.Rooks(MainGame.player1Color,  0, 9))
  MainGame.piecesList.append(pieces.Rooks(MainGame.player1Color,  8, 9))
  MainGame.piecesList.append(pieces.Elephants(MainGame.player1Color, 2, 9))
  MainGame.piecesList.append(pieces.Elephants(MainGame.player1Color, 6, 9))
  MainGame.piecesList.append(pieces.King(MainGame.player1Color,  4, 9))
  MainGame.piecesList.append(pieces.Knighs(MainGame.player1Color, 1, 9))
  MainGame.piecesList.append(pieces.Knighs(MainGame.player1Color, 7, 9))
  MainGame.piecesList.append(pieces.Cannons(MainGame.player1Color,  1, 7))
  MainGame.piecesList.append(pieces.Cannons(MainGame.player1Color,  7, 7))
  MainGame.piecesList.append(pieces.Mandarins(MainGame.player1Color,  3, 9))
  MainGame.piecesList.append(pieces.Mandarins(MainGame.player1Color,  5, 9))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 0, 6))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 2, 6))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 4, 6))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 6, 6))
  MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 8, 6))
 
 
 def piecesDisplay(self):
  for item in MainGame.piecesList:
item.displaypieces(MainGame.window)
#MainGame.window.blit(item.image, item.rect)
 
 
 def getEvent(self):
  # 獲取所有的事件
  eventList = pygame.event.get()
  for event in eventList:
if event.type == pygame.QUIT:
 self.endGame()
elif event.type == pygame.MOUSEBUTTONDOWN:
 pos = pygame.mouse.get_pos()
 mouse_x = pos[0]
 mouse_y = pos[1]
 if (mouse_x > MainGame.Start_X - MainGame.Line_Span / 2 and mouse_x < MainGame.Max_X + MainGame.Line_Span / 2) and (mouse_y > MainGame.Start_Y - MainGame.Line_Span / 2 and mouse_y < MainGame.Max_Y + MainGame.Line_Span / 2):
  # print( str(mouse_x) + "" + str(mouse_y))
  # print(str(MainGame.Putdownflag))
  if MainGame.Putdownflag != MainGame.player1Color:return
 
 
  click_x = round((mouse_x - MainGame.Start_X) / MainGame.Line_Span)
  click_y = round((mouse_y - MainGame.Start_Y) / MainGame.Line_Span)
  click_mod_x = (mouse_x - MainGame.Start_X) % MainGame.Line_Span
  click_mod_y = (mouse_y - MainGame.Start_Y) % MainGame.Line_Span
  if abs(click_mod_x - MainGame.Line_Span / 2) >= 5 and abs(
click_mod_y - MainGame.Line_Span / 2) >= 5:# print("有效點(diǎn):x="+str(click_x)+" y="+str(click_y))# 有效點(diǎn)擊點(diǎn)self.PutdownPieces(MainGame.player1Color, click_x, click_y)
 else:
  print("out")
 if MainGame.button_go.is_click():
  #self.restart()
  print("button_go click")
 else:
  print("button_go click out")
 
 
 def PutdownPieces(self, t, x, y):
  selectfilter=list(filter(lambda cm: cm.x == x and cm.y == y and cm.player == MainGame.player1Color,MainGame.piecesList))
  if len(selectfilter):
MainGame.piecesSelected = selectfilter[0]
return
 
 
  if MainGame.piecesSelected :
#print("1111")
 
 
arr = pieces.listPiecestoArr(MainGame.piecesList)
if MainGame.piecesSelected.canmove(arr, x, y):
 self.PiecesMove(MainGame.piecesSelected, x, y)
 MainGame.Putdownflag = MainGame.player2Color
  else:
fi = filter(lambda p: p.x == x and p.y == y, MainGame.piecesList)
listfi = list(fi)
if len(listfi) != 0:
 MainGame.piecesSelected = listfi[0]
 
 
 def PiecesMove(self,pieces,  x , y):
  for item in  MainGame.piecesList:
if item.x ==x and item.y == y:
 MainGame.piecesList.remove(item)
  pieces.x = x
  pieces.y = y
  print("move to " +str(x) +" "+str(y))
  return True
 
 
 def Computerplay(self):
  if MainGame.Putdownflag == MainGame.player2Color:
print("輪到電腦了")
computermove = computer.getPlayInfo(MainGame.piecesList)
#if computer==None:
 #return
piecemove = None
for item in MainGame.piecesList:
 if item.x == computermove[0] and item.y == computermove[1]:
  piecemove= item
 
 
self.PiecesMove(piecemove, computermove[2], computermove[3])
MainGame.Putdownflag = MainGame.player1Color
 
 
 #判斷游戲勝利
 def VictoryOrDefeat(self):
  txt =""
  result = [MainGame.player1Color,MainGame.player2Color]
  for item in MainGame.piecesList:
if type(item) ==pieces.King:
 if item.player == MainGame.player1Color:
  result.remove(MainGame.player1Color)
 if item.player == MainGame.player2Color:
  result.remove(MainGame.player2Color)
 
 
  if len(result)==0:
return
  if result[0] == MainGame.player1Color :
txt = "失敗!"
  else:
txt = "勝利!"
  MainGame.window.blit(self.getTextSuface("%s" % txt), (constants.SCREEN_WIDTH - 100, 200))
  MainGame.Putdownflag = constants.overColor
 
 
 def getTextSuface(self, text):
  pygame.font.init()
  # print(pygame.font.get_fonts())
  font = pygame.font.SysFont('kaiti', 18)
  txt = font.render(text, True, constants.TEXT_COLOR)
  return txt
 
 
 def endGame(self):
  print("exit")
  exit()
 
 
if __name__ == '__main__':
 MainGame().start_game()

3.2 Constants.py 數(shù)據(jù)常量

#數(shù)據(jù)常量
import pygame
 
 
SCREEN_WIDTH=900
SCREEN_HEIGHT=650
Start_X = 50
Start_Y = 50
Line_Span = 60
 
 
player1Color = 1
player2Color = 2
overColor = 3
 
 
BG_COLOR=pygame.Color(200, 200, 200)
Line_COLOR=pygame.Color(255, 255, 200)
TEXT_COLOR=pygame.Color(255, 0, 0)
 
 
# 定義顏色
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
 
 
repeat = 0
 
 
pieces_images = {
 'b_rook': pygame.image.load("imgs/s2/b_c.gif"),
 'b_elephant': pygame.image.load("imgs/s2/b_x.gif"),
 'b_king': pygame.image.load("imgs/s2/b_j.gif"),
 'b_knigh': pygame.image.load("imgs/s2/b_m.gif"),
 'b_mandarin': pygame.image.load("imgs/s2/b_s.gif"),
 'b_cannon': pygame.image.load("imgs/s2/b_p.gif"),
 'b_pawn': pygame.image.load("imgs/s2/b_z.gif"),
 
 
 'r_rook': pygame.image.load("imgs/s2/r_c.gif"),
 'r_elephant': pygame.image.load("imgs/s2/r_x.gif"),
 'r_king': pygame.image.load("imgs/s2/r_j.gif"),
 'r_knigh': pygame.image.load("imgs/s2/r_m.gif"),
 'r_mandarin': pygame.image.load("imgs/s2/r_s.gif"),
 'r_cannon': pygame.image.load("imgs/s2/r_p.gif"),
 'r_pawn': pygame.image.load("imgs/s2/r_z.gif"),
}

3.3 Pieces.py 棋子類,走法

#棋子類,走法
import pygame
import Xiangqi.constants as constants
 
 
class  Pieces():
 def __init__(self, player,  x, y):
  self.imagskey = self.getImagekey()
  self.image = constants.pieces_images[self.imagskey]
  self.x = x
  self.y = y
  self.player = player
  self.rect = self.image.get_rect()
  self.rect.left = constants.Start_X + x * constants.Line_Span - self.image.get_rect().width / 2
  self.rect.top = constants.Start_Y + y * constants.Line_Span - self.image.get_rect().height / 2
 
 
 def displaypieces(self,screen):
  #print(str(self.rect.left))
  self.rect.left = constants.Start_X + self.x * constants.Line_Span - self.image.get_rect().width / 2
  self.rect.top = constants.Start_Y + self.y * constants.Line_Span - self.image.get_rect().height / 2
  screen.blit(self.image,self.rect);
  #self.image = self.images
  #MainGame.window.blit(self.image,self.rect)
 
 
 def canmove(self, arr, moveto_x, moveto_y):
  pass
 def getImagekey(self):
  return None
 def getScoreWeight(self,listpieces):
  return  None
 
 
class Rooks(Pieces):
 def __init__(self, player,  x, y):
  self.player = player
  super().__init__(player,  x, y)
 
 
 def getImagekey(self):
  if self.player == constants.player1Color:
return "r_rook"
  else:
return "b_rook"
 
 
 def canmove(self, arr, moveto_x, moveto_y):
  if self.x == moveto_x and self.y == moveto_y:
return False
  if arr[moveto_x][moveto_y] ==self.player :
return  False
  if self.x == moveto_x:
step = -1 if self.y > moveto_y else 1
for i in range(self.y +step, moveto_y, step):
 if arr[self.x][i] !=0 :
  return False
#print(" move y")
return True
 
 
  if self.y == moveto_y:
step = -1 if self.x > moveto_x else 1
for i in range(self.x + step, moveto_x, step):
 if arr[i][self.y] != 0:
  return False
return True
 
 
 def getScoreWeight(self, listpieces):
  score = 11
  return score
 
 
class Knighs(Pieces):
 def __init__(self, player,  x, y):
  self.player = player
  super().__init__(player,  x, y)
 def getImagekey(self):
  if self.player == constants.player1Color:
return "r_knigh"
  else:
return "b_knigh"
 def canmove(self, arr, moveto_x, moveto_y):
  if self.x == moveto_x and self.y == moveto_y:
return False
  if arr[moveto_x][moveto_y] == self.player:
return False
  #print(str(self.x) +""+str(self.y))
  move_x = moveto_x-self.x
  move_y = moveto_y - self.y
  if abs(move_x) == 1 and abs(move_y) == 2:
step = 1 if move_y > 0 else -1
if arr[self.x][self.y + step] == 0:
 return True
  if abs(move_x) == 2 and abs(move_y) == 1:
step = 1 if move_x >0 else -1
if arr[self.x +step][self.y] ==0 :
 return  True
 
 
 def getScoreWeight(self, listpieces):
  score = 5
  return score
 
 
class Elephants(Pieces):
 def __init__(self, player, x, y):
  self.player = player
  super().__init__(player, x, y)
 def getImagekey(self):
  if self.player == constants.player1Color:
return "r_elephant"
  else:
return "b_elephant"
 def canmove(self, arr, moveto_x, moveto_y):
  if self.x == moveto_x and self.y == moveto_y:
return False
  if arr[moveto_x][moveto_y] == self.player:
return False
  if self.y <=4 and moveto_y >=5 or self.y >=5 and moveto_y <=4:
return  False
  move_x = moveto_x - self.x
  move_y = moveto_y - self.y
  if abs(move_x) == 2 and abs(move_y) == 2:
step_x = 1 if move_x > 0 else -1
step_y = 1 if move_y > 0 else -1
if arr[self.x + step_x][self.y + step_y] == 0:
 return True
 
 
 def getScoreWeight(self, listpieces):
  score = 2
  return score
class Mandarins(Pieces):
 
 
 def __init__(self, player,  x, y):
  self.player = player
  super().__init__(player,  x, y)
 
 
 def getImagekey(self):
  if self.player == constants.player1Color:
return "r_mandarin"
  else:
return "b_mandarin"
 def canmove(self, arr, moveto_x, moveto_y):
  if self.x == moveto_x and self.y == moveto_y:
return False
  if arr[moveto_x][moveto_y] == self.player:
return False
  if moveto_x <3 or moveto_x >5:
return False
  if moveto_y > 2 and moveto_y < 7:
return False
  move_x = moveto_x - self.x
  move_y = moveto_y - self.y
  if abs(move_x) == 1 and abs(move_y) == 1:
return True
 def getScoreWeight(self, listpieces):
  score = 2
  return score
 
 
class King(Pieces):
 def __init__(self, player, x, y):
  self.player = player
  super().__init__(player, x, y)
 def getImagekey(self):
  if self.player == constants.player1Color:
return "r_king"
  else:
return "b_king"
 
 
 def canmove(self, arr, moveto_x, moveto_y):
  if self.x == moveto_x and self.y == moveto_y:
return False
  if arr[moveto_x][moveto_y] == self.player:
return False
  if moveto_x < 3 or moveto_x > 5:
return False
  if moveto_y > 2 and moveto_y < 7:
return False
  move_x = moveto_x - self.x
  move_y = moveto_y - self.y
  if abs(move_x) + abs(move_y) == 1:
return True
 def getScoreWeight(self, listpieces):
  score = 150
  return score
class Cannons(Pieces):
 def __init__(self, player,  x, y):
  self.player = player
  super().__init__(player, x, y)
 def getImagekey(self):
  if self.player == constants.player1Color:
return "r_cannon"
  else:
return "b_cannon"
 
 
 def canmove(self, arr, moveto_x, moveto_y):
  if self.x == moveto_x and self.y == moveto_y:
return False
  if arr[moveto_x][moveto_y] == self.player:
return False
  overflag = False
  if self.x == moveto_x:
step = -1 if self.y > moveto_y else 1
for i in range(self.y + step, moveto_y, step):
 if arr[self.x][i] != 0:
  if overflag:return False
  else:overflag = True
 
 
if overflag and arr[moveto_x][moveto_y] == 0:
 return False
if not overflag and arr[self.x][moveto_y] != 0:
 return False
 
 
return True
 
 
  if self.y == moveto_y:
step = -1 if self.x > moveto_x else 1
for i in range(self.x + step, moveto_x, step):
 if arr[i][self.y] != 0:
  if overflag:return False
  else:overflag = True
 
 
if overflag and arr[moveto_x][moveto_y] == 0:
 return False
if not overflag and arr[moveto_x][self.y] != 0:
 return False
return True
 def getScoreWeight(self, listpieces):
  score = 6
  return score
 
 
class Pawns(Pieces):
 def __init__(self, player, x, y):
  self.player = player
  super().__init__(player,  x, y)
 def getImagekey(self):
  if self.player == constants.player1Color:
return "r_pawn"
  else:
return "b_pawn"
 
 
 def canmove(self, arr, moveto_x, moveto_y):
  if self.x == moveto_x and self.y == moveto_y:
return False
  if arr[moveto_x][moveto_y] == self.player:
return False
  move_x = moveto_x - self.x
  move_y = moveto_y - self.y
 
 
  if self.player == constants.player1Color:
if self.y > 4  and move_x != 0 :
 return  False
if move_y > 0:
 return  False
  elif self.player == constants.player2Color:
if self.y <= 4  and move_x != 0 :
 return  False
if move_y < 0:
 return False
 
 
  if abs(move_x) + abs(move_y) == 1:
return True
 def getScoreWeight(self, listpieces):
  score = 2
  return score
 
 
def listPiecestoArr(piecesList):
 arr = [[0 for i in range(10)] for j in range(9)]
 for i in range(0, 9):
  for j in range(0, 10):
if len(list(filter(lambda cm: cm.x == i and cm.y == j and cm.player == constants.player1Color,
piecesList))):
 arr[i][j] = constants.player1Color
elif len(list(filter(lambda cm: cm.x == i and cm.y == j and cm.player == constants.player2Color,
  piecesList))):
 arr[i][j] = constants.player2Color
 
 
 return arr

3.4 Computer.py 電腦走法計(jì)算

#電腦走法計(jì)算
import Xiangqi.constants as constants
#import time
from Xiangqi.pieces import listPiecestoArr
 
 
def getPlayInfo(listpieces):
 pieces = movedeep(listpieces ,1 ,constants.player2Color)
 return [pieces[0].x,pieces[0].y, pieces[1], pieces[2]]
 
 
def movedeep(listpieces, deepstep, player):
 arr = listPiecestoArr(listpieces)
 listMoveEnabel = []
 for i in range(0, 9):
  for j in range(0, 10):
for item in listpieces:
 if item.player == player and item.canmove(arr, i, j):
  #標(biāo)記是否有子被吃 如果被吃 在下次循環(huán)時(shí)需要補(bǔ)會(huì)
  piecesremove = None
  for itembefore in listpieces:if itembefore.x == i and itembefore.y == j:
piecesremove= itembefore
break
  if piecesremove != None:listpieces.remove(piecesremove)
 
 
  #記錄移動(dòng)之前的位置
  move_x = item.x
  move_y = item.y
  item.x = i
  item.y = j
 
 
  #print(str(move_x) + "," + str(move_y) + "," + str(item.x) + "  ,  " + str(item.y))
  scoreplayer1 = 0
  scoreplayer2 = 0
  for itemafter in listpieces:if itemafter.player == constants.player1Color:
scoreplayer1 += itemafter.getScoreWeight(listpieces)elif  itemafter.player == constants.player2Color:
scoreplayer2 += itemafter.getScoreWeight(listpieces)
 
 
  #print("得分:"+item.imagskey +", "+str(len(moveAfterListpieces))+","+str(i)+","+str(j)+"," +str(scoreplayer1) +"  ,  "+ str(scoreplayer2) )
  #print(str(deepstep))
  #如果得子 判斷對(duì)面是否可以殺過(guò)來(lái),如果又被殺,而且子力評(píng)分低,則不干
  arrkill = listPiecestoArr(listpieces)
 
 
  if scoreplayer2 > scoreplayer1 :for itemkill in listpieces:
if itemkill.player == constants.player1Color and itemkill.canmove(arrkill, i, j):
 scoreplayer2=scoreplayer1
 
 
  if deepstep > 0 :nextplayer = constants.player1Color if player == constants.player2Color else constants.player2Colornextpiecesbest= movedeep(listpieces, deepstep -1, nextplayer)listMoveEnabel.append([item, i, j, nextpiecesbest[3], nextpiecesbest[4], nextpiecesbest[5]])
  else:#print(str(len(listpieces)))#print("得分:" + item.imagskey + ", " + str(len(listpieces)) + "," + str(move_x) + "," + str(move_y) + "," + str(i) + "  ,  " + str(j))if player == constants.player2Color:
listMoveEnabel.append([item, i, j, scoreplayer1, scoreplayer2, scoreplayer1 - scoreplayer2])else:
listMoveEnabel.append([item, i, j, scoreplayer1, scoreplayer2, scoreplayer2 - scoreplayer1])
  #print("得分:"+str(scoreplayer1))
  item.x = move_x
  item.y = move_y
  if piecesremove != None:listpieces.append(piecesremove)
 
 
 list_scorepalyer1 = sorted(listMoveEnabel, key=lambda tm: tm[5], reverse=True)
 piecesbest = list_scorepalyer1[0]
 if deepstep ==1 :
  print(list_scorepalyer1)
 return piecesbest

3.5 Button.py按鈕定義

#設(shè)置按鈕
import pygame
class Button():
 def __init__(self, screen, msg, left,top):  # msg為要在按鈕中顯示的文本
  """初始化按鈕的屬性"""
  self.screen = screen
  self.screen_rect = screen.get_rect()
 
 
  self.width, self.height = 150, 50  # 這種賦值方式很不錯(cuò)
  self.button_color = (72, 61, 139)  # 設(shè)置按鈕的rect對(duì)象顏色為深藍(lán)
  self.text_color = (255, 255, 255)  # 設(shè)置文本的顏色為白色
  pygame.font.init()
  self.font = pygame.font.SysFont('kaiti', 20)  # 設(shè)置文本為默認(rèn)字體,字號(hào)為40
 
 
  self.rect = pygame.Rect(0, 0, self.width, self.height)
  #self.rect.center = self.screen_rect.center  # 創(chuàng)建按鈕的rect對(duì)象,并使其居中
  self.left = left
  self.top = top
 
 
  self.deal_msg(msg)  # 渲染圖像
 
 
 def deal_msg(self, msg):
  """將msg渲染為圖像,并將其在按鈕上居中"""
  self.msg_img = self.font.render(msg, True, self.text_color, self.button_color)  # render將存儲(chǔ)在msg的文本轉(zhuǎn)換為圖像
  self.msg_img_rect = self.msg_img.get_rect()  # 根據(jù)文本圖像創(chuàng)建一個(gè)rect
  self.msg_img_rect.center = self.rect.center  # 將該rect的center屬性設(shè)置為按鈕的center屬性
 
 
 def draw_button(self):
  #self.screen.fill(self.button_color, self.rect)  # 填充顏色
  self.screen.blit(self.msg_img, (self.left,self.top))  # 將該圖像繪制到屏幕
 
 
 def is_click(self):
  point_x, point_y = pygame.mouse.get_pos()
  x = self.left
  y = self.top
  w, h = self.msg_img.get_size()
 
 
  in_x = x < point_x < x + w
  in_y = y < point_y < y + h
  return in_x and in_y

4.游戲效果

總結(jié)

好啦!文章就寫(xiě)到這里了哈,想入門(mén)象棋的可以先試著自己研究下,上面的教程也有說(shuō)走法、行棋的規(guī)則,然后后面就是實(shí)戰(zhàn),自己動(dòng)手跟電腦來(lái)一場(chǎng)對(duì)決吧~

以上就是Python實(shí)現(xiàn)人機(jī)中國(guó)象棋游戲的詳細(xì)內(nèi)容,更多關(guān)于Python中國(guó)象棋的資料請(qǐng)關(guān)注本站其它相關(guān)文章!

海外服務(wù)器租用

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問(wèn)服務(wù)

1對(duì)1客戶咨詢顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部