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()
- {
- Zoo zoo = new Zoo();
- zoo.Work();
- }
- }
- public class Zoo
- {
- private List<Cage> _cages;
- private ICount _provider;
- public Zoo()
- {
- _cages = new List<Cage>();
- _provider = new AnimalProvider();
- CreateCages();
- }
- public void Work()
- {
- bool isWorking = true;
- Console.WriteLine("Welcome in Zoo");
- while (isWorking)
- {
- DisplayCages();
- isWorking = ProcessUserInput();
- }
- }
- private void CreateCages()
- {
- for (int i = 0; i < _provider.GetCount(); i++)
- {
- _cages.Add(new Cage(i));
- }
- }
- private void DisplayCages()
- {
- Console.WriteLine($"\nThere are {_cages.Count} cages");
- for (int i = 0; i < _cages.Count; i++)
- {
- string cageName = _cages[i].Name == null ? null : $"contains " + _cages[i].Name;
- Console.WriteLine($"Cage {i + 1} {cageName}");
- }
- }
- private bool ProcessUserInput()
- {
- const int CommandExit = 5;
- Console.WriteLine($"\nWhich cage do you want to visit? or {CommandExit} for exit..");
- int.TryParse(Console.ReadLine(), out int userInput);
- if (userInput == CommandExit)
- {
- return false;
- }
- if (TrySelectCage(userInput - 1))
- {
- int index = userInput - 1;
- _cages[index].ShowCage(index);
- _cages[index].InteractWithVisitor(index);
- _cages[index].MakeName(index);
- return true;
- }
- return true;
- }
- private bool TrySelectCage(int index)
- {
- if (index >= 0 && index < _cages.Count)
- {
- return true;
- }
- else
- {
- Console.WriteLine("No such cage");
- return false;
- }
- }
- }
- public class Cage
- {
- public event Action<int> IncreaseRequested;
- public event Action<int> RenovateRequested;
- private List<Animal> _animals;
- private AnimalProvider _animalProvider;
- public Cage(int index)
- {
- _animals = new List<Animal>();
- _animalProvider = new AnimalProvider();
- Fill(index);
- IncreaseRequested += Increase;
- RenovateRequested += Renovate;
- }
- public string Name { get; private set; }
- public void MakeName(int index)
- {
- Name = UserUtil.MakeColored(_animalProvider.GetCharacteristic(index).Name);
- }
- public void InteractWithVisitor(int index)
- {
- int increaseableCageIndex = 0;
- int renovateableCageIndex = 2;
- if (index == increaseableCageIndex)
- {
- int maxStep = 3;
- for (int i = 0; i < maxStep; i++)
- {
- Console.WriteLine($"You are coming back. You hear a voice of {UserUtil.MakeColored(_animalProvider.GetCharacteristic(index).Name)}. Press something..");
- Console.ReadKey(true);
- }
- Console.WriteLine($"You became {UserUtil.MakeColored(_animalProvider.GetCharacteristic(index).Name)}. You understand what {UserUtil.MakeColored(_animalProvider.GetCharacteristic(index).Name)} say. Press something..");
- IncreaseRequested?.Invoke(index);
- Console.ReadKey(true);
- Console.WriteLine("You were reborn.. Press something..\n");
- Console.ReadKey(true);
- }
- else if (index == renovateableCageIndex)
- {
- RenovateRequested?.Invoke(index);
- Console.WriteLine("You wanted to come back. But you were eaten. You were reborn.. Press something..\n");
- Console.ReadKey(true);
- }
- else
- {
- Console.WriteLine("You return to the cage selection. Press something..\n");
- Console.ReadKey(true);
- }
- }
- public void ShowCage(int index)
- {
- Console.WriteLine($"\nThere are {UserUtil.MakeColored(_animals.Count.ToString())} {UserUtil.MakeColored(_animalProvider.GetCharacteristic(index).Name)} in the cage, " +
- $"their gender is:\n{GetAnimalsGender()}" +
- $"\n\n{_animalProvider.MakeSound(index)}");
- }
- private string GetAnimalsGender()
- {
- return string.Join(", ", _animals.Select(animal => animal.Gender).Distinct().OrderBy(gender => gender));
- }
- private void Fill(int index)
- {
- for (int j = 0; j < UserUtil.GetRandomNumber(_animalProvider.GetCharacteristic(index).MaxCount, _animalProvider.GetCharacteristic(index).MinCount); j++)
- {
- Increase(index);
- }
- }
- private void Increase(int index)
- {
- string name = _animalProvider.GetCharacteristic(index).Name;
- string gender = _animalProvider.GetCharacteristic(index).Genders != null ?
- _animalProvider.GetCharacteristic(index).Genders[UserUtil.GetRandomNumber(_animalProvider.GetCharacteristic(index).Genders.Length)] :
- string.Format(_animalProvider.GetCharacteristic(index).GenderFormat, UserUtil.GetRandomNumber(_animalProvider.GetCharacteristic(index).GenderVariantCount + 1));
- _animals.Add(new Animal(name, gender));
- }
- private void Renovate(int index)
- {
- int numberRemoved = 3;
- int numberAdded = 3;
- for (int i = 0; i < UserUtil.GetRandomNumber(numberAdded + 1); i++)
- {
- if (_animals.Count < _animalProvider.GetCharacteristic(index).MaxCount)
- {
- Increase(index);
- }
- }
- for (int i = 0; i < UserUtil.GetRandomNumber(numberRemoved + 1); i++)
- {
- if (_animals.Count > _animalProvider.GetCharacteristic(index).MinCount)
- {
- _animals.RemoveAt(UserUtil.GetRandomNumber(_animals.Count));
- }
- }
- }
- }
- interface ICount
- {
- public int GetCount();
- }
- public class AnimalProvider : ICount
- {
- private List<AnimalCharacteristic> _animalsCharacteristic;
- public AnimalProvider()
- {
- _animalsCharacteristic = new List<AnimalCharacteristic>()
- {
- new AnimalCharacteristic("Flamibia", 70, 100, "Gender {0}", 784),
- new AnimalCharacteristic("Qoster", 8, 12, new string[] { "XX", "XT", "TT", "ZZ" }),
- new AnimalCharacteristic ("Kedenee", 1, 7, "Genderless"),
- new AnimalCharacteristic("Wooni", 10, 20, "Parent {0}", 2)
- };
- }
- public int GetCount() => _animalsCharacteristic.Count;
- public AnimalCharacteristic GetCharacteristic(int index)
- {
- return _animalsCharacteristic[index];
- }
- public string MakeSound(int index)
- {
- string[] wooniReplicks = new[] { "let's have a drink", "got a cigarette?", "hey, any spare change?" };
- return index switch
- {
- 0 => $"You hear nothing from the {UserUtil.MakeColored(GetCharacteristic(index).Name)} cage",
- 1 => "You hear: cheepcheepcheep",
- 2 => "Quiet music is playing",
- 3 => $"You hear: {UserUtil.MakeColored(wooniReplicks[UserUtil.GetRandomNumber(wooniReplicks.Length)])}",
- _ => ""
- };
- }
- }
- public class AnimalCharacteristic
- {
- public AnimalCharacteristic(string name, int minCount, int maxCount, string genderFormat, int genderVariantCount = 0)
- {
- Name = name;
- MinCount = minCount;
- MaxCount = maxCount;
- GenderFormat = genderFormat;
- GenderVariantCount = genderVariantCount;
- }
- public AnimalCharacteristic(string name, int minCount, int maxCount, string[] genders)
- {
- Name = name;
- MinCount = minCount;
- MaxCount = maxCount;
- Genders = genders;
- }
- public string Name { get; private set; }
- public int MinCount { get; private set; }
- public int MaxCount { get; private set; }
- public string GenderFormat { get; private set; }
- public int GenderVariantCount { get; private set; }
- public string[] Genders { get; private set; }
- }
- public class Animal
- {
- public Animal(string name, string gender)
- {
- Name = name;
- Gender = gender;
- }
- public string Name { get; private set; }
- public string Gender { get; private set; }
- }
- public class UserUtil
- {
- private static Random s_random = new Random();
- public static int GetRandomNumber(int max, int min = 0)
- {
- return s_random.Next(min, max);
- }
- public static string MakeColored(string line)
- {
- string colorSet = "\u001b[32m";
- string colorReset = "\u001b[0m";
- return colorSet + line + colorReset;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement