FWT - CompositionGame - C# (Tightly Coupled)

CSharp · Script

/Program.cs

using System;

Console.WriteLine("Welcome Jack Sparrow to the World of Pirates");
var pirate1 = new Pirate("Jack Sparrow", 100, new CursedSword());
var pirate2 = new Pirate("Random Pirate", 100, new Sword());
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 Pirate {
	public string Name {get;set;}
	public int HP {get;set;}
	public Sword Weapon {get;set;}
	public void Attack(Pirate 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(string name, int hp, Sword weapon) {
		Name = name;
		HP = hp;
		Weapon = weapon;
	}
}

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

public class CursedSword : Sword {
	public CursedSword() {
		Name = "Cursed Sword";
		AttackPower = 20;
	}
}
	
;