Advertisement
TwinFrame

Aquarium ver2

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