public void ScheduleNewRound(string tournamentName, bool printNewMatches = true)
        {
            Tournament tournament = tournamentRepository.GetTournament(tournamentName);

            tournament.SetupTestTeams(); // Setup 8 teams
            Round newRound = new Round();
            Match newMatch;

            List <Team> tempTeams = new List <Team>(tournament.GetTeams());
            List <Team> newTeams  = new List <Team>();

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

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

            if (isRoundFinished)
            {
                if (lastRound != null)
                {
                    tempTeams = tournament.GetRound(numberOfRound).GetWinningTeams();
                    tempTeams.Add(tournament.GetRound(numberOfRound).FreeRider);
                }

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

                        int  rndNumber1 = rnd.Next(tempTeams.Count);
                        Team team1      = tempTeams[rndNumber1];
                        tempTeams.RemoveAt(rndNumber1);

                        int  rndNumber2 = rnd.Next(tempTeams.Count);
                        Team team2      = tempTeams[rndNumber2];
                        tempTeams.RemoveAt(rndNumber2);

                        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 + 1);
                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();
            }
        }
Exemplo n.º 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();

                        // Da unittesten ikke tager højde for random genererede kampe, kan dette ikke gøres
                        //int randomNumber1 = random.Next(tempTeams.Count);
                        //Team team1 = tempTeams[randomNumber1];
                        //tempTeams.RemoveAt(randomNumber1);

                        //int randomNumber2 = random.Next(tempTeams.Count);
                        //Team team2 = tempTeams[randomNumber2];
                        //tempTeams.RemoveAt(randomNumber2);

                        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();
            }
        }
Exemplo n.º 3
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();
                    newMatch.FirstOpponent  = teams[randomIndices[2 * i]];
                    newMatch.SecondOpponent = teams[randomIndices[2 * i + 1]];
                    newRound.AddMatch(newMatch);
                }
                selectedTournament.AddRound(newRound);
                ShowRound(tournamentName, newRound, numberOfRounds);
            }
        }
Exemplo n.º 4
0
        public void ScheduleNewRound(string tournamentName, bool printNewMatches = true)
        {
            Tournament t = tournamentRepository.GetTournament(tournamentName);
            int        numberOfRounds = t.GetNumberOfRounds();

            Round       lastRound;
            bool        isRoundFinished;
            List <Team> teams = new List <Team>();
            Team        oldFreeRider;
            Team        newFreeRider = null;
            List <Team> scramble;

            if (numberOfRounds == 0)
            {
                lastRound       = null;
                isRoundFinished = true;
            }
            else
            {
                lastRound       = t.GetRound(numberOfRounds - 1);
                isRoundFinished = lastRound.IsMatchesFinished();
            }
            if (isRoundFinished == true)
            {
                if (lastRound == null)
                {
                    teams = t.GetTeams();
                }
                else
                {
                    teams = lastRound.GetWinningTeams();
                    if (lastRound.FreeRider != null)
                    {
                        teams.Add(lastRound.FreeRider);
                    }
                }
                if (teams.Count > 1)
                {
                    scramble = teams.ToList();
                    Round newRound = new Round();

                    if (scramble.Count % 2 != 0)
                    {
                        if (numberOfRounds > 0)
                        {
                            oldFreeRider = lastRound.FreeRider;
                        }
                        else
                        {
                            oldFreeRider = null;
                        }

                        int x = 0;

                        while (newFreeRider == oldFreeRider)
                        {
                            newFreeRider = scramble[x];
                            x++;
                        }

                        newRound.FreeRider = newFreeRider;
                        scramble.Remove(newFreeRider);
                    }
                    for (int i = 0; i < scramble.Count; i = i + 2)
                    {
                        Match match = new Match();
                        match.FirstOpponent  = scramble[i];
                        match.SecondOpponent = scramble[i + 1];
                        newRound.AddMatch(match);
                    }
                    t.AddRound(newRound);
                    // Vis kampe i ny runde (se wireframe) Sprint 2 dag 1
                }
                else
                {
                    throw new Exception("Tournament is finished");
                }
            }
            else
            {
                throw new Exception("Round not finished");
            }
        }