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