Merge pull request #205 from Tanjecterly/main

C# port of MathDice
This commit is contained in:
Jeff Atwood
2021-03-06 10:47:05 -08:00
committed by GitHub
6 changed files with 185 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
namespace MathDice
{
public enum GameState
{
FirstAttempt = 0,
SecondAttempt = 1,
}
}

View File

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

View File

@@ -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

View File

@@ -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
};
}
}
}

View File

@@ -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.

View File

@@ -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);
}
}
}