From f3f9d196604f09397233bcb0955f4212d9990013 Mon Sep 17 00:00:00 2001 From: ananesicim Date: Thu, 26 Nov 2026 17:51:02 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20works=20=D1=81#/lab=207?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- works с#/lab 7 | 218 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 works с#/lab 7 diff --git a/works с#/lab 7 b/works с#/lab 7 new file mode 100644 index 0000000..61a7849 --- /dev/null +++ b/works с#/lab 7 @@ -0,0 +1,218 @@ +using System; +using System.Collections.Generic; + +interface IEvolvable +{ + void Evolve(); +} + +interface IFlyable +{ + void Fly(); +} + +abstract class Pokemon +{ + protected string name; + protected int level; + protected int hp; + + public static int AttackCount { get; private set; } = 0; + + public string Name + { + get { return name; } + set { name = string.IsNullOrWhiteSpace(value) ? "Безымянный" : value; } + } + + public int Level + { + get { return level; } + set { level = Math.Clamp(value, 1, 100); } + } + + public int HP + { + get { return hp; } + set { hp = Math.Clamp(value, 0, 100); } + } + + public Pokemon(string name, int level, int hp) + { + Name = name; + Level = level; + HP = hp; + } + + public void ShowInfo() + { + Console.Write($"{Name} (Ур. {Level}): "); + } + + public abstract void Attack(); + + protected void RegisterAttack() + { + AttackCount++; + } +} + +class Pikachu : Pokemon +{ + public Pikachu(string name, int level) : base(name, level, 100) { } + + public override void Attack() + { + Console.WriteLine("Пика-пика! Удар током!"); + RegisterAttack(); + } +} + +class Squirtle : Pokemon +{ + public Squirtle(string name, int level) : base(name, level, 100) { } + + public override void Attack() + { + Console.WriteLine("Сквирт-сквирт! Водяной пистолет!"); + RegisterAttack(); + } +} + +class Charmander : Pokemon, IEvolvable +{ + public Charmander(string name, int level) : base(name, level, 100) { } + + public override void Attack() + { + Console.WriteLine("Чар-чар! Огненный хвост!"); + RegisterAttack(); + } + + public void Evolve() + { + Console.WriteLine($"{Name} эволюционирует!"); + } +} + +class Bulbasaur : Pokemon +{ + public Bulbasaur(string name, int level) : base(name, level, 100) { } + + public override void Attack() + { + Console.WriteLine("Бульба! Листовой удар!"); + RegisterAttack(); + } +} + +class Pidgeotto : Pokemon, IFlyable, IEvolvable +{ + public Pidgeotto(string name, int level) : base(name, level, 100) { } + + public override void Attack() + { + Console.WriteLine("Пиджеотто атакует с воздуха!"); + RegisterAttack(); + } + + public void Fly() + { + Console.WriteLine($"{Name} взлетает!"); + } + + public void Evolve() + { + Console.WriteLine($"{Name} эволюционирует!"); + } +} + +class Magikarp : Pokemon +{ + public Magikarp(string name, int level) : base(name, level, 100) { } + + public override void Attack() + { + Console.WriteLine("Splash! Ничего не происходит."); + RegisterAttack(); + } +} + +class Program +{ + static void Main() + { + List tournament = new List + { + new Pikachu("Пикачу", 5), + new Squirtle("Сквиртл", 7), + new Charmander("Чармандер", 6), + new Bulbasaur("Бульбазавр", 4) + }; + + Console.WriteLine("=== ТУРНИР: ВСЕ ПРОТИВ ВСЕХ ==="); + Console.WriteLine($"Всего покемонов: {tournament.Count}"); + Console.WriteLine(); + Console.WriteLine("--- Раунд 1 ---"); + foreach (Pokemon pokemon in tournament) + { + pokemon.ShowInfo(); + pokemon.Attack(); + } + Console.WriteLine(); + Console.WriteLine($"Всего атак проведено: {Pokemon.AttackCount}"); + + Console.WriteLine(); + Console.WriteLine("=== СПОСОБНОСТИ ПОКЕМОНОВ ==="); + Console.WriteLine(); + + List trainers = new List + { + new Charmander("Чармандер", 6), + new Pidgeotto("Пиджеотто", 9), + new Magikarp("Магикарп", 3) + }; + + foreach (Pokemon pokemon in trainers) + { + List abilities = new List { "атаковать" }; + if (pokemon is IFlyable) abilities.Add("летать"); + if (pokemon is IEvolvable) abilities.Add("эволюционировать"); + + string text = abilities.Count == 1 + ? "только атаковать (пока что...)" + : string.Join(", ", abilities); + Console.WriteLine($"{pokemon.Name} (Ур. {pokemon.Level}) умеет: {text}"); + } + + Console.WriteLine(); + Console.WriteLine("--- Проверка способностей ---"); + int evolveCount = 0; + int flyCount = 0; + foreach (Pokemon pokemon in trainers) + { + bool canDoAnything = false; + if (pokemon is IFlyable flyable) + { + flyable.Fly(); + flyCount++; + canDoAnything = true; + } + if (pokemon is IEvolvable evolvable) + { + evolvable.Evolve(); + evolveCount++; + canDoAnything = true; + } + if (!canDoAnything) + { + Console.WriteLine($"{pokemon.Name} ничего не умеет, кроме Splash."); + } + } + + Console.WriteLine(); + Console.WriteLine("Итого:"); + Console.WriteLine($"Эволюционируют: {evolveCount}"); + Console.WriteLine($"Летают: {flyCount}"); + } +}