public static void CreateRounds(TournamentModel tournament) { List <TeamModel> participants = tournament.ParticipatingTeams; participants.Shuffle(); if (tournament.IsLeague) { // SOLUTION FOUND ON https://stackoverflow.com/questions/1293058/round-robin-tournament-algorithm-in-c-sharp int numberOfRounds = GetNumberOfRounds(tournament); int halfOfTeams = tournament.ParticipatingTeams.Count / 2; List <TeamModel> shortenedTeamList = new List <TeamModel>(); shortenedTeamList.AddRange(participants); shortenedTeamList.RemoveAt(0); int shortenedCount = shortenedTeamList.Count; // Create rounds for (int roundNumber = 1; roundNumber <= numberOfRounds; roundNumber++) { // Create round RoundModel round = new RoundModel(tournament.Id, roundNumber); SqlDataHandler.CreateRound(tournament, round); tournament.Rounds.Add(round); int teamIndexer = roundNumber % shortenedCount; // Create 1st game in the round GameModel game = new GameModel(tournament.Id, round.Id, true); SqlDataHandler.CreateGame(game); round.Games.Add(game); // Create 1st game participants GameParticipantModel homeTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = round.Id, GameId = game.Id, TeamName = shortenedTeamList[teamIndexer].TeamName }; GameParticipantModel awayTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = round.Id, GameId = game.Id, TeamName = participants[0].TeamName }; game.Competitors.Add(homeTeam); game.Competitors.Add(awayTeam); foreach (GameParticipantModel participant in game.Competitors) { SqlDataHandler.CreateGameParticipant(participant); } // Create other games of the round for (int index = 1; index < halfOfTeams; index++) { int homeIndex = (roundNumber + index) % shortenedCount; int awayIndex = (roundNumber + shortenedCount - index) % shortenedCount; // Create the next game GameModel nextGame = new GameModel(tournament.Id, round.Id, true); SqlDataHandler.CreateGame(nextGame); round.Games.Add(nextGame); // Create next game participants GameParticipantModel nextHomeTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = round.Id, GameId = nextGame.Id, TeamName = shortenedTeamList[homeIndex].TeamName }; GameParticipantModel nextAwayTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = round.Id, GameId = nextGame.Id, TeamName = shortenedTeamList[awayIndex].TeamName }; nextGame.Competitors.Add(nextHomeTeam); nextGame.Competitors.Add(nextAwayTeam); foreach (GameParticipantModel participant in nextGame.Competitors) { SqlDataHandler.CreateGameParticipant(participant); } } } if (tournament.HomeAndAway) { int numberToAdd = 1; // Create the duplicate rounds and duplicate of every game in the round with teams on opposite places foreach (RoundModel createdRound in tournament.Rounds) { // Create the 1st next duplicate round RoundModel nextRound = new RoundModel(tournament.Id, tournament.Rounds.Count + numberToAdd); SqlDataHandler.CreateRound(tournament, nextRound); numberToAdd += 1; foreach (GameModel createdGame in createdRound.Games) { // Create duplicate game GameModel nextGame = new GameModel(tournament.Id, nextRound.Id, true); SqlDataHandler.CreateGame(nextGame); // Create game participants GameParticipantModel homeTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = nextRound.Id, GameId = nextGame.Id, TeamName = createdGame.Competitors[1].TeamName }; GameParticipantModel awayTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = nextRound.Id, GameId = nextGame.Id, TeamName = createdGame.Competitors[0].TeamName }; nextGame.Competitors.Add(homeTeam); nextGame.Competitors.Add(awayTeam); foreach (GameParticipantModel participant in nextGame.Competitors) { SqlDataHandler.CreateGameParticipant(participant); } } } } } else { int numberOfRounds = GetNumberOfRounds(tournament); for (int roundNumber = 1; roundNumber <= numberOfRounds; roundNumber++) { RoundModel round = new RoundModel(tournament.Id, roundNumber); SqlDataHandler.CreateRound(tournament, round); if (round.RoundNumber == 1) { CreateCupRoundGames(participants, round); } // Every next round's games will be created subsequently after all previous round's results are stored // Its only needed to get previous round winners and pass them as parameter to the CreateCupRoundGames() method } } }