Body: <p>In this example &quot;'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 9&quot;
  should be &quot;'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 11&quot;
  according to Stock-Fish.</p>
  <p><strong>EDIT</strong> The code should select <strong>14::</strong> preferable to <strong>17::</strong>
  below.</p>
  <p><a href="https://pastebin.com/mJRGL9kx" rel="nofollow noreferrer">THE CODE</a></p>
  <pre><code>Final Output:
  {'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 9, 'parent': None, 'color': True, 'result': 1, 'processed': True, 'sequence': [17], 'children': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30], 'to_end': 9}
  Node ID: 1, Moves to Mate: 9, Result: 1
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 17, Moves to Mate: 8, Result: 1
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 101, Moves to Mate: 7, Result: 1
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 1190, Moves to Mate: 6, Result: 1
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . K . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 3743, Moves to Mate: 5, Result: 1
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . . K . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 15967, Moves to Mate: 4, Result: 1
  . . . . . . . .
  . . . . . . k .
  . . . . K . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 31639, Moves to Mate: 3, Result: 1
  . . . . . . k .
  . . . . . . . .
  . . . . K . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 70638, Moves to Mate: 2, Result: 1
  . . . . . . k .
  . . . . . . . .
  . . . . . K . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 106380, Moves to Mate: 1, Result: 1
  . . . . . k . .
  . . . . . . . .
  . . . . . K . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 165316, Moves to Mate: 0, Result: 1
  . . Q . . k . .
  . . . . . . . .
  . . . . . K . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . . 
  
  1:: 9 {'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 9, 'parent': None, 'color': True, 'result': 1, 'processed': True, 'sequence': [17], 'children': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30], 'to_end': 9}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  2:: 10 {'fen': '6Q1/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': 10, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [34], 'children': [31, 32, 33, 34, 35, 36], 'to_end': 10}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  3:: 8 {'fen': '5Q2/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [38], 'children': [37, 38, 39, 40], 'to_end': 8}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  4:: 3 {'fen': '4Q3/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [42], 'children': [41, 42, 43, 44, 45, 46], 'to_end': 3}
  . . . . Q . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  5:: 9 {'fen': '3Q4/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [50], 'children': [47, 48, 49, 50, 51, 52], 'to_end': 9}
  . . . Q . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  6:: 9 {'fen': '2Q5/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [55], 'children': [53, 54, 55, 56, 57, 58], 'to_end': 9}
  . . Q . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  7:: 10 {'fen': '1Q6/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [60], 'children': [59, 60, 61, 62, 63, 64, 65], 'to_end': 10}
  . Q . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  8:: 12 {'fen': 'Q7/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': 12, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [69], 'children': [66, 67, 68, 69, 70, 71, 72, 73], 'to_end': 12}
  Q . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  9:: 12 {'fen': '8/7Q/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [74], 'children': [74, 75, 76], 'to_end': 12}
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  10:: 10 {'fen': '8/6Q1/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [77], 'children': [77, 78, 79, 80], 'to_end': 10}
  . . . . . . . .
  . . . . . . Q .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  11:: 12 {'fen': '8/8/7Q/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [82], 'children': [81, 82, 83], 'to_end': 12}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  12:: 6 {'fen': '8/8/5Q2/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [85], 'children': [84, 85, 86], 'to_end': 6}
  . . . . . . . .
  . . . . . . . .
  . . . . . Q . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  13:: 8 {'fen': '8/8/8/6kQ/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [89], 'children': [87, 88, 89], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k Q
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  14:: 10 {'fen': '8/8/8/4Q1k1/2K5/8/8/8 b - -', 'moves_to_mate': 10, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [90], 'children': [90, 91, 92, 93], 'to_end': 10}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . Q . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  15:: 8 {'fen': '8/8/8/6k1/2K4Q/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [95], 'children': [94, 95, 96], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  16:: 12 {'fen': '8/8/8/6k1/2KQ4/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [98], 'children': [97, 98, 99, 100], 'to_end': 12}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K Q . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  17:: 8 {'fen': '8/8/8/6k1/2K5/7Q/8/8 b - -', 'moves_to_mate': 8, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [101], 'children': [101, 102, 103], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .&lt;
  
  18:: 10 {'fen': '8/8/8/6k1/2K5/2Q5/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [108], 'children': [104, 105, 106, 107, 108, 109, 110], 'to_end': 10}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . Q . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  19:: 12 {'fen': '8/8/8/6k1/2K5/8/7Q/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [112], 'children': [111, 112, 113, 114], 'to_end': 12}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .&lt;
  
  20:: 8 {'fen': '8/8/8/6k1/2K5/8/1Q6/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [120], 'children': [115, 116, 117, 118, 119, 120, 121], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . Q . . . . . .
  . . . . . . . .&lt;
  
  21:: 8 {'fen': '8/8/8/6k1/2K5/8/8/7Q b - -', 'moves_to_mate': 8, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [122], 'children': [122, 123, 124, 125, 126], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q&lt;
  
  22:: 8 {'fen': '8/8/8/6k1/2K5/8/8/Q7 b - -', 'moves_to_mate': 8, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [127], 'children': [127, 128, 129, 130, 131, 132, 133], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  Q . . . . . . .&lt;
  
  23:: 8 {'fen': '7Q/8/8/3K2k1/8/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [135], 'children': [134, 135, 136, 137], 'to_end': 8}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . K . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  24:: 3 {'fen': '7Q/8/8/2K3k1/8/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [138], 'children': [138, 139, 140, 141], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . K . . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  25:: 3 {'fen': '7Q/8/8/1K4k1/8/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [142], 'children': [142, 143, 144, 145], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . K . . . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  26:: 12 {'fen': '7Q/8/8/6k1/3K4/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [147], 'children': [146, 147, 148, 149], 'to_end': 12}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . K . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  27:: 3 {'fen': '7Q/8/8/6k1/1K6/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [150], 'children': [150, 151, 152, 153], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . K . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  28:: 10 {'fen': '7Q/8/8/6k1/8/3K4/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [155], 'children': [154, 155, 156, 157], 'to_end': 10}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . . K . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  29:: 3 {'fen': '7Q/8/8/6k1/8/2K5/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [158], 'children': [158, 159, 160, 161], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  30:: 3 {'fen': '7Q/8/8/6k1/8/1K6/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [162], 'children': [162, 163, 164, 165], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . K . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  31:: 7 {'fen': '6Q1/8/7k/8/2K5/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [184], 'children': [166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . k
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  32:: 7 {'fen': '6Q1/8/5k2/8/2K5/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [202], 'children': [192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . k . .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  33:: 7 {'fen': '6Q1/8/8/7k/2K5/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [236], 'children': [218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . k
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  34:: 9 {'fen': '6Q1/8/8/5k2/2K5/8/8/8 w - -', 'moves_to_mate': 9, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [244], 'children': [244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269], 'to_end': 9}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . k . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  35:: 7 {'fen': '6Q1/8/8/8/2K4k/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [288], 'children': [270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . . k
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  36:: 7 {'fen': '6Q1/8/8/8/2K2k2/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [296], 'children': [296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . k . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  37:: 5 {'fen': '5Q2/8/6k1/8/2K5/8/8/8 w - -', 'moves_to_mate': None, 'parent': 3, 'color': True, 'result': 0, 'processed': True, 'sequence': [332], 'children': [322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350], 'to_end': 5}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  38:: 7 {'fen': '5Q2/8/8/7k/2K5/8/8/8 w - -', 'moves_to_mate': None, 'parent': 3, 'color': True, 'result': 0, 'processed': True, 'sequence': [363], 'children': [351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371], 'to_end': 7}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . k
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  39:: 7 {'fen': '5Q2/8/8/8/2K4k/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 3, 'color': True, 'result': 1, 'processed': True, 'sequence': [388], 'children': [372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392], 'to_end': 7}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . . k
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  40:: 2 {'fen': '5Q2/8/8/8/2K3k1/8/8/8 w - -', 'moves_to_mate': None, 'parent': 3, 'color': True, 'result': 0, 'processed': True, 'sequence': [408], 'children': [393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421], 'to_end': 2}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  41:: 9 {'fen': '4Q3/8/7k/8/2K5/8/8/8 w - -', 'moves_to_mate': 9, 'parent': 4, 'color': True, 'result': 1, 'processed': True, 'sequence': [434], 'children': [422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441], 'to_end': 9}
  . . . . Q . . .
  . . . . . . . .
  . . . . . . . k
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  42:: 2 {'fen': '4Q3/8/5k2/8/2K5/8/8/8 w - -', 'moves_to_mate': None, 'parent': 4, 'color': True, 'result': 0, 'processed': True, 'sequence': [443], 'children': [442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461], 'to_end': 2}
  . . . . Q . . .
  . . . . . . . .
  . . . . . k . .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  43:: 7 {'fen': '4Q3/8/8/5k2/2K5/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 4, 'color': True, 'result': 1, 'processed': True, 'sequence': [477], 'children': [462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481], 'to_end': 7}
  . . . . Q . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . k . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  44:: 7 {'fen': '4Q3/8/8/8/2K4k/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 4, 'color': True, 'result': 1, 'processed': True, 'sequence': [495], 'children': [482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499], 'to_end': 7}
  . . . . Q . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . . k
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  
  46:: 9 {'fen': '4Q3/8/8/8/2K2k2/8/8/8 w - -', 'moves_to_mate': 9, 'parent': 4, 'color': True, 'result': 1, 'processed': True, 
  
  51:: 7 {'fen': '3Q4/8/8/8/2K3k1/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 5, 'color': True, 'result': 1, 'processed': True, 'sequence': [619], 'children': [614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631], 'to_end': 7}
  . . . Q . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .&lt;
  </code></pre>
  <p><strong>EDIT MY CODE</strong></p>
  <pre><code>import chess
  
  def simplify_fen(fen):
      &quot;&quot;&quot;Simplifies a FEN string by keeping only the position, turn, castling availability, and en passant target square.&quot;&quot;&quot;
      return ' '.join(fen.split(' ')[:4])
  
  def add_descendants_iteratively(A, fen_to_node_id):
      queue = [(1, 0)]
      while queue:
          node_id, depth = queue.pop(0)
          board = chess.Board(A[node_id]['fen'] + &quot; 0 1&quot;)
          for move in board.legal_moves:
              if move.promotion:
                  move = chess.Move(move.from_square, move.to_square, promotion=chess.QUEEN)
              board.push(move)
              simplified_fen = simplify_fen(board.fen())
              if simplified_fen not in fen_to_node_id:
                  new_node_id = len(A) + 1
                  A[new_node_id] = {
                      'fen': simplified_fen,
                      'moves_to_mate': None,
                      'parent': node_id,
                      'color': chess.WHITE if simplified_fen.split()[1] == 'w' else chess.BLACK,
                      'result': None,
                      'processed': False,
                      'sequence': [],
                      'children': [],
                      'to_end': None,
                  }
                  fen_to_node_id[simplified_fen] = new_node_id
                  A[node_id]['children'].append(new_node_id)
                  queue.append((new_node_id, depth + 1))
              board.pop()
  
  def initialize_game_tree(initial_fen):
      simplified_fen = simplify_fen(initial_fen)
      A = {1: {'fen': simplified_fen, 'moves_to_mate': None, 'parent': None,
               'color': chess.WHITE if initial_fen.split()[1] == 'w' else chess.BLACK,
               'result': None, 'processed': False, 'sequence': [], 'children': [], 'to_end': None}}
      fen_to_node_id = {simplified_fen: 1}
      return A, fen_to_node_id
  
  def update_game_outcomes(A):
      for key, node in A.items():
          board = chess.Board(node['fen'] + &quot; 0 1&quot;)
          if board.is_game_over():
              outcome = board.outcome()
              node['processed'] = True
              if outcome.termination == chess.Termination.CHECKMATE:
                  node['result'] = 1 if outcome.winner == chess.WHITE else -1
                  node['moves_to_mate'] = 0
              else:
                  node['result'] = 0
                  node['moves_to_mate'] = None
              node['to_end'] = 0
  
  
  def update_parent_preferences(node_id, A):
      node = A[node_id]
      # Return the current values if the node has already been processed
      if node['processed']:
          return node['to_end'], node['moves_to_mate'], node['result']
  
      best_child_id = None
      # Initialize best_result with an appropriate value based on the color to play
      best_result = float('-inf') if node['color'] else float('inf')
      best_to_end = None
  
      for child_id in node['children']:
          child_to_end, child_moves_to_mate, child_result = update_parent_preferences(child_id, A)
          
          # Ensure comparisons are valid by checking for None
          if child_result is None:
              continue  # Skip this child because it has no result, indicating it's not a terminal node or not evaluated yet
  
          # Handle the logic for updating the best choice based on the color to play
          if node['color']:  # If it's White's turn to play
              if child_result &gt; best_result or (child_result == best_result and (best_to_end is None or (child_to_end is not None and child_to_end &lt; best_to_end))):
                  best_child_id = child_id
                  best_result = child_result
                  best_to_end = child_to_end
          else:  # If it's Black's turn to play
              if child_result &lt; best_result or (child_result == best_result and (best_to_end is None or (child_to_end is not None and child_to_end &gt; best_to_end))):
                  best_child_id = child_id
                  best_result = child_result
                  best_to_end = child_to_end
  
      # Update the current node based on the best child node found
      if best_child_id is not None:
          node['sequence'] = [best_child_id]
          node['result'] = best_result
          node['to_end'] = 1 + (best_to_end if best_to_end is not None else 0)
          node['moves_to_mate'] = None if node['result'] == 0 else 1 + (best_to_end if best_to_end is not None else 0)
      else:
          node['to_end'] = None
          node['moves_to_mate'] = None
  
      node['processed'] = True
      return node['to_end'], node['moves_to_mate'], node['result']
  
  
  # Note: This function assumes the presence of an evaluate_board function or similar logic
  # that sets the initial 'result' for leaf nodes where the game is over.
  
  
  
  
  def print_sequence_from_root(A):
      current_node_id = 1
      while True:
          node = A[current_node_id]
          board = chess.Board(node['fen'] + &quot; 0 1&quot;)
          print(f&quot;Node ID: {current_node_id}, Moves to Mate: {node['moves_to_mate']}, Result: {node['result']}&quot;)
          print(board, &quot;\n&quot;)
          
          if not node['sequence']:
              break
          current_node_id = node['sequence'][0]
  
  def main():
      initial_fen = &quot;7Q/8/8/6k1/2K5/8/8/8 w - - 0 1&quot;
      A, fen_to_node_id = initialize_game_tree(initial_fen)
      add_descendants_iteratively(A, fen_to_node_id)
      update_game_outcomes(A)
      update_parent_preferences(1, A)
      print(&quot;Final Output:&quot;)
      print(A[1],&quot;1*\n&quot;)
      print(A[15967],&quot;15967*\n&quot;)
      print(A[31639],&quot;31639*\n&quot;)
      print(A[31640],&quot;31640\n&quot;)
      print(A[31641],&quot;31641\n&quot;)
      print(A[3743],&quot;3743*\n&quot;)
      print(A[1190],&quot;1190*\n&quot;)
      print(A[101],&quot;101*\n&quot;)
      print(A[17],&quot;17*\n&quot;)
      print(A[70638],&quot;70638*\n&quot;)
      print(A[106380],&quot;106380*\n&quot;)
      print(A[165316],&quot;165316*\n&quot;)
      
      print_sequence_from_root(A)
  
  #    for key in [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]:
      for key in range(1,1000000):
          if A[key]['to_end'] != None: # and A[key]['to_end'] &gt; 9:
              print(f&quot;{key}:: {A[key]['to_end']} {A[key]}\n{chess.Board(A[key]['fen'])}&lt;\n&quot;)
  
          
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-29T21:28:05.373Z
Comments.Text: How have you tried debugging?
Comments.UserDisplayName: qwr
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-29T21:41:42.153Z
Comments.Text: @qwr Yes, I'm still trying it very intensively. No much results obtained so far.
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-29T21:44:32.940Z
Comments.Text: `Processing node 14
  Node 14 is weaker player's node, aiming for min result.
  Checking child 90 of node 14.
  Updating best child for node 14 to 90 (weaker).
  Checking child 91 of node 14.
  Child 91 has no result, skipping.
  Checking child 92 of node 14.
  Child 92 has no result, skipping.
  Checking child 93 of node 14.
  Child 93 has no result, skipping.
  Node 14 updated with best child 90, result: 1, to_end: 10.
  Node 14 marked as processed.
  Final Output:
  {'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_m`
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-29T22:21:21.773Z
Comments.Text: I've partially identified the problem. There are 2 players stronger and weaker. The stronger player's primary goal is to capitalize on their advantage to secure a win as efficiently as possible. The weaker player aims to prolong the game, hoping for a mistake from the stronger player or finding an opportunity to level the playing field. The 'moves_to_end' should be selected larger for the weaker player, but I do not understand where to achieve this in my code.
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-03T17:02:34.210Z
Comments.Text: Please put the code in the question and get rid of the commented out code. Are you trying to construct a tablebase?
Comments.UserDisplayName: qwr
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-03T19:28:52.920Z
Comments.Text: @qwr I'm trying to create an endgame Python code without relying on StockFish as an external source. I'm not creating any tablebase. I'll put my code in the question, if you wish so.
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-03T19:37:26.163Z
Comments.Text: @qwr What I consider as the most important feature about how the code should work is that both player B and  W should play 100% optimally. The stronger player should reach a  win in as few moves as possible and the weaker one delay the checkmate as long as possible or even try to achieve a draw. The **stronger** player is provided to the code as an external information by the user.
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-03T19:52:56.723Z
Comments.Text: @qwr Yet to your question, I'm trying to create a dictionary A populated by **all** the FEN strings that are obtainable by a legal move from an initial_fen of arbitrary depth. So in essence I'll keep in my RAM memory 64^D positions where D is the number of pieces left on the board in the endgame in question.Then from the terminal  nodes I'll climb up to A[1][,moves_to_mate'] which in my string should give the number 11 but it is giving to me 9, like as if B is not playing optimally.
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-03T20:44:16.583Z
Comments.Text: So that's pretty much creating a table base. do you know how those are constructed?
Comments.UserDisplayName: qwr
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-03T21:08:10.520Z
Comments.Text: @qwr Not at all. I was not going to store anything in my HDD. I pursue a simpler task of populating my dictionary A with correct descendants of initial_fen and then enjoy looking at its play with fully optimal moves including this Reti [endgame](https://lichess.org/analysis/7K/8/k1P5/7p/8/8/8/8_w_-_-_0_1?color=white) for which there is a unique move of K playing to h8g7 which saves a draw for W with stronger second player Black.
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-03T21:55:18.960Z
Comments.Text: So you're just making a table base on the fly
Comments.UserDisplayName: qwr
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-04T10:02:35.997Z
Comments.Text: @qwr Yes! Do you think I'll get help here for this project ?
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-04T11:08:32.223Z
Comments.Text: @user2925716 do you have some kind of move ordering and score back propagation?
Comments.UserDisplayName: ferdy
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-04T13:35:27.597Z
Comments.Text: @ferdy Your question should be read off from my code above. I don't think that there is some kind of move ordering and score back propagation. Feel free too edit my code and post the result back on pastebin.com Also, [here](https://www.kaggle.com/discussions/questions-and-answers/481444) is some kind of advice to my problem.
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-04T21:24:18.240Z
Comments.Text: I don't know if you'll get help. Debugging someone else's code beyond a certain complexity is no fun. I only do it for my job.
Comments.UserDisplayName: qwr
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-04T21:42:05.830Z
Comments.Text: A BREAKTHROUGH: the only node which is incorrectly computed is the A[1]['sequence']. The code gives [13] but when manually set to [14] **all other nodes are OK**. What's the reason for this and how can I fix it beyond setting it manually ? I'll assign my bounty even for this simple help. I.e. I mean *simple* with the advice given.
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-09T20:42:48.243Z
Comments.Text: Nobody interested in the Bounty ? :-)
Comments.UserDisplayName: user2925716
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-10T03:50:32.330Z
Comments.Text: This epd `7Q/8/8/6k1/2K5/8/8/8 w - -` is mate in 6 moves. why are you saying `"'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 11" according to Stock-Fish.` Perhaps you mean, `plies_to_mate: 11`
Comments.UserDisplayName: ferdy
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-10T15:59:00.100Z
Comments.Text: @ferdy You are correct, I'm now used to mean by a move in fact a plie (or plye?).
Comments.UserDisplayName: user2925716
Tags: <engines><endgame><software><python>
Title: I have a code in Python which should compute fully optimal moves in an endgame for Black and White player, but it seems that k is playing suboptimally
ThreadText: # I have a code in Python which should compute fully optimal moves in an endgame for Black and White player, but it seems that k is playing suboptimally
  
  (Asked by: username_0 on 2024-02-29)
  
  In this example "'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 9"
  should be "'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 11"
  according to Stock-Fish.
  EDIT The code should select 14:: preferable to 17::
  below.
  THE CODE
  Final Output:
  {'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 9, 'parent': None, 'color': True, 'result': 1, 'processed': True, 'sequence': [17], 'children': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30], 'to_end': 9}
  Node ID: 1, Moves to Mate: 9, Result: 1
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 17, Moves to Mate: 8, Result: 1
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 101, Moves to Mate: 7, Result: 1
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 1190, Moves to Mate: 6, Result: 1
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . K . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 3743, Moves to Mate: 5, Result: 1
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . . K . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 15967, Moves to Mate: 4, Result: 1
  . . . . . . . .
  . . . . . . k .
  . . . . K . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 31639, Moves to Mate: 3, Result: 1
  . . . . . . k .
  . . . . . . . .
  . . . . K . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 70638, Moves to Mate: 2, Result: 1
  . . . . . . k .
  . . . . . . . .
  . . . . . K . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 106380, Moves to Mate: 1, Result: 1
  . . . . . k . .
  . . . . . . . .
  . . . . . K . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . . 
  
  Node ID: 165316, Moves to Mate: 0, Result: 1
  . . Q . . k . .
  . . . . . . . .
  . . . . . K . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . . 
  
  1:: 9 {'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 9, 'parent': None, 'color': True, 'result': 1, 'processed': True, 'sequence': [17], 'children': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30], 'to_end': 9}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  2:: 10 {'fen': '6Q1/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': 10, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [34], 'children': [31, 32, 33, 34, 35, 36], 'to_end': 10}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  3:: 8 {'fen': '5Q2/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [38], 'children': [37, 38, 39, 40], 'to_end': 8}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  4:: 3 {'fen': '4Q3/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [42], 'children': [41, 42, 43, 44, 45, 46], 'to_end': 3}
  . . . . Q . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  5:: 9 {'fen': '3Q4/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [50], 'children': [47, 48, 49, 50, 51, 52], 'to_end': 9}
  . . . Q . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  6:: 9 {'fen': '2Q5/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [55], 'children': [53, 54, 55, 56, 57, 58], 'to_end': 9}
  . . Q . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  7:: 10 {'fen': '1Q6/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [60], 'children': [59, 60, 61, 62, 63, 64, 65], 'to_end': 10}
  . Q . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  8:: 12 {'fen': 'Q7/8/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': 12, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [69], 'children': [66, 67, 68, 69, 70, 71, 72, 73], 'to_end': 12}
  Q . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  9:: 12 {'fen': '8/7Q/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [74], 'children': [74, 75, 76], 'to_end': 12}
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  10:: 10 {'fen': '8/6Q1/8/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [77], 'children': [77, 78, 79, 80], 'to_end': 10}
  . . . . . . . .
  . . . . . . Q .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  11:: 12 {'fen': '8/8/7Q/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [82], 'children': [81, 82, 83], 'to_end': 12}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  12:: 6 {'fen': '8/8/5Q2/6k1/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [85], 'children': [84, 85, 86], 'to_end': 6}
  . . . . . . . .
  . . . . . . . .
  . . . . . Q . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  13:: 8 {'fen': '8/8/8/6kQ/2K5/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [89], 'children': [87, 88, 89], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k Q
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  14:: 10 {'fen': '8/8/8/4Q1k1/2K5/8/8/8 b - -', 'moves_to_mate': 10, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [90], 'children': [90, 91, 92, 93], 'to_end': 10}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . Q . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  15:: 8 {'fen': '8/8/8/6k1/2K4Q/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [95], 'children': [94, 95, 96], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  16:: 12 {'fen': '8/8/8/6k1/2KQ4/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [98], 'children': [97, 98, 99, 100], 'to_end': 12}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K Q . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  17:: 8 {'fen': '8/8/8/6k1/2K5/7Q/8/8 b - -', 'moves_to_mate': 8, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [101], 'children': [101, 102, 103], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .<
  
  18:: 10 {'fen': '8/8/8/6k1/2K5/2Q5/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [108], 'children': [104, 105, 106, 107, 108, 109, 110], 'to_end': 10}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . Q . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  19:: 12 {'fen': '8/8/8/6k1/2K5/8/7Q/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [112], 'children': [111, 112, 113, 114], 'to_end': 12}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . Q
  . . . . . . . .<
  
  20:: 8 {'fen': '8/8/8/6k1/2K5/8/1Q6/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [120], 'children': [115, 116, 117, 118, 119, 120, 121], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . Q . . . . . .
  . . . . . . . .<
  
  21:: 8 {'fen': '8/8/8/6k1/2K5/8/8/7Q b - -', 'moves_to_mate': 8, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [122], 'children': [122, 123, 124, 125, 126], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . Q<
  
  22:: 8 {'fen': '8/8/8/6k1/2K5/8/8/Q7 b - -', 'moves_to_mate': 8, 'parent': 1, 'color': False, 'result': 1, 'processed': True, 'sequence': [127], 'children': [127, 128, 129, 130, 131, 132, 133], 'to_end': 8}
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  Q . . . . . . .<
  
  23:: 8 {'fen': '7Q/8/8/3K2k1/8/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [135], 'children': [134, 135, 136, 137], 'to_end': 8}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . K . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  24:: 3 {'fen': '7Q/8/8/2K3k1/8/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [138], 'children': [138, 139, 140, 141], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . K . . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  25:: 3 {'fen': '7Q/8/8/1K4k1/8/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [142], 'children': [142, 143, 144, 145], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . K . . . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  26:: 12 {'fen': '7Q/8/8/6k1/3K4/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [147], 'children': [146, 147, 148, 149], 'to_end': 12}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . K . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  27:: 3 {'fen': '7Q/8/8/6k1/1K6/8/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [150], 'children': [150, 151, 152, 153], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . K . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  28:: 10 {'fen': '7Q/8/8/6k1/8/3K4/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [155], 'children': [154, 155, 156, 157], 'to_end': 10}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . . K . . . .
  . . . . . . . .
  . . . . . . . .<
  
  29:: 3 {'fen': '7Q/8/8/6k1/8/2K5/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [158], 'children': [158, 159, 160, 161], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  30:: 3 {'fen': '7Q/8/8/6k1/8/1K6/8/8 b - -', 'moves_to_mate': None, 'parent': 1, 'color': False, 'result': 0, 'processed': True, 'sequence': [162], 'children': [162, 163, 164, 165], 'to_end': 3}
  . . . . . . . Q
  . . . . . . . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . K . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  31:: 7 {'fen': '6Q1/8/7k/8/2K5/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [184], 'children': [166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . k
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  32:: 7 {'fen': '6Q1/8/5k2/8/2K5/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [202], 'children': [192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . k . .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  33:: 7 {'fen': '6Q1/8/8/7k/2K5/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [236], 'children': [218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . k
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  34:: 9 {'fen': '6Q1/8/8/5k2/2K5/8/8/8 w - -', 'moves_to_mate': 9, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [244], 'children': [244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269], 'to_end': 9}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . k . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  35:: 7 {'fen': '6Q1/8/8/8/2K4k/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [288], 'children': [270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . . k
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  36:: 7 {'fen': '6Q1/8/8/8/2K2k2/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 2, 'color': True, 'result': 1, 'processed': True, 'sequence': [296], 'children': [296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321], 'to_end': 7}
  . . . . . . Q .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . k . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  37:: 5 {'fen': '5Q2/8/6k1/8/2K5/8/8/8 w - -', 'moves_to_mate': None, 'parent': 3, 'color': True, 'result': 0, 'processed': True, 'sequence': [332], 'children': [322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350], 'to_end': 5}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . k .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  38:: 7 {'fen': '5Q2/8/8/7k/2K5/8/8/8 w - -', 'moves_to_mate': None, 'parent': 3, 'color': True, 'result': 0, 'processed': True, 'sequence': [363], 'children': [351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371], 'to_end': 7}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . k
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  39:: 7 {'fen': '5Q2/8/8/8/2K4k/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 3, 'color': True, 'result': 1, 'processed': True, 'sequence': [388], 'children': [372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392], 'to_end': 7}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . . k
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  40:: 2 {'fen': '5Q2/8/8/8/2K3k1/8/8/8 w - -', 'moves_to_mate': None, 'parent': 3, 'color': True, 'result': 0, 'processed': True, 'sequence': [408], 'children': [393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421], 'to_end': 2}
  . . . . . Q . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  41:: 9 {'fen': '4Q3/8/7k/8/2K5/8/8/8 w - -', 'moves_to_mate': 9, 'parent': 4, 'color': True, 'result': 1, 'processed': True, 'sequence': [434], 'children': [422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441], 'to_end': 9}
  . . . . Q . . .
  . . . . . . . .
  . . . . . . . k
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  42:: 2 {'fen': '4Q3/8/5k2/8/2K5/8/8/8 w - -', 'moves_to_mate': None, 'parent': 4, 'color': True, 'result': 0, 'processed': True, 'sequence': [443], 'children': [442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461], 'to_end': 2}
  . . . . Q . . .
  . . . . . . . .
  . . . . . k . .
  . . . . . . . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  43:: 7 {'fen': '4Q3/8/8/5k2/2K5/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 4, 'color': True, 'result': 1, 'processed': True, 'sequence': [477], 'children': [462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481], 'to_end': 7}
  . . . . Q . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . k . .
  . . K . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  44:: 7 {'fen': '4Q3/8/8/8/2K4k/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 4, 'color': True, 'result': 1, 'processed': True, 'sequence': [495], 'children': [482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499], 'to_end': 7}
  . . . . Q . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . . k
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  46:: 9 {'fen': '4Q3/8/8/8/2K2k2/8/8/8 w - -', 'moves_to_mate': 9, 'parent': 4, 'color': True, 'result': 1, 'processed': True, 
  
  51:: 7 {'fen': '3Q4/8/8/8/2K3k1/8/8/8 w - -', 'moves_to_mate': 7, 'parent': 5, 'color': True, 'result': 1, 'processed': True, 'sequence': [619], 'children': [614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631], 'to_end': 7}
  . . . Q . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . K . . . k .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .<
  
  EDIT MY CODE
  import chess
  
  def simplify_fen(fen):
      """Simplifies a FEN string by keeping only the position, turn, castling availability, and en passant target square."""
      return ' '.join(fen.split(' ')[:4])
  
  def add_descendants_iteratively(A, fen_to_node_id):
      queue = [(1, 0)]
      while queue:
          node_id, depth = queue.pop(0)
          board = chess.Board(A[node_id]['fen'] + " 0 1")
          for move in board.legal_moves:
              if move.promotion:
                  move = chess.Move(move.from_square, move.to_square, promotion=chess.QUEEN)
              board.push(move)
              simplified_fen = simplify_fen(board.fen())
              if simplified_fen not in fen_to_node_id:
                  new_node_id = len(A) + 1
                  A[new_node_id] = {
                      'fen': simplified_fen,
                      'moves_to_mate': None,
                      'parent': node_id,
                      'color': chess.WHITE if simplified_fen.split()[1] == 'w' else chess.BLACK,
                      'result': None,
                      'processed': False,
                      'sequence': [],
                      'children': [],
                      'to_end': None,
                  }
                  fen_to_node_id[simplified_fen] = new_node_id
                  A[node_id]['children'].append(new_node_id)
                  queue.append((new_node_id, depth + 1))
              board.pop()
  
  def initialize_game_tree(initial_fen):
      simplified_fen = simplify_fen(initial_fen)
      A = {1: {'fen': simplified_fen, 'moves_to_mate': None, 'parent': None,
               'color': chess.WHITE if initial_fen.split()[1] == 'w' else chess.BLACK,
               'result': None, 'processed': False, 'sequence': [], 'children': [], 'to_end': None}}
      fen_to_node_id = {simplified_fen: 1}
      return A, fen_to_node_id
  
  def update_game_outcomes(A):
      for key, node in A.items():
          board = chess.Board(node['fen'] + " 0 1")
          if board.is_game_over():
              outcome = board.outcome()
              node['processed'] = True
              if outcome.termination == chess.Termination.CHECKMATE:
                  node['result'] = 1 if outcome.winner == chess.WHITE else -1
                  node['moves_to_mate'] = 0
              else:
                  node['result'] = 0
                  node['moves_to_mate'] = None
              node['to_end'] = 0
  
  
  def update_parent_preferences(node_id, A):
      node = A[node_id]
      # Return the current values if the node has already been processed
      if node['processed']:
          return node['to_end'], node['moves_to_mate'], node['result']
  
      best_child_id = None
      # Initialize best_result with an appropriate value based on the color to play
      best_result = float('-inf') if node['color'] else float('inf')
      best_to_end = None
  
      for child_id in node['children']:
          child_to_end, child_moves_to_mate, child_result = update_parent_preferences(child_id, A)
          
          # Ensure comparisons are valid by checking for None
          if child_result is None:
              continue  # Skip this child because it has no result, indicating it's not a terminal node or not evaluated yet
  
          # Handle the logic for updating the best choice based on the color to play
          if node['color']:  # If it's White's turn to play
              if child_result > best_result or (child_result == best_result and (best_to_end is None or (child_to_end is not None and child_to_end < best_to_end))):
                  best_child_id = child_id
                  best_result = child_result
                  best_to_end = child_to_end
          else:  # If it's Black's turn to play
              if child_result < best_result or (child_result == best_result and (best_to_end is None or (child_to_end is not None and child_to_end > best_to_end))):
                  best_child_id = child_id
                  best_result = child_result
                  best_to_end = child_to_end
  
      # Update the current node based on the best child node found
      if best_child_id is not None:
          node['sequence'] = [best_child_id]
          node['result'] = best_result
          node['to_end'] = 1 + (best_to_end if best_to_end is not None else 0)
          node['moves_to_mate'] = None if node['result'] == 0 else 1 + (best_to_end if best_to_end is not None else 0)
      else:
          node['to_end'] = None
          node['moves_to_mate'] = None
  
      node['processed'] = True
      return node['to_end'], node['moves_to_mate'], node['result']
  
  
  # Note: This function assumes the presence of an evaluate_board function or similar logic
  # that sets the initial 'result' for leaf nodes where the game is over.
  
  
  
  
  def print_sequence_from_root(A):
      current_node_id = 1
      while True:
          node = A[current_node_id]
          board = chess.Board(node['fen'] + " 0 1")
          print(f"Node ID: {current_node_id}, Moves to Mate: {node['moves_to_mate']}, Result: {node['result']}")
          print(board, "\n")
          
          if not node['sequence']:
              break
          current_node_id = node['sequence'][0]
  
  def main():
      initial_fen = "7Q/8/8/6k1/2K5/8/8/8 w - - 0 1"
      A, fen_to_node_id = initialize_game_tree(initial_fen)
      add_descendants_iteratively(A, fen_to_node_id)
      update_game_outcomes(A)
      update_parent_preferences(1, A)
      print("Final Output:")
      print(A[1],"1*\n")
      print(A[15967],"15967*\n")
      print(A[31639],"31639*\n")
      print(A[31640],"31640\n")
      print(A[31641],"31641\n")
      print(A[3743],"3743*\n")
      print(A[1190],"1190*\n")
      print(A[101],"101*\n")
      print(A[17],"17*\n")
      print(A[70638],"70638*\n")
      print(A[106380],"106380*\n")
      print(A[165316],"165316*\n")
      
      print_sequence_from_root(A)
  
  #    for key in [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]:
      for key in range(1,1000000):
          if A[key]['to_end'] != None: # and A[key]['to_end'] > 9:
              print(f"{key}:: {A[key]['to_end']} {A[key]}\n{chess.Board(A[key]['fen'])}<\n")
  
          
  if __name__ == "__main__":
      main()
  
  **Tags:** engines, endgame, software, python
  
  > **Comments:**
  > **username_1**: How have you tried debugging?  
  > **username_0**: @username_1 Yes, I'm still trying it very intensively. No much results obtained so far.  
  > **username_0**: `Processing node 14 Node 14 is weaker player's node, aiming for min result. Checking child 90 of node 14. Updating best child for node 14 to 90 (weaker). Checking child 91 of node 14. Child 91 has no result, skipping. Checking child 92 of node 14. Child 92 has no result, skipping. Checking child 93 of node 14. Child 93 has no result, skipping. Node 14 updated with best child 90, result: 1, to_end: 10. Node 14 marked as processed. Final Output: {'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_m`  
  > **username_0**: I've partially identified the problem. There are 2 players stronger and weaker. The stronger player's primary goal is to capitalize on their advantage to secure a win as efficiently as possible. The weaker player aims to prolong the game, hoping for a mistake from the stronger player or finding an opportunity to level the playing field. The 'moves_to_end' should be selected larger for the weaker player, but I do not understand where to achieve this in my code.  
  > **username_1**: Please put the code in the question and get rid of the commented out code. Are you trying to construct a tablebase?  
  > **username_0**: @username_1 I'm trying to create an endgame Python code without relying on StockFish as an external source. I'm not creating any tablebase. I'll put my code in the question, if you wish so.  
  > **username_0**: @username_1 What I consider as the most important feature about how the code should work is that both player B and  W should play 100% optimally. The stronger player should reach a  win in as few moves as possible and the weaker one delay the checkmate as long as possible or even try to achieve a draw. The **stronger** player is provided to the code as an external information by the user.  
  > **username_0**: @username_1 Yet to your question, I'm trying to create a dictionary A populated by **all** the FEN strings that are obtainable by a legal move from an initial_fen of arbitrary depth. So in essence I'll keep in my RAM memory 64^D positions where D is the number of pieces left on the board in the endgame in question.Then from the terminal  nodes I'll climb up to A[1][,moves_to_mate'] which in my string should give the number 11 but it is giving to me 9, like as if B is not playing optimally.  
  > **username_1**: So that's pretty much creating a table base. do you know how those are constructed?  
  > **username_0**: @username_1 Not at all. I was not going to store anything in my HDD. I pursue a simpler task of populating my dictionary A with correct descendants of initial_fen and then enjoy looking at its play with fully optimal moves including this Reti [endgame](https://lichess.org/analysis/7K/8/k1P5/7p/8/8/8/8_w_-_-_0_1?color=white) for which there is a unique move of K playing to h8g7 which saves a draw for W with stronger second player Black.  
  > **username_1**: So you're just making a table base on the fly  
  > **username_0**: @username_1 Yes! Do you think I'll get help here for this project ?  
  > **username_2**: @username_0 do you have some kind of move ordering and score back propagation?  
  > **username_0**: @username_2 Your question should be read off from my code above. I don't think that there is some kind of move ordering and score back propagation. Feel free too edit my code and post the result back on pastebin.com Also, [here](https://www.kaggle.com/discussions/questions-and-answers/481444) is some kind of advice to my problem.  
  > **username_1**: I don't know if you'll get help. Debugging someone else's code beyond a certain complexity is no fun. I only do it for my job.  
  > **username_0**: A BREAKTHROUGH: the only node which is incorrectly computed is the A[1]['sequence']. The code gives [13] but when manually set to [14] **all other nodes are OK**. What's the reason for this and how can I fix it beyond setting it manually ? I'll assign my bounty even for this simple help. I.e. I mean *simple* with the advice given.  
  > **username_0**: Nobody interested in the Bounty ? :-)  
  > **username_2**: This epd `7Q/8/8/6k1/2K5/8/8/8 w - -` is mate in 6 moves. why are you saying `"'fen': '7Q/8/8/6k1/2K5/8/8/8 w - -', 'moves_to_mate': 11" according to Stock-Fish.` Perhaps you mean, `plies_to_mate: 11`  
  > **username_0**: @username_2 You are correct, I'm now used to mean by a move in fact a plie (or plye?).  
  
  ---
  
  ## 0 answers
  
  