jeudi 28 avril 2016

C# Pairing The Elements of 2 Array For Once

I'm working on fixture project(still :) ) and trying to assign teams with each other.I have 18 teams and half of this is playing at home and the other half is playing at away.I created 2 structs.First struct is named "Teams" and holds the infos about each team(it's code is at below)

public struct Teams                        
    {
        public String name;                    
        public int[] day;                     //It's match day
        public int[] dayCount;                //Holds how much time the theam played match at days
        public bool isMatching;      
        public bool TeamPlace;                 //true=plays at home - false plays at away
        public bool isUefa;                    //Is team plays at Uefa
        public bool isCL;                      //Is team plays at CL
        public int extraMatchDay;              
        public int extraMatchWeek;             
        public bool extraMatchInfo;          //If team has extra match?
        public int[] rivals;                //holds team's previous rivals

        public void CreateArray()
        {
            day = new int[34];
            dayCount = new int[4];  //0->pzt  1->cuma   2->cmt   3->pzr
            rivals = new int[17];
            extraMatchWeek = 1; 

            for (int i = 0; i < rivals.Length; i++)
            {
                rivals[i] = -1;
            }
        }

        public void NULL()
        {
            name = null;
            TeamPlace = false;
            isUefa = false;
            isCL = false;
            Array.Clear(day, 0, day.Length);  //clear the day array
        }
    }

Second struct is named "matchedTeams" and holds the infos about the competitions.(it's code is at below)

public struct matchedTeams  //holds competitions
    {
        public Teams team1;
        public Teams team2;
        public int MatchDayDate; 

        public void CreateArrayFromTeams() //the objects from Teams struct which uses the function thet "CreateArray" from "Teams" struct encountered the NULLException error so I had to create them again.
        {
            team1.CreateArray();
            team2.CreateArray();
        }

        public void NULLmatched()
        {
            team1.NULL();
            team2.NULL();
            MatchDayDate = 0;

        }

        public void EqualTheDays(int i)
        {

            team1.day[i] = this.MatchDayDate;
            team2.day[i] = this.MatchDayDate;
        }
    }

And I seperated and assigned teams to 2 array."Home" and "Away" arrays.And assigned 2 teams from these 2 arrays(1 team from each array) to matchedTeams' "team1" and "team2".But I want that each team pairs once with each other.I tried it but I failed.My code is at below:

public static void ShuffleTeamsWithoutWeek(int weekDay, int[] randomHomeNumArray, int[] randomAwayNumArray, matchedTeams[,] weeklyMatched, matchedTeams[] matchTeams, Teams[] home, Teams[] away, Teams[] teams)
    {

        int randomHome; //It indicates the element which will be chosen from Home Array
        int randomAway; ////It indicates the element which will be chosen from Away Array

        Random randomNum = new Random();

        for (int j = 0; j < weekDay; j++)
        {
            for (int i = 0; i < weeklyMatched.GetLength(1); i++)
            {
                weeklyMatched[j, i].CreateArrayFromTeams(); //weeklyMatched is a 2 dimensional array instantiated from matchedTeams struct and it's size is weeklyMatched[34,9]
            }
        }

        for (int week = 0; week < weekDay; week++)
        {

            if (week < weekDay / 2)
            {

                for (int i = 0; i < 9; i++)   //It equals the elements of 2 arrays to -1
                {
                    randomHomeNumArray[i] = -1;   //Holds the random numbers for Home array
                    randomAwayNumArray[i] = -1;  //Holds the random numbers for Away array
                }

                for (int homeArrayCounter = 0; homeArrayCounter < randomHomeNumArray.Length; homeArrayCounter++)
                {
                    randomHome = randomNum.Next(home.Length);    

                    if (!randomHomeNumArray.Contains(randomHome))
                    {
                        randomHomeNumArray[homeArrayCounter] = randomHome;   
                        //Console.WriteLine(homeArrayCounter + ". iterasyon in Home " + randomHomeNumArray[homeArrayCounter]); 
                    }
                    else
                    {
                        homeArrayCounter--;
                    }
                }
                for (int awayArrayCounter = 0; awayArrayCounter < randomAwayNumArray.Length; awayArrayCounter++) 
                {
                    randomAway = randomNum.Next(randomAwayNumArray.Length);    

                    if (!randomAwayNumArray.Contains(randomAway))
                    {
                        randomAwayNumArray[awayArrayCounter] = randomAway;     
                    }
                    else
                    {
                        awayArrayCounter--;
                    }
                }
                int RivalCounter = 1;

                for (int c = 0; c < 9; c++) //There are 9 matches each week
                {
                    if (weeklyMatched[week, c].team1.isMatching == false)  //choose a team from unchosen ones
                    {
                        weeklyMatched[week, c].team1 = home[randomHomeNumArray[c]];

                        weeklyMatched[week, c].team1.isMatching = true;
                    }

                    if (weeklyMatched[week, c].team2.isMatching == false)   choose the second team from unchosen ones
                    {
                        weeklyMatched[week, c].team2 = away[randomAwayNumArray[c]];

                        if (weeklyMatched[week, c].team1.rivals[RivalCounter] != Array.IndexOf(teams, (weeklyMatched[week, c].team2))) //if team 1 didn't encountered with team2 before
                        {
                            weeklyMatched[week, c].team1.rivals[RivalCounter] = Array.IndexOf(teams, (weeklyMatched[week, c].team2));//assign team as team2
                            weeklyMatched[week, c].team2.isMatching = true; 

                        }
                        else {
                            RivalCounter--; //I'm not sure about this line
                        }
                        RivalCounter++;
                    }
                }
            }
        }
    }

The output is here: Output Code

Thanks for your kind helps.

Aucun commentaire:

Enregistrer un commentaire