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> _fishesList;
- private List<Fish> _fishes;
- public Aquarium()
- {
- _volume = 50;
- _fishesList = new List<Fish>()
- {
- new PoeciliaReticulata(),
- new Paracheirodon(),
- new Xiphophorus(),
- new PoeciliaVelifera(),
- new Catfish(),
- new Goldfish()
- };
- _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");
- while (Console.ReadKey().Key == menuLaunchKey)
- {
- Console.Clear();
- LaunchMenu(out isRunning);
- }
- if (isRunning == false)
- {
- continue;
- }
- Console.Clear();
- Console.WriteLine("Turn :" + turn++);
- GrowOldFishes();
- KillFishes();
- RemoveFishes();
- ShowAllFishes();
- }
- }
- private void Fill()
- {
- int fishListCount = _fishesList.Count;
- int minOccupiedVolume = _fishesList.Min(fish => fish.OccupiedVolume);
- for (int volume = 0; volume < _volume;)
- {
- Fish fish = _fishesList[UserUtil.GenerateRandomNumber(fishListCount)];
- if (volume + fish.OccupiedVolume > _volume)
- {
- fishListCount = fishListCount > minOccupiedVolume ? fishListCount-- : minOccupiedVolume;
- continue;
- }
- _fishes.Add(fish.Clone());
- volume += fish.OccupiedVolume;
- }
- }
- private void LaunchMenu(out bool isRunning)
- {
- const string CommandAddFish = "1";
- const string CommandRemoveFish = "2";
- const string CommandShowFish = "3";
- const string CommandExitMenu = "4";
- const string CommandExit = "5";
- bool hasSelected = false;
- 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;
- }
- Console.WriteLine("\nPress something for next turn...");
- }
- }
- private void IncreaseFishes()
- {
- ShowListFishes();
- Console.Write("\nChoose type of fish to add: ");
- if (int.TryParse(Console.ReadLine(), out int typeNumber) && typeNumber >= 0 && typeNumber < _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(_fishesList[typeNumber].Clone());
- }
- }
- 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 < _fishesList.Count)
- {
- string fishName = _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 < _fishesList.Count; i++)
- {
- Console.Write($"{i} - {_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 = _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 fullMultiplier = 1;
- if (volumeFish > _volume)
- {
- fullMultiplier = 2;
- }
- return fullMultiplier;
- }
- private int VerifyAge(Fish fish)
- {
- int probabilityDeath = 2;
- int firstHalf = 2;
- int secondHalf = 5;
- int longLife = 90;
- if (fish.Age < fish.LifeExpectancy / 2)
- {
- 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 aloneMultiplier = 1;
- if (nameCount == 1)
- {
- aloneMultiplier = 2;
- }
- return aloneMultiplier;
- }
- 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 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 ConsoleColor Color { 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 = ">-->";
- Color = ConsoleColor.Cyan;
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new PoeciliaReticulata();
- }
- }
- public class Paracheirodon : Fish
- {
- public Paracheirodon()
- {
- Name = "Paracheirodon";
- LifeExpectancy = 48;
- OccupiedVolume = 2;
- Image = ">==>";
- Color = ConsoleColor.Blue;
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new Paracheirodon();
- }
- }
- public class Xiphophorus : Fish
- {
- public Xiphophorus()
- {
- Name = "Xiphophorus";
- LifeExpectancy = 60;
- OccupiedVolume = 4;
- Image = "_>=>";
- Color = ConsoleColor.Red;
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new Xiphophorus();
- }
- }
- public class PoeciliaVelifera : Fish
- {
- public PoeciliaVelifera()
- {
- Name = "Yucatan molly";
- LifeExpectancy = 30;
- OccupiedVolume = 4;
- Image = ">WW>";
- Color = ConsoleColor.DarkCyan;
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new PoeciliaVelifera();
- }
- }
- public class Catfish : Fish
- {
- public Catfish()
- {
- Name = "Catfish";
- LifeExpectancy = 96;
- OccupiedVolume = 8;
- Image = ">=>_";
- Color = ConsoleColor.Gray;
- InitiateAge();
- }
- public override Fish Clone()
- {
- return new Catfish();
- }
- }
- public class Goldfish : Fish
- {
- public Goldfish()
- {
- Name = "Goldfish";
- LifeExpectancy = 120;
- OccupiedVolume = 12;
- Image = ">=@>";
- Color = ConsoleColor.DarkYellow;
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement