text_plain_output_1.png
import matplotlib.pyplot as plt

import networkx as nx

def knight_moves(pos):
    x, y = pos
    moves = [(x + 1, y + 2), (x + 1, y - 2), (x - 1, y + 2), (x - 1, y - 2), (x + 2, y + 1), (x + 2, y - 1), (x - 2, y + 1), (x - 2, y - 1)]
    return [(a, b) for a, b in moves if 0 <= a < 8 and 0 <= b < 8]

def create_chessboard_graph():
    chess_graph = nx.Graph()
    for x in range(8):
        for y in range(8):
            pos = (x, y)
            chess_graph.add_node(pos, pos=pos)
            for move in knight_moves(pos):
                chess_graph.add_edge(pos, move)
    return chess_graph

def draw_chessboard_graph(chess_graph):
    pos = nx.get_node_attributes(chess_graph, 'pos')
    nx.draw(chess_graph, pos, node_size=500, with_labels=True)
    nx.draw_networkx_labels(chess_graph, {x: (y[0], y[1] - 0.4) for x, y in pos.items()}, labels=dict(chess_graph.degree()), font_size=10)
    plt.show()
draw_chessboard_graph(create_chessboard_graph())