Fixed JoinChain bug from Person.cs

This commit is contained in:
MarcUs7i 2024-12-13 23:23:05 +01:00
parent b7367aeb20
commit 0f1420367d

View file

@ -50,15 +50,26 @@ public sealed class Person
/// neighbor of this person; otherwise it will become the right hand side neighbor</param> /// neighbor of this person; otherwise it will become the right hand side neighbor</param>
public void JoinChain(Person neighbor, bool isLeftNeighbor) public void JoinChain(Person neighbor, bool isLeftNeighbor)
{ {
// expect that that all neighbors of the new person are null var neighborOfNeighbor = isLeftNeighbor ? neighbor.RightNeighbor : neighbor.LeftNeighbor;
// so that way we truly know that the provided person is new if (neighborOfNeighbor != null)
if (neighbor.LeftNeighbor == null || neighbor.RightNeighbor == null)
{ {
return; if(isLeftNeighbor)
{
neighborOfNeighbor.LeftNeighbor = this;
RightNeighbor = neighborOfNeighbor;
LeftNeighbor = neighbor;
neighbor.RightNeighbor = this;
} }
else
var thisNeighbor = isLeftNeighbor ? LeftNeighbor : RightNeighbor; {
thisNeighbor?.JoinChain(neighbor, isLeftNeighbor); neighborOfNeighbor.RightNeighbor = this;
LeftNeighbor = neighborOfNeighbor;
RightNeighbor = neighbor;
neighbor.LeftNeighbor = this;
}
}
else
{
if (isLeftNeighbor) if (isLeftNeighbor)
{ {
neighbor.RightNeighbor = this; neighbor.RightNeighbor = this;
@ -70,6 +81,7 @@ public sealed class Person
RightNeighbor = neighbor; RightNeighbor = neighbor;
} }
} }
}
/// <summary> /// <summary>
/// Attempts to move the forward bucket along the chain towards the fire. /// Attempts to move the forward bucket along the chain towards the fire.