Move output to context

This commit is contained in:
Andrew Cooper
2022-10-03 22:00:42 +11:00
parent d16d72965f
commit 0c9d3580f5
4 changed files with 86 additions and 47 deletions

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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))

View File

@@ -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; }
}
}
}