From ff40494de65c3b45401af7984c972ee39a479016 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Sat, 17 Apr 2021 18:49:34 +1000 Subject: [PATCH 1/2] Add C# project --- .gitignore | 4 ++-- 82 Stars/csharp/Stars.sln | 34 ++++++++++++++++++++++++++++++ 82 Stars/csharp/Stars/Program.cs | 12 +++++++++++ 82 Stars/csharp/Stars/Stars.csproj | 8 +++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 82 Stars/csharp/Stars.sln create mode 100644 82 Stars/csharp/Stars/Program.cs create mode 100644 82 Stars/csharp/Stars/Stars.csproj diff --git a/.gitignore b/.gitignore index fc50f7f2..a186b7a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ */.vs -*/csharp/bin -*/csharp/obj +bin/ +obj/ .idea *.iws diff --git a/82 Stars/csharp/Stars.sln b/82 Stars/csharp/Stars.sln new file mode 100644 index 00000000..5f85e6b7 --- /dev/null +++ b/82 Stars/csharp/Stars.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stars", "Stars\Stars.csproj", "{5832C21C-4DE5-475E-893D-745DA15F1AAC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Debug|x64.ActiveCfg = Debug|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Debug|x64.Build.0 = Debug|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Debug|x86.ActiveCfg = Debug|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Debug|x86.Build.0 = Debug|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Release|Any CPU.Build.0 = Release|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Release|x64.ActiveCfg = Release|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Release|x64.Build.0 = Release|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Release|x86.ActiveCfg = Release|Any CPU + {5832C21C-4DE5-475E-893D-745DA15F1AAC}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/82 Stars/csharp/Stars/Program.cs b/82 Stars/csharp/Stars/Program.cs new file mode 100644 index 00000000..2d53de62 --- /dev/null +++ b/82 Stars/csharp/Stars/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace Stars +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/82 Stars/csharp/Stars/Stars.csproj b/82 Stars/csharp/Stars/Stars.csproj new file mode 100644 index 00000000..20827042 --- /dev/null +++ b/82 Stars/csharp/Stars/Stars.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + From 1236ee8c48deeec172ac2396a910ef795da3daf9 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Mon, 19 Apr 2021 08:59:35 +1000 Subject: [PATCH 2/2] Implement stars game --- 82 Stars/csharp/Stars/Game.cs | 94 ++++++++++++++++++++++++++++++++ 82 Stars/csharp/Stars/Input.cs | 31 +++++++++++ 82 Stars/csharp/Stars/Program.cs | 20 ++++++- 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 82 Stars/csharp/Stars/Game.cs create mode 100644 82 Stars/csharp/Stars/Input.cs diff --git a/82 Stars/csharp/Stars/Game.cs b/82 Stars/csharp/Stars/Game.cs new file mode 100644 index 00000000..7045fe4b --- /dev/null +++ b/82 Stars/csharp/Stars/Game.cs @@ -0,0 +1,94 @@ +using System; + +namespace Stars +{ + internal class Game + { + private readonly int _maxNumber; + private readonly int _maxGuessCount; + private readonly Random _random; + + public Game(int maxNumber, int maxGuessCount) + { + _maxNumber = maxNumber; + _maxGuessCount = maxGuessCount; + _random = new Random(); + } + + internal void DisplayInstructions() + { + if (Input.GetString("Do you want instructions? ").Equals("N", StringComparison.InvariantCultureIgnoreCase)) + { + return; + } + + Console.WriteLine($"I am thinking of a number between 1 and {_maxNumber}."); + Console.WriteLine("Try to guess my number. After you guess, I"); + Console.WriteLine("will type one or more stars (*). The more"); + Console.WriteLine("stars I type, the close you are to my number."); + Console.WriteLine("One star (*) means far away, seven stars (*******)"); + Console.WriteLine($"means really close! You get {_maxGuessCount} guesses."); + } + + internal void Play() + { + Console.WriteLine(); + Console.WriteLine(); + + var target = _random.Next(_maxNumber) + 1; + + Console.WriteLine("Ok, I am thinking of a number. Start guessing."); + + AcceptGuesses(target); + } + + private void AcceptGuesses(int target) + { + for (int guessCount = 1; guessCount <= _maxGuessCount; guessCount++) + { + Console.WriteLine(); + var guess = Input.GetNumber("Your guess? "); + + if (guess == target) + { + DisplayWin(guessCount); + return; + } + + DisplayStars(target, guess); + } + + DisplayLoss(target); + } + + private static void DisplayStars(int target, float guess) + { + var stars = Math.Abs(guess - target) switch + { + >= 64 => "*", + >= 32 => "**", + >= 16 => "***", + >= 8 => "****", + >= 4 => "*****", + >= 2 => "******", + _ => "*******" + }; + + Console.WriteLine(stars); + } + + private static void DisplayWin(int guessCount) + { + Console.WriteLine(); + Console.WriteLine(new string('*', 79)); + Console.WriteLine(); + Console.WriteLine($"You got it in {guessCount} guesses!!! Let's play again..."); + } + + private void DisplayLoss(int target) + { + Console.WriteLine(); + Console.WriteLine($"Sorry, that's {_maxGuessCount} guesses. The number was {target}."); + } + } +} \ No newline at end of file diff --git a/82 Stars/csharp/Stars/Input.cs b/82 Stars/csharp/Stars/Input.cs new file mode 100644 index 00000000..6ba6e3a1 --- /dev/null +++ b/82 Stars/csharp/Stars/Input.cs @@ -0,0 +1,31 @@ +using System; + +namespace Stars +{ + internal static class Input + { + // Float, because that's what the BASIC input operation returns + internal static float GetNumber(string prompt) + { + Console.Write(prompt); + + while (true) + { + var response = Console.ReadLine(); + if (float.TryParse(response, out var value)) + { + return value; + } + + Console.WriteLine("!Number expected - retry input line"); + Console.Write("? "); + } + } + + internal static string GetString(string prompt) + { + Console.Write(prompt); + return Console.ReadLine(); + } + } +} diff --git a/82 Stars/csharp/Stars/Program.cs b/82 Stars/csharp/Stars/Program.cs index 2d53de62..b072595c 100644 --- a/82 Stars/csharp/Stars/Program.cs +++ b/82 Stars/csharp/Stars/Program.cs @@ -6,7 +6,25 @@ namespace Stars { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + DisplayTitle(); + + var game = new Game(maxNumber: 100, maxGuessCount: 7); + + game.DisplayInstructions(); + + while (true) + { + game.Play(); + } + } + + private static void DisplayTitle() + { + Console.WriteLine(" Stars"); + Console.WriteLine(" Creative Computing Morristown, New Jersey"); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); } } }