Advertisement
SnowPhoenix347

5.5

Nov 12th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.43 KB | None | 0 0
  1. using System;
  2.  
  3. namespace FifthProject
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Aquarium aquarium = new Aquarium();
  10.             Fish[] fishes = new Fish[0];
  11.             UIRender render = new UIRender();
  12.             Renderer renderer = new Renderer();
  13.             FishPosition fishPosition = new FishPosition();
  14.  
  15.             while (true)
  16.             {
  17.                 renderer.Render(aquarium.Height, aquarium.Width, fishes);
  18.                 fishPosition.FishMovement(fishes);
  19.  
  20.                 Console.SetCursorPosition(0, aquarium.Height);
  21.                 render.ShowMenu();
  22.  
  23.                 render.RenderFishList(fishes);
  24.  
  25.                 Console.WriteLine($"{fishes.Length}/{aquarium.Size}");
  26.  
  27.                 int menu = Convert.ToInt32(Console.ReadLine());
  28.                 switch (menu)
  29.                 {
  30.                     case 1:
  31.                         if (aquarium.Size == fishes.Length)
  32.                         {
  33.                             Console.WriteLine("Аквариум полон");
  34.                         }
  35.                         else
  36.                         {
  37.                             aquarium.AddNewFish(ref fishes);
  38.                         }
  39.                         break;
  40.                     case 2:
  41.                         if (fishes.Length == 0)
  42.                         {
  43.                             Console.WriteLine("Аквариум пуст");
  44.                         }
  45.                         else
  46.                         {
  47.                             Console.WriteLine("Какую рыбку удалить?");
  48.                             int id = Convert.ToInt32(Console.ReadLine());
  49.                             aquarium.DeleteFish(ref fishes, id);
  50.                         }
  51.                         break;
  52.                 }
  53.                 aquarium.FishLive(ref fishes);
  54.                 Console.Clear();
  55.             }
  56.         }
  57.     }
  58.  
  59.     class UIRender
  60.     {
  61.         public void RenderFishList(Fish[] fishes)
  62.         {
  63.             for (int i = 0; i < fishes.Length; i++)
  64.             {
  65.                 Console.WriteLine($"Рыбка номер {i} - {fishes[i].Name}. {fishes[i].Age}/{fishes[i].MaxAge}. {fishes[i].Appearance}");
  66.             }
  67.         }
  68.         public void ShowMenu()
  69.         {
  70.             Console.WriteLine($"1. Добавить рыбку в аквариум\n" +
  71.                               $"2. Вытащить рыбку из аквариума\n" +
  72.                               $"3. Ждать");
  73.         }
  74.     }
  75.  
  76.     class Renderer
  77.     {
  78.         public void Render(int width, int height, Fish[] fishes)
  79.         {
  80.             for (int i = 0; i < width; i++)
  81.             {
  82.                 for (int j = 0; j < height; j++)
  83.                 {
  84.                     if (i == 0 || i == width - 1) Console.Write("#");
  85.                     else if (j == 0 || j == height - 1) Console.Write("#");
  86.                     else Console.Write(" ");
  87.                 }
  88.                 Console.WriteLine();
  89.             }
  90.             for (int i = 0; i < fishes.Length; i++)
  91.             {
  92.                 Console.SetCursorPosition(fishes[i].PositionX, fishes[i].PositionY);
  93.                 Console.WriteLine(fishes[i].Appearance);
  94.             }
  95.         }
  96.     }
  97.  
  98.     class FishPosition
  99.     {
  100.         Aquarium aquarium = new Aquarium();
  101.         public void FishMovement(Fish[] fishes)
  102.         {
  103.             Random random = new Random();
  104.             for (int i = fishes.Length - 1; i > -1; i--)
  105.             {
  106.                 Console.SetCursorPosition(fishes[i].PositionX, fishes[i].PositionY);
  107.                 int direction = random.Next(1, 5);
  108.  
  109.                 switch (direction)
  110.                 {
  111.                     case 1:
  112.                         if (Console.CursorTop - 1 != 1)
  113.                         {
  114.                             fishes[i].PositionY--;
  115.                         }
  116.  
  117.                         break;
  118.                     case 2:
  119.                         if (Console.CursorTop + 1 != aquarium.Height - 1)
  120.                         {
  121.                             fishes[i].PositionY++;
  122.                         }
  123.  
  124.                         break;
  125.                     case 3:
  126.                         if (Console.CursorLeft - 1 != 1)
  127.                         {
  128.                             fishes[i].PositionX--;
  129.                         }
  130.  
  131.                         break;
  132.                     case 4:
  133.                         if (Console.CursorLeft + 1 != aquarium.Width - 1)
  134.                         {
  135.                             fishes[i].PositionX++;
  136.                         }
  137.  
  138.                         break;
  139.                 }
  140.             }
  141.         }
  142.     }
  143.     class Fish
  144.     {
  145.         public int MaxAge { get; set; }
  146.         public int Age { get; set; }
  147.         public string Name { get; set; }
  148.         public string Appearance { get; set; }
  149.         public int PositionX { get; set; }
  150.         public int PositionY { get; set; }
  151.  
  152.         public Fish(int maxAge, int age, string name, string appearance, int positionX, int positionY)
  153.         {
  154.             MaxAge = maxAge;
  155.             Age = age;
  156.             Name = name;
  157.             Appearance = appearance;
  158.             PositionX = positionX;
  159.             PositionY = positionY;
  160.         }
  161.     }
  162.  
  163.     class Aquarium
  164.     {
  165.         public int Size { get; set; } = 5;
  166.         public int Height { get; set; } = 20;
  167.         public int Width { get; set; } = 15;
  168.  
  169.         public void AddNewFish(ref Fish[] fishes)
  170.         {
  171.             Random random = new Random();
  172.  
  173.             Console.WriteLine("Сколько должна прожить ваша рыбка?");
  174.             int maxAge = Convert.ToInt32(Console.ReadLine());
  175.  
  176.             Console.WriteLine($"Каков её возраст на данный момент? Максимум {maxAge - 1}");
  177.             int age = Convert.ToInt32(Console.ReadLine()) - 1;
  178.  
  179.             Console.WriteLine("Как зовут вашу рыбку?");
  180.             string name = Console.ReadLine();
  181.  
  182.             Console.WriteLine("Нарисуйте ваши рыбку");
  183.             string appearance = Console.ReadLine();
  184.  
  185.             Fish[] tempFishes = new Fish[fishes.Length + 1];
  186.  
  187.             for (int i = 0; i < tempFishes.Length - 1; i++)
  188.             {
  189.                 tempFishes[i] = fishes[i];
  190.             }
  191.             tempFishes[tempFishes.Length - 1] = new Fish(maxAge, age, name, appearance, random.Next(1, Width - 1), random.Next(1, Height - 1));
  192.  
  193.             fishes = tempFishes;
  194.         }
  195.  
  196.         public void DeleteFish(ref Fish[] fishes, int id)
  197.         {
  198.             fishes[id] = null;
  199.             Fish[] tempFishes = new Fish[fishes.Length - 1];
  200.             for (int i = 0; i < fishes.Length - 1; i++)
  201.             {
  202.                 if (fishes[i] != null)
  203.                 {
  204.                     tempFishes[i] = fishes[i];
  205.                 }
  206.                 else
  207.                 {
  208.                     tempFishes[i] = fishes[i + 1];
  209.                 }
  210.             }
  211.  
  212.             fishes = tempFishes;
  213.         }
  214.         public void FishLive(ref Fish[] fishes)
  215.         {
  216.             for (int i = 0; i < fishes.Length; i++)
  217.             {
  218.                 if (fishes[i].Age == fishes[i].MaxAge)
  219.                 {
  220.                     DeleteFish(ref fishes, i);
  221.                 }
  222.                 else
  223.                 {
  224.                     fishes[i].Age++;
  225.                 }
  226.             }
  227.         }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement