TournamentWindow.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. namespace Курсовой_проект_3._1
  15. {
  16. /// <summary>
  17. /// Логика взаимодействия для TournamentWindow.xaml
  18. /// </summary>
  19. public partial class TournamentWindow : Window
  20. {
  21. public TournamentWindow()
  22. {
  23. InitializeComponent();
  24. MainGrid.Children.Add(GenerateSingleEliminationBracket(16));
  25. }
  26. public Grid GenerateSingleEliminationBracket(int teamCount)
  27. {
  28. Grid grid = new Grid();
  29. grid.ShowGridLines = false;
  30. // Создание Grid'ов ///
  31. // Высчитывание размеров сетки
  32. int roundCount = (int)Math.Log(teamCount, 2); // количество раундов
  33. int gridColumnCount = roundCount + roundCount - 1 + 2; // количество колонок в grid
  34. int gridRowCount = teamCount + (teamCount - 2) + 4; // количество строк в grid
  35. // Добавление колонок
  36. for (int i = 0; i < gridColumnCount; i++)
  37. {
  38. grid.ColumnDefinitions.Add(new ColumnDefinition());
  39. }
  40. // Добавление строк
  41. for (int j = 0; j < gridRowCount; j++)
  42. {
  43. grid.RowDefinitions.Add(new RowDefinition());
  44. }
  45. // Всё остальное
  46. int roundNum = 1; // номер раунда
  47. int skipStart = 2; // сколько пропустить строк с начала
  48. int skipBetween = 2; // сколько пропускать строк между играми
  49. // Наполнение сетки
  50. for (int i = 1; i < gridColumnCount - 1; i++)
  51. {
  52. if (i % 2 != 0) // раунды через одну колонку
  53. {
  54. int counter = 0;
  55. for (int j = skipStart; j < gridRowCount - 2; j++)
  56. {
  57. // создание grid'a с двумя колонками разной ширины (НАЗВАНИЕ КОМАНДЫ и ОЧКИ)
  58. Grid gridChild = new Grid();
  59. ColumnDefinition wideColumn = new ColumnDefinition();
  60. wideColumn.Width = new GridLength(0.8, GridUnitType.Star);
  61. ColumnDefinition smallColumn = new ColumnDefinition();
  62. smallColumn.Width = new GridLength(0.2, GridUnitType.Star);
  63. gridChild.ColumnDefinitions.Add(wideColumn);
  64. gridChild.ColumnDefinitions.Add(smallColumn);
  65. // текст бокс с названием команды
  66. TextBox tbName = new TextBox();
  67. Grid.SetColumn(tbName, 0);
  68. gridChild.Children.Add(tbName);
  69. // текст бокс со счетом
  70. TextBox tbScore = new TextBox();
  71. tbScore.MaxLength = 2;
  72. tbScore.HorizontalContentAlignment = HorizontalAlignment.Center;
  73. Grid.SetColumn(tbScore, 1);
  74. gridChild.Children.Add(tbScore);
  75. // добавление мини-grid'a к большому папочке
  76. Grid.SetColumn(gridChild, i);
  77. Grid.SetRow(gridChild, j);
  78. grid.Children.Add(gridChild);
  79. // после каждыой игры нужен пропуск (skipBetween) до следующей игры
  80. counter++;
  81. if (counter == 2)
  82. {
  83. j += skipBetween;
  84. counter = 0;
  85. }
  86. }
  87. // пересчет значений для следующего раунда
  88. roundNum++;
  89. int gamesInRoundCount = (int)(teamCount / Math.Pow(2, roundNum));
  90. skipStart *= 2;
  91. skipBetween = roundNum == roundCount ? skipStart : (gridRowCount - (skipStart * 2) - (gamesInRoundCount * 2)) / (gamesInRoundCount - 1);
  92. }
  93. else // соединяющие линии между раундами
  94. {
  95. if (i == 2)
  96. {
  97. for (int j = 4; j < gridRowCount; j++)
  98. {
  99. TextBox linetb1 = new TextBox();
  100. linetb1.BorderBrush = Brushes.Black;
  101. linetb1.IsReadOnly = true;
  102. var bc = new BrushConverter();
  103. linetb1.Background = (Brush)bc.ConvertFrom("#fffff0");
  104. linetb1.BorderThickness = new Thickness(2, 0, 0, 2);
  105. Grid.SetColumn(linetb1, i);
  106. Grid.SetRow(linetb1, j);
  107. grid.Children.Add(linetb1);
  108. TextBox linetb2 = new TextBox();
  109. linetb2.BorderBrush = Brushes.Black;
  110. linetb2.IsReadOnly = true;
  111. linetb2.Background = (Brush)bc.ConvertFrom("#fffff0");
  112. linetb2.BorderThickness = new Thickness(2, 2, 0, 0);
  113. Grid.SetColumn(linetb2, i);
  114. Grid.SetRow(linetb2, j + 1);
  115. grid.Children.Add(linetb2);
  116. j += 7;
  117. }
  118. }
  119. else
  120. {
  121. int drawStatus = 0; // 0 - не рисовать, 1 - leftbottom, 2 - lefttop, 3 - leftonly
  122. int leftCounter = 0;
  123. int rightCounter = 0;
  124. for (int j = 4; j < gridRowCount; j++)
  125. {
  126. // тут рисовка
  127. if (drawStatus == 3)
  128. {
  129. TextBox linetb3 = new TextBox();
  130. linetb3.BorderBrush = Brushes.Black;
  131. linetb3.IsReadOnly = true;
  132. var bc = new BrushConverter();
  133. linetb3.Background = (Brush)bc.ConvertFrom("#fffff0");
  134. linetb3.BorderThickness = new Thickness(2, 0, 0, 0);
  135. Grid.SetColumn(linetb3, i);
  136. Grid.SetRow(linetb3, j);
  137. grid.Children.Add(linetb3);
  138. }
  139. else if (drawStatus == 2)
  140. {
  141. TextBox linetb2 = new TextBox();
  142. linetb2.BorderBrush = Brushes.Black;
  143. linetb2.IsReadOnly = true;
  144. var bc = new BrushConverter();
  145. linetb2.Background = (Brush)bc.ConvertFrom("#fffff0");
  146. linetb2.BorderThickness = new Thickness(2, 2, 0, 0);
  147. Grid.SetColumn(linetb2, i);
  148. Grid.SetRow(linetb2, j - 1);
  149. grid.Children.Add(linetb2);
  150. }
  151. else if (drawStatus == 1)
  152. {
  153. TextBox linetb1 = new TextBox();
  154. linetb1.BorderBrush = Brushes.Black;
  155. linetb1.IsReadOnly = true;
  156. var bc = new BrushConverter();
  157. linetb1.Background = (Brush)bc.ConvertFrom("#fffff0");
  158. linetb1.BorderThickness = new Thickness(2, 0, 0, 2);
  159. Grid.SetColumn(linetb1, i);
  160. Grid.SetRow(linetb1, j - 1);
  161. grid.Children.Add(linetb1);
  162. drawStatus = 3;
  163. }
  164. // тут проверка
  165. if (GetElementInGridPosition(i - 1, j, grid) is Grid)
  166. {
  167. leftCounter++;
  168. drawStatus = 0;
  169. if (leftCounter == 2)
  170. {
  171. drawStatus = 3;
  172. leftCounter = 0;
  173. }
  174. }
  175. else if (GetElementInGridPosition(i + 1, j, grid) is Grid)
  176. {
  177. rightCounter++;
  178. if (rightCounter == 1)
  179. {
  180. drawStatus = 1;
  181. }
  182. else
  183. {
  184. drawStatus = 2;
  185. rightCounter = 0;
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. return grid;
  193. }
  194. private UIElement GetElementInGridPosition(int column, int row, Grid RootGrid)
  195. {
  196. foreach (UIElement element in RootGrid.Children)
  197. {
  198. if (Grid.GetColumn(element) == column && Grid.GetRow(element) == row)
  199. return element;
  200. }
  201. return null;
  202. }
  203. }
  204. }