Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Created October 23, 2015 15:28
Show Gist options
  • Select an option

  • Save hyrmn/39efd6ed099e7d7f4a3a to your computer and use it in GitHub Desktop.

Select an option

Save hyrmn/39efd6ed099e7d7f4a3a to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace ConsoleApplication28
{
internal class Program
{
public delegate bool GetNum(string prompt, out int val);
private static readonly Dictionary<GuessAnswerKey, Func<int, string>> answerMap = new Dictionary
<GuessAnswerKey, Func<int, string>>
{
{new GuessAnswerKey(true, true), number => "Something is like really bad"},
{new GuessAnswerKey(true, false), number => "Too low"},
{new GuessAnswerKey(false, true), number => "Too high"},
{new GuessAnswerKey(false, false), number => string.Format("You guess correctly! You guessed {0}", number)},
};
private static readonly GetNum Reader = (string prompt, out int val) =>
{
Console.Write(prompt);
return int.TryParse(Console.ReadLine(), out val);
};
private static void Main(string[] args)
{
var numToGuess = 6; // random number, chosen by fair dice roll
Console.WriteLine("Enter a number between 1 and 100. Anything else to quit");
int guess;
Reader("Guess: ", out guess);
do
{
Console.WriteLine(answerMap[new GuessAnswerKey(guess < numToGuess, guess > numToGuess)](guess));
} while (guess != numToGuess && Reader("Guess: ", out guess));
}
internal struct GuessAnswerKey
{
internal GuessAnswerKey(bool guessTooLow, bool guessTooHigh)
: this()
{
GuessTooLow = guessTooLow;
GuessTooHigh = guessTooHigh;
}
internal bool GuessTooLow { get; private set; }
internal bool GuessTooHigh { get; private set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment