Advertisement
illiden

ShopO

Dec 18th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.24 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.  
  7. namespace Shop
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Trader trader = new Trader(20);
  14.             Player player = new Player(1000);
  15.             bool exit = false;
  16.  
  17.             while(!exit)
  18.             {
  19.                 Console.Clear();
  20.                 Console.WriteLine("1. Посмотреть инвентарь\n\n2. Торговаться\n\n3. Выход");
  21.                 Console.Write("\nВведите номер команды: ");
  22.  
  23.                 switch(Console.ReadLine())
  24.                 {
  25.                     case "1":
  26.                         player.ShowPurse();
  27.                         Console.WriteLine();
  28.                         player.ShowBag();
  29.                         Console.ReadKey();
  30.                         break;
  31.                     case "2":
  32.                         trader.Treading(player);
  33.                         break;
  34.                     case "3":
  35.                         break;
  36.                     default:
  37.                         Console.WriteLine("Такой команды не существует!");
  38.                         break;
  39.                 }
  40.             }
  41.             player.ShowPurse();
  42.             trader.ShowBag();
  43.             Console.ReadKey();
  44.         }
  45.     }
  46.  
  47.     class Player
  48.     {
  49.         public int Purse { get; private set; }
  50.         Bag _bag;
  51.  
  52.         public Player(int money)
  53.         {
  54.             Purse = money;
  55.             _bag = new Bag(0);
  56.         }
  57.  
  58.         public void ShowPurse()
  59.         {
  60.             Console.WriteLine($"\nУ вас {Purse} монет");
  61.         }
  62.  
  63.         public void ShowBag()
  64.         {
  65.             if (_bag.Items.Length == 0)
  66.             {
  67.                 Console.WriteLine("\nВ инвентаре нет предметов");
  68.             }
  69.             else
  70.             {
  71.                 _bag.ShowItems();
  72.             }
  73.         }
  74.  
  75.         public void Buy(Item item)
  76.         {
  77.             _bag.AddItem(item);
  78.             Purse -= item.Price;
  79.         }
  80.     }
  81.  
  82.     class Trader
  83.     {
  84.         Bag _bag;
  85.         public Trader(int quantyty)
  86.         {
  87.             _bag = new Bag(quantyty);
  88.         }
  89.        
  90.         public void ShowBag()
  91.         {
  92.             if (_bag.Items.Length == 0)
  93.             {
  94.                 Console.WriteLine("\nУ нас кончились предметы!");
  95.             }
  96.             else
  97.             {
  98.                 _bag.ShowItems();
  99.             }
  100.         }
  101.  
  102.         public void Treading(Player player)
  103.         {
  104.             bool exit = false;
  105.             Console.Clear();
  106.            
  107.             while(!exit)
  108.             {
  109.                 Console.SetCursorPosition(0, 0);
  110.                 Console.WriteLine("Добро пожаловать в наш магазин!\n");
  111.                 Console.WriteLine("1. Посмотреть товары\n\n2. Купить предмет\n\n3. Выход\n");
  112.                 Console.Write("Введите номер команды: ");
  113.  
  114.                 switch(Console.ReadLine())
  115.                 {
  116.                     case "1":
  117.                         Console.Clear();
  118.                         Console.SetCursorPosition(0, 18);
  119.                         ShowBag();
  120.                         Console.ReadKey();
  121.                         break;
  122.                     case "2":
  123.                         Console.Write("Введите номер предмета, который вы хотите купить: ");
  124.                         int item = Convert.ToInt32(Console.ReadLine()) - 1;
  125.                         SellItem(player, item);
  126.                         Console.ReadKey();
  127.                         Console.Clear();
  128.                         break;
  129.                     case "3":
  130.                         exit = true;
  131.                         break;
  132.                     default:
  133.                         Console.WriteLine("Такой команды не существует!");
  134.                         break;
  135.                 }
  136.             }
  137.         }
  138.  
  139.         public void SellItem(Player player, int number)
  140.         {
  141.             if (_bag.Items[number].Price <= player.Purse)
  142.             {
  143.                 player.Buy(_bag.Items[number]);
  144.                 DleteItem(number);
  145.                 Console.WriteLine($"\nПродано! У вас осталось {player.Purse} монет");
  146.             }
  147.             else
  148.             {
  149.                 Console.WriteLine("У вас недостаточно денег");
  150.             }
  151.         }
  152.  
  153.         void DleteItem(int number)
  154.         {
  155.             Item[] newItems = new Item[_bag.Items.Length - 1];
  156.             int index = 0;
  157.             for (int i = 0; i < newItems.Length; i++)
  158.             {
  159.                 if (i == number)
  160.                 {
  161.                     index++;
  162.                 }
  163.                 newItems[i] = _bag.Items[index];
  164.                 index++;
  165.             }
  166.             _bag.Items = newItems;
  167.         }
  168.     }
  169.  
  170.     class Bag
  171.     {
  172.         private Item[] _allItems = {
  173.             new Shield("Легкий щит", 30, 10),
  174.             new Shield("Железный щит", 40, 30),
  175.             new Shield("Божественный щит", 70, 60),
  176.  
  177.             new Sword("Легкий меч", 30, 30),
  178.             new Sword("Железный меч", 40, 50),
  179.             new Sword("Острое лезвие", 80, 100),
  180.             new Sword("Божественное лезвие", 200, 150),
  181.  
  182.             new Potion("Маленькое зелье здоровья", 50, 10),
  183.             new Potion("Обычное зелье здоровья", 100, 20),
  184.             new Potion("Большое зелье здоровья", 150, 30),
  185.             new Potion("Огромное зелье здоровья", 200, 40),
  186.  
  187.             new Item("Фонарик", 100, "светит, когда включить"),
  188.             new Item("Карта", 100, "карта местной деревни"),
  189.             new Item("Свиток магии", 500, "позволяет познаь тайны магии"),
  190.             new Item("Какой-то камень", 200)
  191.         };
  192.         public Item[] Items;
  193.         Random _rand = new Random();
  194.  
  195.         public Bag(int quantyty)
  196.         {
  197.             Items = new Item[0];
  198.             AddRandomItems(quantyty);
  199.         }
  200.  
  201.         public void AddRandomItems(int quantyty)
  202.         {
  203.             for(int i = 0; i < quantyty; i++)
  204.             {
  205.                 AddItem();
  206.             }
  207.         }
  208.  
  209.         public void AddItem()
  210.         {
  211.             Item[] newItems = new Item[Items.Length + 1];
  212.             for (int i = 0; i < Items.Length; i++)
  213.             {
  214.                 newItems[i] = Items[i];
  215.             }
  216.             newItems[newItems.Length - 1] = _allItems[_rand.Next(0, _allItems.Length)];
  217.             Items = newItems;
  218.         }
  219.  
  220.         public void AddItem(Item item)
  221.         {
  222.             Item[] newItems = new Item[Items.Length + 1];
  223.             for (int i = 0; i < Items.Length; i++)
  224.             {
  225.                 newItems[i] = Items[i];
  226.             }
  227.             newItems[newItems.Length - 1] = item;
  228.             Items = newItems;
  229.         }
  230.  
  231.         public void ShowItems()
  232.         {
  233.             for (int i = 0; i < Items.Length; i++)
  234.             {
  235.                 Console.Write((i + 1) + ". ");
  236.                 Items[i].ShowInfo();
  237.             }
  238.         }
  239.     }
  240.  
  241.     class Item
  242.     {
  243.         protected string _name;
  244.         protected string _description;
  245.         public int Price { get; private set; }
  246.  
  247.         public Item(string name, int price, string description = "без описания")
  248.         {
  249.             _name = name;
  250.             Price = price;
  251.             _description = description;
  252.         }
  253.  
  254.         public virtual void ShowInfo()
  255.         {
  256.             Console.WriteLine($"{_name} ({_description}) - {Price} монет");
  257.         }
  258.     }
  259.  
  260.     class Shield : Item
  261.     {
  262.         private int _armor;
  263.         public Shield(string name, int price, int armor, string statistic = "броня") : base(name, price, description: statistic)
  264.         {
  265.             _armor = armor;
  266.         }
  267.  
  268.         public override void ShowInfo()
  269.         {
  270.             Console.WriteLine($"{_name} ({_description}: {_armor}) - {Price} монет");
  271.         }
  272.     }
  273.  
  274.     class Sword : Item
  275.     {
  276.         private int _damage;
  277.         public Sword(string name, int price, int damage, string statistic = "урон") : base(name, price, description: statistic)
  278.         {
  279.             _damage = damage;
  280.         }
  281.  
  282.         public override void ShowInfo()
  283.         {
  284.             Console.WriteLine($"{_name} ({_description}: {_damage}) - {Price} монет");
  285.         }
  286.     }
  287.  
  288.     class Potion : Item
  289.     {
  290.         private int _restordHealth;
  291.         public Potion(string name, int price, int health, string statistic = "восстанавливаемое здоровье") : base(name, price, description: statistic)
  292.         {
  293.             _restordHealth = health;
  294.         }
  295.  
  296.         public override void ShowInfo()
  297.         {
  298.             Console.WriteLine($"{_name} ({_description}: {_restordHealth}) - {Price} монет");
  299.         }
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement