MiniHack 2: what Morse Code and Caps Lock have in common

I only write about MiniHack 2 as unfortunately I missed MiniHack 1 and most probably will miss MiniHack 3. MiniHack (https://www.meetup.com/mini-hack/) is a nice experience for somebody who has a vacation, but is not able to travel or see friends, because of Corona restrictions. Also, for people who want to have fun meeting others and solving puzzles. So, I brought my usual activity from my workdays in my calm evening: hacking. “Why are you not programming in your workdays?”, you might ask – well according to Google, hacking happens during “an event in which a large number of people meet to engage in collaborative computer programming” – and that is what I mostly do at work. And when I write code alone, I would rather call it side hustling 😉

We were randomly assigned in groups of up to 4 people and given the task: create a game with no graphics. That means we shouldn’t draw anything on the screen, even using symbols in the console. We could, however, take input and output words to the console, output sound, make an IOT device turn on/ off etc. Also, we had around 90 minutes to produce something that works.

After short intro I was assigned to the team Orange and we started brainstorming. My idea was to use the flashlight you would find next to the web camera of any decent laptop and make it blink in the Morse Code (https://en.wikipedia.org/wiki/Morse_code). That turned out to be quite tricky, because of manufacturer and privacy stuff and so we went for the small light some computers have on their Caps Lock button. Programmatically you can turn Caps Lock on and off and that is all we did.

Our program will greet you in the console, blink the Caps Lock light and ask you to decode the Morse Code message and write it back to the console. If you succeed it will continue asking for more words, gradually increasing the speed of the blinking light.

Here is our code. I decided to leave it as is, so you can improve it as you want.

//imports...

namespace ConsoleApp29
{

    class Program
    {
        [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

        static void Main(string[] args)
        {
            var words = new Dictionary()
            {
                ["DAVID"] = "-.. .- ...- .. -..",
                ["BORIS"] = "-... --- .-. .. ...",
                ["GAVIN"] = "--. .- ...- .. -.",
                ["NUH"] = "-. ..- ....",
                ["MILES"] = "-- .. .-.. . ...",
            };

            var random = new Random();
            var level = 1;

            Console.WriteLine("Please enter a word ... or type EXIT to leave the program");
            var userWord = string.Empty;

            while (userWord.ToUpper() != "EXIT")
            {
                var result = random.Next(0, words.Count());
                var word = words.Keys.ElementAt(result);
                var code = words[word];

                StrToBlinkConverter(code, level);

                userWord = Console.ReadLine();

                if(userWord.Equals(word, StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Congrats! You got the next word!");
                    level++;
                }
                else
                {
                    Console.WriteLine("That was wrong. Keep practising :)");
                }
            }
        }

        private static void TurnOff(double seconds)
        {
            const int KEYEVENTF_EXTENDEDKEY = 0x1;
            const int KEYEVENTF_KEYUP = 0x2;
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
            (UIntPtr)0);
            Thread.Sleep(TimeSpan.FromSeconds(seconds));
        }

        private static void TurnOn(double seconds)
        {
            const int KEYEVENTF_EXTENDEDKEY = 0x1;
            const int KEYEVENTF_KEYUP = 0x1;
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
            (UIntPtr)0);
            Thread.Sleep(TimeSpan.FromSeconds(seconds));

        }

        private static void StrToBlinkConverter(string word, int level)
        {
            foreach (var letter in word)
            {
                switch (letter)
                {
                    case '.':
                        TurnOn(2 / (double)level);
                        TurnOff(1 / (double)level);
                        break;
                    case '-':
                        TurnOn(4 / (double)level);
                        TurnOff(1 / (double)level);
                        break;
                    default:
                        TurnOff(1 / (double)level);
                        break;
                }
            }

            Console.Write("? ");
        }
    }
}

We actually won this edition of MiniHack, and I got this nice present:


Many thanks to Paul Michaels for organizing it.


0

About: user


Leave a Reply

Your email address will not be published. Required fields are marked *