diff --git a/61 Math Dice/csharp/GameState.cs b/61 Math Dice/csharp/GameState.cs new file mode 100644 index 00000000..ef538781 --- /dev/null +++ b/61 Math Dice/csharp/GameState.cs @@ -0,0 +1,8 @@ +namespace MathDice +{ + public enum GameState + { + FirstAttempt = 0, + SecondAttempt = 1, + } +} diff --git a/61 Math Dice/csharp/MathDice.csproj b/61 Math Dice/csharp/MathDice.csproj new file mode 100644 index 00000000..38aa472a --- /dev/null +++ b/61 Math Dice/csharp/MathDice.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + MathDice + + + diff --git a/61 Math Dice/csharp/MathDice.sln b/61 Math Dice/csharp/MathDice.sln new file mode 100644 index 00000000..4efc8c9c --- /dev/null +++ b/61 Math Dice/csharp/MathDice.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31025.194 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MathDice", "MathDice.csproj", "{4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A1337DCA-4EFA-45EE-A1A9-22E37E74F362} + EndGlobalSection +EndGlobal diff --git a/61 Math Dice/csharp/Program.cs b/61 Math Dice/csharp/Program.cs new file mode 100644 index 00000000..e755f1f2 --- /dev/null +++ b/61 Math Dice/csharp/Program.cs @@ -0,0 +1,121 @@ +using System; + +namespace MathDice +{ + public static class Program + { + readonly static Random random = new Random(); + + static int DieOne = 0; + static int DieTwo = 0; + + private const string NoPips = "I I"; + private const string LeftPip = "I * I"; + private const string CentrePip = "I * I"; + private const string RightPip = "I * I"; + private const string TwoPips = "I * * I"; + private const string Edge = " ----- "; + + static void Main(string[] args) + { + int answer; + + GameState gameState = GameState.FirstAttempt; + + Console.WriteLine("MATH DICE".CentreAlign()); + Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".CentreAlign()); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("THIS PROGRAM GENERATES SUCCESSIVE PICTURES OF TWO DICE."); + Console.WriteLine("WHEN TWO DICE AND AN EQUAL SIGN FOLLOWED BY A QUESTION"); + Console.WriteLine("MARK HAVE BEEN PRINTED, TYPE YOUR ANSWER AND THE RETURN KEY."); + Console.WriteLine("TO CONCLUDE THE LESSON, TYPE CONTROL-C AS YOUR ANSWER."); + Console.WriteLine(); + Console.WriteLine(); + + while (true) + { + if (gameState == GameState.FirstAttempt) + { + Roll(ref DieOne); + Roll(ref DieTwo); + + DrawDie(DieOne); + Console.WriteLine(" +"); + DrawDie(DieTwo); + } + + answer = GetAnswer(); + + if (answer == DieOne + DieTwo) + { + Console.WriteLine("RIGHT!"); + Console.WriteLine(); + Console.WriteLine("THE DICE ROLL AGAIN..."); + + gameState = GameState.FirstAttempt; + } + else + { + if (gameState == GameState.FirstAttempt) + { + Console.WriteLine("NO, COUNT THE SPOTS AND GIVE ANOTHER ANSWER."); + gameState = GameState.SecondAttempt; + } + else + { + Console.WriteLine($"NO, THE ANSWER IS{DieOne + DieTwo}"); + Console.WriteLine(); + Console.WriteLine("THE DICE ROLL AGAIN..."); + gameState = GameState.FirstAttempt; + } + } + } + } + + private static int GetAnswer() + { + int answer; + + Console.Write(" =?"); + var input = Console.ReadLine(); + + int.TryParse(input, out answer); + + return answer; + } + + private static void DrawDie(int pips) + { + Console.WriteLine(Edge); + Console.WriteLine(OuterRow(pips, true)); + Console.WriteLine(CentreRow(pips)); + Console.WriteLine(OuterRow(pips, false)); + Console.WriteLine(Edge); + Console.WriteLine(); + } + + private static void Roll(ref int die) => die = random.Next(1, 7); + + private static string OuterRow(int pips, bool top) + { + return pips switch + { + 1 => NoPips, + var x when x == 2 || x == 3 => top ? LeftPip : RightPip, + _ => TwoPips + }; + } + + private static string CentreRow(int pips) + { + return pips switch + { + var x when x == 2 || x == 4 => NoPips, + 6 => TwoPips, + _ => CentrePip + }; + } + } +} diff --git a/61 Math Dice/csharp/README.md b/61 Math Dice/csharp/README.md index 4daabb5c..7017b45a 100644 --- a/61 Math Dice/csharp/README.md +++ b/61 Math Dice/csharp/README.md @@ -1,3 +1,10 @@ Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) Conversion to [Microsoft C#](https://docs.microsoft.com/en-us/dotnet/csharp/) + +Conversion Notes + +- There are minor spacing issues which have been preserved in this port. +- This implementation uses switch expressions to concisely place the dice pips in the right place. +- Random() is only pseudo-random but perfectly adequate for the purposes of simulating dice rolls. +- Console width is assumed to be 120 chars for the purposes of centrally aligned the intro text. \ No newline at end of file diff --git a/61 Math Dice/csharp/StringExtensions.cs b/61 Math Dice/csharp/StringExtensions.cs new file mode 100644 index 00000000..b87ed30c --- /dev/null +++ b/61 Math Dice/csharp/StringExtensions.cs @@ -0,0 +1,15 @@ +namespace MathDice +{ + public static class StringExtensions + { + private const int ConsoleWidth = 120; // default console width + + public static string CentreAlign(this string value) + { + int spaces = ConsoleWidth - value.Length; + int leftPadding = spaces / 2 + value.Length; + + return value.PadLeft(leftPadding).PadRight(ConsoleWidth); + } + } +}