Initialized everything and completed Bucket.cs, Fire.cs, Well.cs.

This commit is contained in:
MarcUs7i 2024-12-12 17:04:24 +01:00
parent 176b3b7cbb
commit 6d01e25314
5 changed files with 55 additions and 31 deletions

View file

@ -5,7 +5,8 @@
/// </summary>
public sealed class Bucket
{
// TODO
private readonly double _capacityLiters;
private double _contentLiters;
/// <summary>
/// Creates a new bucket with the given capacity.
@ -13,13 +14,14 @@ public sealed class Bucket
/// <param name="capacityLiters">Bucket capacity in liters</param>
public Bucket(double capacityLiters)
{
// TODO
_capacityLiters = capacityLiters;
_contentLiters = 0D;
}
/// <summary>
/// Gets if this bucket is currently completely empty or not.
/// </summary>
public bool IsEmpty => false; // TODO
public bool IsEmpty => _contentLiters <= 0D;
/// <summary>
/// Fills the bucket to the brim if enough water is available.
@ -30,8 +32,17 @@ public sealed class Bucket
/// <returns>The amount taken for filling the bucket</returns>
public double Fill(double maxAvailable)
{
// TODO
return -1D;
if (_capacityLiters - _contentLiters <= 0D)
{
return 0D;
}
maxAvailable = Math.Abs(_capacityLiters - maxAvailable);
_contentLiters = _contentLiters + maxAvailable > _capacityLiters
? _capacityLiters
: _contentLiters + maxAvailable;
return maxAvailable;
}
/// <summary>
@ -40,7 +51,8 @@ public sealed class Bucket
/// <returns>Amount emptied from the bucket, in liters</returns>
public double Empty()
{
// TODO
return -1D;
var content = _contentLiters;
_contentLiters = 0D;
return content;
}
}

View file

@ -5,7 +5,12 @@
/// </summary>
public sealed class Chain
{
// TODO
private readonly Fire _fire;
private readonly int _requiredPeople;
private readonly Well _well;
private readonly double _bucketSize;
private Person _firstPerson;
private int _availableBuckets;
/// <summary>
/// Creates a new bucket chain based on the supplied parameters.
@ -19,7 +24,12 @@ public sealed class Chain
public Chain(int requiredPeople, int availableBuckets, double bucketSize,
Well well, Fire fire, Person firstPerson)
{
// TODO
_requiredPeople = requiredPeople;
_availableBuckets = availableBuckets;
_bucketSize = bucketSize;
_well = well;
_fire = fire;
_firstPerson = firstPerson;
}
/// <summary>

View file

@ -5,7 +5,7 @@
/// </summary>
public sealed class Fire
{
// TODO
private readonly double _growRate;
/// <summary>
/// Creates a new fire with the passed properties.
@ -14,19 +14,19 @@ public sealed class Fire
/// <param name="growRate">Rate by which the fire grows with each (chain operation) step</param>
public Fire(double initialFireSize, double growRate)
{
// TODO
_growRate = growRate;
FireSize = initialFireSize;
}
/// <summary>
/// Gets the current size of the fire; cannot be smaller than 0.
/// </summary>
// TODO
// public double FireSize ...
public double FireSize { get; private set; }
/// <summary>
/// Gets if the fire has been extinguished (= size is 0).
/// </summary>
public bool Extinguished => false; // TODO
public bool Extinguished => FireSize <= 0D;
/// <summary>
/// The fire is hit by a load of water from a bucket.
@ -35,7 +35,7 @@ public sealed class Fire
/// <param name="amount">Liters of water</param>
public void GetHitByWater(double amount)
{
// TODO
FireSize = FireSize - amount < 0D ? 0D : FireSize - amount;
}
/// <summary>
@ -44,7 +44,7 @@ public sealed class Fire
/// </summary>
public void BurnHigher()
{
// TODO
FireSize += _growRate;
}
/// <summary>
@ -52,5 +52,5 @@ public sealed class Fire
/// The size is rounded to whole numbers.
/// </summary>
/// <returns>String representation of the fire</returns>
public override string ToString() => string.Empty; // TODO
public override string ToString() => $"🔥 {Math.Round(FireSize)}";
}

View file

@ -5,7 +5,8 @@
/// </summary>
public sealed class Person
{
// TODO
private Bucket? _backwardBucket;
private Bucket? _forwardBucket;
/// <summary>
/// Creates a new person.
@ -25,21 +26,18 @@ public sealed class Person
/// <summary>
/// Gets the left neighbor of the person. Can be null.
/// </summary>
// TODO
// public Person? LeftNeighbor ...
public Person? LeftNeighbor { get; private set; }
/// <summary>
/// Gets the right neighbor of the person. Can be null.
/// </summary>
// TODO
// public Person? RightNeighbor ...
public Person? RightNeighbor { get; private set; }
/// <summary>
/// Gets the number of the last step in which this person performed a bucket move action.
/// Each person can only move, at most, once per step.
/// </summary>
// TODO
// public int LastStepPerformed ...
public int LastStepPerformed { get; private set; }
/// <summary>
/// The person joins an existing bucket chain at a specific position.

View file

@ -5,7 +5,8 @@
/// </summary>
public sealed class Well
{
// TODO
private readonly double _maxCapacity;
private readonly double _refillRate;
/// <summary>
/// Creates a new well with the supplied properties.
@ -16,22 +17,24 @@ public sealed class Well
/// operation step</param>
public Well(double maxCapacity, double refillRate)
{
// TODO
_maxCapacity = maxCapacity;
_refillRate = refillRate;
}
/// <summary>
/// Gets how many liters currently remain in the well.
/// Cannot exceed the max. capacity of the well or drop below 0.
/// </summary>
// TODO
// public double LitersRemaining ...
public double LitersRemaining { get; private set; }
/// <summary>
/// Refills the well by the constant rate.
/// </summary>
public void Refill()
{
// TODO
LitersRemaining = LitersRemaining + _refillRate > _maxCapacity
? _maxCapacity
: LitersRemaining + _refillRate;
}
/// <summary>
@ -42,7 +45,8 @@ public sealed class Well
/// <param name="bucket">The bucket to fill</param>
public void FillBucket(Bucket bucket)
{
// TODO
var litersTaken = bucket.Fill(LitersRemaining);
LitersRemaining -= litersTaken;
}
/// <summary>
@ -50,5 +54,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() => string.Empty; // TODO
public override string ToString() => $"💧 {LitersRemaining}/{_maxCapacity}";
}