using System.Collections.Generic;
public static void Main()
Console.WriteLine("Simple Round Robin Generator");
Console.WriteLine("Ref: https://stackoverflow.com/q/77730901/1690217");
Console.WriteLine($"{Players} Players");
button_G_Click(null, EventArgs.Empty);
public static int Players { get;set; }
private static void button_G_Click(object sender, EventArgs e)
int StartFrom = players % 2 == 0 ? 1 : 0;
players = players % 2 == 0 ? players : players + 1;
int Rounds = players - 1;
List<Pair> pairs = new List<Pair>();
List<int> WasPairedThisRound = new();
bool ContainsPair(int player1, int player2)
foreach (Pair pair in pairs)
if ((pair.Player1 == player1 && pair.Player2 == player2) ||
(pair.Player1 == player2 && pair.Player2 == player1))
int completeTheSquare = players % 2 == 0 ? 2 : 3;
for (int round = 0; round < Rounds; round++)
foreach (var p1 in Enumerable.Range(1,players))
int startFrom = (p1 + round/completeTheSquare) % players;
if(StartFrom == 1) startFrom ++;
if (WasPairedThisRound.Count == players) { break; }
foreach (var p2 in Enumerable.Range(1,players))
int player2 = (p2 + ((round/completeTheSquare)*3)) % players;
if(StartFrom == 1) player2 ++;
if (WasPairedThisRound.Count == players) { break; }
if (player1 == player2) { continue; }
if (WasPairedThisRound.Contains(player1)) { continue; }
if (WasPairedThisRound.Contains(player2)) { continue; }
if (ContainsPair(player1, player2)) { continue; }
Pair p = new Pair(player1, player2);
WasPairedThisRound.Add(player1);
WasPairedThisRound.Add(player2);
Console.WriteLine($"{TotalPairs}. Round {round + 1}, game {Game}: [player {player1}] vs [player {player2}]");
if (WasPairedThisRound.Count == players) { break; }
Console.WriteLine(WasPairedThisRound.Count.ToString()); ;
WasPairedThisRound.Clear();
Console.WriteLine("Show each player matchup");
foreach(var player in Enumerable.Range(1,players))
Console.WriteLine($"Player: {player.ToString().PadLeft(2)}: {(String.Join(", ", pairs.Where(x => x.Player1 == player || x.Player2 == player).Select(x => (x.Player1 == player ? x.Player2 : x.Player1).ToString().PadLeft(2))))}");
public int Player1 { get; set; } = -1;
public int Player2 { get; set; } = -1;
public Pair(int p1, int p2)