棋盘游戏库
pykhet的Python项目详细描述
简介
用python实现的khet棋盘游戏逻辑和结构。同时也展示了基于对抗性搜索的算法。
frompykhet.components.typesimportTeamColorfrompykhet.games.game_typesimportClassicGameimportrandomfrompykhet.solvers.minmaximportMinmaxSolver# Create a game with classic piece placementgame=ClassicGame()# Get all valid silver movessilver_moves=game.get_available_moves(TeamColor.silver)# Randomly Play Onegame.apply_move(random.choice(silver_moves))# Finish the turn by applying the lasergame.apply_laser(TeamColor.silver)# Use adversarial search to pick a movesolver=MinmaxSolver()move=solver.get_move(game,TeamColor.red)game.apply_move(move)game.apply_laser(TeamColor.red)
序列化
有足够的支持将对象的状态序列化为字典。有助于作为json轻松存储。
frompykhet.components.typesimportTeamColor,Piecefrompykhet.games.game_typesimportClassicGameimportrandomfrompykhet.solvers.minmaximportMinmaxSolver# Create a game with classic piece placementgame=ClassicGame()# Serialize the board (list of serialized piece positions, orientations, and colors)squares=game.to_serialized_squares()# Deserialize the boardGame.from_serialized_squares(squares)# Serialize a piecesp1=Piece(PieceType.scarab,TeamColor.silver,Orientation.down).to_dictionary()# Deserialize a piecesame_piece=Piece.from_dictionary(p1)