Advertisement
SnowPhoenix347

5.4

Nov 9th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.72 KB | None | 0 0
  1. using System;
  2.  
  3. namespace FifthProject
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Player player = new Player();
  10.             Trader trader = new Trader();
  11.  
  12.             while (true)
  13.             {
  14.                 int id;
  15.                 ShowMenu();
  16.                 ShowBalance(player.PlayerBalance, trader.TraderBalance);
  17.                 int menu = ConvertToInt();
  18.                 switch (menu)
  19.                 {
  20.                     case 1:
  21.                         AddItem(ref player.PlayerItems);
  22.                         break;
  23.                     case 2:
  24.                         AddItem(ref trader.TraderItems);
  25.                         break;
  26.                     case 3:
  27.                         ShowItems(player.PlayerItems);
  28.                         id = ConvertToInt();
  29.                         TradeItem(ref player.PlayerItems, ref trader.TraderItems, id, ref player.PlayerBalance, ref trader.TraderBalance);
  30.                         break;
  31.                     case 4:
  32.                         ShowItems(trader.TraderItems);
  33.                         id = ConvertToInt();
  34.                         TradeItem(ref trader.TraderItems, ref player.PlayerItems, id, ref trader.TraderBalance, ref player.PlayerBalance);
  35.                         break;
  36.                     case 5:
  37.                         ShowItems(player.PlayerItems);
  38.                         Console.ReadKey();
  39.                         break;
  40.                     case 6:
  41.                         ShowItems(trader.TraderItems);
  42.                         Console.ReadKey();
  43.                         break;
  44.                 }
  45.                 Console.Clear();
  46.             }
  47.         }
  48.  
  49.         static void ShowMenu()
  50.         {
  51.             Console.WriteLine("\tMenu\n" +
  52.                               "1. Добавить новый предмет в инвентарь\n" +
  53.                               "2. Добавить новый предмет торговцу\n" +
  54.                               "3. Продать предмет\n" +
  55.                               "4. Купить предмет\n" +
  56.                               "5. Показать инвентарь\n" +
  57.                               "6. Показать инвентарь продавца\n");
  58.         }
  59.  
  60.         static void AddItem(ref Item[] items)
  61.         {
  62.             bool usability = false;
  63.             Console.WriteLine("Введите цену");
  64.             int price = ConvertToInt();
  65.             Console.WriteLine("Введите размер");
  66.             int scale = ConvertToInt();
  67.             Console.WriteLine("Введите имя");
  68.             string name = Console.ReadLine();
  69.             Console.WriteLine("Можно ли использовать этот предмет?\n" +
  70.                               "1. Да\n" +
  71.                               "2. Нет\n");
  72.  
  73.             int menu = ConvertToInt();
  74.             if (menu == 1)
  75.             {
  76.                 usability = true;
  77.             }
  78.             else if (menu == 2)
  79.             {
  80.                 usability = false;
  81.             }
  82.  
  83.             Item[] tempItems = new Item[items.Length + 1];
  84.  
  85.             for (int i = 0; i < tempItems.Length - 1; i++)
  86.             {
  87.                 tempItems[i] = items[i];
  88.             }
  89.  
  90.             tempItems[tempItems.Length - 1] = new Item(price, scale, name, usability);
  91.             items = tempItems;
  92.         }
  93.  
  94.         static void AddNewItem(int price, int scale, string name, bool usability, ref Item[] items)
  95.         {
  96.             Item[] tempItems = new Item[items.Length + 1];
  97.  
  98.             for (int i = 0; i < tempItems.Length - 1; i++)
  99.             {
  100.                 tempItems[i] = items[i];
  101.             }
  102.  
  103.             tempItems[tempItems.Length - 1] = new Item(price, scale, name, usability);
  104.             items = tempItems;
  105.         }
  106.  
  107.         static int ConvertToInt()
  108.         {
  109.             int output;
  110.             bool enterIsCorrect = false;
  111.             do
  112.             {
  113.                 string input = Console.ReadLine();
  114.                 enterIsCorrect = int.TryParse(input, out output);
  115.                 if (!enterIsCorrect)
  116.                 {
  117.                     Console.WriteLine("Error. Try again");
  118.                 }
  119.             } while (!enterIsCorrect);
  120.  
  121.             return output;
  122.         }
  123.         static void RemoveItem(int id, ref Item[] items)
  124.         {
  125.             items[id] = null;
  126.             Item[] tempItems = new Item[items.Length - 1];
  127.             for (int i = 0; i < items.Length - 1; i++)
  128.             {
  129.                 if (items[i] != null)
  130.                 {
  131.                     tempItems[i] = items[i];
  132.                 }
  133.                 else
  134.                 {
  135.                     tempItems[i] = items[i + 1];
  136.                 }
  137.             }
  138.  
  139.             items = tempItems;
  140.         }
  141.  
  142.         static void TradeItem(ref Item[] items, ref Item[] itemsSecond, int id, ref int balance, ref int balanceSecond)
  143.         {
  144.  
  145.             if (balanceSecond >= items[id].Price)
  146.             {
  147.  
  148.                 balanceSecond -= items[id].Price;
  149.                 balance += items[id].Price;
  150.  
  151.                 AddNewItem(0, 0, null, false, ref itemsSecond);
  152.                 itemsSecond[itemsSecond.Length - 1] = items[id];
  153.                 RemoveItem(id, ref items);
  154.             }
  155.             else
  156.             {
  157.                 Console.WriteLine("Недостаточно средств");
  158.                 Console.ReadKey();
  159.             }
  160.         }
  161.  
  162.         static void ShowItems(Item[] items)
  163.         {
  164.             for (int i = 0; i < items.Length; i++)
  165.             {
  166.                 Console.WriteLine($"ID: {i} Название: {items[i].Name} Цена: {items[i].Price} " +
  167.                                   $"Размер: {items[i].Scale} Юзабельность: {items[i].Usability}");
  168.             }
  169.         }
  170.         static void ShowBalance(int playerBalance, int traderBalance)
  171.         {
  172.             Console.WriteLine($"Баланс игрока: {playerBalance}\n" +
  173.                 $"Баланс торговца: {traderBalance}");
  174.         }
  175.     }
  176.  
  177.     class Trader
  178.     {
  179.         public Item[] TraderItems = new Item[0];
  180.         public int TraderBalance = 10000;
  181.     }
  182.  
  183.     class Player
  184.     {
  185.         public Item[] PlayerItems = new Item[0];
  186.         public int PlayerBalance = 1000;
  187.     }
  188.  
  189.     class Item
  190.     {
  191.         public int Price { get; }
  192.         public int Scale { get; }
  193.         public string Name { get; }
  194.         public bool Usability { get; }
  195.  
  196.         public Item(int price, int scale, string name, bool usability)
  197.         {
  198.             Price = price;
  199.             Scale = scale;
  200.             Name = name;
  201.             Usability = usability;
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement