mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Implement stars game
This commit is contained in:
94
82 Stars/csharp/Stars/Game.cs
Normal file
94
82 Stars/csharp/Stars/Game.cs
Normal file
@@ -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}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
31
82 Stars/csharp/Stars/Input.cs
Normal file
31
82 Stars/csharp/Stars/Input.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user