diff --git a/58 Love/csharp/Love.sln b/58 Love/csharp/Love.sln new file mode 100644 index 00000000..a89fd1d8 --- /dev/null +++ b/58 Love/csharp/Love.sln @@ -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 diff --git a/58 Love/csharp/Love/Input.cs b/58 Love/csharp/Love/Input.cs new file mode 100644 index 00000000..030e3ead --- /dev/null +++ b/58 Love/csharp/Love/Input.cs @@ -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); + } +} diff --git a/58 Love/csharp/Love/Love.csproj b/58 Love/csharp/Love/Love.csproj new file mode 100644 index 00000000..fc2efa30 --- /dev/null +++ b/58 Love/csharp/Love/Love.csproj @@ -0,0 +1,11 @@ + + + + Exe + net5.0 + + + + + + diff --git a/58 Love/csharp/Love/LovePattern.cs b/58 Love/csharp/Love/LovePattern.cs new file mode 100644 index 00000000..4c24ed84 --- /dev/null +++ b/58 Love/csharp/Love/LovePattern.cs @@ -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(); + } + } + } +} diff --git a/58 Love/csharp/Love/Program.cs b/58 Love/csharp/Love/Program.cs new file mode 100644 index 00000000..a9784466 --- /dev/null +++ b/58 Love/csharp/Love/Program.cs @@ -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); + } + } +} diff --git a/58 Love/csharp/Love/SourceCharacters.cs b/58 Love/csharp/Love/SourceCharacters.cs new file mode 100644 index 00000000..64716f9c --- /dev/null +++ b/58 Love/csharp/Love/SourceCharacters.cs @@ -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 GetCharacters(int count) + { + var span = new ReadOnlySpan(_chars[_currentRow], _currentIndex, count); + + _currentRow = 1 - _currentRow; + _currentIndex += count; + if (_currentIndex >= _lineLength) + { + _currentIndex = _currentRow = 0; + } + + return span; + } + } +} diff --git a/58 Love/csharp/Love/Strings/Intro.txt b/58 Love/csharp/Love/Strings/Intro.txt new file mode 100644 index 00000000..bcf1afe2 --- /dev/null +++ b/58 Love/csharp/Love/Strings/Intro.txt @@ -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' +