Removed bugs from Bucket.cs and Well.cs

Added working FightFire method from Person.cs
This commit is contained in:
MarcUs7i 2024-12-13 22:49:41 +01:00
parent 6d01e25314
commit b7367aeb20
3 changed files with 53 additions and 9 deletions

View file

@ -32,17 +32,18 @@ public sealed class Bucket
/// <returns>The amount taken for filling the bucket</returns>
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);
}
/// <summary>

View file

@ -21,7 +21,7 @@ public sealed class Person
/// <summary>
/// Gets if the person currently has at least one bucket (forward or backward).
/// </summary>
public bool HasBucket => false; // TODO
public bool HasBucket => _forwardBucket != null || _backwardBucket != null;
/// <summary>
/// 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</param>
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;
}
}
/// <summary>
@ -66,7 +84,29 @@ public sealed class Person
/// that a bucket was moved, only that no invalid state was detected; false otherwise</returns>
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
/// <param name="fire">The fire to throw water at</param>
public void FightFire(Fire fire)
{
// TODO
var amount = _forwardBucket?.Empty();
fire.GetHitByWater(amount ?? 0);
_backwardBucket = _forwardBucket;
}
/// <summary>

View file

@ -19,6 +19,7 @@ public sealed class Well
{
_maxCapacity = maxCapacity;
_refillRate = refillRate;
LitersRemaining = maxCapacity;
}
/// <summary>
@ -54,5 +55,5 @@ public sealed class Well
/// Shows the current and max. amount of liters - rounded to whole numbers.
/// </summary>
/// <returns>String representation of the well</returns>
public override string ToString() => $"💧 {LitersRemaining}/{_maxCapacity}";
public override string ToString() => $"💧 {Math.Round(LitersRemaining)}/{Math.Round(_maxCapacity)}";
}