mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Update 82_Stars with common library
This commit is contained in:
@@ -94,6 +94,13 @@ public interface IReadWrite
|
||||
/// <param name="value">The <see cref="object" /> to be written.</param>
|
||||
void WriteLine(object value);
|
||||
|
||||
/// <summary>
|
||||
/// Writes a formatted string to output.
|
||||
/// </summary>
|
||||
/// <param name="format">The format <see cref="string" /> to be written.</param>
|
||||
/// <param name="value">The values to be inserted into the format.</param>
|
||||
void WriteLine(string format, params object[] values);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a <see cref="Stream" /> to output.
|
||||
/// </summary>
|
||||
|
||||
@@ -99,6 +99,8 @@ public class TextIO : IReadWrite
|
||||
|
||||
public void WriteLine(object value) => _output.WriteLine(value.ToString());
|
||||
|
||||
public void WriteLine(string format, params object[] values) => _output.WriteLine(format, values);
|
||||
|
||||
public void Write(Stream stream, bool keepOpen = false)
|
||||
{
|
||||
using var reader = new StreamReader(stream);
|
||||
|
||||
@@ -1,43 +1,55 @@
|
||||
using System;
|
||||
using Games.Common.IO;
|
||||
using Games.Common.Randomness;
|
||||
using Stars.Resources;
|
||||
|
||||
namespace Stars
|
||||
namespace Stars;
|
||||
|
||||
internal class Game
|
||||
{
|
||||
internal class Game
|
||||
{
|
||||
private readonly TextIO _io;
|
||||
private readonly IRandom _random;
|
||||
private readonly int _maxNumber;
|
||||
private readonly int _maxGuessCount;
|
||||
private readonly Random _random;
|
||||
|
||||
public Game(int maxNumber, int maxGuessCount)
|
||||
public Game(TextIO io, IRandom random, int maxNumber, int maxGuessCount)
|
||||
{
|
||||
_io = io;
|
||||
_random = random;
|
||||
_maxNumber = maxNumber;
|
||||
_maxGuessCount = maxGuessCount;
|
||||
_random = new Random();
|
||||
}
|
||||
|
||||
internal void DisplayInstructions()
|
||||
internal void Play(Func<bool> playAgain)
|
||||
{
|
||||
if (Input.GetString("Do you want instructions? ").Equals("N", StringComparison.InvariantCultureIgnoreCase))
|
||||
DisplayIntroduction();
|
||||
|
||||
do
|
||||
{
|
||||
Play();
|
||||
} while (playAgain.Invoke());
|
||||
}
|
||||
|
||||
private void DisplayIntroduction()
|
||||
{
|
||||
_io.Write(Resource.Streams.Title);
|
||||
|
||||
if (_io.ReadString("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.");
|
||||
_io.WriteLine(Resource.Formats.Instructions, _maxNumber, _maxGuessCount);
|
||||
}
|
||||
|
||||
internal void Play()
|
||||
private void Play()
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
_io.WriteLine();
|
||||
_io.WriteLine();
|
||||
|
||||
var target = _random.Next(_maxNumber) + 1;
|
||||
|
||||
Console.WriteLine("Ok, I am thinking of a number. Start guessing.");
|
||||
_io.WriteLine("Ok, I am thinking of a number. Start guessing.");
|
||||
|
||||
AcceptGuesses(target);
|
||||
}
|
||||
@@ -46,8 +58,8 @@ namespace Stars
|
||||
{
|
||||
for (int guessCount = 1; guessCount <= _maxGuessCount; guessCount++)
|
||||
{
|
||||
Console.WriteLine();
|
||||
var guess = Input.GetNumber("Your guess? ");
|
||||
_io.WriteLine();
|
||||
var guess = _io.ReadNumber("Your guess");
|
||||
|
||||
if (guess == target)
|
||||
{
|
||||
@@ -61,7 +73,7 @@ namespace Stars
|
||||
DisplayLoss(target);
|
||||
}
|
||||
|
||||
private static void DisplayStars(int target, float guess)
|
||||
private void DisplayStars(int target, float guess)
|
||||
{
|
||||
var stars = Math.Abs(guess - target) switch
|
||||
{
|
||||
@@ -74,21 +86,20 @@ namespace Stars
|
||||
_ => "*******"
|
||||
};
|
||||
|
||||
Console.WriteLine(stars);
|
||||
_io.WriteLine(stars);
|
||||
}
|
||||
|
||||
private static void DisplayWin(int guessCount)
|
||||
private 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...");
|
||||
_io.WriteLine();
|
||||
_io.WriteLine(new string('*', 79));
|
||||
_io.WriteLine();
|
||||
_io.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}.");
|
||||
}
|
||||
_io.WriteLine();
|
||||
_io.WriteLine($"Sorry, that's {_maxGuessCount} guesses. The number was {target}.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,7 @@
|
||||
using System;
|
||||
using Games.Common.IO;
|
||||
using Games.Common.Randomness;
|
||||
using Stars;
|
||||
|
||||
namespace Stars
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
DisplayTitle();
|
||||
var game = new Game(new ConsoleIO(), new RandomNumberGenerator(), maxNumber: 100, maxGuessCount: 7);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
game.Play(() => true);
|
||||
|
||||
6
82_Stars/csharp/Resources/Instructions.txt
Normal file
6
82_Stars/csharp/Resources/Instructions.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
I am thinking of a number between 1 and {0}.
|
||||
Try to guess my number. After you guess, I
|
||||
will type one or more stars (*). The more
|
||||
stars I type, the closer you are to my number.
|
||||
One star (*) means far away, seven stars (*******)
|
||||
means really close! You get {1} guesses.
|
||||
28
82_Stars/csharp/Resources/Resource.cs
Normal file
28
82_Stars/csharp/Resources/Resource.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Stars.Resources;
|
||||
|
||||
internal static class Resource
|
||||
{
|
||||
internal static class Streams
|
||||
{
|
||||
public static Stream Title => GetStream();
|
||||
}
|
||||
|
||||
internal static class Formats
|
||||
{
|
||||
public static string Instructions => GetString();
|
||||
}
|
||||
|
||||
private static string GetString([CallerMemberName] string name = null)
|
||||
{
|
||||
using var stream = GetStream(name);
|
||||
using var reader = new StreamReader(stream);
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
private static Stream GetStream([CallerMemberName] string name = null)
|
||||
=> Assembly.GetExecutingAssembly().GetManifestResourceStream($"Stars.Resources.{name}.txt");
|
||||
}
|
||||
5
82_Stars/csharp/Resources/Title.txt
Normal file
5
82_Stars/csharp/Resources/Title.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
Stars
|
||||
Creative Computing Morristown, New Jersey
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,15 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Resources/*.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\00_Common\dotnet\Games.Common\Games.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user