diff --git a/53_King/csharp/Country.cs b/53_King/csharp/Country.cs new file mode 100644 index 00000000..1e94e821 --- /dev/null +++ b/53_King/csharp/Country.cs @@ -0,0 +1,34 @@ +namespace King; + +internal class Country +{ + private readonly IRandom _random; + private float _rallods; + private float _countrymen; + private float _foreigners; + private float _land; + private float _plantingCost; + private float _landValue; + + public Country(IRandom random) + : this( + random, + (int)(60000 + random.NextFloat(1000) - random.NextFloat(1000)), + (int)(500 + random.NextFloat(10) - random.NextFloat(10)), + 0, + 2000) + { + } + + public Country(IRandom random, float rallods, float countrymen, float foreigners, float land) + { + _random = random; + _rallods = rallods; + _countrymen = countrymen; + _foreigners = foreigners; + _land = land; + + _plantingCost = random.Next(10, 15); + _landValue = random.Next(95, 105); + } +} diff --git a/53_King/csharp/Game.cs b/53_King/csharp/Game.cs index d357bdf3..c8a44a8c 100644 --- a/53_King/csharp/Game.cs +++ b/53_King/csharp/Game.cs @@ -17,12 +17,30 @@ internal class Game { _io.Write(Resource.Title); - var response = _io.ReadString(Resource.Instructions_Prompt).ToUpper(); - if (!response.StartsWith('N')) + if (SetUpReign() is Reign reign) { - _io.Write(Resource.Instructions_Text(TermOfOffice)); + _io.Write(reign); } _io.WriteLine(); + _io.WriteLine(); } -} \ No newline at end of file + + private Reign? SetUpReign() + { + var response = _io.ReadString(Resource.InstructionsPrompt).ToUpper(); + + if (response.Equals("Again", StringComparison.InvariantCultureIgnoreCase)) + { + return _io.TryReadGameData(_random, out var reign) ? reign : null; + } + + if (!response.StartsWith("N", StringComparison.InvariantCultureIgnoreCase)) + { + _io.Write(Resource.InstructionsText(TermOfOffice)); + } + + _io.WriteLine(); + return new Reign(_io, _random); + } +} diff --git a/53_King/csharp/IOExtensions.cs b/53_King/csharp/IOExtensions.cs new file mode 100644 index 00000000..183621ca --- /dev/null +++ b/53_King/csharp/IOExtensions.cs @@ -0,0 +1,43 @@ +using System.Diagnostics.CodeAnalysis; +using static King.Resources.Resource; + +namespace King; + +internal static class IOExtensions +{ + internal static bool TryReadGameData(this IReadWrite io, IRandom random, [NotNullWhen(true)] out Reign? reign) + { + if (io.TryReadValue(SavedYearsPrompt, v => v < Reign.MaxTerm, SavedYearsError(Reign.MaxTerm), out var years) && + io.TryReadValue(SavedTreasuryPrompt, out var rallods) && + io.TryReadValue(SavedCountrymenPrompt, out var countrymen) && + io.TryReadValue(SavedWorkersPrompt, out var workers) && + io.TryReadValue(SavedLandPrompt, v => v is > 1000 and <= 2000, SavedLandError, out var land)) + { + reign = new Reign(io, new Country(random, rallods, countrymen, workers, land), years + 1); + return true; + } + + reign = default; + return false; + } + + private static bool TryReadValue(this IReadWrite io, string prompt, out float value) + => io.TryReadValue(prompt, _ => true, "", out value); + + private static bool TryReadValue( + this IReadWrite io, + string prompt, + Predicate isValid, + string error, + out float value) + { + while (true) + { + value = io.ReadNumber(prompt); + if (value < 0) { return false; } + if (isValid(value)) { return true; } + + io.Write(error); + } + } +} \ No newline at end of file diff --git a/53_King/csharp/Reign.cs b/53_King/csharp/Reign.cs new file mode 100644 index 00000000..4e6c45c5 --- /dev/null +++ b/53_King/csharp/Reign.cs @@ -0,0 +1,23 @@ +namespace King; + +internal class Reign +{ + public const int MaxTerm = 8; + + private readonly IReadWrite _io; + private readonly Country _country; + private readonly float _year; + + public Reign(IReadWrite io, IRandom random) + : this(io, new Country(random), 0) + { + + } + + public Reign(IReadWrite io, Country country, float year) + { + _io = io; + _country = country; + _year = year; + } +} diff --git a/53_King/csharp/Resources/Instructions_Prompt.txt b/53_King/csharp/Resources/InstructionsPrompt.txt similarity index 100% rename from 53_King/csharp/Resources/Instructions_Prompt.txt rename to 53_King/csharp/Resources/InstructionsPrompt.txt diff --git a/53_King/csharp/Resources/Instructions_Text.txt b/53_King/csharp/Resources/InstructionsText.txt similarity index 100% rename from 53_King/csharp/Resources/Instructions_Text.txt rename to 53_King/csharp/Resources/InstructionsText.txt diff --git a/53_King/csharp/Resources/Resource.cs b/53_King/csharp/Resources/Resource.cs index f8ddfd3f..6626a3dd 100644 --- a/53_King/csharp/Resources/Resource.cs +++ b/53_King/csharp/Resources/Resource.cs @@ -7,9 +7,17 @@ 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); + public static string InstructionsPrompt => GetString(); + public static string InstructionsText(int years) => string.Format(GetString(), years); + public static string SavedYearsPrompt => GetString(); + public static string SavedYearsError(int years) => string.Format(GetString(), years); + public static string SavedTreasuryPrompt => GetString(); + public static string SavedCountrymenPrompt => GetString(); + public static string SavedWorkersPrompt => GetString(); + public static string SavedLandPrompt => GetString(); + public static string SavedLandError => GetString(); + internal static class Formats { public static string Player => GetString(); diff --git a/53_King/csharp/Resources/SavedCountrymenPrompt.txt b/53_King/csharp/Resources/SavedCountrymenPrompt.txt new file mode 100644 index 00000000..120718d2 --- /dev/null +++ b/53_King/csharp/Resources/SavedCountrymenPrompt.txt @@ -0,0 +1 @@ +How many countrymen \ No newline at end of file diff --git a/53_King/csharp/Resources/SavedLandError.txt b/53_King/csharp/Resources/SavedLandError.txt new file mode 100644 index 00000000..9ac66dad --- /dev/null +++ b/53_King/csharp/Resources/SavedLandError.txt @@ -0,0 +1,2 @@ + Come on, you started with 1000 sq. miles of farm land + and 10,000 sq. miles of forest land diff --git a/53_King/csharp/Resources/SavedLandPrompt.txt b/53_King/csharp/Resources/SavedLandPrompt.txt new file mode 100644 index 00000000..e8afdf72 --- /dev/null +++ b/53_King/csharp/Resources/SavedLandPrompt.txt @@ -0,0 +1 @@ +How many square miles of land \ No newline at end of file diff --git a/53_King/csharp/Resources/SavedTreasuryPrompt.txt b/53_King/csharp/Resources/SavedTreasuryPrompt.txt new file mode 100644 index 00000000..f4a50f58 --- /dev/null +++ b/53_King/csharp/Resources/SavedTreasuryPrompt.txt @@ -0,0 +1 @@ +How much did you have in the treasury \ No newline at end of file diff --git a/53_King/csharp/Resources/SavedWorkersPrompt.txt b/53_King/csharp/Resources/SavedWorkersPrompt.txt new file mode 100644 index 00000000..b88a9c10 --- /dev/null +++ b/53_King/csharp/Resources/SavedWorkersPrompt.txt @@ -0,0 +1 @@ +How many workers \ No newline at end of file diff --git a/53_King/csharp/Resources/SavedYearsError.txt b/53_King/csharp/Resources/SavedYearsError.txt new file mode 100644 index 00000000..1c8c3c99 --- /dev/null +++ b/53_King/csharp/Resources/SavedYearsError.txt @@ -0,0 +1 @@ + Come on, your term in office is only {0} years. diff --git a/53_King/csharp/Resources/SavedYearsPrompt.txt b/53_King/csharp/Resources/SavedYearsPrompt.txt new file mode 100644 index 00000000..afbda229 --- /dev/null +++ b/53_King/csharp/Resources/SavedYearsPrompt.txt @@ -0,0 +1 @@ +How many years had you been in office when interrupted \ No newline at end of file