Minor quality improvements and fixes

* Improved names and fixed inconsistent spacing
* Update year at the start of the turn instead of the end
* Input functions work more like the BASIC versions
* Display city summary before end game results (as in the original version)
This commit is contained in:
Peter
2021-04-26 19:06:01 -04:00
parent a843634261
commit 81e36428b2
6 changed files with 46 additions and 44 deletions

View File

@@ -23,7 +23,7 @@ namespace Hammurabi
/// <returns>
/// The updated game state.
/// </returns>
public static GameState TryUntilSuccess(
public static GameState UpdateGameState(
GameState state,
Action prompt,
Func<GameState, int, (GameState newState, ActionResult result)> rule)
@@ -35,29 +35,28 @@ namespace Hammurabi
if (!Int32.TryParse(Console.ReadLine(), out var amount))
{
View.ShowInvalidNumber();
continue;
}
else
{
var (newState, result) = rule(state, amount);
switch (result)
{
case ActionResult.InsufficientLand:
View.ShowInsufficientLand(state);
break;
case ActionResult.InsufficientPopulation:
View.ShowInsufficientPopulation(state);
break;
case ActionResult.InsufficientStores:
View.ShowInsufficientStores(state);
break;
case ActionResult.Offense:
// Not sure why we have to blow up the game here...
// Maybe this made sense in the 70's.
throw new GreatOffence();
default:
return newState;
}
var (newState, result) = rule(state, amount);
switch (result)
{
case ActionResult.InsufficientLand:
View.ShowInsufficientLand(state);
break;
case ActionResult.InsufficientPopulation:
View.ShowInsufficientPopulation(state);
break;
case ActionResult.InsufficientStores:
View.ShowInsufficientStores(state);
break;
case ActionResult.Offense:
// Not sure why we have to blow up the game here...
// Maybe this made sense in the 70's.
throw new GreatOffence();
default:
return newState;
}
}
}

View File

@@ -40,6 +40,6 @@
/// Gets a flag indicating whether the player was impeached for
/// starving too many people.
/// </summary>
public bool WasImpeached { get; init; }
public bool WasPlayerImpeached { get; init; }
}
}

View File

@@ -68,6 +68,6 @@
/// <summary>
/// Gets a flag indicating whether the player has been impeached.
/// </summary>
public bool IsImpeached { get; init; }
public bool IsPlayerImpeached { get; init; }
}
}

View File

@@ -9,7 +9,7 @@ namespace Hammurabi
public static void Main(string[] args)
{
var random = new Random ((int) (DateTime.UtcNow.Ticks / 10000)) ;
var random = new Random() ;
var state = Rules.BeginGame();
var history = ImmutableList<GameState>.Empty;
@@ -17,21 +17,24 @@ namespace Hammurabi
try
{
while (state.Year <= GameLength && !state.IsImpeached)
while (!state.IsPlayerImpeached)
{
state = Rules.BeginTurn(state, random);
View.ShowCitySummary(state);
if (state.Year > GameLength)
break;
View.ShowLandPrice(state);
var newState = Controller.TryUntilSuccess(state, View.PromptBuyLand, Rules.BuyLand);
var newState = Controller.UpdateGameState(state, View.PromptBuyLand, Rules.BuyLand);
state = newState.Acres != state.Acres ?
newState : Controller.TryUntilSuccess(state, View.PromptSellLand, Rules.SellLand);
newState : Controller.UpdateGameState(state, View.PromptSellLand, Rules.SellLand);
View.ShowSeparator();
state = Controller.TryUntilSuccess(state, View.PromptFeedPeople, Rules.FeedPeople);
state = Controller.UpdateGameState(state, View.PromptFeedPeople, Rules.FeedPeople);
View.ShowSeparator();
state = Controller.TryUntilSuccess(state, View.PromptPlantCrops, Rules.PlantCrops);
state = Controller.UpdateGameState(state, View.PromptPlantCrops, Rules.PlantCrops);
state = Rules.EndTurn(state, random);
history = history.Add(state);

View File

@@ -12,7 +12,7 @@ namespace Hammurabi
public static GameState BeginGame() =>
new GameState
{
Year = 1,
Year = 0,
Population = 95,
PopulationIncrease = 5,
Starvation = 0,
@@ -22,7 +22,7 @@ namespace Hammurabi
Productivity = 3,
Spoilage = 200,
IsPlagueYear = false,
IsImpeached = false
IsPlayerImpeached = false
};
/// <summary>
@@ -31,6 +31,7 @@ namespace Hammurabi
public static GameState BeginTurn(GameState state, Random random) =>
state with
{
Year = state.Year + 1,
Population = (state.Population + state.PopulationIncrease - state.Starvation) / (state.IsPlagueYear ? 2 : 1),
LandPrice = random.Next(10) + 17,
Stores = state.Stores + (state.AcresPlanted * state.Productivity) - state.Spoilage,
@@ -139,23 +140,22 @@ namespace Hammurabi
_ => 0
};
var populationIncrease= (int)((double) random.Next(1, 6) * (20 * state.Acres + state.Stores + harvest - spoilage) / state.Population / 100 + 1);
var populationIncrease= (int)((double)random.Next(1, 6) * (20 * state.Acres + state.Stores + harvest - spoilage) / state.Population / 100 + 1);
var plagueYear = random.Next(20) < 3;
var peopleFed = state.FoodDistributed / 20;
var peopleFed = state.FoodDistributed / 20;
var starvation = peopleFed < state.Population ? state.Population - peopleFed : 0;
var impeached = starvation > state.Population * 0.45;
return state with
{
Year = state.Year + 1,
Productivity = productivity,
Spoilage = spoilage,
PopulationIncrease = populationIncrease,
Starvation = starvation,
IsPlagueYear = plagueYear,
IsImpeached = impeached
IsPlayerImpeached = impeached
};
}
@@ -176,7 +176,7 @@ namespace Hammurabi
var acresPerPerson = finalState.Acres / finalState.Population;
var rating = finalState.IsImpeached ?
var rating = finalState.IsPlayerImpeached ?
PerformanceRating.Disgraceful :
(averageStarvationRate, acresPerPerson) switch
{
@@ -200,7 +200,7 @@ namespace Hammurabi
TotalStarvation = totalStarvation,
AverageStarvationRate = averageStarvationRate,
Assassins = assassins,
WasImpeached = finalState.IsImpeached
WasPlayerImpeached = finalState.IsPlayerImpeached
};
}
}

View File

@@ -110,7 +110,7 @@ namespace Hammurabi
/// </summary>
public static void ShowGameResult(GameResult result)
{
if (!result.WasImpeached)
if (!result.WasPlayerImpeached)
{
Console.WriteLine($"IN YOUR 10-YEAR TERM OF OFFICE, {result.AverageStarvationRate} PERCENT OF THE");
Console.WriteLine("POPULATION STARVED PER YEAR ON THE AVERAGE, I.E. A TOTAL OF");
@@ -124,7 +124,7 @@ namespace Hammurabi
switch (result.Rating)
{
case PerformanceRating.Disgraceful:
if (result.WasImpeached)
if (result.WasPlayerImpeached)
Console.WriteLine($"YOU STARVED {result.FinalStarvation} PEOPLE IN ONE YEAR!!!");
Console.WriteLine("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY");
@@ -163,7 +163,7 @@ namespace Hammurabi
/// </summary>
public static void PromptBuyLand()
{
Console.WriteLine ("HOW MANY ACRES DO YOU WISH TO BUY");
Console.Write("HOW MANY ACRES DO YOU WISH TO BUY? ");
}
/// <summary>
@@ -171,7 +171,7 @@ namespace Hammurabi
/// </summary>
public static void PromptSellLand()
{
Console.WriteLine("HOW MANY ACRES DO YOU WISH TO SELL");
Console.Write("HOW MANY ACRES DO YOU WISH TO SELL? ");
}
/// <summary>
@@ -179,7 +179,7 @@ namespace Hammurabi
/// </summary>
public static void PromptFeedPeople()
{
Console.WriteLine("HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE");
Console.Write("HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? ");
}
/// <summary>
@@ -187,7 +187,7 @@ namespace Hammurabi
/// </summary>
public static void PromptPlantCrops()
{
Console.WriteLine("HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED");
Console.Write("HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? ");
}
}
}