mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-25 04:15:45 -08:00
Move output to context
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user