I am looking to have a C# application implement the Konami Code to display an Easter Egg. http://en.wikipedia.org/wiki/Konami_Code
What is the best way to do this?<
The answer can be found in Reactive Extensions. You need a sliding buffer for this to work. Meaning you have to compare the latest ten keystrokes with the Konami code. This works using two different statements
The buffer within RX does both these tasks for us. Buffers the last 10 items and skips 1 (so effectively creates 10 buffers).
var keysIO = Observable.FromEventPattern(this, "KeyDown")
.Select(arg => arg.EventArgs.Key)
.Buffer(10, 1)
.Select(keys => Enumerable.SequenceEqual(keys, _konamiArray))
.Where(result => result)
.Subscribe(i =>
{
Debug.WriteLine("Found Konami");
});
EDIT: Removed the timed solution., too complex
EDIT II: I cracked the time-out solution as well. The beauty of SelectMany :-)
var keysIO = Observable.FromEventPattern(this, "KeyDown")
.Select(e => e.EventArgs.Key)
.Window(10, 1)
.SelectMany(obs => obs.Buffer(TimeSpan.FromSeconds(10), 10))
.Where(keys => Enumerable.SequenceEqual(_konamiArray, keys))
.Subscribe(keys => Debug.Write("Found Konami"));