private static void SaveMatchupToFile(this MatchUp matchUp, string matchupFile, string mactupEntryFile) { var matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchup(); int currentId = 1; if (matchups.Count > 0) { currentId = matchups.OrderByDescending(x => x.Id).First().Id + 1; } matchUp.Id = currentId; foreach (var entry in matchUp.Entries) { entry.SaveMatchupEntryToFile(mactupEntryFile); } var lines = new List <string>(); foreach (var _matchup in matchups) { lines.Add($"{ _matchup.Id },{ ConvertMatchupEntriesListToString(_matchup.Entries) },{ _matchup.Winner.Id },{ _matchup.Round }"); } File.WriteAllLines(GlobalConfig.MatchupFile.FullFilePath(), lines); }
private static List <MatchUp> CreateFirstRound(int byes, List <Team> teams) { var matchups = new List <MatchUp>(); var currentMacthup = new MatchUp(); foreach (var team in teams) { currentMacthup.Entries.Add(new MatchUpEntry { TeamCompeting = team }); if (byes > 0 || currentMacthup.Entries.Count > 1) { currentMacthup.Round = 1; matchups.Add(currentMacthup); currentMacthup = new MatchUp(); if (byes > 0) { byes -= 1; } } } return(matchups); }
private static void CreateOtherRound(Tournament tournament, int rounds) { int round = 2; var previousRound = tournament.Rounds[0]; var currentRound = new List <MatchUp>(); var currentMatchup = new MatchUp(); while (round <= rounds) { foreach (var matchup in previousRound) { currentMatchup.Entries.Add(new MatchUpEntry { Parent = matchup }); if (currentMatchup.Entries.Count > 1) { currentMatchup.Round = round; currentRound.Add(currentMatchup); currentMatchup = new MatchUp(); } } tournament.Rounds.Add(currentRound); previousRound = currentRound; currentRound = new List <MatchUp>(); round += 1; } }
public static IEnumerable <MatchUp> OrganiseGames(IEnumerable <string> players, IEnumerable <MatchUp> games) { var results = new List <MatchUp>(); //a list that we will treat as a queue - most recently played at the back of the queue var playerStack = new List <string>(players); //a list that we can modify var gamesList = new List <MatchUp>(games); while (gamesList.Count > 0) { //find a game for the top player on the stack var player1 = playerStack.First(); var player2 = playerStack.Skip(1).First(); //the players are in the order of least recently played first MatchUp matchUp = FindFirstAvailableGame(playerStack, gamesList); //drop the players that just played to the back of the list playerStack.Remove(matchUp.Player1); playerStack.Remove(matchUp.Player2); playerStack.Add(matchUp.Player1); playerStack.Add(matchUp.Player2); //remove that pairing gamesList.Remove(matchUp); //yield return matchUp; //optional way of doing this results.Add(matchUp); } return(results); }
public static IEnumerable <MatchUp> PairPlayers(List <string> players) { var results = new List <MatchUp>(); for (int i = 0; i < players.Count - 1; i++) { for (int j = i + 1; j < players.Count; j++) { var matchup = new MatchUp { Player1 = players[i], Player2 = players[j] }; //yield return matchup; //this is how Jon Skeet suggested, I am showing you "eager" evaluation results.Add(matchup); } } return(results); }
private static void PlayGame(MatchUp matchUp, Simulator simulator, int simulationDepth) { using Player player1 = new() { Name = matchUp.StartingPlayer.Name }, player2 = new() { Name = matchUp.Opponent.Name }; player1.Deck = new(GetCards(player1.Id, matchUp.StartingPlayer.DeckPath)); player2.Deck = new(GetCards(player2.Id, matchUp.Opponent.DeckPath)); using var duel = simulator.PlayDuel(player1, player2, simulationDepth); var usedCards = duel.Turns.SelectMany(x => x.Steps).SelectMany(x => x.UsedCards); if (duel.Winner != null) { UpdateWinnerUsedCards(duel, usedCards, matchUp.Players.Distinct().Single(x => x.Name == duel.Winner.Name)); } foreach (var loser in duel.Losers) { UpdateLoserUsedCards(duel, usedCards, loser, matchUp.Players.Distinct().Single(x => x.Name == loser.Name)); } }
public static List <MatchUp> ConvertToMatchup(this List <string> lines) { var output = new List <MatchUp>(); foreach (var line in lines) { string[] cols = line.Split(','); var _person = new MatchUp { Id = int.Parse(cols[0]), Entries = ConvertToStringMatchupEntries(cols[1]), Winner = LookupTeamById(int.Parse(cols[2])), Round = int.Parse(cols[3]), }; output.Add(_person); } return(output); }