Use common library in Love

This commit is contained in:
Andrew Cooper
2022-03-18 07:33:28 +11:00
parent 1a8ea5aabd
commit d6ddc6bf9e
8 changed files with 116 additions and 127 deletions

View File

@@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
namespace Love
{
// Provides input methods which emulate the BASIC interpreter's keyboard input routines
internal static class Input
{
private static void Prompt(string text = "") => Console.Write($"{text}? ");
public static string ReadLine(string prompt)
{
Prompt(prompt);
var values = ReadStrings();
if (values.Length > 1)
{
Console.WriteLine("!Extra input ingored");
}
return values[0];
}
private static string[] ReadStrings() => Console.ReadLine().Split(',', StringSplitOptions.TrimEntries);
}
}

View File

@@ -2,10 +2,15 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Strings\Intro.txt" />
<EmbeddedResource Include="Resources\Intro.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\00_Common\dotnet\Games.Common\Games.Common.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,9 +1,11 @@
using System.IO;
using System.Text;
namespace Love
namespace Love;
internal class LovePattern
{
internal class LovePattern
{
private const int _lineLength = 60;
private readonly int[] _segmentLengths = new[] {
60, 1, 12, 26, 9, 12, 3, 8, 24, 17, 8, 4, 6, 23, 21, 6, 4, 6, 22, 12, 5,
6, 5, 4, 6, 21, 11, 8, 6, 4, 4, 6, 21, 10, 10, 5, 4, 4, 6, 21, 9, 11, 5,
@@ -18,40 +20,36 @@ namespace Love
6, 3, 3, 15, 6, 15, 2, 1, 10, 6, 1, 3, 16, 6, 14, 3, 1, 10, 10, 16, 6,
12, 5, 1, 11, 8, 13, 27, 1, 11, 8, 13, 27, 1, 60
};
private readonly StringBuilder _pattern = new();
public int LineLength => 60;
internal void Write(SourceCharacters source, Stream destination)
public LovePattern(string message)
{
using var writer = new StreamWriter(destination);
WritePadding(writer);
Fill(new SourceCharacters(_lineLength, message));
}
private void Fill(SourceCharacters source)
{
var lineLength = 0;
foreach (var segmentLength in _segmentLengths)
{
foreach (var character in source.GetCharacters(segmentLength))
{
writer.Write(character);
_pattern.Append(character);
}
lineLength += segmentLength;
if (lineLength >= LineLength)
if (lineLength >= _lineLength)
{
writer.WriteLine();
_pattern.AppendLine();
lineLength = 0;
}
}
WritePadding(writer);
}
private void WritePadding(StreamWriter writer)
{
for (int i = 0; i < 10; i++)
{
writer.WriteLine();
}
}
}
public override string ToString() =>
new StringBuilder()
.AppendLines(10)
.Append(_pattern)
.AppendLines(10)
.ToString();
}

View File

@@ -1,31 +1,11 @@
using System;
using System.Reflection;
using Games.Common.IO;
using Love;
using Love.Resources;
namespace Love
{
internal class Program
{
static void Main(string[] args)
{
DisplayIntro();
var io = new ConsoleIO();
var message = Input.ReadLine("Your message, please");
var pattern = new LovePattern();
io.Write(Resource.Streams.Intro);
var source = new SourceCharacters(pattern.LineLength, message);
var message = io.ReadString("Your message, please");
using var destination = Console.OpenStandardOutput();
pattern.Write(source, destination);
}
private static void DisplayIntro()
{
using var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("Love.Strings.Intro.txt");
using var stdout = Console.OpenStandardOutput();
stream.CopyTo(stdout);
}
}
}
io.Write(new LovePattern(message));

View File

@@ -7,3 +7,4 @@ A tribute to the great American artist, Robert Indiana.
His greatest work will be reproduced with a message of
your choice up to 60 characters. If you can't think of
a message, simply type the word 'LOVE'

View File

@@ -0,0 +1,16 @@
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Love.Resources;
internal static class Resource
{
internal static class Streams
{
public static Stream Intro => GetStream();
}
private static Stream GetStream([CallerMemberName] string name = null)
=> Assembly.GetExecutingAssembly().GetManifestResourceStream($"Love.Resources.{name}.txt");
}

View File

@@ -1,9 +1,9 @@
using System;
namespace Love
namespace Love;
internal class SourceCharacters
{
internal class SourceCharacters
{
private readonly int _lineLength;
private readonly char[][] _chars;
private int _currentRow;
@@ -34,5 +34,4 @@ namespace Love
return span;
}
}
}

View File

@@ -0,0 +1,16 @@
using System.Text;
namespace Love;
internal static class StringBuilderExtensions
{
internal static StringBuilder AppendLines(this StringBuilder builder, int count)
{
for (int i = 0; i < count; i++)
{
builder.AppendLine();
}
return builder;
}
}