3

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()
19
  • 1
    How have you tried debugging?
    – qwr
    Commented Feb 29 at 21:28
  • @qwr Yes, I'm still trying it very intensively. No much results obtained so far. Commented Feb 29 at 21:41
  • 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 Commented Feb 29 at 21:44
  • 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. Commented Feb 29 at 22:21
  • 1
    So you're just making a table base on the fly
    – qwr
    Commented Mar 3 at 21:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.