how to make a chess board in python code example

Example 1: how to make a chess bot in python

from chess import Board, Move, STARTING_FEN

# an adjacency list
positions = {}

# depth-first search from a FEN string
def generate_tree(fen):
    board = Board(fen)
    legal_moves = list(board.legal_moves)
    if fen in positions:
        positions[fen] += legal_moves
    else:
        positions[fen] = legal_moves

    for move in legal_moves:
        board.push(move)
        next_fen = board.fen()
        board.pop()
        
        generate_tree(next_fen)

try:
    generate_tree(STARTING_FEN)
except RecursionError: 
    print(len(positions) + sum(len(p) for p in positions))

Example 2: chess in python

# If you haven't installed chess yet...
import os
os.system("pip install pychess")

# Else start here
import chess

# This is the start position
board = chess.Board()

# You can then play moves this way
board.push_san("e4")
board.push_san("Nc6")

# Get the fen this way...
print(board.fen())
# r1bqkbnr/pppppppp/2n5/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 1 2

# ...or print the board, ascii-style.
print(board)
# r . b q k b n r
# p p p p p p p p
# . . n . . . . .
# . . . . . . . .
# . . . . P . . .
# . . . . . . . .
# P P P P . P P P
# R N B Q K B N R