def sign(score: Score) -> int: if score.is_mate(): s = score.mate() else: s = score.score() if s > 0: return 1 elif s < 0: return -1 return 0
def win_chances(score: Score) -> float: """ winning chances from -1 to 1 https://graphsketch.com/?eqn1_color=1&eqn1_eqn=100+*+%282+%2F+%281+%2B+exp%28-0.004+*+x%29%29+-+1%29&eqn2_color=2&eqn2_eqn=&eqn3_color=3&eqn3_eqn=&eqn4_color=4&eqn4_eqn=&eqn5_color=5&eqn5_eqn=&eqn6_color=6&eqn6_eqn=&x_min=-1000&x_max=1000&y_min=-100&y_max=100&x_tick=100&y_tick=10&x_label_freq=2&y_label_freq=2&do_grid=0&do_grid=1&bold_labeled_lines=0&bold_labeled_lines=1&line_width=4&image_w=850&image_h=525 """ mate = score.mate() if mate is not None: return 1 if mate > 0 else -1 cp = score.score() return 2 / (1 + math.exp(-0.004 * cp)) - 1 if cp is not None else 0
def should_investigate(a: Score, b: Score, board: Board) -> bool: """ determine if the difference between scores A and B makes the position worth investigating for a puzzle. A and B are normalized scores (scores from white's perspective) """ a_cp = a.score() b_cp = b.score() if a_cp is not None and material_total(board) > 3: if b_cp is not None and material_count(board) > 6: # from an even position, the position changed by more than 1.1 cp if abs(a_cp) < 110 and abs(b_cp - a_cp) >= 110: return True # from a winning position, the position is now even elif abs(a_cp) > 200 and abs(b_cp) < 110: return True # from a winning position, a player blundered into a losing position elif abs(a_cp) > 200 and sign(b) != sign(a): return True elif b.is_mate(): # from an even position, someone is getting checkmated if abs(a_cp) < 110: return True # from a major advantage, blundering and getting checkmated elif sign(a) != sign(b): return True elif a.is_mate(): if b.is_mate(): # blundering a checkmating position into being checkmated if b.mate() != 0 and sign(a) != sign(b): return True elif b_cp is not None: # blundering a mate threat into a major disadvantage if sign(a) != sign(b): return True # blundering a mate threat into an even position if abs(b_cp) < 110: return True return False