Advertisement
TravaMan

Basic_Task23

Dec 31st, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace Basic_Task23
  6. {
  7.     class Program
  8.     {
  9.         public const int COUNT_FISH = 5;
  10.         static void Main(string[] args)
  11.         {
  12.             Interface.DrawAquarium();
  13.  
  14.             Interface.DrawMenu();
  15.  
  16.             List<Fish> listFish = new List<Fish>();
  17.  
  18.             while (true)
  19.             {
  20.                 if (Console.KeyAvailable)
  21.                 {
  22.                     ConsoleKeyInfo key = Console.ReadKey(true);
  23.  
  24.                     switch (key.Key)
  25.                     {
  26.                         case ConsoleKey.D1:
  27.                             if (listFish.Count < COUNT_FISH)
  28.                             {
  29.                                 listFish.Add(new DogFish());
  30.                             }
  31.                             break;
  32.                         case ConsoleKey.D2:
  33.                             if (listFish.Count < COUNT_FISH)
  34.                             {
  35.                                 listFish.Add(new DollarFish());
  36.                             }
  37.                             break;
  38.                         case ConsoleKey.D3:
  39.                             if (listFish.Count < COUNT_FISH)
  40.                             {
  41.                                 listFish.Add(new PercentFish());
  42.                             }
  43.                             break;
  44.                         case ConsoleKey.D4:
  45.                             if (listFish.Count > 0)
  46.                             {
  47.                                 listFish[0].Remove();
  48.                                 listFish.RemoveAt(0);
  49.                             }
  50.                             break;
  51.                         case ConsoleKey.Escape:
  52.                             return;
  53.                     }
  54.                 }
  55.  
  56.                 List<Fish> removedFish = new List<Fish>();
  57.  
  58.                 foreach (Fish fish in listFish)
  59.                 {
  60.                     if (!fish.Tick())
  61.                     {
  62.                         removedFish.Add(fish);
  63.                     }
  64.                 }
  65.  
  66.                 foreach (Fish fish in removedFish)
  67.                 {
  68.                     listFish.Remove(fish);
  69.                 }
  70.  
  71.                 Interface.DrawListFish(listFish, COUNT_FISH);
  72.  
  73.                 Thread.Sleep(200);
  74.             }
  75.         }
  76.     }
  77.  
  78.     class Interface
  79.     {
  80.         public const int WIDTH = 30;
  81.         public const int HEIGHT = 15;
  82.  
  83.         public static void DrawAquarium()
  84.         {
  85.             string line = "#";
  86.             for (int i = 0; i <= WIDTH; i++)
  87.             {
  88.                 Console.Write('#');
  89.             }
  90.  
  91.             Console.WriteLine();
  92.  
  93.             for (int i = 1; i < WIDTH; i++)
  94.             {
  95.                 line += ' ';
  96.             }
  97.  
  98.             line += '#';
  99.  
  100.             for (int i = 1; i < HEIGHT; i++)
  101.             {
  102.                 Console.WriteLine(line);
  103.             }
  104.  
  105.             for (int i = 0; i <= WIDTH; i++)
  106.             {
  107.                 Console.Write('#');
  108.             }
  109.             Console.WriteLine();
  110.         }
  111.  
  112.         public static void DrawMenu()
  113.         {
  114.             Console.WriteLine("1. Добавить рыбу - @");
  115.             Console.WriteLine("2. Добавить рыбу - $");
  116.             Console.WriteLine("3. Добавить рыбу - %");
  117.             Console.WriteLine("4. Убрать первую рыбку");
  118.             Console.WriteLine("ESC. Выйти");
  119.         }
  120.  
  121.         public static void DrawListFish(List<Fish> list, int count)
  122.         {
  123.             ClearListFish(count);
  124.             for (int i = 0; i < list.Count; i++)
  125.             {
  126.                 Console.SetCursorPosition(WIDTH + 5, i + 1);
  127.                 Console.WriteLine($"{i+1}. {list[i].Symbol} - {list[i].Age} лет");
  128.             }
  129.         }
  130.  
  131.         private static void ClearListFish(int count)
  132.         {
  133.             for (int i = 0; i < count; i++)
  134.             {
  135.                 Console.SetCursorPosition(WIDTH + 5, i + 1);
  136.                 Console.WriteLine("                             ");
  137.             }
  138.         }
  139.     }
  140.  
  141.     abstract class Fish
  142.     {
  143.         const int MAX_AGE = 30;
  144.  
  145.         public int Age { get; set; }
  146.         public char Symbol { get; set; }
  147.         protected int _x;
  148.         protected int _y;
  149.  
  150.         public Fish()
  151.         {
  152.             Random rnd = new Random();
  153.             _x = rnd.Next(1, 30);
  154.             _y = rnd.Next(1, 15);        
  155.         }
  156.  
  157.         public bool GrowOld()
  158.         {
  159.             return ++Age <= MAX_AGE;
  160.         }
  161.  
  162.         public abstract void Swim();
  163.  
  164.         public bool Tick()
  165.         {
  166.             Remove();
  167.            
  168.             if (GrowOld())
  169.             {
  170.  
  171.                 Swim();
  172.                 Console.SetCursorPosition(_x, _y);
  173.                 Console.Write(Symbol);
  174.                 return true;
  175.             } else
  176.             {
  177.                 return false;
  178.             }
  179.         }
  180.  
  181.         public void Remove()
  182.         {
  183.             Console.SetCursorPosition(_x, _y);
  184.             Console.Write(' ');
  185.         }
  186.     }
  187.  
  188.     class DogFish : Fish
  189.     {
  190.         private int _dx;
  191.         private int _dy;
  192.  
  193.         public DogFish() : base()
  194.         {
  195.             Random rnd = new Random();
  196.             _dx = rnd.Next(2);
  197.             if (_dx == 0)
  198.             {
  199.                 _dx = -1;
  200.             }
  201.             _dy = rnd.Next(2);
  202.             if (_dy == 0)
  203.             {
  204.                 _dy = -1;
  205.             }
  206.             Symbol = '@';
  207.         }
  208.         public override void Swim()
  209.         {
  210.             if (_x + _dx >= Interface.WIDTH || _x + _dx <= 0)
  211.             {
  212.                 _dx = -_dx;
  213.             }
  214.             if (_y + _dy >= Interface.HEIGHT || _y + _dy <= 0)
  215.             {
  216.                 _dy = -_dy;
  217.             }
  218.             _x += _dx;
  219.             _y += _dy;
  220.         }
  221.     }
  222.  
  223.     class DollarFish : Fish
  224.     {
  225.         public DollarFish() : base()
  226.         {            
  227.             Symbol = '$';
  228.         }
  229.  
  230.         public override void Swim()
  231.         {
  232.             Random rnd = new Random();
  233.  
  234.             int dx = rnd.Next(2);
  235.             if (dx == 0)
  236.             {
  237.                 dx = -1;
  238.             }
  239.             int dy = rnd.Next(2);
  240.             if (dy == 0)
  241.             {
  242.                 dy = -1;
  243.             }
  244.  
  245.             if (_x + dx >= Interface.WIDTH || _x + dx <= 0)
  246.             {
  247.                 dx = -dx;
  248.             }
  249.             if (_y + dy >= Interface.HEIGHT || _y + dy <= 0)
  250.             {
  251.                 dy = -dy;
  252.             }
  253.  
  254.             _x += dx;
  255.             _y += dy;
  256.         }
  257.     }
  258.  
  259.     class PercentFish : Fish
  260.     {
  261.         public PercentFish() : base()
  262.         {
  263.             Symbol = '%';
  264.         }
  265.  
  266.         public override void Swim()
  267.         {
  268.             Random rnd = new Random();
  269.  
  270.             if (rnd.Next(5) == 0)
  271.             {
  272.                 int dx = rnd.Next(0, Interface.WIDTH / 5);
  273.                 int dy = rnd.Next(0, Interface.HEIGHT / 5);
  274.  
  275.                 if (rnd.Next(2) == 0)
  276.                 {
  277.                     dx = -dx;
  278.                 }
  279.                 if (rnd.Next(2) == 0)
  280.                 {
  281.                     dy = -dy;
  282.                 }
  283.  
  284.                 if (_x + dx >= Interface.WIDTH || _x + dx <= 0)
  285.                 {
  286.                     dx = -dx;
  287.                 }
  288.                 if (_y + dy >= Interface.HEIGHT || _y + dy <= 0)
  289.                 {
  290.                     dy = -dy;
  291.                 }
  292.  
  293.                 _x += dx;
  294.                 _y += dy;
  295.             }
  296.         }
  297.     }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement