Demo på Clean Code
This commit is contained in:
47
.gitignore
vendored
Normal file
47
.gitignore
vendored
Normal file
@@ -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/
|
||||
16
CleanDemo.sln
Normal file
16
CleanDemo.sln
Normal file
@@ -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
|
||||
30
CleanDemo/BattleService.cs
Normal file
30
CleanDemo/BattleService.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
10
CleanDemo/CleanDemo.csproj
Normal file
10
CleanDemo/CleanDemo.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
30
CleanDemo/Enemy.cs
Normal file
30
CleanDemo/Enemy.cs
Normal file
@@ -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}");
|
||||
}
|
||||
}
|
||||
4
CleanDemo/EnemyType.cs
Normal file
4
CleanDemo/EnemyType.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
public enum EnemyType {
|
||||
Normal,
|
||||
Boss
|
||||
}
|
||||
37
CleanDemo/GameRunner.cs
Normal file
37
CleanDemo/GameRunner.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
5
CleanDemo/IAttackable.cs
Normal file
5
CleanDemo/IAttackable.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
public interface IAttackable {
|
||||
string Name { get; }
|
||||
bool IsDead { get; }
|
||||
void TakeDamage(int amount);
|
||||
}
|
||||
3
CleanDemo/IDescribable.cs
Normal file
3
CleanDemo/IDescribable.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
public interface IDescribable {
|
||||
void Describe();
|
||||
}
|
||||
45
CleanDemo/Player.cs
Normal file
45
CleanDemo/Player.cs
Normal 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)}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user