Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace C_sharp_Light
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- TradeRoom tradeRoom = new TradeRoom(new Player());
- tradeRoom.Work();
- }
- }
- public static class RandomStatic
- {
- static private Random _rand = new Random();
- static public int GetNext(int min, int max)
- {
- return _rand.Next(min, max);
- }
- static public int GetNext(int max)
- {
- return _rand.Next(max);
- }
- }
- public static class Messager
- {
- static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
- {
- ConsoleColor defaultColor = Console.ForegroundColor;
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ForegroundColor = defaultColor;
- if (delay)
- Console.ReadKey();
- }
- }
- class Item
- {
- public int Cost { get; private set; }
- public string Name { get; private set; }
- public Item()
- {
- Name = new string[] { "Железный меч", "Рогатый шлем", "Посох огня", "Кожаный сапоги", "Сладкий рулет" }[RandomStatic.GetNext(0, 5)];
- Cost = RandomStatic.GetNext(20, 150);
- }
- public string GetInfo()
- {
- return $"{Name} стоит - {Cost}";
- }
- }
- class Person
- {
- public string Name { get; private set; }
- public int Сash { get { return Money; } }
- protected int Money;
- protected List<Item> Inventory = new List<Item>();
- public Person(string name, int money)
- {
- Money = money;
- Name = name;
- int amountItems = RandomStatic.GetNext(3, 9);
- for (int i = 0; i < amountItems; i++)
- {
- Inventory.Add(new Item());
- }
- }
- public string GetInfoAboutAllItem()
- {
- string result = "";
- for (int i = 0; i < Inventory.Count; i++)
- {
- result += $"{i}:{Inventory[i].GetInfo()}\n";
- }
- return result;
- }
- }
- class Player : Person
- {
- public Player() : base("Player",1500){ }
- public void Buy(Item item)
- {
- Inventory.Add(item);
- Money -= item.Cost;
- Messager.ShowMessageWithColor($"{item.Name} - Куплен", ConsoleColor.Green, true);
- }
- public bool СheckSolvency(Item iten)
- {
- if (Money >= iten.Cost) return true;
- else return false;
- }
- public void ShowInfo()
- {
- Messager.ShowMessageWithColor(Name, ConsoleColor.Green, false);
- Messager.ShowMessageWithColor(GetInfoAboutAllItem(), ConsoleColor.White, false);
- Messager.ShowMessageWithColor($"\nВаши деньги: {Money}", ConsoleColor.Yellow, false);
- }
- }
- class Trader : Person
- {
- public int SelectedIndexItem { get; private set; } = 0;
- public Trader() : base("Торговец",RandomStatic.GetNext(300,900)){}
- public int GetSizeInventory()
- {
- return Inventory.Count;
- }
- public void ChangeActiveItem(int step)
- {
- SelectedIndexItem += step;
- UpdateIndex();
- }
- public Item Sell()
- {
- Item temp = Inventory[SelectedIndexItem];
- Inventory.RemoveAt(SelectedIndexItem);
- Money += temp.Cost;
- UpdateIndex();
- return temp;
- }
- public Item PickItem()
- {
- return Inventory[SelectedIndexItem];
- }
- public void ShowInfo()
- {
- Messager.ShowMessageWithColor(Name, ConsoleColor.Cyan, false);
- Messager.ShowMessageWithColor(GetInfoAboutAllItem(), ConsoleColor.White, false);
- Messager.ShowMessageWithColor($"Выбранный индекс предмета {SelectedIndexItem}", ConsoleColor.Green,false);
- }
- private void UpdateIndex()
- {
- if (SelectedIndexItem < 0)
- SelectedIndexItem = 0;
- else if (SelectedIndexItem > Inventory.Count - 1)
- SelectedIndexItem = Inventory.Count - 1;
- }
- }
- class TradeRoom
- {
- private Player _player;
- private Trader _trader;
- public TradeRoom(Player player)
- {
- _player = player;
- CreateNewTrader();
- }
- public void Work()
- {
- bool isOpen = true;
- bool isTrading = false;
- while (isOpen)
- {
- if (isTrading)
- {
- Console.Clear();
- _trader.ShowInfo();
- Console.WriteLine($"\n\nСтрелка верх или вниз - выбор товара" +
- $"\n{ConsoleKey.Enter} - Покупка" +
- $"\n{ConsoleKey.Escape} - Выход из меню торговли");
- ConsoleKeyInfo key = Console.ReadKey();
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- _trader.ChangeActiveItem(-1);
- break;
- case ConsoleKey.DownArrow:
- _trader.ChangeActiveItem(1);
- break;
- case ConsoleKey.Enter:
- Buy();
- break;
- case ConsoleKey.Escape:
- isTrading = false;
- break;
- }
- }
- else
- {
- Console.Clear();
- Console.WriteLine("Tab - создать нового торговца \nI - Посмотреть свой инвентарь \nEnter - Попросит торговца показать своей товар \nEsc закрыть");
- ConsoleKeyInfo key = Console.ReadKey();
- switch (key.Key)
- {
- case ConsoleKey.Tab:
- CreateNewTrader();
- break;
- case ConsoleKey.I:
- Console.Clear();
- _player.ShowInfo();
- Console.ReadKey();
- break;
- case ConsoleKey.Enter:
- isTrading = true;
- break;
- case ConsoleKey.Escape:
- isOpen = false;
- break;
- }
- }
- }
- }
- private void Buy()
- {
- if (_trader.GetSizeInventory() > 0)
- if (_player.СheckSolvency(_trader.PickItem()))
- _player.Buy(_trader.Sell());
- else
- Messager.ShowMessageWithColor("У ваc больше нет денег.", ConsoleColor.Red, true);
- else
- Messager.ShowMessageWithColor("У торговца больше нет вещей", ConsoleColor.Red, true);
- }
- private void CreateNewTrader()
- {
- _trader = new Trader();
- Messager.ShowMessageWithColor("Торговец создан", ConsoleColor.White, true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement