Garloon

5.5

Sep 10th, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.16 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 CSLight
  8. {
  9.     class Menu
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int posTextX = 34;
  14.             int posTextY = 3;
  15.  
  16.             bool IsActive = true;
  17.  
  18.             Renderer rendAquarium = new Renderer();
  19.  
  20.             Fish[] fishes = new Fish[0];
  21.             Fishes allFishes = new Fishes(fishes);
  22.  
  23.             while (IsActive == true)
  24.             {
  25.                 Console.Clear();
  26.                 Console.CursorVisible = false;
  27.                 Console.SetCursorPosition(0, 0);
  28.                 rendAquarium.WriteAquarium();
  29.                
  30.                 Console.SetCursorPosition(posTextX, posTextY);
  31.                 Console.WriteLine("Список рыб в аквариуме:");
  32.                 allFishes.WriteAllFishes(posTextX, posTextY);
  33.                 posTextY = 3;
  34.  
  35.                 Console.SetCursorPosition(0, 13);
  36.                 Console.WriteLine("Нажми \"1\" чтобы добавить рыбку \nНажми \"2\" чтобы забрать рыбку \nНажми \"Esc\" чтобы выйти из приложения");
  37.                 ConsoleKeyInfo userKey = Console.ReadKey(true);
  38.                 switch (userKey.Key)
  39.                 {
  40.                     case ConsoleKey.D1:
  41.                         Console.Write("\nСимвол желаемой рыбки: ");
  42.                         char inputApp = Convert.ToChar(Console.ReadLine());
  43.                         Console.Write("Здоровье рыбки: ");
  44.                         int inputHealth = Convert.ToInt32(Console.ReadLine());
  45.                         allFishes.AddFish(inputApp, inputHealth);
  46.                         break;
  47.                     case ConsoleKey.D2:
  48.                         Console.Write("\nСимвол удаляемой рыбки: ");
  49.                         char inputApp2 = Convert.ToChar(Console.ReadLine());
  50.                         allFishes.DelFish(inputApp2);
  51.                         break;
  52.                     case ConsoleKey.Escape:
  53.                         IsActive = false;
  54.                         break;
  55.                     default:
  56.                         ConsoleMessage("Неизвестная команда");
  57.                         break;
  58.                 }
  59.             }
  60.         }
  61.         static void ConsoleMessage(string message)
  62.         {
  63.             Console.ForegroundColor = ConsoleColor.Red;
  64.             Console.WriteLine("\n" + message);
  65.             Console.ResetColor();
  66.             Console.ReadKey();
  67.         }
  68.     }
  69.     class Renderer
  70.     {
  71.         public char[,] AquariumBG =
  72.         {
  73.             {'=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','='},
  74.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  75.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  76.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  77.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  78.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  79.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  80.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  81.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  82.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  83.             {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
  84.             {'=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','=','='}
  85.         };
  86.         public void WriteAquarium()
  87.         {
  88.             for (int i = 0; i < AquariumBG.GetLength(0); i++)
  89.             {
  90.                 for (int j = 0; j < AquariumBG.GetLength(1); j++)
  91.                 {
  92.                     Console.Write(AquariumBG[i, j]);
  93.                 }
  94.                 Console.WriteLine();
  95.             }
  96.         }
  97.     }
  98.     class Fish
  99.     {
  100.         public int Health { get; set; }
  101.         public char Appearance { get; set; }
  102.  
  103.         public Fish(char appearance, int health)
  104.         {
  105.             Health = health;
  106.             Appearance = appearance;
  107.         }
  108.     }
  109.     class Fishes
  110.     {
  111.         public Fish[] AllFishes;
  112.         public Fishes(Fish[] allFishes)
  113.         {
  114.             AllFishes = allFishes;
  115.         }
  116.         public void WriteAllFishes(int posTextX, int posTextY)
  117.         {
  118.             Random random = new Random();
  119.             foreach (Fish fish in AllFishes)
  120.             {
  121.                 Console.SetCursorPosition(posTextX, ++posTextY);
  122.                 Console.WriteLine("Рыбка " + fish.Appearance + " - " + fish.Health + " здоровья;");
  123.                 fish.Health--;
  124.                 Console.SetCursorPosition(random.Next(1, 29), random.Next(1, 10));
  125.                 Console.Write(fish.Appearance);
  126.                 if(fish.Health <= 0)
  127.                 {
  128.                    DelFish(fish.Appearance);
  129.                 }
  130.             }
  131.         }
  132.         public void AddFish(char inputApp, int inputHealth)
  133.         {
  134.             if (AllFishes.Length >= 8)
  135.             {
  136.                 Console.ForegroundColor = ConsoleColor.Red;
  137.                 Console.WriteLine("\nАквариум полный");
  138.                 Console.ResetColor();
  139.                 Console.ReadKey();
  140.             }
  141.             else
  142.             {
  143.                 Fish newFish = new Fish(inputApp, inputHealth);
  144.                 Fish[] tempFishes = new Fish[AllFishes.Length + 1];
  145.                 for (int i = 0; i < AllFishes.Length; i++)
  146.                 {
  147.                     tempFishes[i] = AllFishes[i];
  148.                 }
  149.                 tempFishes[tempFishes.Length - 1] = newFish;
  150.                 AllFishes = tempFishes;
  151.             }
  152.         }
  153.         public void DelFish(char inputApp)
  154.         {
  155.             int index = -1;
  156.  
  157.             for (int i = 0; i < AllFishes.Length; ++i)
  158.             {
  159.                 if (inputApp == AllFishes[i].Appearance)
  160.                 {
  161.                     index = i;
  162.                     break;
  163.                 }
  164.             }
  165.             Fish[] tempFish = new Fish[AllFishes.Length - 1];
  166.             for (int i = 0, j = 0; i < tempFish.Length; ++i, ++j)
  167.             {
  168.                 if (j == index)
  169.                     ++j;
  170.  
  171.                 tempFish[i] = AllFishes[j];
  172.             }
  173.             AllFishes = tempFish;
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment