generated from admin/template
Добавить works с#/lab 7
This commit is contained in:
+218
@@ -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<Pokemon> tournament = new List<Pokemon>
|
||||
{
|
||||
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<Pokemon> trainers = new List<Pokemon>
|
||||
{
|
||||
new Charmander("Чармандер", 6),
|
||||
new Pidgeotto("Пиджеотто", 9),
|
||||
new Magikarp("Магикарп", 3)
|
||||
};
|
||||
|
||||
foreach (Pokemon pokemon in trainers)
|
||||
{
|
||||
List<string> abilities = new List<string> { "атаковать" };
|
||||
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}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user