def eval_fn(self, board): res = 0 for p in chess.PIECE_TYPES: for wp in board.pieces(p, chess.WHITE): res += self.get_weigth_piece(chess.piece_symbol(p), wp) for bp in board.pieces(p, chess.BLACK): res -= self.get_weigth_piece(chess.piece_symbol(p), chess.square_mirror(bp)) return res
def visit_move(self, board, move): if move.promotion is not None: self.gm.has_promotion = True self.gm.promotion_count[board.turn] += 1 piece_symbol = chess.piece_symbol(move.promotion) self.gm.promotions[board.turn].append(piece_symbol)
def vp(pocket: CrazyhousePocket, white: bool): symbols = " ".join( [chess.piece_symbol(p) for p in chess.PIECE_TYPES[:-1]]) if white: symbols = symbols.upper() counts = " ".join( [str(pocket.pieces.get(p, 0)) for p in chess.PIECE_TYPES[:-1]]) return [symbols, counts]
def visit_move(self, board, move): if move.promotion is not None: self.gm.headers._others['has_promotion'] = True self.gm.headers._others['promotion_count'][board.turn] += 1 piece_symbol = chess.piece_symbol(move.promotion) self.gm.headers._others['promotions'][board.turn].append( piece_symbol) # noqa
def parse_piece(piece): if piece is None: return {} else: piece_type = piece.piece_type data = { "color": "WHITE" if piece.color else "BLACK", "type": piece_type, "name": piece_name(piece_type), "symbol": piece_symbol(piece_type), "value": PIECE_VALUES[piece_type], } return data
def __str__(self) -> str: return "".join( chess.piece_symbol(pt) * self.count(pt) for pt in reversed(chess.PIECE_TYPES))
def to_piece_symbol(pieces): nd = {} for p, s in pieces.items(): nd[chess.piece_symbol(p)] = s return nd
def __str__(self) -> str: return "".join(chess.piece_symbol(pt) * self.count(pt) for pt in reversed(chess.PIECE_TYPES))
def set_move(self, move: engine.PlayResult): """ Plays new move on chess.com :param move: reports move of opponent to client using normal engine output format """ if move.move is None: if move.resigned: resign_button = self._driver.find_elements_by_class_name( "secondary-controls-icon") # If a bot is being played if len(resign_button) != 0: resign_button[-1].click() else: # Resign confirmation should be turned off self._driver.find_element_by_class_name( "resign-button-component.live-game-buttons-button" ).click() return else: raise Exception("Internal error empty move given as input") if move.draw_offered: draw = self._driver.find_element_by_class_name( "draw-button-component.live-game-buttons-button") draw.click() piece = self._board.piece_at(move.move.from_square) self._board.push(move.move) piece_char: str = chess.piece_symbol(piece.piece_type).lower() piece_color = 'w' if piece.color == chess.WHITE else 'b' piece_elem_name = "piece.{piece_color}{piece_char}.square-{piece_file}{piece_rank}".format( piece_color=piece_color, piece_char=piece_char, piece_file=chess.square_file(move.move.from_square) + 1, piece_rank=chess.square_rank(move.move.from_square) + 1) # Find square with the piece to move piece_elem = self._driver.find_element_by_class_name(piece_elem_name) square_height = piece_elem.size['height'] # Calculate relative position of to square from whites perspective transpose_file = chess.square_file( move.move.to_square) - chess.square_file(move.move.from_square) transpose_rank = chess.square_rank( move.move.to_square) - chess.square_rank(move.move.from_square) # Flip transpose if player if black transpose_file = transpose_file if self.color is chess.WHITE else -transpose_file transpose_rank = transpose_rank if self.color is chess.WHITE else -transpose_rank # Drag piece with transposition act = ActionChains(self._driver) act.drag_and_drop_by_offset(piece_elem, -transpose_file * square_height, transpose_rank * square_height).perform() if move.move.promotion is not None: time.sleep(0.1) piece_char: str = chess.piece_symbol(move.move.promotion).lower() piece_elem_name = "promotion-piece.{piece_color}{piece_char}".format( piece_color=piece_color, piece_char=piece_char) piece_elem = self._driver.find_element_by_class_name( piece_elem_name) piece_elem.click()
('--........._ /'--.....___| |__ ___ ___ ___ `"--..../ __| '_ \\ / _ \\/ __/ __| | (__| | | | __/\\__ \\__ \\ \\___|_| |_|\\___||___/___/ ''' FIGURES_ASCII = { chess.PAWN: PAWN_FIGURE_ASCII, chess.ROOK: ROOK_FIGURE_ASCII, chess.KNIGHT: KNIGHT_FIGURE_ASCII, chess.BISHOP: BISHOP_FIGURE_ASCII, chess.QUEEN: QUEEN_FIGURE_ASCII, chess.KING: KING_FIGURE_ASCII, chess.piece_symbol(chess.PAWN): PAWN_FIGURE_ASCII, chess.piece_symbol(chess.ROOK): ROOK_FIGURE_ASCII, chess.piece_symbol(chess.KNIGHT): KNIGHT_FIGURE_ASCII, chess.piece_symbol(chess.BISHOP): BISHOP_FIGURE_ASCII, chess.piece_symbol(chess.QUEEN): QUEEN_FIGURE_ASCII, chess.piece_symbol(chess.KING): KING_FIGURE_ASCII, chess.piece_symbol(chess.PAWN).upper(): PAWN_FIGURE_ASCII, chess.piece_symbol(chess.ROOK).upper(): ROOK_FIGURE_ASCII, chess.piece_symbol(chess.KNIGHT).upper(): KNIGHT_FIGURE_ASCII, chess.piece_symbol(chess.BISHOP).upper(): BISHOP_FIGURE_ASCII, chess.piece_symbol(chess.QUEEN).upper(): QUEEN_FIGURE_ASCII, chess.piece_symbol(chess.KING).upper(): KING_FIGURE_ASCII, } SQUARE_WIDTH = 10
else: # if in knightmode pieces are all knights pieces[i] = 2 #generate random positions while True: for i in range(level): #random squares squares[i] = randint(0, 63) if level == len(set( squares)): #make sure that no pieces are on the same square break #printing position of pieces in algebraic notation print('Following pieces are on the board:') print('') for i in range(level): piece = chess.piece_symbol(pieces[i]).upper() file = chess.FILE_NAMES[chess.square_file(squares[i])] rank = chess.RANK_NAMES[chess.square_rank(squares[i])] algebraic = piece + file + rank print(algebraic) print('') #setting up board and pieces board = chess.Board() board.clear() for i in range(level): # set piece method: # set_piece_at(square: int, piece: Optional[chess.Piece], promoted: bool = False) # sets a piece at the given square. board.set_piece_at(square=squares[i], piece=chess.Piece(pieces[i], True))