using System.Collections.Generic;
public static void Main(string[] args)
Program prg = new Program();
prg.PrepareMatches(nTeams);
private void PrepareMatches(int nTeams)
ITournament trmnt = new ProKabaddiTournament();
ITournament trmntWithCons = new ProKabaddiTournamentWithConstraints();
List<Match> baseMatches = trmnt.PrepareMatches(nTeams);
trmntWithCons.BaseTournament = baseMatches;
List<Match> matches = trmntWithCons.PrepareMatches(nTeams);
foreach (Match mth in matches)
Console.WriteLine(mth.MatchName + " MatchDay_@Home: " + mth.MatchDayHome + " MatchDay_@Away: " + mth.MatchDayAway);
public string MatchName { get; set; }
public int MatchDayHome { get; set; }
public int MatchDayAway { get; set; }
public int TeamA { get; set; }
public int TeamB { get; set; }
public DateTime MatchDateHome { get; set; }
public DateTime MatchDateAway { get; set; }
public string MatchLocationHome { get; set; }
public string MatchLocationAway { get; set; }
public interface ITournament
List<Match> BaseTournament { get; set; }
List<Match> PrepareMatches(int nTeams);
public class ProKabaddiTournament : ITournament
public List<Match> BaseTournament
{ get { return sch; } set { sch = value; } }
public List<Match> PrepareMatches(int nTeams)
RemoveDummyTeamMatch(nTeams, ref sch);
private void RemoveDummyTeamMatch(int nTeams, ref List<Match> matches)
int teams = (nTeams % 2 == 1 ? nTeams + 1 : nTeams);
string dummyTeam = "Team" + teams.ToString();
matches.RemoveAll(m => m.MatchName.Contains(dummyTeam) == true);
private void DrawMatches(int nTeams)
int teams = (nTeams % 2 == 1 ? nTeams + 1 : nTeams);
string[] sideA = new string[teams / 2];
string[] sideB = new string[teams / 2];
for (int i = 0; i < teams; i = i + 2)
sideA[y] = "Team" + (i + 1).ToString();
sideB[y] = "Team" + (i + 2).ToString();
for (int i = 0; i < teams - 1; i++)
SetContest(teams, sideA, sideB);
ReArrangeSides(teams / 2, ref sideA, ref sideB);
private void SetContest(int teams, string[] sideA, string[] sideB)
for (int i = 0; i < (teams / 2); i++)
Match contest = new Match();
contest.MatchName = sideA[i] + " vs " + sideB[i];
contest.TeamA = Convert.ToInt32(sideA[i].Replace("Team", ""));
contest.TeamB = Convert.ToInt32(sideB[i].Replace("Team", ""));
private void ReArrangeSides(int size, ref string[] sideA, ref string[] sideB)
string temp = sideA[size - 1];
for (int i = size - 1; i > 1; i--)
for (int i = 0; i < size - 1; i++)
public class ProKabaddiTournamentWithConstraints : ITournament
public List<Match> BaseTournament
{ get { return sch; } set { sch = value; } }
public List<Match> PrepareMatches(int nTeams)
List<Match> sch = this.sch;
DrawMatchDays(nTeams, ref sch);
public List<Match> Tournament
private void DrawMatchDays(int nTeams, ref List<Match> matches)
int[,] match_matrix = DrawDays(nTeams);
foreach (Match match in matches)
match.MatchDayHome = match_matrix[r - 1, c - 1];
match.MatchDayAway = match_matrix[c - 1, r - 1];
private int[,] DrawDays(int nTeams)
int day_limit = nTeams < 7 ? 1 : 2;
int n_matches = (nTeams * (nTeams - 1)) / 2;
int[,] matches = new int[nTeams, nTeams];
int curr_day_matches = 0;
while (m_day <= n_matches + 10)
for (int r = 0; r < nTeams; r++)
for (int c = r + 1; c < nTeams; c++)
if (IsAvailable(m_day, r, c, nTeams, ref matches))
if (curr_day_matches == day_limit)
for (int r = 0; r < nTeams; r++)
for (int c = 0; c < r; c++)
matches[r, c] = matches[c, r] + m_day + 1;
private bool IsAvailable(int day, int r, int c, int nTeams, ref int[,] matches)
int prev_day = day == 1 ? day : day - 1;
for (int i = 0; i < nTeams; i++)
if (matches[i, c] == day || matches[i, c] == prev_day)
else if (matches[c, i] == day || matches[c, i] == prev_day)
else if (matches[r, i] == day || matches[r, i] == prev_day)
else if (matches[i, r] == day || matches[i, r] == prev_day)