Advertisement
SnowPhoenix347

5.4

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