Add reign initizalization

This commit is contained in:
Andrew Cooper
2022-10-22 15:46:44 +11:00
parent 9b471073b0
commit da0f2d13ab
14 changed files with 140 additions and 6 deletions

34
53_King/csharp/Country.cs Normal file
View File

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

View File

@@ -17,12 +17,30 @@ internal class Game
{ {
_io.Write(Resource.Title); _io.Write(Resource.Title);
var response = _io.ReadString(Resource.Instructions_Prompt).ToUpper(); if (SetUpReign() is Reign reign)
if (!response.StartsWith('N'))
{ {
_io.Write(Resource.Instructions_Text(TermOfOffice)); _io.Write(reign);
} }
_io.WriteLine(); _io.WriteLine();
_io.WriteLine();
}
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);
} }
} }

View File

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

23
53_King/csharp/Reign.cs Normal file
View File

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

View File

@@ -7,8 +7,16 @@ internal static class Resource
{ {
public static Stream Title => GetStream(); public static Stream Title => GetStream();
public static string Instructions_Prompt => GetString(); public static string InstructionsPrompt => GetString();
public static string Instructions_Text(int years) => string.Format(GetString(), years); 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 internal static class Formats
{ {

View File

@@ -0,0 +1 @@
How many countrymen

View File

@@ -0,0 +1,2 @@
Come on, you started with 1000 sq. miles of farm land
and 10,000 sq. miles of forest land

View File

@@ -0,0 +1 @@
How many square miles of land

View File

@@ -0,0 +1 @@
How much did you have in the treasury

View File

@@ -0,0 +1 @@
How many workers

View File

@@ -0,0 +1 @@
Come on, your term in office is only {0} years.

View File

@@ -0,0 +1 @@
How many years had you been in office when interrupted