Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //6 Оружейный арсенал. 5 баллов.
- //Сделайте 3 функции:
- //Удаление элемента из массива.
- //Добавление элемента в массив.
- //Экипировка орудия
- //Не используйте готовые функции!
- namespace Home6
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[,] dataTableWeapon = { { "Плазмомет «Disruptor»", "150" }, { "Tri-bolt «Тройник»", "136" }, { "Тяжелый пулемет «Heavy Machinegun»", "120" } };
- string[] menu = { "Добавить", "Удалить", "Экипировать", "Выход" };
- int index = 0;
- string currentMenu = "menu";
- string nameWeapon = dataTableWeapon[0, 0];
- string damageWeapon = dataTableWeapon[0, 1];
- ShowEqupment(nameWeapon, damageWeapon);
- while (true)
- {
- Console.SetCursorPosition(0, 3);
- Console.ResetColor();
- Console.WriteLine("\t\tМеню:");
- switch (currentMenu)
- {
- case "menu":
- index = ShomMenu(menu, index);
- ControlMenu(ref index, ref dataTableWeapon, ref currentMenu, menu);
- break;
- case "ShowDataTable":
- index = ShomMenu(dataTableWeapon, index);
- ControlMenu(ref index, ref dataTableWeapon, ref currentMenu, dataTableWeapon);
- break;
- case "deleteDataTable":
- index = ShomMenu(dataTableWeapon, index);
- ControlMenu(ref index, ref dataTableWeapon, ref currentMenu, dataTableWeapon);
- break;
- default:
- break;
- }
- }
- }
- static int ShomMenu(string[] menu, int index)
- {
- for (int i = 0; i < menu.Length; i++)
- {
- if (index == i)
- {
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Black;
- }
- Console.WriteLine(menu[i]);
- Console.ResetColor();
- }
- return index;
- }
- static void ControlMenu(ref int index, ref string[,] dataTableWeapon, ref string currentMenu, string[] menu)
- {
- ConsoleKeyInfo userInput = Console.ReadKey(true);
- switch (userInput.Key)
- {
- case ConsoleKey.UpArrow:
- if (index != 0) index--;
- break;
- case ConsoleKey.DownArrow:
- if (index != menu.Length - 1) index++;
- break;
- case ConsoleKey.Enter:
- dataTableWeapon = SelectElementofMenu(index, dataTableWeapon, ref currentMenu);
- index = 0;
- break;
- default:
- break;
- }
- }
- static int ShomMenu(string[,] menu, int index)
- {
- ClearArea();
- for (int i = 0; i < menu.GetLength(0); i++)
- {
- if (index == i)
- {
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Black;
- }
- Console.WriteLine(menu[i, 0] + "-" + menu[i, 1]);
- Console.ResetColor();
- }
- return index;
- }
- static void ControlMenu(ref int index, ref string[,] dataTableWeapon, ref string currentMenu, string[,] menu)
- {
- ConsoleKeyInfo userInput = Console.ReadKey(true);
- switch (userInput.Key)
- {
- case ConsoleKey.UpArrow:
- if (index != 0) index--;
- break;
- case ConsoleKey.DownArrow:
- if (index != menu.GetLength(0) - 1) index++;
- break;
- case ConsoleKey.Enter:
- switch (currentMenu)
- {
- case "ShowDataTable":
- ShowEqupment(menu[index, 0], menu[index, 1]);
- currentMenu = "menu";
- ShowMessage($"Вы успешно экипировали {menu[index, 0]} - {menu[index, 1]}", ConsoleColor.Yellow);
- break;
- case "deleteDataTable":
- dataTableWeapon = DeleteElementfromFile(dataTableWeapon, index);
- currentMenu = "menu";
- break;
- default:
- break;
- }
- index = 0;
- break;
- default:
- break;
- }
- }
- static void ShowEqupment(string nameWeapon, string damageWeapon)
- {
- Console.SetCursorPosition(0, 0);
- Console.WriteLine("\t\tЭкипировано:\n" +
- $"Оружие: {nameWeapon}\t\t\t\t\t \n" +
- $"Урон: {damageWeapon}\t ");
- }
- static string[,] SelectElementofMenu(int index, string[,] dataTableWeapon, ref string currentMenu)
- {
- switch (index)
- {
- case 0:
- dataTableWeapon = AddElemetToFile(dataTableWeapon);
- break;
- case 1:
- currentMenu = ShowElementfromFile(dataTableWeapon, "deleteDataTable");
- break;
- case 2:
- currentMenu = ShowElementfromFile(dataTableWeapon, "ShowDataTable");
- break;
- case 3:
- Console.Clear();
- Environment.Exit(0);
- break;
- default:
- break;
- }
- return dataTableWeapon;
- }
- static string[,] AddElemetToFile(string[,] array)
- {
- ClearArea();
- Console.Write("Добавление оружия.\nВведите название: ");
- string name = Console.ReadLine();
- Console.Write("Введите урон: ");
- string post = Console.ReadLine();
- string[,] tempArray = new string[array.GetLength(0) + 1, array.GetLength(1)];
- for (int i = 0; i < array.GetLength(0); i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- tempArray[i, j] = array[i, j];
- }
- }
- array = tempArray;
- array[array.GetLength(0) - 1, 0] = name;
- array[array.GetLength(0) - 1, 1] = post;
- ShowMessage($"Вы добавили {name} - {post} успешно", ConsoleColor.Yellow);
- return array;
- }
- static string[,] DeleteElementfromFile(string[,] array, int index)
- {
- string[,] tempArray = new string[array.GetLength(0) - 1, array.GetLength(1)];
- for (int i = 0; i < index; i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- tempArray[i, j] = array[i, j];
- }
- }
- for (int i = index; i < tempArray.GetLength(0); i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- tempArray[i, j] = array[i + 1, j];
- }
- }
- ShowMessage($"Вы успешно удалили {array[array.GetLength(0) - 1, 0]} - {array[array.GetLength(0) - 1, 1]}", ConsoleColor.Yellow);
- array = tempArray;
- return array;
- }
- static string ShowElementfromFile(string[,] dataTableWeapon, string currentMenu)
- {
- ClearArea();
- Console.WriteLine("Список оружия");
- for (int i = 0; i < dataTableWeapon.GetLength(0); i++)
- {
- Console.WriteLine((i + 1) + "." + dataTableWeapon[i, 0] + " - " + dataTableWeapon[i, 1]);
- }
- return currentMenu;
- }
- static void ShowMessage(string message, ConsoleColor color = ConsoleColor.Red)
- {
- ClearArea();
- Console.SetCursorPosition(0, 8);
- Console.ForegroundColor = color;
- Console.WriteLine(message + "\t\t\t\t\t");
- Console.ResetColor();
- }
- static void ClearArea(int x = 0, int y = 9)
- {
- Console.SetCursorPosition(x, y);
- for (int i = 0; i < 10; i++)
- {
- Console.ResetColor();
- Console.WriteLine("\t\t\t\t\t\t\t\t");
- }
- Console.SetCursorPosition(x, y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement