mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Add reign initizalization
This commit is contained in:
34
53_King/csharp/Country.cs
Normal file
34
53_King/csharp/Country.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
43
53_King/csharp/IOExtensions.cs
Normal file
43
53_King/csharp/IOExtensions.cs
Normal 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
23
53_King/csharp/Reign.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
1
53_King/csharp/Resources/SavedCountrymenPrompt.txt
Normal file
1
53_King/csharp/Resources/SavedCountrymenPrompt.txt
Normal file
@@ -0,0 +1 @@
|
||||
How many countrymen
|
||||
2
53_King/csharp/Resources/SavedLandError.txt
Normal file
2
53_King/csharp/Resources/SavedLandError.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Come on, you started with 1000 sq. miles of farm land
|
||||
and 10,000 sq. miles of forest land
|
||||
1
53_King/csharp/Resources/SavedLandPrompt.txt
Normal file
1
53_King/csharp/Resources/SavedLandPrompt.txt
Normal file
@@ -0,0 +1 @@
|
||||
How many square miles of land
|
||||
1
53_King/csharp/Resources/SavedTreasuryPrompt.txt
Normal file
1
53_King/csharp/Resources/SavedTreasuryPrompt.txt
Normal file
@@ -0,0 +1 @@
|
||||
How much did you have in the treasury
|
||||
1
53_King/csharp/Resources/SavedWorkersPrompt.txt
Normal file
1
53_King/csharp/Resources/SavedWorkersPrompt.txt
Normal file
@@ -0,0 +1 @@
|
||||
How many workers
|
||||
1
53_King/csharp/Resources/SavedYearsError.txt
Normal file
1
53_King/csharp/Resources/SavedYearsError.txt
Normal file
@@ -0,0 +1 @@
|
||||
Come on, your term in office is only {0} years.
|
||||
1
53_King/csharp/Resources/SavedYearsPrompt.txt
Normal file
1
53_King/csharp/Resources/SavedYearsPrompt.txt
Normal file
@@ -0,0 +1 @@
|
||||
How many years had you been in office when interrupted
|
||||
Reference in New Issue
Block a user