diff --git a/58_Love/csharp/Input.cs b/58_Love/csharp/Input.cs deleted file mode 100644 index 030e3ead..00000000 --- a/58_Love/csharp/Input.cs +++ /dev/null @@ -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); - } -} diff --git a/58_Love/csharp/Love.csproj b/58_Love/csharp/Love.csproj index fc2efa30..523087a3 100644 --- a/58_Love/csharp/Love.csproj +++ b/58_Love/csharp/Love.csproj @@ -2,10 +2,15 @@ Exe - net5.0 + net6.0 - + + + + + + diff --git a/58_Love/csharp/LovePattern.cs b/58_Love/csharp/LovePattern.cs index 4c24ed84..37e6703c 100644 --- a/58_Love/csharp/LovePattern.cs +++ b/58_Love/csharp/LovePattern.cs @@ -1,57 +1,55 @@ 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, + 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 + }; + private readonly StringBuilder _pattern = new(); + + public LovePattern(string message) { - 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 - }; + Fill(new SourceCharacters(_lineLength, message)); + } - public int LineLength => 60; + private void Fill(SourceCharacters source) + { + var lineLength = 0; - internal void Write(SourceCharacters source, Stream destination) + foreach (var segmentLength in _segmentLengths) { - using var writer = new StreamWriter(destination); - - WritePadding(writer); - - var lineLength = 0; - - foreach (var segmentLength in _segmentLengths) + foreach (var character in source.GetCharacters(segmentLength)) { - foreach (var character in source.GetCharacters(segmentLength)) - { - writer.Write(character); - } - lineLength += segmentLength; - if (lineLength >= LineLength) - { - writer.WriteLine(); - lineLength = 0; - } + _pattern.Append(character); } - - WritePadding(writer); - } - - private void WritePadding(StreamWriter writer) - { - for (int i = 0; i < 10; i++) + lineLength += segmentLength; + if (lineLength >= _lineLength) { - writer.WriteLine(); + _pattern.AppendLine(); + lineLength = 0; } } } + + public override string ToString() => + new StringBuilder() + .AppendLines(10) + .Append(_pattern) + .AppendLines(10) + .ToString(); } diff --git a/58_Love/csharp/Program.cs b/58_Love/csharp/Program.cs index a9784466..575bbec2 100644 --- a/58_Love/csharp/Program.cs +++ b/58_Love/csharp/Program.cs @@ -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)); diff --git a/58_Love/csharp/Strings/Intro.txt b/58_Love/csharp/Resources/Intro.txt similarity index 99% rename from 58_Love/csharp/Strings/Intro.txt rename to 58_Love/csharp/Resources/Intro.txt index cc10189e..bcf1afe2 100644 --- a/58_Love/csharp/Strings/Intro.txt +++ b/58_Love/csharp/Resources/Intro.txt @@ -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' + diff --git a/58_Love/csharp/Resources/Resource.cs b/58_Love/csharp/Resources/Resource.cs new file mode 100644 index 00000000..fcbe6add --- /dev/null +++ b/58_Love/csharp/Resources/Resource.cs @@ -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"); +} \ No newline at end of file diff --git a/58_Love/csharp/SourceCharacters.cs b/58_Love/csharp/SourceCharacters.cs index 64716f9c..f6015754 100644 --- a/58_Love/csharp/SourceCharacters.cs +++ b/58_Love/csharp/SourceCharacters.cs @@ -1,38 +1,37 @@ using System; -namespace Love +namespace Love; + +internal class SourceCharacters { - internal class SourceCharacters + private readonly int _lineLength; + private readonly char[][] _chars; + private int _currentRow; + private int _currentIndex; + + public SourceCharacters(int lineLength, string message) { - private readonly int _lineLength; - private readonly char[][] _chars; - private int _currentRow; - private int _currentIndex; + _lineLength = lineLength; + _chars = new[] { new char[lineLength], new char[lineLength] }; - public SourceCharacters(int lineLength, string message) + for (int i = 0; i < lineLength; i++) { - _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; + _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/StringBuilderExtensions.cs b/58_Love/csharp/StringBuilderExtensions.cs new file mode 100644 index 00000000..8a985ed9 --- /dev/null +++ b/58_Love/csharp/StringBuilderExtensions.cs @@ -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; + } +}