diff options
| author | Fuwn <[email protected]> | 2025-10-16 20:23:12 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-10-16 20:23:12 -0700 |
| commit | c9d655807d0046be1fa2cd19991354b703c55c87 (patch) | |
| tree | ac14221c639baf208c01cc11ee01649d9eef05b7 /MorgSimulator/Dish.cs | |
| download | cst276-c9d655807d0046be1fa2cd19991354b703c55c87.tar.xz cst276-c9d655807d0046be1fa2cd19991354b703c55c87.zip | |
feat: Implement Assignment 1 functionality
Diffstat (limited to 'MorgSimulator/Dish.cs')
| -rw-r--r-- | MorgSimulator/Dish.cs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/MorgSimulator/Dish.cs b/MorgSimulator/Dish.cs new file mode 100644 index 0000000..d81fc8a --- /dev/null +++ b/MorgSimulator/Dish.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using System.Linq; + +namespace MorgSimulator +{ + public class Dish + { + private readonly List<Morg> _morgs = []; + + public void AddMorg(Morg morg) + { + _morgs.Add(morg); + } + + public List<Morg> GetAllMorgs() + { + return [.. _morgs]; + } + +#nullable enable + public Morg? FindNearestPrey(Morg predator) + { + Morg? nearestPrey = null; + double nearestDistance = double.MaxValue; + + foreach (var potentialPrey in _morgs.Where(m => m.IsAlive && m != predator)) + if (predator.CanEat(potentialPrey.Type)) + { + double distance = predator.DistanceTo(potentialPrey.Location); + + if (distance < nearestDistance) + { + nearestDistance = distance; + nearestPrey = potentialPrey; + } + } + + return nearestPrey; + } +#nullable disable + } +}
\ No newline at end of file |