diff --git a/32_Diamond/csharp/Diamond.csproj b/32_Diamond/csharp/Diamond.csproj index d3fe4757..3870320c 100644 --- a/32_Diamond/csharp/Diamond.csproj +++ b/32_Diamond/csharp/Diamond.csproj @@ -6,4 +6,12 @@ enable enable + + + + + + + + diff --git a/32_Diamond/csharp/Pattern.cs b/32_Diamond/csharp/Pattern.cs new file mode 100644 index 00000000..ce6d493a --- /dev/null +++ b/32_Diamond/csharp/Pattern.cs @@ -0,0 +1,57 @@ +using System.Text; +using static Diamond.Resources.Resource; + +namespace Diamond; + +internal class Pattern +{ + private readonly IReadWrite _io; + + public Pattern(IReadWrite io) + { + _io = io; + io.Write(Streams.Introduction); + } + + public void Draw() + { + var diamondSize = _io.ReadNumber(Prompts.TypeNumber); + _io.WriteLine(); + + var diamondCount = (int)(60 / diamondSize); + + var diamondLines = new List(GetDiamondLines(diamondSize)).AsReadOnly(); + + for (int patternRow = 0; patternRow < diamondCount; patternRow++) + { + for (int diamondRow = 0; diamondRow < diamondLines.Count; diamondRow++) + { + var line = new StringBuilder(); + for (int patternColumn = 0; patternColumn < diamondCount; patternColumn++) + { + line.PadToLength((int)(patternColumn * diamondSize)).Append(diamondLines[diamondRow]); + } + _io.WriteLine(line); + } + } + } + + public static IEnumerable GetDiamondLines(float size) + { + for (var i = 1; i <= size; i += 2) + { + yield return GetLine(i); + } + + for (var i = size - 2; i >= 1; i -= 2) + { + yield return GetLine(i); + } + + string GetLine(float i) => + string.Concat( + new string(' ', (int)(size - i) / 2), + new string('C', Math.Min((int)i, 2)), + new string('!', Math.Max(0, (int)i - 2))); + } +} diff --git a/32_Diamond/csharp/Program.cs b/32_Diamond/csharp/Program.cs new file mode 100644 index 00000000..46dd67c5 --- /dev/null +++ b/32_Diamond/csharp/Program.cs @@ -0,0 +1,4 @@ +global using Games.Common.IO; +using Diamond; + +new Pattern(new ConsoleIO()).Draw(); diff --git a/32_Diamond/csharp/Resources/Introduction.txt b/32_Diamond/csharp/Resources/Introduction.txt new file mode 100644 index 00000000..e983fc9f --- /dev/null +++ b/32_Diamond/csharp/Resources/Introduction.txt @@ -0,0 +1,5 @@ + Diamond + Creative Computing Morristown, New Jersey + + + diff --git a/32_Diamond/csharp/Resources/Resource.cs b/32_Diamond/csharp/Resources/Resource.cs new file mode 100644 index 00000000..d7051f5c --- /dev/null +++ b/32_Diamond/csharp/Resources/Resource.cs @@ -0,0 +1,29 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace Diamond.Resources; + +internal static class Resource +{ + internal static class Streams + { + public static Stream Introduction => GetStream(); + } + + internal static class Prompts + { + public static string TypeNumber => GetString(); + } + + private static string GetString([CallerMemberName] string? name = null) + { + using var stream = GetStream(name); + using var reader = new StreamReader(stream); + return reader.ReadToEnd(); + } + + + private static Stream GetStream([CallerMemberName] string? name = null) => + Assembly.GetExecutingAssembly().GetManifestResourceStream($"{typeof(Resource).Namespace}.{name}.txt") + ?? throw new Exception($"Could not find embedded resource stream '{name}'."); +} \ No newline at end of file diff --git a/32_Diamond/csharp/Resources/Rules.txt b/32_Diamond/csharp/Resources/Rules.txt new file mode 100644 index 00000000..2fd9177f --- /dev/null +++ b/32_Diamond/csharp/Resources/Rules.txt @@ -0,0 +1,22 @@ +Chomp is for 1 or more players (humans only). + +Here's how a board looks (this one is 5 by 7): + + 1 2 3 4 5 6 7 8 9 + 1 P * * * * * * + 2 * * * * * * * + 3 * * * * * * * + 4 * * * * * * * + 5 * * * * * * * + + +The board is a big cookie - R rows high and C columns +wide. You input R and C at the start. In the upper left +corner of the cookie is a poison square (P). The one who +chomps the poison square loses. To take a chomp, type the +row and column of one of the squares on the cookie. +All of the squares below and to the right of that square +(including that square, too) disappear -- Chomp!! +No fair chomping on squares that have already been chomped, +or that are outside the original dimensions of the cookie. + diff --git a/32_Diamond/csharp/Resources/TypeNumber.txt b/32_Diamond/csharp/Resources/TypeNumber.txt new file mode 100644 index 00000000..d74d21fd --- /dev/null +++ b/32_Diamond/csharp/Resources/TypeNumber.txt @@ -0,0 +1,2 @@ +For a pretty diamond pattern, +type in an odd number between 5 and 21 \ No newline at end of file diff --git a/32_Diamond/csharp/StringBuilderExtensions.cs b/32_Diamond/csharp/StringBuilderExtensions.cs new file mode 100644 index 00000000..3ada4146 --- /dev/null +++ b/32_Diamond/csharp/StringBuilderExtensions.cs @@ -0,0 +1,9 @@ +using System.Text; + +namespace Diamond; + +internal static class StringBuilderExtensions +{ + internal static StringBuilder PadToLength(this StringBuilder builder, int length) => + builder.Append(' ', length - builder.Length); +} \ No newline at end of file