C#在18行中将18个对象中的2个对象相互配对

时间:2016-08-11 15:57:06

标签: c# list console-application fixture

我正在开发一个夹具创建的控制台应用程序项目,但是我遇到了一些小问题。我有Team类,它拥有Team属性。(名称等)。在这些属性中我有Rivals数组,它拥有团队的竞争对手确保它们不会再次匹配。(从这个数组中检查。)我创建了4个列表:

    List<Team> Home = new List<Team>(9); //Holds teams which play at Home
    List<Team> Away = new List<Team>(9); //Holds teams which play at Away

    List<Team> TempHomeList = new List<Team>(9); //Holds teams which play at home current week(purpose is swapping teams to Home to Away week to week.)
    List<Team> TempAwayList = new List<Team>(9); //Holds teams which play at away current week.

一开始,我将一半的团队分配到主页列表,另一半分配给Away List。当我尝试匹配它们时,程序运行到一周,但是在本周之后导致崩溃,程序仅输出那一周在无限循环中使用不同的赋值。这是我的代码和截图:

声明:

        int counter = 0;
        int[] HomeRandomHolder = new int[Home.Count];//holds random numbers 0-8
        int[] AwayRandomHolder = new int[Away.Count];//holds random numbers 0-8
        int[] ReplayRandomHolder = new int[MatchesPerWeek];//Holds random numbers 0-8.This array will show up for assignmets which will be after 17th week.
        Competition[,] matches = new Competition[WeekNumber, MatchesPerWeek];//Competition class holds two teams which will be play with each other.
        bool found=true;//checks if altenative way of match between teams have found.

代码:

for (int i = 0; i < WeekNumber / 2; i++) //There will be 34 weeks and I will assign first 17 weeks.(1st week's teams will be swapped Home to Team and will be assigned to 18.week.And it will go on so far until 34.week.)
        {
            Console.WriteLine("\nWeek:" + i);
            Console.WriteLine("-----------------------------");

            HomeCount = Home.Count;
            AwayCount = Away.Count;


            for (int j = 0; j < MatchesPerWeek; j++) // There will be 9 matches per week.
            {
                ReplayRandomHolderCounter = 0;
                matchCounter = 0;

                temporaryHome = Home[HomeRandomHolder[matchCounter] % HomeCount];
                temporaryAway = Away[AwayRandomHolder[matchCounter] % AwayCount];

                if (i == 0)
                {
                    if (j == 0) 
                    {
                        matches[i, j].FillCompetition(temporaryHome, temporaryAway);

                        temporaryHome.Rival[i] = temporaryAway;
                        temporaryAway.Rival[i] = temporaryHome;

                        TempHomeList.Add(temporaryHome);
                        TempAwayList.Add(temporaryAway);

                        Home.Remove(temporaryHome);
                        Away.Remove(temporaryAway);

                        HomeCount--;
                        AwayCount--;

                        matches[i + (WeekNumber / 2), ReplayRandomHolder[ReplayRandomHolderCounter]].FillCompetition(temporaryAway, temporaryHome);//FillCompetition function assigns two teams to match.
                    }
                    else 
                    {
                        counter = 0;

                        while (temporaryHome.Rival.Contains(temporaryAway) && temporaryAway.Rival.Contains(temporaryHome))
                        {
                            if (counter == Away.Count)
                            {
                                j--;
                                found = false;
                                break;
                            }
                            temporaryAway = Away[counter];
                            counter++;
                        }

                        if (found)
                        {
                            matches[i, j].FillCompetition(temporaryHome, temporaryAway);

                            temporaryHome.Rival[i] = temporaryAway;
                            temporaryAway.Rival[i] = temporaryHome;


                            TempHomeList.Add(temporaryHome);
                            TempAwayList.Add(temporaryAway);

                            Home.Remove(temporaryHome);
                            Away.Remove(temporaryAway);

                            HomeCount--;
                            AwayCount--;

                            matches[i + (WeekNumber / 2), ReplayRandomHolder[ReplayRandomHolderCounter]].FillCompetition(temporaryAway, temporaryHome);
                        }
                    }
                }

                else
                {

                    if (temporaryHome.Rival.Contains(temporaryAway) && temporaryAway.Rival.Contains(temporaryHome))
                    {
                        counter = 0;

                        while (temporaryHome.Rival.Contains(temporaryAway) && temporaryAway.Rival.Contains(temporaryHome))
                        {
                            if (counter == Away.Count)
                            {
                                j--;
                                found = false;
                                break;
                            }

                            temporaryAway = Away[counter];
                            counter++;
                        }

                        if (found)
                        {
                            matches[i, j].FillCompetition(temporaryHome, temporaryAway);

                            temporaryHome.Rival[i] = temporaryAway;
                            temporaryAway.Rival[i] = temporaryHome;

                            TempHomeList.Add(temporaryHome);
                            TempAwayList.Add(temporaryAway);

                            Home.Remove(temporaryHome);
                            Away.Remove(temporaryAway);

                            HomeCount--;
                            AwayCount--;

                            matches[i + (WeekNumber / 2), ReplayRandomHolder[ReplayRandomHolderCounter]].FillCompetition(temporaryAway, temporaryHome);
                        }

                        if (!found)   //I think this part causes the error
                        {
                            j = 0;
                            i--;

                            Home.AddRange(TempHomeList);
                            Away.AddRange(TempAwayList);

                            TempHomeList.Clear();
                            TempAwayList.Clear();

                            found = true;
                            break;
                        }

                    }

                    else
                    {
                        matches[i, j].FillCompetition(temporaryHome, temporaryAway);
                        temporaryHome.Rival[i] = temporaryAway;
                        temporaryAway.Rival[i] = temporaryHome;

                        TempHomeList.Add(temporaryHome);
                        TempAwayList.Add(temporaryAway);

                        Home.Remove(temporaryHome);
                        Away.Remove(temporaryAway);

                        HomeCount--;
                        AwayCount--;


                    }
                }

                Console.WriteLine(temporaryHome.name + " vs " + temporaryAway.name);//Prints competition teams
            }
            ShuffleHomeAndAway(ref TempHomeList, ref TempAwayList, ref Home, ref Away); //It shuffles Home and Away per week.


        }
    }

ShuffleHomeAndAway功能的代码:

public void ShuffleHomeAndAway(ref List<Team> TempHome, ref List<Team> TempAway, ref List<Team> Home, ref List<Team> Away)
    {
        Home.AddRange(TempAway);
        Away.AddRange(TempHome);

        TempHome.Clear();
        TempAway.Clear();
    }

输出:

Output

0 个答案:

没有答案