mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
Implementing https://github.com/coding-horror/basic-computer-games/blob/main/15_Boxing/boxing.bas in C#
This commit is contained in:
48
15_Boxing/csharp/AttackStrategy.cs
Normal file
48
15_Boxing/csharp/AttackStrategy.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
namespace Boxing;
|
||||
|
||||
public abstract class AttackStrategy
|
||||
{
|
||||
protected const int KnockoutDamageThreshold = 35;
|
||||
protected readonly Boxer Other;
|
||||
protected readonly Stack<Action> Work;
|
||||
private readonly Action _notifyGameEnded;
|
||||
|
||||
public AttackStrategy(Boxer other, Stack<Action> work, Action notifyGameEnded)
|
||||
{
|
||||
Other = other;
|
||||
Work = work;
|
||||
_notifyGameEnded = notifyGameEnded;
|
||||
}
|
||||
|
||||
public void Attack()
|
||||
{
|
||||
var punch = GetPunch();
|
||||
if (punch.IsBestPunch)
|
||||
{
|
||||
Other.DamageTaken += 2;
|
||||
}
|
||||
|
||||
Work.Push(punch.Punch switch
|
||||
{
|
||||
Punch.FullSwing => FullSwing,
|
||||
Punch.Hook => Hook,
|
||||
Punch.Uppercut => Uppercut,
|
||||
_ => Jab
|
||||
});
|
||||
}
|
||||
|
||||
protected abstract AttackPunch GetPunch();
|
||||
protected abstract void FullSwing();
|
||||
protected abstract void Hook();
|
||||
protected abstract void Uppercut();
|
||||
protected abstract void Jab();
|
||||
|
||||
protected void RegisterKnockout(string knockoutMessage)
|
||||
{
|
||||
Work.Clear();
|
||||
_notifyGameEnded();
|
||||
Console.WriteLine(knockoutMessage);
|
||||
}
|
||||
|
||||
protected record AttackPunch(Punch Punch, bool IsBestPunch);
|
||||
}
|
||||
Reference in New Issue
Block a user