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;
- namespace Shop
- {
- class Program
- {
- static void Main(string[] args)
- {
- Trader trader = new Trader(20);
- Player player = new Player(1000);
- bool exit = false;
- while(!exit)
- {
- Console.Clear();
- Console.WriteLine("1. Посмотреть инвентарь\n\n2. Торговаться\n\n3. Выход");
- Console.Write("\nВведите номер команды: ");
- switch(Console.ReadLine())
- {
- case "1":
- player.ShowPurse();
- Console.WriteLine();
- player.ShowBag();
- Console.ReadKey();
- break;
- case "2":
- trader.Treading(player);
- break;
- case "3":
- break;
- default:
- Console.WriteLine("Такой команды не существует!");
- break;
- }
- }
- player.ShowPurse();
- trader.ShowBag();
- Console.ReadKey();
- }
- }
- class Player
- {
- public int Purse { get; private set; }
- Bag _bag;
- public Player(int money)
- {
- Purse = money;
- _bag = new Bag(0);
- }
- public void ShowPurse()
- {
- Console.WriteLine($"\nУ вас {Purse} монет");
- }
- public void ShowBag()
- {
- if (_bag.Items.Length == 0)
- {
- Console.WriteLine("\nВ инвентаре нет предметов");
- }
- else
- {
- _bag.ShowItems();
- }
- }
- public void Buy(Item item)
- {
- _bag.AddItem(item);
- Purse -= item.Price;
- }
- }
- class Trader
- {
- Bag _bag;
- public Trader(int quantyty)
- {
- _bag = new Bag(quantyty);
- }
- public void ShowBag()
- {
- if (_bag.Items.Length == 0)
- {
- Console.WriteLine("\nУ нас кончились предметы!");
- }
- else
- {
- _bag.ShowItems();
- }
- }
- public void Treading(Player player)
- {
- bool exit = false;
- Console.Clear();
- while(!exit)
- {
- Console.SetCursorPosition(0, 0);
- Console.WriteLine("Добро пожаловать в наш магазин!\n");
- Console.WriteLine("1. Посмотреть товары\n\n2. Купить предмет\n\n3. Выход\n");
- Console.Write("Введите номер команды: ");
- switch(Console.ReadLine())
- {
- case "1":
- Console.Clear();
- Console.SetCursorPosition(0, 18);
- ShowBag();
- Console.ReadKey();
- break;
- case "2":
- Console.Write("Введите номер предмета, который вы хотите купить: ");
- int item = Convert.ToInt32(Console.ReadLine()) - 1;
- SellItem(player, item);
- Console.ReadKey();
- Console.Clear();
- break;
- case "3":
- exit = true;
- break;
- default:
- Console.WriteLine("Такой команды не существует!");
- break;
- }
- }
- }
- public void SellItem(Player player, int number)
- {
- if (_bag.Items[number].Price <= player.Purse)
- {
- player.Buy(_bag.Items[number]);
- DleteItem(number);
- Console.WriteLine($"\nПродано! У вас осталось {player.Purse} монет");
- }
- else
- {
- Console.WriteLine("У вас недостаточно денег");
- }
- }
- void DleteItem(int number)
- {
- Item[] newItems = new Item[_bag.Items.Length - 1];
- int index = 0;
- for (int i = 0; i < newItems.Length; i++)
- {
- if (i == number)
- {
- index++;
- }
- newItems[i] = _bag.Items[index];
- index++;
- }
- _bag.Items = newItems;
- }
- }
- class Bag
- {
- private Item[] _allItems = {
- new Shield("Легкий щит", 30, 10),
- new Shield("Железный щит", 40, 30),
- new Shield("Божественный щит", 70, 60),
- new Sword("Легкий меч", 30, 30),
- new Sword("Железный меч", 40, 50),
- new Sword("Острое лезвие", 80, 100),
- new Sword("Божественное лезвие", 200, 150),
- new Potion("Маленькое зелье здоровья", 50, 10),
- new Potion("Обычное зелье здоровья", 100, 20),
- new Potion("Большое зелье здоровья", 150, 30),
- new Potion("Огромное зелье здоровья", 200, 40),
- new Item("Фонарик", 100, "светит, когда включить"),
- new Item("Карта", 100, "карта местной деревни"),
- new Item("Свиток магии", 500, "позволяет познаь тайны магии"),
- new Item("Какой-то камень", 200)
- };
- public Item[] Items;
- Random _rand = new Random();
- public Bag(int quantyty)
- {
- Items = new Item[0];
- AddRandomItems(quantyty);
- }
- public void AddRandomItems(int quantyty)
- {
- for(int i = 0; i < quantyty; i++)
- {
- AddItem();
- }
- }
- public void AddItem()
- {
- Item[] newItems = new Item[Items.Length + 1];
- for (int i = 0; i < Items.Length; i++)
- {
- newItems[i] = Items[i];
- }
- newItems[newItems.Length - 1] = _allItems[_rand.Next(0, _allItems.Length)];
- Items = newItems;
- }
- public void AddItem(Item item)
- {
- Item[] newItems = new Item[Items.Length + 1];
- for (int i = 0; i < Items.Length; i++)
- {
- newItems[i] = Items[i];
- }
- newItems[newItems.Length - 1] = item;
- Items = newItems;
- }
- public void ShowItems()
- {
- for (int i = 0; i < Items.Length; i++)
- {
- Console.Write((i + 1) + ". ");
- Items[i].ShowInfo();
- }
- }
- }
- class Item
- {
- protected string _name;
- protected string _description;
- public int Price { get; private set; }
- public Item(string name, int price, string description = "без описания")
- {
- _name = name;
- Price = price;
- _description = description;
- }
- public virtual void ShowInfo()
- {
- Console.WriteLine($"{_name} ({_description}) - {Price} монет");
- }
- }
- class Shield : Item
- {
- private int _armor;
- public Shield(string name, int price, int armor, string statistic = "броня") : base(name, price, description: statistic)
- {
- _armor = armor;
- }
- public override void ShowInfo()
- {
- Console.WriteLine($"{_name} ({_description}: {_armor}) - {Price} монет");
- }
- }
- class Sword : Item
- {
- private int _damage;
- public Sword(string name, int price, int damage, string statistic = "урон") : base(name, price, description: statistic)
- {
- _damage = damage;
- }
- public override void ShowInfo()
- {
- Console.WriteLine($"{_name} ({_description}: {_damage}) - {Price} монет");
- }
- }
- class Potion : Item
- {
- private int _restordHealth;
- public Potion(string name, int price, int health, string statistic = "восстанавливаемое здоровье") : base(name, price, description: statistic)
- {
- _restordHealth = health;
- }
- public override void ShowInfo()
- {
- Console.WriteLine($"{_name} ({_description}: {_restordHealth}) - {Price} монет");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement