Advertisement
TwinFrame

BasePlayer

Jan 31st, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.10 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Clight_24_OOP_BasePlayers
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int maxLenghtName = 15;
  10. int xSecondMenu = 55;
  11. ConsoleColor defaultColor = ConsoleColor.White;
  12.  
  13. Player player1 = new Player("Rolando", 'R', ConsoleColor.Red, 69, 82);
  14. Player player2 = new Player("Dzubinyo", 'D', ConsoleColor.Green, 84, 68);
  15. Player player3 = new Player("Гушаков", '@', ConsoleColor.Blue, 71, 73);
  16. Player player4 = new Player("Pepe", 'Y', ConsoleColor.Magenta, 78, 76);
  17.  
  18. Team club = new Team(new Player[] { player1, player2, player3, player4 });
  19.  
  20. bool isActive = true;
  21. while (isActive)
  22. {
  23. Console.CursorVisible = false;
  24. Console.WriteLine("Основной состав:");
  25. Console.WriteLine("Номер\tИмя\t\tАватар\tВес\tРост\t\n");
  26. Console.SetCursorPosition(xSecondMenu, 0);
  27. Console.WriteLine("Скамейка запасных:");
  28. Console.SetCursorPosition(xSecondMenu, 1);
  29. Console.WriteLine("Номер\tИмя\t\tАватар\tВес\tРост\t\n");
  30. Console.SetCursorPosition(0, 3);
  31. club.ShowPlayers(defaultColor, xSecondMenu);
  32.  
  33. Console.SetCursorPosition(0, club.NumOfPlayers() + 5);
  34. Console.WriteLine("Меню:");
  35. Console.WriteLine("F1 - Добавить игрока");
  36. Console.WriteLine("F2 - Удалить игрока");
  37. Console.WriteLine("F3 - Менеджмент состава");
  38. Console.WriteLine("F5 - Выход");
  39.  
  40. ConsoleKeyInfo key = Console.ReadKey();
  41.  
  42. switch (key.Key)
  43. {
  44. case ConsoleKey.F1:
  45. club.AddPlayer(club.NumOfPlayers(), maxLenghtName);
  46. break;
  47. case ConsoleKey.F2:
  48. club.DelPlayer(club.NumOfPlayers());
  49. break;
  50. case ConsoleKey.F3:
  51. club.ManagePlayer(club.NumOfPlayers());
  52. break;
  53. case ConsoleKey.F5:
  54. Console.SetCursorPosition(0, club.NumOfPlayers() + 12);
  55. Console.WriteLine("Играйте в футбол!");
  56. Console.ReadKey();
  57. Environment.Exit(0);
  58. break;
  59. }
  60. Console.Clear();
  61. }
  62. }
  63. }
  64. class Player
  65. {
  66. private string _name;
  67. private char _avatar;
  68. private ConsoleColor _color;
  69. private int _weigth;
  70. private int _speed;
  71. private bool _reserve;
  72.  
  73. public Player(string name, char avatar, ConsoleColor color, int weigth, int speed, bool reserve = false)
  74. {
  75. _name = name;
  76. _avatar = avatar;
  77. _color = color;
  78. _weigth = weigth;
  79. _speed = speed;
  80. _reserve = reserve;
  81. }
  82.  
  83. public void ShowInfo(ConsoleColor defaultColor)
  84. {
  85. if (_name.Length < 8)
  86. {
  87. CollectInfoInLine("\t\t", defaultColor);
  88. }
  89. else
  90. {
  91. CollectInfoInLine("\t", defaultColor);
  92. }
  93. }
  94. public void SetReservePlayer()
  95. {
  96. _reserve = true;
  97. }
  98. public void SetActivePlayer()
  99. {
  100. _reserve = false;
  101. }
  102. public bool GetStatusPlayer()
  103. {
  104. return _reserve;
  105. }
  106. private void CollectInfoInLine(string space, ConsoleColor defaultColor)
  107. {
  108. Console.Write($"{_name}{space}");
  109. Console.ForegroundColor = _color;
  110. Console.Write($"{_avatar}\t");
  111. Console.ForegroundColor = defaultColor;
  112. Console.Write($"{_weigth}\t{_speed}\n");
  113. }
  114. }
  115. class Team
  116. {
  117. private Player[] Club;
  118. public Team(Player[] club)
  119. {
  120. Club = club;
  121. }
  122. public Player[] ReservePlayers()
  123. {
  124. int numReservePlayer = 0;
  125. for (int i = 0; i < Club.Length; i++)
  126. {
  127. if (Club[i].GetStatusPlayer())
  128. {
  129. numReservePlayer++;
  130. }
  131. }
  132.  
  133. int z = 0;
  134. int j = 0;
  135. Player[] reservePlayers = new Player[numReservePlayer];
  136. for (int i = 0; i < reservePlayers.Length; i++)
  137. {
  138. j = z;
  139. while (Club[j].GetStatusPlayer() != true && j < NumOfPlayers())
  140. {
  141. j++;
  142. }
  143. z = j + 1;
  144. reservePlayers[i] = Club[j];
  145. }
  146. return reservePlayers;
  147. }
  148. public Player[] ActivePlayers()
  149. {
  150. int numReservePlayer = 0;
  151. for (int i = 0; i < Club.Length; i++)
  152. {
  153. if (Club[i].GetStatusPlayer())
  154. {
  155. numReservePlayer++;
  156. }
  157. }
  158.  
  159. int z = 0;
  160. int j = 0;
  161. Player[] activePlayers = new Player[NumOfPlayers() - numReservePlayer];
  162. for (int i = 0; i < activePlayers.Length; i++)
  163. {
  164. j = z;
  165. while (Club[j].GetStatusPlayer() && j < NumOfPlayers())
  166. {
  167. j++;
  168. }
  169. z = j + 1;
  170. activePlayers[i] = Club[j];
  171.  
  172. }
  173. return activePlayers;
  174. }
  175. public void ShowPlayers(ConsoleColor defaultColor, int xSecondMenu)
  176. {
  177. for (int i = 0; i < ActivePlayers().Length; i++)
  178. {
  179. Console.Write($"{i + 1}\t");
  180. ActivePlayers()[i].ShowInfo(defaultColor);
  181. }
  182.  
  183. for (int i = 0; i < ReservePlayers().Length; i++)
  184. {
  185. Console.SetCursorPosition(xSecondMenu, i + 3);
  186. Console.Write($"{i + 1}\t");
  187. ReservePlayers()[i].ShowInfo(defaultColor);
  188. }
  189. }
  190. public int NumOfPlayers()
  191. {
  192. return Club.Length;
  193. }
  194.  
  195. public void AddPlayer(int numOfPlayer, int maxLenghtName)
  196. {
  197. string currentName = CheckInputUser("Введите имя игрока", numOfPlayer, maxLenghtName);
  198. char currentAvatar = CheckInputUser("Введите аватар игрока (один символ)", numOfPlayer + 1);
  199. ConsoleColor currentColor = CheckInputUserColor("Введите цвет аватара", numOfPlayer + 2);
  200. int currentWeigth = CheckInputUser("Введите вес игрока", 15, 300, numOfPlayer + 3);
  201. int currentSpeed = CheckInputUser("Введите скорость игрока", 1, 100, numOfPlayer + 4);
  202.  
  203. Player currentPlayer = new Player(currentName, currentAvatar, currentColor, currentWeigth, currentSpeed);
  204. Player[] currentClub = new Player[Club.Length + 1];
  205.  
  206. for (int i = 0; i < Club.Length; i++)
  207. {
  208. currentClub[i] = Club[i];
  209. }
  210.  
  211. currentClub[Club.Length] = currentPlayer;
  212. Club = currentClub;
  213. }
  214. public void DelPlayer(int numOfPlayer)
  215. {
  216. if (ReservePlayers().Length < 1)
  217. {
  218. Console.SetCursorPosition(0, numOfPlayer + 16);
  219. Console.WriteLine("Игроки удаляются только со скамейки запасных\n" +
  220. "Чтобы поместить игрока на скамейку, нажмите - F3");
  221. Console.ReadKey();
  222. }
  223. else
  224. {
  225. Console.SetCursorPosition(0, numOfPlayer + 16);
  226. Console.WriteLine("Игрока можно удалить только со скамейки запасных");
  227. int currentNumPlayer = CheckInputUser("Введите номер игрока", 1, ReservePlayers().Length, numOfPlayer);
  228.  
  229. int j = 0;
  230. for (int i = 0; i < Club.Length; i++)
  231. {
  232. if (Club[i].GetStatusPlayer())
  233. {
  234. j++;
  235. }
  236. if (j == currentNumPlayer)
  237. {
  238. Player[] currentClub = new Player[Club.Length - 1];
  239.  
  240. for (int i1 = 0; i1 < i; i1++)
  241. {
  242. currentClub[i1] = Club[i1];
  243. }
  244. for (int i1 = i; i1 < currentClub.Length; i1++)
  245. {
  246. currentClub[i1] = Club[i1 + 1];
  247. }
  248. Club = currentClub;
  249. }
  250. }
  251. }
  252. }
  253. public void ManagePlayer(int numOfPlayer)
  254. {
  255. Console.WriteLine("\n1 - Перемещение игрока на скамейку запасных ->");
  256. Console.WriteLine("2 - Перемещение игрока в основной состав <-");
  257. int currentNumMenu = CheckInputUser("Введите номер действия", 1, 2, numOfPlayer + 3);
  258. int currentNumPlayer;
  259. int j;
  260.  
  261. switch (currentNumMenu)
  262. {
  263. case 1:
  264. if (ActivePlayers().Length < 1)
  265. {
  266. Console.SetCursorPosition(0, numOfPlayer + 16);
  267. Console.WriteLine("Нет игроков в основном составе\n" +
  268. "Чтобы поместить игрока в основной состав, нажмите - F3");
  269. Console.ReadKey();
  270. }
  271. else
  272. {
  273. currentNumPlayer = CheckInputUser("Введите номер игрока", 1, ActivePlayers().Length, numOfPlayer + 4);
  274.  
  275. j = 0;
  276. for (int i = 0; i < Club.Length; i++)
  277. {
  278. if (Club[i].GetStatusPlayer() != true)
  279. {
  280. j++;
  281. }
  282.  
  283. if (j == currentNumPlayer)
  284. {
  285. Club[i].SetReservePlayer();
  286. break;
  287. }
  288. }
  289. }
  290. break;
  291. case 2:
  292. if (ReservePlayers().Length < 1)
  293. {
  294. Console.SetCursorPosition(0, numOfPlayer + 16);
  295. Console.WriteLine("Нет игроков на скамейке запасных\n" +
  296. "Чтобы поместить игрока на скамейку, нажмите - F3");
  297. Console.ReadKey();
  298. }
  299. else
  300. {
  301. currentNumPlayer = CheckInputUser("Введите номер игрока", 1, ReservePlayers().Length, numOfPlayer + 4);
  302.  
  303. j = 0;
  304. for (int i = 0; i < Club.Length; i++)
  305. {
  306. if (Club[i].GetStatusPlayer())
  307. {
  308. j++;
  309. }
  310.  
  311. if (j == currentNumPlayer)
  312. {
  313. Club[i].SetActivePlayer();
  314. break;
  315. }
  316. }
  317. }
  318. break;
  319. }
  320. }
  321. static int CheckInputUser(string text, int minValue, int maxValue, int numOfPlayer)
  322. {
  323. bool goodCheckInput;
  324. bool isCheckInput = true;
  325. int input = 1;
  326. Console.CursorVisible = true;
  327.  
  328. while (isCheckInput)
  329. {
  330. Console.SetCursorPosition(0, numOfPlayer + 11);
  331. Console.Write($"{text} от {minValue} до {maxValue}: ");
  332. string userInput = Console.ReadLine();
  333.  
  334. goodCheckInput = Int32.TryParse(userInput, out int value);
  335.  
  336. if (goodCheckInput != true || value < minValue || value > maxValue)
  337. {
  338. Console.WriteLine("Введите корректное число.");
  339. Console.ReadKey();
  340. Console.SetCursorPosition(0, numOfPlayer + 11);
  341. Console.WriteLine(" ");
  342. Console.WriteLine(" ");
  343. }
  344. else
  345. {
  346. input = value;
  347. isCheckInput = false;
  348. Console.SetCursorPosition(0, numOfPlayer + 12);
  349. Console.WriteLine(" ");
  350. }
  351. }
  352. return input;
  353. }
  354. static int CheckInputUser(string text, Player[] club, int numOfPlayer)
  355. {
  356. bool goodCheckInput;
  357. bool isCheckInput = true;
  358. int input = 1;
  359. Console.CursorVisible = true;
  360.  
  361. while (isCheckInput)
  362. {
  363. Console.SetCursorPosition(0, numOfPlayer + 11);
  364. Console.Write($"{text}: ");
  365. string userInput = Console.ReadLine();
  366.  
  367. goodCheckInput = Int32.TryParse(userInput, out int value);
  368.  
  369. if (goodCheckInput != true || value < 0 || value > club.Length)
  370. {
  371. Console.WriteLine("Введите корректное число.");
  372. Console.ReadKey();
  373. Console.SetCursorPosition(0, numOfPlayer + 11);
  374. Console.WriteLine(" ");
  375. Console.WriteLine(" ");
  376. }
  377. else
  378. {
  379. input = value;
  380. isCheckInput = false;
  381. Console.SetCursorPosition(0, numOfPlayer + 12);
  382. Console.WriteLine(" ");
  383. }
  384. }
  385. return input;
  386. }
  387. static string CheckInputUser(string text, int numOfPlayer, int maxLenghtName)
  388. {
  389. bool isCheckInput = true;
  390. string input = "х";
  391. Console.CursorVisible = true;
  392.  
  393. while (isCheckInput)
  394. {
  395. Console.SetCursorPosition(0, numOfPlayer + 11);
  396. Console.Write($"{text}: ");
  397. string userInput = Console.ReadLine();
  398.  
  399. if (userInput.Length <= maxLenghtName && userInput.Length > 0)
  400. {
  401. input = userInput;
  402. isCheckInput = false;
  403. }
  404. else
  405. {
  406. Console.WriteLine($"Введите имя не более {maxLenghtName} символов");
  407. Console.ReadKey();
  408. Console.SetCursorPosition(0, numOfPlayer + 11);
  409. Console.WriteLine(" ");
  410. Console.WriteLine(" ");
  411. }
  412. }
  413. return input;
  414. }
  415. static char CheckInputUser(string text, int numOfPlayer)
  416. {
  417. bool isCheckInput = true;
  418. char input = ' ';
  419. Console.CursorVisible = true;
  420.  
  421. while (isCheckInput)
  422. {
  423. Console.SetCursorPosition(0, numOfPlayer + 11);
  424. Console.Write($"{text}: ");
  425. string userInput = Console.ReadLine();
  426.  
  427. if (userInput.Length == 1)
  428. {
  429. input = Convert.ToChar(userInput);
  430. isCheckInput = false;
  431. }
  432. else
  433. {
  434. Console.WriteLine($"Введите один символ");
  435. Console.ReadKey();
  436. Console.SetCursorPosition(0, numOfPlayer + 11);
  437. Console.WriteLine(" ");
  438. Console.WriteLine(" ");
  439. }
  440. }
  441. return input;
  442. }
  443. static ConsoleColor CheckInputUserColor(string text, int numOfPlayer)
  444. {
  445. bool isCheckInput = true;
  446. ConsoleColor input = ConsoleColor.White;
  447. Console.CursorVisible = true;
  448.  
  449. while (isCheckInput)
  450. {
  451. Console.SetCursorPosition(0, numOfPlayer + 16);
  452. Console.WriteLine("Варианты цветов: White, Red, Blue, Green, Gray, Cyan, Magenta, Yellow\n" +
  453. "Также, вы можете добавить приставку Dark (кроме White). Например, DarkRed.");
  454. Console.SetCursorPosition(0, numOfPlayer + 11);
  455. Console.Write($"{text}: ");
  456. string userInput = Console.ReadLine().ToLower();
  457. switch (userInput)
  458. {
  459. case "white": input = ConsoleColor.White; isCheckInput = false; break;
  460. case "red": input = ConsoleColor.Red; isCheckInput = false; break;
  461. case "blue": input = ConsoleColor.Blue; isCheckInput = false; break;
  462. case "green": input = ConsoleColor.Green; isCheckInput = false; break;
  463. case "gray": input = ConsoleColor.Gray; isCheckInput = false; break;
  464. case "cyan": input = ConsoleColor.Cyan; isCheckInput = false; break;
  465. case "magenta": input = ConsoleColor.Magenta; isCheckInput = false; break;
  466. case "yellow": input = ConsoleColor.Yellow; isCheckInput = false; break;
  467.  
  468. case "darkred": input = ConsoleColor.DarkRed; isCheckInput = false; break;
  469. case "darkblue": input = ConsoleColor.DarkBlue; isCheckInput = false; break;
  470. case "darkgreen": input = ConsoleColor.DarkGreen; isCheckInput = false; break;
  471. case "darkgray": input = ConsoleColor.DarkGray; isCheckInput = false; break;
  472. case "darkcyan": input = ConsoleColor.DarkCyan; isCheckInput = false; break;
  473. case "darkmagenta": input = ConsoleColor.DarkMagenta; isCheckInput = false; break;
  474. case "darkyellow": input = ConsoleColor.DarkYellow; isCheckInput = false; break;
  475.  
  476. default:
  477. Console.WriteLine("Вы ввели не корректный цвет");
  478. Console.ReadKey();
  479. Console.SetCursorPosition(0, numOfPlayer + 11);
  480. Console.WriteLine(" ");
  481. Console.WriteLine(" ");
  482. break;
  483. }
  484. Console.SetCursorPosition(0, numOfPlayer + 16);
  485. Console.WriteLine(" ");
  486. Console.WriteLine(" ");
  487. }
  488. return input;
  489. }
  490. }
  491. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement