how to make a chess bot in python

Solutions on MaxInterview for how to make a chess bot in python by the best coders in the world

showing results for - "how to make a chess bot in python"
Fabiana
09 Sep 2016
1from chess import Board, Move, STARTING_FEN
2
3# an adjacency list
4positions = {}
5
6# depth-first search from a FEN string
7def generate_tree(fen):
8    board = Board(fen)
9    legal_moves = list(board.legal_moves)
10    if fen in positions:
11        positions[fen] += legal_moves
12    else:
13        positions[fen] = legal_moves
14
15    for move in legal_moves:
16        board.push(move)
17        next_fen = board.fen()
18        board.pop()
19        
20        generate_tree(next_fen)
21
22try:
23    generate_tree(STARTING_FEN)
24except RecursionError: 
25    print(len(positions) + sum(len(p) for p in positions))