Exemplo n.º 1
0
        /// <summary>
        /// Starts the football <see cref="Cup" />. Execution intended in console mode.
        /// </summary>
        /// <returns>
        /// The winner <see cref="Team" />.
        /// </returns>
        /// <exception cref="Exception">Not enough teams or referees in a cup.
        /// or
        /// Not even number of teams in a cup.</exception>
        public Team StartFootballCup()
        {
            if (isFinished)
            {
                throw new Exception("Cup already played out.");
            }
            int teamsInCup    = listOfTeamsInCup.Count();
            int refereesInCup = listOfRefereesInCup.Count();

            if (teamsInCup < 8 && refereesInCup < 3)
            {
                throw new Exception("Not enough teams or referees in a cup.");
            }
            if (teamsInCup % 2 != 0)
            {
                throw new Exception("Not even number of teams in a cup.");
            }
            if (teamsInCup != 8 & teamsInCup != 16 & teamsInCup != 24)
            {
                throw new Exception("Need to be 8, 16 or 24 teams for a cup.");
            }
            int xx = 1;
            int yy = listOfTeamsInCup.Count();

            while ((yy / 2) > 1)
            {
                xx++;
                yy /= 2;
            }
            for (int h = 0; h < xx; h++)
            {
                List <Team> tempList = new List <Team>(listOfTeamsInCup);
                for (int i = 0; i < teamsInCup / 2; i++)
                {
                    TwoTeams teams = RandomTeamsForMatch(tempList);
                    Team     teamA = teams.GetHostTeam;
                    Team     teamB = teams.GetGuestTeam;

                    ThreeReferees referees    = RandomRefereesForMatch();
                    Referee       mainReferee = referees.GetMainReferee;
                    Referee       suppRef1    = referees.GetSupportingReferee1;
                    Referee       suppRef2    = referees.GetSupportingReferee2;

                    FootballMatch footballMatch = new FootballMatch(mainReferee, teamA, teamB, suppRef1, suppRef2);
                    matchEngine.SimulateFootballMatch(ref footballMatch, true);
                    if (footballMatch.GetScoreOfTeamA() > footballMatch.GetScoreOfTeamB())
                    {
                        listOfTeamsInCup.Remove(teamB);
                    }
                    else if (footballMatch.GetScoreOfTeamB() > footballMatch.GetScoreOfTeamA())
                    {
                        listOfTeamsInCup.Remove(teamA);
                    }
                    tempList.Remove(teamA);
                    tempList.Remove(teamB);
                }
                teamsInCup /= 2;
            }
            listOfTeamsInCup.ElementAt(0).AddTournamentWon();
            isFinished = true;
            return(listOfTeamsInCup.ElementAt(0));
        }