本文整理汇总了Python中piece.Piece.color方法的典型用法代码示例。如果您正苦于以下问题:Python Piece.color方法的具体用法?Python Piece.color怎么用?Python Piece.color使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类piece.Piece
的用法示例。
在下文中一共展示了Piece.color方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_attackers
# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import color [as 别名]
def get_attackers(self, color, square):
"""Gets the attackers of a specific square.
:param color:
Filter attackers by this piece color.
:param square:
The square to check for.
:yield:
Source squares of the attack.
"""
if not color in [BLACK, WHITE]:
raise KeyError("Invalid color: %s." % repr(color))
for x88, source in Square._x88_squares.iteritems():
piece = self._pieces[x88]
if not piece or Piece.color(piece) != color:
continue
difference = x88 - square._x88
index = difference + X88.ATTACKER_DIFF
klass = Piece.klass(piece)
if X88.ATTACKS[index] & (1 << X88.SHIFTS[klass]):
# Handle pawns.
if klass == PAWN:
if difference > 0:
if Piece.color(piece) == WHITE:
yield source
else:
if Piece.color(piece) == BLACK:
yield source
continue
# Handle knights and king.
if klass in [KNIGHT, KING]:
yield source
# Handle the others.
offset = X88.RAYS[index]
j = source._x88 + offset
blocked = False
while j != square._x88:
if self._pieces[j]:
blocked = True
break
j += offset
if not blocked:
yield source
示例2: get_piece_counts
# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import color [as 别名]
def get_piece_counts(self, colors=[WHITE, BLACK]):
"""Counts the pieces on the board.
:param color:
list of colors to check. Defualts to black and white
:return:
A dictionary of piece counts, keyed by lowercase piece type
letters.
"""
#if not color in ["w", "b", "wb", "bw"]:
# raise KeyError(
# "Expected color filter to be one of 'w', 'b', 'wb', 'bw', "
# "got: %s." % repr(color))
counts = {
PAWN: 0,
BISHOP: 0,
KNIGHT: 0,
ROOK: 0,
KING: 0,
QUEEN: 0,
}
for piece in self._pieces:
if piece and Piece.color(piece) in colors:
counts[Piece.klass(piece)] += 1
return counts
示例3: get_pseudo_legal_moves
# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import 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
#.........这里部分代码省略.........
示例4: test_color
# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import color [as 别名]
def test_color(self):
self.assertEqual(piece.WHITE, Piece.color('P'))
self.assertEqual(piece.BLACK, Piece.color('p'))