Skip to content

Instantly share code, notes, and snippets.

@SecretDeveloper
Created October 20, 2022 13:15
Show Gist options
  • Save SecretDeveloper/1d23d6787dc83e994652abdf270b30f2 to your computer and use it in GitHub Desktop.
Save SecretDeveloper/1d23d6787dc83e994652abdf270b30f2 to your computer and use it in GitHub Desktop.
Console game where you guess the frequency of the beep.
Console.WriteLine("Guess the frequency");
var random = new Random();
var playing = true;
var MIN = 37;
var MAX = 5000;
var threshold = MAX / 20;
var test = 37;
while (test <= MAX)
{
Console.WriteLine($"This is {test}hz.");
Console.Beep(test, 1000);
Thread.Sleep(500);
if (test == MIN)
{
test = 500;
continue;
}
test += 500;
}
while (playing)
{
Console.WriteLine("Enter a number between 1 and 5000.");
Console.WriteLine("Press Enter to hear it again or q to quit.");
Console.Write("Guess: ");
Thread.Sleep(1000);
var freq = random.Next(MIN, MAX);
var lives = 3;
while (lives > 0)
{
Console.Beep(freq, 1000);
var guess = Console.ReadLine() ?? "";
if (guess == "")
{
Console.Write("Playing again, what is it?: ");
continue; // play it again sam.
}
if (guess.Trim().Equals("q", StringComparison.InvariantCultureIgnoreCase))
{
playing = false;
break;
}
if (int.TryParse(guess, out int value))
{
if (value > freq - threshold && value < freq + threshold)
{
Console.WriteLine($"Yes! The frequency was {freq}.");
break;
}
else if (value < freq - threshold) Console.WriteLine("Too low!");
else if (value > freq + threshold) Console.WriteLine("Too high!");
}
Console.WriteLine($"Nope. {lives} lives remaining.");
lives--;
if (lives == 0)
{
Console.WriteLine("DEAD");
Console.WriteLine($"Frequency was {freq}");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment