mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-07 10:50:53 -08:00
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:
@@ -23,7 +23,7 @@ namespace Hammurabi
|
|||||||
/// <returns>
|
/// <returns>
|
||||||
/// The updated game state.
|
/// The updated game state.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public static GameState TryUntilSuccess(
|
public static GameState UpdateGameState(
|
||||||
GameState state,
|
GameState state,
|
||||||
Action prompt,
|
Action prompt,
|
||||||
Func<GameState, int, (GameState newState, ActionResult result)> rule)
|
Func<GameState, int, (GameState newState, ActionResult result)> rule)
|
||||||
@@ -35,29 +35,28 @@ namespace Hammurabi
|
|||||||
if (!Int32.TryParse(Console.ReadLine(), out var amount))
|
if (!Int32.TryParse(Console.ReadLine(), out var amount))
|
||||||
{
|
{
|
||||||
View.ShowInvalidNumber();
|
View.ShowInvalidNumber();
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
var (newState, result) = rule(state, amount);
|
|
||||||
|
|
||||||
switch (result)
|
var (newState, result) = rule(state, amount);
|
||||||
{
|
|
||||||
case ActionResult.InsufficientLand:
|
switch (result)
|
||||||
View.ShowInsufficientLand(state);
|
{
|
||||||
break;
|
case ActionResult.InsufficientLand:
|
||||||
case ActionResult.InsufficientPopulation:
|
View.ShowInsufficientLand(state);
|
||||||
View.ShowInsufficientPopulation(state);
|
break;
|
||||||
break;
|
case ActionResult.InsufficientPopulation:
|
||||||
case ActionResult.InsufficientStores:
|
View.ShowInsufficientPopulation(state);
|
||||||
View.ShowInsufficientStores(state);
|
break;
|
||||||
break;
|
case ActionResult.InsufficientStores:
|
||||||
case ActionResult.Offense:
|
View.ShowInsufficientStores(state);
|
||||||
// Not sure why we have to blow up the game here...
|
break;
|
||||||
// Maybe this made sense in the 70's.
|
case ActionResult.Offense:
|
||||||
throw new GreatOffence();
|
// Not sure why we have to blow up the game here...
|
||||||
default:
|
// Maybe this made sense in the 70's.
|
||||||
return newState;
|
throw new GreatOffence();
|
||||||
}
|
default:
|
||||||
|
return newState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,6 @@
|
|||||||
/// Gets a flag indicating whether the player was impeached for
|
/// Gets a flag indicating whether the player was impeached for
|
||||||
/// starving too many people.
|
/// starving too many people.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool WasImpeached { get; init; }
|
public bool WasPlayerImpeached { get; init; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,6 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a flag indicating whether the player has been impeached.
|
/// Gets a flag indicating whether the player has been impeached.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsImpeached { get; init; }
|
public bool IsPlayerImpeached { get; init; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Hammurabi
|
|||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var random = new Random ((int) (DateTime.UtcNow.Ticks / 10000)) ;
|
var random = new Random() ;
|
||||||
var state = Rules.BeginGame();
|
var state = Rules.BeginGame();
|
||||||
var history = ImmutableList<GameState>.Empty;
|
var history = ImmutableList<GameState>.Empty;
|
||||||
|
|
||||||
@@ -17,21 +17,24 @@ namespace Hammurabi
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (state.Year <= GameLength && !state.IsImpeached)
|
while (!state.IsPlayerImpeached)
|
||||||
{
|
{
|
||||||
state = Rules.BeginTurn(state, random);
|
state = Rules.BeginTurn(state, random);
|
||||||
View.ShowCitySummary(state);
|
View.ShowCitySummary(state);
|
||||||
|
|
||||||
|
if (state.Year > GameLength)
|
||||||
|
break;
|
||||||
|
|
||||||
View.ShowLandPrice(state);
|
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 ?
|
state = newState.Acres != state.Acres ?
|
||||||
newState : Controller.TryUntilSuccess(state, View.PromptSellLand, Rules.SellLand);
|
newState : Controller.UpdateGameState(state, View.PromptSellLand, Rules.SellLand);
|
||||||
|
|
||||||
View.ShowSeparator();
|
View.ShowSeparator();
|
||||||
state = Controller.TryUntilSuccess(state, View.PromptFeedPeople, Rules.FeedPeople);
|
state = Controller.UpdateGameState(state, View.PromptFeedPeople, Rules.FeedPeople);
|
||||||
|
|
||||||
View.ShowSeparator();
|
View.ShowSeparator();
|
||||||
state = Controller.TryUntilSuccess(state, View.PromptPlantCrops, Rules.PlantCrops);
|
state = Controller.UpdateGameState(state, View.PromptPlantCrops, Rules.PlantCrops);
|
||||||
|
|
||||||
state = Rules.EndTurn(state, random);
|
state = Rules.EndTurn(state, random);
|
||||||
history = history.Add(state);
|
history = history.Add(state);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Hammurabi
|
|||||||
public static GameState BeginGame() =>
|
public static GameState BeginGame() =>
|
||||||
new GameState
|
new GameState
|
||||||
{
|
{
|
||||||
Year = 1,
|
Year = 0,
|
||||||
Population = 95,
|
Population = 95,
|
||||||
PopulationIncrease = 5,
|
PopulationIncrease = 5,
|
||||||
Starvation = 0,
|
Starvation = 0,
|
||||||
@@ -22,7 +22,7 @@ namespace Hammurabi
|
|||||||
Productivity = 3,
|
Productivity = 3,
|
||||||
Spoilage = 200,
|
Spoilage = 200,
|
||||||
IsPlagueYear = false,
|
IsPlagueYear = false,
|
||||||
IsImpeached = false
|
IsPlayerImpeached = false
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -31,6 +31,7 @@ namespace Hammurabi
|
|||||||
public static GameState BeginTurn(GameState state, Random random) =>
|
public static GameState BeginTurn(GameState state, Random random) =>
|
||||||
state with
|
state with
|
||||||
{
|
{
|
||||||
|
Year = state.Year + 1,
|
||||||
Population = (state.Population + state.PopulationIncrease - state.Starvation) / (state.IsPlagueYear ? 2 : 1),
|
Population = (state.Population + state.PopulationIncrease - state.Starvation) / (state.IsPlagueYear ? 2 : 1),
|
||||||
LandPrice = random.Next(10) + 17,
|
LandPrice = random.Next(10) + 17,
|
||||||
Stores = state.Stores + (state.AcresPlanted * state.Productivity) - state.Spoilage,
|
Stores = state.Stores + (state.AcresPlanted * state.Productivity) - state.Spoilage,
|
||||||
@@ -139,23 +140,22 @@ namespace Hammurabi
|
|||||||
_ => 0
|
_ => 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 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 starvation = peopleFed < state.Population ? state.Population - peopleFed : 0;
|
||||||
var impeached = starvation > state.Population * 0.45;
|
var impeached = starvation > state.Population * 0.45;
|
||||||
|
|
||||||
return state with
|
return state with
|
||||||
{
|
{
|
||||||
Year = state.Year + 1,
|
|
||||||
Productivity = productivity,
|
Productivity = productivity,
|
||||||
Spoilage = spoilage,
|
Spoilage = spoilage,
|
||||||
PopulationIncrease = populationIncrease,
|
PopulationIncrease = populationIncrease,
|
||||||
Starvation = starvation,
|
Starvation = starvation,
|
||||||
IsPlagueYear = plagueYear,
|
IsPlagueYear = plagueYear,
|
||||||
IsImpeached = impeached
|
IsPlayerImpeached = impeached
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,7 +176,7 @@ namespace Hammurabi
|
|||||||
|
|
||||||
var acresPerPerson = finalState.Acres / finalState.Population;
|
var acresPerPerson = finalState.Acres / finalState.Population;
|
||||||
|
|
||||||
var rating = finalState.IsImpeached ?
|
var rating = finalState.IsPlayerImpeached ?
|
||||||
PerformanceRating.Disgraceful :
|
PerformanceRating.Disgraceful :
|
||||||
(averageStarvationRate, acresPerPerson) switch
|
(averageStarvationRate, acresPerPerson) switch
|
||||||
{
|
{
|
||||||
@@ -200,7 +200,7 @@ namespace Hammurabi
|
|||||||
TotalStarvation = totalStarvation,
|
TotalStarvation = totalStarvation,
|
||||||
AverageStarvationRate = averageStarvationRate,
|
AverageStarvationRate = averageStarvationRate,
|
||||||
Assassins = assassins,
|
Assassins = assassins,
|
||||||
WasImpeached = finalState.IsImpeached
|
WasPlayerImpeached = finalState.IsPlayerImpeached
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ namespace Hammurabi
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void ShowGameResult(GameResult result)
|
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($"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");
|
Console.WriteLine("POPULATION STARVED PER YEAR ON THE AVERAGE, I.E. A TOTAL OF");
|
||||||
@@ -124,7 +124,7 @@ namespace Hammurabi
|
|||||||
switch (result.Rating)
|
switch (result.Rating)
|
||||||
{
|
{
|
||||||
case PerformanceRating.Disgraceful:
|
case PerformanceRating.Disgraceful:
|
||||||
if (result.WasImpeached)
|
if (result.WasPlayerImpeached)
|
||||||
Console.WriteLine($"YOU STARVED {result.FinalStarvation} PEOPLE IN ONE YEAR!!!");
|
Console.WriteLine($"YOU STARVED {result.FinalStarvation} PEOPLE IN ONE YEAR!!!");
|
||||||
|
|
||||||
Console.WriteLine("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY");
|
Console.WriteLine("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY");
|
||||||
@@ -163,7 +163,7 @@ namespace Hammurabi
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void PromptBuyLand()
|
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>
|
/// <summary>
|
||||||
@@ -171,7 +171,7 @@ namespace Hammurabi
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void PromptSellLand()
|
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>
|
/// <summary>
|
||||||
@@ -179,7 +179,7 @@ namespace Hammurabi
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void PromptFeedPeople()
|
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>
|
/// <summary>
|
||||||
@@ -187,7 +187,7 @@ namespace Hammurabi
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void PromptPlantCrops()
|
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? ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user