python_pygame_alpha-beta剪枝演算法_玩中國象棋
import time
import pygame
import ChinaChess.constants
from ChinaChess import constants, pieces, computer
import ChinaChess.computer
import ChinaChess.my_game as mg
'''
此檔案為視覺化中國象棋執行入口
'''
class MainGame():
window = None
Start_X = ChinaChess.constants.Start_X
Start_Y = ChinaChess.constants.Start_Y
Line_Span = ChinaChess.constants.Line_Span
Max_X = Start_X + 8 * Line_Span
Max_Y = Start_Y + 9 * Line_Span
from_x = 0
from_y = 0
to_x = 0
to_y = 0
clickx = -1
clicky = -1
mgInit = mg.my_game()
player1Color = constants.player1Color
player2Color = constants.player2Color
Putdownflag = player1Color
piecesSelected = None
piecesList = []
def start_game(self):
MainGame.window = pygame.display.set_mode([constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT])
pygame.display.set_caption(" 中國象棋 ")
# 把所有棋子擺好
self.piecesInit()
while True:
time.sleep(0.1)
# 獲取事件
MainGame.window.fill(constants.BG_COLOR)
self.drawChessboard()
# 遍歷所有棋子,顯示所有棋子
self.piecesDisplay()
# 判斷遊戲勝利
self.VictoryOrDefeat()
# 輪到電腦了
self.Computerplay()
# 獲取所有的事件
self.getEvent()
pygame.display.update()
pygame.display.flip()
def drawChessboard(self):
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:
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):
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 - 外匯跟單gendan5.comMainGame.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(" 有效點: x=" + str(click_x) + " y=" + str(click_y))
# 有效點選點
self.from_x = MainGame.clickx
self.from_y = MainGame.clicky
self.to_x = click_x
self.to_y = click_y
print(self.from_x)
print(self.from_y)
MainGame.clickx = click_x
MainGame.clicky = click_y
self.PutdownPieces(MainGame.player1Color, click_x, click_y)
else:
print("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("MainGame.piecesSelected")
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, self.from_x, self.from_y, self.to_x, self.to_y,
self.mgInit)
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):
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()
if __name__ == '__main__':
MainGame().start_game()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2763186/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python 中國象棋原始碼 V1Python原始碼
- [開源] 一個分散式中國象棋 Alpha zero分散式
- 模型剪枝:剪枝粒度、剪枝標準、剪枝時機、剪枝頻率模型
- Luogu P5005 中國象棋 - 擺上馬 / Luogu P8756 國際象棋 題解 [ 藍 ] [ 狀壓 dp ] [ 位運算 ]
- 模型壓縮-剪枝演算法詳解模型演算法
- pytorch中中的模型剪枝方法PyTorch模型
- turtle繪製國際象棋棋盤
- 【180629】C++版智商超高的中國象棋遊戲原始碼C++遊戲原始碼
- 親自動手實現Python+pygame中國象棋遊戲PythonGAM遊戲
- Python3+pygame中國象棋 程式碼完整 非常好 有效果演示PythonGAM
- 實踐Pytorch中的模型剪枝方法PyTorch模型
- 怎樣解題|題3.2.14:填充國際象棋棋盤
- CSS3象棋效果CSSS3
- 將傳統遊戲融入虛擬現實,網友自制《中國象棋VR》創意爆棚遊戲VR
- Alpha-Beta 剪枝
- 怎樣解題|題3.3.28:國際象棋棋盤上的車
- 活字印刷 回溯剪枝
- LeetCode 1812[判斷國際象棋棋盤中一個格子的顏色]LeetCode
- DFS剪枝最佳化策略
- 空間剪枝最佳化
- 當外國人玩《山海旅人》,他們怎麼理解中國文化?
- 基於pytorch實現模型剪枝PyTorch模型
- 英偉達玩轉剪枝、蒸餾:把Llama 3.1 8B引數減半,效能同尺寸更強
- 666行的象棋程式下得不錯啊
- G胖的中國之行,被網友們玩壞了
- 《中國式網遊》新品節試玩demo正式開啟!
- P1433 吃乳酪 (dfs+剪枝)
- [leetcode 87 擾亂字串] [剪枝搜尋]LeetCode字串
- 模型小型化:蒸餾法、剪枝模型
- 【每日一題】1812. 判斷國際象棋棋盤中一個格子的顏色每日一題
- C語言+圖形程式設計——自制象棋C語言程式設計
- 基於大資料的人工智慧象棋大資料人工智慧
- 陪玩app原始碼,加密演算法中金鑰生成和讀取一覽APP原始碼加密演算法
- 陪玩平臺原始碼中的排序演算法,插入排序的實現原始碼排序演算法
- 這個雙節,中國的手遊玩家都在玩什麼
- YouGov:1/4的中國玩家使用PC玩視訊遊戲Go遊戲
- 遊戲陪玩系統原始碼中不同排序演算法的實現方式遊戲原始碼排序演算法
- 2024.3.15 Alpha-Beta 剪枝+CF div2