Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Classes
- {
- internal class Program
- {
- static void Main()
- {
- Aquarium aquarium = new Aquarium();
- aquarium.Live();
- }
- }
- public class Aquarium
- {
- private int _volume;
- private List<Fish> _fishes;
- private FishList _fishList ;
- public Aquarium()
- {
- _volume = 50;
- _fishList = new FishList();
- _fishes = new List<Fish>();
- Fill();
- }
- public void Live()
- {
- int turn = 1;
- Console.WriteLine("Turn :" + turn++);
- ShowAllFishes();
- bool isRunning = true;
- ConsoleKey menuLaunchKey = ConsoleKey.D0;
- string menuLaunch = "0";
- while (isRunning)
- {
- Console.WriteLine($"\nPress something for next turn... or {menuLaunch} for menu");
- if (Console.ReadKey().Key == menuLaunchKey)
- {
- Console.Clear();
- isRunning = LaunchMenu();
- continue;
- }
- Console.Clear();
- Console.WriteLine("Turn :" + turn++);
- GrowOldFishes();
- KillFishes();
- RemoveFishes();
- ShowAllFishes();
- }
- }
- private void Fill()
- {
- int fishListCount = _fishList.FishesList.Count;
- int minOccupiedVolume = _fishList.FishesList.Min(fish => fish.OccupiedVolume);
- for (int volume = 0; volume < _volume;)
- {
- Fish fish = _fishList.FishesList[UserUtil.GenerateRandomNumber(fishListCount)];
- if (volume + fish.OccupiedVolume > _volume)
- {
- fishListCount = fishListCount > minOccupiedVolume ? fishListCount-- : minOccupiedVolume;
- continue;
- }
- _fishes.Add(fish.Clone(fish.Name,fish.LifeExpectancy,fish.OccupiedVolume,fish.Image,fish.Color));
- volume += fish.OccupiedVolume;
- }
- }
- private bool LaunchMenu()
- {
- const string CommandAddFish = "1";
- const string CommandRemoveFish = "2";
- const string CommandShowFish = "3";
- const string CommandExitMenu = "4";
- const string CommandExit = "5";
- bool hasSelected = false;
- bool isRunning = true;
- while (hasSelected == false)
- {
- Console.Clear();
- Console.WriteLine($"Press {CommandAddFish} for add fish," +
- $"\n {CommandRemoveFish} for remove," +
- $"\n {CommandShowFish} for show," +
- $"\n {CommandExitMenu} for exit the menu" +
- $"\n or {CommandExit} for close aquarium");
- string userCommand = Console.ReadLine();
- switch (userCommand)
- {
- case CommandAddFish:
- IncreaseFishes();
- break;
- case CommandRemoveFish:
- DecreaseFishes();
- break;
- case CommandShowFish:
- Console.Clear();
- ShowAllFishes();
- Console.WriteLine("\nPress something..");
- Console.ReadKey();
- break;
- case CommandExitMenu:
- hasSelected = true;
- break;
- case CommandExit:
- hasSelected = true;
- isRunning = false;
- break;
- default:
- Console.WriteLine("Wrong key");
- break;
- }
- }
- return isRunning;
- }
- private void IncreaseFishes()
- {
- ShowListFishes();
- Console.Write("\nChoose type of fish to add: ");
- if (int.TryParse(Console.ReadLine(), out int typeNumber) && typeNumber >= 0 && typeNumber < _fishList.FishesList.Count)
- {
- Console.WriteLine("How many fish to add?");
- if (int.TryParse(Console.ReadLine(), out int amount) && amount > 0)
- {
- for (int i = 0; i < amount; i++)
- {
- _fishes.Add(_fishList.FishesList[typeNumber].Clone(_fishList.FishesList[typeNumber].Name, _fishList.FishesList[typeNumber].LifeExpectancy, _fishList.FishesList[typeNumber].OccupiedVolume,_fishList.FishesList[typeNumber].Image, _fishList.FishesList[typeNumber].Color));
- }
- }
- else
- {
- Console.WriteLine("Wrong amount");
- }
- }
- else
- {
- Console.WriteLine("Wrong type");
- }
- }
- private void DecreaseFishes()
- {
- ShowListFishes();
- Console.Write("\nChoose the type of fish to remove: ");
- if (int.TryParse(Console.ReadLine(), out int typeNumber) && typeNumber >= 0 && typeNumber < _fishList.FishesList.Count)
- {
- string fishName = _fishList.FishesList[typeNumber].Name;
- int nameCount = _fishes.Count(fish => fish.Name == fishName);
- Console.WriteLine($"How many fish to remove? There are {nameCount} fish of this type");
- if (int.TryParse(Console.ReadLine(), out int amount) && amount > 0)
- {
- if (amount > nameCount)
- {
- Console.WriteLine("The entered quantity exceeds the quantity of fish of this type in the aquarium, delete all fish of this type");
- Console.WriteLine("\nPress something..");
- Console.ReadKey();
- }
- amount = Math.Min(amount, nameCount);
- for (int i = _fishes.Count - 1; i >= 0; i--)
- {
- if (_fishes[i].Name == fishName && amount > 0)
- {
- _fishes.RemoveAt(i);
- amount--;
- }
- }
- }
- else
- {
- Console.WriteLine("Wrong amount");
- }
- }
- else
- {
- Console.WriteLine("Wrong type");
- }
- }
- private void ShowListFishes()
- {
- Console.Write("There is the following fish: ");
- for (int i = 0; i < _fishList.FishesList.Count; i++)
- {
- Console.Write($"{i} - {_fishList.FishesList[i].Name} ");
- }
- }
- private void ShowAllFishes()
- {
- foreach (var fish in _fishes)
- {
- Console.ForegroundColor = fish.Color;
- int minLeftPosition = 30;
- int maxLeftPosition = 110;
- int minTopPosition = 1;
- int maxTopPosition = 14;
- Console.SetCursorPosition(UserUtil.GenerateRandomNumber(minLeftPosition, maxLeftPosition + 1), UserUtil.GenerateRandomNumber(minTopPosition, maxTopPosition + 1));
- Console.WriteLine(fish.Image);
- }
- var ordered = _fishes.OrderBy(fish => fish.Name).ThenBy(fish => fish.Age);
- int leftPosition = 0;
- int topPosition = 4;
- Console.SetCursorPosition(leftPosition, topPosition);
- foreach (var fish in ordered)
- {
- Console.ForegroundColor = fish.Color;
- Console.WriteLine($"{fish.Name} - {fish.Age}({fish.LifeExpectancy})");
- }
- Console.ResetColor();
- }
- private void GrowOldFishes()
- {
- foreach (var fish in _fishes)
- {
- fish.IncreaseAge();
- }
- }
- private void KillFishes()
- {
- int fishCount = _fishList.FishesList.Count;
- int quantityPerLiter = 10;
- int maxFishCount = _volume * quantityPerLiter;
- int volumeFish = _fishes.Sum(fish => fish.OccupiedVolume);
- int fullMultiplier = VerifyFullVolume(volumeFish);
- if (volumeFish >= maxFishCount)
- {
- foreach (var fish in _fishes)
- {
- fish.Kill();
- }
- }
- else
- {
- foreach (var fish in _fishes)
- {
- int probabilityDeath = VerifyAge(fish);
- probabilityDeath *= VerifyAlone(fish);
- probabilityDeath *= fullMultiplier;
- int maxChance = 100;
- if (UserUtil.GenerateRandomNumber(maxChance + 1) < probabilityDeath)
- {
- fish.Kill();
- }
- }
- }
- }
- private int VerifyFullVolume(int volumeFish)
- {
- int multiplier = 1;
- int fullMultiplier = 2;
- if (volumeFish > _volume)
- {
- multiplier = fullMultiplier;
- }
- return multiplier;
- }
- private int VerifyAge(Fish fish)
- {
- int probabilityDeath = 2;
- int firstHalf = 2;
- int secondHalf = 5;
- int longLife = 90;
- int lifePeriod = 2;
- if (fish.Age < fish.LifeExpectancy / lifePeriod)
- {
- probabilityDeath = firstHalf;
- }
- else if (fish.Age < fish.LifeExpectancy)
- {
- probabilityDeath = secondHalf;
- }
- else if (fish.Age >= fish.LifeExpectancy)
- {
- probabilityDeath = longLife;
- }
- return probabilityDeath;
- }
- private int VerifyAlone(Fish fish)
- {
- string fishName = fish.Name;
- int nameCount = _fishes.Count(fish => fish.Name == fishName);
- int multiplier = 1;
- int aloneMultiplier = 2;
- if (nameCount == 1)
- {
- multiplier = aloneMultiplier;
- }
- return multiplier;
- }
- private void RemoveFishes()
- {
- int quantityPerLiter = 10;
- int maxFishCount = _volume * quantityPerLiter;
- int volumeFish = _fishes.Sum(fish => fish.OccupiedVolume);
- if (volumeFish >= maxFishCount)
- {
- Console.WriteLine("Too many fish. All died.");
- }
- int numberDead = 0;
- for (int i = _fishes.Count - 1; i >= 0; i--)
- {
- if (_fishes[i].IsAlive == false)
- {
- _fishes.RemoveAt(i);
- numberDead++;
- }
- }
- int leftPosition = 0;
- int topPosition = 2;
- Console.SetCursorPosition(leftPosition, topPosition);
- Console.WriteLine($"fish died: {numberDead}");
- }
- }
- public class Fish
- {
- public Fish(string name, int lifeExpectancy, int occupiedVolume, string image, ConsoleColor color)
- {
- Name = name;
- LifeExpectancy = lifeExpectancy;
- OccupiedVolume = occupiedVolume;
- Image = image;
- Color = color;
- IsAlive = true;
- InitiateAge();
- }
- public string Name { get; protected set; }
- public string Image { get; protected set; }
- public int OccupiedVolume { get; protected set; }
- public int LifeExpectancy { get; protected set; }
- public int Age { get; protected set; }
- public bool IsAlive { get; protected set; }
- public ConsoleColor Color { get; protected set; }
- public Fish Clone(string name, int lifeExpectancy, int occupiedVolume, string image, ConsoleColor color)
- {
- Fish fish = new Fish( name, lifeExpectancy, occupiedVolume, image, color);
- return fish;
- }
- public void IncreaseAge()
- {
- Age++;
- }
- public void Kill()
- {
- IsAlive = false;
- }
- private void InitiateAge()
- {
- int minAge = 1;
- int minLifeSpan = 10;
- int maxAge = LifeExpectancy>minLifeSpan? LifeExpectancy - minLifeSpan:1;
- Age = UserUtil.GenerateRandomNumber(minAge, maxAge + 1);
- }
- }
- public class FishList : Fish
- {
- public List<Fish> FishesList;
- public FishList(string name = "", int lifeExpectancy = 0, int occupiedVolume = 0, string image="", ConsoleColor color = 0) : base(name, lifeExpectancy, occupiedVolume, image , color)
- {
- FishesList = new List<Fish>()
- {
- new Fish("Guppy", 30, 2, ">-->", ConsoleColor.Cyan),
- new Fish("Paracheirodon", 48, 2, ">==>",ConsoleColor.Blue),
- new Fish("Xiphophorus",60,4 ,"_>=>",ConsoleColor.Red),
- new Fish("Yucatan molly",30,4, ">WW>",ConsoleColor.DarkCyan),
- new Fish("Catfish", 96,8,">=>_",ConsoleColor.Gray ),
- new Fish("Goldfish",120,12,">=@>",ConsoleColor.DarkYellow)
- };
- }
- }
- public class UserUtil
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int min, int max)
- {
- return s_random.Next(min, max);
- }
- public static int GenerateRandomNumber(int max)
- {
- return s_random.Next(max);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement