Доделать

This commit is contained in:
EgorGud
2026-10-14 10:58:22 +03:00
parent 8f8bf7ef8d
commit fd8994066a
+96 -17
View File
@@ -1,15 +1,54 @@
using System.Globalization; var pokemonList = new List<Pokemon>();
for (byte i = 1; i < 6; Console.WriteLine($"Added pokemon {i++}"))
{
if (i < 3)
{
pokemonList.Add(new Pokemon($"Pokemon {i}", (byte)(1 * i), (short)(100 * i)));
continue;
}
if (i < 5)
{
pokemonList.Add(new OtherPokemon($"Other pokemon {i}", (byte)(1 * i), (short)(100 * i)));
continue;
}
pokemonList.Add(new OtherPokemon($"", (byte)(1 * i), (short)(100 * i)));
}
Console.WriteLine();
foreach (var pokemon in pokemonList)
{
pokemon.ShowInfo();
pokemon.Attack();
switch (pokemon)
{
case IEvolvable ev:
ev.Evolve();
break;
case IMovable mov:
mov.Move();
break;
default:
Console.WriteLine("Pokemon don't have an IEvolvable or IMovable.");
break;
}
Console.WriteLine('\n');
}
Console.WriteLine($"Battle count: {Pokemon.BattleCount}");
// Classes
class Pokemon class Pokemon
{ {
// CONSTANTS // CONSTANTS
private const byte protected const byte
MIN_LEVEL = 1, MIN_LEVEL = 1,
MAX_LEVEL = 100; MAX_LEVEL = 100;
private const short protected const short
MIN_HP = 0, MIN_HP = 0,
MAX_HP = 2500; MAX_HP = 2500;
private const string DEFAULT_NAME = "Unknown"; protected const string DEFAULT_NAME = "Unknown";
// FIELDS // FIELDS
@@ -17,9 +56,11 @@ class Pokemon
protected byte level = MIN_LEVEL; protected byte level = MIN_LEVEL;
protected short hp = MIN_HP; protected short hp = MIN_HP;
protected static byte battleCount;
// PROPS // PROPS
public string Name public virtual string Name
{ {
get => name; get => name;
set => name = value ?? DEFAULT_NAME; set => name = value ?? DEFAULT_NAME;
@@ -45,29 +86,67 @@ class Pokemon
} }
} }
public static byte BattleCount => battleCount;
// CONSTRUCTORS // CONSTRUCTORS
public Pokemon(string name) public Pokemon(string name)
{ {
Name = name; Name = name;
} battleCount++;
public Pokemon(string name, byte level) : this(name) }
{ public Pokemon(string name, byte level) : this(name) => Level = level;
Level = level; public Pokemon(string name, byte level, short hp) : this(name, level) => Hp = hp;
}
public Pokemon(string name, byte level, short hp) : this(name, level)
{
Hp = hp;
}
// METHODS // METHODS
public override string ToString() => $"Pokemon: {Name}\tLevel: {Level}\tHp: {Hp}"; public override string ToString() => $"Pokemon: {Name}\tLevel: {Level}\tHp: {Hp}";
public virtual void Attack() => Console.WriteLine($"{Name} is attacking!"); public virtual void Attack() => Console.WriteLine($"{Name} is attacking!");
public void ShowInfo() => Console.WriteLine($"=== INFORMATION ===\nPokemon: {Name}\t(level: {Level} | hp: {Hp})"); public void ShowInfo() => Console.WriteLine($"=== INFORMATION ===\nPokemon: {Name}\t(level: {Level} | hp: {Hp})");
} }
class OtherPokemon : Pokemon class OtherPokemon : Pokemon
{ {
// CONSTRUCTORS
public OtherPokemon(string name) : base(name){}
public OtherPokemon(string name, byte level) : base(name, level){}
public OtherPokemon(string name, byte level, short hp) : base(name, level, hp){}
}
// METHODS
public override void Attack() => Console.WriteLine($"{Name} is attacks in a different way!");
}
class Charmander : Pokemon, IEvolvable, IMovable
{
// CONSTANTS
private const string NAME = "Charmander";
// FIELDS
private new string name;
// PROPS
public override string Name => name;
// CONSTRUCTORS
public Charmander(string name) : base(name) => this.name = NAME;
public Charmander(string name, byte level) : base(name, level) => this.name = NAME;
public Charmander(string name, byte level, short hp) : base(name, level, hp) => this.name = NAME;
// METHODS
public void Move() => Console.WriteLine($"{Name} is moving!");
public void Evolve() => Console.WriteLine($"{Name} is evolving!");
}
// Interfaces
interface IEvolvable
{
void Evolve();
}
interface IMovable
{
void Move();
}