GPbl3YH

CSLight #36

Feb 10th, 2021 (edited)
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace CSLight
  7. {
  8.  
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             Aquarium aquarium = new Aquarium();
  14.             aquarium.Start();
  15.             Console.ReadKey();
  16.         }
  17.     }
  18.  
  19.     class Aquarium
  20.     {
  21.         private static Random _random = new Random();
  22.         private List<Fish> _aquarium = new List<Fish>();
  23.  
  24.         public Aquarium()
  25.         {
  26.             int fishCount = _random.Next(1, 11);
  27.             for (int i = 0; i < fishCount; i++)
  28.             {
  29.                 _aquarium.Add(new Fish());
  30.             }
  31.         }
  32.  
  33.         public void Start()
  34.         {
  35.             int userChoice = 0;
  36.             string userInput = null;
  37.  
  38.             while (userChoice != 3)
  39.             {
  40.                 Console.WriteLine("1) Добавить рыбку\n2) Удалить рыбку\n3) Запустить симуляцию\n");
  41.  
  42.                 userChoice = CheckInput(userChoice, userInput, 1, 3);
  43.  
  44.                 switch (userChoice)
  45.                 {
  46.                     case 1:
  47.                         AddFish();
  48.                         break;
  49.                     case 2:
  50.                         RemoveFish();
  51.                         break;
  52.                     case 3:
  53.                         continue;
  54.                 }
  55.             }
  56.  
  57.             while (_aquarium.Count > 0)
  58.             {
  59.                 ShowInfo();
  60.                 Live();
  61.                 System.Threading.Thread.Sleep(1000);
  62.             }
  63.  
  64.             Console.WriteLine("\n\nРыбки закончились :((");
  65.         }
  66.  
  67.         private int CheckInput(int userChoice, string userInput, int minValue, int maxValue)
  68.         {
  69.             bool isInputInorrect = true;
  70.             while (isInputInorrect)
  71.             {
  72.                 Console.Write("Введите команду: ");
  73.                 userInput = Console.ReadLine();
  74.                 if (int.TryParse(userInput, out userChoice) && userChoice <= maxValue && userChoice >= minValue)
  75.                     isInputInorrect = false;
  76.             }
  77.  
  78.             return userChoice;
  79.         }
  80.  
  81.         private void Live()
  82.         {
  83.             for (int i = 0; i < _aquarium.Count; i++)
  84.             {
  85.                 _aquarium[i].SkipSecond();
  86.  
  87.                 if (_aquarium[i].Health < 0)
  88.                 {
  89.                     DeleteFish(i);
  90.                     i -= 1;
  91.                 }
  92.             }
  93.         }
  94.  
  95.         private void DeleteFish(int id)
  96.         {
  97.             Console.WriteLine($"Рыбка {_aquarium[id].Name} умерла :(");
  98.             _aquarium.RemoveAt(id);
  99.         }
  100.  
  101.         private void ShowInfo()
  102.         {
  103.             Console.WriteLine("\n");
  104.             for (int i = 0; i < _aquarium.Count; i++)
  105.             {
  106.                 Console.WriteLine($"{i + 1}) Имя - {_aquarium[i].Name}, Здоровье - {_aquarium[i].Health}");
  107.             }
  108.             Console.WriteLine("\n");
  109.         }
  110.  
  111.         private void AddFish()
  112.         {
  113.             Console.Write("Введите имя: ");
  114.             string name = Console.ReadLine();
  115.             _aquarium.Add(new Fish(name));
  116.         }
  117.  
  118.         private void RemoveFish()
  119.         {
  120.             int deletedId = -1;
  121.             string userInput;
  122.  
  123.             while (_aquarium.Count > 0 && (deletedId < 1 || deletedId >= _aquarium.Count + 1))
  124.             {
  125.                 ShowInfo();
  126.                 Console.Write("Введите номер рыбы: ");
  127.                 userInput = Console.ReadLine();
  128.  
  129.                 if (int.TryParse(userInput, out deletedId))
  130.                     continue;
  131.             }
  132.  
  133.             if (_aquarium.Count > 0)
  134.                 _aquarium.RemoveAt(deletedId - 1);
  135.             else
  136.                 Console.WriteLine("\n\nНечего удалять");
  137.         }
  138.     }
  139.  
  140.     class Fish
  141.     {
  142.         private static Random _random = new Random();
  143.         private static List<string> _namesVariants = new List<string>() { "Дора", "Ирбис", "Каунти", "Фин", "Плотти", "Шугард", "Пирс" };
  144.         private int _damagePerSecond;
  145.  
  146.         public string Name { get; private set; }
  147.         public int Health { get; private set; }
  148.  
  149.         public Fish()
  150.         {
  151.             Name = _namesVariants[_random.Next(0, _namesVariants.Count)];
  152.             Health = _random.Next(70, 121);
  153.             _damagePerSecond = _random.Next(4, 11);
  154.         }
  155.  
  156.         public Fish(string name)
  157.         {
  158.             Name = name;
  159.             Health = _random.Next(70, 121);
  160.             _damagePerSecond = _random.Next(4, 11);
  161.         }
  162.  
  163.         public void SkipSecond()
  164.         {
  165.             Health -= _damagePerSecond;
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment