Files
basic-computer-games/07_Basketball/csharp/Shot.cs
2022-04-11 08:45:15 +10:00

37 lines
932 B
C#

namespace Basketball;
public class Shot
{
private readonly string _name;
public Shot(string name)
{
_name = name;
}
public static bool TryGet(int shotNumber, out Shot? shot)
{
shot = shotNumber switch
{
// Although the game instructions reference two different jump shots,
// the original game code treats them both the same and just prints "Jump shot"
<= 2 => new JumpShot(),
3 => new Shot("Lay up"),
4 => new Shot("Set shot"),
_ => null
};
return shot is not null;
}
public static Shot Get(float shotNumber) =>
shotNumber switch
{
<= 2 => new JumpShot(),
> 3 => new Shot("Set shot"),
> 2 => new Shot("Lay up"),
_ => throw new Exception("Unexpected value")
};
public override string ToString() => _name;
}