Advertisement
TwinFrame

Aquarium

Jan 24th, 2020
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.76 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Clight_23_OOP_Aquarium
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int frameLines = 20;
  10. int frameRows = 10;
  11. char frameSymbol = '#';
  12.  
  13. int maxLenghtFish = 3;
  14. int maxSumFish = 6;
  15. int maxHealth = 50;
  16. int maxLines = 50;
  17. int maxRows = 15;
  18. int stepOfLive = 1;
  19.  
  20. Fish fish = new Fish();
  21. Draw drawFish = new Draw();
  22. Aquarium aquarium = new Aquarium(fish);
  23.  
  24. bool isDraw = true;
  25.  
  26. while (isDraw)
  27. {
  28. Console.CursorVisible = false;
  29. DrawFrame(frameLines, frameRows, frameSymbol);
  30. drawFish.Drawfish(aquarium.Fishs);
  31. aquarium.ShowAllInfo(maxLenghtFish, frameLines);
  32.  
  33. Console.SetCursorPosition(0, frameRows + 1);
  34. Console.WriteLine("\nНажмите пробел, чтобы выполнить один шаг или зажмите, чтобы потекла жизнь.\n");
  35. Console.WriteLine("F1 - Добавить рыбу в акваирум");
  36. Console.WriteLine("F2 - Удалить рыбу из аквариума");
  37. Console.WriteLine("F3 - Изменить размеры аквариума.");
  38. Console.WriteLine("F5 - Выход.");
  39.  
  40. int currentSumFish = aquarium.Fishs.Length;
  41.  
  42. ConsoleKeyInfo key = Console.ReadKey(true);
  43. switch (key.Key)
  44. {
  45. case ConsoleKey.Spacebar:
  46. aquarium.StepOfLive(frameLines, frameRows, stepOfLive);
  47. aquarium.CheckOfLive(maxHealth, frameRows);
  48. break;
  49.  
  50. case ConsoleKey.F1:
  51. bool goodAdding = CheckSumFish(currentSumFish, maxSumFish, frameRows);
  52.  
  53. if (goodAdding)
  54. {
  55. int userHealthFish = CheckInputUser("Введите уровень жизни рыбы", 1, maxHealth, frameRows);
  56. string userTexture = CheckInputUser($"Введите изображение рыбы, не более {maxLenghtFish} символов", frameRows, maxLenghtFish);
  57. aquarium.AddFish(userHealthFish, 1, 1, userTexture);
  58. }
  59. break;
  60.  
  61. case ConsoleKey.F2:
  62. if (aquarium.Fishs.Length > 1)
  63. {
  64. int numFish = CheckInputUser("Введите номер рыбки", 1, currentSumFish, frameRows);
  65. aquarium.DelFish(numFish);
  66. }
  67. else
  68. {
  69. Console.SetCursorPosition(0, frameRows + 9);
  70. Console.WriteLine("Пустой аквариум скучен.\nСначала добавьте еще рыбу - нажмите F1.");
  71. Console.ReadKey();
  72. Console.SetCursorPosition(0, frameRows + 9);
  73. Console.WriteLine(" ");
  74. Console.WriteLine(" ");
  75. }
  76. break;
  77.  
  78. case ConsoleKey.F3:
  79. int currentLine = CheckInputUser("Введите длину аквариума", 5, maxLines, frameRows);
  80. Console.WriteLine();
  81. int currentRow = CheckInputUser("Введите ширину аквариума", 5, maxRows, frameRows);
  82.  
  83. frameLines = currentLine;
  84. frameRows = currentRow;
  85.  
  86. for (int i = 0; i < aquarium.Fishs.Length; i++)
  87. {
  88. aquarium.Fishs[i].SetFishCoord(1, 1);
  89. }
  90. break;
  91.  
  92. case ConsoleKey.F5:
  93. Console.SetCursorPosition(0, frameRows + 9);
  94. Console.WriteLine("Пока!");
  95. Console.ReadKey();
  96. Environment.Exit(0);
  97. break;
  98. }
  99. Console.Clear();
  100. }
  101. }
  102. static void DrawFrame(int frameLine, int frameRow, char symbol)
  103. {
  104. for (int j = 0; j < frameRow; j++)
  105. {
  106. for (int i = 0; i < frameLine; i++)
  107. {
  108. if ((i == 0 || j == 0) || (i == frameLine - 1 || j == frameRow - 1))
  109. {
  110. Console.SetCursorPosition(i, j);
  111. Console.Write(symbol);
  112. }
  113. }
  114. Console.WriteLine();
  115. }
  116. }
  117. static int CheckInputUser(string text, int minValue, int maxValue, int frameRows)
  118. {
  119. bool goodCheckInput;
  120. bool isCheckInput = true;
  121. int valueInput = 1;
  122. Console.CursorVisible = true;
  123.  
  124. while (isCheckInput)
  125. {
  126. Console.SetCursorPosition(0, frameRows + 9);
  127. Console.Write($"{text} от {minValue} до {maxValue}: ");
  128. string userInput = Console.ReadLine();
  129.  
  130. goodCheckInput = Int32.TryParse(userInput, out int value);
  131.  
  132. if (goodCheckInput != true || value < minValue || value > maxValue)
  133. {
  134. Console.WriteLine("Введите корректное число.");
  135. Console.ReadKey();
  136. Console.SetCursorPosition(0, frameRows + 9);
  137. Console.WriteLine(" ");
  138. Console.WriteLine(" ");
  139. }
  140. else
  141. {
  142. valueInput = value;
  143. isCheckInput = false;
  144. Console.SetCursorPosition(0, frameRows + 9);
  145. Console.WriteLine(" ");
  146. Console.WriteLine(" ");
  147.  
  148. }
  149. }
  150. return valueInput;
  151. }
  152. static string CheckInputUser(string text, int frameRows, int maxLenghtFish)
  153. {
  154. bool isCheckInput = true;
  155. string stringInput = "х";
  156. Console.CursorVisible = true;
  157.  
  158. while (isCheckInput)
  159. {
  160. Console.SetCursorPosition(0, frameRows + 9);
  161. Console.Write($"{text}: ");
  162. string userInput = Console.ReadLine();
  163.  
  164. if (userInput.Length <= maxLenghtFish && userInput.Length > 0)
  165. {
  166. stringInput = userInput;
  167. isCheckInput = false;
  168. }
  169. else
  170. {
  171. Console.WriteLine("Введите корректное число.");
  172. Console.ReadKey();
  173. Console.SetCursorPosition(0, frameRows + 9);
  174. Console.WriteLine(" ");
  175. Console.WriteLine(" ");
  176. }
  177. }
  178. return stringInput;
  179. }
  180. static bool CheckSumFish(int currnetSumFish, int maxSumFish, int frameRows)
  181. {
  182. bool valueInput;
  183. Console.CursorVisible = true;
  184.  
  185. if (currnetSumFish == maxSumFish)
  186. {
  187. Console.SetCursorPosition(0, frameRows + 9);
  188. Console.WriteLine("Добавление рыбы превысит лимит аквариума.\nСначала удалите минимум одну рыбу, для этого нажмите F2.");
  189. Console.ReadKey();
  190. Console.SetCursorPosition(0, frameRows + 9);
  191. Console.WriteLine(" ");
  192. Console.WriteLine(" ");
  193. valueInput = false;
  194. }
  195. else
  196. {
  197. valueInput = true;
  198. }
  199. return valueInput;
  200. }
  201. }
  202. class Aquarium
  203. {
  204. public Fish[] Fishs = new Fish[1];
  205. public Aquarium(Fish fish)
  206. {
  207. Fishs[0] = fish;
  208. }
  209. public void DelFish(int numFish)
  210. {
  211. Fish[] currentFishs = new Fish[Fishs.Length - 1];
  212. if (numFish == 1 && currentFishs.Length >= 1)
  213. {
  214. for (int i = 0; i < currentFishs.Length; i++)
  215. {
  216. currentFishs[i] = Fishs[i + 1];
  217. }
  218. Fishs = currentFishs;
  219. }
  220. else if (numFish == currentFishs.Length + 1 && currentFishs.Length >= 1)
  221. {
  222. for (int i = 0; i < currentFishs.Length; i++)
  223. {
  224. currentFishs[i] = Fishs[i];
  225. }
  226. Fishs = currentFishs;
  227. }
  228. else if (currentFishs.Length > 1)
  229. {
  230. for (int i = 0; i < numFish - 1; i++)
  231. {
  232. currentFishs[i] = Fishs[i];
  233. }
  234. for (int i = numFish - 1; i < currentFishs.Length; i++)
  235. {
  236. currentFishs[i] = Fishs[i + 1];
  237. }
  238. Fishs = currentFishs;
  239. }
  240. }
  241. public void ShowAllInfo(int maxLenghtFish, int frameLine)
  242. {
  243. for (int i = 0; i < Fishs.Length; i++)
  244. {
  245. Console.SetCursorPosition(frameLine + 3, i);
  246. Console.Write($"{i + 1}. ");
  247. Fishs[i].ShowInfo(maxLenghtFish);
  248. Console.WriteLine();
  249. }
  250. }
  251. public void AddFish(int health, int x, int y, string texture)
  252. {
  253. Fish currentFish = new Fish(health, x, y, texture);
  254. Fish[] currentFishs = new Fish[Fishs.Length + 1];
  255. for (int i = 0; i < Fishs.Length; i++)
  256. {
  257. currentFishs[i] = Fishs[i];
  258. }
  259. currentFishs[Fishs.Length] = currentFish;
  260. Fishs = currentFishs;
  261. }
  262. public void StepOfLive(int frameLines, int frameRows, int stepOfLive)
  263. {
  264. int dX = 0;
  265. int dY = 0;
  266. int currentX;
  267. int currentY;
  268. Random random = new Random();
  269.  
  270. for (int i = 0; i < Fishs.Length; i++)
  271. {
  272. currentX = Fishs[i].cursorX;
  273. currentY = Fishs[i].cursorY;
  274. dX = random.Next(-1, 2);
  275. dY = random.Next(-1, 2);
  276. int lenghtFish = Fishs[i].texture.Length;
  277.  
  278. if ((Fishs[i].cursorX + dX > 0 && Fishs[i].cursorY + dY > 0) &&
  279. (Fishs[i].cursorX + dX < frameLines - lenghtFish && Fishs[i].cursorY + dY < frameRows - 1))
  280. {
  281. currentX += dX;
  282. currentY += dY;
  283. }
  284.  
  285. Fishs[i].SetFishCoord(currentX, currentY);
  286. Fishs[i].SetFishHealth(Fishs[i].health - stepOfLive);
  287. }
  288. }
  289. public void CheckOfLive(int maxHealth, int frameRows)
  290. {
  291. int deathFish = 0;
  292. for (int i = 0; i < Fishs.Length; i++)
  293. {
  294. if (Fishs[i].health <= 0)
  295. {
  296. deathFish++;
  297. }
  298. }
  299. for (int j = 0; j < deathFish; j++)
  300. {
  301. for (int i = 0; i < Fishs.Length; i++)
  302. {
  303. if (Fishs[i].health <= 0 && Fishs.Length > 1)
  304. {
  305. DelFish(i + 1);
  306. }
  307. else if (Fishs[i].health <= 0 && Fishs.Length <= 1)
  308. {
  309. Fishs[i].SetFishHealth(maxHealth);
  310. Console.SetCursorPosition(0, frameRows + 9);
  311. Console.WriteLine("Аквариум без рыб скучноват:)");
  312. Console.ReadKey();
  313. Console.SetCursorPosition(0, frameRows + 9);
  314. Console.WriteLine(" ");
  315. }
  316. }
  317. }
  318. }
  319. }
  320. class Fish
  321. {
  322. public int health { get; private set; }
  323. public int cursorX { get; private set; }
  324. public int cursorY { get; private set; }
  325. public string texture { get; private set; }
  326.  
  327. public Fish(int health, int x, int y, string texture)
  328. {
  329. this.health = health;
  330. cursorX = x;
  331. cursorY = y;
  332. this.texture = texture;
  333. }
  334. public Fish()
  335. {
  336. health = 50;
  337. cursorX = 1;
  338. cursorY = 1;
  339. texture = "<->";
  340. }
  341. public void ShowInfo(int maxLenghtFish)
  342. {
  343. int currentLenght = maxLenghtFish - texture.Length;
  344. char[] spacingMassive = new char[currentLenght];
  345. for (int i = 0; i < currentLenght; i++)
  346. {
  347. spacingMassive[i] = ' ';
  348. }
  349. string spacing = new string(spacingMassive);
  350. Console.Write(texture + spacing + " - " + health);
  351. }
  352. public void SetFishCoord(int x, int y)
  353. {
  354. cursorX = x;
  355. cursorY = y;
  356. }
  357. public void SetFishHealth(int health)
  358. {
  359. this.health = health;
  360. }
  361. }
  362. class Draw
  363. {
  364. public void Drawfish(Fish[] currentFish)
  365. {
  366. for (int i = 0; i < currentFish.Length; i++)
  367. {
  368. Console.SetCursorPosition(currentFish[i].cursorX, currentFish[i].cursorY);
  369. Console.Write(currentFish[i].texture);
  370. }
  371. }
  372. }
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement