1

I'm quite familiar with how round robin works, now I'm trying to implement a function (Python works fine) to create round-robin matchings accounting for how many times during the tournament a player plays first.

Ideally, I would like to have everybody begin the same (±1 if even players) number of turns and have the function telling me that, without me making the adjustments manually.

Edit: this is my attempt so far, creating a discriminant to "randomize" somehow the first player, but in doesn't work in general

import random

def get_rr_rounds(players):
    """
    players = ["Player1", "Player2", "Player3","Player4", "Player5", "Player6"]
    """
    if len(players) % 2:
        players.append('DUMMY')

    n = len(players)

    matchs = []
    rounds = []

    discriminant = random.choice([0, 1])

    for round in range(1, n):
        for i in range(n//2):
            if (round % 2 == discriminant):
                matchs.append((players[i], players[n - 1 - i]))
            else:
                matchs.append((players[n - 1 - i], players[i]))
        players.insert(1, players.pop())
        rounds.append(matchs)
        matchs = []
    return rounds
huncletheo
  • 63
  • 6

1 Answers1

0

Since this is round-robin, everybody plays against everybody, so the round-robin algorithm that decides the order of the matches can be independent from the function that determines whether player i or player j starts when i plays against j.

So, just paint the table you use for results in a checkerboard pattern:

  0  1  2  3  4  5
0    <  ^  <  ^  <
1 ^     <  ^  <  ^
2 <  ^     <  ^  <
3 ^  <  ^     <  ^
4 <  ^  <  ^     <
5 ^  <  ^  <  ^   

Here the arrow points to the start player. This means that 0 starts when playing against 1, but is second when playing against 2, etc.

Turning this into an arithmetic formula:

When i plays against j, if i < j, then i starts iff j-i is odd.

(Note: you can replace j-i with i+j in the above formula if you want; because +i and -i have the same parity.)

Stef
  • 13,242
  • 2
  • 17
  • 28