Merge pull request #264 from pgruderman/main

Ported Hammurabi to C#
This commit is contained in:
Jeff Atwood
2021-04-03 21:50:14 -07:00
committed by GitHub
11 changed files with 730 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31129.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Game", "Game.csproj", "{20599300-7C6E-48A2-AB24-EC7CCF224A5C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{20599300-7C6E-48A2-AB24-EC7CCF224A5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20599300-7C6E-48A2-AB24-EC7CCF224A5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20599300-7C6E-48A2-AB24-EC7CCF224A5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20599300-7C6E-48A2-AB24-EC7CCF224A5C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {89DBE213-C0F0-4ABA-BB2D-5D9AAED41FF6}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,37 @@
namespace Hammurabi
{
/// <summary>
/// Enumerates the different possible outcomes of attempting the various
/// actions in the game.
/// </summary>
public enum ActionResult
{
/// <summary>
/// The action was a success.
/// </summary>
Success,
/// <summary>
/// The action could not be completed because the city does not have
/// enough bushels of grain.
/// </summary>
InsufficientStores,
/// <summary>
/// The action could not be completed because the city does not have
/// sufficient acreage.
/// </summary>
InsufficientLand,
/// <summary>
/// The action could not be completed because the city does not have
/// sufficient population.
/// </summary>
InsufficientPopulation,
/// <summary>
/// The requested action offended the city steward.
/// </summary>
Offense
}
}

View File

@@ -0,0 +1,65 @@
using System;
namespace Hammurabi
{
/// <summary>
/// Provides methods for reading input from the user.
/// </summary>
public static class Controller
{
/// <summary>
/// Continuously prompts the user to enter a number until he or she
/// enters a valid number and updates the game state.
/// </summary>
/// <param name="state">
/// The current game state.
/// </param>
/// <param name="prompt">
/// Action that will display the prompt to the user.
/// </param>
/// <param name="rule">
/// The rule to invoke once input is retrieved.
/// </param>
/// <returns>
/// The updated game state.
/// </returns>
public static GameState TryUntilSuccess(
GameState state,
Action prompt,
Func<GameState, int, (GameState newState, ActionResult result)> rule)
{
while (true)
{
prompt();
if (!Int32.TryParse(Console.ReadLine(), out var amount))
{
View.ShowInvalidNumber();
}
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;
}
}
}
}
}
}

View File

@@ -0,0 +1,45 @@
namespace Hammurabi
{
/// <summary>
/// Stores the final game result.
/// </summary>
public record GameResult
{
/// <summary>
/// Gets the player's performance rating.
/// </summary>
public PerformanceRating Rating { get; init; }
/// <summary>
/// Gets the number of acres in the city per person.
/// </summary>
public int AcresPerPerson { get; init; }
/// <summary>
/// Gets the number of people who starved the final year in office.
/// </summary>
public int FinalStarvation { get; init; }
/// <summary>
/// Gets the total number of people who starved.
/// </summary>
public int TotalStarvation { get; init; }
/// <summary>
/// Gets the average starvation rate per year (as a percentage
/// of population).
/// </summary>
public int AverageStarvationRate { get; init; }
/// <summary>
/// Gets the number of people who want to assassinate the player.
/// </summary>
public int Assassins { get; init; }
/// <summary>
/// Gets a flag indicating whether the player was impeached for
/// starving too many people.
/// </summary>
public bool WasImpeached { get; init; }
}
}

View File

@@ -0,0 +1,73 @@
namespace Hammurabi
{
/// <summary>
/// Stores the state of the game.
/// </summary>
public record GameState
{
/// <summary>
/// Gets the current game year.
/// </summary>
public int Year { get; init; }
/// <summary>
/// Gets the city's population.
/// </summary>
public int Population { get; init; }
/// <summary>
/// Gets the population increase this year.
/// </summary>
public int PopulationIncrease { get; init; }
/// <summary>
/// Gets the number of people who starved.
/// </summary>
public int Starvation { get; init; }
/// <summary>
/// Gets the city's size in acres.
/// </summary>
public int Acres { get; init; }
/// <summary>
/// Gets the price for an acre of land (in bushels).
/// </summary>
public int LandPrice { get; init; }
/// <summary>
/// Gets the number of bushels of grain in the city stores.
/// </summary>
public int Stores { get; init; }
/// <summary>
/// Gets the amount of food distributed to the people.
/// </summary>
public int FoodDistributed { get; init; }
/// <summary>
/// Gets the number of acres that were planted.
/// </summary>
public int AcresPlanted { get; init; }
/// <summary>
/// Gets the number of bushels produced per acre.
/// </summary>
public int Productivity { get; init; }
/// <summary>
/// Gets the amount of food lost to rats.
/// </summary>
public int Spoilage { get; init; }
/// <summary>
/// Gets a flag indicating whether the current year is a plague year.
/// </summary>
public bool IsPlagueYear { get; init; }
/// <summary>
/// Gets a flag indicating whether the player has been impeached.
/// </summary>
public bool IsImpeached { get; init; }
}
}

View File

@@ -0,0 +1,12 @@
using System;
namespace Hammurabi
{
/// <summary>
/// Indicates that the game cannot continue due to the player's extreme
/// incompetance and/or unserious attitude!
/// </summary>
public class GreatOffence : InvalidOperationException
{
}
}

View File

@@ -0,0 +1,14 @@
namespace Hammurabi
{
/// <summary>
/// Enumerates the different performance ratings that the player can
/// achieve.
/// </summary>
public enum PerformanceRating
{
Disgraceful,
Bad,
Ok,
Terrific
}
}

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Immutable;
namespace Hammurabi
{
public static class Program
{
public const int GameLength = 10;
public static void Main(string[] args)
{
var random = new Random ((int) (DateTime.UtcNow.Ticks / 10000)) ;
var state = Rules.BeginGame();
var history = ImmutableList<GameState>.Empty;
View.ShowBanner();
try
{
while (state.Year <= GameLength && !state.IsImpeached)
{
state = Rules.BeginTurn(state, random);
View.ShowCitySummary(state);
View.ShowLandPrice(state);
var newState = Controller.TryUntilSuccess(state, View.PromptBuyLand, Rules.BuyLand);
state = newState.Acres != state.Acres ?
newState : Controller.TryUntilSuccess(state, View.PromptSellLand, Rules.SellLand);
View.ShowSeparator();
state = Controller.TryUntilSuccess(state, View.PromptFeedPeople, Rules.FeedPeople);
View.ShowSeparator();
state = Controller.TryUntilSuccess(state, View.PromptPlantCrops, Rules.PlantCrops);
state = Rules.EndTurn(state, random);
history = history.Add(state);
}
var result = Rules.GetGameResult(history, random);
View.ShowGameResult(result);
}
catch (GreatOffence)
{
View.ShowGreatOffence();
}
View.ShowFarewell();
}
}
}

View File

@@ -0,0 +1,207 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Hammurabi
{
public static class Rules
{
/// <summary>
/// Creates the initial state for a new game.
/// </summary>
public static GameState BeginGame() =>
new GameState
{
Year = 1,
Population = 95,
PopulationIncrease = 5,
Starvation = 0,
Acres = 1000,
Stores = 0,
AcresPlanted = 1000,
Productivity = 3,
Spoilage = 200,
IsPlagueYear = false,
IsImpeached = false
};
/// <summary>
/// Updates the game state to start a new turn.
/// </summary>
public static GameState BeginTurn(GameState state, Random random) =>
state with
{
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,
AcresPlanted = 0,
FoodDistributed = 0
};
/// <summary>
/// Attempts to purchase the given number of acres.
/// </summary>
/// <returns>
/// The updated game state and action result.
/// </returns>
public static (GameState newState, ActionResult result) BuyLand(GameState state, int amount)
{
var price = state.LandPrice * amount;
if (price < 0)
return (state, ActionResult.Offense);
else
if (price > state.Stores)
return (state, ActionResult.InsufficientStores);
else
return (state with { Acres = state.Acres + amount, Stores = state.Stores - price }, ActionResult.Success);
}
/// <summary>
/// Attempts to sell the given number of acres.
/// </summary>
/// <returns>
/// The updated game state and action result.
/// </returns>
public static (GameState newState, ActionResult result) SellLand(GameState state, int amount)
{
var price = state.LandPrice * amount;
if (price < 0)
return (state, ActionResult.Offense);
else
if (amount >= state.Acres)
return (state, ActionResult.InsufficientLand);
else
return (state with { Acres = state.Acres - amount, Stores = state.Stores + price }, ActionResult.Success);
}
/// <summary>
/// Attempts to feed the people the given number of buschels.
/// </summary>
/// <returns>
/// <returns>
/// The updated game state and action result.
/// </returns>
public static (GameState newState, ActionResult result) FeedPeople(GameState state, int amount)
{
if (amount < 0)
return (state, ActionResult.Offense);
else
if (amount > state.Stores)
return (state, ActionResult.InsufficientStores);
else
return (state with { Stores = state.Stores - amount, FoodDistributed = state.FoodDistributed + amount }, ActionResult.Success);
}
/// <summary>
/// Attempts to plant crops on the given number of acres.
/// </summary>
/// <returns>
/// The updated game state and action result.
/// </returns>
public static (GameState newState, ActionResult result) PlantCrops(GameState state, int amount)
{
var storesRequired = amount / 2;
var maxAcres = state.Population * 10;
if (amount < 0)
return (state, ActionResult.Offense);
else
if (amount > state.Acres)
return (state, ActionResult.InsufficientLand);
else
if (storesRequired > state.Stores)
return (state, ActionResult.InsufficientStores);
else
if ((state.AcresPlanted + amount) > maxAcres)
return (state, ActionResult.InsufficientPopulation);
else
return (state with
{
AcresPlanted = state.AcresPlanted + amount,
Stores = state.Stores - storesRequired,
}, ActionResult.Success);
}
/// <summary>
/// Ends the current turn and returns the updated game state.
/// </summary>
public static GameState EndTurn(GameState state, Random random)
{
var productivity = random.Next(1, 6);
var harvest = productivity * state.AcresPlanted;
var spoilage = random.Next(1, 6) switch
{
2 => state.Stores / 2,
4 => state.Stores / 4,
_ => 0
};
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 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
};
}
/// <summary>
/// Examines the game's history to arrive at the final result.
/// </summary>
public static GameResult GetGameResult(IEnumerable<GameState> history, Random random)
{
var (_, averageStarvationRate, totalStarvation, finalState) = history.Aggregate(
(count: 0, starvationRate: 0, totalStarvation: 0, finalState: default(GameState)),
(stats, state) =>
(
stats.count + 1,
((stats.starvationRate * stats.count) + (state.Starvation * 100 / state.Population)) / (stats.count + 1),
stats.totalStarvation + state.Starvation,
state
));
var acresPerPerson = finalState.Acres / finalState.Population;
var rating = finalState.IsImpeached ?
PerformanceRating.Disgraceful :
(averageStarvationRate, acresPerPerson) switch
{
(> 33, _) => PerformanceRating.Disgraceful,
(_, < 7) => PerformanceRating.Disgraceful,
(> 10, _) => PerformanceRating.Bad,
(_, < 9) => PerformanceRating.Bad,
(> 3, _) => PerformanceRating.Ok,
(_, < 10) => PerformanceRating.Ok,
_ => PerformanceRating.Terrific
};
var assassins = rating == PerformanceRating.Ok ?
random.Next(0, (int)(finalState.Population * 0.8)) : 0;
return new GameResult
{
Rating = rating,
AcresPerPerson = acresPerPerson,
FinalStarvation = finalState.Starvation,
TotalStarvation = totalStarvation,
AverageStarvationRate = averageStarvationRate,
Assassins = assassins,
WasImpeached = finalState.IsImpeached
};
}
}
}

View File

@@ -0,0 +1,193 @@
using System;
namespace Hammurabi
{
/// <summary>
/// Provides various methods for presenting information to the user.
/// </summary>
public static class View
{
/// <summary>
/// Shows the introductory banner to the player.
/// </summary>
public static void ShowBanner()
{
Console.WriteLine(" HAMURABI");
Console.WriteLine(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("TRY YOUR HAND AT GOVERNING ANCIENT SUMERIA");
Console.WriteLine("FOR A TEN-YEAR TERM OF OFFICE.");
}
/// <summary>
/// Shows a summary of the current state of the city.
/// </summary>
public static void ShowCitySummary(GameState state)
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("HAMURABI: I BEG TO REPORT TO YOU,");
Console.WriteLine($"IN YEAR {state.Year}, {state.Starvation} PEOPLE STARVED, {state.PopulationIncrease} CAME TO THE CITY,");
if (state.IsPlagueYear)
{
Console.WriteLine("A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED.");
}
Console.WriteLine($"POPULATION IS NOW {state.Population}");
Console.WriteLine($"THE CITY NOW OWNS {state.Acres} ACRES.");
Console.WriteLine($"YOU HARVESTED {state.Productivity} BUSHELS PER ACRE.");
Console.WriteLine($"THE RATS ATE {state.Spoilage} BUSHELS.");
Console.WriteLine($"YOU NOW HAVE {state.Stores} BUSHELS IN STORE.");
Console.WriteLine();
}
/// <summary>
/// Shows the current cost of land.
/// </summary>
/// <param name="state"></param>
public static void ShowLandPrice(GameState state)
{
Console.WriteLine ($"LAND IS TRADING AT {state.LandPrice} BUSHELS PER ACRE.");
}
/// <summary>
/// Displays a section separator.
/// </summary>
public static void ShowSeparator()
{
Console.WriteLine();
}
/// <summary>
/// Inform the player that he or she has entered an invalid number.
/// </summary>
public static void ShowInvalidNumber()
{
Console.WriteLine("PLEASE ENTER A VALID NUMBER");
}
/// <summary>
/// Inform the player that he or she has insufficient acreage.
/// </summary>
public static void ShowInsufficientLand(GameState state)
{
Console.WriteLine($"HAMURABI: THINK AGAIN. YOU OWN ONLY {state.Acres} ACRES. NOW THEN,");
}
/// <summary>
/// Inform the player that he or she has insufficient population.
/// </summary>
public static void ShowInsufficientPopulation(GameState state)
{
Console.WriteLine($"BUT YOU HAVE ONLY {state.Population} PEOPLE TO TEND THE FIELDS! NOW THEN,");
}
/// <summary>
/// Inform the player that he or she has insufficient grain stores.
/// </summary>
public static void ShowInsufficientStores(GameState state)
{
Console.WriteLine("HAMURABI: THINK AGAIN. YOU HAVE ONLY");
Console.WriteLine($"{state.Stores} BUSHELS OF GRAIN. NOW THEN,");
}
/// <summary>
/// Show the player that he or she has caused great offence.
/// </summary>
public static void ShowGreatOffence()
{
Console.WriteLine();
Console.WriteLine("HAMURABI: I CANNOT DO WHAT YOU WISH.");
Console.WriteLine("GET YOURSELF ANOTHER STEWARD!!!!!");
}
/// <summary>
/// Shows the game's final result to the user.
/// </summary>
public static void ShowGameResult(GameResult result)
{
if (!result.WasImpeached)
{
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($"{result.TotalStarvation} PEOPLE DIED!!");
Console.WriteLine("YOU STARTED WITH 10 ACRES PER PERSON AND ENDED WITH");
Console.WriteLine($"{result.AcresPerPerson} ACRES PER PERSON.");
Console.WriteLine();
}
switch (result.Rating)
{
case PerformanceRating.Disgraceful:
if (result.WasImpeached)
Console.WriteLine($"YOU STARVED {result.FinalStarvation} PEOPLE IN ONE YEAR!!!");
Console.WriteLine("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY");
Console.WriteLine("BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE");
Console.WriteLine("ALSO BEEN DECLARED NATIONAL FINK!!!!");
break;
case PerformanceRating.Bad:
Console.WriteLine("YOUR HEAVY-HANDED PERFORMANCE SMACKS OF NERO AND IVAN IV.");
Console.WriteLine("THE PEOPLE (REMIANING) FIND YOU AN UNPLEASANT RULER, AND,");
Console.WriteLine("FRANKLY, HATE YOUR GUTS!!");
break;
case PerformanceRating.Ok:
Console.WriteLine("YOUR PERFORMANCE COULD HAVE BEEN SOMEWHAT BETTER, BUT");
Console.WriteLine($"REALLY WASN'T TOO BAD AT ALL. {result.Assassins} PEOPLE");
Console.WriteLine("WOULD DEARLY LIKE TO SEE YOU ASSASSINATED BUT WE ALL HAVE OUR");
Console.WriteLine("TRIVIAL PROBLEMS.");
break;
case PerformanceRating.Terrific:
Console.WriteLine("A FANTASTIC PERFORMANCE!!! CHARLEMANGE, DISRAELI, AND");
Console.WriteLine("JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!");
break;
}
}
/// <summary>
/// Shows a farewell message to the user.
/// </summary>
public static void ShowFarewell()
{
Console.WriteLine("SO LONG FOR NOW.");
Console.WriteLine();
}
/// <summary>
/// Prompts the user to buy land.
/// </summary>
public static void PromptBuyLand()
{
Console.WriteLine ("HOW MANY ACRES DO YOU WISH TO BUY");
}
/// <summary>
/// Prompts the user to sell land.
/// </summary>
public static void PromptSellLand()
{
Console.WriteLine("HOW MANY ACRES DO YOU WISH TO SELL");
}
/// <summary>
/// Prompts the user to feed the people.
/// </summary>
public static void PromptFeedPeople()
{
Console.WriteLine("HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE");
}
/// <summary>
/// Prompts the user to plant crops.
/// </summary>
public static void PromptPlantCrops()
{
Console.WriteLine("HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED");
}
}
}