Advertisement
centner_dc

Exercise 1 v2

Aug 25th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. ///Exercise 1
  2.  
  3. using System;
  4.  
  5. namespace IMJunior
  6. {
  7.     public class CharacterStat
  8.     {
  9.         const int MaxPoints = 10;
  10.  
  11.         public string Name { get; private set; }
  12.  
  13.         public int value = 0;
  14.  
  15.         public CharacterStat(string _name) => Name = _name;
  16.  
  17.         public string GetViewStat() => "".PadLeft(value, '#').PadRight(MaxPoints, '_');
  18.  
  19.         public int GetAvailable(int sign)
  20.         {
  21.             if (sign == 1 && value == 0 || sign == -1 && value == MaxPoints)
  22.                 return MaxPoints;
  23.             else
  24.                 return (MaxPoints - sign * value) % MaxPoints;
  25.         }
  26.     }
  27.  
  28.  
  29.     class Program
  30.     {
  31.         static void Main(string[] args)
  32.         {
  33.             int points = 25;
  34.             int age = 0;
  35.  
  36.             CharacterStat[] Abilities = new CharacterStat[3]
  37.             {
  38.                     new CharacterStat("Сила"),
  39.                     new CharacterStat("Ловкость"),
  40.                     new CharacterStat("Интелект")
  41.             };
  42.  
  43.             Console.WriteLine("Добро пожаловать в меню выбора создания персонажа!");
  44.             Console.WriteLine("У вас есть {0} очков, которые вы можете распределить по умениям", points);
  45.             Console.WriteLine("Нажмите любую клавишу чтобы продолжить...");
  46.  
  47.             Console.ReadKey();
  48.  
  49.             while (points > 0)
  50.             {
  51.                 Console.Clear();
  52.  
  53.                 ViewAllStats();
  54.  
  55.                 Console.WriteLine("Какую характеристику вы хотите изменить?");
  56.                 string subject = Console.ReadLine();
  57.  
  58.                 Console.WriteLine(@"Что вы хотите сделать? +\-");
  59.                 string operation = Console.ReadLine();
  60.  
  61.                 Console.WriteLine(@"Колличество поинтов которые следует {0}", operation == "+" ? "прибавить" : "отнять");
  62.  
  63.                 int operandPoints = 0;
  64.                 ReadIntLine(out operandPoints);
  65.  
  66.                 if (operandPoints < 0)
  67.                     operandPoints *= -1;
  68.  
  69.                 ChangeStat(subject, (operation == "+") ? 1 : -1, operandPoints);
  70.             }
  71.  
  72.             Console.WriteLine("Вы распределили все очки. Введите возраст персонажа:");
  73.             ReadIntLine(out age);
  74.  
  75.             Console.Clear();
  76.             ViewAllStats();
  77.             Console.ReadLine();
  78.            
  79.  
  80.             void ViewAllStats()
  81.             {
  82.                 string abilities = $"Поинтов - {points}\nВозраст - {age}\n";
  83.                 foreach (var item in Abilities)
  84.                     abilities += $"{item.Name} - [{item.GetViewStat()}] \n";
  85.  
  86.                 Console.WriteLine(abilities);
  87.             }
  88.  
  89.             void ChangeStat(string subject, int sign, int operandPoints)
  90.             {
  91.                 subject = subject.ToLower();
  92.                 foreach (var item in Abilities)
  93.                 {
  94.                     if (item.Name.ToLower() == subject)
  95.                     {
  96.                         int available = item.GetAvailable(sign);
  97.  
  98.                         if (sign == 1)
  99.                             available = Math.Min(available, points);
  100.  
  101.                         operandPoints = sign * Math.Min(available, operandPoints);
  102.                         item.value += operandPoints;
  103.                         points -= operandPoints;
  104.  
  105.                         break;
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             void ReadIntLine(out int value)
  111.             {
  112.                 string readValue = string.Empty;
  113.                 do
  114.                 {
  115.                     readValue = Console.ReadLine();
  116.                 } while (!int.TryParse(readValue, out value));
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement