Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Collections.Generic;
- namespace CSLight
- {
- class Program
- {
- static void Main()
- {
- Aquarium aquarium = new Aquarium();
- aquarium.Start();
- Console.ReadKey();
- }
- }
- class Aquarium
- {
- private static Random _random = new Random();
- private List<Fish> _aquarium = new List<Fish>();
- public Aquarium()
- {
- int fishCount = _random.Next(1, 11);
- for (int i = 0; i < fishCount; i++)
- {
- _aquarium.Add(new Fish());
- }
- }
- public void Start()
- {
- int userChoice = 0;
- string userInput = null;
- while (userChoice != 3)
- {
- Console.WriteLine("1) Добавить рыбку\n2) Удалить рыбку\n3) Запустить симуляцию\n");
- userChoice = CheckInput(userChoice, userInput, 1, 3);
- switch (userChoice)
- {
- case 1:
- AddFish();
- break;
- case 2:
- RemoveFish();
- break;
- case 3:
- continue;
- }
- }
- while (_aquarium.Count > 0)
- {
- ShowInfo();
- Live();
- System.Threading.Thread.Sleep(1000);
- }
- Console.WriteLine("\n\nРыбки закончились :((");
- }
- private int CheckInput(int userChoice, string userInput, int minValue, int maxValue)
- {
- bool isInputInorrect = true;
- while (isInputInorrect)
- {
- Console.Write("Введите команду: ");
- userInput = Console.ReadLine();
- if (int.TryParse(userInput, out userChoice) && userChoice <= maxValue && userChoice >= minValue)
- isInputInorrect = false;
- }
- return userChoice;
- }
- private void Live()
- {
- for (int i = 0; i < _aquarium.Count; i++)
- {
- _aquarium[i].SkipSecond();
- if (_aquarium[i].Health < 0)
- {
- DeleteFish(i);
- i -= 1;
- }
- }
- }
- private void DeleteFish(int id)
- {
- Console.WriteLine($"Рыбка {_aquarium[id].Name} умерла :(");
- _aquarium.RemoveAt(id);
- }
- private void ShowInfo()
- {
- Console.WriteLine("\n");
- for (int i = 0; i < _aquarium.Count; i++)
- {
- Console.WriteLine($"{i + 1}) Имя - {_aquarium[i].Name}, Здоровье - {_aquarium[i].Health}");
- }
- Console.WriteLine("\n");
- }
- private void AddFish()
- {
- Console.Write("Введите имя: ");
- string name = Console.ReadLine();
- _aquarium.Add(new Fish(name));
- }
- private void RemoveFish()
- {
- int deletedId = -1;
- string userInput;
- while (_aquarium.Count > 0 && (deletedId < 1 || deletedId >= _aquarium.Count + 1))
- {
- ShowInfo();
- Console.Write("Введите номер рыбы: ");
- userInput = Console.ReadLine();
- if (int.TryParse(userInput, out deletedId))
- continue;
- }
- if (_aquarium.Count > 0)
- _aquarium.RemoveAt(deletedId - 1);
- else
- Console.WriteLine("\n\nНечего удалять");
- }
- }
- class Fish
- {
- private static Random _random = new Random();
- private static List<string> _namesVariants = new List<string>() { "Дора", "Ирбис", "Каунти", "Фин", "Плотти", "Шугард", "Пирс" };
- private int _damagePerSecond;
- public string Name { get; private set; }
- public int Health { get; private set; }
- public Fish()
- {
- Name = _namesVariants[_random.Next(0, _namesVariants.Count)];
- Health = _random.Next(70, 121);
- _damagePerSecond = _random.Next(4, 11);
- }
- public Fish(string name)
- {
- Name = name;
- Health = _random.Next(70, 121);
- _damagePerSecond = _random.Next(4, 11);
- }
- public void SkipSecond()
- {
- Health -= _damagePerSecond;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment