From b7367aeb20dbf029183d8b7e4edbea1cf5a3df33 Mon Sep 17 00:00:00 2001
From: MarcUs7i <96580944+MarcUs7i@users.noreply.github.com>
Date: Fri, 13 Dec 2024 22:49:41 +0100
Subject: [PATCH] Removed bugs from Bucket.cs and Well.cs Added working
FightFire method from Person.cs
---
BucketChain/Bucket.cs | 9 ++++----
BucketChain/Person.cs | 50 +++++++++++++++++++++++++++++++++++++++----
BucketChain/Well.cs | 3 ++-
3 files changed, 53 insertions(+), 9 deletions(-)
diff --git a/BucketChain/Bucket.cs b/BucketChain/Bucket.cs
index c7da044..b29ab4a 100644
--- a/BucketChain/Bucket.cs
+++ b/BucketChain/Bucket.cs
@@ -32,17 +32,18 @@ public sealed class Bucket
/// The amount taken for filling the bucket
public double Fill(double maxAvailable)
{
- if (_capacityLiters - _contentLiters <= 0D)
+ if (_capacityLiters - _contentLiters <= 0D || maxAvailable <= 0D)
{
return 0D;
}
- maxAvailable = Math.Abs(_capacityLiters - maxAvailable);
+ var amountTaken = _contentLiters;
_contentLiters = _contentLiters + maxAvailable > _capacityLiters
? _capacityLiters
: _contentLiters + maxAvailable;
-
- return maxAvailable;
+
+ amountTaken -= _contentLiters;
+ return Math.Abs(amountTaken);
}
///
diff --git a/BucketChain/Person.cs b/BucketChain/Person.cs
index a6a96c3..c94cdfc 100644
--- a/BucketChain/Person.cs
+++ b/BucketChain/Person.cs
@@ -21,7 +21,7 @@ public sealed class Person
///
/// Gets if the person currently has at least one bucket (forward or backward).
///
- public bool HasBucket => false; // TODO
+ public bool HasBucket => _forwardBucket != null || _backwardBucket != null;
///
/// Gets the left neighbor of the person. Can be null.
@@ -50,7 +50,25 @@ public sealed class Person
/// neighbor of this person; otherwise it will become the right hand side neighbor
public void JoinChain(Person neighbor, bool isLeftNeighbor)
{
- // TODO
+ // expect that that all neighbors of the new person are null
+ // so that way we truly know that the provided person is new
+ if (neighbor.LeftNeighbor == null || neighbor.RightNeighbor == null)
+ {
+ return;
+ }
+
+ var thisNeighbor = isLeftNeighbor ? LeftNeighbor : RightNeighbor;
+ thisNeighbor?.JoinChain(neighbor, isLeftNeighbor);
+ if (isLeftNeighbor)
+ {
+ neighbor.RightNeighbor = this;
+ LeftNeighbor = neighbor;
+ }
+ else
+ {
+ neighbor.LeftNeighbor = this;
+ RightNeighbor = neighbor;
+ }
}
///
@@ -66,7 +84,29 @@ public sealed class Person
/// that a bucket was moved, only that no invalid state was detected; false otherwise
public bool MoveBucket(int currentStep)
{
- // TODO
+ if (currentStep < 0 || currentStep == LastStepPerformed || RightNeighbor == null)
+ {
+ return false;
+ }
+
+ if(RightNeighbor.HasBucket)
+ {
+ if (RightNeighbor._backwardBucket != null && _forwardBucket == null)
+ {
+ _forwardBucket = RightNeighbor._backwardBucket;
+ RightNeighbor._backwardBucket = null;
+ LastStepPerformed = currentStep;
+ return true;
+ }
+ if (RightNeighbor._forwardBucket != null && _backwardBucket == null)
+ {
+ _backwardBucket = RightNeighbor._forwardBucket;
+ RightNeighbor._forwardBucket = null;
+ LastStepPerformed = currentStep;
+ return true;
+ }
+ }
+
return false;
}
@@ -77,7 +117,9 @@ public sealed class Person
/// The fire to throw water at
public void FightFire(Fire fire)
{
- // TODO
+ var amount = _forwardBucket?.Empty();
+ fire.GetHitByWater(amount ?? 0);
+ _backwardBucket = _forwardBucket;
}
///
diff --git a/BucketChain/Well.cs b/BucketChain/Well.cs
index bb9650b..dc0f428 100644
--- a/BucketChain/Well.cs
+++ b/BucketChain/Well.cs
@@ -19,6 +19,7 @@ public sealed class Well
{
_maxCapacity = maxCapacity;
_refillRate = refillRate;
+ LitersRemaining = maxCapacity;
}
///
@@ -54,5 +55,5 @@ public sealed class Well
/// Shows the current and max. amount of liters - rounded to whole numbers.
///
/// String representation of the well
- public override string ToString() => $"💧 {LitersRemaining}/{_maxCapacity}";
+ public override string ToString() => $"💧 {Math.Round(LitersRemaining)}/{Math.Round(_maxCapacity)}";
}
\ No newline at end of file