mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
CSHARP-53 Create program sturcture
This commit is contained in:
28
53_King/csharp/Game.cs
Normal file
28
53_King/csharp/Game.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace King;
|
||||
|
||||
internal class Game
|
||||
{
|
||||
const int TermOfOffice = 8;
|
||||
|
||||
private readonly IReadWrite _io;
|
||||
private readonly IRandom _random;
|
||||
|
||||
public Game(IReadWrite io, IRandom random)
|
||||
{
|
||||
_io = io;
|
||||
_random = random;
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
_io.Write(Resource.Title);
|
||||
|
||||
var response = _io.ReadString(Resource.Instructions_Prompt).ToUpper();
|
||||
if (!response.StartsWith('N'))
|
||||
{
|
||||
_io.Write(Resource.Instructions_Text(TermOfOffice));
|
||||
}
|
||||
|
||||
_io.WriteLine();
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
6
53_King/csharp/Program.cs
Normal file
6
53_King/csharp/Program.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
global using Games.Common.IO;
|
||||
global using Games.Common.Randomness;
|
||||
global using King.Resources;
|
||||
using King;
|
||||
|
||||
new Game(new ConsoleIO(), new RandomNumberGenerator()).Play();
|
||||
1
53_King/csharp/Resources/Instructions_Prompt.txt
Normal file
1
53_King/csharp/Resources/Instructions_Prompt.txt
Normal file
@@ -0,0 +1 @@
|
||||
Do you want instructions
|
||||
17
53_King/csharp/Resources/Instructions_Text.txt
Normal file
17
53_King/csharp/Resources/Instructions_Text.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
|
||||
|
||||
Congratulations! You've just been elected Premier of Setats
|
||||
Detinu, a small communist island 30 by 70 miles long. Your
|
||||
job is to decide upon the country's budget and distribute
|
||||
money to your countrymen from the communal treasury.
|
||||
The money system is rallods, and each person needs 100
|
||||
rallods per year to survive. Your country's income comes
|
||||
from farm produce and tourists visiting your magnificent
|
||||
forests, hunting, fishing, etc. Half your land if farm land
|
||||
which also has an excellent mineral content and may be sold
|
||||
to foreign industry (strip mining) who import and support
|
||||
their own workers. Crops cost between 10 and 15 rallods per
|
||||
square mile to plant.
|
||||
Your goal is to complete your {0} year term of office.
|
||||
Good luck!
|
||||
45
53_King/csharp/Resources/Resource.cs
Normal file
45
53_King/csharp/Resources/Resource.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace King.Resources;
|
||||
|
||||
internal static class Resource
|
||||
{
|
||||
public static Stream Title => GetStream();
|
||||
|
||||
public static string Instructions_Prompt => GetString();
|
||||
public static string Instructions_Text(int years) => string.Format(GetString(), years);
|
||||
|
||||
internal static class Formats
|
||||
{
|
||||
public static string Player => GetString();
|
||||
public static string YouLose => GetString();
|
||||
}
|
||||
|
||||
internal static class Prompts
|
||||
{
|
||||
public static string WantInstructions => GetString();
|
||||
public static string HowManyPlayers => GetString();
|
||||
public static string HowManyRows => GetString();
|
||||
public static string HowManyColumns => GetString();
|
||||
public static string TooManyColumns => GetString();
|
||||
}
|
||||
|
||||
internal static class Strings
|
||||
{
|
||||
public static string TooManyColumns => GetString();
|
||||
public static string TooManyRows => 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}'.");
|
||||
}
|
||||
5
53_King/csharp/Resources/Title.txt
Normal file
5
53_King/csharp/Resources/Title.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
King
|
||||
Creative Computing Morristown, New Jersey
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user