Example #1
0
        public void CanRemoveFromAutoDictionary()
        {
            AutoDictionary <string, bool> tested = new AutoDictionary <string, bool>();

            tested["Hello"] = true;
            Assert.AreEqual(true, tested["Hello"]);
            tested.Remove("Hello");
            Assert.AreEqual(false, tested["Hello"]);
        }
Example #2
0
 public bool Remove(KeyValuePair <string, string> item) => inner.Remove(item.Key.ToLower());
Example #3
0
        static void Main()
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.WindowWidth    = 200;
            Console.WindowHeight   = 40;

            var curUser = Environment.UserName;

            curUser = curUser.Substring(0, 1).ToUpper() + curUser.Substring(1).ToLower();
            Settings settings;

            SettingsUtil.LoadSettings(out settings);
            var highscore = settings.Highscores[curUser];

            var recent = new AutoDictionary <string, Highscore>(_ => new Highscore());
            int cur    = 0;
            ConsoleColoredString lastAttemptOutcome = null;

            while (true)
            {
                Console.Clear();
                Console.WriteLine("Current highscores, in milliseconds:");
                WriteToConsole(settings.Highscores.Concat(recent).ToDictionary());

                if (lastAttemptOutcome != null)
                {
                    Console.WriteLine();
                    ConsoleUtil.WriteLine("Last attempt: " + lastAttemptOutcome);
                }

                Console.WriteLine();
                Console.WriteLine("Type every letter A-Z exactly once, either alphabetically or in any order. Wait 1 second when done, or press space to restart.");
                Console.WriteLine();

                var pressed = new Dictionary <char, DateTime>();
                Console.Title = string.Join(" ", Enumerable.Range('A', 26).Select(x => (char)x).Except(pressed.Keys).OrderBy(x => x));
                while (true)
                {
                    var key = Console.ReadKey(true);
                    Console.Write((key.KeyChar + " ").ToUpper());

                    if (key.KeyChar < 'a' || key.KeyChar > 'z')
                    {
                        break;
                    }
                    var chr = char.ToUpper(key.KeyChar);

                    if (pressed.ContainsKey(chr))
                    {
                        break;
                    }
                    pressed[chr]  = DateTime.UtcNow;
                    Console.Title = string.Join(" ", Enumerable.Range('A', 26).Select(x => (char)x).Except(pressed.Keys).OrderBy(x => x));

                    if (pressed.Count == 26)
                    {
                        break;
                    }
                }

                Console.WriteLine();
                Console.WriteLine('\x7');

                if (pressed.Count == 26)
                {
                    Console.WriteLine("Don't press anything now, to confirm you've typed just the 26 letters accurately and nothing else!");
                    var wait = DateTime.UtcNow;
                    while (DateTime.UtcNow < wait + TimeSpan.FromSeconds(1) && !Console.KeyAvailable)
                    {
                        Thread.Sleep(10);
                    }
                }

                if (pressed.Count == 26 && !Console.KeyAvailable)
                {
                    UpdateHighscore(highscore, pressed);
                    cur++;
                    UpdateHighscore(recent[$"Recent: {cur:00}"], pressed);
                    if (recent.ContainsKey($"Recent: {cur - 20:00}"))
                    {
                        recent.Remove($"Recent: {cur - 20:00}");
                    }

                    lastAttemptOutcome = "";
                    char expected   = 'A';
                    bool alphabetic = true;
                    foreach (var kvp in pressed.OrderBy(kvp => kvp.Value))
                    {
                        if (kvp.Key != expected)
                        {
                            alphabetic = false;
                        }
                        lastAttemptOutcome += $"{kvp.Key} ".Color(kvp.Key != expected ? ConsoleColor.Red : alphabetic ? ConsoleColor.Green : ConsoleColor.White);
                        expected            = (char)(kvp.Key + 1);
                    }
                    lastAttemptOutcome = (alphabetic ? "ALPHABETIC" : "any order") + " " + lastAttemptOutcome;

                    settings.Save();
                }
                else
                {
                    lastAttemptOutcome = "OOPS!...".Color(ConsoleColor.Magenta);
                }

                Console.WriteLine();
                Console.WriteLine();
            }
        }