Beispiel #1
0
        public void ScheduleNewRound(string tournamentName)
        {
            Tournament  selectedTournament = tournamentRepository.GetTournament(tournamentName);
            List <Team> teams     = null;
            Round       thisRound = null;

            //Get teams for new round
            int numberOfRounds = selectedTournament.GetNumberOfRounds();

            //Get teams for new round
            if (numberOfRounds == 0) //SPECIAL CASE: Initialize first round. Handled by adding all teams from tournament
            {
                teams = selectedTournament.Teams();
            }
            else // We are scheduling round > 0, get winners from this round if it is finished
            {
                thisRound = selectedTournament.GetRound(numberOfRounds);
                if (thisRound.IsRoundFinished())
                {
                    teams = thisRound.WinningTeams;
                    if (thisRound.FreeRider != null)
                    {
                        teams.Add(thisRound.FreeRider);
                    }
                }
                else
                {
                    throw new Exception("Round is not finished");
                }
            }

            if (teams.Count < 2)
            {
                selectedTournament.Status = Tournament.State.FINISHED;
            }
            else
            {
                Round newRound = new Round();

                //Manage freeRiders
                if ((teams.Count % 2) != 0) //Select a freeRider
                {
                    //If prior round exists and it had a freeRider and if was first team the select second team
                    if ((numberOfRounds > 0) && (thisRound.FreeRider != null) && (thisRound.FreeRider.Equals(teams[0])))
                    {
                        newRound.FreeRider = teams[1];
                        teams.Remove(teams[1]);
                    }
                    else //select first team
                    {
                        newRound.FreeRider = teams[0];
                        teams.Remove(teams[0]);
                    }
                }

                List <int> randomIndices = GetRandomIndices(teams.Count);
                int        noOfMatches   = teams.Count / 2;
                for (int i = 0; i < noOfMatches; i++)
                {
                    Match newMatch = new Match
                    {
                        FirstOpponent  = teams[randomIndices[2 * i]],
                        SecondOpponent = teams[randomIndices[2 * i + 1]]
                    };
                    newRound.AddMatch(newMatch);
                }
                selectedTournament.AddRound(newRound);
                ShowRound(tournamentName, newRound, numberOfRounds);

                if (newRound.FreeRider != null)
                {
                    teams.Add(newRound.FreeRider);
                }
            }
        }
Beispiel #2
0
        public void ScheduleNewRound(string tournamentName, bool printNewMatches = true)
        {
            Tournament tournament = tournamentRepository.GetTournament(tournamentName);

            tournament.SetupTestTeams(); // Bruges til at teste menuen, udkommenter ved test
            Round newRound = new Round();
            Match newMatch;

            List <Team> tempTeams;
            List <Team> newTeams = new List <Team>();

            int    numberOfRound   = tournament.GetNumberOfRounds();
            Round  lastRound       = null;
            Random random          = new Random();
            bool   isRoundFinished = true;
            Team   freeRider       = null;

            if (numberOfRound != 0)
            {
                numberOfRound--;
                lastRound       = tournament.GetRound(numberOfRound);
                isRoundFinished = tournament.GetRound(numberOfRound).IsMatchesFinished();
            }

            if (isRoundFinished)
            {
                if (lastRound != null)
                {
                    tempTeams = new List <Team>(tournament.GetRound(numberOfRound).GetWinningTeams());
                    if (tournament.GetRound(numberOfRound).FreeRider != null)
                    {
                        tempTeams.Add(tournament.GetRound(numberOfRound).FreeRider);
                    }
                }
                else
                {
                    tempTeams = new List <Team>(tournament.GetTeams());
                }

                while (tempTeams.Count >= 1)
                {
                    if (tempTeams.Count == 1)
                    {
                        freeRider = tempTeams[0];
                        tempTeams.RemoveAt(0);
                    }
                    else
                    {
                        newMatch = new Match();

                        Team team1 = tempTeams[0];
                        tempTeams.RemoveAt(0);

                        Team team2 = tempTeams[0];
                        tempTeams.RemoveAt(0);

                        newMatch.FirstOpponent  = team1;
                        newMatch.SecondOpponent = team2;
                        newTeams.Add(team1);
                        newTeams.Add(team2);
                        newRound.AddMatch(newMatch);
                    }
                }
                tournament.AddRound(newRound);
                tournament.GetRound(numberOfRound).SetFreeRider(freeRider);
            }

            if (printNewMatches == true)
            {
                Console.WriteLine("0-------------------------------------------0");
                PrintLine("Turnering: " + tournamentName);
                PrintLine("Runde: " + numberOfRound);
                PrintLine(newTeams.Count / 2 + " kampe");
                Console.WriteLine("0-------------------------------------------0");
                for (int i = 0; i < newTeams.Count; i++)
                {
                    PrintLine(PaddedText(newTeams[i].Name, 20) + " - " + PaddedText(newTeams[i + 1].Name, 20));
                    i++;
                }
                Console.WriteLine("0-------------------------------------------0");
                Console.ReadLine();
            }
        }