/// <summary> /// Advances the provided competitor to the next round in the tournament. /// </summary> /// <param name="competitor"></param> public void Advance(Competitor competitor) { //Make sure that the provided Competitor object belongs to this bracket if ((competitor != this.Competitor) && (competitor != this.Competitor2)) { throw new InvalidCompetitorException(competitor); } if (ParentBracket is Bracket) { Bracket parent = (Bracket)ParentBracket; //Check to see if this bracket is child 1 or child 2 in the parent if (parent.ChildBracket == this) { if (parent.Competitor != null) { throw new CompetitorAlreadyAssignedForBracketException(parent, 1); } parent.Competitor = competitor; } else if (parent.ChildBracket2 == this) { if (parent.Competitor2 != null) { throw new CompetitorAlreadyAssignedForBracketException(parent, 2); } parent.Competitor2 = competitor; } else { throw new ChildToParentBracketAssociationException(parent, this); } } else if (ParentBracket is Winner) { //Check to see if we're assigning a winner Winner parent = (Winner)ParentBracket; parent.Competitor = competitor; } }
/// <summary> /// Assigns competitors to brackets according to the stored seed option. /// </summary> private void AssignBrackets() { //Get the bottom bracket level for the tournemnt int bottomLevel = CalculateNoOfTournamentLevels(); if (SeedOption == SeedOption.Seeded) { //Sort our competitor collection to ensure that they're sorted ascending by seed //before we begin bracket assignment Competitors.Sort(); } else { //We're randomizing the competitors. Create a list to hold our randomized result. List <Competitor> randomizedCompetitors = new List <Competitor>(); Random rnd = new Random(); while (Competitors.Count > 0) { //Randomly generate an index to grab int rndIndex = rnd.Next(Competitors.Count); //Get the competitor at that index and add it to the randomized collection Competitor tempCompetitor = Competitors[rndIndex]; randomizedCompetitors.Add(tempCompetitor); //Set the seed for the competitor tempCompetitor.Seed = randomizedCompetitors.Count; //Remove the competitor from the original source list Competitors.RemoveAt(rndIndex); } //Now that our competitors are now in the randomized collection, assign that one //as the Competitor collection to use Competitors = randomizedCompetitors; } //Assign Competitor 1 slots in starting brackets AssignCompetitor1Brackets(bottomLevel); //Assign Competitor 2 slots in starting brackets AssignCompetitor2Brackets(bottomLevel); }