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?<
Here's a fairly simple and efficient solution:
public class KonamiSequence
{
private static readonly Keys[] KonamiCode = { Keys.Up, Keys.Up, Keys.Down, Keys.Down, Keys.Left, Keys.Right, Keys.Left, Keys.Right, Keys.B, Keys.A };
private readonly Queue _inputKeys = new Queue();
public bool IsCompletedBy(Keys inputKey)
{
_inputKeys.Enqueue(inputKey);
while (_inputKeys.Count > KonamiCode.Length)
_inputKeys.Dequeue();
return _inputKeys.SequenceEqual(KonamiCode);
}
}
Example usage:
private readonly KonamiSequence _konamiSequence = new KonamiSequence();
private void KonamiForm_KeyDown(object sender, KeyEventArgs e)
{
if (_konamiSequence.IsCompletedBy(e.KeyCode))
MessageBox.Show("Konami!");
}