Advertisement
Dark32167

Untitled

Feb 18th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace IMJunior
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             WriteGreetings();
  11.  
  12.             var hero = new Hero();
  13.  
  14.             while (hero.FreePoints > 0)
  15.             {
  16.                 Console.Clear();
  17.                 Console.WriteLine("Поинтов - {0}", hero.FreePoints);
  18.                 Console.WriteLine(hero);
  19.  
  20.                 var action = ReadUserAction();
  21.  
  22.                 ApplyUserAction(hero, action);
  23.             }
  24.  
  25.             Console.WriteLine("Вы распределили все очки. Введите возраст персонажа:");
  26.             hero.Age = ReadInt();
  27.  
  28.  
  29.             Console.Clear();
  30.             Console.WriteLine(hero);
  31.  
  32.             Console.WriteLine("Нажмите любую клавишу чтобы продолжить...");
  33.             Console.ReadKey();
  34.         }
  35.  
  36.  
  37.         private static void ApplyUserAction(Hero hero, UserAction action)
  38.         {
  39.             Stat stat;
  40.             if (hero.Stats.TryGetValue(action.Subject.ToLower(), out stat))
  41.             {
  42.                 var oldValue = stat.Value;
  43.                 ChangeStat(stat, action.Operation, action.OperandPoints);
  44.                 UpdateFreePoints(hero, oldValue, stat.Value);
  45.             }
  46.         }
  47.  
  48.         private static void ChangeStat(Stat stat, string operation, int operandPoints)
  49.         {
  50.             if (operation == "+")
  51.             {
  52.                 int overhead = operandPoints - (10 - stat.Value);
  53.                 overhead = overhead < 0 ? 0 : overhead;
  54.                 operandPoints -= overhead;
  55.                 stat.Value += operandPoints;
  56.             }
  57.             else
  58.             {
  59.                 int overhead = stat.Value - operandPoints;
  60.                 overhead = overhead < 0 ? overhead : 0;
  61.                 operandPoints += overhead;
  62.                 stat.Value -= operandPoints;
  63.             }
  64.         }
  65.  
  66.         private static void UpdateFreePoints(Hero hero, int oldStatValue, int newStatValue)
  67.         {
  68.             if (oldStatValue > newStatValue)
  69.             {
  70.                 hero.FreePoints += oldStatValue - newStatValue;
  71.             }
  72.             else
  73.             {
  74.                 hero.FreePoints -= newStatValue - oldStatValue;
  75.             }
  76.         }
  77.  
  78.         private static UserAction ReadUserAction()
  79.         {
  80.             var action = new UserAction();
  81.  
  82.             Console.WriteLine("Какую характеристику вы хотите изменить?");
  83.             action.Subject = Console.ReadLine();
  84.             Console.WriteLine(@"Что вы хотите сделать? +\-");
  85.             action.Operation = Console.ReadLine();
  86.             Console.WriteLine(@"Колличество поинтов которые следует {0}", action.Operation == "+" ? "прибавить" : "отнять");
  87.             action.OperandPoints = ReadInt();
  88.  
  89.             return action;
  90.         }
  91.  
  92.         private static int ReadInt()
  93.         {
  94.             int intData = 0;
  95.             string data;
  96.             do
  97.             {
  98.                 data = Console.ReadLine();
  99.             } while (!int.TryParse(data, out intData));
  100.  
  101.             return intData;
  102.         }
  103.  
  104.         private static void WriteGreetings()
  105.         {
  106.             Console.WriteLine("Добро пожаловать в меню выбора создания персонажа!");
  107.             Console.WriteLine("У вас есть 25 очков, которые вы можете распределить по умениям");
  108.             Console.WriteLine("Нажмите любую клавишу чтобы продолжить...");
  109.             Console.ReadKey();
  110.         }
  111.     }
  112. //----------------------------------------------------------------------------------------------------------------------------------------
  113.  
  114.     public class Hero
  115.     {
  116.         public Dictionary<string, Stat> Stats { get; set; } =
  117.             new Dictionary<string, Stat>
  118.             {
  119.                 { "сила", new Stat() },
  120.                 { "ловкость", new Stat() },
  121.                 { "интелект", new Stat() }
  122.             };
  123.  
  124.         public int FreePoints { get; set; } = 25;
  125.  
  126.         public int Age { get; set; }
  127.  
  128.         public override string ToString()
  129.         {
  130.             return $"Возраст - {Age}\nСила - [{Stats["сила"]}]\nЛовкость - [{Stats["ловкость"]}]\nИнтелект - [{Stats["интелект"]}]";
  131.         }
  132.     }
  133. //----------------------------------------------------------------------------------------------------------------------------------------
  134.  
  135.     public class Stat
  136.     {
  137.         public int Value { get; set; }
  138.  
  139.         public override string ToString()
  140.         {
  141.             return Value.ToString().PadLeft(Value, '#').PadRight(10, '_');
  142.         }
  143.     }
  144. //----------------------------------------------------------------------------------------------------------------------------------------
  145.     public class UserAction
  146.     {
  147.         public string Subject { get; internal set; }
  148.         public string Operation { get; internal set; }
  149.         public int OperandPoints { get; internal set; }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement