From c3bd3a74168421fa8739048a5e3f2b7b67e97ab1 Mon Sep 17 00:00:00 2001 From: MarcUs7i <96580944+MarcUs7i@users.noreply.github.com> Date: Wed, 18 Dec 2024 08:09:01 +0100 Subject: [PATCH] Changed fields to match the README.adoc Operate_Initially_NotLongEnough is currently in an infinite loop --- BucketChain/Chain.cs | 29 ++++++++++++++++++++++++----- BucketChain/Person.cs | 15 ++++++++++++--- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/BucketChain/Chain.cs b/BucketChain/Chain.cs index 6ab8f78..b46ee65 100644 --- a/BucketChain/Chain.cs +++ b/BucketChain/Chain.cs @@ -11,7 +11,6 @@ public sealed class Chain private readonly double _bucketSize; private Person _firstPerson; private int _availableBuckets; - private int _usedBuckets; /// /// 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; + } /// /// Represents this with the referenced , diff --git a/BucketChain/Person.cs b/BucketChain/Person.cs index f3a4895..ac23723 100644 --- a/BucketChain/Person.cs +++ b/BucketChain/Person.cs @@ -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); + } } }