mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Merge pull request #282 from drewjcooper/csharp-82-stars
C# 82 Stars implementation
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,7 +1,7 @@
|
||||
*/.vs
|
||||
|
||||
*/csharp/bin
|
||||
*/csharp/obj
|
||||
bin/
|
||||
obj/
|
||||
|
||||
.idea
|
||||
*.iws
|
||||
|
||||
34
82 Stars/csharp/Stars.sln
Normal file
34
82 Stars/csharp/Stars.sln
Normal file
@@ -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
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
30
82 Stars/csharp/Stars/Program.cs
Normal file
30
82 Stars/csharp/Stars/Program.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
|
||||
namespace Stars
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
8
82 Stars/csharp/Stars/Stars.csproj
Normal file
8
82 Stars/csharp/Stars/Stars.csproj
Normal file
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user