Merge pull request #782 from drewjcooper/csharp-32-diamond

C# 32 Diamond
This commit is contained in:
Jeff Atwood
2022-08-25 14:39:53 -07:00
committed by GitHub
8 changed files with 136 additions and 0 deletions

View File

@@ -6,4 +6,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Resources/*.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\00_Common\dotnet\Games.Common\Games.Common.csproj" />
</ItemGroup>
</Project>

View File

@@ -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<string>(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<string> 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)));
}
}

View File

@@ -0,0 +1,4 @@
global using Games.Common.IO;
using Diamond;
new Pattern(new ConsoleIO()).Draw();

View File

@@ -0,0 +1,5 @@
Diamond
Creative Computing Morristown, New Jersey

View File

@@ -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}'.");
}

View File

@@ -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.

View File

@@ -0,0 +1,2 @@
For a pretty diamond pattern,
type in an odd number between 5 and 21

View File

@@ -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);
}