public ControlBracket() { InitializeComponent(); DrawBracketLines(); _bracket = null; }
/// <summary> /// Creates Bracket objects for all available competitor matchups for the tournament. /// </summary> private void CreateBrackets() { //Determine the number of levels deep our tournament will be, based on //the number of competitors we have int tournamentLevelDepth = CalculateNoOfTournamentLevels(); //Create the winner object _winner = new Winner(SeedOption); //Create the top level (finals-level) Bracket object int curBracketLevel = 1; int curBracketSequenceNo = 1; Bracket topLevelBracket = new Bracket(SeedOption, curBracketLevel, curBracketSequenceNo) { ParentBracket = _winner }; //Assign the top-level bracket as the Winner object's child _winner.ChildBracket = topLevelBracket; //Add the top-level Bracket object to the collection _brackets.Add(topLevelBracket); //Continue creating children Bracket objects until we have created brackets to fill the //depth of our tournament while (curBracketLevel < tournamentLevelDepth) { //Reset the bracket sequence number for the child brackets that we're about //to create. curBracketSequenceNo = 1; //Select the current level bracket(s). List <Bracket> foundBrackets = GetBracketsForLevel(curBracketLevel); //Increment the bracket level for the children-level bracket objects we're about to create. curBracketLevel++; //For each bracket we found, add two children to each. foreach (Bracket bracket in foundBrackets) { //Create the first child bracket, using the current bracket as its parent. Bracket childBracket1 = new Bracket(SeedOption, curBracketLevel, curBracketSequenceNo++) { ParentBracket = bracket }; bracket.ChildBracket = childBracket1; _brackets.Add(childBracket1); //Create the second child bracket, using the current bracket as its parent. Bracket childBracket2 = new Bracket(SeedOption, curBracketLevel, curBracketSequenceNo++) { ParentBracket = bracket }; bracket.ChildBracket2 = childBracket2; _brackets.Add(childBracket2); } } }
/// <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> /// Creates Bracket objects for all available competitor matchups for the tournament. /// </summary> private void CreateBrackets() { //Determine the number of levels deep our tournament will be, based on //the number of competitors we have int tournamentLevelDepth = CalculateNoOfTournamentLevels(); //Create the winner object _winner = new Winner(SeedOption); //Create the top level (finals-level) Bracket object int curBracketLevel = 1; int curBracketSequenceNo = 1; Bracket topLevelBracket = new Bracket(SeedOption, curBracketLevel, curBracketSequenceNo) { ParentBracket = _winner }; //Assign the top-level bracket as the Winner object's child _winner.ChildBracket = topLevelBracket; //Add the top-level Bracket object to the collection _brackets.Add(topLevelBracket); //Continue creating children Bracket objects until we have created brackets to fill the //depth of our tournament while (curBracketLevel < tournamentLevelDepth) { //Reset the bracket sequence number for the child brackets that we're about //to create. curBracketSequenceNo = 1; //Select the current level bracket(s). List<Bracket> foundBrackets = GetBracketsForLevel(curBracketLevel); //Increment the bracket level for the children-level bracket objects we're about to create. curBracketLevel++; //For each bracket we found, add two children to each. foreach (Bracket bracket in foundBrackets) { //Create the first child bracket, using the current bracket as its parent. Bracket childBracket1 = new Bracket(SeedOption, curBracketLevel, curBracketSequenceNo++) { ParentBracket = bracket }; bracket.ChildBracket = childBracket1; _brackets.Add(childBracket1); //Create the second child bracket, using the current bracket as its parent. Bracket childBracket2 = new Bracket(SeedOption, curBracketLevel, curBracketSequenceNo++) { ParentBracket = bracket }; bracket.ChildBracket2 = childBracket2; _brackets.Add(childBracket2); } } }
public CompetitorAlreadyAssignedForBracketException(Bracket targetBracket, int competitorNo) : base(string.Format("Competitor {0} already assigned for bracket; Level: {1}, Sequence: {2}", competitorNo, targetBracket.Level, targetBracket.Sequence)) { }
public ChildToParentBracketAssociationException(Bracket parentBracket, Bracket childBracket) : base(string.Format("Child-to-Parent bracket association exception; Child Level: {0}, Sequence: {1}; " + "Parent Level: {2}; Sequence: {3}", childBracket.Level, childBracket.Sequence, parentBracket.Level, parentBracket.Sequence)) { }
public ChildToParentBracketAssociationException(Winner parentBracket, Bracket childBracket) : base(string.Format("Child-to-Parent bracket association exception; Child Level: {0}, Sequence: {1}; Winner", childBracket.Level, childBracket.Sequence)) { }
/// <summary> /// Assigns all Competitor 1 slots to the lowest level tournament brackets. /// </summary> private void AssignCompetitor1Brackets(int lowestBracketLevel) { //Get the final bracket and drill down from there Bracket finalBracket = GetBracketsForLevel(1)[0]; //Execute Linq query to determine how many brackets are at the lowest level int bottomBracketCount = GetBracketsForLevel(lowestBracketLevel).Count; //Make sure that we have enough brackets for the number of competitors that //we have if ((bottomBracketCount * 2) < Competitors.Count) { throw new InvalidBracketCountException(bottomBracketCount, Competitors.Count); } //Assign competitor 1 fields for the bottom level of brackets for (int i = 1; i <= bottomBracketCount; i++) { Bracket curBracket = finalBracket; bool competitorAssigned = false; while (!competitorAssigned) { int bracket1Count = 0; int bracket2Count = 0; if ((curBracket.ChildBracket != null) && (curBracket.ChildBracket2 != null)) { bracket1Count = curBracket.ChildBracket.AssignedBracketCount; bracket2Count = curBracket.ChildBracket2.AssignedBracketCount; if (bracket1Count == bracket2Count) { //Check to see which bracket has a higher seed int bracket1HighSeed = curBracket.ChildBracket.AssignedBracketHighestSeed; int bracket2HighSeed = curBracket.ChildBracket2.AssignedBracketHighestSeed; if (bracket1HighSeed > bracket2HighSeed) { curBracket = curBracket.ChildBracket; } else if (bracket2HighSeed > bracket1HighSeed) { curBracket = curBracket.ChildBracket2; } else { //We haven't assigned any brackets yet, so go with the first one curBracket = curBracket.ChildBracket; } } else if (bracket1Count > bracket2Count) { curBracket = curBracket.ChildBracket2; } else { curBracket = curBracket.ChildBracket; } } else { //We've found the bracket we're looking for, so assign the first //Competitor curBracket.Competitor = (from c in Competitors where c.Seed == i select c).First(); competitorAssigned = true; } } } }