mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-05 17:48:52 -08:00
Move output to context
This commit is contained in:
@@ -2,10 +2,86 @@ namespace Poetry;
|
|||||||
|
|
||||||
internal class Context
|
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 I { get; set; }
|
||||||
public int J { get; set; }
|
public int J { get; set; }
|
||||||
public int K { get; set; }
|
public int K { get; set; }
|
||||||
public int U { get; set; }
|
public int U { get; set; }
|
||||||
public bool SkipComma { get; set; }
|
public bool SkipComma { get; set; }
|
||||||
public bool UseGroup2 { 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("beguiling me", ctx => ctx.U = 2),
|
||||||
new("thrilled me"),
|
new("thrilled me"),
|
||||||
new("still sitting....", ctx => ctx.SkipComma = true),
|
new("still sitting....", ctx => ctx.SkipNextComma()),
|
||||||
new("never flitting", ctx => ctx.U = 2),
|
new("never flitting", ctx => ctx.U = 2),
|
||||||
new("burned")
|
new("burned")
|
||||||
},
|
},
|
||||||
@@ -73,7 +73,6 @@ internal class Phrase
|
|||||||
|
|
||||||
private static Phrase[] GetGroup(int groupNumber) => _phrases[Math.Max(groupNumber - 1, 0)];
|
private static Phrase[] GetGroup(int groupNumber) => _phrases[Math.Max(groupNumber - 1, 0)];
|
||||||
|
|
||||||
|
|
||||||
public void Write(IReadWrite io, Context context)
|
public void Write(IReadWrite io, Context context)
|
||||||
{
|
{
|
||||||
if (_condition.Invoke(context))
|
if (_condition.Invoke(context))
|
||||||
|
|||||||
@@ -8,53 +8,24 @@ internal class Poem
|
|||||||
{
|
{
|
||||||
io.Write(Streams.Title);
|
io.Write(Streams.Title);
|
||||||
|
|
||||||
var context = new Context();
|
var context = new Context(io, random);
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
io.WritePhrase(context);
|
context.WritePhrase();
|
||||||
|
context.MaybeWriteComma();
|
||||||
if (!context.SkipComma && random.NextFloat() <= 0.19F && context.U != 0)
|
context.WriteSpaceOrNewLine();
|
||||||
{
|
|
||||||
io.Write(",");
|
|
||||||
context.U = 2;
|
|
||||||
}
|
|
||||||
context.SkipComma = false;
|
|
||||||
|
|
||||||
if (random.NextFloat() <= 0.65F)
|
|
||||||
{
|
|
||||||
io.Write(" ");
|
|
||||||
context.U += 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
io.WriteLine();
|
|
||||||
context.U = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
context.I = random.Next(1, 6);
|
context.Update(random);
|
||||||
context.J += 1;
|
context.MaybeIndent();
|
||||||
context.K += 1;
|
|
||||||
|
|
||||||
if (context.U == 0 && context.J % 2 == 0)
|
if (context.GroupNumberIsValid) { break; }
|
||||||
{
|
|
||||||
io.Write(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (context.J < 5) { break; }
|
context.ResetGroup(io);
|
||||||
|
|
||||||
context.J = 0;
|
if (context.MaybeCompleteStanza()) { break; }
|
||||||
io.WriteLine();
|
|
||||||
|
|
||||||
if (context.K > 20)
|
|
||||||
{
|
|
||||||
io.WriteLine();
|
|
||||||
context.U = context.K = 0;
|
|
||||||
context.UseGroup2 = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user