From 0c9d3580f52cc0ce77c950ff9092b4e71be71f4a Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Mon, 3 Oct 2022 22:00:42 +1100 Subject: [PATCH] Move output to context --- 70_Poetry/csharp/Context.cs | 76 ++++++++++++++++++++++++++++++++ 70_Poetry/csharp/IOExtensions.cs | 7 --- 70_Poetry/csharp/Phrase.cs | 3 +- 70_Poetry/csharp/Poem.cs | 47 ++++---------------- 4 files changed, 86 insertions(+), 47 deletions(-) delete mode 100644 70_Poetry/csharp/IOExtensions.cs diff --git a/70_Poetry/csharp/Context.cs b/70_Poetry/csharp/Context.cs index 0036b52a..9d340c8e 100644 --- a/70_Poetry/csharp/Context.cs +++ b/70_Poetry/csharp/Context.cs @@ -2,10 +2,86 @@ namespace Poetry; internal class Context { + private readonly IReadWrite _io; + private readonly IRandom _random; + + public Context(IReadWrite io, IRandom random) + { + _io = io; + _random = random; + } + public int I { get; set; } public int J { get; set; } public int K { get; set; } public int U { get; set; } public bool SkipComma { get; set; } public bool UseGroup2 { get; set; } + public bool ShouldIndent => U == 0 && J % 2 == 0; + public bool GroupNumberIsValid => J < 5; + + public void WritePhrase() + { + Phrase.GetPhrase(this).Write(_io, this); + } + + public void MaybeWriteComma() + { + if (!SkipComma && _random.NextFloat() <= 0.19F && U != 0) + { + _io.Write(","); + U = 2; + } + SkipComma = false; + } + + public void WriteSpaceOrNewLine() + { + if (_random.NextFloat() <= 0.65F) + { + _io.Write(" "); + U += 1; + } + else + { + _io.WriteLine(); + U = 0; + } + } + + public void Update(IRandom random) + { + I = random.Next(1, 6); + J += 1; + K += 1; + } + + public void MaybeIndent() + { + if (U == 0 && J % 2 == 0) + { + _io.Write(" "); + } + } + + public void ResetGroup(IReadWrite io) + { + J = 0; + io.WriteLine(); + } + + public bool MaybeCompleteStanza() + { + if (K > 20) + { + _io.WriteLine(); + U = K = 0; + UseGroup2 = true; + return true; + } + + return false; + } + + public void SkipNextComma() => SkipComma = true; } diff --git a/70_Poetry/csharp/IOExtensions.cs b/70_Poetry/csharp/IOExtensions.cs deleted file mode 100644 index 631c80f8..00000000 --- a/70_Poetry/csharp/IOExtensions.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Poetry; - -internal static class IOExtensions -{ - internal static void WritePhrase(this IReadWrite io, Context context) - => Phrase.GetPhrase(context).Write(io, context); -} diff --git a/70_Poetry/csharp/Phrase.cs b/70_Poetry/csharp/Phrase.cs index 3f00abae..919e8a71 100644 --- a/70_Poetry/csharp/Phrase.cs +++ b/70_Poetry/csharp/Phrase.cs @@ -16,7 +16,7 @@ internal class Phrase { new("beguiling me", ctx => ctx.U = 2), new("thrilled me"), - new("still sitting....", ctx => ctx.SkipComma = true), + new("still sitting....", ctx => ctx.SkipNextComma()), new("never flitting", ctx => ctx.U = 2), new("burned") }, @@ -73,7 +73,6 @@ internal class Phrase private static Phrase[] GetGroup(int groupNumber) => _phrases[Math.Max(groupNumber - 1, 0)]; - public void Write(IReadWrite io, Context context) { if (_condition.Invoke(context)) diff --git a/70_Poetry/csharp/Poem.cs b/70_Poetry/csharp/Poem.cs index 64038963..03f3af68 100644 --- a/70_Poetry/csharp/Poem.cs +++ b/70_Poetry/csharp/Poem.cs @@ -8,53 +8,24 @@ internal class Poem { io.Write(Streams.Title); - var context = new Context(); + var context = new Context(io, random); while (true) { - io.WritePhrase(context); - - if (!context.SkipComma && random.NextFloat() <= 0.19F && context.U != 0) - { - io.Write(","); - context.U = 2; - } - context.SkipComma = false; - - if (random.NextFloat() <= 0.65F) - { - io.Write(" "); - context.U += 1; - } - else - { - io.WriteLine(); - context.U = 0; - } + context.WritePhrase(); + context.MaybeWriteComma(); + context.WriteSpaceOrNewLine(); while (true) { - context.I = random.Next(1, 6); - context.J += 1; - context.K += 1; + context.Update(random); + context.MaybeIndent(); - if (context.U == 0 && context.J % 2 == 0) - { - io.Write(" "); - } + if (context.GroupNumberIsValid) { break; } - if (context.J < 5) { break; } + context.ResetGroup(io); - context.J = 0; - io.WriteLine(); - - if (context.K > 20) - { - io.WriteLine(); - context.U = context.K = 0; - context.UseGroup2 = true; - break; - } + if (context.MaybeCompleteStanza()) { break; } } } }