Advertisement
Ash_HeLiX

Untitled

Oct 20th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //6 Оружейный арсенал. 5 баллов.
  7. //Сделайте 3 функции:
  8. //Удаление элемента из массива.
  9. //Добавление элемента в массив.
  10. //Экипировка орудия
  11. //Не используйте готовые функции!
  12.  
  13. namespace Home6
  14. {
  15. class Program
  16. {
  17. static void Main(string[] args)
  18. {
  19. string[,] dataTableWeapon = { { "Плазмомет «Disruptor»", "150" }, { "Tri-bolt «Тройник»", "136" }, { "Тяжелый пулемет «Heavy Machinegun»", "120" } };
  20. string[] menu = { "Добавить", "Удалить", "Экипировать", "Выход" };
  21. int index = 0;
  22. string currentMenu = "menu";
  23. string nameWeapon = dataTableWeapon[0, 0];
  24. string damageWeapon = dataTableWeapon[0, 1];
  25.  
  26. ShowEqupment(nameWeapon, damageWeapon);
  27.  
  28. while (true)
  29. {
  30. Console.SetCursorPosition(0, 3);
  31. Console.ResetColor();
  32. Console.WriteLine("\t\tМеню:");
  33.  
  34. switch (currentMenu)
  35. {
  36. case "menu":
  37. index = ShomMenu(menu, index);
  38. ControlMenu(ref index, ref dataTableWeapon, ref currentMenu, menu);
  39. break;
  40.  
  41. case "ShowDataTable":
  42. index = ShomMenu(dataTableWeapon, index);
  43. ControlMenu(ref index, ref dataTableWeapon, ref currentMenu, dataTableWeapon);
  44. break;
  45.  
  46. case "deleteDataTable":
  47. index = ShomMenu(dataTableWeapon, index);
  48. ControlMenu(ref index, ref dataTableWeapon, ref currentMenu, dataTableWeapon);
  49. break;
  50.  
  51. default:
  52. break;
  53. }
  54. }
  55. }
  56.  
  57. static int ShomMenu(string[] menu, int index)
  58. {
  59. for (int i = 0; i < menu.Length; i++)
  60. {
  61. if (index == i)
  62. {
  63. Console.BackgroundColor = ConsoleColor.White;
  64. Console.ForegroundColor = ConsoleColor.Black;
  65. }
  66.  
  67. Console.WriteLine(menu[i]);
  68. Console.ResetColor();
  69. }
  70.  
  71. return index;
  72. }
  73.  
  74. static void ControlMenu(ref int index, ref string[,] dataTableWeapon, ref string currentMenu, string[] menu)
  75. {
  76. ConsoleKeyInfo userInput = Console.ReadKey(true);
  77.  
  78. switch (userInput.Key)
  79. {
  80. case ConsoleKey.UpArrow:
  81. if (index != 0) index--;
  82. break;
  83.  
  84. case ConsoleKey.DownArrow:
  85. if (index != menu.Length - 1) index++;
  86. break;
  87.  
  88. case ConsoleKey.Enter:
  89. dataTableWeapon = SelectElementofMenu(index, dataTableWeapon, ref currentMenu);
  90. index = 0;
  91. break;
  92.  
  93. default:
  94. break;
  95. }
  96. }
  97.  
  98. static int ShomMenu(string[,] menu, int index)
  99. {
  100. ClearArea();
  101.  
  102. for (int i = 0; i < menu.GetLength(0); i++)
  103. {
  104. if (index == i)
  105. {
  106. Console.BackgroundColor = ConsoleColor.White;
  107. Console.ForegroundColor = ConsoleColor.Black;
  108. }
  109.  
  110. Console.WriteLine(menu[i, 0] + "-" + menu[i, 1]);
  111. Console.ResetColor();
  112. }
  113.  
  114. return index;
  115. }
  116.  
  117. static void ControlMenu(ref int index, ref string[,] dataTableWeapon, ref string currentMenu, string[,] menu)
  118. {
  119. ConsoleKeyInfo userInput = Console.ReadKey(true);
  120.  
  121. switch (userInput.Key)
  122. {
  123. case ConsoleKey.UpArrow:
  124. if (index != 0) index--;
  125. break;
  126.  
  127. case ConsoleKey.DownArrow:
  128. if (index != menu.GetLength(0) - 1) index++;
  129. break;
  130.  
  131. case ConsoleKey.Enter:
  132. switch (currentMenu)
  133. {
  134. case "ShowDataTable":
  135. ShowEqupment(menu[index, 0], menu[index, 1]);
  136. currentMenu = "menu";
  137. ShowMessage($"Вы успешно экипировали {menu[index, 0]} - {menu[index, 1]}", ConsoleColor.Yellow);
  138. break;
  139.  
  140. case "deleteDataTable":
  141. dataTableWeapon = DeleteElementfromFile(dataTableWeapon, index);
  142. currentMenu = "menu";
  143. break;
  144.  
  145. default:
  146. break;
  147. }
  148. index = 0;
  149. break;
  150.  
  151. default:
  152. break;
  153. }
  154. }
  155.  
  156. static void ShowEqupment(string nameWeapon, string damageWeapon)
  157. {
  158. Console.SetCursorPosition(0, 0);
  159. Console.WriteLine("\t\tЭкипировано:\n" +
  160. $"Оружие: {nameWeapon}\t\t\t\t\t \n" +
  161. $"Урон: {damageWeapon}\t ");
  162. }
  163.  
  164. static string[,] SelectElementofMenu(int index, string[,] dataTableWeapon, ref string currentMenu)
  165. {
  166. switch (index)
  167. {
  168. case 0:
  169. dataTableWeapon = AddElemetToFile(dataTableWeapon);
  170. break;
  171.  
  172. case 1:
  173. currentMenu = ShowElementfromFile(dataTableWeapon, "deleteDataTable");
  174. break;
  175.  
  176. case 2:
  177. currentMenu = ShowElementfromFile(dataTableWeapon, "ShowDataTable");
  178. break;
  179.  
  180. case 3:
  181. Console.Clear();
  182. Environment.Exit(0);
  183. break;
  184.  
  185. default:
  186. break;
  187. }
  188.  
  189. return dataTableWeapon;
  190. }
  191.  
  192. static string[,] AddElemetToFile(string[,] array)
  193. {
  194. ClearArea();
  195.  
  196. Console.Write("Добавление оружия.\nВведите название: ");
  197. string name = Console.ReadLine();
  198.  
  199. Console.Write("Введите урон: ");
  200. string post = Console.ReadLine();
  201.  
  202. string[,] tempArray = new string[array.GetLength(0) + 1, array.GetLength(1)];
  203.  
  204. for (int i = 0; i < array.GetLength(0); i++)
  205. {
  206. for (int j = 0; j < array.GetLength(1); j++)
  207. {
  208. tempArray[i, j] = array[i, j];
  209. }
  210. }
  211.  
  212. array = tempArray;
  213.  
  214. array[array.GetLength(0) - 1, 0] = name;
  215. array[array.GetLength(0) - 1, 1] = post;
  216.  
  217. ShowMessage($"Вы добавили {name} - {post} успешно", ConsoleColor.Yellow);
  218.  
  219. return array;
  220. }
  221.  
  222. static string[,] DeleteElementfromFile(string[,] array, int index)
  223. {
  224. string[,] tempArray = new string[array.GetLength(0) - 1, array.GetLength(1)];
  225.  
  226. for (int i = 0; i < index; i++)
  227. {
  228. for (int j = 0; j < array.GetLength(1); j++)
  229. {
  230. tempArray[i, j] = array[i, j];
  231. }
  232. }
  233.  
  234. for (int i = index; i < tempArray.GetLength(0); i++)
  235. {
  236. for (int j = 0; j < array.GetLength(1); j++)
  237. {
  238. tempArray[i, j] = array[i + 1, j];
  239. }
  240. }
  241. ShowMessage($"Вы успешно удалили {array[array.GetLength(0) - 1, 0]} - {array[array.GetLength(0) - 1, 1]}", ConsoleColor.Yellow);
  242.  
  243. array = tempArray;
  244.  
  245. return array;
  246. }
  247.  
  248. static string ShowElementfromFile(string[,] dataTableWeapon, string currentMenu)
  249. {
  250. ClearArea();
  251.  
  252. Console.WriteLine("Список оружия");
  253.  
  254. for (int i = 0; i < dataTableWeapon.GetLength(0); i++)
  255. {
  256. Console.WriteLine((i + 1) + "." + dataTableWeapon[i, 0] + " - " + dataTableWeapon[i, 1]);
  257. }
  258.  
  259. return currentMenu;
  260. }
  261.  
  262. static void ShowMessage(string message, ConsoleColor color = ConsoleColor.Red)
  263. {
  264. ClearArea();
  265.  
  266. Console.SetCursorPosition(0, 8);
  267. Console.ForegroundColor = color;
  268. Console.WriteLine(message + "\t\t\t\t\t");
  269. Console.ResetColor();
  270. }
  271. static void ClearArea(int x = 0, int y = 9)
  272. {
  273. Console.SetCursorPosition(x, y);
  274.  
  275. for (int i = 0; i < 10; i++)
  276. {
  277. Console.ResetColor();
  278. Console.WriteLine("\t\t\t\t\t\t\t\t");
  279. }
  280.  
  281. Console.SetCursorPosition(x, y);
  282. }
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement