Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion CourseApp.Tests/DemoTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
namespace CourseApp.Tests
{
using CourseApp;
using Xunit;

public class DemoTest
{
private Knight k;
private Blind b;
private Attack a;

public DemoTest()
{
k = new Knight(20, 20, "knight");
b = new Blind();
a = new Attack(5);
}

[Fact]
public void Test1()
{
Assert.True(true);
Assert.Equal(20, k.Health);
Assert.Equal(20, k.Damage);
Assert.Equal("knight", k.Name);
Assert.Equal(new Attack(20).Damage, k.ActiveAbility.Damage);
}

[Fact]
public void Test2()
{
k.AddEffect(b);
Assert.Equal(0, k.CheckState());
Assert.Equal(new StepSkip().Damage, k.MakeStep().Damage);
}

[Fact]
public void Test3()
{
k.AddEffect(a);
Assert.Equal(5, k.CheckState());
}

[Fact]
public void Test3()
{
k.Refresh();
Assert.Equal(0, k.CheckState());
}
}
}
36 changes: 36 additions & 0 deletions CourseApp/Ability.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace CourseApp
{
public enum AbilityTypes
{
Skip,
Attack,
Blind,
}

public abstract class Ability
{
public Ability(string abilityName, int damage, int steps, AbilityTypes type)
{
AbilityName = abilityName;
Damage = damage;
StepsDuration = steps;
CanUse = true;
Type = type;
}

public AbilityTypes Type { get; }

public string AbilityName { get; }

public int Damage { get; }

public int StepsDuration { get; set; }

public bool CanUse { get; set; }

public virtual Ability UseAbility()
{
return this;
}
}
}
12 changes: 12 additions & 0 deletions CourseApp/Archer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace CourseApp
{
using System.Collections.Generic;

public class Archer : Entity
{
public Archer(int health, int damage, string name)
: base(health, damage, name, new FireArrows())
{
}
}
}
10 changes: 10 additions & 0 deletions CourseApp/Attack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CourseApp
{
public class Attack : Ability
{
public Attack(int damage)
: base("Атака", damage, 1, AbilityTypes.Attack)
{
}
}
}
80 changes: 80 additions & 0 deletions CourseApp/BattleManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace CourseApp
{
using System.Collections.Generic;

public class BattleManager
{
private List<Entity> players;

private EntitiesFactory entitiesFactory;

private ILogger logger;

public BattleManager(int playerCount, ILogger logger)
{
players = new List<Entity>();
entitiesFactory = new EntitiesFactory();
this.logger = logger;
InitPlayers(playerCount);
}

public void Battle()
{
for (int round = 1; players.Count > 1; round++)
{
logger.Log($"Round {round}");
for (int i = 1; i < players.Count; i++)
{
Duel(players[i - 1], players[i]);
logger.Log($"---------");
}

RefreshPlayers();
}
}

public void Duel(Entity e1, Entity e2)
{
while (e1.Health > 0 && e2.Health > 0)
{
DoEntityAction(e1, e2);
DoEntityAction(e2, e1);
}
}

public void DoEntityAction(Entity src, Entity dest)
{
if (src.Health <= 0)
{
return;
}

dest.AddEffect(src.MakeStep());
int damage = dest.CheckState();
logger.Log($"{src.Name} использует {src.ActiveAbility.AbilityName} и наносит {damage} ед. урона");
if (dest.Health <= 0)
{
logger.Log($"{dest.Name} погибает");
players.Remove(dest);
}
}

public void InitPlayers(int count)
{
while (count > 0)
{
Entity e = entitiesFactory.CreateEntity();
players.Add(e);
count--;
}
}

public void RefreshPlayers()
{
foreach (var p in players)
{
p.Refresh();
}
}
}
}
10 changes: 10 additions & 0 deletions CourseApp/Blind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CourseApp
{
public class Blind : Ability
{
public Blind()
: base("Ослепление", 0, 1, AbilityTypes.Blind)
{
}
}
}
41 changes: 41 additions & 0 deletions CourseApp/EntitiesFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace CourseApp
{
public class EntitiesFactory
{
private string[] names =
{
"Олег",
"Владилен",
"Борислав",
"Всеволод",
"Гаврила",
"Казимир",
};

public enum Classes
{
Archer = 0,
Knight,
Wizard,
ClassesLength,
}

public Entity CreateEntity()
{
System.Random random = new System.Random();
int classIndex = random.Next((int)Classes.ClassesLength);
int nameIndex = random.Next(names.Length);
switch (classIndex)
{
case (int)Classes.Archer:
return new Archer(random.Next(1, 50), random.Next(1, 20), names[nameIndex]);
case (int)Classes.Knight:
return new Knight(random.Next(1, 50), random.Next(1, 20), names[nameIndex]);
case (int)Classes.Wizard:
return new Wizard(random.Next(1, 50), random.Next(1, 20), names[nameIndex]);
default:
return new Knight(random.Next(1, 50), random.Next(1, 20), names[nameIndex]);
}
}
}
}
90 changes: 90 additions & 0 deletions CourseApp/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
namespace CourseApp
{
using System.Collections.Generic;

public class Entity
{
private List<Ability> abilities;
private List<Ability> effects;

public Entity(int health, int damage, string name, Ability ability)
{
abilities = new List<Ability>
{
new Attack(damage),
ability,
};
effects = new List<Ability>();
ActiveAbility = abilities[0];
Health = health;
Damage = damage;
Name = name;
CanAttack = true;
}

public int Health { get; private set; }

public int Damage { get; private set; }

public string Name { get; }

public bool CanAttack { get; protected set; }

public Ability ActiveAbility { get; private set; }

public Ability MakeStep()
{
if (Health <= 0 || !CanAttack)
{
CanAttack = true;
ActiveAbility = new StepSkip();
return ActiveAbility.UseAbility();
}

System.Random random = new System.Random();
int abilityIndex = random.Next(abilities.Count);
ActiveAbility = abilities[abilityIndex];
if (!ActiveAbility.CanUse)
{
ActiveAbility = abilities[0];
}

return ActiveAbility.UseAbility();
}

public int CheckState()
{
int totalDamage = 0;
foreach (var e in effects)
{
totalDamage += e.Damage;
if (e.Type == AbilityTypes.Blind)
{
CanAttack = false;
}

e.StepsDuration--;
}

Health -= totalDamage;
effects.RemoveAll(e => e.StepsDuration <= 0);
return totalDamage;
}

public void Refresh()
{
foreach (var a in abilities)
{
a.CanUse = true;
}

CanAttack = true;
effects = new List<Ability>();
}

public void AddEffect(Ability ability)
{
effects.Add(ability);
}
}
}
16 changes: 16 additions & 0 deletions CourseApp/FireArrows.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace CourseApp
{
public class FireArrows : Ability
{
public FireArrows()
: base("Огненные стрелы", 2, 9999, AbilityTypes.Attack)
{
}

public override Ability UseAbility()
{
CanUse = false;
return this;
}
}
}
7 changes: 7 additions & 0 deletions CourseApp/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CourseApp
{
public interface ILogger
{
void Log(string str);
}
}
12 changes: 12 additions & 0 deletions CourseApp/Knight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace CourseApp
{
using System.Collections.Generic;

public class Knight : Entity
{
public Knight(int health, int damage, string name)
: base(health, damage, name, new RetaliationStrike((int)(damage + ((damage / 10) * 3))))
{
}
}
}
10 changes: 10 additions & 0 deletions CourseApp/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CourseApp
{
public class Logger : ILogger
{
public void Log(string str)
{
System.Console.WriteLine(str);
}
}
}
Loading