def __init__(self, board, color, origin, dest, flags): rank_dest = rank(dest) promotion = 0 if board.pieces[origin] == PAWN and (rank_dest == 7 or rank_dest == 0): promotion = QUEEN self.color = color self._origin = origin self._destination = dest self.flags = flags self.piece = board.pieces[origin] self.promotion = promotion if promotion: self.flags |= PROMOTION if board.pieces[dest]: self.captured = board.pieces[dest] elif flags & EN_PASSANT: self.captured = PAWN else: self.captured = PIECE_EMPTY self.half_moves = board.half_moves self.previous_en_passant = board.en_passant_square self.previous_hash = board.hash self.white_castling = board.castling[WHITE] self.black_castling = board.castling[BLACK]
def p0x88_to_chess_notation(x): icol = col(x) irow = rank(x) return chr(icol + 97) + str(irow + 1)
def p0x88_to_tuple(position): return (col(position), rank(position))
def generate_moves(self, legal, square, color): moves = [] current = color first = A8 last = H1 single = 0 if current == COLOR_EMPTY: current = self.current_color other = next_color(current) if is_square(square): first = square last = square single = 1 for i in range(first, last + 1): if is_not_square(i): i = i + 7 continue if self.colors[i] != current: continue piece = self.pieces[i] if piece == PAWN: # 1 step forward square = i + PAWN_OFFSETS[current][0] if not self.pieces[square]: moves.append(Move(self, current, i, square, NORMAL)) # 2 steps forward square = i + PAWN_OFFSETS[current][1] if (rank(i) == SECOND_RANK[current] and not self.pieces[square]): moves.append(Move(self, current, i, square, BIG_PAWN)) # Captures for j in range(2, 4): square = i + PAWN_OFFSETS[current][j] if is_not_square(square): continue if self.pieces[square] and self.colors[square] == other: moves.append(Move(self, current, i, square, CAPTURE)) elif square == self.en_passant_square: moves.append( Move(self, current, i, square, EN_PASSANT)) else: for j in range(0, PIECE_OFFSET_SIZE[piece]): offset = PIECE_OFFSET[piece][j] square = i while True: square += offset if is_not_square(square): break if not self.pieces[square]: moves.append( Move(self, current, i, square, NORMAL)) else: if self.colors[square] == current: break moves.append( Move(self, current, i, square, CAPTURE)) break # Stop after first for king and knight if (piece == KING or piece == KNIGHT): break # Castling if ((not single or last == self.kings[current]) and self.kings[current] != EMPTY): if self.castling[current] & KINGSIDE: origin = self.kings[current] dest = origin + E + E if (not self.pieces[origin + E] and not self.pieces[dest] and not self.attacked(origin, other) and not self.attacked(origin + E, other) and not self.attacked(dest, other)): moves.append(Move(self, current, origin, dest, KINGSIDE)) if self.castling[current] & QUEENSIDE: origin = self.kings[current] dest = origin + W + W if (not self.pieces[origin + W] and not self.pieces[origin + W + W] and not self.pieces[origin + W + W + W] and not self.pieces[dest] and not self.attacked(origin, other) and not self.attacked(origin + W, other) and not self.attacked(dest, other)): moves.append(Move(self, current, origin, dest, QUEENSIDE)) if not legal: return moves legal_moves = [] for move in moves: move.do(self) if not self.in_check(current): legal_moves.append(move) #else: # self.display() move.undo(self) return legal_moves