This commit is contained in:
Belyaev
2026-11-24 16:05:12 +03:00
parent aae41f6829
commit 0b29942a2e
7 changed files with 491 additions and 98 deletions
+167 -98
View File
@@ -1,83 +1,102 @@
class Program
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Pokemon pika = new Pikachu("Пикачу", 5, 100);
// Pokemon squirtle = new Squirtle("Сквиртл", 7, 90);
// Pokemon charmander = new Charmander("Charmander", 9, 80);
Console.WriteLine("=== ЗАДАНИЕ 1 ===");
Console.WriteLine("=== ПОКЕДЕКС: АТАКИ ===\n");
List<Pokemon> pokemons = new List<Pokemon>(4)
Console.WriteLine("=== ЗАДАНИЕ 1: ИЕРАРХИЯ И ВИРТУАЛЬНЫЕ МЕТОДЫ ===");
Pokemon basePokemon = new Pokemon("Дикий покемон", 1, 10);
Console.Write("Базовый вызов: ");
basePokemon.ShowInfo();
basePokemon.Attack();
Console.WriteLine(new string('-', 40));
/*
Console.WriteLine("=== ЗАДАНИЕ 2: ТУРНИР И ПОЛИМОРФИЗМ (АБСТРАКТНЫЙ КЛАСС) ===");
// Pokemon test = new Pokemon("Абстрактный", 1, 10); // КРИТИЧЕСКАЯ ОШИБКА КОМПИЛЯЦИИ!
*/
List<Pokemon> pokemons = new List<Pokemon>()
{
new Pikachu("Пикачу", 5, 100),
new Squirtle("Сквиртл", 7, 90),
new Charmander("Чармандер", 9, 80),
new Bulbasaur("Бульбазавр", 6, 95)
new Charmander("Чармандер", 6, 80),
new Bulbasaur("Бульбазавр", 4, 95)
};
foreach (Pokemon pokemon in pokemons)
Console.WriteLine($"Всего покемонов в списке: {pokemons.Count}");
Console.WriteLine("\n--- Раунд 1 ---");
foreach (Pokemon pokemon in pokemons)
{
pokemon.ShowInfo();
pokemon.Attack();
Console.WriteLine();
pokemon.Attack();
}
Console.WriteLine($"\nВсего атак проведено: {Pokemon.TotalAttacks}");
Console.WriteLine("\n=== ЗАДАНИЕ 3: СПОСОБНОСТИ ПОКЕМОНОВ ===");
foreach (Pokemon pokemon in pokemons)
{
Console.Write($"\n{pokemon.Name} (Ур. {pokemon.Level}) умеет: ");
if (pokemon is IEvolvable evolvable)
{
CapabilityCounter.EvolvableCount++;
evolvable.Evolve();
}
if (pokemon is IFlyable flyable)
{
CapabilityCounter.FlyableCount++;
flyable.Fly();
}
if (pokemon is not IEvolvable && pokemon is not IFlyable)
{
Console.WriteLine(" -> Особых способностей пока нет (только атака).");
}
}
Console.WriteLine("\nИтого:");
Console.WriteLine($"Эволюционируют: {CapabilityCounter.EvolvableCount}");
Console.WriteLine($"Летают: {CapabilityCounter.FlyableCount}");
Console.ReadLine();
}
// class Pokemon
// {
// protected string name;
//
// protected int hp;
//
// protected int level;
//
// public int HP
// {
// get { return hp; }
// set
// {
// if (value < 0) hp = 0;
// else if (value > 100) hp = 100;
// else hp = value;
// }
// }
//
// public int Level
// {
// get { return level; }
// set
// {
// if (value < 1) level = 1;
// else if (value > 100) level = 100;
// else level = value;
// }
// }
//
// public string Name;
//
// public Pokemon(string name, int level, int hp)
// {
// this.Name = name == "" ? "Безымянный" : name;
// this.Level = level;
// this.HP = hp;
// }
//
// public abstract void Attack()
// {
// Console.WriteLine("Покемон аттакует");
// }
//
// public void ShowInfo()
// {
// Console.WriteLine($"Name: {Name}, Уровень: {Level}, HP: {HP}");
// }
// }
abstract class Pokemon
// ИНТЕРФЕЙСЫ (ЗАДАНИЕ 3)
interface IEvolvable
{
protected string name;
void Evolve();
}
interface IFlyable
{
void Fly();
}
static class CapabilityCounter
{
public static int EvolvableCount { get; set; }
public static int FlyableCount { get; set; }
}
class Pokemon
{
private static int totalAttacks = 0;
public static int TotalAttacks
{
get { return totalAttacks; }
set { totalAttacks = value; }
}
protected int hp;
protected int level;
public int HP
{
get { return hp; }
@@ -89,17 +108,53 @@
}
}
public int Level
public int Level { get; set; }
public string Name;
public Pokemon(string name, int level, int hp)
{
get { return level; }
this.Name = name == "" ? "Безымянный" : name;
this.Level = level;
this.HP = hp;
}
public virtual void Attack()
{
Console.WriteLine("Покемон атакует!");
}
public void ShowInfo()
{
Console.Write($"{Name} (Ур. {Level}): ");
}
}
/*
// ВАРИАНТ ДЛЯ ЗАДАНИЯ 2: АБСТРАКТНЫЙ БАЗОВЫЙ КЛАСС С АБСТРАКТНЫМ МЕТОДОМ
abstract class Pokemon
{
private static int totalAttacks = 0;
public static int TotalAttacks
{
get { return totalAttacks; }
set { totalAttacks = value; }
}
protected int hp;
public int HP
{
get { return hp; }
set
{
if (value < 1) level = 1;
else if (value > 100) level = 100;
else level = value;
if (value < 0) hp = 0;
else if (value > 100) hp = 100;
else hp = value;
}
}
public int Level { get; set; }
public string Name;
public Pokemon(string name, int level, int hp)
@@ -109,58 +164,72 @@
this.HP = hp;
}
virtual public void Attack()
{
Console.WriteLine("Покемон аттакует");
}
// МЕТОД АБСТРАКТНЫЙ: У него НЕТ ТЕЛА. Только точка с запятой!
public abstract void Attack();
public void ShowInfo()
{
Console.WriteLine($"Name: {Name}, Уровень: {Level}, HP: {HP}");
Console.Write($"{Name} (Ур. {Level}): ");
}
}
*/
class Pikachu : Pokemon
{
public Pikachu(string name, int level, int hp) : base(name, level, hp)
{
}
public Pikachu(string name, int level, int hp) : base(name, level, hp) { }
public override void Attack()
{
Console.WriteLine($"{Name} бьёт током! Пика-пика!");
Pokemon.TotalAttacks++;
Console.WriteLine("Пика-пика! Удар током!");
}
}
class Charmander : Pokemon
class Charmander : Pokemon, IEvolvable
{
public Charmander(string name, int level, int hp) : base(name, level, hp)
{
}
public Charmander(string name, int level, int hp) : base(name, level, hp) { }
public override void Attack()
{
Console.WriteLine($"{Name} Чар-чар! Огненный хвост!");
Pokemon.TotalAttacks++;
Console.WriteLine("Чар-чар! Огненный хвост!");
}
public void Evolve()
{
Console.WriteLine(" -> Чармандер готовится к эволюции!");
}
}
class Squirtle : Pokemon
{
public Squirtle(string name, int level, int hp) : base(name, level, hp)
{
}
public Squirtle(string name, int level, int hp) : base(name, level, hp) { }
public override void Attack()
{
Console.WriteLine($"{Name} Сквирт-сквирт! Водяной пистолет!");
Pokemon.TotalAttacks++;
Console.WriteLine("Сквирт-сквирт! Водяной пистолет!");
}
}
class Bulbasaur : Pokemon
class Bulbasaur : Pokemon, IFlyable, IEvolvable
{
public Bulbasaur(string name, int level, int hp) : base(name, level, hp)
{
}
public Bulbasaur(string name, int level, int hp) : base(name, level, hp) { }
public override void Attack()
{
Console.WriteLine($"{Name} Бульба! Листовой удар!");
Pokemon.TotalAttacks++;
Console.WriteLine("Бульба! Листовой удар!");
}
public void Fly()
{
Console.WriteLine(" -> Бульбазавр взлетает на лозах!");
}
public void Evolve()
{
Console.WriteLine("\n -> Бульбазавр готовится к эволюции!");
}
}
}