Advertisement
centner_dc

Exercise 1

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