private static int CalCountOfRounds(int countOfTeams, ScheduleType scheduleType) { return((scheduleType.HomeAndAway ? 2 : 1) * (countOfTeams - 1)); }
public static void GenSchedule(ICollection <string> vs, ScheduleType scheduleType) { //奇数 if (vs.Count % 2 != 0) { vs.Add("EMPTY"); } //偶数 else { } var teams = vs.Select((s) => { return(new Team(s)); }); var countOfTeams = teams.Count(); int countOfRoundsOfSchedule = CalCountOfRounds(countOfTeams, scheduleType); int countOfMatchesPerRound = countOfTeams / 2; var schedule = new Schedule() { Rounds = new List <Round>() }; for (int indexOfThisRound = 0; indexOfThisRound < countOfRoundsOfSchedule; indexOfThisRound++) { Round round = new Round() { Matches = new List <Match>() }; //是第0轮 if (indexOfThisRound == 0) { for (int indexOfMatchInRound = 0; indexOfMatchInRound < countOfMatchesPerRound; indexOfMatchInRound++) { Match match = new Match(); match.HomeTeam = teams.ToList()[indexOfMatchInRound * 2]; match.AwayTeam = teams.ToList()[indexOfMatchInRound * 2 + 1]; round.Matches.Add(match); } } //不是第0轮 else { //前一轮 var previosRound = schedule.Rounds[indexOfThisRound - 1]; for (int indexOfMatchInRound = 0; indexOfMatchInRound < countOfMatchesPerRound; indexOfMatchInRound++) { Match match = new Match(); if (indexOfMatchInRound == 0) { match.HomeTeam = teams.ToList()[0]; match.AwayTeam = previosRound.Matches[indexOfMatchInRound + 1].AwayTeam; } else { //是第二轮吗 match.HomeTeam = indexOfMatchInRound == 1 ? previosRound.Matches[0].AwayTeam : previosRound.Matches[indexOfMatchInRound - 1].HomeTeam; //是最后一轮吗 match.AwayTeam = indexOfMatchInRound == countOfMatchesPerRound - 1 ? previosRound.Matches.Last().HomeTeam : previosRound.Matches[indexOfMatchInRound + 1].AwayTeam; } round.Matches.Add(match); } } schedule.Rounds.Add(round); } ; schedule.CrossMatches(); schedule.Print(); ;; }