Advertisement
MrSoTniK

Aquarium

Jan 19th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.84 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. using System.IO;
  7.  
  8. namespace Aquarium
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             bool isPlaying = true;
  15.             Aquarium aqua = new Aquarium("map");
  16.             Shoal shoal = new Shoal();
  17.             int iteration = 0;
  18.             bool isExit = false;
  19.  
  20.             Console.WindowHeight = 60;
  21.             Console.WindowWidth = 120;
  22.  
  23.             while (isPlaying == true)
  24.             {
  25.                 while (iteration != 0)
  26.                 {
  27.                     System.Threading.Thread.Sleep(1000);
  28.                     Console.SetCursorPosition(0, 0);
  29.                     aqua.Draw();
  30.                     aqua.Map = shoal.Move(aqua.Map);
  31.                     Console.SetCursorPosition(60,0);
  32.                     Console.WriteLine("Список рыб:");
  33.                     shoal.ShowList();
  34.                     shoal.Iteration();
  35.                     aqua.Map = shoal.Death(aqua.Map);
  36.                     iteration--;
  37.                    
  38.                 }
  39.                 while (isExit == false)
  40.                 {
  41.                     Console.Clear();
  42.                     Console.SetCursorPosition(0, 25);                  
  43.                     Console.WriteLine("Меню:\n1 - Добавить рыбу\n2 - Убрать рыбу\n3 - Положить рыбу в аквариум\n4 - Перейти к симуляции");
  44.                     string userInput = Console.ReadLine();
  45.                     switch (userInput)
  46.                     {
  47.                         case "1":
  48.                             aqua.Map = shoal.Add(aqua.Map);
  49.                             break;
  50.                         case "2":
  51.                             aqua.Map = shoal.Delete(aqua.Map);
  52.                             break;
  53.                         case "3":
  54.                             aqua.Map = shoal.TakeFromStorage(aqua.Map);
  55.                             break;
  56.                         case "4":
  57.                             isExit = true;
  58.                             break;
  59.                         default:
  60.                             Console.WriteLine("Неверный ввод");
  61.                             break;
  62.                     }                  
  63.                 }
  64.                 Console.Clear();
  65.                 Console.SetCursorPosition(0, 25);  
  66.                 Console.WriteLine("Каково количество итераций?");
  67.                 iteration = Convert.ToInt32(Console.ReadLine());
  68.                 isExit = false;
  69.                
  70.          
  71.             }
  72.         }
  73.     }
  74.  
  75.     class Aquarium
  76.     {
  77.         private string[] file;
  78.         public char[,] Map;
  79.  
  80.         public Aquarium(string mapName)
  81.         {          
  82.             file = File.ReadAllLines("maps/" + mapName + ".txt");
  83.             Map = new char[file.Length, file[0].Length];
  84.             for (int i = 0; i < Map.GetLength(0); i++)
  85.             {
  86.                 for (int j = 0; j < Map.GetLength(1); j++)
  87.                 {
  88.                     Map[i, j] = file[i][j];
  89.                 }
  90.             }
  91.         }
  92.  
  93.         public void Draw()
  94.         {
  95.             for (int i = 0; i < Map.GetLength(0); i++)
  96.             {
  97.                 for (int j = 0; j < Map.GetLength(1); j++)
  98.                 {
  99.                     Console.Write(Map[i, j]);
  100.                 }
  101.                 Console.WriteLine();
  102.             }          
  103.         }
  104.     }
  105.  
  106.     class Fish
  107.     {
  108.         public int HP{get; private set;}
  109.         public char Symbol { get; private set; }        
  110.         public Random Random = new Random();
  111.         public char InStorage{ get; private set; }  
  112.  
  113.         public Fish(char sign, int lifes = 100, char storage = '-')
  114.         {
  115.             Symbol = sign;
  116.             HP = lifes;
  117.             InStorage = storage;
  118.         }
  119.        
  120.         public void Iteration()
  121.         {
  122.             HP--;
  123.         }
  124.  
  125.         public char[,] Move(char[,] map)
  126.         {
  127.             Random rand = new Random();
  128.             int variant;
  129.             int x = 0;
  130.             int y = 0;
  131.             int dx = 0;
  132.             int dy = 0;
  133.            
  134.             for (int i = 0; i < map.GetLength(0); i++)
  135.             {
  136.                 for (int j = 0; j < map.GetLength(1); j++)
  137.                 {
  138.                     if (map[i, j] == Symbol)
  139.                     {                        
  140.                         x = i;                      
  141.                         y = j;
  142.                         map[x, y] = ' ';        
  143.                     }
  144.                 }
  145.             }
  146.  
  147.             variant = rand.Next(1,5);
  148.             switch (variant)
  149.             {
  150.                 case 1:
  151.                     dx = 0;
  152.                     dy = 1;
  153.                     break;
  154.                 case 2:
  155.                     dx = 1;
  156.                     dy = 0;
  157.                     break;
  158.                 case 3:
  159.                     dx = -1;
  160.                     dy = 0;
  161.                     break;
  162.                 case 4:
  163.                     dx = 0;
  164.                     dy = -1;
  165.                     break;
  166.             }
  167.             if (map[x + dx, y + dy] != '-' && map[x + dx, y + dy] != '#' && map[x + dx, y + dy] != Symbol)
  168.             {
  169.                 map[x + dx, y + dy] = Symbol;
  170.             }
  171.             else
  172.             {
  173.                 switch (dx)
  174.                 {
  175.                     case 0:
  176.                         break;
  177.                     case 1:
  178.                         dx = -1;
  179.                         break;
  180.                     case -1:
  181.                         dx = 1;
  182.                         break;
  183.                 }
  184.  
  185.                 switch (dy)
  186.                 {
  187.                     case 0:
  188.                         break;
  189.                     case 1:
  190.                         dy = -1;
  191.                         break;
  192.                     case -1:
  193.                         dy = 1;
  194.                         break;
  195.                 }
  196.                 map[x + dx, y + dy] = Symbol;
  197.             }
  198.            
  199.                 return map;
  200.         }
  201.  
  202.         public char[,] Add(char[,] map)
  203.         {
  204.             int xPos = Random.Next(3, 17);
  205.             int yPos = Random.Next(2, 51);
  206.  
  207.             map[xPos, yPos] = Symbol;
  208.  
  209.             return map;
  210.         }
  211.  
  212.         public char[,] Delete(char[,] map, bool isDead)
  213.         {
  214.             for (int i = 0; i < map.GetLength(0); i++)
  215.             {
  216.                 for (int j = 0; j < map.GetLength(1); j++)
  217.                 {
  218.                     if (map[i, j] == Symbol)
  219.                     {
  220.                         map[i, j] = ' ';
  221.                         goto finish;
  222.                     }
  223.                 }              
  224.             }
  225.            
  226.             finish:
  227.             if (isDead == false)
  228.             {
  229.                 InStorage = '+';
  230.             }
  231.             return map;
  232.         }
  233.  
  234.         public void ReleaseFromStorage()
  235.         {
  236.             InStorage = '-';
  237.         }
  238.  
  239.     }
  240.  
  241.     class Shoal
  242.     {
  243.         private Fish[] fishes = new Fish[0];
  244.      
  245.  
  246.         public void ShowList()
  247.         {          
  248.             for (int i = 0; i < fishes.Length; i++)
  249.             {
  250.                 Console.SetCursorPosition(60, i+1);
  251.                 Console.WriteLine(i + ") Рыба: '" + fishes[i].Symbol + "'  Жизни: " + fishes[i].HP + "  Состояние: " + fishes[i].InStorage);
  252.             }          
  253.         }
  254.  
  255.         public char[,] Add(char[,] map)
  256.         {
  257.             Console.WriteLine("Добавить рыб? Нажмите Y, если да. Нет - люб. другое значение");
  258.             string userChoice = Console.ReadLine();
  259.  
  260.             if (userChoice == "Y")
  261.             {
  262.                 Console.WriteLine("Сколько рыб добавить?");
  263.                 int amount = Convert.ToInt32(Console.ReadLine());
  264.  
  265.                 for (int i = 0; i < amount; i++)
  266.                 {
  267.                     Fish[] temp = new Fish[fishes.Length + 1];
  268.                     for (int j = 0; j < fishes.Length; j++)
  269.                     {
  270.                         temp[j] = fishes[j];
  271.                     }
  272.  
  273.                     string answer;
  274.  
  275.                     Console.WriteLine("HP по умолчанию? Нажмите Y, если да. Нет - люб. другое значение");
  276.                     answer = Console.ReadLine();
  277.                     if (answer == "Y")
  278.                     {
  279.                         Console.WriteLine("Введите тип рыбы (символ)");
  280.                         temp[fishes.Length] = new Fish(Convert.ToChar(Console.ReadLine()));
  281.                     }
  282.                     else
  283.                     {
  284.                         Console.WriteLine("Введите тип рыбы (символ), затем - количество жизней");
  285.                         temp[fishes.Length] = new Fish(Convert.ToChar(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()));
  286.                     }
  287.                     fishes = temp;
  288.                     map = fishes[fishes.Length-1].Add(map);
  289.                 }
  290.             }
  291.            
  292.             return map;
  293.  
  294.         }
  295.  
  296.         public char[,] Delete(char[,] map)
  297.         {
  298.             Console.WriteLine("Достать рыбу из аквариума? Нажмите Y, если да. Нет - люб. другое значение");
  299.             string userChoice = Console.ReadLine();
  300.  
  301.             if (userChoice == "Y")
  302.             {
  303.                 Console.WriteLine("Убить рыбу или оставить на хранение? Нажмите Y, если убить рыбу. Нет - оставить на хранение (люб. др. значение)");
  304.                 string answer = Console.ReadLine();
  305.                 if(answer == "Y")
  306.                 {
  307.                     Fish[] temp = new Fish[fishes.Length - 1];              
  308.                     bool isHappened = false;
  309.                     int value;
  310.  
  311.                     Console.WriteLine("Номер рыбы в списке: ");
  312.                     value = Convert.ToInt32(Console.ReadLine());
  313.                     for (int i = 0; i < fishes.Length; i++)
  314.                     {
  315.                         if (i != value && isHappened == false)
  316.                         {
  317.                             temp[i] = fishes[i];
  318.                         }
  319.                         if (i == value)
  320.                         {
  321.                             map = fishes[i].Delete(map, true);
  322.                             isHappened = true;
  323.                         }
  324.                         if (isHappened == true && i != temp.Length)
  325.                         {
  326.                             temp[i] = fishes[(i + 1)];
  327.                         }
  328.                     }
  329.                     fishes = temp;
  330.                 }
  331.                 else
  332.                 {
  333.                     int value;
  334.  
  335.                     Console.WriteLine("Номер рыбы в списке: ");
  336.                     value = Convert.ToInt32(Console.ReadLine());
  337.  
  338.                     for (int i = 0; i < fishes.Length; i++)
  339.                     {
  340.                         if (i == value)
  341.                         {
  342.                             map = fishes[i].Delete(map, false);
  343.                         }
  344.                     }
  345.                 }
  346.             }
  347.            
  348.  
  349.             return map;
  350.         }
  351.  
  352.         public char[,] TakeFromStorage(char[,] map)
  353.         {
  354.             int userChoice;
  355.  
  356.             Console.WriteLine("Номер рыбы из списка, которую необходимо положить в аквариум:");
  357.             userChoice = Convert.ToInt32(Console.ReadLine());
  358.  
  359.             for (int i = 0; i < fishes.Length; i++)
  360.             {
  361.                 if (userChoice == i)
  362.                 {
  363.                     if (fishes[i].InStorage == '+')
  364.                     {
  365.                         map = fishes[i].Add(map);
  366.                         fishes[i].ReleaseFromStorage();
  367.                     }
  368.                     else
  369.                     {
  370.                         Console.WriteLine("Данная рыба уже в аквариуме");
  371.                     }
  372.                 }
  373.             }
  374.             return map;
  375.         }
  376.  
  377.         public char[,] Death(char[,] map)
  378.         {          
  379.             for (int i = 0; i < fishes.Length; i++)
  380.             {
  381.                 if (fishes[i].HP == 0)
  382.                 {
  383.                     Fish[] temp = new Fish[fishes.Length - 1];    
  384.                     int value = i;
  385.                     bool isHappened = false;
  386.  
  387.                     for (int j = 0; j < fishes.Length; j++)
  388.                     {
  389.                         if (j != value && isHappened == false)
  390.                         {
  391.                             temp[j] = fishes[j];
  392.                         }
  393.                         if (j == value)
  394.                         {
  395.                             map = fishes[i].Delete(map, true);
  396.                             isHappened = true;
  397.                         }
  398.                         if (isHappened == true && j != temp.Length )
  399.                         {
  400.                             temp[j] = fishes[(j + 1)];
  401.                         }
  402.                     }
  403.                     fishes = temp;
  404.                     i = 0;
  405.                 }
  406.             }
  407.             return map;
  408.         }
  409.  
  410.         public char[,] Move(char[,] map)
  411.         {
  412.             for (int i = 0; i < fishes.Length; i++)
  413.             {
  414.                 map = fishes[i].Move(map);          
  415.             }
  416.                 return map;
  417.         }
  418.  
  419.         public void Iteration()
  420.         {
  421.             for (int i = 0; i < fishes.Length; i++)
  422.             {
  423.                 fishes[i].Iteration();
  424.             }
  425.         }
  426.  
  427.     }
  428.  
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement