From 81e36428b2a7151dda28f657ca73b17d852b2c2d Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 26 Apr 2021 19:06:01 -0400 Subject: [PATCH] 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) --- 43 Hammurabi/csharp/src/Controller.cs | 43 +++++++++++++-------------- 43 Hammurabi/csharp/src/GameResult.cs | 2 +- 43 Hammurabi/csharp/src/GameState.cs | 2 +- 43 Hammurabi/csharp/src/Program.cs | 15 ++++++---- 43 Hammurabi/csharp/src/Rules.cs | 16 +++++----- 43 Hammurabi/csharp/src/View.cs | 12 ++++---- 6 files changed, 46 insertions(+), 44 deletions(-) diff --git a/43 Hammurabi/csharp/src/Controller.cs b/43 Hammurabi/csharp/src/Controller.cs index 91d1b64f..afdb83d2 100644 --- a/43 Hammurabi/csharp/src/Controller.cs +++ b/43 Hammurabi/csharp/src/Controller.cs @@ -23,7 +23,7 @@ namespace Hammurabi /// /// The updated game state. /// - public static GameState TryUntilSuccess( + public static GameState UpdateGameState( GameState state, Action prompt, Func 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; } } } diff --git a/43 Hammurabi/csharp/src/GameResult.cs b/43 Hammurabi/csharp/src/GameResult.cs index 23dfbe28..9c143375 100644 --- a/43 Hammurabi/csharp/src/GameResult.cs +++ b/43 Hammurabi/csharp/src/GameResult.cs @@ -40,6 +40,6 @@ /// Gets a flag indicating whether the player was impeached for /// starving too many people. /// - public bool WasImpeached { get; init; } + public bool WasPlayerImpeached { get; init; } } } diff --git a/43 Hammurabi/csharp/src/GameState.cs b/43 Hammurabi/csharp/src/GameState.cs index 3e2d1eb2..41e6fe11 100644 --- a/43 Hammurabi/csharp/src/GameState.cs +++ b/43 Hammurabi/csharp/src/GameState.cs @@ -68,6 +68,6 @@ /// /// Gets a flag indicating whether the player has been impeached. /// - public bool IsImpeached { get; init; } + public bool IsPlayerImpeached { get; init; } } } diff --git a/43 Hammurabi/csharp/src/Program.cs b/43 Hammurabi/csharp/src/Program.cs index 45d5b101..8f158839 100644 --- a/43 Hammurabi/csharp/src/Program.cs +++ b/43 Hammurabi/csharp/src/Program.cs @@ -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.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); diff --git a/43 Hammurabi/csharp/src/Rules.cs b/43 Hammurabi/csharp/src/Rules.cs index 56a59d96..36fc9c50 100644 --- a/43 Hammurabi/csharp/src/Rules.cs +++ b/43 Hammurabi/csharp/src/Rules.cs @@ -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 }; /// @@ -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 }; } } diff --git a/43 Hammurabi/csharp/src/View.cs b/43 Hammurabi/csharp/src/View.cs index be10af32..fcfb4cd5 100644 --- a/43 Hammurabi/csharp/src/View.cs +++ b/43 Hammurabi/csharp/src/View.cs @@ -110,7 +110,7 @@ namespace Hammurabi /// 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 /// public static void PromptBuyLand() { - Console.WriteLine ("HOW MANY ACRES DO YOU WISH TO BUY"); + Console.Write("HOW MANY ACRES DO YOU WISH TO BUY? "); } /// @@ -171,7 +171,7 @@ namespace Hammurabi /// public static void PromptSellLand() { - Console.WriteLine("HOW MANY ACRES DO YOU WISH TO SELL"); + Console.Write("HOW MANY ACRES DO YOU WISH TO SELL? "); } /// @@ -179,7 +179,7 @@ namespace Hammurabi /// 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? "); } /// @@ -187,7 +187,7 @@ namespace Hammurabi /// 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? "); } } }