MeGaLiGhT14

4.6 Оружейный арсенал

Oct 19th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.31 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Lesson4_6
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string[,] guns = new string[0 , 4];
  10.  
  11.             string[] gunEquipped = new string[3];
  12.  
  13.             bool exit = false;
  14.             bool errorInput;
  15.             int idGun;
  16.  
  17.             do
  18.             {
  19.                 errorInput = false;
  20.  
  21.                 Console.Write("1) Создать оружие\n" +
  22.                               "2) Экипировать оружие\n" +
  23.                               "3) Удалить оружие\n" +
  24.                               "4) Вывести список оружия\n" +
  25.                               "0) Выход\n\n" +
  26.                               "Ввод: ");
  27.  
  28.                 string idCommand = Console.ReadLine();
  29.  
  30.                 switch (idCommand)
  31.                 {
  32.                     case "1":
  33.                         guns = CreateGun(guns);
  34.                         break;
  35.  
  36.                     case "2":
  37.                         gunEquipped = EquipGun(gunEquipped , guns);
  38.                         break;
  39.  
  40.                     case "3":
  41.                         guns = DeletionGun(guns);
  42.                         break;
  43.  
  44.                     case "4":
  45.                         Console.WriteLine("\n  №  |             Название             |   Урон   | Скорострел.");
  46.  
  47.                         for (int i = 0; i < guns.GetLength(0); i++)
  48.                         {
  49.                             Console.Write($" {guns[i , 0]}");
  50.                             for (int j = 0; j < 3 - Convert.ToString(guns[i , 0]).Length; j++)
  51.                             {
  52.                                 Console.Write(" ");
  53.                             }
  54.  
  55.                             Console.Write($" | {guns[i , 1]}");
  56.                             for (int j = 0; j < 32 - Convert.ToString(guns[i , 1]).Length; j++)
  57.                             {
  58.                                 Console.Write(" ");
  59.                             }
  60.  
  61.                             Console.Write($" | {guns[i , 2]}");
  62.                             for (int j = 0; j < 8 - Convert.ToString(guns[i , 2]).Length; j++)
  63.                             {
  64.                                 Console.Write(" ");
  65.                             }
  66.  
  67.                             Console.Write($" | {guns[i , 3]}");
  68.                             for (int j = 0; j < 8 - Convert.ToString(guns[i , 3]).Length; j++)
  69.                             {
  70.                                 Console.Write(" ");
  71.                             }
  72.                             Console.WriteLine();
  73.                         }
  74.                         Console.WriteLine();
  75.                         break;
  76.  
  77.                     case "0":
  78.                         exit = true;
  79.                         break;
  80.  
  81.                     default:
  82.                         Console.WriteLine("\nОшибка ввода! Попробуйте снова\n");
  83.  
  84.                         errorInput = true;
  85.                         break;
  86.                 }
  87.             }
  88.             while (errorInput || !exit);
  89.         }
  90.  
  91.         private static string[,] CreateGun(string[,] guns)
  92.         {
  93.             Console.WriteLine("\nСоздание оружия:");
  94.  
  95.             string name = "";
  96.             do
  97.             {
  98.                 Console.Write("Название(до 32 символов): ");
  99.                 name = Console.ReadLine();
  100.             }
  101.             while (name.Length > 32);
  102.  
  103.             Console.Write("Урон: ");
  104.             int damage = Convert.ToInt32(Console.ReadLine());
  105.  
  106.             Console.Write("Скорострельность: ");
  107.             int speedFire = Convert.ToInt32(Console.ReadLine());
  108.  
  109.             Console.WriteLine();
  110.  
  111.             string[,] gunsNew = new string[guns.GetLength(0) + 1 , guns.GetLength(1)];
  112.             for (int i = 0; i < guns.GetLength(0); i++)
  113.             {
  114.                 for (int j = 0; j < guns.GetLength(1); j++)
  115.                 {
  116.                     gunsNew[i , j] = guns[i , j];
  117.                 }
  118.             }
  119.             gunsNew[guns.GetLength(0) , 0] = Convert.ToString(guns.GetLength(0) + 1);
  120.             gunsNew[guns.GetLength(0) , 1] = name;
  121.             gunsNew[guns.GetLength(0) , 2] = Convert.ToString(damage);
  122.             gunsNew[guns.GetLength(0) , 3] = Convert.ToString(speedFire);
  123.  
  124.             return gunsNew;
  125.         }
  126.  
  127.         private static string[] EquipGun(string[] gunEquipped , string[,] guns)
  128.         {
  129.             bool exit = false;
  130.             bool errorInput;
  131.             string idCommand;
  132.             int idGun;
  133.  
  134.             do
  135.             {
  136.                 Console.WriteLine("\nЭкипированое оружие:");
  137.  
  138.                 if (gunEquipped[0] == null)
  139.                     Console.WriteLine("Пусто\n");
  140.                 else
  141.                     Console.WriteLine($"Название: {gunEquipped[0]}\n" +
  142.                                       $"Урон: {gunEquipped[1]}\n" +
  143.                                       $"Скорострельность: {gunEquipped[2]}\n");
  144.  
  145.                 do
  146.                 {
  147.                     errorInput = false;
  148.  
  149.                     if (gunEquipped[0] == null)
  150.                         Console.WriteLine("1) Экипировать оружие");
  151.  
  152.                     else
  153.                         Console.WriteLine("2) Убрать оружие");
  154.  
  155.                     Console.Write("0) Назад\n" +
  156.                                   "Ввод: ");
  157.  
  158.                     idCommand = Console.ReadLine();
  159.  
  160.                     switch (idCommand)
  161.                     {
  162.                         case "1":
  163.                             Console.WriteLine("\nВведите номер оружия , подлежащего экипировке");
  164.  
  165.                             do
  166.                             {
  167.                                 Console.Write("Номер: ");
  168.                                 idGun = Convert.ToInt32(Console.ReadLine());
  169.                             }
  170.                             while (idGun > guns.GetLength(0) || --idGun < 0);
  171.  
  172.                             for (int i = 0; i < gunEquipped.Length; i++)
  173.                             {
  174.                                 gunEquipped[i] = guns[idGun , i + 1];
  175.                             }
  176.                             break;
  177.  
  178.                         case "2":
  179.                             for (int i = 0; i < gunEquipped.Length; i++)
  180.                             {
  181.                                 gunEquipped[i] = null;
  182.                             }
  183.                             break;
  184.  
  185.                         case "0":
  186.                             exit = true;
  187.                             break;
  188.  
  189.                         default:
  190.                             Console.WriteLine("\nОшибка ввода! Попробуйте снова\n");
  191.  
  192.                             errorInput = true;
  193.                             break;
  194.  
  195.                     }
  196.                 }
  197.                 while (errorInput);
  198.             }
  199.             while (!exit);
  200.  
  201.             Console.WriteLine();
  202.             return gunEquipped;
  203.         }
  204.  
  205.         private static string[,] DeletionGun(string[,] guns)
  206.         {
  207.             int idGun;
  208.  
  209.             Console.WriteLine("\nВведите номер оружия , подлежащего аннигиляции");
  210.  
  211.             do
  212.             {
  213.                 Console.Write("Номер: ");
  214.                 idGun = Convert.ToInt32(Console.ReadLine());
  215.             }
  216.             while (idGun > guns.GetLength(0) || --idGun < 0);
  217.  
  218.             Console.WriteLine();
  219.  
  220.             string[,] gunsDelete = new string[guns.GetLength(0) - 1 , guns.GetLength(1)];
  221.             for (int i = 0; i < guns.GetLength(0); i++)
  222.             {
  223.                 for (int j = 0; j < guns.GetLength(1); j++)
  224.                 {
  225.                     if (i < idGun)
  226.                         gunsDelete[i , j] = guns[i , j];
  227.                     else if (i > idGun)
  228.                     {
  229.                         if (j == 0)
  230.                             gunsDelete[i - 1 , j] = guns[i - 1 , j];
  231.                         else
  232.                             gunsDelete[i - 1 , j] = guns[i , j];
  233.                     }
  234.                 }
  235.             }
  236.             return gunsDelete;
  237.         }
  238.     }
  239. }
Add Comment
Please, Sign In to add comment