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> FishList;
- private List<Fish> Fish;
- public Aquarium()
- {
- _volume = 50;
- FishList = new List<Fish>()
- {
- new PoeciliaReticulata(),
- new Paracheirodon(),
- new Xiphophorus(),
- new PoeciliaVelifera(),
- new Catfish(),
- new Goldfish()
- };
- Fish = new List<Fish>();
- Fill();
- }
- public void Live()
- {
- int turn = 1;
- Console.WriteLine("Turn :" + turn++);
- ShowAll();
- bool isRuning = true;
- while (isRuning)
- {
- Console.WriteLine("\nPress something for next turn... or 0 for menu");
- while (Console.ReadKey().Key == ConsoleKey.D0)
- {
- Console.Clear();
- LaunchMenu(out isRuning);
- }
- if (isRuning == false)
- {
- continue;
- }
- Console.Clear();
- Console.WriteLine("Turn :" + turn++);
- GrowOld();
- Kill();
- Remove();
- ShowAll();
- }
- }
- private void Fill()
- {
- int fishListCount = FishList.Count;
- int minOccupiedVolume = FishList.Min(fish => fish.OccupiedVolume);
- for (int volume = 0; volume < _volume;)
- {
- Fish fish = FishList[UserUtil.GenerateRandomNumber(fishListCount)];
- if (volume + fish.OccupiedVolume > _volume)
- {
- fishListCount = fishListCount > minOccupiedVolume ? fishListCount-- : minOccupiedVolume;
- continue;
- }
- Fish.Add(fish.Clone());
- volume += fish.OccupiedVolume;
- }
- }
- private void LaunchMenu(out bool isRuning)
- {
- const string CommandAddFish = "1";
- const string CommandRemoveFish = "2";
- const string CommandShowFish = "3";
- const string CommandExitMenu = "4";
- const string CommandExit = "5";
- isRuning = true;
- bool hasSelected = false;
- 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 \nor {CommandExit} for close aquarium");
- string userCommand = Console.ReadLine();
- switch (userCommand)
- {
- case CommandAddFish:
- Increase();
- break;
- case CommandRemoveFish:
- Decrease();
- break;
- case CommandShowFish:
- ShowAll();
- break;
- case CommandExitMenu:
- hasSelected = true;
- break;
- case CommandExit:
- hasSelected = true;
- isRuning = false;
- break;
- default:
- Console.WriteLine("Wrong key");
- break;
- }
- Console.WriteLine("\nPress something for next turn...");
- }
- }
- private void Increase()
- {
- ShowList();
- Console.Write("\nChoose type of fish to add: ");
- if (int.TryParse(Console.ReadLine(), out int typeNumber) && typeNumber >= 0 && typeNumber < FishList.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++)
- {
- Fish.Add(FishList[typeNumber].Clone());
- }
- }
- else
- {
- Console.WriteLine("Wrong amount");
- }
- }
- else
- {
- Console.WriteLine("Wrong type");
- }
- }
- private void Decrease()
- {
- ShowList();
- Console.Write("\nChoose the type of fish to remove: ");
- if (int.TryParse(Console.ReadLine(), out int typeNumber) && typeNumber >= 0 && typeNumber < FishList.Count)
- {
- string fishName = Fish[typeNumber].Name;
- int nameCount = Fish.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");
- }
- amount = Math.Min(amount, nameCount);
- for (int i = Fish.Count; i > 0; i--)
- {
- if (Fish[i].Name == fishName && amount > 0)
- {
- Fish.RemoveAt(i);
- amount--;
- }
- }
- }
- else
- {
- Console.WriteLine("Wrong amount");
- }
- }
- else
- {
- Console.WriteLine("Wrong type");
- }
- }
- private void ShowList()
- {
- Console.Write("There is the following fish: ");
- for (int i = 0; i < FishList.Count; i++)
- {
- Console.Write($"{i} - {FishList[i].Name} ");
- }
- }
- private void ShowAll()
- {
- foreach (var fish in Fish)
- {
- Dye(fish.Name);
- 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 = Fish.OrderBy(fish => fish.Name).ThenBy(fish => fish.Age);
- int leftPosition = 0;
- int topPosition = 4;
- Console.SetCursorPosition(leftPosition, topPosition);
- foreach (var fish in ordered)
- {
- Dye(fish.Name);
- Console.WriteLine($"{fish.Name} - {fish.Age}({fish.LifeExpectancy})");
- }
- Console.ResetColor();
- }
- private void Dye(string name)
- {
- switch (name)
- {
- case "Guppy":
- Console.ForegroundColor = ConsoleColor.Cyan;
- break;
- case "Paracheirodon":
- Console.ForegroundColor = ConsoleColor.Blue;
- break;
- case "Xiphophorus":
- Console.ForegroundColor = ConsoleColor.Red;
- break;
- case "Yucatan molly":
- Console.ForegroundColor = ConsoleColor.DarkCyan;
- break;
- case "Catfish":
- Console.ForegroundColor = ConsoleColor.Gray;
- break;
- case "Goldfish":
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- break;
- }
- }
- private void GrowOld()
- {
- foreach (var fish in Fish)
- {
- fish.IncreaseAge();
- }
- }
- private void Kill()
- {
- int fishCount = FishList.Count;
- int quantityPerLiter = 10;
- int maxFishCount = _volume * quantityPerLiter;
- int volumeFish = Fish.Sum(fish => fish.OccupiedVolume);
- int aloneMultiplier = 2;
- int fullMultiplier = 2;
- int maxChance = 100;
- int probabilityDeath = 2;
- foreach (var fish in Fish)
- {
- if (fish.Age < fish.LifeExpectancy / 2)
- {
- probabilityDeath = (int)ProbabilityDeath.normal;
- }
- else if (fish.Age < fish.LifeExpectancy)
- {
- probabilityDeath = (int)ProbabilityDeath.secondHalf;
- }
- else if (fish.Age >= fish.LifeExpectancy)
- {
- probabilityDeath = (int)ProbabilityDeath.longLife;
- }
- string fishName = fish.Name;
- int nameCount = Fish.Count(fish => fish.Name == fishName);
- if (nameCount == 1)
- {
- probabilityDeath *= aloneMultiplier;
- }
- if (volumeFish > _volume)
- {
- probabilityDeath *= fullMultiplier;
- }
- if (volumeFish >= maxFishCount)
- {
- probabilityDeath = (int)ProbabilityDeath.overFull;
- }
- if (UserUtil.GenerateRandomNumber(maxChance + 1) < probabilityDeath)
- {
- fish.Kill();
- }
- }
- }
- private void Remove()
- {
- int quantityPerLiter = 10;
- int maxFishCount = _volume * quantityPerLiter;
- int volumeFish = Fish.Sum(fish => fish.OccupiedVolume);
- if (volumeFish >= maxFishCount)
- {
- Console.WriteLine("Too many fish. All died.");
- }
- int numberDead = 0;
- for (int i = Fish.Count - 1; i >= 0; i--)
- {
- if (Fish[i].IsAlive == false)
- {
- Fish.RemoveAt(i);
- numberDead++;
- }
- }
- int leftPosition = 0;
- int topPosition = 2;
- Console.SetCursorPosition(leftPosition, topPosition);
- Console.WriteLine($"fish died: {numberDead}");
- }
- }
- public abstract class Fish
- {
- public Fish()
- {
- IsAlive = true;
- }
- 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 abstract Fish Clone();
- public void IncreaseAge()
- {
- Age++;
- }
- public void Kill()
- {
- IsAlive = false;
- }
- protected void InitiateAge()
- {
- int minAge = 1;
- int minLifeSpan = 10;
- int maxAge = LifeExpectancy - minLifeSpan;
- Age = UserUtil.GenerateRandomNumber(minAge, maxAge + 1);
- }
- }
- public class PoeciliaReticulata : Fish
- {
- public PoeciliaReticulata()
- {
- Name = "Guppy";
- LifeExpectancy = 30;
- OccupiedVolume = 2;
- Image = ">-->";
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new PoeciliaReticulata();
- }
- }
- public class Paracheirodon : Fish
- {
- public Paracheirodon()
- {
- Name = "Paracheirodon";
- LifeExpectancy = 48;
- OccupiedVolume = 2;
- Image = ">==>";
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new Paracheirodon();
- }
- }
- public class Xiphophorus : Fish
- {
- public Xiphophorus()
- {
- Name = "Xiphophorus";
- LifeExpectancy = 60;
- OccupiedVolume = 4;
- Image = "_>=>";
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new Xiphophorus();
- }
- }
- public class PoeciliaVelifera : Fish
- {
- public PoeciliaVelifera()
- {
- Name = "Yucatan molly";
- LifeExpectancy = 30;
- OccupiedVolume = 4;
- Image = ">WW>";
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new PoeciliaVelifera();
- }
- }
- public class Catfish : Fish
- {
- public Catfish()
- {
- Name = "Catfish";
- LifeExpectancy = 96;
- OccupiedVolume = 8;
- Image = ">=>_";
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new Catfish();
- }
- }
- public class Goldfish : Fish
- {
- public Goldfish()
- {
- Name = "Goldfish";
- LifeExpectancy = 120;
- OccupiedVolume = 12;
- Image = ">=@>";
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new Goldfish();
- }
- }
- 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);
- }
- }
- enum ProbabilityDeath
- {
- normal = 2,
- secondHalf = 5,
- longLife = 90,
- overFull = 100
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement