2

我正在开发一个夹具创建的控制台应用程序项目,但我有一些小问题。我有包含团队属性的 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.

一开始,我将一半的球队分配到主场名单,另一半分配到客场名单。当我尝试匹配他们时,程序运行到某个星期,但是在这周之后导致崩溃,程序只在无限循环中输出那一周具有不同的任务。这是我的代码和屏幕截图:

声明:

        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();
    }

输出:

输出

4

0 回答 0