Merge pull request #317 from drewjcooper/csharp-58-love

Implement 58 Love in C#
This commit is contained in:
Jeff Atwood
2021-10-12 11:24:10 -07:00
committed by GitHub
7 changed files with 207 additions and 0 deletions

34
58 Love/csharp/Love.sln Normal file
View File

@@ -0,0 +1,34 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Love", "Love\Love.csproj", "{FC74E025-A50D-4E19-9337-87F2E4A9F83E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Debug|x64.ActiveCfg = Debug|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Debug|x64.Build.0 = Debug|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Debug|x86.ActiveCfg = Debug|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Debug|x86.Build.0 = Debug|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Release|Any CPU.Build.0 = Release|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Release|x64.ActiveCfg = Release|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Release|x64.Build.0 = Release|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Release|x86.ActiveCfg = Release|Any CPU
{FC74E025-A50D-4E19-9337-87F2E4A9F83E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,26 @@
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

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Strings\Intro.txt" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,57 @@
using System.IO;
namespace Love
{
internal class LovePattern
{
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,
4, 4, 6, 21, 8, 11, 6, 4, 4, 6, 21, 7, 11, 7, 4, 4, 6, 21, 6, 11, 8, 4,
4, 6, 19, 1, 1, 5, 11, 9, 4, 4, 6, 19, 1, 1, 5, 10, 10, 4, 4, 6, 18, 2,
1, 6, 8, 11, 4, 4, 6, 17, 3, 1, 7, 5, 13, 4, 4, 6, 15, 5, 2, 23, 5, 1,
29, 5, 17, 8, 1, 29, 9, 9, 12, 1, 13, 5, 40, 1, 1, 13, 5, 40, 1, 4, 6,
13, 3, 10, 6, 12, 5, 1, 5, 6, 11, 3, 11, 6, 14, 3, 1, 5, 6, 11, 3, 11,
6, 15, 2, 1, 6, 6, 9, 3, 12, 6, 16, 1, 1, 6, 6, 9, 3, 12, 6, 7, 1, 10,
7, 6, 7, 3, 13, 6, 6, 2, 10, 7, 6, 7, 3, 13, 14, 10, 8, 6, 5, 3, 14, 6,
6, 2, 10, 8, 6, 5, 3, 14, 6, 7, 1, 10, 9, 6, 3, 3, 15, 6, 16, 1, 1, 9,
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
};
public int LineLength => 60;
internal void Write(SourceCharacters source, Stream destination)
{
using var writer = new StreamWriter(destination);
WritePadding(writer);
var lineLength = 0;
foreach (var segmentLength in _segmentLengths)
{
foreach (var character in source.GetCharacters(segmentLength))
{
writer.Write(character);
}
lineLength += segmentLength;
if (lineLength >= LineLength)
{
writer.WriteLine();
lineLength = 0;
}
}
WritePadding(writer);
}
private void WritePadding(StreamWriter writer)
{
for (int i = 0; i < 10; i++)
{
writer.WriteLine();
}
}
}
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Reflection;
namespace Love
{
internal class Program
{
static void Main(string[] args)
{
DisplayIntro();
var message = Input.ReadLine("Your message, please");
var pattern = new LovePattern();
var source = new SourceCharacters(pattern.LineLength, message);
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);
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
namespace Love
{
internal class SourceCharacters
{
private readonly int _lineLength;
private readonly char[][] _chars;
private int _currentRow;
private int _currentIndex;
public SourceCharacters(int lineLength, string message)
{
_lineLength = lineLength;
_chars = new[] { new char[lineLength], new char[lineLength] };
for (int i = 0; i < lineLength; i++)
{
_chars[0][i] = message[i % message.Length];
_chars[1][i] = ' ';
}
}
public ReadOnlySpan<char> GetCharacters(int count)
{
var span = new ReadOnlySpan<char>(_chars[_currentRow], _currentIndex, count);
_currentRow = 1 - _currentRow;
_currentIndex += count;
if (_currentIndex >= _lineLength)
{
_currentIndex = _currentRow = 0;
}
return span;
}
}
}

View File

@@ -0,0 +1,10 @@
Love
Creative Computing Morristown, New Jersey
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'