[Fork] Seed Example

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
	public static void Main()
	{
		var teamCount = 8;
		var round = 1;
		
		var teams = CreateTeams(teamCount);
		
		while(teams.Count > 1){
			var matchups = CreateMatchups(teams);
			PrintBracket(matchups, round++);
			teams = PlayRound(matchups);
		}
		
		Console.WriteLine("\n" + teams.Single().Name + " is the winner!");
	}

	//creates dummy teams: "Team1" Seed 1, "Team2" Seed 2, etc...
	private static List<Team> CreateTeams(int totalTeams){
		return Enumerable.Range(1, totalTeams).Select(x => new Team("Team " + x, x)).ToList();
	}
	
	private static List<Matchup> CreateMatchups(List<Team> teams)
	{
		var matchups = new List<Matchup>();
		
		for (int i = 0; i < teams.Count / 2; i++)
		{
			matchups.Add(new Matchup(teams[i], teams[teams.Count -(i+1)]));
		}
		
		return matchups;
	}

	private static void PrintBracket(List<Matchup> Matchups, int round)
	{
		//Console.WriteLine("Round " + round + ":\n" + string.Join("\n", Matchups.OrderBy(x=> x.GetFavored().Seed)));
		//or more understandly... ;)
		
		Console.WriteLine("Round " + round + ":\n");
		foreach(var matchup in Matchups.OrderBy(x=> x.GetFavored().Seed)){
			Console.WriteLine(matchup);
		}
	}
	
	private static List<Team> PlayRound(List<Matchup> matchups){
		return matchups.Select(x=> x.GetFavored()).ToList();
	}
}

public class Team
{
	public Team(string name, int seed){
		Name = name;
		Seed = seed;
	}
	
	public string Name {get; set;}

	public int Seed {get; set;}
	
	public static Team GetBetterSeed(Team t1, Team t2){
		return t1.Seed < t2.Seed ? t1 : t2;
	}
}

public class Matchup
{
	public Matchup(Team t1, Team t2){
		Team1 = t1;
		Team2 = t2;
	}
	
	public Team Team1 {get; set;}
	public Team Team2 {get; set;}
	
	public override string ToString(){
		return Team1.Name + " vs. " + Team2.Name;
	}
	
	public Team GetFavored(){
		return Team.GetBetterSeed(Team1, Team2);
	}
}
;