本文整理汇总了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)