Round-Robin Scheduling: Generating fixtures for Zone01 Kisumu's Go Foosball League
Tags: go, fixtures, rotationlogic, softwaredevelopment
Published: 2024-06-21 08:39:37

**Introduction**

This algorithm uses a round-robin approach, a standard method for generating fixtures in tournaments.

The Go Foosball League, presented at the #GophersKisumu Meetup on June 15th, 2024, solves the challenges faced by our community with manual management of fixtures. We were looking for a fair, efficient and automated solution that minimised human intervention. Here is how we created this fixture generating system.

**The Problem**
Our Go Foosball League was manually managed by a single person, leading to several issues:

- Bias in Team Formation: Players often complained about unfair team pairings.
- Scheduling Errors: Manual scheduling resulted in conflicts and errors.
- Inefficiency: The process was time-consuming and prone to human error.

To address these challenges, we needed a solution to automate the management process, ensuring fairness and reducing the potential for errors.

**The Solution**
The Go Foosball League has the following core features:

- Automated Team Formation: A randomization algorithm pairs strikers and defenders fairly.
- Fixture Generation: Automatically creates balanced and unbiased match schedules.
- Web Interface: A user-friendly web platform for players to view teams, fixtures, and other league details.

**Technical Overview**
The system is built with #Go, leveraging its efficiency and concurrency capabilities. Here's a high-level overview of the architecture:
- Backend: Handles core logic for team formation and fixture generation.
- HTTP Handlers: Manage functionalities like the home page, team list, and fixtures.
- Database Integration: Planned for future development to store player stats and match results.
- Web Frontend: Under development to provide an intuitive user interface.

**Core Functionalities**
**Shuffling Players**
The shuffle algorithm in our Go Foosball League randomizes the order of players to ensure that the team formations are fair and unbiased.

**Step-by-Step Breakdown**

- Seeding the Random Number Generator:
The algorithm starts by seeding the random number generator with the current time in nanoseconds.
This ensures that each run of the algorithm produces a different random sequence.

- Shuffling the Array:
The rand.Shuffle function is used to randomize the elements in the array.
The function swaps elements at random indices to achieve a shuffled order.
The shuffle algorithm is applied to the list of defenders to randomize their order.

- Forming Teams:
Teams are formed by pairing each striker with a randomly selected defender.
The GenerateString function creates unique team names based on the paired players.

```
func Shuffle(arr []string) []string { 
rand.New(rand.NewSource(time.Now().UnixNano())) 
rand.Shuffle(len(arr), func(i, j int) { arr[i], arr[j] = arr[j], arr[i] }) 
return arr }
```
**Fixture Generation**
The Fixture function creates a balanced match schedule, ensuring all teams play against each other without duplication.

```
func Fixture(teams []types.Teams) [][]string {
	n := len(teams)
	if (n % 2 != 0) {
    	teams = append(teams, types.Teams{Name: "", Striker: "", Defender: ""})
    	n += 1
	}

	fixtures := [][]string{}
	for round := 1; round < n-1; round++ {
    	match_list := []string{}
    	for match := 0; match < n/2; match++ {
        	home := (round + match) % (n - 1)
        	away := (n - 1 - match + round) % (n - 1)
        	if match == 0 {
            	away = n - 1
        	}
        	if !(teams[away].Name == "" || teams[home].Name == "") {
            	match_list = append(match_list, teams[home].Name + " vs " + teams[away].Name + " \n")
        	}
    	}
    	fixtures = append(fixtures, match_list)
	}

	return fixtures
}

```

**The Algorithm**
The fixture generation algorithm in our Go Foosball League is designed to handle an even number of teams. If there’s an odd number of teams, the algorithm first adds a dummy team (a team with no players) to make the count even. The algorithm ensures every round has the same number of matches.

**Step-by-Step Breakdown**
Here’s a detailed breakdown of how the algorithm works:

- Input Handling:
The algorithm accepts a list of teams.
If the number of teams is odd, it appends a dummy team to make the total even.

- Round-Robin Scheduling:
The algorithm uses a round-robin approach, a standard method for generating fixtures in tournaments.
It ensures each team plays against every other team exactly once.

- Fixture Creation:
For each round, it determines the matchups by rotating the positions of teams.
It pairs teams in a way that minimizes repetition and ensures fairness.

- Rotation Logic:
In each round, the first team stays fixed, and the other teams rotate positions.
This rotation ensures that every team eventually plays against all other teams.

- Avoiding Duplicate Matches:
The algorithm carefully tracks which teams have already played against each other.
It generates a new list of matches for each round without duplication.


![A screenshot of the output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vd2e4ivycq6amu009k8.png)

**Future Work**
Future plans include:

- Database Integration: For persistent storage of player stats and match results.
- Enhanced Web Interface: Improve user experience and add more features.

- Expand Functionality: Include table updating and detailed player statistics.

Check out the full code on my [Github](https://github.com/bravian1)

By Bravian Nyatoro