def create_fen(pieces): """ Create a random FEN position using given pieces """ pos = pieces.rfind("k") pieces = pieces[:pos], pieces[pos:] ok = False while not ok: lboard = LBoard() lboard.applyFen("8/8/8/8/8/8/8/8 w - - 0 1") bishop_cords = [[], []] bishop_colors_ok = True cords = list(range(0, 64)) pawn_cords = list(range(0 + 8, 64 - 8)) # Order of color is important here to prevent offering # positions with trivial captures in first move for color in (WHITE, BLACK): for char in pieces[color]: piece = chrU2Sign[char.upper()] attacked = True limit = 100 while attacked and limit > 0: cord = random.choice(pawn_cords if char == "p" else cords) attacked = isAttacked(lboard, cord, 1 - color) limit -= 1 lboard._addPiece(cord, piece, color) cords.remove(cord) if cord in pawn_cords: pawn_cords.remove(cord) if char == "b": bishop_cords[color].append(cord) # 2 same color bishop is not ok if len(bishop_cords[color]) == 2 and bishop_colors_ok: b0, b1 = bishop_cords[color] b0_color = BLACK if RANK(b0) % 2 == FILE(b0) % 2 else WHITE b1_color = BLACK if RANK(b1) % 2 == FILE(b1) % 2 else WHITE if b0_color == b1_color: bishop_colors_ok = False break ok = (not lboard.isChecked()) and ( not lboard.opIsChecked()) and bishop_colors_ok fen = lboard.asFen() return fen
def create_fen(pieces): """ Create a random FEN position using given pieces """ pos = pieces.rfind("k") pieces = pieces[:pos], pieces[pos:] ok = False while not ok: lboard = LBoard() lboard.applyFen("8/8/8/8/8/8/8/8 w - - 0 1") bishop_cords = [[], []] bishop_colors_ok = True cords = list(range(0, 64)) pawn_cords = list(range(0 + 8, 64 - 8)) for color in (BLACK, WHITE): for char in pieces[color]: piece = chrU2Sign[char.upper()] cord = random.choice(pawn_cords if char == "p" else cords) lboard._addPiece(cord, piece, color) cords.remove(cord) if cord in pawn_cords: pawn_cords.remove(cord) if char == "b": bishop_cords[color].append(cord) # 2 same color bishop is not ok if len(bishop_cords[color]) == 2 and bishop_colors_ok: b0, b1 = bishop_cords[color] b0_color = BLACK if RANK(b0) % 2 == FILE(b0) % 2 else WHITE b1_color = BLACK if RANK(b1) % 2 == FILE(b1) % 2 else WHITE if b0_color == b1_color: bishop_colors_ok = False break ok = (not lboard.isChecked()) and (not lboard.opIsChecked()) and bishop_colors_ok fen = lboard.asFen() return fen
def scoreAllMoves(self, board, probeSoft=False): global URL, expression, PROMOTION_FLAGS fen = board.asFen().split()[0] + " w - - 0 1" if (fen, board.color) in self.table: return self.table[(fen, board.color)] if probeSoft or not conf.get("online_egtb_check"): return [] def get_data(URL, fen): # Request the page url = (URL + fen).replace(" ", "%20") try: f = urlopen(url) except IOError as e: log.warning( "Unable to read endgame tablebase from the Internet: %s" % repr(e)) data = b"" data = f.read() return data loop = asyncio.get_event_loop() future = loop.run_in_executor(None, get_data, URL, fen) data = yield from future # Parse for color, move_data in enumerate(data.split(b"\nNEXTCOLOR\n")): try: moves = [] for fcord, tcord, promotion, result in expression.findall( move_data.decode()): fcord = int(fcord) tcord = int(tcord) if promotion: flag = PROMOTION_FLAGS[int(promotion)] elif RANK(fcord) != RANK(tcord) and FILE(fcord) != FILE(tcord) and \ board.arBoard[fcord] == PAWN and board.arBoard[tcord] == EMPTY: flag = ENPASSANT else: flag = NORMAL_MOVE move = newMove(fcord, tcord, flag) if result == "Draw": state = DRAW steps = 0 else: s, steps = result.split(" in ") steps = int(steps) if result.startswith("Win"): if color == WHITE: state = WHITEWON else: state = BLACKWON elif result.startswith("Lose"): if color == WHITE: state = BLACKWON else: state = WHITEWON moves.append((move, state, steps)) if moves: self.table[(fen, color)] = moves elif color == board.color and board.opIsChecked(): log.warning("Asked endgametable for a won position: %s" % fen) elif color == board.color: log.warning( "Couldn't get %s data for position %s.\nData was: %s" % (reprColor[color], fen, repr(data))) except (KeyError, ValueError): log.warning( "Couldn't parse %s data for position %s.\nData was: %s" % (reprColor[color], fen, repr(data))) self.table[(fen, color)] = [] # Don't try again. if (fen, board.color) in self.table: return self.table[(fen, board.color)] return []
repr(e)) return [] data = f.read() # Parse for color, move_data in enumerate(data.split("\nNEXTCOLOR\n")): try: moves = [] for fcord, tcord, promotion, result in expression.findall( move_data): fcord = int(fcord) tcord = int(tcord) if promotion: flag = PROMOTION_FLAGS[int(promotion)] elif RANK(fcord) != RANK(tcord) and FILE(fcord) != FILE(tcord) and \ board.arBoard[fcord] == PAWN and board.arBoard[tcord] == EMPTY: flag = ENPASSANT else: flag = NORMAL_MOVE move = newMove(fcord, tcord, flag) if result == "Draw": state = DRAW steps = 0 else: s, steps = result.split(" in ") steps = int(steps) if result.startswith("Win"):