FWT - CompositionGame - C# (De-Coupled)

CSharp · Script

/Program.cs

using System;
					
Console.WriteLine("Welcome Jack Sparrow to the World of Pirates");
var pirate1 = PirateFactory.CreateJackSparrow();
var pirate2 = PirateFactory.CreateEnemyPirate();
Console.WriteLine("You are armed with a {0}(ATK:{1}).", pirate1.Weapon.Name, pirate1.Weapon.AttackPower.ToString());
Console.WriteLine("You do battle with {0} armed with a {1}(ATK:{2}).", pirate2.Name, pirate2.Weapon.Name, pirate2.Weapon.AttackPower.ToString());
Console.WriteLine("------------------------");

Console.ReadLine();
var result = false;
while(!result) {
	pirate1.Attack(pirate2);
	pirate2.Attack(pirate1);
	
	result = (pirate1.HP < 1) || (pirate2.HP < 1);
	
	if(result) {
		Console.WriteLine("Winner is {0}", pirate1.HP < 1 ? pirate2.Name : pirate1.Name);
	}
}

public class PirateFactory {
	public static ICharacter CreateJackSparrow() {
		var weapon = WeaponFactory.CreateCursedSword();
		var pirate = new Pirate(weapon) { Name = "Jack Sparrow", HP = 100 };
		
		return pirate;
	}
	public static ICharacter CreateEnemyPirate() {
		var weapon = WeaponFactory.CreateSword();
		var pirate = new Pirate(weapon) { Name = "Enemy Pirate", HP = 100 };
		
		return pirate;
	}
}

public class WeaponFactory {
	public static IWeapon CreateSword() {
		return new Sword();
	}
	public static IWeapon CreateCursedSword() {
		return new CursedSword();
	}
}

public interface ICharacter {
	string Name {get;set;}
	int HP {get;set;}
	IWeapon Weapon {get;set;}
	void Attack(ICharacter character);
}

public interface IWeapon {
	string Name {get;set;}
	int AttackPower {get;set;}
}

public class Pirate : ICharacter {
	public string Name {get;set;}
	public int HP {get;set;}
	public IWeapon Weapon {get;set;}
	public void Attack(ICharacter character) {
		if(Weapon == null) {
			throw new NullReferenceException("Arr, I don't have a weapon");
		}
		character.HP = character.HP - Weapon.AttackPower;
		Console.WriteLine("{0} attacked {1} with a {2}, {1} HP reduced to: {3}", this.Name, character.Name, Weapon.Name, character.HP.ToString());
		Console.WriteLine("---------------------------");
	}
	
	public Pirate(IWeapon weapon) {
		Weapon = weapon;
	}
}

public class Sword : IWeapon {
	public string Name {get;set;}
	public int AttackPower {get;set;}
	
	public Sword() {
		AttackPower = 10;
		Name = "Sword";
	}
}

public class CursedSword : IWeapon {
	public string Name {get;set;}
	public int AttackPower {get;set;}
	
	public CursedSword() {
		AttackPower = 20;
		Name = "Cursed Sword";
	}
}
	
;