illiden

Aquarium

Dec 24th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Aquarium
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WindowWidth = 100;
  14.             bool exit = false;
  15.             Aquarium aqua = new Aquarium();
  16.             aqua.CreateRandomFishes(3);
  17.            
  18.             while (!exit)
  19.             {
  20.                 aqua.DrawAquarium();
  21.                 aqua.Live();
  22.                 Console.SetCursorPosition(0, 0);
  23.                 Console.WriteLine("space - пауза");
  24.                 aqua.ShowFishes(0, 3);
  25.  
  26.                 if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
  27.                 {
  28.                         bool menu = true;
  29.                         while (menu)
  30.                         {
  31.                             if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
  32.                             {
  33.                                 menu = false;
  34.                             }
  35.                             Console.Clear();
  36.                             aqua.DrawAquarium();
  37.                             aqua.ShowFishes(50, 15);
  38.                             Console.SetCursorPosition(0, 0);
  39.                             Console.WriteLine("1. Добавить рыб\n\n2. Удалить рыбу\n\n3. Изменить рыбе имя\n\n4. Очистить аквариум от мертвых рыб\n\n5. Выход из меню\n\n6. Выход\n\n");
  40.                             Console.Write("Введите номер команды: ");
  41.                             switch (Console.ReadLine())
  42.                             {
  43.                                 case "1":
  44.                                     Console.Write("\nСколько рыб вы хотите добавить: ");
  45.                                     aqua.CreateRandomFishes(Convert.ToInt32(Console.ReadLine()));
  46.                                     break;
  47.                                 case "2":
  48.                                     Console.Write("\nВведите номер рыбы: ");
  49.                                     aqua.DeleteFish(Convert.ToInt32(Console.ReadLine()) - 1);
  50.                                     break;
  51.                                 case "3":
  52.                                     Console.Write("\nВведите номер рыбы: ");
  53.                                     int n = Convert.ToInt32(Console.ReadLine()) - 1;
  54.                                     Console.Write($"Введите новое имя {aqua.fishes[n].Name}: ");
  55.                                     aqua.fishes[n].Name = Console.ReadLine();
  56.                                     break;
  57.                                 case "4":
  58.                                     aqua.DeleteDeadFishes();
  59.                                     Console.ReadKey();
  60.                                     break;
  61.                                 case "5":
  62.                                     menu = false;
  63.                                     Console.Clear();
  64.                                     aqua.DrawAquarium();
  65.                                     break;
  66.                                 case "6":
  67.                                     menu = false;
  68.                                     exit = true;
  69.                                     Console.ReadKey();
  70.                                     break;
  71.                                 default:
  72.                                     Console.WriteLine("Я не знаю такой команды");
  73.                                     break;
  74.                             }
  75.                        
  76.                     }
  77.                 }
  78.                 System.Threading.Thread.Sleep(1000);
  79.             }
  80.             Console.ReadKey();
  81.         }
  82.     }
  83.  
  84.     class Aquarium
  85.     {
  86.         char[,] _aquarium =
  87.             {
  88.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  89.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  90.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  91.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  92.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  93.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  94.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  95.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  96.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  97.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  98.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  99.                 { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  100.                 { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  101.             };
  102.  
  103.         private string[] _names = { "Леле", "Нана", "Таната", "Адора", "Тира" };
  104.         public Fish[] fishes = new Fish[0];
  105.         public int MaxValue { get; set; }
  106.         public int PositionX { get; set; }
  107.         public int PositionY { get; set; }
  108.         Random random = new Random();
  109.  
  110.         public Aquarium(int maxValue = 10, int x = 50, int y = 0)
  111.         {
  112.             MaxValue = maxValue;
  113.             PositionX = x;
  114.             PositionY = y;
  115.         }
  116.  
  117.         public void DrawAquarium()
  118.         {
  119.             int row = PositionY;
  120.             int column = PositionX;
  121.             for (int i = 0; i < _aquarium.GetLength(0); i++)
  122.             {
  123.                 for (int j = 0; j < _aquarium.GetLength(1); j++)
  124.                 {
  125.                     Console.SetCursorPosition(column++, row);
  126.                     Console.Write(_aquarium[i, j]);
  127.                 }
  128.                 row++;
  129.                 column = PositionX;
  130.                 Console.WriteLine();
  131.             }
  132.         }
  133.        
  134.         public void CreateRandomFishes(int n = 1)
  135.         {
  136.             if (n + fishes.Length <= MaxValue)
  137.             {
  138.                 for (int i = 0; i < n; i++)
  139.                 {
  140.                     CreateFish(_names[random.Next(0, _names.Length)], random.Next(10, 200));
  141.                 }
  142.             }
  143.             else
  144.             {
  145.                 n = MaxValue - fishes.Length;
  146.                 Console.WriteLine("Возможно создать только " + n + " рыб! Попробуйте снова");
  147.                 Console.ReadKey();
  148.             }
  149.            
  150.         }
  151.  
  152.         public void CreateFish(string name, int hp)
  153.         {
  154.             Fish[] newFishes = new Fish[fishes.Length + 1];
  155.  
  156.             for (int i = 0; i < fishes.Length; i++)
  157.             {
  158.                 newFishes[i] = fishes[i];
  159.             }
  160.             newFishes[newFishes.Length - 1] = new Fish(name, hp, random.Next(2, 44), random.Next(1, 11));
  161.             fishes = newFishes;
  162.  
  163.             foreach (Fish fish in fishes)
  164.             {
  165.                 _aquarium[fish.Y, fish.X] = fish.Symbol;
  166.             }
  167.         }
  168.  
  169.         public void DeleteFish(int deleteFish)
  170.         {
  171.             Fish[] newFishes = new Fish[fishes.Length - 1];
  172.             int index = 0;
  173.             if (fishes.Length > deleteFish)
  174.             {
  175.                 if (deleteFish == fishes.Length - 1)
  176.                 {
  177.                     _aquarium[fishes[fishes.Length - 1].Y, fishes[fishes.Length - 1].X] = ' ';
  178.                 }
  179.                 for (int i = 0; i < newFishes.Length; i++)
  180.                 {
  181.                     if (deleteFish == i)
  182.                     {
  183.                         _aquarium[fishes[i].Y, fishes[i].X] = ' ';
  184.                         index++;
  185.                     }
  186.                     newFishes[i] = fishes[index];
  187.                     index++;
  188.                 }
  189.                 fishes = newFishes;
  190.             }
  191.             else
  192.             {
  193.                 Console.Write("Рыбы стаким номером не существет!");
  194.                 Console.ReadKey();
  195.             }
  196.         }
  197.  
  198.         public void DeleteDeadFishes()
  199.         {
  200.             for (int i = 0; i < fishes.Length; i++)
  201.             {
  202.                 if (!fishes[i].Alive)
  203.                 {
  204.                     DeleteFish(i--);
  205.                 }
  206.             }
  207.  
  208.             for (int i = 1; i < _aquarium.GetLength(1) - 1; i++)
  209.             {
  210.                 _aquarium[0, i] = ' ';
  211.             }
  212.         }
  213.  
  214.         public void ShowFishes(int x = 0, int y = 0)
  215.         {
  216.             int row = y;
  217.             int column = x;
  218.             for (int i = 0; i < fishes.Length; i++)
  219.             {
  220.                 Console.SetCursorPosition(column, row++);
  221.                 Console.Write((i + 1) + ". ");
  222.                 fishes[i].ShowStats();
  223.             }
  224.         }
  225.  
  226.         public void Live()
  227.         {
  228.             foreach (Fish fish in fishes)
  229.             {
  230.                 fish.IsAlive();
  231.                 fish.Oldest();
  232.                 fish.Move(random, _aquarium);
  233.             }
  234.         }
  235.     }
  236.  
  237.     class Fish : Behaviour
  238.     {
  239.         public int Health { get; private set; }
  240.         public string Name { get; set; }
  241.         public char Symbol { get; set; }
  242.         public bool Alive { get; private set; }
  243.  
  244.         public Fish(string name, int hp, int x, int y) : base(x, y)
  245.         {
  246.             Name = name;
  247.             Health = hp;
  248.             X = x;
  249.             Y = y;
  250.             Symbol = '>';
  251.             Alive = Health > 0;
  252.         }
  253.  
  254.         public void IsAlive()
  255.         {
  256.             Alive = Health > 0;
  257.         }
  258.  
  259.         public void Oldest()
  260.         {
  261.             if (Alive)
  262.             {
  263.                 Health--;
  264.             }
  265.         }
  266.  
  267.         public void ShowStats()
  268.         {
  269.             if (Alive)
  270.             {
  271.                 Console.WriteLine($"{Name}: {Health}   ");
  272.             }
  273.             else
  274.             {
  275.                 Console.WriteLine($"{Name}: мертва   ");
  276.             }
  277.         }
  278.  
  279.         public void Move(Random rand, char[,] aqua)
  280.         {
  281.             Direction(rand, aqua, Alive);
  282.  
  283.             if (Alive)
  284.             {
  285.                 if (dx < 0)
  286.                 {
  287.                     Symbol = '<';
  288.                 }
  289.                 else
  290.                 {
  291.                     Symbol = '>';
  292.                 }
  293.             }
  294.             else
  295.             {
  296.                 Symbol = 'x';
  297.             }
  298.  
  299.             if (aqua[Y + dy, X + dx] != '#')
  300.             {
  301.                 X += dx;
  302.                 Y += dy;
  303.                 aqua[Y - dy, X - dx] = ' ';
  304.             }
  305.            
  306.             aqua[Y, X] = Symbol;
  307.         }
  308.     }
  309.  
  310.     class Behaviour
  311.     {
  312.         public int X { get; set; }
  313.         public int Y { get; set; }
  314.         protected int dx = 1;
  315.         protected int dy = 0;
  316.  
  317.         public Behaviour(int x, int y)
  318.         {
  319.             X = x;
  320.             Y = y;
  321.         }
  322.        
  323.         protected void Direction(Random rand, char[,] aqua, bool alive)
  324.         {
  325.             if (Y > 0 && !alive)
  326.             {
  327.                 dx = 0;
  328.                 dy = -1;
  329.                 return;
  330.             }
  331.             else if (Y == 0 && !alive)
  332.             {
  333.                 dx = 0;
  334.                 dy = 0;
  335.                 return;
  336.             }
  337.             if (Y + dy < 0 && alive)
  338.             {
  339.                 dy = 1;
  340.                 return;
  341.             }
  342.  
  343.             while(true)
  344.             {
  345.                 dx = rand.Next(-1, 2);
  346.                 dy = rand.Next(-1, 2);
  347.                 if (X + dx > 0 && X + dx < aqua.GetLength(1) && Y + dy > 0 && Y + dy < aqua.GetLength(0))
  348.                 {
  349.                     if (aqua[Y + dy, X + dx] == ' ')
  350.                     {
  351.                         break;
  352.                     }
  353.                     else if (X + dx == X && Y + dy == Y)
  354.                     {
  355.                         break;
  356.                     }
  357.                 }
  358.             }
  359.         }
  360.     }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment