tournament_brackets

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2025 License: MIT Imports: 4 Imported by: 0

README

Go Tournament Bracket Generator Lib

A simple and flexible Go library for creating and managing single-elimination tournament brackets. This project also includes a fully interactive command-line application to run a tournament from start to finish.

What Is It?

This project provides two main components:

  • A Go Library: A set of data models and functions that you can import into your own Go applications to handle tournament logic programmatically. It correctly calculates rounds, match-ups, and byes for any number of participants.

  • An Interactive CLI: A runnable application (main.go) that uses the library to provide a step-by-step command-line interface.

  • You can enter participant names, and the app will generate a bracket and then prompt you for the winner of each match until a champion is crowned.

Features

  • Single-Elimination Brackets: Generates a standard single-elimination tournament structure.
  • Automatic Bye Handling: Automatically calculates and assigns byes for a number of participants that is not a power of two.
  • Interactive CLI: Comes with a ready-to-run terminal application to manage a tournament in real-time.
  • ASCII Bracket Display: Prints the current state of the tournament bracket to the console, so you can easily track matchups and winners.
  • Simple and Extensible: Built with clear and straightforward data models, making it easy to extend with new features.

Usage

As a Library

To use this package in your own Go project, you can install it with go get:

go get github.com/fezcode/go-tournament-brackets

Then, you can import and use it in your code:

package main

import (
    "fmt"
    "github.com/fezcode/go-tournament-brackets"
)

func main() {
participants := []string{"Team Alpha", "Team Bravo", "Team Charlie", "Team Delta", "Team Echo"}

    // Create a new tournament
    tourney, err := tournament.NewTournament("My Cup", tournament.SingleElimination, participants, nil)
    if err != nil {
        panic(err)
    }

    // Print the initial bracket state
    tourney.PrintAscii()

    // From here, you can programmatically set winners and advance them
    // For example, to set Team Alpha (ID: 1) as the winner of their first match:
    // tourney.SetWinner(roundIndex, matchIndex, 1)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorParticipantNotEnoughParticipants = errors.New("at least two participants are required to create a bracket")
	ErrorParticipantEmptyName             = errors.New("participant name is empty")
)

Functions

This section is empty.

Types

type Match

type Match struct {
	ID           int
	RoundIndex   int
	MatchIndex   int // Index of the match within its round
	Participant1 *Participant
	Participant2 *Participant
	Winner       *Participant
	NextMatch    *Match
}

Match represents a single matchup between two participants.

type Options

type Options struct {
	// SeedingRules is a function to define custom seeding logic. (Not yet implemented)
	SeedingRules func() error
}

Options defines configuration for tournament generation, like custom seeding rules.

type Participant

type Participant struct {
	ID   int
	Name string
}

Participant represents a competitor in the tournament.

func CreateParticipants

func CreateParticipants(names []string, shuffle bool) ([]*Participant, error)

CreateParticipants takes ordered slice of strings and assigns IDs in ascending order. Mutates the original names array, so duplicate it if you want to keep it as is. If shuffle is set then it uses math/rand to shuffle slice. Returns slice of Participants to be used in NewTournament function.

type Round

type Round struct {
	ID      int
	Matches []*Match
}

Round represents a single round in the tournament, containing one or more matches.

type Tournament

type Tournament struct {
	Name           string
	Rounds         []Round
	TournamentType TournamentType
	Options        *Options
}

Tournament is the root object that contains all data for a tournament event.

func NewTournament

func NewTournament(name string, tournamentType TournamentType, participants []*Participant, options ...Options) (*Tournament, error)

func (*Tournament) PrintAscii

func (t *Tournament) PrintAscii()

PrintAscii displays the current state of the tournament bracket in a text format.

func (*Tournament) SetWinner

func (t *Tournament) SetWinner(roundIndex, matchIndex, winnerID int) error

SetWinner sets the winner of a specific match and advances them to the next round.

type TournamentType

type TournamentType string

TournamentType defines the kind of tournament, e.g., single or double elimination.

const (
	SingleElimination TournamentType = "single-elimination"
	DoubleElimination TournamentType = "double-elimination" // TODO
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL