import matplotlib.pyplot as plt
import networkx as nx
# Create a directed graph
G = nx.DiGraph()
# Recursive function to generate the war recursion tree with unique labels
def add_war_nodes(graph, parent, depth, max_depth):
if depth > max_depth:
return
# Generate unique battle names
left_battle = f"Battle-{depth}L"
right_battle = f"Battle-{depth}R"
# Add edges to represent battles branching from strategic decisions
graph.add_edge(parent, left_battle)
graph.add_edge(parent, right_battle)
# Recursively create deeper battle strategies
add_war_nodes(graph, left_battle, depth + 1, max_depth)
add_war_nodes(graph, right_battle, depth + 1, max_depth)
# Root node representing the beginning of war decisions
G.add_node("War Begins")
# Generate recursive war strategy tree
add_war_nodes(G, "War Begins", 1, 5) # Depth of recursion is 5
# Set up figure size
plt.figure(figsize=(14, 10))
# Recalculate positions for better label visibility
pos = nx.spring_layout(G, seed=42)
# Draw nodes and edges without labels to prevent overlap
nx.draw(G, pos, with_labels=False, node_color="black", edge_color="gray", node_size=200)
# Draw labels separately in red with a white background for clarity
for node, (x, y) in pos.items():
plt.text(x, y + 0.03, node, fontsize=9, ha='center', color="red",
bbox=dict(facecolor="white", edgecolor="none", boxstyle="round,pad=0.2"))
# Set the title of the plot
plt.title("The War of Shifting Fronts - Hidden Recursion Tree with Clear Labels")
# Show the final visualization
plt.show()