From f27908a3e4720ebc6a2becab4db51616f44bd861 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Thu, 29 Sep 2022 22:46:10 +1000 Subject: [PATCH 1/6] Implement poetry generator --- 70_Poetry/csharp/Context.cs | 11 +++++ 70_Poetry/csharp/IOExtensions.cs | 8 +++ 70_Poetry/csharp/Phrase.cs | 83 ++++++++++++++++++++++++++++++++ 70_Poetry/csharp/Poem.cs | 56 +++++++++++++++++++++ 70_Poetry/csharp/Poetry.csproj | 3 ++ 70_Poetry/csharp/Program.cs | 5 ++ 6 files changed, 166 insertions(+) create mode 100644 70_Poetry/csharp/Context.cs create mode 100644 70_Poetry/csharp/IOExtensions.cs create mode 100644 70_Poetry/csharp/Phrase.cs create mode 100644 70_Poetry/csharp/Poem.cs create mode 100644 70_Poetry/csharp/Program.cs diff --git a/70_Poetry/csharp/Context.cs b/70_Poetry/csharp/Context.cs new file mode 100644 index 00000000..f9aecc06 --- /dev/null +++ b/70_Poetry/csharp/Context.cs @@ -0,0 +1,11 @@ +namespace Poetry; + +internal class Context +{ + public int U { get; set; } + public int I { get; set; } + public int J { get; set; } + public int K { get; set; } + public bool SkipComma { get; set; } + public bool UseGroup2 { get; set; } +} diff --git a/70_Poetry/csharp/IOExtensions.cs b/70_Poetry/csharp/IOExtensions.cs new file mode 100644 index 00000000..a2089264 --- /dev/null +++ b/70_Poetry/csharp/IOExtensions.cs @@ -0,0 +1,8 @@ +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 new file mode 100644 index 00000000..1df6a774 --- /dev/null +++ b/70_Poetry/csharp/Phrase.cs @@ -0,0 +1,83 @@ +namespace Poetry; + +internal class Phrase +{ + private static Phrase[][] _phrases = new Phrase[][] + { + new Phrase[] + { + new("midnight dreary"), + new("fiery eyes"), + new("bird or fiend"), + new("thing of evil"), + new("prophet") + }, + new Phrase[] + { + new("beguiling me", ctx => ctx.U = 2), + new("thrilled me"), + new("still sitting....", ctx => ctx.SkipComma = true), + new("never flitting", ctx => ctx.U = 2), + new("burned") + }, + new Phrase[] + { + new("and my soul"), + new("darkness there"), + new("shall be lifted"), + new("quoth the raven"), + new(ctx => ctx.U != 0, "sign of parting") + }, + new Phrase[] + { + new("nothing more"), + new("yet again"), + new("slowly creeping"), + new("...evermore"), + new("nevermore") + } + }; + + private readonly Predicate _condition; + private readonly string _text; + private readonly Action _update; + + private Phrase(Predicate condition, string text) + : this(condition, text, _ => { }) + { + } + + private Phrase(string text, Action update) + : this(_ => true, text, update) + { + } + + private Phrase(string text) + : this(_ => true, text, _ => { }) + { + } + + private Phrase(Predicate condition, string text, Action update) + { + _condition = condition; + _text = text; + _update = update; + } + + public static Phrase GetPhrase(Context context) + { + var group = context.UseGroup2 ? _phrases[1] : _phrases[context.J]; + context.UseGroup2 = false; + return group[context.I % 5]; + } + + public void Write(IReadWrite io, Context context) + { + if (_condition.Invoke(context)) + { + io.Write(_text); + } + + _update.Invoke(context); + } +} \ No newline at end of file diff --git a/70_Poetry/csharp/Poem.cs b/70_Poetry/csharp/Poem.cs new file mode 100644 index 00000000..ed08d30c --- /dev/null +++ b/70_Poetry/csharp/Poem.cs @@ -0,0 +1,56 @@ +namespace Poetry; + +internal class Poem +{ + internal static void Compose(IReadWrite io, IRandom random) + { + var context = new Context(); + + while (true) + { + io.WritePhrase(context); + + if (!context.SkipComma && context.U != 0 && random.NextFloat() <= 0.19) + { + io.Write(","); + context.U = 2; + } + + if (random.NextFloat() <= 0.65) + { + io.Write(" "); + context.U += 1; + } + else + { + io.WriteLine(); + context.U = 0; + } + + while (true) + { + context.I = random.Next(1, 6); + context.J += 1; + context.K += 1; + + if (context.U == 0 && context.J % 2 == 0) + { + io.Write(" "); + } + + if (context.J < 4) { break; } + + context.J = 0; + io.WriteLine(); + + if (context.K > 20) + { + io.WriteLine(); + context.U = context.K = 0; + context.UseGroup2 = true; + break; + } + } + } + } +} \ No newline at end of file diff --git a/70_Poetry/csharp/Poetry.csproj b/70_Poetry/csharp/Poetry.csproj index d3fe4757..6d06ca76 100644 --- a/70_Poetry/csharp/Poetry.csproj +++ b/70_Poetry/csharp/Poetry.csproj @@ -6,4 +6,7 @@ enable enable + + + diff --git a/70_Poetry/csharp/Program.cs b/70_Poetry/csharp/Program.cs new file mode 100644 index 00000000..8920b37e --- /dev/null +++ b/70_Poetry/csharp/Program.cs @@ -0,0 +1,5 @@ +global using Games.Common.IO; +global using Games.Common.Randomness; +global using Poetry; + +Poem.Compose(new ConsoleIO(), new RandomNumberGenerator()); \ No newline at end of file From 617053b1f4ae7d05204b03e94fbb3cde89d957e5 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Mon, 3 Oct 2022 21:22:44 +1100 Subject: [PATCH 2/6] Debug poem generation --- 70_Poetry/csharp/Context.cs | 4 ++-- 70_Poetry/csharp/Phrase.cs | 7 +++++-- 70_Poetry/csharp/Poem.cs | 7 ++++--- 70_Poetry/csharp/Program.cs | 2 +- 70_Poetry/poetry.bas | 15 ++++++++++++--- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/70_Poetry/csharp/Context.cs b/70_Poetry/csharp/Context.cs index f9aecc06..0036b52a 100644 --- a/70_Poetry/csharp/Context.cs +++ b/70_Poetry/csharp/Context.cs @@ -2,10 +2,10 @@ namespace Poetry; internal class Context { - public int U { get; set; } public int I { get; set; } - public int J { 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; } } diff --git a/70_Poetry/csharp/Phrase.cs b/70_Poetry/csharp/Phrase.cs index 1df6a774..3f00abae 100644 --- a/70_Poetry/csharp/Phrase.cs +++ b/70_Poetry/csharp/Phrase.cs @@ -66,11 +66,14 @@ internal class Phrase public static Phrase GetPhrase(Context context) { - var group = context.UseGroup2 ? _phrases[1] : _phrases[context.J]; + var group = GetGroup(context.UseGroup2 ? 2 : context.J); context.UseGroup2 = false; - return group[context.I % 5]; + return group[Math.Max(context.I - 1, 0)]; } + 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 ed08d30c..75f24532 100644 --- a/70_Poetry/csharp/Poem.cs +++ b/70_Poetry/csharp/Poem.cs @@ -10,13 +10,14 @@ internal class Poem { io.WritePhrase(context); - if (!context.SkipComma && context.U != 0 && random.NextFloat() <= 0.19) + if (!context.SkipComma && random.NextFloat() <= 0.19F && context.U != 0) { io.Write(","); context.U = 2; } + context.SkipComma = false; - if (random.NextFloat() <= 0.65) + if (random.NextFloat() <= 0.65F) { io.Write(" "); context.U += 1; @@ -38,7 +39,7 @@ internal class Poem io.Write(" "); } - if (context.J < 4) { break; } + if (context.J < 5) { break; } context.J = 0; io.WriteLine(); diff --git a/70_Poetry/csharp/Program.cs b/70_Poetry/csharp/Program.cs index 8920b37e..3a17d72c 100644 --- a/70_Poetry/csharp/Program.cs +++ b/70_Poetry/csharp/Program.cs @@ -2,4 +2,4 @@ global using Games.Common.IO; global using Games.Common.Randomness; global using Poetry; -Poem.Compose(new ConsoleIO(), new RandomNumberGenerator()); \ No newline at end of file +Poem.Compose(new ConsoleIO(), new RandomNumberGenerator()); diff --git a/70_Poetry/poetry.bas b/70_Poetry/poetry.bas index 3661287d..7fa3a1c4 100644 --- a/70_Poetry/poetry.bas +++ b/70_Poetry/poetry.bas @@ -1,3 +1,8 @@ +5 Y=RND(-1) +6 REM FOR X = 1 TO 100 +7 REM PRINT RND(1);"," +8 REM NEXT X +9 REM GOTO 999 10 PRINT TAB(30);"POETRY" 20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY" 30 PRINT:PRINT:PRINT @@ -26,17 +31,21 @@ 133 PRINT "SLOWLY CREEPING";:GOTO 210 134 PRINT "...EVERMORE";:GOTO 210 135 PRINT "NEVERMORE"; -210 IF U=0 OR RND(1)>.19 THEN 212 +210 GOSUB 500 : IF U=0 OR X>.19 THEN 212 211 PRINT ",";:U=2 -212 IF RND(1)>.65 THEN 214 +212 GOSUB 500 : IF X>.65 THEN 214 213 PRINT " ";:U=U+1:GOTO 215 214 PRINT : U=0 -215 I=INT(INT(10*RND(1))/2)+1 +215 GOSUB 500 : I=INT(INT(10*X)/2)+1 220 J=J+1 : K=K+1 +225 REM PRINT "I=";I;"; J=";J;"; K=";K;"; U=";U 230 IF U>0 OR INT(J/2)<>J/2 THEN 240 235 PRINT " "; 240 ON J GOTO 90,110,120,130,250 250 J=0 : PRINT : IF K>20 THEN 270 260 GOTO 215 270 PRINT : U=0 : K=0 : GOTO 110 +500 X = RND(1) +505 REM PRINT "#";X;"#" +510 RETURN 999 END From d16d72965f4c983797b2092c361ca89bb8a97e0d Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Mon, 3 Oct 2022 21:29:34 +1100 Subject: [PATCH 3/6] Add title --- 70_Poetry/csharp/IOExtensions.cs | 1 - 70_Poetry/csharp/Poem.cs | 4 ++++ 70_Poetry/csharp/Poetry.csproj | 5 +++++ 70_Poetry/csharp/Resources/Resource.cs | 16 ++++++++++++++++ 70_Poetry/csharp/Resources/Title.txt | 5 +++++ 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 70_Poetry/csharp/Resources/Resource.cs create mode 100644 70_Poetry/csharp/Resources/Title.txt diff --git a/70_Poetry/csharp/IOExtensions.cs b/70_Poetry/csharp/IOExtensions.cs index a2089264..631c80f8 100644 --- a/70_Poetry/csharp/IOExtensions.cs +++ b/70_Poetry/csharp/IOExtensions.cs @@ -2,7 +2,6 @@ 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/Poem.cs b/70_Poetry/csharp/Poem.cs index 75f24532..64038963 100644 --- a/70_Poetry/csharp/Poem.cs +++ b/70_Poetry/csharp/Poem.cs @@ -1,9 +1,13 @@ +using static Poetry.Resources.Resource; + namespace Poetry; internal class Poem { internal static void Compose(IReadWrite io, IRandom random) { + io.Write(Streams.Title); + var context = new Context(); while (true) diff --git a/70_Poetry/csharp/Poetry.csproj b/70_Poetry/csharp/Poetry.csproj index 6d06ca76..3870320c 100644 --- a/70_Poetry/csharp/Poetry.csproj +++ b/70_Poetry/csharp/Poetry.csproj @@ -6,6 +6,11 @@ enable enable + + + + + diff --git a/70_Poetry/csharp/Resources/Resource.cs b/70_Poetry/csharp/Resources/Resource.cs new file mode 100644 index 00000000..b789f035 --- /dev/null +++ b/70_Poetry/csharp/Resources/Resource.cs @@ -0,0 +1,16 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace Poetry.Resources; + +internal static class Resource +{ + internal static class Streams + { + public static Stream Title => GetStream(); + } + + private static Stream GetStream([CallerMemberName] string? name = null) => + Assembly.GetExecutingAssembly().GetManifestResourceStream($"{typeof(Resource).Namespace}.{name}.txt") + ?? throw new Exception($"Could not find embedded resource stream '{name}'."); +} \ No newline at end of file diff --git a/70_Poetry/csharp/Resources/Title.txt b/70_Poetry/csharp/Resources/Title.txt new file mode 100644 index 00000000..86161340 --- /dev/null +++ b/70_Poetry/csharp/Resources/Title.txt @@ -0,0 +1,5 @@ + Poetry + Creative Computing Morristown, New Jersey + + + From 0c9d3580f52cc0ce77c950ff9092b4e71be71f4a Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Mon, 3 Oct 2022 22:00:42 +1100 Subject: [PATCH 4/6] 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; } } } } From d8dd694ea49acb7d3743a1af0b9b20ca6da58c20 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Mon, 3 Oct 2022 22:19:51 +1100 Subject: [PATCH 5/6] Rationalise visibility of context values --- 70_Poetry/csharp/Context.cs | 55 ++++++++++++++++++++++--------------- 70_Poetry/csharp/Phrase.cs | 17 ++++-------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/70_Poetry/csharp/Context.cs b/70_Poetry/csharp/Context.cs index 9d340c8e..950c7c53 100644 --- a/70_Poetry/csharp/Context.cs +++ b/70_Poetry/csharp/Context.cs @@ -4,6 +4,11 @@ internal class Context { private readonly IReadWrite _io; private readonly IRandom _random; + private int _phraseNumber; + private int _groupNumber; + private bool _skipComma; + private int _lineCount; + private bool _useGroup2; public Context(IReadWrite io, IRandom random) { @@ -11,14 +16,20 @@ internal class Context _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 int PhraseNumber => Math.Max(_phraseNumber - 1, 0); + + public int GroupNumber + { + get + { + var value = _useGroup2 ? 2 : _groupNumber; + _useGroup2 = false; + return Math.Max(value - 1, 0); + } + } + + public int PhraseCount { get; set; } + public bool GroupNumberIsValid => _groupNumber < 5; public void WritePhrase() { @@ -27,12 +38,12 @@ internal class Context public void MaybeWriteComma() { - if (!SkipComma && _random.NextFloat() <= 0.19F && U != 0) + if (!_skipComma && _random.NextFloat() <= 0.19F && PhraseCount != 0) { _io.Write(","); - U = 2; + PhraseCount = 2; } - SkipComma = false; + _skipComma = false; } public void WriteSpaceOrNewLine() @@ -40,25 +51,25 @@ internal class Context if (_random.NextFloat() <= 0.65F) { _io.Write(" "); - U += 1; + PhraseCount += 1; } else { _io.WriteLine(); - U = 0; + PhraseCount = 0; } } public void Update(IRandom random) { - I = random.Next(1, 6); - J += 1; - K += 1; + _phraseNumber = random.Next(1, 6); + _groupNumber += 1; + _lineCount += 1; } public void MaybeIndent() { - if (U == 0 && J % 2 == 0) + if (PhraseCount == 0 && _groupNumber % 2 == 0) { _io.Write(" "); } @@ -66,22 +77,22 @@ internal class Context public void ResetGroup(IReadWrite io) { - J = 0; + _groupNumber = 0; io.WriteLine(); } public bool MaybeCompleteStanza() { - if (K > 20) + if (_lineCount > 20) { _io.WriteLine(); - U = K = 0; - UseGroup2 = true; + PhraseCount = _lineCount = 0; + _useGroup2 = true; return true; } return false; } - public void SkipNextComma() => SkipComma = true; + public void SkipNextComma() => _skipComma = true; } diff --git a/70_Poetry/csharp/Phrase.cs b/70_Poetry/csharp/Phrase.cs index 919e8a71..c1ac0866 100644 --- a/70_Poetry/csharp/Phrase.cs +++ b/70_Poetry/csharp/Phrase.cs @@ -2,7 +2,7 @@ namespace Poetry; internal class Phrase { - private static Phrase[][] _phrases = new Phrase[][] + private readonly static Phrase[][] _phrases = new Phrase[][] { new Phrase[] { @@ -14,10 +14,10 @@ internal class Phrase }, new Phrase[] { - new("beguiling me", ctx => ctx.U = 2), + new("beguiling me", ctx => ctx.PhraseCount = 2), new("thrilled me"), new("still sitting....", ctx => ctx.SkipNextComma()), - new("never flitting", ctx => ctx.U = 2), + new("never flitting", ctx => ctx.PhraseCount = 2), new("burned") }, new Phrase[] @@ -26,7 +26,7 @@ internal class Phrase new("darkness there"), new("shall be lifted"), new("quoth the raven"), - new(ctx => ctx.U != 0, "sign of parting") + new(ctx => ctx.PhraseCount != 0, "sign of parting") }, new Phrase[] { @@ -64,14 +64,7 @@ internal class Phrase _update = update; } - public static Phrase GetPhrase(Context context) - { - var group = GetGroup(context.UseGroup2 ? 2 : context.J); - context.UseGroup2 = false; - return group[Math.Max(context.I - 1, 0)]; - } - - private static Phrase[] GetGroup(int groupNumber) => _phrases[Math.Max(groupNumber - 1, 0)]; + public static Phrase GetPhrase(Context context) => _phrases[context.GroupNumber][context.PhraseNumber]; public void Write(IReadWrite io, Context context) { From 257ba1ab1799d56936f4187824703fb0a414ace4 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Wed, 5 Oct 2022 07:45:50 +1100 Subject: [PATCH 6/6] Capitalise first char of line --- 70_Poetry/csharp/Context.cs | 17 ++++++++++++++--- 70_Poetry/csharp/Phrase.cs | 2 +- 70_Poetry/csharp/Poem.cs | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/70_Poetry/csharp/Context.cs b/70_Poetry/csharp/Context.cs index 950c7c53..a9297915 100644 --- a/70_Poetry/csharp/Context.cs +++ b/70_Poetry/csharp/Context.cs @@ -9,6 +9,7 @@ internal class Context private bool _skipComma; private int _lineCount; private bool _useGroup2; + private bool _atStartOfLine = true; public Context(IReadWrite io, IRandom random) { @@ -34,6 +35,7 @@ internal class Context public void WritePhrase() { Phrase.GetPhrase(this).Write(_io, this); + _atStartOfLine = false; } public void MaybeWriteComma() @@ -55,7 +57,7 @@ internal class Context } else { - _io.WriteLine(); + EndLine(); PhraseCount = 0; } } @@ -75,10 +77,10 @@ internal class Context } } - public void ResetGroup(IReadWrite io) + public void ResetGroup() { _groupNumber = 0; - io.WriteLine(); + EndLine(); } public bool MaybeCompleteStanza() @@ -94,5 +96,14 @@ internal class Context return false; } + internal string MaybeCapitalise(string text) => + _atStartOfLine ? (char.ToUpper(text[0]) + text[1..]) : text; + public void SkipNextComma() => _skipComma = true; + + public void EndLine() + { + _io.WriteLine(); + _atStartOfLine = true; + } } diff --git a/70_Poetry/csharp/Phrase.cs b/70_Poetry/csharp/Phrase.cs index c1ac0866..f70de30b 100644 --- a/70_Poetry/csharp/Phrase.cs +++ b/70_Poetry/csharp/Phrase.cs @@ -70,7 +70,7 @@ internal class Phrase { if (_condition.Invoke(context)) { - io.Write(_text); + io.Write(context.MaybeCapitalise(_text)); } _update.Invoke(context); diff --git a/70_Poetry/csharp/Poem.cs b/70_Poetry/csharp/Poem.cs index 03f3af68..fa3d5045 100644 --- a/70_Poetry/csharp/Poem.cs +++ b/70_Poetry/csharp/Poem.cs @@ -23,7 +23,7 @@ internal class Poem if (context.GroupNumberIsValid) { break; } - context.ResetGroup(io); + context.ResetGroup(); if (context.MaybeCompleteStanza()) { break; } }