Advertisement
MaoChessy

Task 31 - fix 2

Nov 10th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace C_sharp_Light
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Console.CursorVisible = false;
  11.             TradeRoom tradeRoom = new TradeRoom(new Player());
  12.             tradeRoom.Work();
  13.         }
  14.     }
  15.     public static class RandomStatic
  16.     {
  17.         static private Random _rand = new Random();
  18.  
  19.         static public int GetNext(int min, int max)
  20.         {
  21.             return _rand.Next(min, max);
  22.  
  23.         }
  24.         static public int GetNext(int max)
  25.         {
  26.             return _rand.Next(max);
  27.         }
  28.     }
  29.  
  30.     public static class Messager
  31.     {
  32.         static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
  33.         {
  34.             ConsoleColor defaultColor = Console.ForegroundColor;
  35.             Console.ForegroundColor = color;
  36.             Console.WriteLine(message);
  37.             Console.ForegroundColor = defaultColor;
  38.             if (delay)
  39.                 Console.ReadKey();
  40.         }
  41.     }
  42.  
  43.     class Item
  44.     {
  45.         public int Cost { get; private set; }
  46.         public string Name { get; private set; }
  47.         public Item()
  48.         {
  49.             Name = new string[] { "Железный меч", "Рогатый шлем", "Посох огня", "Кожаный сапоги", "Сладкий рулет" }[RandomStatic.GetNext(0, 5)];
  50.             Cost = RandomStatic.GetNext(20, 150);
  51.         }
  52.         public string GetInfo()
  53.         {
  54.             return $"{Name} стоит - {Cost}";
  55.         }
  56.     }
  57.     class Person
  58.     {
  59.         public string Name { get; private set; }
  60.         public int Сash { get { return Money; } }
  61.         protected int Money;
  62.         protected List<Item> Inventory = new List<Item>();
  63.         public Person(string name, int money)
  64.         {
  65.             Money = money;
  66.             Name = name;
  67.             int amountItems = RandomStatic.GetNext(3, 9);
  68.             for (int i = 0; i < amountItems; i++)
  69.             {
  70.                 Inventory.Add(new Item());
  71.             }
  72.         }
  73.         public string GetInfoAboutAllItem()
  74.         {
  75.             string result = "";
  76.             for (int i = 0; i < Inventory.Count; i++)
  77.             {
  78.                 result += $"{i}:{Inventory[i].GetInfo()}\n";
  79.             }
  80.             return result;
  81.         }
  82.     }
  83.     class Player : Person
  84.     {
  85.         public Player() : base("Player",1500){     }
  86.         public void Buy(Item item)
  87.         {
  88.             Inventory.Add(item);
  89.             Money -= item.Cost;
  90.             Messager.ShowMessageWithColor($"{item.Name} - Куплен", ConsoleColor.Green, true);
  91.         }
  92.         public bool СheckSolvency(Item iten)
  93.         {
  94.             if (Money >= iten.Cost) return true;
  95.             else return false;
  96.         }
  97.         public void ShowInfo()
  98.         {
  99.             Messager.ShowMessageWithColor(Name, ConsoleColor.Green, false);
  100.             Messager.ShowMessageWithColor(GetInfoAboutAllItem(), ConsoleColor.White, false);
  101.             Messager.ShowMessageWithColor($"\nВаши деньги: {Money}", ConsoleColor.Yellow, false);
  102.         }
  103.     }
  104.     class Trader : Person
  105.     {
  106.         public int SelectedIndexItem { get; private set; } = 0;
  107.         public Trader() : base("Торговец",RandomStatic.GetNext(300,900)){}
  108.         public int GetSizeInventory()
  109.         {
  110.             return Inventory.Count;
  111.         }  
  112.         public void ChangeActiveItem(int step)
  113.         {
  114.             SelectedIndexItem += step;
  115.             UpdateIndex();
  116.         }
  117.         public Item Sell()
  118.         {
  119.             Item temp = Inventory[SelectedIndexItem];
  120.             Inventory.RemoveAt(SelectedIndexItem);
  121.             Money += temp.Cost;
  122.             UpdateIndex();
  123.             return temp;
  124.         }
  125.         public Item PickItem()
  126.         {
  127.             return Inventory[SelectedIndexItem];
  128.         }
  129.         public void ShowInfo()
  130.         {
  131.             Messager.ShowMessageWithColor(Name, ConsoleColor.Cyan, false);
  132.             Messager.ShowMessageWithColor(GetInfoAboutAllItem(), ConsoleColor.White, false);
  133.             Messager.ShowMessageWithColor($"Выбранный индекс предмета {SelectedIndexItem}", ConsoleColor.Green,false);
  134.         }
  135.         private void UpdateIndex()
  136.         {
  137.             if (SelectedIndexItem < 0)
  138.                 SelectedIndexItem = 0;
  139.             else if (SelectedIndexItem > Inventory.Count - 1)
  140.                 SelectedIndexItem = Inventory.Count - 1;
  141.         }
  142.     }
  143.     class TradeRoom
  144.     {
  145.         private Player _player;
  146.         private Trader _trader;
  147.         public TradeRoom(Player player)
  148.         {
  149.             _player = player;
  150.             CreateNewTrader();
  151.         }
  152.         public void Work()
  153.         {
  154.             bool isOpen = true;
  155.             bool isTrading = false;
  156.             while (isOpen)
  157.             {
  158.                 if (isTrading)
  159.                 {
  160.                     Console.Clear();
  161.                     _trader.ShowInfo();
  162.                     Console.WriteLine($"\n\nСтрелка верх или вниз - выбор товара" +
  163.                         $"\n{ConsoleKey.Enter} - Покупка" +
  164.                         $"\n{ConsoleKey.Escape} - Выход из меню торговли");
  165.                     ConsoleKeyInfo key = Console.ReadKey();
  166.                     switch (key.Key)
  167.                     {
  168.                         case ConsoleKey.UpArrow:
  169.                             _trader.ChangeActiveItem(-1);
  170.                             break;
  171.                         case ConsoleKey.DownArrow:
  172.                             _trader.ChangeActiveItem(1);
  173.                             break;
  174.                         case ConsoleKey.Enter:
  175.                             Buy();
  176.                             break;
  177.                         case ConsoleKey.Escape:
  178.                             isTrading = false;
  179.                             break;
  180.                     }
  181.                 }
  182.                 else
  183.                 {
  184.                     Console.Clear();
  185.                     Console.WriteLine("Tab - создать нового торговца \nI - Посмотреть свой инвентарь \nEnter - Попросит торговца показать своей товар \nEsc закрыть");
  186.                     ConsoleKeyInfo key = Console.ReadKey();
  187.                     switch (key.Key)
  188.                     {
  189.                         case ConsoleKey.Tab:
  190.                             CreateNewTrader();
  191.                             break;
  192.                         case ConsoleKey.I:
  193.                             Console.Clear();
  194.                             _player.ShowInfo();
  195.                             Console.ReadKey();
  196.                             break;
  197.                         case ConsoleKey.Enter:
  198.                             isTrading = true;
  199.                             break;
  200.                         case ConsoleKey.Escape:
  201.                             isOpen = false;
  202.                             break;
  203.                     }
  204.                 }
  205.             }
  206.         }
  207.         private void Buy()
  208.         {
  209.             if (_trader.GetSizeInventory() > 0)
  210.                 if (_player.СheckSolvency(_trader.PickItem()))
  211.                     _player.Buy(_trader.Sell());
  212.                 else
  213.                     Messager.ShowMessageWithColor("У ваc больше нет денег.", ConsoleColor.Red, true);
  214.             else
  215.                 Messager.ShowMessageWithColor("У торговца больше нет вещей", ConsoleColor.Red, true);
  216.         }
  217.         private void CreateNewTrader()
  218.         {
  219.             _trader = new Trader();
  220.             Messager.ShowMessageWithColor("Торговец создан", ConsoleColor.White, true);
  221.         }
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement