mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-02-04 11:07:59 -08:00
Use common library in Love
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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'
|
||||
|
||||
16
58_Love/csharp/Resources/Resource.cs
Normal file
16
58_Love/csharp/Resources/Resource.cs
Normal 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");
|
||||
}
|
||||
@@ -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<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;
|
||||
_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;
|
||||
}
|
||||
}
|
||||
|
||||
16
58_Love/csharp/StringBuilderExtensions.cs
Normal file
16
58_Love/csharp/StringBuilderExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user