NCAA bracket
NCAA bracket C# source code results
Submitted by Blake on Mon, 04/13/2009 - 12:34Just about a month ago, I posted some C# source code about making picks for an NCAA bracket. The code is basically broken into 3 parts:
- Retrieving all team information from a config file.
- For each match-up between two teams, pick the winner given an algorithm.
- Display the result bracket.
The code was built to be modular enough so that any step could be expanded upon at a later date. The most obvious step that needs building is (2). The code as it is now only looks at the two teams' ranks - though it doesn't just take the lower rank (it's random but favors the lower rank), but still.
Anyways, now that the entire championship is complete and all the results are out there, I can run it a few more times and see how the bracket generator holds up when compared against the actual winners. Given that in most NCAA pools, the Final Four and on is really what it comes down to, I figured I would just run the program N times and check the following: 1) Did it nail all four teams in the Final Four? 2) The two teams in the finals? 3) The final winner.
Instead of using another config file, I just quickly built the final four, finals, and champion data manually in the code:
private List GetWinners()
{
Team mich = new Team("Michigan State", 2);
Team conn = new Team("Connecticut", 1);
Team vill = new Team("Villanova", 3);
Team nc = new Team("North Carolina", 1);
Round four = new Round();
four.Teams.Add(mich);
four.Teams.Add(conn);
four.Teams.Add(vill);
four.Teams.Add(nc);
Round two = new Round();
two.Teams.Add(mich);
two.Teams.Add(nc);
Round one = new Round();
one.Teams.Add(nc);
List winners = new List();
winners.Add(four);
winners.Add(two);
winners.Add(one);
return winners;
}
Here's the code for checking if one set of teams is equal to another set, i.e. if the teams in my Final Four picks match the actual Final Four results:
private bool RoundsEqual(Round r1, Round r2)
{
int equalCount = 0;
foreach (Team t1 in r1.Teams)
{
foreach (Team t2 in r2.Teams)
{
if (String.Compare(t1.Name, t2.Name, true) == 0)
{
equalCount++;
break;
}
}
}
return (equalCount == r1.Teams.Count);
}
One more quick thing. I moved all the code from last week into one class: BracketProcessor. For the last entry, all the code was just in the page CreateBracket.aspx. But now I wanted a new page BracketResultsReport.aspx. So I moved into all into a separate object, called that from both pages, left the method DisplayBracket on CreateBracket, and included this new code on the BracketResultsReport page. The code from last post is all the same; only change is that I made sure that the GetFirstRound method is only called once, since on the new page, it would be called several times.
With all that in place, the rest of the code is pretty straightforward:
private const int TEST_COUNT = 1000000;
protected void Page_Load(object sender, EventArgs e)
{
BracketProcessor processor = new BracketProcessor();
List winners = GetWinners();
int[] correctCounts = new int[] { 0, 0, 0 };
for (int i = 0; i < TEST_COUNT; i++)
{
List rounds = processor.BuildBracket();
int roundOffset = rounds.Count - 3;
for (int j = 0; j < 3; j++)
{
if (RoundsEqual(winners[j], rounds[j + roundOffset]))
correctCounts[j]++;
}
}
Response.Write(String.Format("Total tests: {0}
", TEST_COUNT.ToString()));
Response.Write(String.Format("Final Four: {0}
", correctCounts[0].ToString()));
Response.Write(String.Format("Finals: {0}
", correctCounts[1].ToString()));
Response.Write(String.Format("Winner: {0}", correctCounts[2].ToString()));
}
I double-checked this would work, then set the count up to a million. I ran it a few times and got pretty consistent results:
Total tests: 1000000
Final Four: 6238
Finals: 31904
Winner: 185449
Total tests: 1000000
Final Four: 6252
Finals: 32005
Winner: 185466
Total tests: 1000000
Final Four: 6225
Finals: 32114
Winner: 185839
With percents, that's roughly:
Final Four: 0.6%
Finals: 3.2%
Winner: 18.5%
Not too shabby, especially with an admittedly rudimentary algorithm to pick a winner. That's about 1 in 5 for picking the winner. With the structure in place, the PickWinner method could be beefed up to take in more stats, including further results of this NCAA championship.
