本文整理匯總了Python中chess.board.Board.get_king方法的典型用法代碼示例。如果您正苦於以下問題:Python Board.get_king方法的具體用法?Python Board.get_king怎麽用?Python Board.get_king使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類chess.board.Board
的用法示例。
在下文中一共展示了Board.get_king方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from chess.board import Board [as 別名]
# 或者: from chess.board.Board import get_king [as 別名]
#.........這裏部分代碼省略.........
if figure.move_to(target[0], target[1], 8, 8):
# Castling
if figure.get_type() == figures.figure.KING and figure.isCastling():
pos = figure.get_position()
# Check if king moved left or right
if pos[0] > 4:
rook = self._board.get_figure(7, pos[1])
rook.move_to(
target[0] - 1,
pos[1],
8,
8,
False)
logging.info(
"Move rook figure on %s:%s after castling", 7, pos[1])
else:
rook = self._board.get_figure(0, pos[1])
rook.move_to(
target[0] + 1,
pos[1],
8,
8,
False)
logging.info(
"Move rook figure on %s:%s after castling", 0, pos[1])
def get_state(self):
"""
Get current state of game.
"""
state = []
# Loop through whole game board
# If figure is found in position, then print figure
# else print empty space
logging.info("Return chess game board state.")
for y_index in range(0, 8):
for x_index in range(0, 8):
fig = self._board.get_figure(x_index, y_index)
if fig:
logging.debug(
"Figure found on %s:%s with color %s",
x_index,
y_index,
fig.get_owner())
state.append(self._get_figure_mark(fig))
else:
state.append('')
return state
def _get_figure_mark(self, figure):
"""
Return mark that will be print in application output.
"""
mark = ''
if figure.get_owner() == figures.figure.BLACK:
mark += 'b'
else:
mark += 'w'
fig_type = figure.get_type()
if fig_type == figures.figure.PAWN:
mark += 'p'
elif fig_type == figures.figure.KNIGHT:
mark += 'kn'
elif fig_type == figures.figure.ROOK:
mark += 'r'
elif fig_type == figures.figure.BISHOP:
mark += 'b'
elif fig_type == figures.figure.QUEEN:
mark += 'q'
elif fig_type == figures.figure.KING:
mark += 'ki'
return mark
def get_condition(self):
"""
Check if current player is in check.
"""
king = self._board.get_king(self._current_player)
if king:
if king.isCheck(8, 8):
# Check mate
if self.get_moves(king):
return Conditions.check
else:
return Conditions.checkMate
return Conditions.play
def set_player(self, player):
"""
Return current player.
"""
if player.lower() == 'black' or player.lower() == 'b':
self._current_player = figures.figure.BLACK
elif player.lower == 'white' or player.lower() == 'w':
self._current_player = figures.figure.WHITE
logging.debug("Player %s is on move", self._current_player)