Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Diagnostics;
  5.  
  6. namespace TopReplays.Extensions
  7. {
  8. public static class IEnumerableExtensions
  9. {
  10. private class TournamentSeedPosition<T>
  11. {
  12. public int Seed
  13. {
  14. get;
  15. set;
  16. }
  17.  
  18. public T TournamentObject
  19. {
  20. get;
  21. set;
  22. }
  23. }
  24.  
  25. public static T SelectRandom<T>(this IEnumerable<T> ienumerable)
  26. {
  27. Random r = new Random();
  28. return ienumerable.ElementAt<T>(r.Next(ienumerable.Count()));
  29. }
  30.  
  31. public static IEnumerable<T> TournamentSeedShuffle<T>(this IEnumerable<T> players)
  32. {
  33. List<TournamentSeedPosition<T>> finalPositions = new List<TournamentSeedPosition<T>>();
  34.  
  35. finalPositions.Add(new TournamentSeedPosition<T>
  36. {
  37. Seed = 1,
  38. TournamentObject = players.ElementAt(0)
  39. });
  40.  
  41. finalPositions.Add(new TournamentSeedPosition<T>
  42. {
  43. Seed = 2,
  44. TournamentObject = players.ElementAt(1)
  45. });
  46.  
  47. double nr_rounds = Math.Ceiling(Math.Log(players.Count(), 2));
  48.  
  49. for (int round = 2; round <= nr_rounds; round++)
  50. {
  51. int currentRoundMatchPairing = Convert.ToInt32(Math.Pow(2, round)) + 1;
  52. int currentPlayer = 0;
  53.  
  54. while (currentPlayer < finalPositions.Count())
  55. {
  56. int currentOpponent = currentRoundMatchPairing - finalPositions.ElementAt(currentPlayer).Seed;
  57. finalPositions.Insert(currentPlayer + 1, new TournamentSeedPosition<T>
  58. {
  59. Seed = currentOpponent,
  60. TournamentObject = players.ElementAt(currentOpponent - 1)
  61. });
  62.  
  63. currentPlayer += 2;
  64. }
  65. }
  66.  
  67. return finalPositions.Select(t => t.TournamentObject);
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement