mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Add Life as a sequence of Gneerations
This commit is contained in:
31
56_Life_for_Two/csharp/Life.cs
Normal file
31
56_Life_for_Two/csharp/Life.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
|
||||
internal class Life : IEnumerable<Generation>
|
||||
{
|
||||
private readonly IReadWrite _io;
|
||||
|
||||
public Life(IReadWrite io)
|
||||
{
|
||||
_io = io;
|
||||
FirstGeneration = Generation.Create(io);
|
||||
}
|
||||
|
||||
public Generation FirstGeneration { get; }
|
||||
public string? Result { get; private set; }
|
||||
|
||||
public IEnumerator<Generation> GetEnumerator()
|
||||
{
|
||||
var current = FirstGeneration;
|
||||
while (current.Result is null)
|
||||
{
|
||||
current = current.CalculateNextGeneration();
|
||||
yield return current;
|
||||
|
||||
if (current.Result is null) { current.AddPieces(_io); }
|
||||
}
|
||||
|
||||
Result = current.Result;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
Reference in New Issue
Block a user