Skip to content

Instantly share code, notes, and snippets.

@dsoverby1986
Last active September 6, 2017 01:21
Show Gist options
  • Save dsoverby1986/18d3bd57694205438f931f7f761937e6 to your computer and use it in GitHub Desktop.
Save dsoverby1986/18d3bd57694205438f931f7f761937e6 to your computer and use it in GitHub Desktop.
C# Number Guessing Game
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace NumberGuessingGame
{
class Program
{
private const string _MIN = "minimum";
private const string _MAX = "maximum";
static void Main(string[] args)
{
Console.WriteLine("Think of a number and keep that number in your mind.");
Console.WriteLine("Now, provide a number range, from which the program should guess to find the number you're thinking of...");
NumberGuess.GetGuessingRangeFromUser(_MIN, 0);
NumberGuess.GuessNumber();
NumberGuess.CheckIfUserWantsToPlayAgain(0);
}
public static class NumberGuess
{
private static int? GuessRangeMinimum { get; set; }
private static int? GuessRangeMaximum { get; set; }
private static List<int> GuessRange { get; set; }
private static List<int> GuessedNumbers { get; set; } = new List<int>();
private const string _INVALID_INPUT_MESSAGE = "Your input is invalid. Try again";
public static void GetGuessingRangeFromUser(string rangePosition, int attempts)
{
if (attempts > 0)
Console.WriteLine(_INVALID_INPUT_MESSAGE);
Console.WriteLine($"Enter a whole number that is the {rangePosition} number in the range of possible numbers to guess from...");
string response = Console.ReadLine();
int num;
if (Int32.TryParse(response, out num))
{
if (rangePosition == _MIN)
{
GuessRangeMinimum = num;
GetGuessingRangeFromUser(_MAX, 0);
}
else if (rangePosition == _MAX)
{
if (num > GuessRangeMinimum)
{
GuessRangeMaximum = num + 1;
GuessRange = new List<int>(Enumerable.Range((int)GuessRangeMinimum, (int)GuessRangeMaximum - (int)GuessRangeMinimum));
}
else
GetGuessingRangeFromUser(rangePosition, ++attempts);
}
}
else
GetGuessingRangeFromUser(rangePosition, ++attempts);
}
public static void GuessNumber()
{
if (!AllPossibleNumbersHaveBeenGuessed())
{
Random rdm = new Random();
int number;
do
number = rdm.Next((int)GuessRangeMinimum, (int)GuessRangeMaximum);
while (GuessedNumbers.Contains(number));
GuessedNumbers.Add(number);
PresentGuessToUser(number, 0);
}
else
Console.WriteLine("Well, I think you must have forgotten your number, because I've guessed all the numbers in the range that you specified.");
}
private static void PresentGuessToUser(int guess, int attempts)
{
if (attempts > 0)
Console.WriteLine(_INVALID_INPUT_MESSAGE);
Console.WriteLine($"Is {guess} the number you're thinking of? (y/n)");
string response = Console.ReadLine().ToLower();
if(response.IsYes())
Console.WriteLine("Hooray! I guessed it!");
else if (response.IsNo())
GuessNumber();
else
PresentGuessToUser(guess, ++attempts);
}
private static bool AllPossibleNumbersHaveBeenGuessed() =>
GuessedNumbers.Count == GuessRange.Count;
public static void CheckIfUserWantsToPlayAgain(int attempts)
{
if (attempts > 0)
Console.WriteLine(_INVALID_INPUT_MESSAGE);
Console.WriteLine("Would you like to play again? (y/n)");
string response = Console.ReadLine().ToLower();
if (response.IsYes())
{
ResetRange();
Main(new string[] { });
}
else if (response.IsNo())
{
Console.WriteLine("Thanks for playing! I hope you enjoyed it!\nPress any key to exit...");
Console.ReadKey();
}
else
CheckIfUserWantsToPlayAgain(++attempts);
}
private static void ResetRange()
{
GuessRangeMaximum = null;
GuessRangeMinimum = null;
GuessRange.Clear();
GuessedNumbers.Clear();
}
}
}
public static class StringExtensions
{
public static bool IsYes(this string response) =>
Regex.IsMatch(response, @"\b(y|yes)\b");
public static bool IsNo(this string response) =>
Regex.IsMatch(response, @"\b(n|no)\b");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment