形成锦标赛表LINQ(预定列表) [英] Forming a tournament table with LINQ (Fixture List)

查看:156
本文介绍了形成锦标赛表LINQ(预定列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对球员的数组(字符串[]),现在我需要得到一个数组的对重presenting游戏(playerN-playerM),以orginize赛事表像在这样的画面:

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:

的期望的最终结果是产生一个固定件列表的所有需要​​播放的游戏。

The desired end result is to generate a fixture list with all the games the need to be played.

我怎样才能做到这一点使用LINQ的有效的方式?

How can I do this with LINQ in efficient way?

更新: AB,AC,AD是不正确的 - 游戏应该能够并行运行。 我需要导致相同的顺序在图片

UPDATED: A-B, A-C, A-D is not correct - games should be able to run in parallel. I need result in the same order as at the picture

推荐答案

以下code可被用来生成一个固定列表团队的集合,以确保每一次播放所有其他球队在1家和1客场比赛。

The following code can be used to generate a fixture list for a collection of teams to ensure that each time plays all other teams in 1 home and 1 away match.

在code是一个有点长篇大论,但它的工作,通过为您提供在您指定的顺序列表。

The code is a bit long winded but it does work by providing you with a list in the order you have specified.

在code大概可以优化,但在目前,这是它是如何来自我的头。

The code can probably be optimised but at the moment this is how it has come from my head.

注意:结果列表将包含主客场夹具,其中基于网格会是什么,你需要做的呢

NOTE: The resulting list will contain both Home and Away fixture, which based on your grid will be what you need to do anyway.

    class Fixture
    {
        public string Home { get; set; }
        public string Away { get; set; }
    }

    void CallCode()
    {
        string players = new string[] { "A", "B", "C", "D" };
        List<Fixture> fixtures = CalculateFixtures(players);
    }

    List<Fixture> CalculateFixtures(string[] players)
    {
        //create a list of all possible fixtures (order not important)
        List<Fixture> fixtures = new List<Fixture>();
        for (int i = 0; i < players.Length; i++)
        {
            for (int j = 0; j < players.Length; j++)
            {
                if (players[i] != players[j])
                {
                    fixtures.Add(new Fixture() { Home = players[i], Away = players[j] });
                }
            }
        }

        fixtures.Reverse();//reverse the fixture list as we are going to remove element from this and will therefore have to start at the end

        //calculate the number of game weeks and the number of games per week
        int gameweeks = (players.Length - 1) * 2;
        int gamesPerWeek = gameweeks / 2;

        List<Fixture> sortedFixtures = new List<Fixture>();

        //foreach game week get all available fixture for that week and add to sorted list
        for (int i = 0; i < gameweeks; i++)
        {
            sortedFixtures.AddRange(TakeUnique(fixtures, gamesPerWeek));
        }

        return sortedFixtures;
    }

    List<Fixture> TakeUnique(List<Fixture> fixtures, int gamesPerWeek)
    {
        List<Fixture> result = new List<Fixture>();

        //pull enough fixture to cater for the number of game to play
        for (int i = 0; i < gamesPerWeek; i++)
        {
            //loop all fixture to find an unused set of teams
            for (int j = fixtures.Count - 1; j >= 0; j--)
            {
                //check to see if any teams in current fixtue have already been used this game week and ignore if they have
                if (!result.Any(r => r.Home == fixtures[j].Home || r.Away == fixtures[j].Home || r.Home == fixtures[j].Away || r.Away == fixtures[j].Away))
                {
                    //teams not yet used
                    result.Add(fixtures[j]);
                    fixtures.RemoveAt(j);
                }
            }
        }

        return result;
    }

这篇关于形成锦标赛表LINQ(预定列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆