From f966c97d3298b604f99b8ec7735abbbb0cf8091a Mon Sep 17 00:00:00 2001 From: Christian Ohlsson Date: Sun, 8 Mar 2026 11:55:18 +0100 Subject: [PATCH] =?UTF-8?q?Demo=20p=C3=A5=20Clean=20Code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 47 ++++++++++++++++++++++++++++++++++++++ CleanDemo.sln | 16 +++++++++++++ CleanDemo/BattleService.cs | 30 ++++++++++++++++++++++++ CleanDemo/CleanDemo.csproj | 10 ++++++++ CleanDemo/Enemy.cs | 30 ++++++++++++++++++++++++ CleanDemo/EnemyType.cs | 4 ++++ CleanDemo/GameRunner.cs | 37 ++++++++++++++++++++++++++++++ CleanDemo/IAttackable.cs | 5 ++++ CleanDemo/IDescribable.cs | 3 +++ CleanDemo/Player.cs | 45 ++++++++++++++++++++++++++++++++++++ 10 files changed, 227 insertions(+) create mode 100644 .gitignore create mode 100644 CleanDemo.sln create mode 100644 CleanDemo/BattleService.cs create mode 100644 CleanDemo/CleanDemo.csproj create mode 100644 CleanDemo/Enemy.cs create mode 100644 CleanDemo/EnemyType.cs create mode 100644 CleanDemo/GameRunner.cs create mode 100644 CleanDemo/IAttackable.cs create mode 100644 CleanDemo/IDescribable.cs create mode 100644 CleanDemo/Player.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7fe2b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,47 @@ +############################ +# Build results +############################ +bin/ +obj/ +out/ + +############################ +# Rider +############################ +.idea/ +*.sln.iml + +############################ +# User-specific files +############################ +*.user +*.userosscache +*.suo +*.userprefs + +############################ +# Mono / macOS +############################ +*.pidb +*.booproj +*.svd +.DS_Store + +############################ +# Logs +############################ +*.log + +.vscode/ + +############################ +# NuGet +############################ +packages/ +*.nupkg +*.snupkg + +############################ +# Publish output +############################ +publish/ \ No newline at end of file diff --git a/CleanDemo.sln b/CleanDemo.sln new file mode 100644 index 0000000..e56280f --- /dev/null +++ b/CleanDemo.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CleanDemo", "CleanDemo\CleanDemo.csproj", "{7A8AC146-FADF-47E1-8DA7-92FCA2C19354}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7A8AC146-FADF-47E1-8DA7-92FCA2C19354}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7A8AC146-FADF-47E1-8DA7-92FCA2C19354}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7A8AC146-FADF-47E1-8DA7-92FCA2C19354}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7A8AC146-FADF-47E1-8DA7-92FCA2C19354}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/CleanDemo/BattleService.cs b/CleanDemo/BattleService.cs new file mode 100644 index 0000000..a9630dc --- /dev/null +++ b/CleanDemo/BattleService.cs @@ -0,0 +1,30 @@ +public class BattleService { + public void PlayerAttacks(Player player, Enemy enemy) { + int totalDamage = player.CalculateDamage(); + enemy.TakeDamage(totalDamage); + Console.WriteLine($"{player.Name} attackts {enemy.Name} for {totalDamage}"); + + if (enemy.IsDead) { + Console.WriteLine($"{enemy.Name} is dead"); + string loot = enemy.GetLoot(); + player.AddToInventory((loot)); + Console.WriteLine($"{player.Name} received: {loot}"); + } + } + public void EnemyAttacks(Enemy enemy, Player player) { + if(enemy.IsDead) return; + + int totalDamage = CalculateEnemyDamage(enemy); + player.TakeDamage(totalDamage); + Console.WriteLine($"{enemy.Name} attackts {player.Name} for {totalDamage}"); + + if (player.IsDead) { + Console.WriteLine($"{player.Name} is dead"); + } + } + private int CalculateEnemyDamage(Enemy enemy) { + return enemy.Type == EnemyType.Boss + ? (int)(enemy.Damage * 1.5) + : enemy.Damage; + } +} \ No newline at end of file diff --git a/CleanDemo/CleanDemo.csproj b/CleanDemo/CleanDemo.csproj new file mode 100644 index 0000000..6c1dc92 --- /dev/null +++ b/CleanDemo/CleanDemo.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/CleanDemo/Enemy.cs b/CleanDemo/Enemy.cs new file mode 100644 index 0000000..2b6c7e5 --- /dev/null +++ b/CleanDemo/Enemy.cs @@ -0,0 +1,30 @@ +public class Enemy : IAttackable, IDescribable{ + public string Name { get; } + public bool IsDead { get; private set; } + public int Damage { get; } + public EnemyType Type { get; } + private int _health; + + public Enemy(string name, int health, int damage, EnemyType type) { + Name = name; + _health = health; + Damage = damage; + Type = type; + } + + public void TakeDamage(int amount) { + _health -= amount; + if (_health <= 0) + IsDead = true; + } + + public string GetLoot() { + return Name == "Dragon" ? "dragon_scale" : "gold"; + } + + public void Describe() { + Console.WriteLine($"--- " + Name + " (enemy) ---"); + Console.WriteLine($"HP: {_health}"); + Console.WriteLine($"Type: {Type}"); + } +} \ No newline at end of file diff --git a/CleanDemo/EnemyType.cs b/CleanDemo/EnemyType.cs new file mode 100644 index 0000000..665bbba --- /dev/null +++ b/CleanDemo/EnemyType.cs @@ -0,0 +1,4 @@ +public enum EnemyType { + Normal, + Boss +} \ No newline at end of file diff --git a/CleanDemo/GameRunner.cs b/CleanDemo/GameRunner.cs new file mode 100644 index 0000000..9e5ef13 --- /dev/null +++ b/CleanDemo/GameRunner.cs @@ -0,0 +1,37 @@ +public class GameRunner { + static void Main(string[] args) { + Player player = new Player("Hero", 100, 15); + player.AddToInventory("potion"); + player.AddToInventory("sword"); + + Enemy goblin = new Enemy("Goblin", 40, 8, EnemyType.Normal); + Enemy dragon = new Enemy("Dragon", 120, 25, EnemyType.Boss); + + var battle = new BattleService(); + + Console.WriteLine("=== GAME START ==="); + player.Describe(); + goblin.Describe(); + + Console.WriteLine("\n-- Round 1 --"); + battle.PlayerAttacks(player, goblin); + battle.EnemyAttacks(goblin, player); + battle.PlayerAttacks(player, goblin); + + Console.WriteLine("\n-- Round 2 --"); + player.Describe(); + player.Heal(); + + Console.WriteLine("\n-- Boss Fight --"); + dragon.Describe(); + battle.PlayerAttacks(player, dragon); + battle.EnemyAttacks(dragon, player); + battle.PlayerAttacks(player, dragon); + battle.EnemyAttacks(dragon, player); + battle.PlayerAttacks(player, dragon); + + + Console.WriteLine("\n=== RESULT ==="); + player.Describe(); + } +} \ No newline at end of file diff --git a/CleanDemo/IAttackable.cs b/CleanDemo/IAttackable.cs new file mode 100644 index 0000000..2176074 --- /dev/null +++ b/CleanDemo/IAttackable.cs @@ -0,0 +1,5 @@ +public interface IAttackable { + string Name { get; } + bool IsDead { get; } + void TakeDamage(int amount); +} \ No newline at end of file diff --git a/CleanDemo/IDescribable.cs b/CleanDemo/IDescribable.cs new file mode 100644 index 0000000..8dc6d43 --- /dev/null +++ b/CleanDemo/IDescribable.cs @@ -0,0 +1,3 @@ +public interface IDescribable { + void Describe(); +} \ No newline at end of file diff --git a/CleanDemo/Player.cs b/CleanDemo/Player.cs new file mode 100644 index 0000000..d8c610a --- /dev/null +++ b/CleanDemo/Player.cs @@ -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 _inventory = new List(); + + 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)}"); + } +} \ No newline at end of file