Demo på Clean Code

This commit is contained in:
2026-03-08 11:55:18 +01:00
parent fe9a1e6400
commit f966c97d32
10 changed files with 227 additions and 0 deletions

45
CleanDemo/Player.cs Normal file
View File

@@ -0,0 +1,45 @@
public class Player : IAttackable, IDescribable{
public string Name { get; }
public int _health;
public readonly int _baseDamage;
public bool IsDead { get; private set; }
public readonly List<string> _inventory = new List<string>();
public Player(string name, int health, int baseDamage) {
Name = name;
_health = health;
_baseDamage = baseDamage;
}
public void TakeDamage(int amount) {
_health -= amount;
if (_health <= 0)
IsDead = true;
}
public void Heal() {
if (_inventory.Contains("potion")) {
_health += 30;
_inventory.Remove("potion");
Console.WriteLine($"{Name} healed to {_health}");
}
else {
Console.WriteLine("No potion available");
}
}
public int CalculateDamage() {
int totalDamage = _baseDamage;
if (_inventory.Contains("sword")) totalDamage += 10;
return totalDamage;
}
public void AddToInventory(string item) {
_inventory.Add(item);
}
public void Describe() {
Console.WriteLine($"--- {Name} ---");
Console.WriteLine($"HP: {_health}");
Console.WriteLine($"Base DMG: {_baseDamage}");
Console.WriteLine($"Inventory: {string.Join(", ", _inventory)}");
}
}