Forming a tournament table with LINQ (Fixture List)

前端 未结 3 1883
盖世英雄少女心
盖世英雄少女心 2020-12-30 18:39

I have an array of players (string[]) and now I need to get an array of pairs representing games (playerN-playerM) to orginize tournament table like at this picture:

3条回答
  •  别那么骄傲
    2020-12-30 18:54

    The implementation I really wanted:

    public static List>> ListMatches(List listTeam)
    {
        var result = new List>>();
    
        int numDays = (listTeam.Count - 1);
        int halfSize = listTeam.Count / 2;
        var teams = new List();
        teams.AddRange(listTeam.Skip(halfSize).Take(halfSize));
        teams.AddRange(listTeam.Skip(1).Take(halfSize - 1).ToArray().Reverse());
        int teamsSize = teams.Count;
    
        for (int day = 0; day < numDays; day++)
        {
            var round = new List>();
            int teamIdx = day % teamsSize;
            round.Add(new Tuple(teams[teamIdx], listTeam[0]));
    
            for (int idx = 1; idx < halfSize; idx++)
            {
                int firstTeam = (day + idx) % teamsSize;
                int secondTeam = (day + teamsSize - idx) % teamsSize;
    
                round.Add(new Tuple(teams[firstTeam], teams[secondTeam]));
            }
            result.Add(round);
        }
        return result;
    }
    

提交回复
热议问题