Created
February 4, 2025 19:28
-
-
Save Sl0n4ik/8c2d77e40ec8323886aff0ee971b7fd7 to your computer and use it in GitHub Desktop.
Гладиаторские бои
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Arena | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Arena arena = new Arena(); | |
arena.Play(); | |
} | |
} | |
interface IDamageable | |
{ | |
void TakeDamage(int damage); | |
} | |
class Arena | |
{ | |
private List<Fighter> _fighters; | |
private UserUtils _userUtils; | |
public Arena() | |
{ | |
_fighters = new List<Fighter> { new Rascal(), new Warrior(), new Barbar(), new Wizzard(), new Ninja() }; | |
} | |
public void Play() | |
{ | |
const int CommandWelcomeMessage = 1; | |
const int CommandShowFight = 2; | |
const int CommandExit = 3; | |
int numberUserCommands = 3; | |
bool isRunning = true; | |
string commandInfo = $"{CommandWelcomeMessage} - приветственное сообщение.\n" + | |
$"{CommandShowFight} - выбрать бойцов для сражения.\n" + | |
$"{CommandExit} - закончить работу."; | |
string welcomeMessage = "Приветствуем на арене, именно тут вы сможете увидеть то, чего всем нам так не хватает!!!"; | |
Console.WriteLine(commandInfo); | |
while (isRunning) | |
{ | |
switch (_userUtils.ReadInt(numberUserCommands)) | |
{ | |
case CommandWelcomeMessage: | |
Console.WriteLine(welcomeMessage); | |
break; | |
case CommandShowFight: | |
ShowAllFighters(); | |
Fight(); | |
break; | |
case CommandExit: | |
isRunning = false; | |
break; | |
default: | |
Console.Clear(); | |
Console.WriteLine(commandInfo); | |
break; | |
} | |
} | |
} | |
private void ShowAllFighters() | |
{ | |
for (int i = 0; i < _fighters.Count; i++) | |
{ | |
Console.Write($"{i + 1}. "); | |
_fighters[i].ShowInfo(); | |
} | |
} | |
private void Fight() | |
{ | |
Fighter fighter1 = GetFighter().Clone(); | |
Fighter fighter2 = GetFighter().Clone(); | |
while (fighter1.IsFighterDead() == false && fighter2.IsFighterDead() == false) | |
{ | |
fighter1.Attack(fighter2); | |
fighter2.Attack(fighter1); | |
} | |
} | |
private Fighter GetFighter() | |
{ | |
Console.Write("выбран боец: "); | |
Fighter fighter = _fighters[_userUtils.ReadInt(_fighters.Count) - 1]; | |
fighter.RecoverStats(); | |
return fighter; | |
} | |
} | |
class UserUtils | |
{ | |
public int ReadInt(int numberLimit) | |
{ | |
bool isNumber = false; | |
int number = 0; | |
while (!isNumber) | |
{ | |
isNumber = int.TryParse(Console.ReadLine(), out int result); | |
if (isNumber && result <= numberLimit && result > 0) | |
{ | |
number = result; | |
} | |
else | |
{ | |
Console.WriteLine("Ошибка, попробуйте ещё раз."); | |
isNumber = false; | |
} | |
} | |
return number; | |
} | |
} | |
class Fighter : IDamageable | |
{ | |
protected string Name; | |
protected int MaxHealth; | |
protected int Health; | |
protected int MaxMana = 0; | |
protected int Mana = 0; | |
protected int Damage = 1; | |
protected bool IsDead = false; | |
protected int AbilityCount = 0; | |
protected int MinAbilityCount = 0; | |
private Random _random = new Random(); | |
public virtual Fighter Clone() => | |
new Fighter(); | |
public void RecoverStats() | |
{ | |
Health = MaxHealth; | |
Mana = MaxMana; | |
} | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"{Name} | Здоровье:{MaxHealth} | урон:{Damage} | запас маны: {MaxMana}."); | |
} | |
public virtual void Attack(Fighter fighter) | |
{ | |
Console.Write($"{Name}: атакует! "); | |
Console.WriteLine($"{Name}: нанёс {Damage} урона."); | |
fighter.TakeDamage(Damage); | |
} | |
public virtual void TakeDamage(int damage) => Health -= damage; | |
public bool IsFighterDead() | |
{ | |
if (Health <= 0) | |
{ | |
IsDead = true; | |
PaintMessage($"\t\t{Name}: умер.", ConsoleColor.Red); | |
return true; | |
} | |
PaintMessage($"\t\t{Name}: осталось {Health} здоровья.", ConsoleColor.Green); | |
return false; | |
} | |
protected void PaintMessage(string message, ConsoleColor color = ConsoleColor.Yellow) | |
{ | |
Console.ForegroundColor = color; | |
Console.WriteLine(message); | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
protected int GetRandomNumber() | |
{ | |
int minValue = 0; | |
int maxValue = 10; | |
return _random.Next(minValue, maxValue); | |
} | |
} | |
class Rascal : Fighter | |
{ | |
public Rascal() | |
{ | |
Name = "Плут"; | |
MaxHealth = 170; | |
Health = MaxHealth; | |
Damage = 25; | |
AbilityCount = 0; | |
MinAbilityCount = 2; | |
} | |
public override Fighter Clone() => | |
new Rascal(); | |
public override void Attack(Fighter fighter) | |
{ | |
if (IsDobleAttack()) | |
{ | |
fighter.TakeDamage(Damage); | |
fighter.TakeDamage(Damage); | |
} | |
else | |
base.Attack(fighter); | |
} | |
private bool IsDobleAttack() | |
{ | |
if (AbilityCount >= MinAbilityCount) | |
{ | |
AbilityCount = 0; | |
PaintMessage($"{Name}: атакует и наносит урон дважды!"); | |
return true; | |
} | |
AbilityCount++; | |
return false; | |
} | |
} | |
class Warrior : Fighter | |
{ | |
public Warrior() | |
{ | |
Name = "Воин"; | |
MaxHealth = 260; | |
Health = MaxHealth; | |
Damage = 34; | |
MinAbilityCount = 7; | |
} | |
public override Fighter Clone() => | |
new Warrior(); | |
public override void Attack(Fighter fighter) | |
{ | |
if (IsCriticalDamage(out int criticalDamage)) | |
fighter.TakeDamage(criticalDamage); | |
else | |
base.Attack(fighter); | |
} | |
private bool IsCriticalDamage(out int criticalDamage) | |
{ | |
AbilityCount = GetRandomNumber(); | |
if (AbilityCount >= MinAbilityCount) | |
{ | |
criticalDamage = Damage + Damage; | |
PaintMessage($"{Name}: атакует и наносит критический урон({criticalDamage})!"); | |
return true; | |
} | |
criticalDamage = 0; | |
return false; | |
} | |
} | |
class Barbar : Fighter | |
{ | |
public Barbar() | |
{ | |
Name = "Варвар"; | |
MaxHealth = 270; | |
Health = MaxHealth; | |
Damage = 35; | |
MinAbilityCount = 100; | |
} | |
public override Fighter Clone() => | |
new Barbar(); | |
public override void TakeDamage(int damage) | |
{ | |
base.TakeDamage(damage); | |
RecoverHealth(damage); | |
} | |
private void RecoverHealth(int damage) | |
{ | |
int healing = 37; | |
AbilityCount += damage; | |
if (AbilityCount >= MinAbilityCount) | |
{ | |
AbilityCount = 0; | |
Health += healing; | |
if (Health > MaxHealth && IsDead == false) | |
{ | |
Health = MaxHealth; | |
} | |
PaintMessage($"{Name}: востановил {healing} здоровья.\n"); | |
} | |
} | |
} | |
class Wizzard : Fighter | |
{ | |
private int _abilityManaCost = 80; | |
private int _abilityDamage = 70; | |
public Wizzard() | |
{ | |
Name = "Чародей"; | |
MaxHealth = 200; | |
Health = MaxHealth; | |
Damage = 15; | |
MaxMana = 200; | |
Mana = MaxMana; | |
} | |
public override Fighter Clone() => | |
new Wizzard(); | |
public override void Attack(Fighter fighter) | |
{ | |
if (CanUseFireBall(out int fireBall)) | |
fighter.TakeDamage(fireBall); | |
else | |
base.Attack(fighter); | |
} | |
private bool CanUseFireBall(out int fireBall) | |
{ | |
if (_abilityManaCost <= Mana) | |
{ | |
Mana -= _abilityManaCost; | |
fireBall = _abilityDamage; | |
PaintMessage($"{Name}: использует заклинание огненый шар, наносит {fireBall} урона!"); | |
return true; | |
} | |
fireBall = 0; | |
return false; | |
} | |
} | |
class Ninja : Fighter | |
{ | |
public Ninja() | |
{ | |
Name = "Ниндзя"; | |
MaxHealth = 260; | |
Health = MaxHealth; | |
Damage = 55; | |
MinAbilityCount = 7; | |
} | |
public override Fighter Clone() => | |
new Ninja(); | |
public override void TakeDamage(int damage) | |
{ | |
if (IsDodging() == false) | |
base.TakeDamage(damage); | |
} | |
private bool IsDodging() | |
{ | |
AbilityCount = GetRandomNumber(); | |
if (AbilityCount > MinAbilityCount) | |
{ | |
PaintMessage($"{Name}: увернулся от атаки."); | |
return true; | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment