Advertisement
illiden

Aquariumv2

Dec 25th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.14 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].SetName(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.         private string[] _types = { "Золотая", "Обычная", "Гуппи" };
  105.         public Fish[] fishes = new Fish[0];
  106.         public int MaxValue { get; private set; }
  107.         public int PositionX { get; private set; }
  108.         public int PositionY { get; private set; }
  109.         Random random = new Random();
  110.  
  111.         public Aquarium(int maxValue = 10, int x = 50, int y = 0)
  112.         {
  113.             MaxValue = maxValue;
  114.             PositionX = x;
  115.             PositionY = y;
  116.         }
  117.  
  118.         public void ChangePosition(int x, int y)
  119.         {
  120.             PositionX = x;
  121.             PositionY = y;
  122.         }
  123.  
  124.         public void DrawAquarium()
  125.         {
  126.             int row = PositionY;
  127.             int column = PositionX;
  128.             for (int i = 0; i < _aquarium.GetLength(0); i++)
  129.             {
  130.                 for (int j = 0; j < _aquarium.GetLength(1); j++)
  131.                 {
  132.                     Console.SetCursorPosition(column++, row);
  133.                     if (_aquarium[i, j] == '@')
  134.                     {
  135.                         DrawAquariumFish(j, i);
  136.                         continue;
  137.                     }
  138.                     Console.Write(_aquarium[i, j]);
  139.                 }
  140.                 row++;
  141.                 column = PositionX;
  142.                 Console.WriteLine();
  143.             }
  144.         }
  145.  
  146.         public void DrawAquariumFish(int x, int y)
  147.         {
  148.             for (int i = 0; i < fishes.Length; i++)
  149.             {
  150.                 if (fishes[i].X == x && fishes[i].Y == y)
  151.                 {
  152.                     fishes[i].DrawFish();
  153.                 }
  154.             }
  155.         }
  156.        
  157.         public void CreateRandomFishes(int n = 1)
  158.         {
  159.             if (n + fishes.Length <= MaxValue)
  160.             {
  161.                 for (int i = 0; i < n; i++)
  162.                 {
  163.                     CreateFish(_names[random.Next(0, _names.Length)], random.Next(10, 200), _types[random.Next(0, _types.Length)]);
  164.                 }
  165.             }
  166.             else
  167.             {
  168.                 n = MaxValue - fishes.Length;
  169.                 Console.WriteLine("Возможно создать только " + n + " рыб! Попробуйте снова");
  170.                 Console.ReadKey();
  171.             }
  172.            
  173.         }
  174.  
  175.         public void CreateFish(string name, int hp, string type)
  176.         {
  177.             Fish[] newFishes = new Fish[fishes.Length + 1];
  178.  
  179.             for (int i = 0; i < fishes.Length; i++)
  180.             {
  181.                 newFishes[i] = fishes[i];
  182.             }
  183.             newFishes[newFishes.Length - 1] = new Fish(name, hp, type, random.Next(2, 44), random.Next(1, 11));
  184.             fishes = newFishes;
  185.  
  186.             foreach (Fish fish in fishes)
  187.             {
  188.                 _aquarium[fish.Y, fish.X] = '@';
  189.             }
  190.         }
  191.  
  192.         public void DeleteFish(int deleteFish)
  193.         {
  194.             Fish[] newFishes = new Fish[fishes.Length - 1];
  195.             int index = 0;
  196.             if (fishes.Length > deleteFish)
  197.             {
  198.                 if (deleteFish == fishes.Length - 1)
  199.                 {
  200.                     _aquarium[fishes[fishes.Length - 1].Y, fishes[fishes.Length - 1].X] = ' ';
  201.                 }
  202.                 for (int i = 0; i < newFishes.Length; i++)
  203.                 {
  204.                     if (deleteFish == i)
  205.                     {
  206.                         _aquarium[fishes[i].Y, fishes[i].X] = ' ';
  207.                         index++;
  208.                     }
  209.                     newFishes[i] = fishes[index];
  210.                     index++;
  211.                 }
  212.                 fishes = newFishes;
  213.             }
  214.             else
  215.             {
  216.                 Console.Write("Рыбы стаким номером не существет!");
  217.                 Console.ReadKey();
  218.             }
  219.         }
  220.  
  221.         public void DeleteDeadFishes()
  222.         {
  223.             for (int i = 0; i < fishes.Length; i++)
  224.             {
  225.                 if (!fishes[i].Alive)
  226.                 {
  227.                     DeleteFish(i--);
  228.                 }
  229.             }
  230.  
  231.             for (int i = 1; i < _aquarium.GetLength(1) - 1; i++)
  232.             {
  233.                 _aquarium[0, i] = ' ';
  234.             }
  235.         }
  236.  
  237.         public void ShowFishes(int x = 0, int y = 0)
  238.         {
  239.             int row = y;
  240.             int column = x;
  241.             for (int i = 0; i < fishes.Length; i++)
  242.             {
  243.                 Console.SetCursorPosition(column, row++);
  244.                 Console.Write((i + 1) + ". ");
  245.                 fishes[i].ShowStats();
  246.             }
  247.         }
  248.  
  249.         public void Live()
  250.         {
  251.             foreach (Fish fish in fishes)
  252.             {
  253.                 fish.IsAlive();
  254.                 fish.Oldest();
  255.                 fish.Move(random, _aquarium);
  256.             }
  257.         }
  258.     }
  259.  
  260.     class Fish : Behaviour
  261.     {
  262.         public int Health { get; private set; }
  263.         public string Name { get; private set;  }
  264.         public bool Alive { get; private set; }
  265.         public string Type { get; private set; }
  266.         private char _symbol;
  267.         private ConsoleColor _color;
  268.         private ConsoleColor _defaultColor = Console.ForegroundColor;
  269.  
  270.         public Fish(string name, int hp, string type, int x, int y) : base(x, y)
  271.         {
  272.             Name = name;
  273.             Health = hp;
  274.             Type = type;
  275.             X = x;
  276.             Y = y;
  277.             _symbol = '>';
  278.             Alive = Health > 0;
  279.             switch (type.ToLower())
  280.             {
  281.                 case "обычная":
  282.                     _color = ConsoleColor.White;
  283.                     break;
  284.                 case "золотая":
  285.                     _color = ConsoleColor.Yellow;
  286.                     break;
  287.                 case "гуппи":
  288.                     _color = ConsoleColor.Blue;
  289.                     break;
  290.             }
  291.         }
  292.  
  293.         public void SetName(string name)
  294.         {
  295.             Name = name;
  296.         }
  297.  
  298.         public void IsAlive()
  299.         {
  300.             Alive = Health > 0;
  301.         }
  302.  
  303.         public void Oldest()
  304.         {
  305.             if (Alive)
  306.             {
  307.                 Health--;
  308.             }
  309.         }
  310.  
  311.         public void DrawFish()
  312.         {
  313.             Console.ForegroundColor = _color;
  314.             Console.Write(_symbol);
  315.             Console.ForegroundColor = _defaultColor;
  316.         }
  317.  
  318.         public void ShowStats()
  319.         {
  320.             if (Alive)
  321.             {
  322.                 Console.Write($"{Name} - ");
  323.                 Console.ForegroundColor = _color;
  324.                 Console.Write(Type);
  325.                 Console.ForegroundColor = _defaultColor;
  326.                 Console.WriteLine($" : {Health}   ");
  327.             }
  328.             else
  329.             {
  330.                 Console.Write($"{Name} - ");
  331.                 Console.ForegroundColor = _color;
  332.                 Console.Write(Type);
  333.                 Console.ForegroundColor = _defaultColor;
  334.                 Console.WriteLine($": мертва   ");
  335.             }
  336.         }
  337.  
  338.         public void Move(Random rand, char[,] aqua)
  339.         {
  340.             Direction(rand, aqua, Alive);
  341.  
  342.             if (Alive)
  343.             {
  344.                 if (dx < 0)
  345.                 {
  346.                     _symbol = '<';
  347.                 }
  348.                 else
  349.                 {
  350.                     _symbol = '>';
  351.                 }
  352.             }
  353.             else
  354.             {
  355.                 _symbol = 'x';
  356.             }
  357.  
  358.             if (aqua[Y + dy, X + dx] != '#')
  359.             {
  360.                 X += dx;
  361.                 Y += dy;
  362.                 aqua[Y - dy, X - dx] = ' ';
  363.             }
  364.            
  365.             aqua[Y, X] = '@';
  366.         }
  367.     }
  368.  
  369.     class Behaviour
  370.     {
  371.         public int X { get; protected set; }
  372.         public int Y { get; protected set; }
  373.         protected int dx = 1;
  374.         protected int dy = 0;
  375.  
  376.         public Behaviour(int x, int y)
  377.         {
  378.             X = x;
  379.             Y = y;
  380.         }
  381.        
  382.         protected void Direction(Random rand, char[,] aqua, bool alive)
  383.         {
  384.             if (Y > 0 && !alive)
  385.             {
  386.                 dx = 0;
  387.                 dy = -1;
  388.                 return;
  389.             }
  390.             else if (Y == 0 && !alive)
  391.             {
  392.                 dx = 0;
  393.                 dy = 0;
  394.                 return;
  395.             }
  396.             if (Y + dy < 0 && alive)
  397.             {
  398.                 dy = 1;
  399.                 return;
  400.             }
  401.  
  402.             while(true)
  403.             {
  404.                 dx = rand.Next(-1, 2);
  405.                 dy = rand.Next(-1, 2);
  406.                 if (X + dx > 0 && X + dx < aqua.GetLength(1) && Y + dy > 0 && Y + dy < aqua.GetLength(0))
  407.                 {
  408.                     if (aqua[Y + dy, X + dx] == ' ')
  409.                     {
  410.                         break;
  411.                     }
  412.                     else if (X + dx == X && Y + dy == Y)
  413.                     {
  414.                         break;
  415.                     }
  416.                 }
  417.             }
  418.         }
  419.     }
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement