Changed fields to match the README.adoc

Operate_Initially_NotLongEnough is currently in an infinite loop
This commit is contained in:
MarcUs7i 2024-12-18 08:09:01 +01:00
parent db300f950c
commit c3bd3a7416
2 changed files with 36 additions and 8 deletions

View file

@ -11,7 +11,6 @@ public sealed class Chain
private readonly double _bucketSize;
private Person _firstPerson;
private int _availableBuckets;
private int _usedBuckets;
/// <summary>
/// Creates a new bucket chain based on the supplied parameters.
@ -58,10 +57,9 @@ public sealed class Chain
// step 3
Bucket? bucket = null;
if (_usedBuckets < _availableBuckets)
if (GetBucketCount() < _availableBuckets)
{
bucket = new Bucket(_bucketSize);
_usedBuckets++;
}
_firstPerson.UseWell(_well, bucket);
@ -80,13 +78,18 @@ public sealed class Chain
lastPerson = currentPerson;
bool movedSuccessfully = currentPerson.MoveBucket(currentStep);
if (!movedSuccessfully && currentPerson.RightNeighbor != null)
if (!movedSuccessfully)
{
if (currentPerson.RightNeighbor.HasBucket)
// check if move failed because the step was already performed
if (currentPerson.LastStepPerformed != currentStep &&
(currentPerson.RightNeighbor?.HasBucket ?? false))
{
// move failed because of an invalid state (neighbor can't accept the bucket)
error = true;
return false;
}
// else, move wasn't successful because it was already performed
// continue without error
}
currentPerson = currentPerson.RightNeighbor;
@ -100,6 +103,22 @@ public sealed class Chain
return _fire.Extinguished;
}
private int GetBucketCount()
{
int count = 0;
Person? currentPerson = _firstPerson;
while (currentPerson != null)
{
if (currentPerson.HasBucket)
{
count++;
}
currentPerson = currentPerson.RightNeighbor;
}
return count;
}
/// <summary>
/// Represents this <see cref="Chain"/> with the referenced <see cref="Well"/>, <see cref="Fire"/>

View file

@ -113,6 +113,7 @@ public sealed class Person
}
LastStepPerformed = currentStep;
RightNeighbor.LastStepPerformed = currentStep;
return true;
}
@ -157,10 +158,18 @@ public sealed class Person
}
}
// else add additional bucket
else if (additionalBucket != null && _forwardBucket == null && _backwardBucket == null)
else if (additionalBucket != null)
{
_forwardBucket = additionalBucket;
well.FillBucket(_forwardBucket);
if (_forwardBucket == null)
{
_forwardBucket = additionalBucket;
well.FillBucket(_forwardBucket);
}
else if (_backwardBucket == null)
{
_backwardBucket = additionalBucket;
well.FillBucket(_backwardBucket);
}
}
}