本文整理汇总了Python中piece.Piece.opposite_color方法的典型用法代码示例。如果您正苦于以下问题:Python Piece.opposite_color方法的具体用法?Python Piece.opposite_color怎么用?Python Piece.opposite_color使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类piece.Piece
的用法示例。
在下文中一共展示了Piece.opposite_color方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_theoretical_ep_right
# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import opposite_color [as 别名]
def get_theoretical_ep_right(self, x):
"""Checks if a player could have an ep-move in theory from
looking just at the piece positions.
:param file:
The file to check as a letter between `"a"` and `"h"`.
:return:
A boolean indicating whether the player could theoretically
have that en-passant move.
"""
if x < 0 or x > 7:
raise ValueError(x)
'''
3 states of en-passant
p. pP ..
.. .. .p
.P .. ..
'''
# Check there is a pawn on the right rank for e.p.
y = 3 if self.fen._to_move == WHITE else 4
x88 = X88.from_x_and_y(x, y)
piece = self._pieces[x88]
if not piece:
return False
# If the square is not an opposite colored pawn then its not possible.
ocolor = Piece.opposite_color(self.fen._to_move)
if not Piece.is_klass_and_color(piece, PAWN, ocolor):
return False
# If the square below the pawn is not empty then it not possible.
y = 2 if self.fen.turn == WHITE else 5
x88 = X88.from_x_and_y(x, y)
if self[x88]:
return False
# If there is not pawn of opposite color on a neighboring file then its not possible.
xs = [_x for _x in range(8) if _x>=0 and _x<8 and abs(x-_x) == 1]
for _x in xs:
x88 = X88.from_x_and_y(_x, y)
piece = self._pieces[x88]
if Piece.is_klass_and_color(piece, PAWN, Piece.opposite_color(self.fen._to_move)):
return True
# Else its just not possible.
return False
示例2: __init__
# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import opposite_color [as 别名]
def __init__(self, position, move):
resulting_position = position.copy().make_move(move)
captured = position._pieces[move.target._x88]
piece = position._pieces[move.source._x88]
ocolor = Piece.opposite_color(position.fen._to_move)
# Pawn moves.
enpassant = False
if Piece.klass(piece) == PAWN:
# En-passant.
if move.target.file != move.source.file and not captured:
enpassant = True
captured = Piece.from_klass_and_color(PAWN, ocolor)
# Castling.
# TODO: Support Chess960.
# TODO: Validate the castling move.
if Piece.klass(piece) == KING:
self.is_king_side_castle = move.target.x - move.source.x == 2
self.is_queen_side_castle = move.target.x - move.source.x == -2
else:
self.is_king_side_castle = self.is_queen_side_castle = False
# Checks.
self.is_check = resulting_position.is_check()
self.is_checkmate = resulting_position.is_checkmate()
self.move = move
self.piece = piece
self.captured = captured
self.is_enpassant = enpassant
self._set_text(position)
示例3: is_king_attacked
# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import opposite_color [as 别名]
def is_king_attacked(self, color):
""":return: Whether the king of the given color is attacked.
:param color: `"w"` or `"b"`.
"""
square = self.get_king(color)
if square:
return self.is_attacked(Piece.opposite_color(color), square)
# XXX No king?
else:
return False
示例4: get_pseudo_legal_moves
# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import opposite_color [as 别名]
def get_pseudo_legal_moves(self, source=None):
""":yield: Pseudo legal moves in the current position.
:param source: The source square to limit moves or None for
all possible moves.
"""
tomove = self.fen._to_move
for x88 in [ x88 for x88 in Square._x88_squares.keys()
if self._pieces[x88]
and Piece.color(self._pieces[x88]) == tomove
and (source is None or x88 == source._x88)]:
piece = self._pieces[x88]
klass = Piece.klass(piece)
# pawn moves
if klass == PAWN:
single, double, capleft, capright = X88.PAWN_OFFSETS[tomove]
# Single square ahead. Do not capture.
offset = x88 + single
if not self._pieces[offset]:
# Promotion.
if X88.is_backrank(offset, tomove):
for promote_to in Piece.promote_to:
yield Move.from_x88(x88, offset, promote_to)
else:
yield Move.from_x88(x88, offset)
# Two squares ahead. Do not capture.
if X88.is_secondrank(x88, tomove):
offset = x88 + double
if not self._pieces[offset]:
yield Move.from_x88(x88, offset)
# Pawn captures.
for cap in [capleft, capright]:
offset = x88 + cap
if offset & X88.X88:
continue
target = self._pieces[offset]
if target and Piece.color(target) != tomove:
# Promotion.
if X88.is_backrank(offset, tomove):
for promote_to in Piece.promote_to:
yield Move.from_x88(x88, offset, promote_to)
else:
yield Move.from_x88(x88, offset)
# En-passant.
elif not target and offset == self.fen._ep:
yield Move.from_x88(target, self.fen._ep)
#piece moves
else:
# for each a direction a piece moves in
for offset in X88.PIECE_OFFSETS[Piece.klass(piece)]:
t_x88 = x88 + offset
# while we do not fall off the board
while not t_x88 & 0x88:
# if there was not piece to attack then yield a quiet move
if not self._pieces[t_x88]:
yield Move.from_x88(x88, t_x88)
# do not break out
# else there is a piece there
else:
# if we can attack generate a move
if Piece.color(self._pieces[t_x88]) != tomove:
yield Move.from_x88(x88, t_x88)
# we hit something so break out
break
# Knight and king do not go multiple times in their direction.
if klass in [KNIGHT, KING]:
break
# travel down the board in the direction
t_x88 += offset
# castling moves
opponent = Piece.opposite_color(tomove)
ok = True
# get possible castling for the side to move
for castle in [c for c in self.fen._castle_rights if Piece.color(c) == tomove]:
(square, enum), _ = Piece.castle_squares[castle]
king = Square(square)
if Piece.klass(castle) == KING:
direc = 1
else:
direc = -1
#.........这里部分代码省略.........
示例5: make_move
# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import opposite_color [as 别名]
def make_move(self, move, validate=True):
"""Makes a move.
:param move:
The move to make.
:param validate:
Defaults to `True`. Whether the move should be validated.
:return:
Making a move changes the position object. The same
(changed) object is returned for chainability.
:raise MoveError:
If the validate parameter is `True` and the move is not
legal in the position.
"""
#if validate:
if validate:
if move not in self.get_legal_moves(source=move.source):
raise MoveError(
"%s is not a legal move in the position %s." % (move, self.fen))
piece = self._pieces[move._source_x88]
capture = self._pieces[move._target_x88]
target = move.target
source = move.source
# Move the piece.
self._pieces[move._target_x88] = piece
self._pieces[move._source_x88] = None
# It is the next players turn.
ocolor = Piece.opposite_color(self.fen._to_move)
self.fen._to_move = ocolor
# Pawn moves.
self._ep = None
if Piece.klass(piece) == PAWN:
# En-passant.
if target.x != source.x and not capture:
offset = 16 if self.fen._to_move == WHITE else -16
self._pieces[target.x88() + offset] = None
capture = True
# If big pawn move, set the en-passant file.
if abs(target.y - source.y) == 2:
if self.get_theoretical_ep_right(target.x):
self._ep = move.target
# Promotion.
if move.promotion:
self._pieces[move.target._x88] = move.promotion
# Potential castling.
if Piece.klass(piece) == KING:
steps = move.target.x - move.source.x
if abs(steps) == 2:
# Queen-side castling.
if steps == -2:
rook_target = move.target.x88 + 1
rook_source = move.target.x88 - 2
# King-side castling.
else:
rook_target = move.target.x88 - 1
rook_source = move.target.x88 + 1
self._pieces[rook_target] = self._pieces[rook_source]
self._pieces[rook_source] = None
# Update castling rights.
for klass in self.fen._castle_rights:
if not self.get_theoretical_castling_right(klass):
self.fen._castle_rights.remove(klass)
# XXX Castling rights can only be removed
#self.set_castling_right(klass, False)
# Increment the 50 move half move counter.
if Piece.klass(piece) == PAWN or capture:
self.fen._fifty_move = 0
else:
self.fen._fifty_move += 1
# Increment the move number.
if self.fen._to_move == WHITE:
self.fen._full_move += 1
return self