Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace C_sharp_Light
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- Console.Write("Введите имя игрока: ");
- Person player = new Person(Console.ReadLine());
- TradeRoom tradeRoom = new TradeRoom(player);
- tradeRoom.Work();
- }
- }
- 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);
- }
- }
- static class DisplayingMessages
- {
- static public void ShowMessageWithColor(string message, ConsoleColor color, int CursorleftPosition)
- {
- Console.CursorLeft = CursorleftPosition;
- ConsoleColor defaultColor = Console.ForegroundColor;
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ForegroundColor = defaultColor;
- }
- 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();
- }
- }
- }
- namespace C_sharp_Light
- {
- class Item
- {
- private string _name;
- private int _cost;
- public int Cost
- {
- get
- {
- return _cost;
- }
- }
- public string Name
- {
- get
- {
- return _name;
- }
- }
- public Item()
- {
- _name = new string[] { "Железный меч", "Рогатый шлем", "Посох огня", "Кожаный сапоги", "Сладкий рулет" }[RandomStatic.GetNext(0, 5)];
- _cost = RandomStatic.GetNext(20, 150);
- }
- private Item(string name, int baseCost)
- {
- _name = name;
- _cost = baseCost;
- }
- public Item Clone()
- {
- return new Item(Name, Cost);
- }
- public string Info()
- {
- return $"{Name} стоит - {Cost}";
- }
- }
- class Person
- {
- private int _money;
- public string Name { get; private set; }
- public int Money
- {
- get
- {
- return _money;
- }
- }
- private List<Item> _inventory = new List<Item>();
- public int SelectedIndexItem { get; private set; }
- public Person()
- {
- _money = RandomStatic.GetNext(300, 900);
- SelectedIndexItem = 0;
- Name = "Торговец";
- int amountItems = RandomStatic.GetNext(3, 9);
- for (int i = 0; i < amountItems; i++)
- {
- _inventory.Add(new Item());
- }
- }
- public Person(string name)
- {
- _money = RandomStatic.GetNext(300, 900);
- SelectedIndexItem = 0;
- Name = name;
- int amountItems = RandomStatic.GetNext(3, 9);
- for (int i = 0; i < amountItems; i++)
- {
- _inventory.Add(new Item());
- }
- }
- //
- public bool CanBuyIt(Item iten)
- {
- if (Money >= iten.Cost) return true;
- else return false;
- }
- public int GetSizeInventory()
- {
- return _inventory.Count;
- }
- public string GetInfoItemByIndex(int index)
- {
- return _inventory[index].Info();
- }
- public void ChangeActiveItem(int step)
- {
- SelectedIndexItem += step;
- UpdateIndex();
- }
- public Item GiveItem()
- {
- Item temp = _inventory[SelectedIndexItem];
- _inventory.RemoveAt(SelectedIndexItem);
- _money += temp.Cost;
- UpdateIndex();
- return temp.Clone();
- }
- public Item PickItem()
- {
- return _inventory[SelectedIndexItem];
- }
- public void GetItem(Item item)
- {
- _inventory.Add(item);
- _money -= item.Cost;
- UpdateIndex();
- }
- public void DrawnInfo(bool isSelected, int CursorPositionX)
- {
- if (isSelected)
- DisplayingMessages.ShowMessageWithColor($"Инветарь {Name} - выбран", ConsoleColor.Green, CursorPositionX);
- else
- DisplayingMessages.ShowMessageWithColor($"Инветарь {Name} - не выбран", ConsoleColor.Red, CursorPositionX);
- DisplayingMessages.ShowMessageWithColor($"Денег: {Money}", ConsoleColor.Yellow, CursorPositionX);
- for (int i = 0; i < GetSizeInventory(); i++)
- {
- if (i == SelectedIndexItem && isSelected)
- DisplayingMessages.ShowMessageWithColor($"{i}: {GetInfoItemByIndex(i)}", ConsoleColor.Green, CursorPositionX);
- else
- DisplayingMessages.ShowMessageWithColor($"{i}: {GetInfoItemByIndex(i)}", ConsoleColor.White, CursorPositionX);
- }
- }
- private void UpdateIndex()
- {
- if (SelectedIndexItem < 0)
- SelectedIndexItem = 0;
- else if (SelectedIndexItem > _inventory.Count - 1)
- SelectedIndexItem = _inventory.Count - 1;
- }
- }
- class TradeRoom
- {
- private Person _player;
- private Person _trader;
- bool playerIsSelected = true;
- public TradeRoom(Person player)
- {
- _player = player;
- CreateNewTrader();
- }
- public void Work()
- {
- bool isOpen = true;
- int xPosMenu = 0, yPosMeny = 22;
- while (isOpen)
- {
- Console.Clear();
- DrawnInfoAboutTraders();
- Console.SetCursorPosition(xPosMenu, yPosMeny);
- Console.WriteLine($"{ConsoleKey.RightArrow} или {ConsoleKey.LeftArrow} - для смены активного лица " +
- $"\n{ConsoleKey.UpArrow} и {ConsoleKey.DownArrow} - для выбора предмета" +
- $"\n{ConsoleKey.Enter} - Покупка/продажа" +
- $"\n{ConsoleKey.Tab} - Получить нового торговца" +
- $"\n{ConsoleKey.Escape} - Выход");
- ConsoleKeyInfo key = Console.ReadKey();
- switch (key.Key)
- {
- case ConsoleKey.RightArrow: case ConsoleKey.LeftArrow:
- ChangeSellingSide();
- break;
- case ConsoleKey.UpArrow:
- ChangeActiveItem(-1);
- break;
- case ConsoleKey.DownArrow:
- ChangeActiveItem(1);
- break;
- case ConsoleKey.Tab:
- CreateNewTrader();
- break;
- case ConsoleKey.Enter:
- BuyOrSell();
- break;
- case ConsoleKey.Escape:
- isOpen = false;
- break;
- }
- }
- }
- private void BuyOrSell()
- {
- if (ActiveTrader().GetSizeInventory() > 0)
- if (UnactiveTrader().CanBuyIt(ActiveTrader().PickItem()))
- UnactiveTrader().GetItem(ActiveTrader().GiveItem());
- else
- DisplayingMessages.ShowMessageWithColor("ОШИБКА: У неактивного участника недостаточно денег для покупки предмета у активного", ConsoleColor.Red, true);
- else
- DisplayingMessages.ShowMessageWithColor("ОШИБКА: У активного участника больше не осталось вещей.", ConsoleColor.Red, true);
- }
- private void DrawnInfoAboutTraders()
- {
- int xPosPlayer = 0;
- int xPosTrader = xPosPlayer + 30;
- _player.DrawnInfo(playerIsSelected, xPosPlayer);
- Console.CursorTop = 0;
- _trader.DrawnInfo(!playerIsSelected, xPosTrader);
- }
- private void ChangeSellingSide()
- {
- playerIsSelected = !playerIsSelected;
- }
- private void CreateNewTrader()
- {
- _trader = new Person();
- }
- private void ChangeActiveItem(int step)
- {
- ActiveTrader().ChangeActiveItem(step);
- }
- private Person ActiveTrader()
- {
- if (playerIsSelected)
- return _player;
- else
- return _trader;
- }
- private Person UnactiveTrader()
- {
- if (playerIsSelected)
- return _trader;
- else
- return _player;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement