Untitled

 avatar
unknown
plain_text
a month ago
2.0 kB
2
Indexable
import matplotlib.pyplot as plt

def plot_bracket(players, title):
    """Generate a single-elimination tournament bracket."""
    num_players = len(players)
    
    # Determine byes (if needed)
    next_power_of_2 = 1
    while next_power_of_2 < num_players:
        next_power_of_2 *= 2
    num_byes = next_power_of_2 - num_players

    # Assign byes
    full_bracket = players + ["Bye"] * num_byes

    # Generate rounds
    rounds = [full_bracket]
    while len(full_bracket) > 1:
        next_round = []
        for i in range(0, len(full_bracket), 2):
            match = f"Winner of {full_bracket[i]} vs {full_bracket[i+1]}"
            next_round.append(match)
        full_bracket = next_round
        rounds.append(full_bracket)

    # Create figure
    fig, ax = plt.subplots(figsize=(10, len(rounds[0]) * 0.7))
    ax.set_xlim(0, len(rounds) - 1)
    ax.set_ylim(-len(rounds[0]) // 2, 1)

    # Draw bracket rounds
    for round_idx, round_matches in enumerate(rounds):
        for match_idx, match in enumerate(round_matches):
            ax.text(round_idx, -match_idx, match, ha="center", va="center",
                    bbox=dict(facecolor="white", edgecolor="black"), fontsize=10)

    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_frame_on(False)
    plt.title(title, fontsize=14, fontweight="bold")
    plt.show()

# Example player lists
players_4 = ["Anna", "Brian", "Carla", "David"]
players_5 = ["Emily", "Jack", "Kevin", "Lily", "Mark"]
players_6 = ["Nathan", "Olivia", "Paul", "Quinn", "Ryan", "Sarah"]
players_8 = ["Andrew", "Betty", "Charles", "Diana", "Ethan", "Fiona", "George", "Hannah"]

# Generate and display clear single-elimination brackets
plot_bracket(players_4, "Single Elimination Bracket - 4 Players")
plot_bracket(players_5, "Single Elimination Bracket - 5 Players (with Byes)")
plot_bracket(players_6, "Single Elimination Bracket - 6 Players (with Byes)")
plot_bracket(players_8, "Single Elimination Bracket - 8 Players")
Editor is loading...
Leave a Comment