Skip to content

Instantly share code, notes, and snippets.

@BytewaveMLP
Created August 18, 2017 17:35
Show Gist options
  • Save BytewaveMLP/433221fb61e08f1fe4b9bf3abca57318 to your computer and use it in GitHub Desktop.
Save BytewaveMLP/433221fb61e08f1fe4b9bf3abca57318 to your computer and use it in GitHub Desktop.
using System;
namespace GuessTheNumber {
class Program {
const int MAX_VALUE = 100;
static void Main(string[] args) {
Console.WriteLine("Guess the Number!");
Console.Write("Would you like to play? [y/n]: ");
// Declare input, and assign it the value of whatever the user types
string input = Console.ReadLine();
// Seed our random number generator
// (You should only ever do this once per program lifecycle, unless you have a reason to make more than one)
Random rnd = new Random();
// Ensures we actually got something from ReadLine, and that what we got is y or Y
// string.Equals is the same thing as == if you're comparing two strings, but it lets you pass in some extra options
// For instance, StringComparsion.InvariantCultureIgnoreCase is a (very long) option that basically says the strings only have
// to match by contents, but not by case. So, comparing Y and y would be true!
// The extra check, input != null, is required to make sure input actually exists, because Console.ReadLine may return null
// in some rare cases.
while (input != null && input.Equals("y", StringComparison.InvariantCultureIgnoreCase)) {
// The user hasn't won when the game starts, of course
// This could just as easily be called notWin and be true, which would save me the ! later here, but
// I prefer calling it win cus I'm weird
bool win = false;
int guesses = 0;
// Random.Next's upper bound is EXCLUSIVE, so we need to add 1 to our MAX_VALUE to get a range of [1, MAX_VALUE]
int num = rnd.Next(1, MAX_VALUE + 1);
// While the user has **not** won...
while (!win) {
// Increment our guess counter
guesses++;
// The number the user entered
int inNum = 0;
// Whether or not the text the user inputted was a number
bool valid = false;
// While the user has **not** inputted a valid number...
while (!valid) {
Console.Write($"What number am I thinking of? [1-{MAX_VALUE}]: ");
input = Console.ReadLine();
valid = Int32.TryParse(input, out inNum);
// Bounds checking - ensures 1 <= inNum <= MAX_VALUE
if (valid) {
valid = inNum >= 1 && inNum <= MAX_VALUE;
}
// Fancy error message - uses Console.Error
// Console actually has two outputs: normal out (standard out, stdout), which is what Console.WriteLine defaults to,
// and error (standard error, stderr), which we can access using Console.Error.Write/WriteLine
if (!valid) {
Console.Error.WriteLine("Invalid/out-of-range number entered. Try again!");
}
}
if (inNum == num) {
Console.WriteLine("You win!");
win = true;
} else if (inNum < num) {
Console.WriteLine("Too low!");
} else {
// The else case above is the same as writing else if (inNum > num), but programmers are lazy
Console.WriteLine("Too high!");
}
}
Console.WriteLine($"That took you {guesses} guesses.");
// Re-prompt the user if they'd like to play
Console.Write("Would you like to play agin? [y/n]: ");
input = Console.ReadLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment