本文整理汇总了Python中chess.Board方法的典型用法代码示例。如果您正苦于以下问题:Python chess.Board方法的具体用法?Python chess.Board怎么用?Python chess.Board使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chess
的用法示例。
在下文中一共展示了chess.Board方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_long_mating_pv
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def test_long_mating_pv(self):
""" A long pv that ends the game should not be truncated """
board = chess.Board('1Q3bk1/5p2/2p3p1/1p1bN2p/4n2P/8/r5PK/8 b - - 1 34') # noqa E501
line = [chess.Move.from_uci('g8g7'), chess.Move.from_uci('e5f7'),
chess.Move.from_uci('d5f7'), chess.Move.from_uci('b8e5'),
chess.Move.from_uci('e4f6'), chess.Move.from_uci('h2h3'),
chess.Move.from_uci('b5b4'), chess.Move.from_uci('g2g4'),
chess.Move.from_uci('f8d6'), chess.Move.from_uci('e5d6'),
chess.Move.from_uci('h5g4'), chess.Move.from_uci('h3g3'),
chess.Move.from_uci('f6e4'), chess.Move.from_uci('g3f4'),
chess.Move.from_uci('e4d6'), chess.Move.from_uci('f4e5'),
chess.Move.from_uci('b4b3'), chess.Move.from_uci('e5d6'),
chess.Move.from_uci('b3b2'), chess.Move.from_uci('h4h5'),
chess.Move.from_uci('g6h5'), chess.Move.from_uci('d6d7'),
chess.Move.from_uci('b2b1q'), chess.Move.from_uci('d7c7'),
chess.Move.from_uci('b1b4'), chess.Move.from_uci('c7c6'),
chess.Move.from_uci('a2c2'), chess.Move.from_uci('c6d7'),
chess.Move.from_uci('b4b8'), chess.Move.from_uci('d7e7'),
chess.Move.from_uci('b8c7')]
result = annotator.truncate_pv(board, line)
self.assertEqual(result, line)
示例2: test_long_non_mating_pv
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def test_long_non_mating_pv(self):
"""
A long pv that does not end the game should be truncated to 10 moves
"""
board = chess.Board('1Q3bk1/5p2/2p3p1/1p1bN2p/4n2P/8/r5PK/8 b - - 1 34') # noqa E501
line = [chess.Move.from_uci('g8g7'), chess.Move.from_uci('e5f7'),
chess.Move.from_uci('d5f7'), chess.Move.from_uci('b8e5'),
chess.Move.from_uci('e4f6'), chess.Move.from_uci('h2h3'),
chess.Move.from_uci('b5b4'), chess.Move.from_uci('g2g4'),
chess.Move.from_uci('f8d6'), chess.Move.from_uci('e5d6'),
chess.Move.from_uci('h5g4'), chess.Move.from_uci('h3g3'),
chess.Move.from_uci('f6e4'), chess.Move.from_uci('g3f4'),
chess.Move.from_uci('e4d6'), chess.Move.from_uci('f4e5'),
chess.Move.from_uci('b4b3'), chess.Move.from_uci('e5d6'),
chess.Move.from_uci('b3b2'), chess.Move.from_uci('h4h5'),
chess.Move.from_uci('g6h5'), chess.Move.from_uci('d6d7'),
chess.Move.from_uci('b2b1q'), chess.Move.from_uci('d7c7'),
chess.Move.from_uci('b1b4'), chess.Move.from_uci('c7c6'),
chess.Move.from_uci('a2c2'), chess.Move.from_uci('c6d7'),
chess.Move.from_uci('b4b8'), chess.Move.from_uci('d7e7')]
target = line[:annotator.SHORT_PV_LEN]
result = annotator.truncate_pv(board, line)
self.assertEqual(len(result), annotator.SHORT_PV_LEN)
self.assertEqual(result, target)
示例3: test_raises_assertionerror
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def test_raises_assertionerror(self):
""" A line with an illegal move should raise an AssertionError """
board = chess.Board('1Q3bk1/5p2/2p3p1/1p1bN2p/4n2P/8/r5PK/8 b - - 1 34') # noqa E501
line = [chess.Move.from_uci('g8g7'), chess.Move.from_uci('e5f7'),
chess.Move.from_uci('d5f7'), chess.Move.from_uci('b8e5'),
chess.Move.from_uci('e4f6'), chess.Move.from_uci('h2h3'),
chess.Move.from_uci('b5b4'), chess.Move.from_uci('g2g4'),
chess.Move.from_uci('f8d6'), chess.Move.from_uci('e5d6'),
chess.Move.from_uci('h5g4'), chess.Move.from_uci('h3g3'),
chess.Move.from_uci('f6e4'), chess.Move.from_uci('g3f4'),
chess.Move.from_uci('e4d6'), chess.Move.from_uci('f4e5'),
chess.Move.from_uci('b4b3'), chess.Move.from_uci('e5d6'),
chess.Move.from_uci('b3b2'), chess.Move.from_uci('h4h5'),
chess.Move.from_uci('g6h5'), chess.Move.from_uci('d6c8'),
chess.Move.from_uci('b2b1q'), chess.Move.from_uci('d7c7'),
chess.Move.from_uci('b1b4'), chess.Move.from_uci('c7c6'),
chess.Move.from_uci('a2c2'), chess.Move.from_uci('c6d7'),
chess.Move.from_uci('b4b8'), chess.Move.from_uci('d7e7'),
chess.Move.from_uci('b8c7')]
self.assertRaises(AssertionError, annotator.truncate_pv, board, line)
示例4: test_eco_json
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def test_eco_json(self):
ecopath = os.path.join(
os.path.dirname(__file__), '../annotator/eco/eco.json'
)
with open(ecopath, 'r') as ecofile:
ecodata = json.load(ecofile)
for row in ecodata:
fen = "{} {}".format(row["f"], '- 0 1')
chess.Board(fen=fen)
classification = annotator.classify_fen(row["f"], ecodata)
assert classification["code"] == row["c"]
assert classification["desc"] == row["n"]
assert classification["path"] == row["m"]
示例5: process_console_command
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def process_console_command(self, raw):
cmd = raw.lower()
try:
# Here starts the simulation of a dgt-board!
# Let the user send events like the board would do
if cmd.startswith('fen:'):
fen = raw.split(':')[1].strip()
# dgt board only sends the basic fen => be sure it's same no matter what fen the user entered
fen = fen.split(' ')[0]
bit_board = chess.Board() # valid the fen
bit_board.set_board_fen(fen)
Observable.fire(Event.KEYBOARD_FEN(fen=fen))
# end simulation code
elif cmd.startswith('go'):
if 'last_dgt_move_msg' in self.shared:
fen = self.shared['last_dgt_move_msg']['fen'].split(' ')[0]
Observable.fire(Event.KEYBOARD_FEN(fen=fen))
else:
# Event.KEYBOARD_MOVE tranfers "move" to "fen" and then continues with "Message.DGT_FEN"
move = chess.Move.from_uci(cmd)
Observable.fire(Event.KEYBOARD_MOVE(move=move))
except (ValueError, IndexError):
logging.warning('Invalid user input [%s]', raw)
示例6: book
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def book(self, bookreader, game_copy: chess.Board):
"""Get a BookMove or None from game position."""
try:
choice = bookreader.weighted_choice(game_copy, self.excludemoves)
except IndexError:
return None
book_move = choice.move()
self.add(book_move)
game_copy.push(book_move)
try:
choice = bookreader.weighted_choice(game_copy)
book_ponder = choice.move()
except IndexError:
book_ponder = None
return chess.uci.BestMove(book_move, book_ponder)
示例7: is_illegal_castle
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [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
示例8: pawn_capture_moves_on
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def pawn_capture_moves_on(board: chess.Board) -> List[chess.Move]:
"""Generates all pawn captures on `board`, even if there is no piece to capture. All promotion moves are included."""
pawn_capture_moves = []
no_opponents_board = without_opponent_pieces(board)
for pawn_square in board.pieces(chess.PAWN, board.turn):
for attacked_square in board.attacks(pawn_square):
# skip this square if one of our own pieces are on the square
if no_opponents_board.piece_at(attacked_square):
continue
pawn_capture_moves.append(chess.Move(pawn_square, attacked_square))
# add in promotion moves
if attacked_square in chess.SquareSet(chess.BB_BACKRANKS):
for piece_type in chess.PIECE_TYPES[1:-1]:
pawn_capture_moves.append(chess.Move(pawn_square, attacked_square, promotion=piece_type))
return pawn_capture_moves
示例9: default
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [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)
示例10: truth_board_before_move
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [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 Board [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 Board [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: start
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def start(self, message: Dict[str, str], bot_handler: Any) -> None:
"""Starts a game with another user, with the current user as white.
Replies to the bot handler.
Parameters:
- message: The Zulip Bots message object.
- bot_handler: The Zulip Bots bot handler object.
"""
new_board = chess.Board()
bot_handler.send_reply(
message,
make_start_reponse(new_board)
)
# `bot_handler`'s `storage` only accepts `str` values.
bot_handler.storage.put('is_with_computer', str(False))
bot_handler.storage.put('last_fen', new_board.fen())
示例14: make_loss_response
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def make_loss_response(board: chess.Board, reason: str) -> str:
"""Makes a response string for a loss (or win).
Parameters:
- board: The board object at the end of the game.
- reason: The reason for the loss, in the form of a predicate, e.g.,
'was checkmated'.
Returns: The loss response string.
"""
return (
'*{}* {}. **{}** wins!\n\n'
'{}'
).format(
'White' if board.turn else 'Black',
reason,
'Black' if board.turn else 'White',
make_str(board, board.turn)
)
示例15: make_not_legal_response
# 需要导入模块: import chess [as 别名]
# 或者: from chess import Board [as 别名]
def make_not_legal_response(board: chess.Board, move_san: str) -> str:
"""Makes a response string for a not-legal move.
Parameters:
- board: The board object before the move.
- move_san: The SAN of the not-legal move.
Returns: The not-legal-move response string.
"""
return (
'Sorry, the move *{}* isn\'t legal.\n\n'
'{}'
'\n\n\n'
'{}'
).format(
move_san,
make_str(board, board.turn),
make_footer()
)