本文整理汇总了Python中chess.Move方法的典型用法代码示例。如果您正苦于以下问题:Python chess.Move方法的具体用法?Python chess.Move怎么用?Python chess.Move使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chess
的用法示例。
在下文中一共展示了chess.Move方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def find_move(self, board, debug=False):
# Should we flip the board to make sure it always gets it from white?
vec_board = board_to_vec(
board if board.turn == chess.WHITE else board.mirror())
vec_board = tensor_sketch(vec_board, self.sketches)
probs = self.move_clf.predict_proba([vec_board])[0]
score = self.score_clf.predict([vec_board])[0]
for n, (mp, from_to) in enumerate(sorted((-p, ft) for ft, p in enumerate(probs))):
to_square, from_square = divmod(from_to, 64)
if board.turn == chess.BLACK:
from_square = chess.square_mirror(from_square)
to_square = chess.square_mirror(to_square)
move = chess.Move(from_square, to_square)
if move in board.legal_moves:
print(f'Choice move {n}, p={-mp}')
return move, score
示例2: choose_sense
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def choose_sense(self, sense_actions: List[Square], move_actions: List[chess.Move], seconds_left: float) -> \
Optional[Square]:
"""
The method to implement your sensing strategy. The chosen sensing action should be returned from this function.
I.e. the value returned is the square at the center of the 3x3 sensing area you want to sense. The returned
square must be one of the squares in the `sense_actions` parameter.
You can pass instead of sensing by returning `None` from this function.
Move actions are provided through `move_actions` in case you want to sense based on a potential move.
Called after :meth:`handle_opponent_move_result()`.
Example implementation: ::
def choose_sense(self, sense_actions: List[Square], move_actions: List[chess.Move], seconds_left: float) -> Square:
return random.choice(sense_actions)
:param sense_actions: A :class:`list` containing the valid squares to sense over.
:param move_actions: A :class:`list` containing the valid moves that can be returned in :meth:`choose_move()`.
:param seconds_left: The time in seconds you have left to use in the game.
:return: a :class:`Square` that is the center of the 3x3 sensing area you want to get information about.
"""
pass
示例3: choose_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def choose_move(self, move_actions: List[chess.Move], seconds_left: float) -> Optional[chess.Move]:
"""
The method to implement your movement strategy. The chosen movement action should be returned from this function.
I.e. the value returned is the move to make. The returned move must be one of the moves in the `move_actions`
parameter.
The pass move is legal, and is executed by returning `None` from this method.
Called after :meth:`handle_sense_result()`.
Example implementation: ::
def choose_move(self, move_actions: List[chess.Move], seconds_left: float) -> Optional[chess.Move]:
return random.choice(move_actions)
:param move_actions: A `list` containing the valid :class:`chess.Move` you can choose.
:param seconds_left: The time in seconds you have left to use in the game.
:return: The :class:`chess.Move` to make.
"""
pass
示例4: play_sense
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def play_sense(game: Game, player: Player, sense_actions: List[Square], move_actions: List[chess.Move]):
"""
Runs the sense phase for `player` in `game`. Does the following sequentially:
#. Get the sensing action using :meth:`Player.choose_sense`.
#. Apply the sense action using :meth:`Game.sense`.
#. Give the result of the sense action to player using :meth:`Player.handle_sense_result`.
:param game: The :class:`Game` that `player` is playing in.
:param player: The :class:`Player` whose turn it is.
:param sense_actions: The possible sense actions for `player`.
:param move_actions: The possible move actions for `player`.
"""
sense = player.choose_sense(sense_actions, move_actions, game.get_seconds_left())
sense_result = game.sense(sense)
player.handle_sense_result(sense_result)
示例5: is_illegal_castle
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def is_illegal_castle(board: chess.Board, move: chess.Move) -> bool:
if not board.is_castling(move):
return False
# illegal without kingside rights
if board.is_kingside_castling(move) and not board.has_kingside_castling_rights(board.turn):
return True
# illegal without queenside rights
if board.is_queenside_castling(move) and not board.has_queenside_castling_rights(board.turn):
return True
# illegal if any pieces are between king & rook
rook_square = chess.square(7 if board.is_kingside_castling(move) else 0, chess.square_rank(move.from_square))
between_squares = chess.SquareSet(chess.between(move.from_square, rook_square))
if any(map(lambda s: board.piece_at(s), between_squares)):
return True
# its legal
return False
示例6: default
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def default(self, o):
if isinstance(o, chess.Piece):
return {
'type': 'Piece',
'value': o.symbol(),
}
elif isinstance(o, chess.Move):
return {
'type': 'Move',
'value': o.uci(),
}
elif isinstance(o, chess.Board):
return {
'type': 'Board',
'value': o.fen(),
}
elif isinstance(o, WinReason):
return {
'type': 'WinReason',
'value': o.name,
}
return super().default(o)
示例7: requested_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def requested_move(self, turn: Turn) -> Optional[chess.Move]:
"""
Get the requested move action on the given turn.
Examples:
>>> history.requested_move(Turn(WHITE, 0))
Move(E2, E4)
>>> history.requested_move(Turn(BLACK, 0))
Move(E7, E5)
>>> history.requested_move(Turn(WHITE, 1))
None
:param turn: The :class:`Turn` in question.
:return: If the player requested to move, then the requested move action as a :class:`chess.Move`, otherwise
`None` if the player requested to pass.
"""
self._validate_turn(turn, self._requested_moves)
return self._requested_moves[turn.color][turn.turn_number]
示例8: taken_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def taken_move(self, turn: Turn) -> Optional[chess.Move]:
"""
Get the executed move action on the given turn. This may be different than the requested move, as the requested
move may not be legal.
Examples:
>>> history.requested_move(Turn(WHITE, 0))
Move(E2, D3)
>>> history.taken_move(Turn(WHITE, 0))
None
>>> history.requested_move(Turn(WHITE, 1))
Move(E2, E4)
>>> history.taken_move(Turn(WHITE, 1))
Move(E2, E3)
:param turn: The :class:`Turn` in question.
:return: `None` if the player requested to pass or made an illegal move, the executed move action as a
:class:`chess.Move` otherwise.
"""
self._validate_turn(turn, self._taken_moves)
return self._taken_moves[turn.color][turn.turn_number]
示例9: truth_fen_before_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def truth_fen_before_move(self, turn: Turn) -> str:
"""
Get the truth state of the board as a fen string before the move was executed on the given turn. Use
:meth:`truth_board_before_move` if you want the truth board as a :class:`chess.Board` object.
Examples:
>>> history.truth_fen_before_move(Turn(WHITE, 0))
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -"
>>> history.taken_move(Turn(WHITE, 0))
Move(E2, E4)
>>> history.truth_fen_before_move(Turn(BLACK, 0))
"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -"
:param turn: The :class:`Turn` in question.
:return: The fen of the truth board.
"""
self._validate_turn(turn, self._fens_before_move)
return self._fens_before_move[turn.color][turn.turn_number]
示例10: truth_board_before_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def truth_board_before_move(self, turn: Turn) -> chess.Board:
"""
Get the truth state of the board as a :class:`chess.Board` before the move was executed on the given turn. Use
:meth:`truth_fen_before_move` if you want the truth board as a fen string.
Examples:
>>> history.truth_board_before_move(Turn(WHITE, 0))
Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -")
>>> history.taken_move(Turn(WHITE, 0))
Move(E2, E4)
>>> history.truth_fen_before_move(Turn(BLACK, 0))
Board("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -")
:param turn: The :class:`Turn` in question.
:return: A :class:`chess.Board` object.
"""
self._validate_turn(turn, self._fens_before_move)
board = chess.Board(self._fens_before_move[turn.color][turn.turn_number])
board.turn = turn.color
return board
示例11: truth_fen_after_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def truth_fen_after_move(self, turn: Turn) -> str:
"""
Get the truth state of the board as a fen string after the move was executed on the given turn. Use
:meth:`truth_board_after_move` if you want the truth board as a :class:`chess.Board` object.
Examples:
>>> history.truth_fen_before_move(Turn(WHITE, 0))
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -"
>>> history.taken_move(Turn(WHITE, 0))
Move(E2, E4)
>>> history.truth_fen_after_move(Turn(WHITE, 0))
"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -"
:param turn: The :class:`Turn` in question.
:return: The fen of the truth board.
"""
self._validate_turn(turn, self._fens_after_move)
return self._fens_after_move[turn.color][turn.turn_number]
示例12: truth_board_after_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def truth_board_after_move(self, turn: Turn) -> chess.Board:
"""
Get the truth state of the board as a :class:`chess.Board` after the move was executed on the given turn. Use
:meth:`truth_fen_after_move` if you want the truth board as a fen string.
Examples:
>>> history.truth_board_before_move(Turn(WHITE, 0))
Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -")
>>> history.taken_move(Turn(WHITE, 0))
Move(E2, E4)
>>> history.truth_fen_after_move(Turn(WHITE, 0))
Board("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -")
:param turn: The :class:`Turn` in question.
:return: A :class:`chess.Board` object.
"""
self._validate_turn(turn, self._fens_after_move)
board = chess.Board(self._fens_after_move[turn.color][turn.turn_number])
board.turn = turn.color
return board
示例13: collect
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def collect(self, get_turn_data_fn: Callable[[Turn], T], turns: Iterable[Turn]) -> Iterable[T]:
"""
Collect data from multiple turns using any of :meth:`sense`, :meth:`sense_result`, :meth:`requested_move`,
:meth:`taken_move`, :meth:`capture_square`, :meth:`move_result`, :meth:`truth_fen_before_move`,
:meth:`truth_board_before_move`, :meth:`truth_fen_after_move`, or :meth:`truth_board_after_move`.
Examples:
>>> history.collect(history.sense, [Turn(WHITE, 0), Turn(BLACK, 0)])
[E7, E2]
>>> history.collect(history.requested_move, history.turns(WHITE))
[Move(E2, E4), Move(D1, H5), ...]
:param get_turn_data_fn: One of the getter functions of the history object.
:param turns: The turns in question.
:return: A list of the data, where each element is the value of the getter function on the corresponding turn.
"""
if get_turn_data_fn not in [self.sense, self.sense_result, self.requested_move, self.taken_move,
self.capture_square, self.move_result, self.truth_board_before_move,
self.truth_board_after_move, self.truth_fen_before_move, self.truth_fen_after_move]:
raise ValueError('get_turn_data_fn must be one of the history getter functions')
for turn in turns:
yield get_turn_data_fn(turn)
示例14: update_text_box
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def update_text_box(self, window, msg, is_hide):
""" Update text elements """
best_move = None
msg_str = str(msg)
if not 'bestmove ' in msg_str:
if 'info_all' in msg_str:
info_all = ' '.join(msg_str.split()[0:-1]).strip()
msg_line = '{}\n'.format(info_all)
window.FindElement('search_info_all_k').Update(
'' if is_hide else msg_line)
else:
# Best move can be None because engine dies
try:
best_move = chess.Move.from_uci(msg.split()[1])
except Exception:
logging.exception('Engine sent {}.'.format(best_move))
sg.Popup('Engine error, it sent a {} bestmove.\n'.format(
best_move) + 'Back to Neutral mode, it is better to '
'change engine {}.'.format(
self.opp_id_name), icon=ico_path[platform]['pecg'],
title=BOX_TITLE)
return best_move
示例15: mirror_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Move [as 别名]
def mirror_move(move):
return chess.Move(chess.square_mirror(move.from_square),
chess.square_mirror(move.to_square),
move.promotion)